File: alignedbuff_test.go

package info (click to toggle)
golang-github-google-nftables 0.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 756 kB
  • sloc: makefile: 8
file content (334 lines) | stat: -rw-r--r-- 6,332 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
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
package alignedbuff

import (
	"testing"
)

func TestAlignmentData(t *testing.T) {
	if uint16AlignMask == 0 {
		t.Fatal("zero uint16 alignment mask")
	}
	if uint32AlignMask == 0 {
		t.Fatal("zero uint32 alignment mask")
	}
	if uint64AlignMask == 0 {
		t.Fatal("zero uint64 alignment mask")
	}
	if len(padding) == 0 {
		t.Fatal("zero alignment padding sequence")
	}
	if uintSize == 0 {
		t.Fatal("zero uint size")
	}
	if int32AlignMask == 0 {
		t.Fatal("zero uint32 alignment mask")
	}
}

func TestAlignedBuff8(t *testing.T) {
	b := NewWithData([]byte{0x42})
	tests := []struct {
		name string
		v    uint8
		err  error
	}{
		{
			name: "first read",
			v:    0x42,
			err:  nil,
		},
		{
			name: "end of buffer",
			v:    0,
			err:  ErrEOF,
		},
	}

	for _, tt := range tests {
		v, err := b.Uint8()
		if v != tt.v || err != tt.err {
			t.Errorf("expected: %#v %#v, got: %#v, %#v",
				tt.v, tt.err, v, err)
		}
	}
}

func TestAlignedBuff16(t *testing.T) {
	b0 := New()
	b0.PutUint8(0x42)
	b0.PutUint16(0x1234)
	b0.PutUint16(0x5678)

	b := NewWithData(b0.data)
	v, err := b.Uint8()
	if v != 0x42 || err != nil {
		t.Fatalf("unaligment read failed")
	}
	tests := []struct {
		name string
		v    uint16
		err  error
	}{
		{
			name: "first read",
			v:    0x1234,
			err:  nil,
		},
		{
			name: "second read",
			v:    0x5678,
			err:  nil,
		},
		{
			name: "end of buffer",
			v:    0,
			err:  ErrEOF,
		},
	}

	for _, tt := range tests {
		v, err := b.Uint16()
		if v != tt.v || err != tt.err {
			t.Errorf("%s failed, expected: %#v %#v, got: %#v, %#v",
				tt.name, tt.v, tt.err, v, err)
		}
	}
}

func TestAlignedBuff32(t *testing.T) {
	b0 := New()
	b0.PutUint8(0x42)
	b0.PutUint32(0x12345678)
	b0.PutUint32(0x01cecafe)

	b := NewWithData(b0.data)

	// Sigh. The Linux kernel expects certain nftables payloads to be padded to
	// the uint64 next alignment. Now, on 64bit platforms this will be a 64bit
	// alignment, yet on 32bit platforms this will be a 32bit alignment. So, we
	// should calculate the expected data length here separately from our
	// implementation to be fail safe! However, this might be rather a recipe
	// for a safe fail...
	expectedlen := 2*(uint32AlignMask+1) + (uint64AlignMask + 1)

	if len(b0.Data()) != expectedlen {
		t.Fatalf("alignment padding failed")
	}

	v, err := b.Uint8()
	if v != 0x42 || err != nil {
		t.Fatalf("unaligment read failed")
	}
	tests := []struct {
		name string
		v    uint32
		err  error
	}{
		{
			name: "first read",
			v:    0x12345678,
			err:  nil,
		},
		{
			name: "second read",
			v:    0x01cecafe,
			err:  nil,
		},
		{
			name: "end of buffer",
			v:    0,
			err:  ErrEOF,
		},
	}

	for _, tt := range tests {
		v, err := b.Uint32()
		if v != tt.v || err != tt.err {
			t.Errorf("expected: %#v %#v, got: %#v, %#v",
				tt.v, tt.err, v, err)
		}
	}
}

