github.com/aergoio/aergo@v1.3.1/p2p/p2pcommon/peermeta_test.go (about)

     1  /*
     2   * @file
     3   * @copyright defined in aergo/LICENSE.txt
     4   */
     5  
     6  package p2pcommon
     7  
     8  import (
     9  	"testing"
    10  
    11  	"github.com/aergoio/aergo/types"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestFromPeerAddress(t *testing.T) {
    16  	type args struct {
    17  		ip   string
    18  		port uint32
    19  		id   string
    20  	}
    21  	tests := []struct {
    22  		name string
    23  		args args
    24  	}{
    25  		{"t1", args{"192.168.1.2", 2, "id0002"}},
    26  		{"t2", args{"0.0.0.0", 2223, "id2223"}},
    27  		{"t3", args{"2001:0db8:85a3:08d3:1319:8a2e:0370:7334", 444, "id0002"}},
    28  		{"t4", args{"::ffff:192.0.1.2", 444, "id0002"}},
    29  		// TODO: Add test cases.
    30  	}
    31  	for _, tt := range tests {
    32  		t.Run(tt.name, func(t *testing.T) {
    33  			ipAddr := tt.args.ip
    34  			addr := &types.PeerAddress{Address: ipAddr, Port: tt.args.port, PeerID: []byte(tt.args.id)}
    35  			actual := FromPeerAddress(addr)
    36  			assert.Equal(t, ipAddr, actual.IPAddress)
    37  			assert.Equal(t, tt.args.port, actual.Port)
    38  			assert.Equal(t, tt.args.id, string(actual.ID))
    39  
    40  			actual2 := actual.ToPeerAddress()
    41  			assert.Equal(t, *addr, actual2)
    42  		})
    43  	}
    44  }
    45  
    46  func TestNewMetaFromStatus(t *testing.T) {
    47  	type args struct {
    48  		ip       string
    49  		port     uint32
    50  		id       string
    51  		noExpose bool
    52  		outbound bool
    53  	}
    54  	tests := []struct {
    55  		name string
    56  		args args
    57  	}{
    58  		{"TExpose", args{"192.168.1.2", 2, "id0002", false, false}},
    59  		{"TNoExpose", args{"0.0.0.0", 2223, "id2223", true, false}},
    60  		{"TOutbound", args{"2001:0db8:85a3:08d3:1319:8a2e:0370:7334", 444, "id0002", false, true}},
    61  	}
    62  	for _, tt := range tests {
    63  		t.Run(tt.name, func(t *testing.T) {
    64  			sender := &types.PeerAddress{Address: tt.args.ip, Port: tt.args.port, PeerID: []byte(tt.args.id)}
    65  			status := &types.Status{Sender: sender, NoExpose: tt.args.noExpose}
    66  			actual := NewMetaFromStatus(status, tt.args.outbound)
    67  			assert.Equal(t, tt.args.ip, actual.IPAddress)
    68  			assert.Equal(t, tt.args.port, actual.Port)
    69  			assert.Equal(t, tt.args.id, string(actual.ID))
    70  			assert.Equal(t, tt.args.noExpose, actual.Hidden)
    71  			assert.Equal(t, tt.args.outbound, actual.Outbound)
    72  
    73  			actual2 := actual.ToPeerAddress()
    74  			assert.Equal(t, *sender, actual2)
    75  		})
    76  	}
    77  }