-
Notifications
You must be signed in to change notification settings - Fork 8
/
utils_test.go
444 lines (374 loc) · 11.1 KB
/
utils_test.go
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//
// Copyright 2019-2020 Nestybox, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/opencontainers/runc/libcontainer/user"
"golang.org/x/sys/unix"
)
func compareSubidRanges(t *testing.T, want, got []user.SubID) {
if len(got) != len(want) {
t.Errorf("AllocSubidRange(): want %v, got %v", want, got)
}
for i, _ := range want {
if got[i] != want[i] {
t.Errorf("AllocSubidRange(): want %v, got %v", want, got)
}
}
}
func TestAllocSubidRange(t *testing.T) {
var subID, got, want []user.SubID
var min uint64 = 100000
var max uint64 = 600100000
var err error
// at end of range
subID = []user.SubID{
{"user1", 100000, 65536},
{"user2", 165536, 65536},
}
got, err = allocSubidRange(subID, 65536, min, max)
if err != nil {
t.Errorf("AllocSubidRange(): %v", err)
}
want = append(subID, user.SubID{"sysbox", 231072, 65536})
compareSubidRanges(t, want, got)
// at beginning of range
subID = []user.SubID{
{"user2", 165536, 65536},
}
got, err = allocSubidRange(subID, 65536, min, max)
if err != nil {
t.Errorf("AllocSubidRange(): %v", err)
}
want = append(subID, user.SubID{"sysbox", 100000, 65536})
compareSubidRanges(t, want, got)
// in middle of range
subID = []user.SubID{
{"user1", 100000, 65536},
{"user2", 231072, 65536},
}
got, err = allocSubidRange(subID, 65536, min, max)
if err != nil {
t.Errorf("AllocSubidRange(): %v", err)
}
want = append(subID, user.SubID{"sysbox", 165536, 65536})
compareSubidRanges(t, want, got)
// with overlapping ranges
subID = []user.SubID{
{"user1", 100000, 65536},
{"user2", 100000, 65536},
{"user3", 165536, 65536},
}
got, err = allocSubidRange(subID, 65536, min, max)
if err != nil {
t.Errorf("AllocSubidRange(): %v", err)
}
want = append(subID, user.SubID{"sysbox", 231072, 65536})
compareSubidRanges(t, want, got)
// more overlapping ranges
subID = []user.SubID{
{"user1", 100000, 65536},
{"user2", 120000, 65536},
{"user3", 165536, 65536},
}
got, err = allocSubidRange(subID, 65536, min, max)
if err != nil {
t.Errorf("AllocSubidRange(): %v", err)
}
want = append(subID, user.SubID{"sysbox", 231072, 65536})
compareSubidRanges(t, want, got)
// empty range
subID = []user.SubID{}
got, err = allocSubidRange(subID, 65536, min, max)
if err != nil {
t.Errorf("AllocSubidRange(): %v", err)
}
want = append(subID, user.SubID{"sysbox", 100000, 65536})
compareSubidRanges(t, want, got)
// not enought ids
max = 165536
subID = []user.SubID{
{"user1", 100000, 65536},
}
got, err = allocSubidRange(subID, 65536, min, max)
if err == nil {
t.Errorf("AllocSubidRange(): expected alloc error, got no error")
}
max = 165536
subID = []user.SubID{
{"user1", 100000, 4096},
}
got, err = allocSubidRange(subID, 65536, min, max)
if err == nil {
t.Errorf("AllocSubidRange(): expected alloc error, got no error")
}
// off-by-one tests
max = 165536
subID = []user.SubID{
{"user1", 100000, 65536},
}
got, err = allocSubidRange(subID, 1, min, max)
if err == nil {
t.Errorf("AllocSubidRange(): expected alloc error, got no error")
}
subID = []user.SubID{
{"user1", 100000, 65535},
}
got, err = allocSubidRange(subID, 1, min, max)
if err != nil {
t.Errorf("AllocSubidRange(): %v", err)
}
want = append(subID, user.SubID{"sysbox", 165535, 1})
compareSubidRanges(t, want, got)
// invalid min/max/size
min = 100000
max = 100000
subID = []user.SubID{}
got, err = allocSubidRange(subID, 1, min, max)
if err == nil {
t.Errorf("AllocSubidRange(): expected alloc error, got no error")
}
subID = []user.SubID{}
got, err = allocSubidRange(subID, 0, min, max)
if err == nil {
t.Errorf("AllocSubidRange(): expected alloc error, got no error")
}
// un-sorted ranges
subID = []user.SubID{
{"user1", 100000, 65536},
{"user2", 231072, 65536},
{"user3", 165536, 65536},
{"user4", 362144, 65536},
}
got, err = allocSubidRange(subID, 65536, min, max)
if err != nil {
t.Errorf("AllocSubidRange(): %v", err)
}
want = append(subID, user.SubID{"sysbox", 296608, 65536})
compareSubidRanges(t, want, got)
}
func verifyFileData(path string, data []byte) error {
fileData, err := ioutil.ReadFile(path)
if err != nil {
return fmt.Errorf("failed to read file %s: %v", path, err)
}
if bytes.Compare(fileData, data) != 0 {
return fmt.Errorf("file data mismatch: want %s, got %s", string(data), string(fileData))
}
return nil
}
func testConfigSubidRangeHelper(subidFilePre, subidFilePost string, size, min, max uint64) error {
f, err := ioutil.TempFile("", "testConfigSubidRange*")
if err != nil {
return fmt.Errorf("failed to create temp file: %v", err)
}
defer os.RemoveAll(f.Name())
if err := ioutil.WriteFile(f.Name(), []byte(subidFilePre), 0644); err != nil {
return fmt.Errorf("failed to write file %s: %v", f.Name(), err)
}
if err := configSubidRange(f.Name(), size, min, max); err != nil {
return fmt.Errorf("configSubidRange(): error = %s", err)
}
verifyFileData(f.Name(), []byte(subidFilePost))
return nil
}
func TestConfigSubidRange(t *testing.T) {
var subidFilePre, subidFilePost string
// at end of range
subidFilePre = `user1:100000:65536
user2:165536:65536`
subidFilePost = `user1:100000:65536
user2:165536:65536
sysbox:231072:268435456
`
if err := testConfigSubidRangeHelper(subidFilePre, subidFilePost, 268435456, 100000, 600100000); err != nil {
t.Errorf(err.Error())
}
// at beginning of range
subidFilePre = `user2:165536:65536`
subidFilePost = `sysbox:100000:65536
user2:165536:65536`
if err := testConfigSubidRangeHelper(subidFilePre, subidFilePost, 65536, 100000, 600100000); err != nil {
t.Errorf(err.Error())
}
// in the middle of range
subidFilePre = `user1:100000:65536
user2:231072:65536`
subidFilePost = `user1:100000:65536
sysbox:165536:65536
user2:231072:65536`
if err := testConfigSubidRangeHelper(subidFilePre, subidFilePost, 65536, 100000, 600100000); err != nil {
t.Errorf(err.Error())
}
// not enought ids
subidFilePre = `user1:100000:65536`
if err := testConfigSubidRangeHelper(subidFilePre, subidFilePost, 600034465, 100000, 600100000); err == nil {
t.Errorf("configSubidRange(): expected alloc error, got no error")
}
// do not disturb existing sysbox entry
subidFilePre = `user1:100000:65536
sysbox:231072,65536
user3:296608:65536`
subidFilePost = subidFilePre
if err := testConfigSubidRangeHelper(subidFilePre, subidFilePost, 65536, 100000, 600100000); err != nil {
t.Errorf(err.Error())
}
// replace redundant sysbox entries with one entry
subidFilePre = `user1:100000:65536
sysbox:231072,65536
user3:165536:65536
sysbox:362144,65536`
subidFilePost = `user1:100000:65536
user3:165536:65536
sysbox:231072,65536`
if err := testConfigSubidRangeHelper(subidFilePre, subidFilePost, 65536, 100000, 600100000); err != nil {
t.Errorf(err.Error())
}
}
func testGetSubidLimitsHelper(fileData string, want []uint64) error {
f, err := ioutil.TempFile("", "testGetSubidLimits*")
if err != nil {
return fmt.Errorf("failed to create temp file: %v", err)
}
if err := ioutil.WriteFile(f.Name(), []byte(fileData), 0644); err != nil {
return fmt.Errorf("failed to write file %s: %v", f.Name(), err)
}
limits, err := getSubidLimits(f.Name())
if err != nil {
return fmt.Errorf("getSubidLimits(): error = %s", err)
}
if len(limits) != 4 {
return fmt.Errorf("getSubidLimits(): limits length incorrect: want 4, got %d", len(limits))
}
for i := 0; i < 4; i++ {
if limits[i] != want[i] {
return fmt.Errorf("getSubidLimits(): failed: got %v, want %v", limits, want)
}
}
if err := os.Remove(f.Name()); err != nil {
return fmt.Errorf("failed to remove file %s", f.Name())
}
return nil
}
func TestGetSubidLimits(t *testing.T) {
// fake login.defs data
fileData := `# some comments
some data
SUB_UID_MIN 100000
some data
SUB_UID_MAX\t 600100000
some data
SUB_GID_MIN 100000
some data
SUB_GID_MAX\t\t 2147483648
# some more comments`
want := []uint64{100000, 600100000, 100000, 2147483648}
if err := testGetSubidLimitsHelper(fileData, want); err != nil {
t.Errorf(err.Error())
}
// login.defs file without uid(gid) limits
fileData = `# some comments
some data
# some more comments`
want = []uint64{100000, 4294967295, 100000, 4294967295}
if err := testGetSubidLimitsHelper(fileData, want); err != nil {
t.Errorf(err.Error())
}
}
func TestGetLibModMounts(t *testing.T) {
var utsname unix.Utsname
if err := unix.Uname(&utsname); err != nil {
t.Errorf("cfgLibModMount: uname failed: %v", err)
}
n := bytes.IndexByte(utsname.Release[:], 0)
path := filepath.Join("/lib/modules/", string(utsname.Release[:n]))
if _, err := os.Stat(path); os.IsNotExist(err) {
return // skip test
}
mounts, err := getLibModMounts()
if err != nil {
t.Errorf("cfgLibModMount: returned error: %v", err)
}
m := mounts[0]
if (m.Destination != path) || (m.Source != path) || (m.Type != "bind") {
t.Errorf("cfgLibModMount: failed basic mount test")
}
}
func TestFindSubPaths(t *testing.T) {
paths := []string{
"/my/prefix/a/b",
"/my/prefix/a/b/c/d",
"/my/prefix/a/b/c",
"/my/prefix/a/b/c/d/e",
"/my/prefix/x/y",
"/my/prefix/x/y/z",
}
prefix := "/my/prefix"
subpaths := findSubPaths(paths, prefix)
for k, v := range subpaths {
if k != "/my/prefix/a" && k != "/my/prefix/x" {
t.Errorf("Unexpected key in subpath map: %s", k)
}
if k == "/my/prefix/a" && len(v) != 4 ||
k == "/my/prefix/x" && len(v) != 2 {
t.Errorf("Unexpected len(val) in subpath map for %s: %v", k, v)
}
//t.Logf("OK: %s: %v", k, v)
}
prefix = ""
subpaths = findSubPaths(paths, prefix)
for k, v := range subpaths {
if k != "/my" {
t.Errorf("Unexpected key in subpath map: %s", k)
}
if len(v) != len(paths) {
t.Errorf("Unexpected len(val) in subpath map: want %d, got %d", len(paths), len(v))
}
//t.Logf("OK: %s: %v", k, v)
}
}
func TestLongestCommonPath(t *testing.T) {
paths := []string{
"/my/prefix/a/b",
"/my/prefix/a/b/c/d",
"/my/prefix/a/b/c",
"/my/prefix/a/b/c/d/e",
"/my/prefix/d/e/f/g",
"/my/prefix/d/e/f/g/h/i/j/k",
"/my/prefix/d/e/f/g/h/i",
"/my/prefix/d/e/f/g/h/i/j",
}
prefix := "/my/prefix"
subpaths := findSubPaths(paths, prefix)
for k, v := range subpaths {
if k != "/my/prefix/a" && k != "/my/prefix/d" {
t.Errorf("Unexpected key in subpath map: %s", k)
}
lcp := longestCommonPath(v)
if k == "/my/prefix/a" && lcp != "/my/prefix/a/b" {
t.Errorf("Unexpected lcp; want \"/my/prefix/a/b\", got \"%s\"", lcp)
}
if k == "/my/prefix/d" && lcp != "/my/prefix/d/e/f/g" {
t.Errorf("Unexpected lcp; want \"/my/prefix/d/e/f\", got \"%s\"", lcp)
}
//t.Logf("OK: k = %s, lcp = %s", k, lcp)
}
}