|
| 1 | +// SPDX-FileCopyrightText: Copyright The Lima Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package envutil |
| 5 | + |
| 6 | +import ( |
| 7 | + "os" |
| 8 | + "reflect" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func TestMatchesPattern(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + pattern string |
| 16 | + expected bool |
| 17 | + }{ |
| 18 | + {"PATH", "PATH", true}, |
| 19 | + {"PATH", "HOME", false}, |
| 20 | + {"SSH_AUTH_SOCK", "SSH_*", true}, |
| 21 | + {"SSH_AGENT_PID", "SSH_*", true}, |
| 22 | + {"HOME", "SSH_*", false}, |
| 23 | + {"XDG_CONFIG_HOME", "XDG_*", true}, |
| 24 | + {"_LIMA_TEST", "_*", true}, |
| 25 | + {"LIMA_HOME", "_*", false}, |
| 26 | + } |
| 27 | + |
| 28 | + for _, tt := range tests { |
| 29 | + t.Run(tt.name+"_matches_"+tt.pattern, func(t *testing.T) { |
| 30 | + result := matchesPattern(tt.name, tt.pattern) |
| 31 | + if result != tt.expected { |
| 32 | + t.Errorf("matchesPattern(%q, %q) = %v, want %v", tt.name, tt.pattern, result, tt.expected) |
| 33 | + } |
| 34 | + }) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func TestIsBlocked(t *testing.T) { |
| 39 | + patterns := []string{"PATH", "SSH_*", "XDG_*"} |
| 40 | + |
| 41 | + tests := []struct { |
| 42 | + name string |
| 43 | + expected bool |
| 44 | + }{ |
| 45 | + {"PATH", true}, |
| 46 | + {"HOME", false}, |
| 47 | + {"SSH_AUTH_SOCK", true}, |
| 48 | + {"XDG_CONFIG_HOME", true}, |
| 49 | + {"USER", false}, |
| 50 | + } |
| 51 | + |
| 52 | + for _, tt := range tests { |
| 53 | + t.Run(tt.name, func(t *testing.T) { |
| 54 | + result := isBlocked(tt.name, patterns) |
| 55 | + if result != tt.expected { |
| 56 | + t.Errorf("isBlocked(%q, %v) = %v, want %v", tt.name, patterns, result, tt.expected) |
| 57 | + } |
| 58 | + }) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func TestParseEnvList(t *testing.T) { |
| 63 | + tests := []struct { |
| 64 | + input string |
| 65 | + expected []string |
| 66 | + }{ |
| 67 | + {"", nil}, |
| 68 | + {"PATH", []string{"PATH"}}, |
| 69 | + {"PATH,HOME", []string{"PATH", "HOME"}}, |
| 70 | + {"PATH, HOME , USER", []string{"PATH", "HOME", "USER"}}, |
| 71 | + {" , , ", nil}, |
| 72 | + } |
| 73 | + |
| 74 | + for _, tt := range tests { |
| 75 | + t.Run(tt.input, func(t *testing.T) { |
| 76 | + result := parseEnvList(tt.input) |
| 77 | + if !reflect.DeepEqual(result, tt.expected) { |
| 78 | + t.Errorf("parseEnvList(%q) = %v, want %v", tt.input, result, tt.expected) |
| 79 | + } |
| 80 | + }) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func TestGetFilterConfig(t *testing.T) { |
| 85 | + originalBlock := os.Getenv("LIMA_SHELLENV_BLOCK") |
| 86 | + originalAllow := os.Getenv("LIMA_SHELLENV_ALLOW") |
| 87 | + defer func() { |
| 88 | + if originalBlock != "" { |
| 89 | + t.Setenv("LIMA_SHELLENV_BLOCK", originalBlock) |
| 90 | + } |
| 91 | + if originalAllow != "" { |
| 92 | + t.Setenv("LIMA_SHELLENV_ALLOW", originalAllow) |
| 93 | + } |
| 94 | + }() |
| 95 | + |
| 96 | + t.Run("default config", func(t *testing.T) { |
| 97 | + os.Unsetenv("LIMA_SHELLENV_BLOCK") |
| 98 | + os.Unsetenv("LIMA_SHELLENV_ALLOW") |
| 99 | + |
| 100 | + config := GetFilterConfig() |
| 101 | + if !config.UseBuiltinBlocklist { |
| 102 | + t.Error("Expected UseBuiltinBlocklist to be true") |
| 103 | + } |
| 104 | + if !reflect.DeepEqual(config.BlockList, BuiltinBlocklist) { |
| 105 | + t.Error("Expected BlockList to equal BuiltinBlocklist") |
| 106 | + } |
| 107 | + if len(config.AllowList) != 0 { |
| 108 | + t.Error("Expected AllowList to be empty") |
| 109 | + } |
| 110 | + }) |
| 111 | + |
| 112 | + t.Run("custom blocklist", func(t *testing.T) { |
| 113 | + t.Setenv("LIMA_SHELLENV_BLOCK", "PATH,HOME") |
| 114 | + os.Unsetenv("LIMA_SHELLENV_ALLOW") |
| 115 | + |
| 116 | + config := GetFilterConfig() |
| 117 | + if config.UseBuiltinBlocklist { |
| 118 | + t.Error("Expected UseBuiltinBlocklist to be false") |
| 119 | + } |
| 120 | + expected := []string{"PATH", "HOME"} |
| 121 | + if !reflect.DeepEqual(config.BlockList, expected) { |
| 122 | + t.Errorf("Expected BlockList to be %v, got %v", expected, config.BlockList) |
| 123 | + } |
| 124 | + }) |
| 125 | + |
| 126 | + t.Run("additive blocklist", func(t *testing.T) { |
| 127 | + t.Setenv("LIMA_SHELLENV_BLOCK", "+CUSTOM_VAR") |
| 128 | + os.Unsetenv("LIMA_SHELLENV_ALLOW") |
| 129 | + |
| 130 | + config := GetFilterConfig() |
| 131 | + if !config.UseBuiltinBlocklist { |
| 132 | + t.Error("Expected UseBuiltinBlocklist to be true") |
| 133 | + } |
| 134 | + expected := make([]string, len(BuiltinBlocklist)+1) |
| 135 | + copy(expected, BuiltinBlocklist) |
| 136 | + expected[len(BuiltinBlocklist)] = "CUSTOM_VAR" |
| 137 | + if !reflect.DeepEqual(config.BlockList, expected) { |
| 138 | + t.Errorf("Expected BlockList to include builtin + custom, got %v", config.BlockList) |
| 139 | + } |
| 140 | + }) |
| 141 | + |
| 142 | + t.Run("allowlist", func(t *testing.T) { |
| 143 | + os.Unsetenv("LIMA_SHELLENV_BLOCK") |
| 144 | + t.Setenv("LIMA_SHELLENV_ALLOW", "FOO,BAR") |
| 145 | + |
| 146 | + config := GetFilterConfig() |
| 147 | + expected := []string{"FOO", "BAR"} |
| 148 | + if !reflect.DeepEqual(config.AllowList, expected) { |
| 149 | + t.Errorf("Expected AllowList to be %v, got %v", expected, config.AllowList) |
| 150 | + } |
| 151 | + }) |
| 152 | +} |
| 153 | + |
| 154 | +func TestFilterEnvironment(t *testing.T) { |
| 155 | + testEnv := []string{ |
| 156 | + "PATH=/usr/bin", |
| 157 | + "HOME=/home/user", |
| 158 | + "USER=testuser", |
| 159 | + "FOO=bar", |
| 160 | + "SSH_AUTH_SOCK=/tmp/ssh", |
| 161 | + "XDG_CONFIG_HOME=/config", |
| 162 | + } |
| 163 | + |
| 164 | + originalEnviron := os.Environ |
| 165 | + defer func() { |
| 166 | + _ = originalEnviron |
| 167 | + }() |
| 168 | + |
| 169 | + t.Run("default blocklist", func(_ *testing.T) { |
| 170 | + config := &FilterConfig{ |
| 171 | + BlockList: BuiltinBlocklist, |
| 172 | + UseBuiltinBlocklist: true, |
| 173 | + } |
| 174 | + |
| 175 | + _ = config |
| 176 | + _ = testEnv |
| 177 | + }) |
| 178 | +} |
| 179 | + |
| 180 | +func TestGetBuiltinBlocklist(t *testing.T) { |
| 181 | + blocklist := GetBuiltinBlocklist() |
| 182 | + |
| 183 | + if &blocklist[0] == &BuiltinBlocklist[0] { |
| 184 | + t.Error("GetBuiltinBlocklist should return a copy, not the original slice") |
| 185 | + } |
| 186 | + |
| 187 | + if !reflect.DeepEqual(blocklist, BuiltinBlocklist) { |
| 188 | + t.Error("GetBuiltinBlocklist should return the same content as BuiltinBlocklist") |
| 189 | + } |
| 190 | + |
| 191 | + expectedItems := []string{"PATH", "HOME", "SSH_*"} |
| 192 | + for _, item := range expectedItems { |
| 193 | + found := false |
| 194 | + for _, blocked := range blocklist { |
| 195 | + if blocked == item { |
| 196 | + found = true |
| 197 | + break |
| 198 | + } |
| 199 | + } |
| 200 | + if !found { |
| 201 | + t.Errorf("Expected builtin blocklist to contain %q", item) |
| 202 | + } |
| 203 | + } |
| 204 | +} |
0 commit comments