File: smp_tlv_test.go

package info (click to toggle)
golang-github-twstrike-otr3 0.0~git20161015.0.744856d-3.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,080 kB
  • sloc: ansic: 127; makefile: 76
file content (284 lines) | stat: -rw-r--r-- 6,856 bytes parent folder | download | duplicates (3)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package otr3

import "testing"

const (
	tlvTypeLen  = 2
	tlvSizeLen  = 2
	mpiCountLen = 4
	c2Len       = 32
	c3Len       = 32
	cpLen       = 32
	crLen       = 32
	d2Len       = 192
	d3Len       = 192
	d5Len       = 192
	d6Len       = 192
	d7Len       = 192
	g2aLen      = 192
	g2bLen      = 192
	g3aLen      = 192
	g3bLen      = 192
	paLen       = 192
	pbLen       = 192
	qaLen       = 192
	qbLen       = 192
	raLen       = 192
	rbLen       = 192
)

func Test_smpMessage1TLV(t *testing.T) {
	expectedLength := tlvTypeLen + tlvSizeLen + mpiCountLen + (4 + g2aLen) + (4 + c2Len) + (4 + d2Len) +
		(4 + g3aLen) + (4 + c3Len) + (4 + d3Len)

	exp := []byte{
		0x00, 0x02,
		0x03, 0x5C,
		0x00, 0x00, 0x00, 0x06,
		0x00, 0x00, 0x00, 0xC0,
		0x8A, 0x88, 0xC3, 0x45,
	}

	msg := fixtureMessage1()
	tlv := msg.tlv().serialize()
	assertEquals(t, len(tlv), expectedLength)
	assertDeepEquals(t, tlv[:len(exp)], exp)
}

func Test_smpMessage2TLV(t *testing.T) {
	expectedLength := tlvTypeLen + tlvSizeLen + mpiCountLen +
		(4 + g2bLen) + (4 + c2Len) + (4 + d2Len) + (4 + g3bLen) +
		(4 + c3Len) + (4 + d3Len) + (4 + pbLen) + (4 + qbLen) +
		(4 + cpLen) + (4 + d5Len) + (4 + d6Len)

	exp := []byte{
		0x00, 0x03,
		0x06, 0x90,
		0x00, 0x00, 0x00, 0x0b,
		0x00, 0x00, 0x00, 0xC0,
		0x8A, 0x88, 0xC3, 0x45,
	}

	msg := fixtureMessage2()
	tlv := msg.tlv().serialize()
	assertEquals(t, len(tlv), expectedLength)
	assertDeepEquals(t, tlv[:len(exp)], exp)
}

func Test_smpMessage3TLV(t *testing.T) {
	expectedLength := tlvTypeLen + tlvSizeLen + mpiCountLen +
		(4 + paLen) + (4 + qaLen) + (4 + cpLen) +
		(4 + d5Len) + (4 + d6Len) + (4 + raLen) +
		(4 + crLen) + (4 + d7Len)

	exp := []byte{
		0x00, 0x04,
		0x04, 0xE4,
		0x00, 0x00, 0x00, 0x08,
		0x00, 0x00, 0x00, 0xC0,
		0x8E, 0xE7, 0x6C, 0x23,
	}

	msg := fixtureMessage3()
	tlv := msg.tlv().serialize()
	assertEquals(t, len(tlv), expectedLength)
	assertDeepEquals(t, tlv[:len(exp)], exp)
}

func Test_smpMessage4TLV(t *testing.T) {
	expectedLength := tlvTypeLen + tlvSizeLen + mpiCountLen + (4 + rbLen) + (4 + crLen) + (4 + d7Len)
	exp := []byte{
		0x00, 0x05,
		0x01, 0xB0,
		0x00, 0x00, 0x00, 0x03,
		0x00, 0x00, 0x00, 0xC0,
		0x6C, 0xA8, 0x8E, 0xE8,
	}

	msg := fixtureMessage4()
	tlv := msg.tlv().serialize()
	assertEquals(t, len(tlv), expectedLength)
	assertDeepEquals(t, tlv[:len(exp)], exp)
}

func Test_readSmpMessage1TLV(t *testing.T) {
	msg := fixtureMessage1()
	tlv := msg.tlv()

	parsedValue, parsedOk := tlv.smpMessage()
	assertEquals(t, parsedOk, true)
	val, ok := parsedValue.(smp1Message)
	assertEquals(t, ok, true)
	assertDeepEquals(t, val, msg)
}

func Test_readSmpMessage1TLVWithAQuestion(t *testing.T) {
	msg := fixtureMessage1Q()
	tlv := msg.tlv()

	parsedValue, parsedOk := tlv.smpMessage()
	assertEquals(t, parsedOk, true)
	val, ok := parsedValue.(smp1Message)
	assertEquals(t, ok, true)
	assertDeepEquals(t, val, msg)
}

func Test_readSmpMessage1TLVWithAQuestion_willFailIfThereIsNoNulByte(t *testing.T) {
	msg := fixtureMessage1Q()
	tlv := msg.tlv()
	tlv.tlvLength = 0
	tlv.tlvValue = []byte{}

	_, parsedOk := tlv.smpMessage()
	assertEquals(t, parsedOk, false)
}

