summaryrefslogtreecommitdiff
path: root/satp/packet_test.go
blob: a69bb106230961f82d5445a451c9012ff8a3fbbc (plain) (blame)
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
//
// Copyright (c) 2017 anygone contributors (see AUTHORS file)
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this
//   list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
//   this list of conditions and the following disclaimer in the documentation
//   and/or other materials provided with the distribution.
//
// * Neither the name of anygone nor the names of its
//   contributors may be used to endorse or promote products derived from
//   this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

package satp

import (
	"bytes"
	"math/rand"
	// "reflect"
	"io/ioutil"
	"testing"
)

const (
	NUM_RANDOM_DATASETS = 100
)

var (
	IPv4Packet   *PlainPacket
	IPv4Type     = uint16(0x0800)
	IPv4Ping     *PlainPacket
	IPv4PingData = [...]byte{
		0x45, 0x00, 0x00, 0x54, 0xB7, 0xCC, 0x40, 0x00, 0x40, 0x01, 0xAD, 0x73, 0xC0, 0xA8, 0x2A, 0x17,
		0xC0, 0xA8, 0x2A, 0x01, 0x08, 0x00, 0xB4, 0x04, 0x3D, 0xE2, 0x00, 0x01, 0xE6, 0x00, 0xB1, 0x59,
		0x00, 0x00, 0x00, 0x00, 0xA4, 0xEA, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x11, 0x12, 0x13,
		0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23,
		0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33,
		0x34, 0x35, 0x36, 0x37}

	IPv6Packet   *PlainPacket
	IPv6Type     = uint16(0x86DD)
	IPv6Ping     *PlainPacket
	IPv6PingData = [...]byte{
		0x60, 0x0E, 0x9B, 0x1F, 0x00, 0x40, 0x3A, 0x40, 0x2A, 0x02, 0x12, 0x34, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x2A, 0x02, 0x12, 0x34, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x80, 0x00, 0xE6, 0x21, 0x46, 0x22, 0x00, 0x01,
		0x3E, 0x06, 0xB1, 0x59, 0x00, 0x00, 0x00, 0x00, 0x21, 0x5E, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
		0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
		0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37}

	EthernetFrame  *PlainPacket
	EthernetType   = uint16(0x6558)
	ARPRequest     *PlainPacket
	ARPRequestData = [...]byte{
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC6, 0x5A, 0xD4, 0xC7, 0x23, 0x43, 0x08, 0x06, 0x00, 0x01,
		0x08, 0x00, 0x06, 0x04, 0x00, 0x01, 0xC6, 0x5A, 0xD4, 0xC7, 0x23, 0x43, 0xC0, 0xA8, 0x2A, 0x17,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xA8, 0x2A, 0x01}
)

func init() {
	IPv4Packet = NewPlainPacket()
	IPv4Packet.SetPayloadType(IPv4Type)

	IPv4Ping = NewPlainPacket()
	IPv4Ping.SetPayloadType(IPv4Type)

	IPv6Packet = NewPlainPacket()
	IPv6Packet.SetPayloadType(IPv6Type)

	IPv6Ping = NewPlainPacket()
	IPv6Ping.SetPayloadType(IPv6Type)

	EthernetFrame = NewPlainPacket()
	EthernetFrame.SetPayloadType(EthernetType)

	ARPRequest = NewPlainPacket()
	ARPRequest.SetPayloadType(EthernetType)
}

func TestPlainPacketPayloadType(t *testing.T) {
	testvectors := []uint16{0, IPv4Type, IPv6Type, EthernetType, 0xAA55, 0xF00F}

	for _, vector := range testvectors {
		pkt := NewPlainPacket()
		if vector > 0 {
			pkt.SetPayloadType(vector)
		}
		result := pkt.getPacket()
		expected := []byte{byte(vector >> 8), byte(vector)}
		if bytes.Compare(result, expected) != 0 {
			t.Fatalf("resulting packet is invalid is: '%v', should be '%v'", result, expected)
		}

		payloadType := pkt.GetPayloadType()
		if vector != payloadType {
			t.Fatalf("resulting payload type is invalid is: '%v', should be '%v'", payloadType, vector)
		}
	}
}

func TestPlainPacketReadFrom(t *testing.T) {
	// TODO: implement this
}

func TestPlainPacketWriteTo(t *testing.T) {
	// TODO: implement this
}

func generateRandomTestDataPlainPacket() (payloadType uint16, payload []byte) {
	payloadType = uint16(rand.Uint32())

	packetlen := uint(2 + rand.Int31n(PACKET_BUFFER_SIZE-2))
	payload = make([]byte, packetlen)
	rand.Read(payload)
	return
}

