github.com/slackhq/nebula@v1.9.0/header/header_test.go (about) 1 package header 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 type headerTest struct { 11 expectedBytes []byte 12 *H 13 } 14 15 // 0001 0010 00010010 16 var headerBigEndianTests = []headerTest{{ 17 expectedBytes: []byte{0x54, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x9}, 18 // 1010 0000 19 H: &H{ 20 // 1111 1+2+4+8 = 15 21 Version: 5, 22 Type: 4, 23 Subtype: 0, 24 Reserved: 0, 25 RemoteIndex: 10, 26 MessageCounter: 9, 27 }, 28 }, 29 } 30 31 func TestEncode(t *testing.T) { 32 for _, tt := range headerBigEndianTests { 33 b, err := tt.Encode(make([]byte, Len)) 34 if err != nil { 35 t.Fatal(err) 36 } 37 38 assert.Equal(t, tt.expectedBytes, b) 39 } 40 } 41 42 func TestParse(t *testing.T) { 43 for _, tt := range headerBigEndianTests { 44 b := tt.expectedBytes 45 parsedHeader := &H{} 46 parsedHeader.Parse(b) 47 48 if !reflect.DeepEqual(tt.H, parsedHeader) { 49 t.Fatalf("got %#v; want %#v", parsedHeader, tt.H) 50 } 51 } 52 } 53 54 func TestTypeName(t *testing.T) { 55 assert.Equal(t, "test", TypeName(Test)) 56 assert.Equal(t, "test", (&H{Type: Test}).TypeName()) 57 58 assert.Equal(t, "unknown", TypeName(99)) 59 assert.Equal(t, "unknown", (&H{Type: 99}).TypeName()) 60 } 61 62 func TestSubTypeName(t *testing.T) { 63 assert.Equal(t, "testRequest", SubTypeName(Test, TestRequest)) 64 assert.Equal(t, "testRequest", (&H{Type: Test, Subtype: TestRequest}).SubTypeName()) 65 66 assert.Equal(t, "unknown", SubTypeName(99, TestRequest)) 67 assert.Equal(t, "unknown", (&H{Type: 99, Subtype: TestRequest}).SubTypeName()) 68 69 assert.Equal(t, "unknown", SubTypeName(Test, 99)) 70 assert.Equal(t, "unknown", (&H{Type: Test, Subtype: 99}).SubTypeName()) 71 72 assert.Equal(t, "none", SubTypeName(Message, 0)) 73 assert.Equal(t, "none", (&H{Type: Message, Subtype: 0}).SubTypeName()) 74 } 75 76 func TestTypeMap(t *testing.T) { 77 // Force people to document this stuff 78 assert.Equal(t, map[MessageType]string{ 79 Handshake: "handshake", 80 Message: "message", 81 RecvError: "recvError", 82 LightHouse: "lightHouse", 83 Test: "test", 84 CloseTunnel: "closeTunnel", 85 Control: "control", 86 }, typeMap) 87 88 assert.Equal(t, map[MessageType]*map[MessageSubType]string{ 89 Message: { 90 MessageNone: "none", 91 MessageRelay: "relay", 92 }, 93 RecvError: &subTypeNoneMap, 94 LightHouse: &subTypeNoneMap, 95 Test: &subTypeTestMap, 96 CloseTunnel: &subTypeNoneMap, 97 Handshake: { 98 HandshakeIXPSK0: "ix_psk0", 99 }, 100 Control: &subTypeNoneMap, 101 }, subTypeMap) 102 } 103 104 func TestHeader_String(t *testing.T) { 105 assert.Equal( 106 t, 107 "ver=100 type=test subtype=testRequest reserved=0x63 remoteindex=98 messagecounter=97", 108 (&H{100, Test, TestRequest, 99, 98, 97}).String(), 109 ) 110 } 111 112 func TestHeader_MarshalJSON(t *testing.T) { 113 b, err := (&H{100, Test, TestRequest, 99, 98, 97}).MarshalJSON() 114 assert.Nil(t, err) 115 assert.Equal( 116 t, 117 "{\"messageCounter\":97,\"remoteIndex\":98,\"reserved\":99,\"subType\":\"testRequest\",\"type\":\"test\",\"version\":100}", 118 string(b), 119 ) 120 }