File: idtools_test.go

package info (click to toggle)
golang-github-containers-storage 1.59.1%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,184 kB
  • sloc: sh: 630; ansic: 389; makefile: 143; awk: 12
file content (457 lines) | stat: -rw-r--r-- 10,337 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
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
445
446
447
448
449
450
451
452
453
454
455
456
457
package idtools

import (
	"io/fs"
	"os"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestToHost(t *testing.T) {
	idMappings := []IDMap{
		{
			ContainerID: 0,
			HostID:      1000,
			Size:        1,
		},
		{
			ContainerID: 1,
			HostID:      100000,
			Size:        65536,
		},
	}

	mappings := IDMappings{
		uids: idMappings,
		gids: idMappings,
	}

	pair, err := mappings.ToHost(IDPair{UID: 0, GID: 0})
	if err != nil {
		t.Fatal(err)
	}
	if pair.UID != 1000 {
		t.Fatalf("Converted to the wrong UID")
	}
	if pair.GID != 1000 {
		t.Fatalf("Converted to the wrong GID")
	}

	pair, err = mappings.ToHost(IDPair{UID: 1000, GID: 1000})
	if err != nil {
		t.Fatal(err)
	}
	if pair.UID != 100999 {
		t.Fatalf("Converted to the wrong UID")
	}
	if pair.GID != 100999 {
		t.Fatalf("Converted to the wrong GID")
	}
}

func TestToHostOverflow(t *testing.T) {
	idMappings := []IDMap{
		{
			ContainerID: 0,
			HostID:      1000,
			Size:        1,
		},
		{
			ContainerID: 1,
			HostID:      100000,
			Size:        65536,
		},
	}

	mappings := IDMappings{
		uids: idMappings,
		gids: idMappings,
	}

	pair, err := mappings.ToHostOverflow(IDPair{UID: 65538, GID: 0})
	if err != nil {
		t.Fatal(err)
	}
	if pair.UID != getOverflowUID() {
		t.Fatalf("Converted to the wrong UID")
	}
	if pair.GID != 1000 {
		t.Fatalf("Converted to the wrong GID")
	}

	pair, err = mappings.ToHostOverflow(IDPair{UID: 10, GID: 65539})
	if err != nil {
		t.Fatal(err)
	}
	if pair.UID != 100009 {
		t.Fatalf("Converted to the wrong UID")
	}
	if pair.GID != getOverflowGID() {
		t.Fatalf("Converted to the wrong GID")
	}
}

func TestGetRootUIDGID(t *testing.T) {
	mappingsUIDs := []IDMap{
		{
			ContainerID: 0,
			HostID:      1000,
			Size:        100,
		},
	}
	mappingsGIDs := []IDMap{
		{
			ContainerID: 0,
			HostID:      2000,
			Size:        100,
		},
	}
	uid, gid, err := GetRootUIDGID(mappingsUIDs, mappingsGIDs)
	if err != nil {
		t.Fatal(err)
	}
	if uid != 1000 {
		t.Fatalf("Detected wrong root uid in the host")
	}
	if gid != 2000 {
		t.Fatalf("Detected wrong root uid in the host")
	}

	mappingsUIDs = []IDMap{
		{
			ContainerID: 100,
			HostID:      1001,
			Size:        1,
		},
	}
	mappingsGIDs = []IDMap{
		{
			ContainerID: 200,
			HostID:      2002,
			Size:        1,
		},
	}
	uid, gid, err = GetRootUIDGID(mappingsUIDs, mappingsGIDs)
	if err != nil {
		t.Fatal(err)
	}
	if uid != 1001 {
		t.Fatalf("Detected wrong root uid in the host")
	}
	if gid != 2002 {
		t.Fatalf("Detected wrong root uid in the host")
	}

	mappingsUIDs = []IDMap{
		{
			ContainerID: 100,
			HostID:      1001,
			Size:        100,
		},
	}
	mappingsGIDs = []IDMap{
		{
			ContainerID: 200,
			HostID:      2002,
			Size:        100,
		},
	}
	_, _, err = GetRootUIDGID(mappingsUIDs, mappingsGIDs)
	if err == nil {
		t.Fatalf("Detected root user")
	}

	mappingsUIDs = []IDMap{
		{
			ContainerID: 100,
			HostID:      1001,
			Size:        1,
		},
		{
			ContainerID: 200,
			HostID:      2001,
			Size:        1,
		},
	}
	mappingsGIDs = []IDMap{
		{
			ContainerID: 100,
			HostID:      1001,
			Size:        1,
		},
		{
			ContainerID: 200,
			HostID:      2001,
			Size:        1,
		},
	}
	_, _, err = GetRootUIDGID(mappingsUIDs, mappingsGIDs)
	if err == nil {
		t.Fatalf("Detected root user")
	}
}

func TestIsContiguous(t *testing.T) {
	mappings := []IDMap{
		{
			ContainerID: 0,
			HostID:      0,
			Size:        100,
		},
		{
			ContainerID: 100,
			HostID:      100,
			Size:        100,
		},
	}
	if !IsContiguous(mappings) {
		t.Errorf("mappings %v expected to be contiguous", mappings)
	}
	mappings = []IDMap{
		{
			ContainerID: 0,
			HostID:      10000,
			Size:        100,
		},
		{
			ContainerID: 100,
			HostID:      100,
			Size:        100,
		},
	}
	if IsContiguous(mappings) {
		t.Errorf("mappings %v expected to not be contiguous", mappings)
	}

	mappings = []IDMap{
		{
			ContainerID: 10000,
			HostID:      0,
			Size:        100,
		},
		{
			ContainerID: 100,
			HostID:      100,
			Size:        100,
		},
	}
	if IsContiguous(mappings) {
		t.Errorf("mappings %v expected to not be contiguous", mappings)
	}

	mappings = []IDMap{
		{
			ContainerID: 0,
			HostID:      10,
			Size:        10,
		},
		{
			ContainerID: 10,
			HostID:      20,
			Size:        10,
		},
		{
			ContainerID: 20,
			HostID:      30,
			Size:        10,
		},
		{
			ContainerID: 30,
			HostID:      40,
			Size:        10,
		},
	}
	if !IsContiguous(mappings) {
		t.Errorf("mappings %v expected to be contiguous", mappings)
	}

	mappings = []IDMap{
		{
			ContainerID: 0,
			HostID:      10,
			Size:        10,
		},
	}
	if !IsContiguous(mappings) {
		t.Errorf("mappings %v expected to be contiguous", mappings)
	}
}

func TestParseOverrideXattr(t *testing.T) {
	tests := []struct {
		name          string
		input         []byte
		expectErr     bool
		expectedUID   int
		expectedGID   int
		expectedMode  os.FileMode
		expectedMajor int
		expectedMinor int
	}{
		{
			name:         "valid regular file",
			input:        []byte("1000:1001:0644:file"),
			expectErr:    false,
			expectedUID:  1000,
			expectedGID:  1001,
			expectedMode: 0o644,
		},
		{
			name:         "valid directory",
			input:        []byte("1000:1001:0755:dir"),
			expectErr:    false,
			expectedUID:  1000,
			expectedGID:  1001,
			expectedMode: 0o755 | os.ModeDir,
		},
		{
			name:      "Invalid format: missing fields",
			input:     []byte("1000:1001"),
			expectErr: true,
		},
		{
			name:      "Invalid UID format",
			input:     []byte("invalid:1001:0644:file"),
			expectErr: true,
		},
		{
			name:         "valid symlink",
			input:        []byte("1000:1001:0777:symlink"),
			expectErr:    false,
			expectedUID:  1000,
			expectedGID:  1001,
			expectedMode: 0o777 | os.ModeSymlink,
		},
		{
			name:         "valid pipe",
			input:        []byte("1000:1001:0666:pipe"),
			expectErr:    false,
			expectedUID:  1000,
			expectedGID:  1001,
			expectedMode: 0o666 | os.ModeNamedPipe,
		},
		{
			name:          "valid block device",
			input:         []byte("1000:1001:0600:block-5-6"),
			expectErr:     false,
			expectedUID:   1000,
			expectedGID:   1001,
			expectedMode:  0o600 | os.ModeDevice,
			expectedMajor: 5,
			expectedMinor: 6,
		},
		{
			name:          "valid char device",
			input:         []byte("1000:1001:0600:char-10-11"),
			expectErr:     false,
			expectedUID:   1000,
			expectedGID:   1001,
			expectedMode:  0o600 | os.ModeCharDevice | os.ModeDevice,
			expectedMajor: 10,
			expectedMinor: 11,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			stat, err := parseOverrideXattr(tt.input)
			if tt.expectErr {
				assert.Error(t, err)
			} else {
				assert.NoError(t, err)
				assert.NotNil(t, stat)
				assert.Equal(t, tt.expectedUID, stat.IDs.UID)
				assert.Equal(t, tt.expectedGID, stat.IDs.GID)
				assert.Equal(t, tt.expectedMode, stat.Mode)
				assert.Equal(t, tt.expectedMajor, stat.Major)
				assert.Equal(t, tt.expectedMinor, stat.Minor)
			}
		})
	}
}

