-
Notifications
You must be signed in to change notification settings - Fork 7
/
feature_test.go
797 lines (700 loc) · 21.4 KB
/
feature_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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
// Copyright 2013 Brady Catherman
//
// 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
//
// http://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.
// +build linux
package goclone
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"strconv"
"strings"
"syscall"
"testing"
"time"
)
// Returns an error if the given command line doesn't match the expected
// command line for the given process.
func checkCommandLine(pid int, command string) error {
cmdlineFile := fmt.Sprintf("/proc/%d/cmdline", pid)
contents, err := ioutil.ReadFile(cmdlineFile)
if err != nil {
return err
}
arg1 := bytes.Split(contents, []byte{0})[0]
if !bytes.Equal(arg1, []byte(command)) {
return fmt.Errorf("Command line not correct: %s\n", string(contents))
}
return nil
}
// Waits for up to 5 seconds for the given command line to appear in the given
// pids proc file. This is necessary since we do not get a notification when
// the process has called exec().
func waitForProcessStart(pid int, command string) error {
end := time.Now().Add(time.Second * 5)
for time.Now().Before(end) {
if checkCommandLine(pid, command) == nil {
return nil
}
time.Sleep(time.Millisecond)
}
return fmt.Errorf("Process never had the correct command line.")
}
func TestFeaturePath(t *testing.T) {
cmd := &Cmd{
Path: sleepBin,
Args: []string{sleepBin, "60"},
}
if err := cmd.Start(); err != nil {
t.Fatalf("Unexpected error: %s", err)
}
defer cmd.Wait()
defer cmd.Process.Kill()
if err := waitForProcessStart(cmd.Process.Pid, sleepBin); err != nil {
t.Fatalf("%s", err)
}
// Read the full command line and make sure that its valid.
cmdlineFile := fmt.Sprintf("/proc/%d/cmdline", cmd.Process.Pid)
cmdline, err := ioutil.ReadFile(cmdlineFile)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
want := bytes.Join([][]byte{
[]byte(sleepBin),
[]byte("60"),
[]byte(""),
}, []byte{0})
if !bytes.Equal(cmdline, want) {
t.Fatalf("Command line not correct: %#v", cmdline)
}
}
func TestFeaturePathArgsDifference(t *testing.T) {
falseName := "false_bin_name"
cmd := &Cmd{
Path: sleepBin,
Args: []string{falseName, "60"},
}
if err := cmd.Start(); err != nil {
t.Fatalf("Unexpected error: %s", err)
}
defer cmd.Wait()
defer cmd.Process.Kill()
if err := waitForProcessStart(cmd.Process.Pid, falseName); err != nil {
t.Fatalf("%s", err)
}
// Read the full command line and make sure that its valid.
cmdlineFile := fmt.Sprintf("/proc/%d/cmdline", cmd.Process.Pid)
cmdline, err := ioutil.ReadFile(cmdlineFile)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
want := bytes.Join([][]byte{
[]byte(falseName),
[]byte("60"),
[]byte(""),
}, []byte{0})
if !bytes.Equal(cmdline, want) {
t.Fatalf("Command line not correct: %#v", cmdline)
}
}
func TestFeatureEnv(t *testing.T) {
cmd := Command(sleepBin, "60")
cmd.Env = []string{"A=B", "B=C", "TEST=TEST"}
if err := cmd.Start(); err != nil {
t.Fatalf("Unexpected error: %s", err)
}
defer cmd.Wait()
defer cmd.Process.Kill()
if err := waitForProcessStart(cmd.Process.Pid, sleepBin); err != nil {
t.Fatalf("%s", err)
}
// Read the processes environment.
environFile := fmt.Sprintf("/proc/%d/environ", cmd.Process.Pid)
environ, err := ioutil.ReadFile(environFile)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
env := bytes.Split(environ, []byte{0})
if len(env) != len(cmd.Env)+1 {
t.Fatalf("Environment variables are the wrong length: %d", len(env))
}
for i, kv := range env[0 : len(env)-1] {
if string(kv) != cmd.Env[i] {
t.Fatalf("Unexpected environment: %s", string(kv))
}
}
}
func TestFeatureDir(t *testing.T) {
cmd := Command(sleepBin, "60")
cmd.Dir = os.TempDir()
if err := cmd.Start(); err != nil {
t.Fatalf("Unexpected error: %s", err)
}
defer cmd.Wait()
defer cmd.Process.Kill()
if err := waitForProcessStart(cmd.Process.Pid, sleepBin); err != nil {
t.Fatalf("%s", err)
}
cwdFile := fmt.Sprintf("/proc/%d/cwd", cmd.Process.Pid)
cwd, err := os.Readlink(cwdFile)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
if cwd != os.TempDir() {
t.Fatalf("Process has the wrong cwd: %s", cwd)
}
}
func TestFeatureStderr(t *testing.T) {
cmd := Command(shBin, "-c", fmt.Sprintf("%s expected output >&2", echoBin))
stderr, err := cmd.StderrPipe()
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
if err := cmd.Start(); err != nil {
t.Fatalf("Unexpected error: %s", err)
}
defer cmd.Wait()
defer cmd.Process.Kill()
output, err := ioutil.ReadAll(stderr)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
} else if string(output) != "expected output\n" {
t.Fatalf("Unexpected output: %s", string(output))
}
}
func TestFeatureStdin(t *testing.T) {
cmd := Command(catBin)
cmd.Stdin = strings.NewReader("expected output")
stdout, err := cmd.StdoutPipe()
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
if err := cmd.Start(); err != nil {
t.Fatalf("Unexpected error: %s", err)
}
defer cmd.Wait()
defer cmd.Process.Kill()
output, err := ioutil.ReadAll(stdout)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
} else if string(output) != "expected output" {
t.Fatalf("Unexpected output: %s", string(output))
}
}
func TestFeatureStdout(t *testing.T) {
cmd := Command(echoBin, "expected output")
stdout, err := cmd.StdoutPipe()
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
if err := cmd.Start(); err != nil {
t.Fatalf("Unexpected error: %s", err)
}
defer cmd.Wait()
defer cmd.Process.Kill()
output, err := ioutil.ReadAll(stdout)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
} else if string(output) != "expected output\n" {
t.Fatalf("Unexpected output: %s", string(output))
}
}
func TestFeatureExtraFiles(t *testing.T) {
openFile := func() *os.File {
fd, err := ioutil.TempFile("", "TestFeatureExtraFiles")
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
return fd
}
extraFiles := []*os.File{}
defer func() {
for _, fd := range extraFiles {
if fd != nil {
os.Remove(fd.Name())
fd.Close()
}
}
}()
// Make a map of 5 extra files and some nils.
extraFiles = append(extraFiles, openFile())
extraFiles = append(extraFiles, nil)
extraFiles = append(extraFiles, openFile())
extraFiles = append(extraFiles, nil)
extraFiles = append(extraFiles, openFile())
extraFiles = append(extraFiles, nil)
extraFiles = append(extraFiles, openFile())
extraFiles = append(extraFiles, nil)
extraFiles = append(extraFiles, openFile())
cmd := Command(sleepBin, "60")
cmd.ExtraFiles = extraFiles
if err := cmd.Start(); err != nil {
t.Fatalf("Unexpected error: %s", err)
}
defer cmd.Wait()
defer cmd.Process.Kill()
if err := waitForProcessStart(cmd.Process.Pid, sleepBin); err != nil {
t.Fatalf("%s", err)
}
fdDir := fmt.Sprintf("/proc/%d/fd", cmd.Process.Pid)
dirs, err := ioutil.ReadDir(fdDir)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
fdMap := make(map[int]string)
for _, dir := range dirs {
link, err := os.Readlink(path.Join(fdDir, dir.Name()))
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
i, err := strconv.Atoi(dir.Name())
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
fdMap[i] = link
}
// Walk the map and verify that it looks the way it should.
for fd, link := range fdMap {
switch fd {
case 0, 1, 2:
if link != os.DevNull {
t.Errorf(
"%d should link to %s, links to %s instead",
fd, link, os.DevNull)
}
case 3, 5, 7, 9, 11:
if link != extraFiles[fd-3].Name() {
t.Errorf(
"%d should link to %s, links to %s instead",
fd, extraFiles[fd-3].Name(), link)
}
default:
t.Errorf("Unknown open file: %d (%s)", fd, link)
}
}
}
func TestFeatureSysProcAttrCredential(t *testing.T) {
// Only run this test if the process is running as root (since we need to
// be able to switch users for this.)
if os.Getuid() != 0 {
t.Skipf("This test must be run as root.")
}
cmd := Command(sleepBin, "60")
cmd.SysProcAttr = &syscall.SysProcAttr{}
cmd.SysProcAttr.Credential = &syscall.Credential{}
cmd.SysProcAttr.Credential.Uid = 100
cmd.SysProcAttr.Credential.Gid = 101
cmd.SysProcAttr.Credential.Groups = []uint32{102, 103, 104}
if err := cmd.Start(); err != nil {
t.Fatalf("Unexpected error: %s", err)
}
defer cmd.Wait()
defer cmd.Process.Kill()
if err := waitForProcessStart(cmd.Process.Pid, sleepBin); err != nil {
t.Fatalf("%s", err)
}
// Read the status file to see what the current working uid and gid of the
// process are.
statusFile := fmt.Sprintf("/proc/%d/status", cmd.Process.Pid)
contents, err := ioutil.ReadFile(statusFile)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
for _, line := range bytes.Split(contents, []byte("\n")) {
parts := bytes.Split(line, []byte(":"))
if len(parts) != 2 {
continue
}
switch string(parts[0]) {
case "Uid":
expect := []byte("\t100\t100\t100\t100")
if !bytes.Equal(expect, parts[1]) {
t.Fatalf("Unexpected Uid line value: %s", string(parts[1]))
}
case "Gid":
expect := []byte("\t101\t101\t101\t101")
if !bytes.Equal(expect, parts[1]) {
t.Fatalf("Unexpected Gid line value: %s", string(parts[1]))
}
case "Groups":
expect := []byte("\t102 103 104 ")
if !bytes.Equal(expect, parts[1]) {
t.Fatalf("Unexpected Groups line value: %s", string(parts[1]))
}
}
}
}
func TestFeatureSysProcAttrChroot(t *testing.T) {
// Check ti see if /bin/busybox exists, without it we will not have
// a static binary to run in the root.
sourceFd, err := os.Open("/bin/busybox")
if err != nil {
if os.IsNotExist(err) {
t.Skipf("Skipping due to a missing /bin/busybox")
}
t.Fatalf("Unknown error stating busybox: %s", err)
}
// Next we make a directory to "chroot" into, and copy the busybox binary
// into it.
mode := os.FileMode(0777)
flags := os.O_WRONLY | os.O_CREATE | os.O_TRUNC
dir, err := ioutil.TempDir("", "goclone")
if err != nil {
t.Fatalf("Error making temporary directory: %s", err)
}
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Fatalf("Error removing temp dir: %s", err)
}
}()
if destFd, err := os.OpenFile(dir+"/busybox", flags, mode); err != nil {
t.Fatalf("Error creating file: %s", err)
} else if _, err := io.Copy(destFd, sourceFd); err != nil {
t.Fatalf("Error copying file contents: %s", err)
} else if err := sourceFd.Close(); err != nil {
t.Fatalf("Error closing the source file: %s", err)
} else if err := destFd.Close(); err != nil {
t.Fatalf("Error closing the dest file: %s", err)
}
// Now we can attempt to execute the busybox binary in a chrooted
// environment.
cmd := Command("/busybox", "sleep", "10")
cmd.SysProcAttr = &syscall.SysProcAttr{Chroot: dir}
if err := cmd.Start(); err != nil {
t.Fatalf("Error starting the command: %s", err)
}
defer cmd.Wait()
defer cmd.Process.Kill()
// Wait for the process to fully start.
if err := waitForProcessStart(cmd.Process.Pid, "/busybox"); err != nil {
t.Fatalf("%s", err)
}
// Start with the link.. The link should be to the directory we created
// above.
root := fmt.Sprintf("/proc/%d/root", cmd.Process.Pid)
if link, err := os.Readlink(root); err != nil {
t.Fatalf("Error reading the root link: %s", err)
} else if link != dir {
t.Fatalf("Process had the wrong root, should be %s, is %s", dir, link)
}
}
func TestFeatureSysProcAttrPtrace(t *testing.T) {
t.Skip("TODO: Funtionality not implemented.")
}
func TestFeatureSysProcAttrSetsid(t *testing.T) {
t.Skip("TODO: Funtionality not implemented.")
}
func TestFeatureSysProcAttrSetpgid(t *testing.T) {
t.Skip("TODO: Funtionality not implemented.")
}
func TestFeatureSysProcAttrSetCtty(t *testing.T) {
t.Skip("TODO: Funtionality not implemented.")
}
func TestFeatureSysProcAttrNotty(t *testing.T) {
t.Skip("TODO: Funtionality not implemented.")
}
func TestFeatureSysProcAttrCtty(t *testing.T) {
t.Skip("TODO: Funtionality not implemented.")
}
func TestFeatureSysProcAttrPdeathsig(t *testing.T) {
t.Skip("FIXME: Test not implemented.")
}
func TestFeatureCgroupsTasksFiles(t *testing.T) {
// Create some fake cgroups files so we don't need cgroups setup
// or anything special.
cgroupsTasksFiles := []string{}
defer func() {
for _, file := range cgroupsTasksFiles {
if err := os.Remove(file); err != nil {
t.Errorf("Unexpected error: %s", err)
}
}
}()
for i := 0; i < 5; i++ {
fn, err := ioutil.TempFile("", "TestFeatureCgroupsTasksFiles")
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
cgroupsTasksFiles = append(cgroupsTasksFiles, fn.Name())
if err := fn.Close(); err != nil {
t.Fatalf("Unexpected error: %s", err)
}
}
cmd := Command(trueBin)
cmd.CgroupsTasksFiles = cgroupsTasksFiles
if err := cmd.Run(); err != nil {
t.Fatalf("Unexpected error: %s", err)
}
// Check that the pid ended up in each of the cgroups tasks files.
expected := []byte(fmt.Sprintf("%d\n", cmd.Process.Pid))
for _, file := range cgroupsTasksFiles {
if contents, err := ioutil.ReadFile(file); err != nil {
t.Fatalf("Unexpected error: %s", err)
} else if !bytes.Equal(expected, contents) {
t.Fatalf("Unexpected contents: %s", contents)
}
}
}
func TestFeatureDoubleFork(t *testing.T) {
t.Skipf("FIXME: Test not implemented.")
}
func TestFeatureIPCNameSpace(t *testing.T) {
// Check to see if ipc namespaces are supported.
if v, ok := SupportedNamespaces(t)["ipc"]; !ok || !v {
t.Skip("Kernel doesn't support ipc namespaces.")
}
t.Skipf("FIXME: Test not implemented.")
}
func TestFeatureMountNameSpace(t *testing.T) {
// Check to see if mount namespaces are supported.
if v, ok := SupportedNamespaces(t)["mnt"]; !ok || !v {
t.Skip("Kernel doesn't support mnt namespaces.")
}
t.Skipf("FIXME: Test not implemented.")
}
func TestFeatureNetworkNameSpace(t *testing.T) {
// Check to see if network namespaces are supported.
if v, ok := SupportedNamespaces(t)["net"]; !ok || !v {
t.Skip("Kernel doesn't support network namespaces.")
}
t.Skipf("FIXME: Test not implemented.")
}
func TestFeatureUserNameSpace(t *testing.T) {
// Check to see if user namespaces are supported.
if v, ok := SupportedNamespaces(t)["user"]; !ok || !v {
t.Skip("Kernel doesn't support user namespaces.")
}
t.Skipf("FIXME: Test not implemented.")
}
func TestFeatureUTSNameSpace(t *testing.T) {
// Check to see if UTS namespaces are supported.
if v, ok := SupportedNamespaces(t)["uts"]; !ok || !v {
t.Skip("Kernel doesn't support uts namespaces.")
}
t.Skipf("FIXME: Test not implemented.")
}
func TestFeatureNewIPCNameSpace(t *testing.T) {
if v, ok := SupportedNamespaces(t)["ipc"]; !ok || !v {
t.Skip("Kernel doesn't support IPC namespaces.")
}
cmd := Command(sleepBin, "60")
cmd.NewIPCNameSpace = true
if err := cmd.Start(); err != nil {
t.Fatalf("Unexpected error: %s", err)
}
defer cmd.Wait()
defer cmd.Process.Kill()
if err := waitForProcessStart(cmd.Process.Pid, sleepBin); err != nil {
t.Fatalf("%s", err)
}
// Read my processes' pid name space.
myNS, err := os.Readlink("/proc/self/ns/ipc")
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
// Read the child processes' pid name space.
childNSFile := fmt.Sprintf("/proc/%d/ns/ipc", cmd.Process.Pid)
childNS, err := os.Readlink(childNSFile)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
// Make sure that they are NOT the same.
if childNS == myNS {
t.Fatalf("Namespace of child not changed.")
}
}
func TestFeatureNewMountNameSpace(t *testing.T) {
// Check to see if mnt namespaces are supported.
if v, ok := SupportedNamespaces(t)["mnt"]; !ok || !v {
t.Skip("Kernel doesn't support mnt namespaces.")
}
cmd := Command(sleepBin, "60")
cmd.NewMountNameSpace = true
if err := cmd.Start(); err != nil {
t.Fatalf("Unexpected error: %s", err)
}
defer cmd.Wait()
defer cmd.Process.Kill()
if err := waitForProcessStart(cmd.Process.Pid, sleepBin); err != nil {
t.Fatalf("%s", err)
}
// Read my processes' pid name space.
myNS, err := os.Readlink("/proc/self/ns/mnt")
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
// Read the child processes' pid name space.
childNSFile := fmt.Sprintf("/proc/%d/ns/mnt", cmd.Process.Pid)
childNS, err := os.Readlink(childNSFile)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
// Make sure that they are NOT the same.
if childNS == myNS {
t.Fatalf("Namespace of child not changed.")
}
}
func TestFeatureNewNetworkNameSpace(t *testing.T) {
// Check to see if network namespaces are supported.
if v, ok := SupportedNamespaces(t)["network"]; !ok || !v {
t.Skip("Kernel doesn't support network namespaces.")
}
t.Skipf("FIXME: Test not implemented.")
}
func TestFeatureNewPIDNameSpace(t *testing.T) {
// Check to see if PID namespaces are supported.
if v, ok := SupportedNamespaces(t)["pid"]; !ok || !v {
t.Skip("Kernel doesn't support pid namespaces.")
}
cmd := Command(sleepBin, "60")
cmd.NewPIDNameSpace = true
if err := cmd.Start(); err != nil {
t.Fatalf("Unexpected error: %s", err)
}
defer cmd.Wait()
defer cmd.Process.Kill()
if err := waitForProcessStart(cmd.Process.Pid, sleepBin); err != nil {
t.Fatalf("%s", err)
}
// Read my processes' pid name space.
myNS, err := os.Readlink("/proc/self/ns/pid")
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
// Read the child processes' pid name space.
childNSFile := fmt.Sprintf("/proc/%d/ns/pid", cmd.Process.Pid)
childNS, err := os.Readlink(childNSFile)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
// Make sure that they are NOT the same.
if childNS == myNS {
t.Fatalf("Namespace of child not changed.")
}
}
func TestFeatureNewUserNameSpace(t *testing.T) {
// Check to see if user namespaces are supported.
if v, ok := SupportedNamespaces(t)["user"]; !ok || !v {
t.Skip("Kernel doesn't support user namespaces.")
}
cmd := Command(sleepBin, "60")
cmd.NewUserNameSpace = true
if err := cmd.Start(); err != nil {
t.Fatalf("Unexpected error: %s", err)
}
defer cmd.Wait()
defer cmd.Process.Kill()
if err := waitForProcessStart(cmd.Process.Pid, sleepBin); err != nil {
t.Fatalf("%s", err)
}
// Read my processes' pid name space.
myNS, err := os.Readlink("/proc/self/ns/user")
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
// Read the child processes' pid name space.
childNSFile := fmt.Sprintf("/proc/%d/ns/user", cmd.Process.Pid)
childNS, err := os.Readlink(childNSFile)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
// Make sure that they are NOT the same.
if childNS == myNS {
t.Fatalf("Namespace of child not changed.")
}
}
func TestFeatureNewUTSNameSpace(t *testing.T) {
// Check to see if UTC namespaces are supported.
if v, ok := SupportedNamespaces(t)["uts"]; !ok || !v {
t.Skip("Kernel doesn't support uts namespaces.")
}
cmd := Command(sleepBin, "60")
cmd.NewUTSNameSpace = true
if err := cmd.Start(); err != nil {
t.Fatalf("Unexpected error: %s", err)
}
defer cmd.Wait()
defer cmd.Process.Kill()
if err := waitForProcessStart(cmd.Process.Pid, sleepBin); err != nil {
t.Fatalf("%s", err)
}
// Read my processes' pid name space.
myNS, err := os.Readlink("/proc/self/ns/uts")
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
// Read the child processes' pid name space.
childNSFile := fmt.Sprintf("/proc/%d/ns/uts", cmd.Process.Pid)
childNS, err := os.Readlink(childNSFile)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
// Make sure that they are NOT the same.
if childNS == myNS {
t.Fatalf("Namespace of child not changed.")
}
}
func TestFeatureUserMap(t *testing.T) {
// Check to see if user namespaces are supported.
if v, ok := SupportedNamespaces(t)["user"]; !ok || !v {
t.Skip("Kernel doesn't support user namespaces.")
}
// Make a temporary directory.
tmpDir, err := ioutil.TempDir("", "TestFeatureNewUserNameSpace")
if err != nil {
t.Fatalf("Error maping a temporary directory: %s", err)
}
defer func() {
if err := os.RemoveAll(tmpDir); err != nil {
t.Fatalf("Error removing temp directory: %s", err)
}
}()
// Make sure that the user can write to that temporary directory.
if err := os.Chmod(tmpDir, 0777); err != nil {
t.Fatalf("Error chmoding the temp directory: %s", err)
}
// Run a command that should touch a file in that temp directory as
// root in the user name space.
tmpFile := tmpDir + "/test"
c := Command(touchBin, tmpFile)
c.NewUserNameSpace = true
c.UserMap = []MapElement{
MapElement{Inside: 0, Outside: 1000, Length: 1},
}
c.SysProcAttr = &syscall.SysProcAttr{}
c.SysProcAttr.Credential = &syscall.Credential{}
c.SysProcAttr.Credential.Uid = 0
c.SysProcAttr.Credential.Gid = 0
if err := c.Start(); err != nil {
t.Fatalf("Error running the command: %s", err)
} else if err := c.Wait(); err != nil {
t.Fatalf("Error waiting on the command: %s", err)
}
// See if the file was created.
var stat syscall.Stat_t
if err := syscall.Stat(tmpFile, &stat); err != nil {
t.Fatalf("Error while stating the output file: %s", err)
} else if stat.Uid != 1000 {
t.Fatalf("User was not set properly, expected 1000, got %d", stat.Uid)
}
}
func TestFeatureGroupMap(t *testing.T) {
// Check to see if user namespaces are supported.
if v, ok := SupportedNamespaces(t)["user"]; !ok || !v {
t.Skip("Kernel doesn't support user namespaces.")
}
t.Skip("FIXME: Not implemented.")
}