func Test_readSmpMessage1TLVWithAQuestion_willHandleItCorrectlyIfTheQuestionEndsOnAByte(t *testing.T) {
	msg := fixtureMessage1Q()
	tlv := msg.tlv()
	tlv.tlvLength = 2
	tlv.tlvValue = []byte{0x01, 0x00}

	_, parsedOk := tlv.smpMessage()
	assertEquals(t, parsedOk, false)
}

func Test_readSmpMessage1TLVWithAQuestion_willHandleItCorrectlyIfANulByteIsTheOnlyContent(t *testing.T) {
	msg := fixtureMessage1Q()
	tlv := msg.tlv()
	tlv.tlvLength = 1
	tlv.tlvValue = []byte{0x00}

	_, parsedOk := tlv.smpMessage()
	assertEquals(t, parsedOk, false)
}

func Test_readSmpMessage1TLV_ReturnsNotOKForInValidMessage1(t *testing.T) {
	msg := fixtureMessage1()
	tlv := msg.tlv()
	tlv.tlvValue = tlv.tlvValue[:24]

	_, parsedOk := tlv.smpMessage()
	assertEquals(t, parsedOk, false)
}

func Test_readSmpMessage1TLV_ReturnsNotOKIfTheNumberOfMPIsIsTooShort(t *testing.T) {
	msg := fixtureMessage1()
	tlv := msg.tlv()
	tlv.tlvValue[3] = 0x01

	_, parsedOk := tlv.smpMessage()
	assertEquals(t, parsedOk, false)
}

func Test_readSmpMessage2TLV_ReturnsNotOKForInValidMessage2(t *testing.T) {
	msg := fixtureMessage2()
	tlv := msg.tlv()
	tlv.tlvValue = tlv.tlvValue[:24]

	_, parsedOk := tlv.smpMessage()
	assertEquals(t, parsedOk, false)
}

func Test_readSmpMessage2TLV_ReturnsNotOKIfTheNumberOfMPIsIsTooShort(t *testing.T) {
	msg := fixtureMessage2()
	tlv := msg.tlv()
	tlv.tlvValue[3] = 0x01

	_, parsedOk := tlv.smpMessage()
	assertEquals(t, parsedOk, false)
}

func Test_readSmpMessage3TLV_ReturnsNotOKForInValidMessage2(t *testing.T) {
	msg := fixtureMessage3()
	tlv := msg.tlv()
	tlv.tlvValue = tlv.tlvValue[:24]

	_, parsedOk := tlv.smpMessage()
	assertEquals(t, parsedOk, false)
}

func Test_readSmpMessage3TLV_ReturnsNotOKIfTheNumberOfMPIsIsTooShort(t *testing.T) {
	msg := fixtureMessage3()
	tlv := msg.tlv()
	tlv.tlvValue[3] = 0x01

	_, parsedOk := tlv.smpMessage()
	assertEquals(t, parsedOk, false)
}

func Test_readSmpMessage4TLV_ReturnsNotOKForInValidMessage2(t *testing.T) {
	msg := fixtureMessage4()
	tlv := msg.tlv()
	tlv.tlvValue = tlv.tlvValue[:24]

	_, parsedOk := tlv.smpMessage()
	assertEquals(t, parsedOk, false)
}

func Test_readSmpMessage4TLV_ReturnsNotOKIfTheNumberOfMPIsIsTooShort(t *testing.T) {
	msg := fixtureMessage4()
	tlv := msg.tlv()
	tlv.tlvValue[3] = 0x01

	_, parsedOk := tlv.smpMessage()
	assertEquals(t, parsedOk, false)
}

func Test_toSMPMessage_ReturnsNotOKForIncorrectTLVType(t *testing.T) {
	tlv := tlv{tlvType: 0x0A}

	_, parsedOk := tlv.smpMessage()
	assertEquals(t, parsedOk, false)
}

func Test_toSMPMessage_ReturnsNotOKForTooShortTLV(t *testing.T) {
	tlv := tlv{}

	_, parsedOk := tlv.smpMessage()
	assertEquals(t, parsedOk, false)
}

func Test_readSmpMessage2TLV(t *testing.T) {
	msg := fixtureMessage2()
	tlv := msg.tlv()

	parsedValue, parsedOk := tlv.smpMessage()
	assertEquals(t, parsedOk, true)
	val, ok := parsedValue.(smp2Message)
	assertEquals(t, ok, true)
	assertDeepEquals(t, val, msg)
}

func Test_readSmpMessage3TLV(t *testing.T) {
	msg := fixtureMessage3()
	tlv := msg.tlv()

	parsedValue, parsedOk := tlv.smpMessage()
	assertEquals(t, parsedOk, true)
	val, ok := parsedValue.(smp3Message)
	assertEquals(t, ok, true)
	assertDeepEquals(t, val, msg)
}

func Test_readSmpMessage4TLV(t *testing.T) {
	msg := fixtureMessage4()
	tlv := msg.tlv()

	parsedValue, parsedOk := tlv.smpMessage()
	assertEquals(t, parsedOk, true)
	val, ok := parsedValue.(smp4Message)
	assertEquals(t, ok, true)
	assertDeepEquals(t, val, msg)
}

func Test_readSmpMessageAbortTLV(t *testing.T) {
	msg := fixtureMessageAbort()
	tlv := msg.tlv()

	parsedValue, parsedOk := tlv.smpMessage()
	assertEquals(t, parsedOk, true)
	val, ok := parsedValue.(smpMessageAbort)
	assertEquals(t, ok, true)
	assertDeepEquals(t, val, msg)
}