carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package cgroup
2
3import (
4 "reflect"
5 "strings"
6 "testing"
7)
8
9func TestShimSpecRoundTripsExactBytes(t *testing.T) {
10 in := shimSpec{
11 Path: "/run/carlos/exe/app",
12 Dir: "/data/app dir",
13 Argv: []string{"app", "", "two words", "line\nbreak", "--db", "/data/x.db"},
14 Env: []string{"A=1", "EMPTY=", "EQ=a=b", "BAD=\xff\xfe", "BIG=" + strings.Repeat("x", 1<<20)},
15 Expected: "/system.slice/carlos-edge.service/carlos-exec@a.example/g3",
16 KeepFDs: 5,
17 TestHold: 2,
18 SigIgn: 1<<0 | 1<<14 | 1<<63,
19 }
20 b, err := encodeShimSpec(in)
21 if err != nil {
22 t.Fatal(err)
23 }
24 out, err := decodeShimSpec(b)
25 if err != nil {
26 t.Fatal(err)
27 }
28 if !reflect.DeepEqual(in, out) {
29 t.Fatalf("round trip changed the spec")
30 }
31}
32
33// An empty argv and env round-trip as empty, not as one empty string.
34func TestShimSpecRoundTripsEmptyLists(t *testing.T) {
35 in := shimSpec{Path: "/x", Argv: []string{}, Env: []string{}, Expected: "/g", KeepFDs: 3}
36 b, err := encodeShimSpec(in)
37 if err != nil {
38 t.Fatal(err)
39 }
40 out, err := decodeShimSpec(b)
41 if err != nil {
42 t.Fatal(err)
43 }
44 if len(out.Argv) != 0 || len(out.Env) != 0 || out.Path != "/x" || out.Expected != "/g" || out.KeepFDs != 3 {
45 t.Fatalf("got %+v", out)
46 }
47}
48
49func TestShimSpecRefusesNUL(t *testing.T) {
50 if _, err := encodeShimSpec(shimSpec{Path: "/x", Argv: []string{"a\x00b"}, Expected: "/g", KeepFDs: 3}); err == nil {
51 t.Fatal("a NUL inside argv was encoded")
52 }
53 if _, err := encodeShimSpec(shimSpec{Path: "/x", Env: []string{"A=\x00"}, Expected: "/g", KeepFDs: 3}); err == nil {
54 t.Fatal("a NUL inside env was encoded")
55 }
56 if _, err := encodeShimSpec(shimSpec{Path: "/x\x00y", Expected: "/g", KeepFDs: 3}); err == nil {
57 t.Fatal("a NUL inside the path was encoded")
58 }
59}
60
61func TestShimSpecRefusesTruncatedAndTrailing(t *testing.T) {
62 b, _ := encodeShimSpec(shimSpec{Path: "/x", Argv: []string{"x"}, Env: []string{"A=1"}, Expected: "/g", KeepFDs: 3})
63 for _, bad := range [][]byte{b[:len(b)-1], append(append([]byte{}, b...), 'z', 0), []byte("carlos-cgexec-0\x00")} {
64 if _, err := decodeShimSpec(bad); err == nil {
65 t.Fatalf("decoded a bad spec %q", bad)
66 }
67 }
68}
69
70func TestShimSpecRefusesHugeCounts(t *testing.T) {
71 bad := []byte("carlos-cgexec-2\x00/x\x00\x00/g\x003\x000\x000\x001048577\x00")
72 if _, err := decodeShimSpec(bad); err == nil {
73 t.Fatal("decoded an argv count above 1<<20")
74 }
75 // The control: the same spec with a count in bounds decodes.
76 ok := []byte("carlos-cgexec-2\x00/x\x00\x00/g\x003\x000\x000\x000\x000\x00")
77 if _, err := decodeShimSpec(ok); err != nil {
78 t.Fatalf("the control spec: %v", err)
79 }
80}
81
82// The ignored-signal set is a 64-bit number: a bad one, and a spec in the
83// encoding before the set was added (magic carlos-cgexec-1), are refused.
84func TestShimSpecRefusesBadSigIgnAndTheOldEncoding(t *testing.T) {
85 for _, bad := range []string{
86 "carlos-cgexec-2\x00/x\x00\x00/g\x003\x000\x00x\x000\x000\x00",
87 "carlos-cgexec-2\x00/x\x00\x00/g\x003\x000\x0018446744073709551616\x000\x000\x00",
88 "carlos-cgexec-2\x00/x\x00\x00/g\x003\x000\x00-1\x000\x000\x00",
89 "carlos-cgexec-1\x00/x\x00\x00/g\x003\x000\x000\x000\x00",
90 } {
91 if _, err := decodeShimSpec([]byte(bad)); err == nil {
92 t.Fatalf("decoded a bad spec %q", bad)
93 }
94 }
95}
96