1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package kds
import (
"encoding/hex"
"fmt"
"net/url"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-sev-guest/abi"
pb "github.com/google/go-sev-guest/proto/sevsnp"
"google.golang.org/protobuf/testing/protocmp"
"google.golang.org/protobuf/types/known/wrapperspb"
)
func TestProductCertChainURL(t *testing.T) {
got := ProductCertChainURL(abi.VcekReportSigner, "Milan")
want := "https://kdsintf.amd.com/vcek/v1/Milan/cert_chain"
if got != want {
t.Errorf("ProductCertChainURL(\"Milan\") = %q, want %q", got, want)
}
}
func TestVCEKCertURL(t *testing.T) {
hwid := make([]byte, abi.ChipIDSize)
hwid[0] = 0xfe
hwid[abi.ChipIDSize-1] = 0xc0
got := VCEKCertURL("Milan", hwid, TCBVersion(0))
want := "https://kdsintf.amd.com/vcek/v1/Milan/fe0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0?blSPL=0&teeSPL=0&snpSPL=0&ucodeSPL=0"
if got != want {
t.Errorf("VCEKCertURL(\"Milan\", %v, 0) = %q, want %q", hwid, got, want)
}
}
func TestParseProductBaseURL(t *testing.T) {
tcs := []struct {
name string
url string
wantProduct string
wantURL *url.URL
wantErr string
}{
{
name: "happy path",
url: ProductCertChainURL(abi.VcekReportSigner, "Milan"),
wantProduct: "Milan",
wantURL: &url.URL{
Scheme: "https",
Host: "kdsintf.amd.com",
Path: "cert_chain", // The vcek/v1/Milan part is expected to be trimmed.
},
},
{
name: "bad host",
url: "https://fakekds.com/vcek/v1/Milan/cert_chain",
wantErr: "unexpected AMD KDS URL host \"fakekds.com\", want \"kdsintf.amd.com\"",
},
{
name: "bad scheme",
url: "http://kdsintf.amd.com/vcek/v1/Milan/cert_chain",
wantErr: "unexpected AMD KDS URL scheme \"http\", want \"https\"",
},
{
name: "bad path",
url: "https://kdsintf.amd.com/vcek/v2/Milan/cert_chain",
wantErr: "unexpected AMD KDS URL path \"/vcek/v2/Milan/cert_chain\", want prefix \"/vcek/v1/\"",
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
parsed, err := parseBaseProductURL(tc.url)
if (err == nil && tc.wantErr != "") || (err != nil && !strings.Contains(err.Error(), tc.wantErr)) {
t.Fatalf("parseBaseProductURL(%q) = _, _, %v, want %q", tc.url, err, tc.wantErr)
}
if err == nil {
if diff := cmp.Diff(parsed.simpleURL, tc.wantURL); diff != "" {
t.Errorf("parseBaseProductURL(%q) returned unexpected diff (-want +got):\n%s", tc.url, diff)
}
if parsed.productLine != tc.wantProduct {
t.Errorf("parseBaseProductURL(%q) = %q, _, _ want %q", tc.url, parsed.productLine, tc.wantProduct)
}
}
})
}
}
func TestParseProductCertChainURL(t *testing.T) {
tests := []struct {
key abi.ReportSigner
product string
wantKey CertFunction
}{
{
key: abi.VcekReportSigner,
product: "Milan",
wantKey: VcekCertFunction,
},
{
key: abi.VlekReportSigner,
product: "Milan",
wantKey: VlekCertFunction,
},
}
for _, tc := range tests {
url := ProductCertChainURL(tc.key, tc.product)
got, key, err := ParseProductCertChainURL(url)
if err != nil {
t.Fatalf("ParseProductCertChainURL(%q) = _, _, %v, want nil", tc.product, err)
}
if got != tc.product || key != tc.wantKey {
t.Errorf("ProductCertChainURL(%q) = %q, %v, nil want %q, %v", url, got, key, tc.product, tc.wantKey)
}
}
}
func TestParseVCEKCertURL(t *testing.T) {
hwid := make([]byte, abi.ChipIDSize)
hwidhex := hex.EncodeToString(hwid)
tcs := []struct {
name string
url string
want VCEKCert
wantErr string
}{
{
name: "happy path",
url: VCEKCertURL("Milan", hwid, TCBVersion(0)),
want: func() VCEKCert {
c := VCEKCertProduct("Milan")
c.HWID = hwid
c.TCB = 0
return c
}(),
},
{
name: "bad query format",
url: fmt.Sprintf("https://kdsintf.amd.com/vcek/v1/Milan/%s?ha;ha", hwidhex),
wantErr: "invalid AMD KDS URL query \"ha;ha\": invalid semicolon separator in query",
},
{
name: "bad query key",
url: fmt.Sprintf("https://kdsintf.amd.com/vcek/v1/Milan/%s?fakespl=4", hwidhex),
wantErr: "unexpected KDS TCB version URL argument \"fakespl\"",
},
{
name: "bad query argument numerical",
url: fmt.Sprintf("https://kdsintf.amd.com/vcek/v1/Milan/%s?blSPL=-4", hwidhex),
wantErr: "invalid KDS TCB version URL argument value \"-4\", want a value 0-255",
},
{
name: "bad query argument numerical",
url: fmt.Sprintf("https://kdsintf.amd.com/vcek/v1/Milan/%s?blSPL=alpha", hwidhex),
wantErr: "invalid KDS TCB version URL argument value \"alpha\", want a value 0-255",
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
got, err := ParseVCEKCertURL(tc.url)
if (err == nil && tc.wantErr != "") || (err != nil && !strings.Contains(err.Error(), tc.wantErr)) {
t.Fatalf("ParseVCEKCertURL(%q) = _, %v, want %q", tc.url, err, tc.wantErr)
}
if err == nil {
if diff := cmp.Diff(got, tc.want); diff != "" {
t.Errorf("ParseVCEKCertURL(%q) returned unexpected diff (-want +got):\n%s", tc.url, diff)
}
}
})
}
}
func TestProductName(t *testing.T) {
tcs := []struct {
name string
input *pb.SevProduct
want string
}{
{
name: "nil",
want: "Milan-B1",
},
{
name: "unknown",
input: &pb.SevProduct{
MachineStepping: &wrapperspb.UInt32Value{Value: 0x1A},
},
want: "badstepping",
},
{
name: "Milan-B0",
input: &pb.SevProduct{
Name: pb.SevProduct_SEV_PRODUCT_MILAN,
},
want: "UnknownStepping",
},
{
name: "Milan-B0",
input: &pb.SevProduct{
Name: pb.SevProduct_SEV_PRODUCT_MILAN,
MachineStepping: &wrapperspb.UInt32Value{Value: 0},
},
want: "Milan-B0",
},
{
name: "Genoa-FF",
input: &pb.SevProduct{
Name: pb.SevProduct_SEV_PRODUCT_GENOA,
MachineStepping: &wrapperspb.UInt32Value{Value: 0xff},
},
want: "badstepping",
},
{
name: "unknown milan stepping",
input: &pb.SevProduct{
Name: pb.SevProduct_SEV_PRODUCT_MILAN,
MachineStepping: &wrapperspb.UInt32Value{Value: 15},
},
want: "unmappedMilanStepping",
},
{
name: "unknown genoa stepping",
input: &pb.SevProduct{
Name: pb.SevProduct_SEV_PRODUCT_GENOA,
MachineStepping: &wrapperspb.UInt32Value{Value: 15},
},
want: "unmappedGenoaStepping",
},
{
name: "unknown",
input: &pb.SevProduct{
Name: pb.SevProduct_SEV_PRODUCT_UNKNOWN,
MachineStepping: &wrapperspb.UInt32Value{Value: 15},
},
want: "Unknown",
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
if got := ProductName(tc.input); got != tc.want {
t.Errorf("ProductName(%v) = %q, want %q", tc.input, got, tc.want)
}
})
}
}
func TestParseProductName(t *testing.T) {
tcs := []struct {
name string
input string
key abi.ReportSigner
want *pb.SevProduct
wantErr string
}{
{
name: "empty",
wantErr: "unknown product name",
},
{
name: "Too big",
input: "Milan-100",
wantErr: "unknown product name",
},
{
name: "happy path Genoa",
input: "Genoa-B1",
want: &pb.SevProduct{
Name: pb.SevProduct_SEV_PRODUCT_GENOA,
MachineStepping: &wrapperspb.UInt32Value{Value: 1},
},
},
{
name: "bad revision Milan",
input: "Milan-A1",
wantErr: "unknown product name",
},
{
name: "vlek products have no stepping",
input: "Genoa",
key: abi.VlekReportSigner,
want: &pb.SevProduct{
Name: pb.SevProduct_SEV_PRODUCT_GENOA,
},
},
{
name: "Unhandled report signer",
input: "ignored",
key: abi.NoneReportSigner,
wantErr: "internal: unhandled reportSigner",
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
got, err := ParseProductName(tc.input, tc.key)
if (err == nil && tc.wantErr != "") || (err != nil && (tc.wantErr == "" || !strings.Contains(err.Error(), tc.wantErr))) {
t.Fatalf("ParseProductName(%v) errored unexpectedly: %v, want %q", tc.input, err, tc.wantErr)
}
if tc.wantErr == "" {
if diff := cmp.Diff(got, tc.want, protocmp.Transform()); diff != "" {
t.Fatalf("ParseProductName(%v) = %v, want %v\nDiff: %s", tc.input, got, tc.want, diff)
}
}
})
}
}
|