func TestFormatContainersOverrideXattrDevice(t *testing.T) {
	tests := []struct {
		name     string
		uid      int
		gid      int
		mode     fs.FileMode
		major    int
		minor    int
		expected string
	}{
		{"regular file", 1000, 1011, fs.FileMode(0o644), 0, 0, "1000:1011:0644:file"},
		{"directory", 1001, 1012, fs.FileMode(0o755) | os.ModeDir, 0, 0, "1001:1012:0755:dir"},
		{"symlink", 1002, 1013, fs.FileMode(0o777) | os.ModeSymlink, 0, 0, "1002:1013:0777:symlink"},
		{"pipe", 1003, 1014, fs.FileMode(0o666) | os.ModeNamedPipe, 0, 0, "1003:1014:0666:pipe"},
		{"socket", 1004, 1015, fs.FileMode(0o700) | os.ModeSocket, 0, 0, "1004:1015:0700:socket"},
		{"block device", 1005, 1016, fs.FileMode(0o600) | os.ModeDevice, 8, 0, "1005:1016:0600:block-8-0"},
		{"char device", 1006, 1017, fs.FileMode(0o600) | os.ModeDevice | os.ModeCharDevice, 1, 3, "1006:1017:0600:char-1-3"},

		{"setuid bit", 1012, 1022, fs.FileMode(0o755) | fs.ModeSetuid, 0, 0, "1012:1022:4755:file"},
		{"setgid bit", 1013, 1023, fs.FileMode(0o755) | fs.ModeSetgid, 0, 0, "1013:1023:2755:file"},
		{"sticky bit", 1014, 1024, fs.FileMode(0o777) | fs.ModeSticky, 0, 0, "1014:1024:1777:file"},

		{"user read", 1020, 1030, fs.FileMode(0o400), 0, 0, "1020:1030:0400:file"},
		{"user write", 1021, 1031, fs.FileMode(0o200), 0, 0, "1021:1031:0200:file"},
		{"user execute", 1022, 1032, fs.FileMode(0o100), 0, 0, "1022:1032:0100:file"},

		{"group read", 1030, 1040, fs.FileMode(0o040), 0, 0, "1030:1040:0040:file"},
		{"group write", 1031, 1041, fs.FileMode(0o020), 0, 0, "1031:1041:0020:file"},
		{"group execute", 1032, 1042, fs.FileMode(0o010), 0, 0, "1032:1042:0010:file"},

		{"others read", 1040, 1050, fs.FileMode(0o004), 0, 0, "1040:1050:0004:file"},
		{"others write", 1041, 1051, fs.FileMode(0o002), 0, 0, "1041:1051:0002:file"},
		{"others execute", 1042, 1052, fs.FileMode(0o001), 0, 0, "1042:1052:0001:file"},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			result := FormatContainersOverrideXattrDevice(tt.uid, tt.gid, tt.mode, tt.major, tt.minor)
			require.Equal(t, tt.expected, result)

			stat, err := parseOverrideXattr([]byte(result))
			require.NoError(t, err)
			require.NotNil(t, stat)
			require.Equal(t, tt.uid, stat.IDs.UID)
			require.Equal(t, tt.gid, stat.IDs.GID)
			require.Equal(t, tt.mode, stat.Mode)
			require.Equal(t, tt.major, stat.Major)
			require.Equal(t, tt.minor, stat.Minor)
		})
	}
}

func TestParseDevice(t *testing.T) {
	tests := []struct {
		name      string
		input     string
		wantMajor int
		wantMinor int
		expectErr bool
	}{
		{"valid block device", "block-8-1", 8, 1, false},
		{"valid char device", "char-1-3", 1, 3, false},
		{"invalid prefix", "disk-1-2", 0, 0, true},
		{"invalid format: missing dashes", "block800", 0, 0, true},
		{"valid format: extra parts", "block-3-4-option_from_the_future", 3, 4, false},
		{"invalid major number", "block-abc-0", 0, 0, true},
		{"invalid minor number", "char-1-xyz", 0, 0, true},
		{"empty string", "", 0, 0, true},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			major, minor, err := parseDevice(tt.input)
			if tt.expectErr {
				assert.Error(t, err)
			} else {
				assert.NoError(t, err)
				assert.Equal(t, tt.wantMajor, major)
				assert.Equal(t, tt.wantMinor, minor)
			}
		})
	}
}