|
| 1 | +/* |
| 2 | + * Copyright 2024 The CNAI Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package config |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/base64" |
| 21 | + "encoding/json" |
| 22 | + "os" |
| 23 | + "testing" |
| 24 | +) |
| 25 | + |
| 26 | +func TestExtractCred(t *testing.T) { |
| 27 | + b64 := base64.StdEncoding.EncodeToString([]byte("user2:pass2")) |
| 28 | + |
| 29 | + cases := []struct { |
| 30 | + name string |
| 31 | + cfg AuthConfig |
| 32 | + registry string |
| 33 | + wantUser string |
| 34 | + wantPass string |
| 35 | + wantErr bool |
| 36 | + }{ |
| 37 | + { |
| 38 | + name: "username password fields", |
| 39 | + cfg: AuthConfig{Auths: map[string]AuthConfigEntry{"example.io": {Username: "u", Password: "p"}}}, |
| 40 | + registry: "example.io", |
| 41 | + wantUser: "u", |
| 42 | + wantPass: "p", |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "auth base64 field", |
| 46 | + cfg: AuthConfig{Auths: map[string]AuthConfigEntry{"registry.local": {Auth: b64}}}, |
| 47 | + registry: "registry.local", |
| 48 | + wantUser: "user2", |
| 49 | + wantPass: "pass2", |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "registry missing", |
| 53 | + cfg: AuthConfig{Auths: map[string]AuthConfigEntry{}}, |
| 54 | + registry: "miss.io", |
| 55 | + wantErr: true, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "malformed auth", |
| 59 | + cfg: AuthConfig{Auths: map[string]AuthConfigEntry{"bad": {Auth: base64.StdEncoding.EncodeToString([]byte("onlyuser"))}}}, |
| 60 | + registry: "bad", |
| 61 | + wantErr: true, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "empty entry", |
| 65 | + cfg: AuthConfig{Auths: map[string]AuthConfigEntry{"empty": {}}}, |
| 66 | + registry: "empty", |
| 67 | + wantErr: true, |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + for _, tc := range cases { |
| 72 | + t.Run(tc.name, func(t *testing.T) { |
| 73 | + user, pass, err := ExtractCred(tc.cfg, tc.registry) |
| 74 | + if (err != nil) != tc.wantErr { |
| 75 | + t.Fatalf("expected err=%v got %v", tc.wantErr, err) |
| 76 | + } |
| 77 | + if !tc.wantErr { |
| 78 | + if user != tc.wantUser || pass != tc.wantPass { |
| 79 | + t.Fatalf("want (%s,%s) got (%s,%s)", tc.wantUser, tc.wantPass, user, pass) |
| 80 | + } |
| 81 | + } |
| 82 | + }) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// minimal I/O test to ensure ParseAuthFile ties together read+unmarshal+ExtractCred |
| 87 | +func TestParseAuthFile(t *testing.T) { |
| 88 | + cfg := AuthConfig{Auths: map[string]AuthConfigEntry{"io": {Username: "a", Password: "b"}}} |
| 89 | + |
| 90 | + tmp, err := os.CreateTemp(t.TempDir(), "authfile-*.json") |
| 91 | + if err != nil { |
| 92 | + t.Fatalf("temp: %v", err) |
| 93 | + } |
| 94 | + defer tmp.Close() |
| 95 | + |
| 96 | + enc := json.NewEncoder(tmp) |
| 97 | + if err := enc.Encode(cfg); err != nil { |
| 98 | + t.Fatalf("encode: %v", err) |
| 99 | + } |
| 100 | + |
| 101 | + path := tmp.Name() |
| 102 | + |
| 103 | + user, pass, err := ParseAuthFile(path, "io") |
| 104 | + if err != nil { |
| 105 | + t.Fatalf("unexpected err: %v", err) |
| 106 | + } |
| 107 | + if user != "a" || pass != "b" { |
| 108 | + t.Fatalf("want (a,b) got (%s,%s)", user, pass) |
| 109 | + } |
| 110 | + |
| 111 | + // ensure file read error propagates |
| 112 | + if _, _, err := ParseAuthFile("/non/exist", "io"); err == nil { |
| 113 | + t.Fatalf("expected error for missing file") |
| 114 | + } |
| 115 | +} |
0 commit comments