|
| 1 | +// Copyright 2026 Contributors to the Veraison project. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package verification |
| 5 | + |
| 6 | +import ( |
| 7 | + "net/http" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + "github.com/veraison/apiclient/common" |
| 13 | +) |
| 14 | + |
| 15 | +var ( |
| 16 | + testDiscoveryObject = &DiscoveryObject{ |
| 17 | + PublicKey: []byte(`{ "alg": "ES256", "crv": "P-256", "kty": "EC", "x": "usWxHK2PmfnHKwXPS54m0kTcGJ90UiglWiGahtagnv8", "y": "IBOL-C3BttVivg-lSreASjpkttcsz-1rb7btKLv8EX4" }`), |
| 18 | + MediaTypes: []string{ |
| 19 | + "application/vnd.enacttrust.tpm-evidence", |
| 20 | + "application/vnd.parallaxsecond.key-attestation.tpm", |
| 21 | + "application/eat+cwt; eat_profile=\"tag:psacertified.org,2023:psa#tfm\"", |
| 22 | + "application/vnd.veraison.tsm-report+cbor", |
| 23 | + "application/vnd.veraison.configfs-tsm+json", |
| 24 | + "application/vnd.parallaxsecond.key-attestation.cca", |
| 25 | + "application/psa-attestation-token", |
| 26 | + "application/eat-cwt; profile=\"http://arm.com/psa/2.0.0\"", |
| 27 | + "application/eat+cwt; eat_profile=\"tag:psacertified.org,2019:psa#legacy\"", |
| 28 | + "application/pem-certificate-chain", |
| 29 | + "application/eat-collection; profile=\"http://arm.com/CCA-SSD/1.0.0\"", |
| 30 | + "application/eat+cwt; eat_profile=\"tag:github.com,2025:veraison/ratsd/cmw\"", |
| 31 | + }, |
| 32 | + Version: "0.0.2511+f1ccf18", |
| 33 | + ServiceState: "READY", |
| 34 | + ApiEndpoints: map[string]string{ |
| 35 | + "newChallengeResponseSession": "/challenge-response/v1/newSession", |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + testDiscoveryObjectJSON = `{ |
| 40 | + "ear-verification-key": { "alg": "ES256", "crv": "P-256", "kty": "EC", "x": "usWxHK2PmfnHKwXPS54m0kTcGJ90UiglWiGahtagnv8", "y": "IBOL-C3BttVivg-lSreASjpkttcsz-1rb7btKLv8EX4" }, |
| 41 | + "media-types": [ |
| 42 | + "application/vnd.enacttrust.tpm-evidence", |
| 43 | + "application/vnd.parallaxsecond.key-attestation.tpm", |
| 44 | + "application/eat+cwt; eat_profile=\"tag:psacertified.org,2023:psa#tfm\"", |
| 45 | + "application/vnd.veraison.tsm-report+cbor", |
| 46 | + "application/vnd.veraison.configfs-tsm+json", |
| 47 | + "application/vnd.parallaxsecond.key-attestation.cca", |
| 48 | + "application/psa-attestation-token", |
| 49 | + "application/eat-cwt; profile=\"http://arm.com/psa/2.0.0\"", |
| 50 | + "application/eat+cwt; eat_profile=\"tag:psacertified.org,2019:psa#legacy\"", |
| 51 | + "application/pem-certificate-chain", |
| 52 | + "application/eat-collection; profile=\"http://arm.com/CCA-SSD/1.0.0\"", |
| 53 | + "application/eat+cwt; eat_profile=\"tag:github.com,2025:veraison/ratsd/cmw\"" |
| 54 | + ], |
| 55 | + "version": "0.0.2511+f1ccf18", |
| 56 | + "service-state": "READY", |
| 57 | + "api-endpoints": { |
| 58 | + "newChallengeResponseSession": "/challenge-response/v1/newSession" |
| 59 | + } |
| 60 | +}` |
| 61 | + testDiscoveryURI = "http://discovery.example.com/.well-known/verification" |
| 62 | + testDiscoveryURIHTTPS = "https://discovery.example.com/.well-known/verification" |
| 63 | +) |
| 64 | + |
| 65 | +func TestDiscoveryConfig_Run_ok(t *testing.T) { |
| 66 | + var err error |
| 67 | + |
| 68 | + expectedBody := testDiscoveryObject |
| 69 | + |
| 70 | + h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 71 | + w.Header().Set("Content-Type", discoveryMediaType) |
| 72 | + w.WriteHeader(http.StatusOK) |
| 73 | + w.Write([]byte(testDiscoveryObjectJSON)) |
| 74 | + }) |
| 75 | + |
| 76 | + client, teardown := common.NewTestingHTTPClient(h) |
| 77 | + defer teardown() |
| 78 | + |
| 79 | + var cfg DiscoveryConfig |
| 80 | + |
| 81 | + err = cfg.SetDiscoveryURI(testDiscoveryURI) |
| 82 | + require.NoError(t, err) |
| 83 | + |
| 84 | + err = cfg.SetClient(client) |
| 85 | + require.NoError(t, err) |
| 86 | + |
| 87 | + actualBody, err := cfg.Run() |
| 88 | + assert.NoError(t, err) |
| 89 | + assert.Equal(t, expectedBody, actualBody) |
| 90 | +} |
| 91 | + |
| 92 | +func TestDiscoveryConfig_Run_fail_bad_object(t *testing.T) { |
| 93 | + var err error |
| 94 | + |
| 95 | + badObject := []byte(`[ "deadbeef" ]`) |
| 96 | + |
| 97 | + h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 98 | + w.Header().Set("Content-Type", discoveryMediaType) |
| 99 | + w.WriteHeader(http.StatusOK) |
| 100 | + w.Write(badObject) |
| 101 | + }) |
| 102 | + |
| 103 | + client, teardown := common.NewTestingHTTPClient(h) |
| 104 | + defer teardown() |
| 105 | + |
| 106 | + var cfg DiscoveryConfig |
| 107 | + |
| 108 | + err = cfg.SetDiscoveryURI(testDiscoveryURI) |
| 109 | + require.NoError(t, err) |
| 110 | + |
| 111 | + err = cfg.SetClient(client) |
| 112 | + require.NoError(t, err) |
| 113 | + |
| 114 | + _, err = cfg.Run() |
| 115 | + assert.ErrorContains(t, err, "failure JSON decoding response body") |
| 116 | +} |
| 117 | + |
| 118 | +func TestDiscoveryConfig_Run_fail_configuration(t *testing.T) { |
| 119 | + var err error |
| 120 | + var cfg DiscoveryConfig |
| 121 | + |
| 122 | + _, err = cfg.Run() |
| 123 | + assert.ErrorContains(t, err, "bad configuration: no discovery URI") |
| 124 | +} |
| 125 | + |
| 126 | +func TestDiscoveryConfig_Run_fail_init_tls_with_nonexistent_certs(t *testing.T) { |
| 127 | + var err error |
| 128 | + var cfg DiscoveryConfig |
| 129 | + |
| 130 | + err = cfg.SetDiscoveryURI(testDiscoveryURIHTTPS) |
| 131 | + require.NoError(t, err) |
| 132 | + |
| 133 | + err = cfg.SetCerts([]string{"/path/to/nonexistent/cert.pem"}) |
| 134 | + require.NoError(t, err) |
| 135 | + |
| 136 | + _, err = cfg.Run() |
| 137 | + assert.ErrorContains(t, err, "could not read cert") |
| 138 | +} |
| 139 | + |
| 140 | +func TestDiscoveryConfig_Setters_fail_misc(t *testing.T) { |
| 141 | + var err error |
| 142 | + var cfg DiscoveryConfig |
| 143 | + |
| 144 | + err = cfg.SetDiscoveryURI("http://[::1]:namedport") |
| 145 | + assert.ErrorContains(t, err, "malformed discovery URI") |
| 146 | + |
| 147 | + err = cfg.SetDiscoveryURI("relative/path") |
| 148 | + assert.ErrorContains(t, err, "the supplied discovery URI is not absolute") |
| 149 | + |
| 150 | + err = cfg.SetCerts(nil) |
| 151 | + assert.ErrorContains(t, err, "no CA certificate paths supplied") |
| 152 | + |
| 153 | + err = cfg.SetClient(nil) |
| 154 | + assert.ErrorContains(t, err, "no client supplied") |
| 155 | +} |
| 156 | + |
| 157 | +func TestDiscoveryConfig_Setters_ok_misc(t *testing.T) { |
| 158 | + var err error |
| 159 | + var cfg DiscoveryConfig |
| 160 | + |
| 161 | + err = cfg.SetDiscoveryURI(testDiscoveryURIHTTPS) |
| 162 | + assert.NoError(t, err) |
| 163 | + |
| 164 | + err = cfg.SetCerts([]string{"/path/to/cert1.pem", "/path/to/cert2.pem"}) |
| 165 | + assert.NoError(t, err) |
| 166 | + |
| 167 | + err = cfg.SetClient(common.NewClient(nil)) |
| 168 | + assert.NoError(t, err) |
| 169 | +} |
0 commit comments