File: buffer_test.go

package info (click to toggle)
golang-github-awnumar-memguard 0.22.5-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 572 kB
  • sloc: makefile: 3
file content (292 lines) | stat: -rw-r--r-- 6,325 bytes parent folder | download
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
package core

import (
	"bytes"
	"testing"
	"unsafe"
)

func TestNewBuffer(t *testing.T) {
	// Check the error case with zero length.
	b, err := NewBuffer(0)
	if err != ErrNullBuffer {
		t.Error("expected ErrNullBuffer; got", err)
	}
	if b != nil {
		t.Error("expected nil buffer; got", b)
	}

	// Check the error case with negative length.
	b, err = NewBuffer(-1)
	if err != ErrNullBuffer {
		t.Error("expected ErrNullBuffer; got", err)
	}
	if b != nil {
		t.Error("expected nil buffer; got", b)
	}

	// Test normal execution.
	b, err = NewBuffer(32)
	if err != nil {
		t.Error("expected nil err; got", err)
	}
	if !b.alive {
		t.Error("did not expect destroyed buffer")
	}
	if len(b.Data()) != 32 || cap(b.Data()) != 32 {
		t.Errorf("buffer has invalid length (%d) or capacity (%d)", len(b.Data()), cap(b.Data()))
	}
	if !b.mutable {
		t.Error("buffer is not marked mutable")
	}
	if len(b.memory) != roundToPageSize(32)+(2*pageSize) {
		t.Error("allocated incorrect length of memory")
	}
	if !bytes.Equal(b.Data(), make([]byte, 32)) {
		t.Error("container is not zero-filled")
	}

	// Check if the buffer was added to the buffers list.
	if !buffers.exists(b) {
		t.Error("buffer not in buffers list")
	}

	// Destroy the buffer.
	b.Destroy()
}

func TestLotsOfAllocs(t *testing.T) {
	for i := 1; i <= 16385; i++ {
		b, err := NewBuffer(i)
		if err != nil {
			t.Error(err)
		}
		if !b.alive || !b.mutable {
			t.Error("invalid metadata")
		}
		if len(b.data) != i {
			t.Error("invalid data length")
		}
		if len(b.memory) != roundToPageSize(i)+2*pageSize {
			t.Error("memory length invalid")
		}
		if len(b.preguard) != pageSize || len(b.postguard) != pageSize {
			t.Error("guard pages length invalid")
		}
		if len(b.canary) != len(b.inner)-i {
			t.Error("canary length invalid")
		}
		if len(b.inner)%pageSize != 0 {
			t.Error("inner length is not multiple of page size")
		}
		for j := range b.data {
			b.data[j] = 1
		}
		for j := range b.data {
			if b.data[j] != 1 {
				t.Error("region rw test failed")
			}
		}
		b.Destroy()
	}
}

func TestData(t *testing.T) {
	b, err := NewBuffer(32)
	if err != nil {
		t.Error(err)
	}
	datasegm := b.data
	datameth := b.Data()

	// Check for naive equality.
	if !bytes.Equal(datasegm, datameth) {
		t.Error("naive equality check failed")
	}

	// Modify and check if the change was reflected in both.
	datameth[0] = 1
	datameth[31] = 1
	if !bytes.Equal(datasegm, datameth) {
		t.Error("modified equality check failed")
	}

	// Do a deep comparison.
	if uintptr(unsafe.Pointer(&datameth[0])) != uintptr(unsafe.Pointer(&datasegm[0])) {
		t.Error("pointer values differ")
	}
	if len(datameth) != len(datasegm) || cap(datameth) != cap(datasegm) {
		t.Error("length or capacity values differ")
	}

	b.Destroy()
	if b.Data() != nil {
		t.Error("expected nil data slice for destroyed buffer")
	}
}

