-
Notifications
You must be signed in to change notification settings - Fork 1
/
message_test.go
153 lines (145 loc) · 4.13 KB
/
message_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package ntgo
import (
"testing"
"bytes"
"reflect"
)
func TestDecodeDataClientHello(t *testing.T) {
messageBytes := []byte{0x03, 0x00, 0x04, 0x6e, 0x74, 0x67, 0x6F}
result, err := DecodeDataClientHello(bytes.NewBuffer(messageBytes))
if err != nil {
t.Fatalf("Unexpected error! %s", err)
}
var expected = &MessageDataClientHello{
ProtocVersion: [2]byte{0x03, 0x00},
Identity: &ValueString{
Value: "ntgo",
RawValue: []byte{0x04, 0x6e, 0x74, 0x67, 0x6F},
},
}
if !reflect.DeepEqual(expected, result) {
t.Fatalf("Expected %s but got %s", expected, result)
}
}
func TestDecodeDataProtocVersionUnsupported(t *testing.T) {
messageBytes := []byte{0x03, 0x00}
result, err := DecodeDataProtocVersionUnsupported(bytes.NewBuffer(messageBytes))
if err != nil {
t.Fatalf("Unexpected error! %s", err)
}
var expected = &MessageDataProtocVersionUnsupported{
SupportedProtoc: [2]byte{0x03, 0x00},
}
if !reflect.DeepEqual(expected, result) {
t.Fatalf("Expected %s but got %s", expected, result)
}
}
func TestDecodeDataServerHello(t *testing.T) {
messageBytes := []byte{0x00, 0x04, 0x6e, 0x74, 0x67, 0x6F}
result, err := DecodeDataServerHello(bytes.NewBuffer(messageBytes))
if err != nil {
t.Fatalf("Unexpected error! %s", err)
}
var expected = &MessageDataServerHello{
Flags: FlagMessageClientNew,
Identity: &ValueString{
Value: "ntgo",
RawValue: []byte{0x04, 0x6e, 0x74, 0x67, 0x6F},
},
}
if !reflect.DeepEqual(expected, result) {
t.Fatalf("Expected %s but got %s", expected, result)
}
}
func TestDecodeDataEntryAssignment(t *testing.T) {
messageBytes := []byte{0x05, 0x65, 0x6e, 0x74, 0x72, 0x79, // Entry Name ("entry")
byte(EntryTypeBoolean), // Entry Type
0x50, 0x21, // Unique ID
0x00, 0x01, // Sequential ID
byte(EntryFlagTemporary), // Flags
byte(BoolTrue), // Value
}
result, err := DecodeDataEntryAssignment(bytes.NewBuffer(messageBytes))
if err != nil {
t.Fatalf("Unexpected error! %s", err)
}
var expected = &MessageDataEntryAssignment{
Entry: &Entry{
Name: &ValueString{
Value: "entry",
RawValue: []byte{0x05, 0x65, 0x6e, 0x74, 0x72, 0x79},
},
Type: EntryTypeBoolean,
ID: [2]byte{0x50, 0x21},
Sequence: [2]byte{0x00, 0x01},
Flags: EntryFlagTemporary,
Value: &ValueBoolean{
Value: true,
RawValue: []byte{byte(BoolTrue)},
},
},
}
if !reflect.DeepEqual(expected, result) {
t.Fatalf("Expected %s but got %s", expected, result)
}
}
func TestDecodeDataEntryUpdate(t *testing.T) {
messageBytes := []byte{
0x50, 0x21, // ID
0x00, 0x01, // Sequential ID
byte(EntryTypeBoolean), // Entry Type
byte(BoolFalse), // Value
}
result, err := DecodeDataEntryUpdate(bytes.NewBuffer(messageBytes))
if err != nil {
t.Fatalf("Unexpected error! %s", err)
}
var expected = &MessageDataEntryUpdate{
Entry: &Entry{
ID: [2]byte{0x50, 0x21},
Sequence: [2]byte{0x00, 0x01},
Type: EntryTypeBoolean,
Value: &ValueBoolean{
Value: false,
RawValue: []byte{byte(BoolFalse)},
},
},
}
if !reflect.DeepEqual(expected, result) {
t.Fatalf("Expected %s but got %s", expected, result)
}
}
func TestDecodeDataEntryFlagsUpdate(t *testing.T) {
messageBytes := []byte{
0x50, 0x21,
byte(EntryFlagPersistent),
}
result, err := DecodeDataEntryFlagsUpdate(bytes.NewBuffer(messageBytes))
if err != nil {
t.Fatalf("Unexpected error! %s", err)
}
var expected = &MessageDataEntryFlagsUpdate{
Entry: &Entry{
ID: [2]byte{0x50, 0x21},
Flags: EntryFlagPersistent,
},
}
if !reflect.DeepEqual(expected, result) {
t.Fatalf("Expected %s but got %s", expected, result)
}
}
func TestDecodeDataClearAll(t *testing.T) {
messageBytes := [4]byte{
0x00, 0x00, 0x00, 0x00,
}
result, err := DecodeDataClearAll(bytes.NewBuffer(messageBytes[:]))
if err != nil {
t.Fatalf("Unexpected error! %s", err)
}
var expected = &MessageDataClearAll{
PotentialMagic: messageBytes,
}
if !reflect.DeepEqual(expected, result) {
t.Fatalf("Expected %s but got %s", expected, result)
}
}