File: fragmentation_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, sid, trixie
  • size: 1,080 kB
  • sloc: ansic: 127; makefile: 76
file content (307 lines) | stat: -rw-r--r-- 11,525 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package otr3

import (
	"crypto/rand"
	"testing"
)

const defaultInstanceTag = 0x00000100

func Test_isFragmented_returnsFalseForAShortValue(t *testing.T) {
	ctx := newConversation(otrV2{}, rand.Reader)
	assertEquals(t, ctx.version.isFragmented([]byte("")), false)
}

func Test_isFragmented_returnsFalseForALongValue(t *testing.T) {
	ctx := newConversation(otrV2{}, rand.Reader)
	assertEquals(t, ctx.version.isFragmented([]byte("?OTR:BLA")), false)
}

func Test_isFragmented_returnsFalseForAFragmentedV3MessageWhenRunningV2(t *testing.T) {
	ctx := newConversation(otrV2{}, rand.Reader)
	assertEquals(t, ctx.version.isFragmented([]byte("?OTR|BLA")), false)
}

func Test_isFragmented_returnsTrueForAFragmentedV3MessageWhenRunningV3(t *testing.T) {
	ctx := newConversation(otrV3{}, rand.Reader)
	assertEquals(t, ctx.version.isFragmented([]byte("?OTR|BLA")), true)
}

func Test_isFragmented_returnsTrueForAFragmentedV2MessageWhenRunningV2(t *testing.T) {
	ctx := newConversation(otrV2{}, rand.Reader)
	assertEquals(t, ctx.version.isFragmented([]byte("?OTR,BLA")), true)
}

func Test_isFragmented_returnsTrueForAFragmentedV2MessageWhenRunningV3(t *testing.T) {
	ctx := newConversation(otrV3{}, rand.Reader)
	assertEquals(t, ctx.version.isFragmented([]byte("?OTR,BLA")), true)
}

func Test_fragment_returnsNoChangeForASmallerPackage(t *testing.T) {
	ctx := newConversation(otrV3{}, rand.Reader)
	ctx.ourInstanceTag = defaultInstanceTag
	ctx.theirInstanceTag = defaultInstanceTag

	data := []byte("one two three")

	assertDeepEquals(t, ctx.fragment(data, 13), []ValidMessage{data})
}

func Test_fragment_returnsFragmentsForNeededFragmentation(t *testing.T) {
	ctx := newConversation(otrV3{}, rand.Reader)
	ctx.ourInstanceTag = defaultInstanceTag
	ctx.theirInstanceTag = defaultInstanceTag + 2

	data := []byte("one one one two two two three three three")
	assertDeepEquals(t, ctx.fragment(data, 40), []ValidMessage{
		[]byte("?OTR|00000100|00000102,00001,00011,one ,"),
		[]byte("?OTR|00000100|00000102,00002,00011,one ,"),
		[]byte("?OTR|00000100|00000102,00003,00011,one ,"),
		[]byte("?OTR|00000100|00000102,00004,00011,two ,"),
		[]byte("?OTR|00000100|00000102,00005,00011,two ,"),
		[]byte("?OTR|00000100|00000102,00006,00011,two ,"),
		[]byte("?OTR|00000100|00000102,00007,00011,thre,"),
		[]byte("?OTR|00000100|00000102,00008,00011,e th,"),
		[]byte("?OTR|00000100|00000102,00009,00011,ree ,"),
		[]byte("?OTR|00000100|00000102,00010,00011,thre,"),
		[]byte("?OTR|00000100|00000102,00011,00011,e,"),
	})
}