func TestAlignedBuff64(t *testing.T) {
	b0 := New()
	b0.PutUint8(0x42)
	b0.PutUint64(0x1234567823456789)
	b0.PutUint64(0x01cecafec001beef)

	b := NewWithData(b0.data)
	v, err := b.Uint8()
	if v != 0x42 || err != nil {
		t.Fatalf("unaligment read failed")
	}
	tests := []struct {
		name string
		v    uint64
		err  error
	}{
		{
			name: "first read",
			v:    0x1234567823456789,
			err:  nil,
		},
		{
			name: "second read",
			v:    0x01cecafec001beef,
			err:  nil,
		},
		{
			name: "end of buffer",
			v:    0,
			err:  ErrEOF,
		},
	}

	for _, tt := range tests {
		v, err := b.Uint64()
		if v != tt.v || err != tt.err {
			t.Errorf("expected: %#v %#v, got: %#v, %#v",
				tt.v, tt.err, v, err)
		}
	}
}

func TestAlignedUint(t *testing.T) {
	expectedv := uint(^uint32(0) - 1)
	b0 := New()
	b0.PutUint8(0x55)
	b0.PutUint(expectedv)
	b0.PutUint8(0xAA)

	b := NewWithData(b0.data)
	v, err := b.Uint8()
	if v != 0x55 || err != nil {
		t.Fatalf("sentinel read failed")
	}
	uiv, err := b.Uint()
	if uiv != expectedv || err != nil {
		t.Fatalf("uint read failed, expected: %d, got: %d", expectedv, uiv)
	}
	v, err = b.Uint8()
	if v != 0xAA || err != nil {
		t.Fatalf("sentinel read failed")
	}
}

func TestAlignedBuffInt32(t *testing.T) {
	b0 := New()
	b0.PutUint8(0x42)
	b0.PutInt32(0x12345678)
	b0.PutInt32(0x01cecafe)

	b := NewWithData(b0.data)

	// Sigh. The Linux kernel expects certain nftables payloads to be padded to
	// the uint64 next alignment. Now, on 64bit platforms this will be a 64bit
	// alignment, yet on 32bit platforms this will be a 32bit alignment. So, we
	// should calculate the expected data length here separately from our
	// implementation to be fail safe! However, this might be rather a recipe
	// for a safe fail...
	expectedlen := 2*(uint32AlignMask+1) + (uint64AlignMask + 1)

	if len(b0.Data()) != expectedlen {
		t.Fatalf("alignment padding failed")
	}

	v, err := b.Uint8()
	if v != 0x42 || err != nil {
		t.Fatalf("unaligment read failed")
	}
	tests := []struct {
		name string
		v    int32
		err  error
	}{
		{
			name: "first read",
			v:    0x12345678,
			err:  nil,
		},
		{
			name: "second read",
			v:    0x01cecafe,
			err:  nil,
		},
		{
			name: "end of buffer",
			v:    0,
			err:  ErrEOF,
		},
	}

	for _, tt := range tests {
		v, err := b.Int32()
		if v != tt.v || err != tt.err {
			t.Errorf("expected: %#v %#v, got: %#v, %#v",
				tt.v, tt.err, v, err)
		}
	}
}

func TestAlignedBuffPutNullTerminatedString(t *testing.T) {
	b0 := New()
	b0.PutUint8(0x42)
	b0.PutString("test" + "\x00")

	b := NewWithData(b0.data)

	v, err := b.Uint8()
	if v != 0x42 || err != nil {
		t.Fatalf("unaligment read failed")
	}
	tests := []struct {
		name string
		v    string
		err  error
	}{
		{
			name: "first read",
			v:    "test",
			err:  nil,
		},
	}

	for _, tt := range tests {
		v, err := b.String()
		if v != tt.v || err != tt.err {
			t.Errorf("expected: %#v %#v, got: %#v, %#v",
				tt.v, tt.err, v, err)
		}
	}
}

func TestAlignedBuffPutString(t *testing.T) {
	b0 := New()
	b0.PutUint8(0x42)
	b0.PutString("test")

	b := NewWithData(b0.data)

	v, err := b.Uint8()
	if v != 0x42 || err != nil {
		t.Fatalf("unaligment read failed")
	}
	tests := []struct {
		name string
		v    string
		err  error
	}{
		{
			name: "first read",
			v:    "test",
			err:  nil,
		},
	}

	for _, tt := range tests {
		v, err := b.StringWithLength(len("test"))
		if v != tt.v || err != tt.err {
			t.Errorf("expected: %#v %#v, got: %#v, %#v",
				tt.v, tt.err, v, err)
		}
	}
}