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

     1  /*
     2   * @file
     3   * @copyright defined in aergo/LICENSE.txt
     4   */
     5  
     6  package p2p
     7  
     8  import (
     9  	"github.com/aergoio/aergo/config"
    10  	"github.com/aergoio/aergo/types"
    11  	"github.com/stretchr/testify/assert"
    12  	"testing"
    13  )
    14  
    15  func Test_initMeta(t *testing.T) {
    16  	samplePeerID, _ := types.IDB58Decode("16Uiu2HAmFqptXPfcdaCdwipB2fhHATgKGVFVPehDAPZsDKSU7jRm")
    17  
    18  	type args struct {
    19  		peerID   types.PeerID
    20  		noExpose bool
    21  	}
    22  	tests := []struct {
    23  		name string
    24  		conf *config.P2PConfig
    25  
    26  		args args
    27  
    28  		wantSameAddr bool
    29  		wantPort     uint32
    30  		wantID       types.PeerID
    31  		wantHidden   bool
    32  	}{
    33  		{"TIP6", &config.P2PConfig{NetProtocolAddr: "fe80::dcbf:beff:fe87:e30a", NetProtocolPort: 7845, NPExposeSelf:true}, args{samplePeerID, false}, true, 7845, samplePeerID, false},
    34  		{"TIP4", &config.P2PConfig{NetProtocolAddr: "211.1.1.2", NetProtocolPort: 7845, NPExposeSelf:true}, args{samplePeerID, false}, true, 7845, samplePeerID, false},
    35  		{"TDN", &config.P2PConfig{NetProtocolAddr: "www.aergo.io", NetProtocolPort: 7845, NPExposeSelf:true}, args{samplePeerID, false}, true, 7845, samplePeerID, false},
    36  		{"TDefault", &config.P2PConfig{NetProtocolAddr: "", NetProtocolPort: 7845, NPExposeSelf:true}, args{samplePeerID, false}, false, 7845, samplePeerID, false},
    37  		{"THidden", &config.P2PConfig{NetProtocolAddr: "211.1.1.2", NetProtocolPort: 7845, NPExposeSelf:false}, args{samplePeerID, true}, true, 7845, samplePeerID, true},
    38  	}
    39  	for _, tt := range tests {
    40  		t.Run(tt.name, func(t *testing.T) {
    41  			got := SetupSelfMeta(tt.args.peerID, tt.conf)
    42  
    43  			if tt.wantSameAddr {
    44  				assert.Equal(t, tt.conf.NetProtocolAddr, got.IPAddress)
    45  			} else {
    46  				assert.NotEqual(t, tt.conf.NetProtocolAddr, got.IPAddress)
    47  			}
    48  			assert.Equal(t, tt.wantPort, got.Port)
    49  			assert.Equal(t, tt.wantID, got.ID)
    50  			assert.Equal(t, tt.wantHidden, got.Hidden)
    51  
    52  			//assert.NotNil(t, sl.bindAddress)
    53  			//fmt.Println("ProtocolAddress: ", sl.selfMeta.IPAddress)
    54  			//fmt.Println("bindAddress:     ", sl.bindAddress.String())
    55  		})
    56  	}
    57  }