Debian Codegraph @

dockerclient/chmod_test.go

raw
1 package dockerclient
2
3 import (
4 "archive/tar"
5 "testing"
6
7 "github.com/tonistiigi/dchapes-mode"
8 )
9
10 // TestApplyChmod covers the tar-header rewrite done for --chmod in
11 // CopyContainer, which the conformance suite only exercises behind a build
12 // tag and a live daemon.
13 func TestApplyChmod(t *testing.T) {
14 tests := []struct {
15 name string
16 chmod string
17 typeflag byte
18 inMode int64
19 wantMode int64
20 }{
21 // Numeric modes replace the permission bits (and any special bits)
22 // of a regular file, keeping the file-type bits.
23 {name: "numeric basic", chmod: "755", typeflag: tar.TypeReg, inMode: 0o100644, wantMode: 0o100755},
24 {name: "numeric clears exec", chmod: "644", typeflag: tar.TypeReg, inMode: 0o100755, wantMode: 0o100644},
25 {name: "numeric setuid", chmod: "4755", typeflag: tar.TypeReg, inMode: 0o100644, wantMode: 0o104755},
26 {name: "numeric setgid", chmod: "2755", typeflag: tar.TypeReg, inMode: 0o100644, wantMode: 0o102755},
27 {name: "numeric sticky", chmod: "1755", typeflag: tar.TypeReg, inMode: 0o100644, wantMode: 0o101755},
28 // Numeric modes are absolute: a pre-existing setuid bit is cleared,
29 // matching chmod(1).
30 {name: "numeric clears setuid", chmod: "755", typeflag: tar.TypeReg, inMode: 0o104755, wantMode: 0o100755},
31 // Symbolic modes.
32 {name: "symbolic add exec", chmod: "+x", typeflag: tar.TypeReg, inMode: 0o100644, wantMode: 0o100755},
33 {name: "symbolic clause list", chmod: "u+x,go-w", typeflag: tar.TypeReg, inMode: 0o100644, wantMode: 0o100744},
34 {name: "symbolic multi group", chmod: "u+rX-w", typeflag: tar.TypeReg, inMode: 0o100700, wantMode: 0o100500},
35 {name: "symbolic setuid", chmod: "u+s", typeflag: tar.TypeReg, inMode: 0o100644, wantMode: 0o104644},
36 {name: "symbolic sticky", chmod: "+t", typeflag: tar.TypeReg, inMode: 0o100755, wantMode: 0o101755},
37 // The conditional X: granted on directories, and on regular files
38 // only if an execute bit is already set.
39 {name: "X on directory", chmod: "u=rwX,go=rX", typeflag: tar.TypeDir, inMode: 0o040644, wantMode: 0o040755},
40 {name: "X on non-exec regular", chmod: "u=rwX", typeflag: tar.TypeReg, inMode: 0o100644, wantMode: 0o100644},
41 {name: "X on exec regular", chmod: "a+rX", typeflag: tar.TypeReg, inMode: 0o100700, wantMode: 0o100755},
42 }
43 for _, tc := range tests {
44 t.Run(tc.name, func(t *testing.T) {
45 set, err := mode.Parse(tc.chmod)
46 if err != nil {
47 t.Fatalf("parsing %q: %v", tc.chmod, err)
48 }
49 h := &tar.Header{Typeflag: tc.typeflag, Mode: tc.inMode}
50 applyChmod(h, set)
51 if h.Mode != tc.wantMode {
52 t.Errorf("chmod %q on header %#o (%c): got %#o, want %#o",
53 tc.chmod, tc.inMode, tc.typeflag, h.Mode, tc.wantMode)
54 }
55 })
56 }
57 }