| 1 |
// Copyright 2024 Google LLC |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 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 cert |
| 16 |
|
| 17 |
import ( |
| 18 |
"errors" |
| 19 |
"testing" |
| 20 |
) |
| 21 |
|
| 22 |
func TestWorkloadCertSource_ConfigMissing(t *testing.T) { |
| 23 |
source, err := NewWorkloadX509CertProvider("missing.json") |
| 24 |
if got, want := err, errSourceUnavailable; !errors.Is(err, errSourceUnavailable) { |
| 25 |
t.Fatalf("got %v, want %v", got, want) |
| 26 |
} |
| 27 |
if source != nil { |
| 28 |
t.Errorf("got %v, want nil source", source) |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
func TestWorkloadCertSource_EmptyConfig(t *testing.T) { |
| 33 |
source, err := NewWorkloadX509CertProvider("testdata/certificate_config_workload_empty.json") |
| 34 |
if err == nil { |
| 35 |
t.Fatal("got nil, want non-nil error") |
| 36 |
} |
| 37 |
if !errors.Is(err, errSourceUnavailable) { |
| 38 |
t.Errorf("got %v, want errSourceUnavailable", err) |
| 39 |
} |
| 40 |
if source != nil { |
| 41 |
t.Errorf("got %v, want nil source", source) |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
func TestWorkloadCertSource_MissingCert(t *testing.T) { |
| 46 |
source, err := NewWorkloadX509CertProvider("testdata/certificate_config_workload_no_cert.json") |
| 47 |
if err == nil { |
| 48 |
t.Fatal("got nil, want non-nil error") |
| 49 |
} |
| 50 |
if source != nil { |
| 51 |
t.Errorf("got %v, want nil source", source) |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
func TestWorkloadCertSource_MissingKey(t *testing.T) { |
| 56 |
source, err := NewWorkloadX509CertProvider("testdata/certificate_config_workload_no_key.json") |
| 57 |
if err == nil { |
| 58 |
t.Fatal("got nil, want non-nil error") |
| 59 |
} |
| 60 |
if source != nil { |
| 61 |
t.Errorf("got %v, want nil source", source) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
func TestWorkloadCertSource_GetClientCertificateInvalidCert(t *testing.T) { |
| 66 |
source, err := NewWorkloadX509CertProvider("testdata/certificate_config_workload_invalid_cert.json") |
| 67 |
if err != nil { |
| 68 |
t.Fatal(err) |
| 69 |
} |
| 70 |
_, err = source(nil) |
| 71 |
if err == nil { |
| 72 |
t.Fatal("got nil, want non-nil error") |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
func TestWorkloadCertSource_GetClientCertificateSuccess(t *testing.T) { |
| 77 |
source, err := NewWorkloadX509CertProvider("testdata/certificate_config_workload.json") |
| 78 |
if err != nil { |
| 79 |
t.Fatal(err) |
| 80 |
} |
| 81 |
cert, err := source(nil) |
| 82 |
if err != nil { |
| 83 |
t.Fatal(err) |
| 84 |
} |
| 85 |
if cert.Certificate == nil { |
| 86 |
t.Fatal("got nil, want non-nil Certificate") |
| 87 |
} |
| 88 |
if cert.PrivateKey == nil { |
| 89 |
t.Fatal("got nil, want non-nil PrivateKey") |
| 90 |
} |
| 91 |
} |