|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestIsValidTaskName(t *testing.T) { |
| 9 | + t.Run("valid names", func(t *testing.T) { |
| 10 | + valid := []string{ |
| 11 | + "process-image", |
| 12 | + "my_task", |
| 13 | + "abc123", |
| 14 | + "A", |
| 15 | + "z", |
| 16 | + "TaskName", |
| 17 | + "a-b_c-d", |
| 18 | + strings.Repeat("a", 128), |
| 19 | + } |
| 20 | + for _, name := range valid { |
| 21 | + if !isValidTaskName(name) { |
| 22 | + t.Errorf("expected %q to be valid", name) |
| 23 | + } |
| 24 | + } |
| 25 | + }) |
| 26 | + |
| 27 | + t.Run("invalid names", func(t *testing.T) { |
| 28 | + invalid := []string{ |
| 29 | + "../etc/passwd", |
| 30 | + "foo/bar", |
| 31 | + "bad.name", |
| 32 | + "has.dot", |
| 33 | + "", |
| 34 | + strings.Repeat("a", 129), |
| 35 | + "has space", |
| 36 | + "special!char", |
| 37 | + "semi;colon", |
| 38 | + "back\\slash", |
| 39 | + "at@sign", |
| 40 | + "hash#tag", |
| 41 | + } |
| 42 | + for _, name := range invalid { |
| 43 | + if isValidTaskName(name) { |
| 44 | + t.Errorf("expected %q to be invalid", name) |
| 45 | + } |
| 46 | + } |
| 47 | + }) |
| 48 | +} |
| 49 | + |
| 50 | +func TestIsPathWithin(t *testing.T) { |
| 51 | + t.Run("path inside base", func(t *testing.T) { |
| 52 | + cases := []struct { |
| 53 | + path, base string |
| 54 | + }{ |
| 55 | + {"/opt/bin/task", "/opt/bin"}, |
| 56 | + {"/opt/bin/sub/task", "/opt/bin"}, |
| 57 | + {"/opt/bin", "/opt/bin"}, |
| 58 | + } |
| 59 | + for _, tc := range cases { |
| 60 | + if !isPathWithin(tc.path, tc.base) { |
| 61 | + t.Errorf("expected isPathWithin(%q, %q) = true", tc.path, tc.base) |
| 62 | + } |
| 63 | + } |
| 64 | + }) |
| 65 | + |
| 66 | + t.Run("path outside base", func(t *testing.T) { |
| 67 | + cases := []struct { |
| 68 | + path, base string |
| 69 | + }{ |
| 70 | + {"/opt/bin/../etc/passwd", "/opt/bin"}, |
| 71 | + {"/opt/other/task", "/opt/bin"}, |
| 72 | + {"/opt/bi", "/opt/bin"}, |
| 73 | + } |
| 74 | + for _, tc := range cases { |
| 75 | + if isPathWithin(tc.path, tc.base) { |
| 76 | + t.Errorf("expected isPathWithin(%q, %q) = false", tc.path, tc.base) |
| 77 | + } |
| 78 | + } |
| 79 | + }) |
| 80 | +} |
| 81 | + |
| 82 | +func TestFilterEnv(t *testing.T) { |
| 83 | + t.Run("allows permitted vars", func(t *testing.T) { |
| 84 | + input := []string{ |
| 85 | + "PATH=/usr/bin", |
| 86 | + "HOME=/home/user", |
| 87 | + "LANG=en_US.UTF-8", |
| 88 | + "USER=testuser", |
| 89 | + "GOPATH=/go", |
| 90 | + "TMPDIR=/tmp", |
| 91 | + "TEMP=/tmp", |
| 92 | + "TMP=/tmp", |
| 93 | + "LC_ALL=C", |
| 94 | + } |
| 95 | + result := filterEnv(input) |
| 96 | + if len(result) != len(input) { |
| 97 | + t.Errorf("expected %d vars, got %d", len(input), len(result)) |
| 98 | + } |
| 99 | + for i, v := range result { |
| 100 | + if v != input[i] { |
| 101 | + t.Errorf("expected %q, got %q", input[i], v) |
| 102 | + } |
| 103 | + } |
| 104 | + }) |
| 105 | + |
| 106 | + t.Run("filters sensitive vars", func(t *testing.T) { |
| 107 | + input := []string{ |
| 108 | + "PATH=/usr/bin", |
| 109 | + "AWS_SECRET_ACCESS_KEY=supersecret", |
| 110 | + "DATABASE_URL=postgres://...", |
| 111 | + "GOVEL_AUTH_TOKEN=mytoken", |
| 112 | + "API_KEY=abc123", |
| 113 | + "HOME=/home/user", |
| 114 | + } |
| 115 | + result := filterEnv(input) |
| 116 | + if len(result) != 2 { |
| 117 | + t.Errorf("expected 2 vars, got %d: %v", len(result), result) |
| 118 | + } |
| 119 | + expected := map[string]bool{ |
| 120 | + "PATH=/usr/bin": true, |
| 121 | + "HOME=/home/user": true, |
| 122 | + } |
| 123 | + for _, v := range result { |
| 124 | + if !expected[v] { |
| 125 | + t.Errorf("unexpected env var in output: %q", v) |
| 126 | + } |
| 127 | + } |
| 128 | + }) |
| 129 | + |
| 130 | + t.Run("empty input", func(t *testing.T) { |
| 131 | + result := filterEnv([]string{}) |
| 132 | + if len(result) != 0 { |
| 133 | + t.Errorf("expected empty result, got %v", result) |
| 134 | + } |
| 135 | + }) |
| 136 | + |
| 137 | + t.Run("malformed entries ignored", func(t *testing.T) { |
| 138 | + input := []string{ |
| 139 | + "NOEQUALS", |
| 140 | + "PATH=/usr/bin", |
| 141 | + } |
| 142 | + result := filterEnv(input) |
| 143 | + if len(result) != 1 { |
| 144 | + t.Errorf("expected 1 var, got %d: %v", len(result), result) |
| 145 | + } |
| 146 | + }) |
| 147 | +} |
0 commit comments