func TestPlainPacketReadWrite(t *testing.T) {
	for i := 0; i < NUM_RANDOM_DATASETS; i++ {
		pkt := NewPlainPacket()
		inType, in := generateRandomTestDataPlainPacket()

		pkt.SetPayloadType(inType)
		n, err := pkt.ReadFrom(bytes.NewReader(in))
		if err != nil {
			t.Fatal("unexpected error:", err)
		}
		if n != int64(len(in)) {
			t.Fatalf("returned length is invalid is: %d, should be %d", n, len(in))
		}

		out := &bytes.Buffer{}
		if n, err = pkt.WriteTo(out); err != nil {
			t.Fatal("unexpected error:", err)
		}
		if n != int64(len(in)) {
			t.Fatalf("returned length is invalid is: %d, should be %d", n, len(in))
		}

		if bytes.Compare(in, out.Bytes()) != 0 {
			t.Fatalf("resulting packet is invalid is: '%v', should be '%v'", out.Bytes(), in)
		}
	}
}

// func TestEncryptedPacketMarshal(t *testing.T) {
// 	testvectors := []struct {
// 		packet   EncryptedPacket
// 		valid    bool
// 		expected []byte
// 	}{
// 		{EncryptedPacket{}, false, []byte{}},
// 		{EncryptedPacket{Payload: []byte{0x0}}, false, []byte{}},
// 		{EncryptedPacket{Payload: []byte{0x8}}, false, []byte{}},
// 		{EncryptedPacket{Payload: []byte{0x0, 0x0}}, true, []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}},
// 		{EncryptedPacket{Payload: []byte{0x42, 0x23}}, true, []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x42, 0x23}},
// 		{EncryptedPacket{Payload: []byte{0x17, 0x4, 0x0}}, true, []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x17, 0x4, 0x0}},
// 		{*NewEncryptedPacket(3), false, []byte{}},
// 		{EncryptedPacket{Payload: []byte{0x00, 0x56, 0xa5}, AuthTag: make([]byte, 3)}, true, []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x56, 0xa5, 0x0, 0x0, 0x0}},
// 	}

// 	for _, vector := range testvectors {
// 		result, err := vector.packet.MarshalBinary()
// 		if vector.valid {
// 			if err != nil {
// 				t.Fatal("unexpected error:", err)
// 			}
// 			if bytes.Compare(vector.expected, result) != 0 {
// 				t.Fatalf("resulting packet is invalid is: '%v', should be '%v'", result, vector.expected)
// 			}
// 		} else {
// 			if err == nil {
// 				t.Fatalf("marshalling '%+v' should give an error", vector.packet)
// 			}
// 		}
// 	}
// }

// func TestEncryptedPacketUnmarshal(t *testing.T) {
// 	testvectors := []struct {
// 		data       []byte
// 		valid      bool
// 		authTagLen uint
// 		expected   EncryptedPacket
// 	}{
// 		{[]byte{}, false, 0, EncryptedPacket{}},
// 		{[]byte{0x0, 0x0}, false, 0, EncryptedPacket{}},
// 		{[]byte{0x0, 0x0, 0x0}, false, 0, EncryptedPacket{}},
// 		{[]byte{0x0, 0x0, 0x0, 0x0}, false, 0, EncryptedPacket{}},
// 		{[]byte{0x0, 0x0, 0x0, 0x0, 0x0}, false, 0, EncryptedPacket{}},
// 		{[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, false, 0, EncryptedPacket{}},
// 		{[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, false, 0, EncryptedPacket{}},
// 		{[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, false, 0, EncryptedPacket{}},
// 		{[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, false, 0, EncryptedPacket{}},
// 		{[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, true, 0, EncryptedPacket{Payload: []byte{0x0, 0x0}}},
// 		{[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, false, 1, EncryptedPacket{Payload: []byte{0x0, 0x0}}},
// 		{[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, true, 1, EncryptedPacket{Payload: []byte{0x0, 0x0}, AuthTag: []byte{0x0}}},
// 		{[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, true, 0, EncryptedPacket{Payload: []byte{0x0, 0x0, 0x0}}},
// 		{[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, true, 3, EncryptedPacket{Payload: []byte{0x0, 0x0}, AuthTag: []byte{0x0, 0x0, 0x0}}},
// 	}

