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

     1  /*
     2   * @file
     3   * @copyright defined in aergo/LICENSE.txt
     4   */
     5  
     6  package p2pcommon
     7  
     8  import (
     9  	"bytes"
    10  	"github.com/stretchr/testify/assert"
    11  	"reflect"
    12  	"testing"
    13  )
    14  
    15  func TestHSHeader_Marshal(t *testing.T) {
    16  	type fields struct {
    17  		Magic   uint32
    18  		Version P2PVersion
    19  	}
    20  	tests := []struct {
    21  		name   string
    22  		fields fields
    23  		wantLen int
    24  	}{
    25  		{"T1", fields{MAGICMain, P2PVersion030}, 8},
    26  		{"T2", fields{MAGICTest, P2PVersion031}, 8},
    27  	}
    28  	for _, tt := range tests {
    29  		t.Run(tt.name, func(t *testing.T) {
    30  			h := HSHeader{
    31  				Magic:   tt.fields.Magic,
    32  				Version: tt.fields.Version,
    33  			}
    34  			got := h.Marshal()
    35  			if !reflect.DeepEqual(len(got), tt.wantLen) {
    36  				t.Errorf("HSHeader.Marshal() = %v, want %v", len(got), tt.wantLen)
    37  			}
    38  			got2 := HSHeader{}
    39  			got2.Unmarshal(got)
    40  
    41  			if !reflect.DeepEqual(got2, h) {
    42  				t.Errorf("HSHeader.Unmarshal() = %v, want %v", got2, h)
    43  			}
    44  
    45  		})
    46  	}
    47  }
    48  
    49  func TestHSHeader_Marshal2(t *testing.T) {
    50  	tests := []struct {
    51  		name            string
    52  		input           []byte
    53  		expectedNetwork uint32
    54  		expectedVersion P2PVersion
    55  	}{
    56  		{"TMain030", []byte{0x047, 0x041, 0x68, 0x41, 0, 0, 3, 0}, MAGICMain, P2PVersion030},
    57  		{"TMain020", []byte{0x02e, 0x041, 0x54, 0x29, 0, 1, 3, 5}, MAGICTest, 0x010305},
    58  		// TODO: test cases
    59  	}
    60  	for _, test := range tests {
    61  		t.Run(test.name, func(t *testing.T) {
    62  			hs := HSHeader{}
    63  			hs.Unmarshal(test.input)
    64  			assert.Equal(t, test.expectedNetwork, hs.Magic)
    65  			assert.Equal(t, test.expectedVersion, hs.Version)
    66  
    67  			actualBytes := hs.Marshal()
    68  			assert.True(t, bytes.Equal(test.input, actualBytes))
    69  		})
    70  	}
    71  }
    72  
    73  func TestOutHSHeader_Marshal(t *testing.T) {
    74  	type fields struct {
    75  		Magic    uint32
    76  		Versions []P2PVersion
    77  	}
    78  	tests := []struct {
    79  		name   string
    80  		fields fields
    81  		wantLen int
    82  	}{
    83  		{"TEmpty", fields{MAGICMain, nil}, 8},
    84  		{"TSingle", fields{MAGICMain, []P2PVersion{P2PVersion030}}, 12},
    85  		{"TSingle", fields{MAGICMain, []P2PVersion{0x033333, 0x092fa10, P2PVersion031,P2PVersion030}}, 24},
    86  	}
    87  	for _, tt := range tests {
    88  		t.Run(tt.name, func(t *testing.T) {
    89  			h := HSHeadReq{
    90  				Magic:    tt.fields.Magic,
    91  				Versions: tt.fields.Versions,
    92  			}
    93  
    94  			got := h.Marshal()
    95  			if !reflect.DeepEqual(len(got), tt.wantLen) {
    96  				t.Errorf("HSHeader.Marshal() = %v, want %v", len(got), tt.wantLen)
    97  			}
    98  			//got2 := HSHeadReq{}
    99  			//got2.Unmarshal(got)
   100  			//if !reflect.DeepEqual(got2, got) {
   101  			//	t.Errorf("HSHeader.Unmarshal() = %v, want %v", got2, got)
   102  			//}
   103  		})
   104  	}
   105  }