func Test_fragment_returnsFragmentsForNeededFragmentationForV2(t *testing.T) {
	ctx := newConversation(otrV2{}, rand.Reader)
	ctx.ourInstanceTag = defaultInstanceTag
	ctx.theirInstanceTag = defaultInstanceTag + 1

	data := []byte("one one one two two two three three three")

	res := ctx.fragment(data, 22)
	assertDeepEquals(t, res, []ValidMessage{
		[]byte("?OTR,00001,00011,one ,"),
		[]byte("?OTR,00002,00011,one ,"),
		[]byte("?OTR,00003,00011,one ,"),
		[]byte("?OTR,00004,00011,two ,"),
		[]byte("?OTR,00005,00011,two ,"),
		[]byte("?OTR,00006,00011,two ,"),
		[]byte("?OTR,00007,00011,thre,"),
		[]byte("?OTR,00008,00011,e th,"),
		[]byte("?OTR,00009,00011,ree ,"),
		[]byte("?OTR,00010,00011,thre,"),
		[]byte("?OTR,00011,00011,e,"),
	})
}

func Test_receiveFragment_returnsANewFragmentationContextForANewMessage(t *testing.T) {
	c := newConversation(otrV2{}, rand.Reader)
	data := []byte("?OTR,00001,00004,one ,")

	fctx, e := c.receiveFragment(fragmentationContext{}, data)

	assertDeepEquals(t, fctx.frag, []byte("one "))
	assertDeepEquals(t, e, nil)
	assertEquals(t, fctx.currentIndex, uint16(1))
	assertEquals(t, fctx.currentLen, uint16(4))
}

func Test_receiveFragment_returnsANewFragmentationContextForANewV3Message(t *testing.T) {
	c := newConversation(otrV3{}, rand.Reader)
	c.ourInstanceTag = 0x102
	c.theirInstanceTag = 0x100
	data := []byte("?OTR|00000100|00000102,00001,00004,one ,")

	fctx, e := c.receiveFragment(fragmentationContext{}, data)

	assertDeepEquals(t, fctx.frag, []byte("one "))
	assertDeepEquals(t, e, nil)
	assertEquals(t, fctx.currentIndex, uint16(1))
	assertEquals(t, fctx.currentLen, uint16(4))
}

func Test_receiveFragment_returnsTheExistingContextIfTheInstanceTagsDoesNotMatch(t *testing.T) {
	c := newConversation(otrV3{}, rand.Reader)
	c.ourInstanceTag = 0x103
	c.theirInstanceTag = 0x104

	existingContext := fragmentationContext{frag: []byte("shouldn't change")}

	fctx, _ := c.receiveFragment(existingContext, []byte("?OTR|00000204|00000103,00001,00004,one ,"))
	assertDeepEquals(t, fctx, existingContext)

	fctx, _ = c.receiveFragment(existingContext, []byte("?OTR|00000104|00000203,00001,00004,one ,"))
	assertDeepEquals(t, fctx, existingContext)
}

func Test_receiveFragment_signalsMessageEventIfInstanceTagsDoesNotMatch(t *testing.T) {
	c := newConversation(otrV3{}, rand.Reader)
	c.ourInstanceTag = 0x103
	c.theirInstanceTag = 0x104

	existingContext := fragmentationContext{frag: []byte("shouldn't change")}

	c.expectMessageEvent(t, func() {
		c.receiveFragment(existingContext, []byte("?OTR|00000204|00000103,00001,00004,one ,"))
	}, MessageEventReceivedMessageForOtherInstance, nil, nil)
}

func Test_receiveFragment_sendsAnErrorMessageAboutMalformedIfHandlerExists(t *testing.T) {
	c := newConversation(otrV3{}, rand.Reader)
	c.ourInstanceTag = 0x103
	c.theirInstanceTag = 0x0A

	existingContext := fragmentationContext{frag: []byte("shouldn't change")}

	c.errorMessageHandler = dynamicErrorMessageHandler{
		func(error ErrorCode) []byte {
			if error == ErrorCodeMessageMalformed {
				return []byte("black happened")
			}
			return []byte("white happened")
		}}

	c.receiveFragment(existingContext, []byte("?OTR|0000000A|00000103,00001,00004,one ,"))
	ts, _ := c.withInjections(nil, nil)
	assertDeepEquals(t, string(ts[0]), "?OTR Error: black happened")
}