func TestBufferState(t *testing.T) {
	b, err := NewBuffer(32)
	if err != nil {
		t.Error("expected nil err; got", err)
	}

	if b.Mutable() != true {
		t.Error("state mismatch: mutability")
	}

	if b.Alive() != true {
		t.Error("state mismatch: alive")
	}

	b.Freeze()

	if b.Mutable() != false {
		t.Error("state mismatch: mutability")
	}

	if b.Alive() != true {
		t.Error("state mismatch: alive")
	}

	b.Melt()

	if b.Mutable() != true {
		t.Error("state mismatch: mutability")
	}

	if b.Alive() != true {
		t.Error("state mismatch: alive")
	}

	b.Destroy()

	if b.Mutable() != false {
		t.Error("state mismatch: mutability")
	}

	if b.Alive() != false {
		t.Error("state mismatch: alive")
	}
}

func TestDestroy(t *testing.T) {
	// Allocate a new buffer.
	b, err := NewBuffer(32)
	if err != nil {
		t.Error("expected nil err; got", err)
	}

	// Destroy it.
	b.Destroy()

	// Pick apart the destruction.
	if b.Data() != nil {
		t.Error("expected bytes buffer to be nil; got", b.Data())
	}
	if b.memory != nil {
		t.Error("expected memory to be nil; got", b.memory)
	}
	if b.mutable || b.alive {
		t.Error("buffer should be dead and immutable")
	}
	if b.preguard != nil || b.postguard != nil {
		t.Error("guard page slice references are not nil")
	}
	if b.inner != nil {
		t.Error("inner pages slice reference not nil")
	}
	if b.canary != nil {
		t.Error("canary slice reference not nil")
	}

	// Check if the buffer was removed from the buffers list.
	if buffers.exists(b) {
		t.Error("buffer is still in buffers list")
	}

	// Call destroy again to check idempotency.
	b.Destroy()

	// Verify that it didn't come back to life.
	if b.Data() != nil {
		t.Error("expected bytes buffer to be nil; got", b.Data())
	}
	if b.memory != nil {
		t.Error("expected memory to be nil; got", b.memory)
	}
	if b.mutable || b.alive {
		t.Error("buffer should be dead and immutable")
	}
	if b.preguard != nil || b.postguard != nil {
		t.Error("guard page slice references are not nil")
	}
	if b.inner != nil {
		t.Error("inner pages slice reference not nil")
	}
	if b.canary != nil {
		t.Error("canary slice reference not nil")
	}
}

func TestBufferList(t *testing.T) {
	// Create a new BufferList for testing with.
	l := new(bufferList)

	// Create some example buffers to test with.
	a := new(Buffer)
	b := new(Buffer)

	// Check what Exists is saying.
	if l.exists(a) || l.exists(b) {
		t.Error("list is empty yet contains buffers?!")
	}

	// Add our two buffers to the list.
	l.add(a)
	if len(l.list) != 1 || l.list[0] != a {
		t.Error("buffer was not added correctly")
	}
	l.add(b)
	if len(l.list) != 2 || l.list[1] != b {
		t.Error("buffer was not added correctly")
	}

	// Now check that they exist.
	if !l.exists(a) || !l.exists(b) {
		t.Error("expected buffers to be in list")
	}

	// Remove the buffers from the list.
	l.remove(a)
	if len(l.list) != 1 || l.list[0] != b {
		t.Error("buffer was not removed correctly")
	}
	l.remove(b)
	if len(l.list) != 0 {
		t.Error("item was not removed correctly")
	}

	// Check what exists is saying now.
	if l.exists(a) || l.exists(b) {
		t.Error("list is empty yet contains buffers?!")
	}

	// Add the buffers again to test Empty.
	l.add(a)
	l.add(b)
	bufs := l.flush()
	if l.list != nil {
		t.Error("list was not nullified")
	}
	if len(bufs) != 2 || bufs[0] != a || bufs[1] != b {
		t.Error("buffers dump incorrect")
	}

	// Try appending again.
	l.add(a)
	if !l.exists(a) || l.exists(b) {
		t.Error("list is in invalid state")
	}
	l.remove(a)
}