// 	for _, vector := range testvectors {
// 		result := *NewEncryptedPacket(vector.authTagLen)
// 		err := result.UnmarshalBinary(vector.data)
// 		if vector.valid {
// 			if err != nil {
// 				t.Fatal("unexpected error:", err)
// 			}
// 			if !reflect.DeepEqual(vector.expected, result) {
// 				t.Fatalf("unmarshalled packet is wrong: is '%+v', should be '%+v'", result, vector.expected)
// 			}
// 		} else {
// 			if err == nil {
// 				t.Fatalf("unmarshalling '%+v' should give an error", vector.data)
// 			}
// 		}
// 	}
// }

// func generateRandomTestDataEncryptedPacket() (ep *EncryptedPacket) {
// 	authtaglen := uint(rand.Int31n(32))
// 	payloadlen := uint(2 + rand.Int31n(2000))

// 	ep = NewEncryptedPacket(authtaglen)
// 	ep.SequenceNumber = rand.Uint32()
// 	ep.SenderID = uint16(rand.Uint32())
// 	ep.Mux = uint16(rand.Uint32())
// 	ep.Payload = make([]byte, payloadlen)
// 	rand.Read(ep.Payload)
// 	return
// }

// func TestEncryptedPacketMarshalUnmarshal(t *testing.T) {
// 	for i := 0; i < NUM_RANDOM_DATASETS; i++ {
// 		in := generateRandomTestDataEncryptedPacket()
// 		marshalled, err := in.MarshalBinary()
// 		if err != nil {
// 			t.Fatal("unexpected error:", err)
// 		}
// 		out := NewEncryptedPacket(uint(len(in.AuthTag)))
// 		if err = out.UnmarshalBinary(marshalled); err != nil {
// 			t.Fatal("unexpected error:", err)
// 		}
// 		if !reflect.DeepEqual(in, out) {
// 			t.Fatalf("unmarshalled packet is wrong: is '%+v', should be '%+v'", out, in)
// 		}
// 	}
// }

//
// Benchmarking
//

func BenchmarkPlainPacketReadFrom(b *testing.B) {
	ins := []*bytes.Buffer{}
	for i := 0; i < NUM_RANDOM_DATASETS; i++ {
		_, payload := generateRandomTestDataPlainPacket()
		ins = append(ins, bytes.NewBuffer(payload))
	}
	pkt := NewPlainPacket()

	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		in := ins[i%len(ins)]
		in.Reset()
		if _, err := pkt.ReadFrom(in); err != nil {
			b.Fatal("unexpected error:", err)
		}
	}
}

func BenchmarkPlainPacketWriteTo(b *testing.B) {
	ins := []*PlainPacket{}
	for i := 0; i < NUM_RANDOM_DATASETS; i++ {
		_, payload := generateRandomTestDataPlainPacket()
		in := NewPlainPacket()
		if _, err := in.ReadFrom(bytes.NewReader(payload)); err != nil {
			b.Fatal("unexpected error:", err)
		}
		ins = append(ins, in)
	}

	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		in := ins[i%len(ins)]
		if _, err := in.WriteTo(ioutil.Discard); err != nil {
			b.Fatal("unexpected error:", err)
		}
	}
}

// func BenchmarkEncryptedPacketMarshal(b *testing.B) {
// 	ins := []*EncryptedPacket{}
// 	for i := 0; i < NUM_RANDOM_DATASETS; i++ {
// 		ins = append(ins, generateRandomTestDataEncryptedPacket())
// 	}

// 	b.ResetTimer()

// 	for i := 0; i < b.N; i++ {
// 		in := ins[i%len(ins)]
// 		if _, err := in.MarshalBinary(); err != nil {
// 			b.Fatal("unexpected error:", err)
// 		}
// 	}
// }

// func BenchmarkEncryptedPacketUnMarshal(b *testing.B) {
// 	type inType struct {
// 		authTagLen uint
// 		data       []byte
// 	}
// 	ins := []inType{}
// 	for i := 0; i < NUM_RANDOM_DATASETS; i++ {
// 		pkt := generateRandomTestDataEncryptedPacket()
// 		inData, err := pkt.MarshalBinary()
// 		if err != nil {
// 			b.Fatal("unexpected error:", err)
// 		}
// 		ins = append(ins, inType{uint(len(pkt.AuthTag)), inData})
// 	}

// 	b.ResetTimer()

// 	for i := 0; i < b.N; i++ {
// 		in := ins[i%len(ins)]
// 		out := NewEncryptedPacket(in.authTagLen)
// 		if err := out.UnmarshalBinary(in.data); err != nil {
// 			b.Fatal("unexpected error:", err)
// 		}
// 	}
// }