func Test_receiveFragment_signalsMalformedMessageIfTheirInstanceTagIsBelowTheLimit(t *testing.T) {
	c := newConversation(otrV3{}, rand.Reader)
	c.ourInstanceTag = 0x103
	c.theirInstanceTag = 0x0A

	existingContext := fragmentationContext{frag: []byte("shouldn't change")}

	c.expectMessageEvent(t, func() {
		c.receiveFragment(existingContext, []byte("?OTR|0000000A|00000103,00001,00004,one ,"))
	}, MessageEventReceivedMessageMalformed, nil, nil)
}

func Test_receiveFragment_returnsTheSameContextIfMessageNumberIsZero(t *testing.T) {
	c := newConversation(otrV3{}, rand.Reader)
	data := []byte("?OTR,00000,00004,one ,")
	fctx, _ := c.receiveFragment(fragmentationContext{}, data)
	assertDeepEquals(t, fctx, fragmentationContext{})
}

func Test_receiveFragment_returnsTheSameContextIfMessageCountIsZero(t *testing.T) {
	c := newConversation(otrV3{}, rand.Reader)
	data := []byte("?OTR,00001,00000,one ,")
	fctx, _ := c.receiveFragment(fragmentationContext{}, data)
	assertDeepEquals(t, fctx, fragmentationContext{})
}

func Test_receiveFragment_returnsTheSameContextIfMessageNumberIsAboveMessageCount(t *testing.T) {
	c := newConversation(otrV3{}, rand.Reader)
	data := []byte("?OTR,00005,00004,one ,")
	fctx, _ := c.receiveFragment(fragmentationContext{}, data)
	assertDeepEquals(t, fctx, fragmentationContext{})
}

func Test_receiveFragment_returnsTheNextContextIfMessageNumberIsOneMoreThanThePreviousOne(t *testing.T) {
	c := newConversation(otrV2{}, rand.Reader)
	data := []byte("?OTR,00003,00004, one,")
	fctx, _ := c.receiveFragment(fragmentationContext{[]byte("blarg one two"), 2, 4}, data)
	assertDeepEquals(t, fctx, fragmentationContext{[]byte("blarg one two one"), 3, 4})
}

func Test_receiveFragment_resetsTheContextIfTheMessageCountIsNotTheSame(t *testing.T) {
	c := newConversation(otrV2{}, rand.Reader)
	data := []byte("?OTR,00003,00005, one,")
	fctx, _ := c.receiveFragment(fragmentationContext{[]byte("blarg one two"), 2, 4}, data)
	assertDeepEquals(t, fctx, fragmentationContext{})
}

func Test_receiveFragment_resetsTheContextIfTheMessageNumberIsNotExactlyOnePlus(t *testing.T) {
	c := newConversation(otrV2{}, rand.Reader)
	data := []byte("?OTR,00004,00005, one,")
	fctx, _ := c.receiveFragment(fragmentationContext{[]byte("blarg one two"), 2, 5}, data)
	assertDeepEquals(t, fctx, fragmentationContext{})
}

func Test_fragmentFinished_isFalseIfThereAreNoFragments(t *testing.T) {
	assertDeepEquals(t, fragmentsFinished(fragmentationContext{[]byte{}, 0, 0}), false)
}

func Test_fragmentFinished_isFalseIfTheNumberOfFragmentsIsNotTheSame(t *testing.T) {
	assertDeepEquals(t, fragmentsFinished(fragmentationContext{[]byte{}, 1, 2}), false)
}

func Test_fragmentFinished_isFalseIfTheNumberOfFragmentsIsNotTheSameWhereTheNumberIsHigher(t *testing.T) {
	assertDeepEquals(t, fragmentsFinished(fragmentationContext{[]byte{}, 3, 2}), false)
}

func Test_fragmentFinished_isTrueIfTheNumberIsTheSameAsTheCount(t *testing.T) {
	assertDeepEquals(t, fragmentsFinished(fragmentationContext{[]byte{}, 3, 3}), true)
}

