github.com/aergoio/aergo@v1.3.1/tools/experiments/multiaddr_test.go (about)

     1  /**
     2   *  @file
     3   *  @copyright defined in aergo/LICENSE.txt
     4   */
     5  package experiments
     6  
     7  import (
     8  	"fmt"
     9  	"testing"
    10  
    11  	ma "github.com/multiformats/go-multiaddr"
    12  )
    13  
    14  func Test_Addr(t *testing.T) {
    15  	// construct from a string (err signals parse failure)
    16  	m1, err := ma.NewMultiaddr("/ip4/127.0.0.1/udp/1234")
    17  	fmt.Println("M1 " + m1.String())
    18  	// construct from bytes (err signals parse failure)
    19  	m2, err := ma.NewMultiaddrBytes(m1.Bytes())
    20  
    21  	// true
    22  	if m1.String() != ("/ip4/127.0.0.1/udp/1234") {
    23  		t.Errorf("invalide parsing ")
    24  	}
    25  	if m1.String() != m2.String() {
    26  		t.Errorf("invalide parsing ")
    27  	}
    28  	// get the multiaddr protocol description objects
    29  	protocols := m1.Protocols()
    30  	fmt.Println(protocols)
    31  	// []Protocol{
    32  	//   Protocol{ Code: 4, Name: 'ip4', Size: 32},
    33  	//   Protocol{ Code: 17, Name: 'udp', Size: 16},
    34  	// }
    35  	toDecap, err := ma.NewMultiaddr("/sctp/5678")
    36  	if err != nil {
    37  		t.Errorf("Wrong addr string %s , %s", "/sctp/5678", err.Error())
    38  	}
    39  	encResult := m1.Encapsulate(toDecap)
    40  	fmt.Println("encap result " + encResult.String())
    41  	// <Multiaddr /ip4/127.0.0.1/udp/1234/sctp/5678>
    42  	udp, err := ma.NewMultiaddr("/udp/9876")
    43  	if err != nil {
    44  		t.Errorf("Wrong addr string %s , %s", "/udp", err.Error())
    45  	}
    46  	decapReult := m1.Decapsulate(udp) // up to + inc last occurrence of subaddr
    47  	fmt.Println("decap result " + decapReult.String())
    48  
    49  	// <Multiaddr /ip4/127.0.0.1>
    50  
    51  }