| // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | package v1 |
| 16 |
| 17 | import ( |
| 18 | "context" |
| 19 | "os" |
| 20 | "path/filepath" |
| 21 | "testing" |
| 22 | ) |
| 23 |
| 24 | func TestValueSource_Validate(t *testing.T) { |
| 25 | tests := []struct { |
| 26 | name string |
| 27 | vs *ValueSource |
| 28 | wantErr bool |
| 29 | }{ |
| 30 | { |
| 31 | name: "nil valueSource", |
| 32 | vs: nil, |
| 33 | wantErr: true, |
| 34 | }, |
| 35 | { |
| 36 | name: "unsupported type", |
| 37 | vs: &ValueSource{ |
| 38 | Type: "unsupported", |
| 39 | }, |
| 40 | wantErr: true, |
| 41 | }, |
| 42 | { |
| 43 | name: "file type without file config", |
| 44 | vs: &ValueSource{ |
| 45 | Type: "file", |
| 46 | File: nil, |
| 47 | }, |
| 48 | wantErr: true, |
| 49 | }, |
| 50 | { |
| 51 | name: "valid file type with absolute path", |
| 52 | vs: &ValueSource{ |
| 53 | Type: "file", |
| 54 | File: &FileSource{ |
| 55 | Path: "/tmp/test", |
| 56 | }, |
| 57 | }, |
| 58 | wantErr: false, |
| 59 | }, |
| 60 | { |
| 61 | name: "valid file type with relative path", |
| 62 | vs: &ValueSource{ |
| 63 | Type: "file", |
| 64 | File: &FileSource{ |
| 65 | Path: "configs/token", |
| 66 | }, |
| 67 | }, |
| 68 | wantErr: false, |
| 69 | }, |
| 70 | } |
| 71 |
| 72 | for _, tt := range tests { |
| 73 | t.Run(tt.name, func(t *testing.T) { |
| 74 | err := tt.vs.Validate() |
| 75 | if (err != nil) != tt.wantErr { |
| 76 | t.Errorf("ValueSource.Validate() error = %v, wantErr %v", err, tt.wantErr) |
| 77 | } |
| 78 | }) |
| 79 | } |
| 80 | } |
| 81 |
| 82 | func TestFileSource_Validate(t *testing.T) { |
| 83 | tests := []struct { |
| 84 | name string |
| 85 | fs *FileSource |
| 86 | wantErr bool |
| 87 | }{ |
| 88 | { |
| 89 | name: "nil fileSource", |
| 90 | fs: nil, |
| 91 | wantErr: true, |
| 92 | }, |
| 93 | { |
| 94 | name: "empty path", |
| 95 | fs: &FileSource{ |
| 96 | Path: "", |
| 97 | }, |
| 98 | wantErr: true, |
| 99 | }, |
| 100 | { |
| 101 | name: "relative path (allowed)", |
| 102 | fs: &FileSource{ |
| 103 | Path: "relative/path", |
| 104 | }, |
| 105 | wantErr: false, |
| 106 | }, |
| 107 | { |
| 108 | name: "absolute path", |
| 109 | fs: &FileSource{ |
| 110 | Path: "/absolute/path", |
| 111 | }, |
| 112 | wantErr: false, |
| 113 | }, |
| 114 | } |
| 115 |
| 116 | for _, tt := range tests { |
| 117 | t.Run(tt.name, func(t *testing.T) { |
| 118 | err := tt.fs.Validate() |
| 119 | if (err != nil) != tt.wantErr { |
| 120 | t.Errorf("FileSource.Validate() error = %v, wantErr %v", err, tt.wantErr) |
| 121 | } |
| 122 | }) |
| 123 | } |
| 124 | } |
| 125 |
| 126 | func TestFileSource_Resolve(t *testing.T) { |
| 127 | // Create a temporary file for testing |
| 128 | tmpDir := t.TempDir() |
| 129 | testFile := filepath.Join(tmpDir, "test_token") |
| 130 | testContent := "test-token-value\n\t " |
| 131 | expectedContent := "test-token-value" |
| 132 |
| 133 | err := os.WriteFile(testFile, []byte(testContent), 0o600) |
| 134 | if err != nil { |
| 135 | t.Fatalf("failed to create test file: %v", err) |
| 136 | } |
| 137 |
| 138 | tests := []struct { |
| 139 | name string |
| 140 | fs *FileSource |
| 141 | want string |
| 142 | wantErr bool |
| 143 | }{ |
| 144 | { |
| 145 | name: "valid file path", |
| 146 | fs: &FileSource{ |
| 147 | Path: testFile, |
| 148 | }, |
| 149 | want: expectedContent, |
| 150 | wantErr: false, |
| 151 | }, |
| 152 | { |
| 153 | name: "non-existent file", |
| 154 | fs: &FileSource{ |
| 155 | Path: "/non/existent/file", |
| 156 | }, |
| 157 | want: "", |
| 158 | wantErr: true, |
| 159 | }, |
| 160 | { |
| 161 | name: "path traversal attempt (should fail validation)", |
| 162 | fs: &FileSource{ |
| 163 | Path: "../../../etc/passwd", |
| 164 | }, |
| 165 | want: "", |
| 166 | wantErr: true, |
| 167 | }, |
| 168 | } |
| 169 |
| 170 | for _, tt := range tests { |
| 171 | t.Run(tt.name, func(t *testing.T) { |
| 172 | got, err := tt.fs.Resolve(context.Background()) |
| 173 | if (err != nil) != tt.wantErr { |
| 174 | t.Errorf("FileSource.Resolve() error = %v, wantErr %v", err, tt.wantErr) |
| 175 | return |
| 176 | } |
| 177 | if got != tt.want { |
| 178 | t.Errorf("FileSource.Resolve() = %v, want %v", got, tt.want) |
| 179 | } |
| 180 | }) |
| 181 | } |
| 182 | } |
| 183 |
| 184 | func TestValueSource_Resolve(t *testing.T) { |
| 185 | // Create a temporary file for testing |
| 186 | tmpDir := t.TempDir() |
| 187 | testFile := filepath.Join(tmpDir, "test_token") |
| 188 | testContent := "test-token-value" |
| 189 |
| 190 | err := os.WriteFile(testFile, []byte(testContent), 0o600) |
| 191 | if err != nil { |
| 192 | t.Fatalf("failed to create test file: %v", err) |
| 193 | } |
| 194 |
| 195 | tests := []struct { |
| 196 | name string |
| 197 | vs *ValueSource |
| 198 | want string |
| 199 | wantErr bool |
| 200 | }{ |
| 201 | { |
| 202 | name: "valid file type", |
| 203 | vs: &ValueSource{ |
| 204 | Type: "file", |
| 205 | File: &FileSource{ |
| 206 | Path: testFile, |
| 207 | }, |
| 208 | }, |
| 209 | want: testContent, |
| 210 | wantErr: false, |
| 211 | }, |
| 212 | { |
| 213 | name: "unsupported type", |
| 214 | vs: &ValueSource{ |
| 215 | Type: "unsupported", |
| 216 | }, |
| 217 | want: "", |
| 218 | wantErr: true, |
| 219 | }, |
| 220 | { |
| 221 | name: "file type with path traversal", |
| 222 | vs: &ValueSource{ |
| 223 | Type: "file", |
| 224 | File: &FileSource{ |
| 225 | Path: "../../../etc/passwd", |
| 226 | }, |
| 227 | }, |
| 228 | want: "", |
| 229 | wantErr: true, |
| 230 | }, |
| 231 | } |
| 232 |
| 233 | ctx := context.Background() |
| 234 | for _, tt := range tests { |
| 235 | t.Run(tt.name, func(t *testing.T) { |
| 236 | got, err := tt.vs.Resolve(ctx) |
| 237 | if (err != nil) != tt.wantErr { |
| 238 | t.Errorf("ValueSource.Resolve() error = %v, wantErr %v", err, tt.wantErr) |
| 239 | return |
| 240 | } |
| 241 | if got != tt.want { |
| 242 | t.Errorf("ValueSource.Resolve() = %v, want %v", got, tt.want) |
| 243 | } |
| 244 | }) |
| 245 | } |
| 246 | } |