func Test_parseFragment_returnsNotOKIfThereAreNotEnoughParts(t *testing.T) {
	_, _, _, ok := parseFragment([]byte{0x2C, 0x2C})
	assertDeepEquals(t, ok, false)
}

func Test_parseFragment_returnsNotOKIfThereAreTooManyParts(t *testing.T) {
	_, _, _, ok := parseFragment([]byte{0x2C, 0x2C, 0x2C, 0x2C})
	assertDeepEquals(t, ok, false)
}

func Test_parseFragment_returnsNotOKIfTheIndexIsNotAValidUint(t *testing.T) {
	_, _, _, ok := parseFragment([]byte{0x30, 0x30, 0x30, 0x30, 0x29, 0x2C, 0x30, 0x30, 0x30, 0x30, 0x31, 0x2C, 0x01, 0x2C})
	assertDeepEquals(t, ok, false)
}

func Test_parseFragment_returnsNotOKIfTheLengthIsNotAValidUint(t *testing.T) {
	_, _, _, ok := parseFragment([]byte{0x30, 0x30, 0x30, 0x30, 0x31, 0x2C, 0x30, 0x30, 0x30, 0x30, 0x29, 0x2C, 0x01, 0x2C})
	assertDeepEquals(t, ok, false)
}

func Test_parseFragment_returnsOKIfThereAreExactlyTheRightAmountOfParts(t *testing.T) {
	_, _, _, ok := parseFragment([]byte{0x30, 0x30, 0x30, 0x30, 0x31, 0x2C, 0x30, 0x30, 0x30, 0x30, 0x31, 0x2C, 0x01, 0x2C})
	assertDeepEquals(t, ok, true)
}

func Test_receiveFragment_returnsErrorIfTheFragmentIsNotCorrect(t *testing.T) {
	c := newConversation(otrV2{}, rand.Reader)
	_, e := c.receiveFragment(fragmentationContext{}, []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x30, 0x30, 0x29, 0x2C, 0x30, 0x30, 0x30, 0x30, 0x31, 0x2C, 0x01, 0x2C})
	assertDeepEquals(t, e, newOtrError("invalid OTR fragment"))
}

func Test_parseFragmentPrefix_resolveVersion2IfNotDefined(t *testing.T) {
	fragment := []byte("?OTR,00001,00004,?OTR:AAICAAAAxJh7YMX8vCry1O+3ewL88,")

	c := &Conversation{Policies: policies(allowV2)}
	c.parseFragmentPrefix(fragment)

	assertEquals(t, c.version, otrV2{})

}

func Test_parseFragmentPrefix_rejectsVersion2IfNotAllowedByThePolicy(t *testing.T) {
	fragment := []byte("?OTR,00001,00004,?OTR:AAICAAAAxJh7YMX8vCry1O+3ewL88,")

	c := &Conversation{Policies: policies(allowV3)}
	_, ignore, ok := c.parseFragmentPrefix(fragment)

	assertEquals(t, ok, false)
	assertEquals(t, ignore, true)
	assertEquals(t, c.version, nil)

}

func Test_parseFragmentPrefix_resolveVersion3IfNotDefined(t *testing.T) {
	fragment := []byte("?OTR|5a73a599|27e31597,00001,00003,?OTR:AAMDJ+MVmSfjF,")

	c := &Conversation{Policies: policies(allowV3)}
	c.parseFragmentPrefix(fragment)

	assertEquals(t, c.version, otrV3{})
}

func Test_parseFragmentPrefix_rejectsVersion3IfNotAllowedByThePolicy(t *testing.T) {
	fragment := []byte("?OTR|5a73a599|27e31597,00001,00003,?OTR:AAMDJ+MVmSfjF,")

	c := &Conversation{Policies: policies(allowV2)}
	_, ignore, ok := c.parseFragmentPrefix(fragment)

	assertEquals(t, ok, false)
	assertEquals(t, ignore, true)
	assertEquals(t, c.version, nil)
}