Debian Codegraph @

attributecert/attributecert_test.go

raw
1 // Copyright 2019 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // 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, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 package attributecert
16
17 import (
18 "crypto/x509"
19 "encoding/json"
20 "os"
21 "reflect"
22 "strings"
23 "testing"
24 )
25
26 func TestVerifyAttributeCert(t *testing.T) {
27 testfiles := [...]string{"testdata/Intel_nuc_pc2.cer",
28 "testdata/Intel_nuc_pc.cer",
29 "testdata/Intel_pc2.cer",
30 "testdata/Intel_pc3.cer",
31 }
32 data, err := os.ReadFile("testdata/IntelSigningKey_20April2017.cer")
33 if err != nil {
34 t.Fatalf("failed to read Intel intermediate certificate: %v", err)
35 }
36 cert, err := x509.ParseCertificate(data)
37 if err != nil {
38 t.Fatalf("failed to parse Intel intermediate certificate: %v", err)
39 }
40
41 for _, filename := range testfiles {
42 t.Run(filename, func(t *testing.T) {
43 data, err = os.ReadFile(filename)
44 if err != nil {
45 t.Fatalf("failed to read %s: %v", filename, err)
46 }
47
48 attributecert, err := ParseAttributeCertificate(data)
49 if err != nil {
50 t.Fatalf("failed to parse %s: %v", filename, err)
51 }
52
53 err = attributecert.CheckSignatureFrom(cert)
54 if err != nil {
55 t.Fatalf("failed to verify signature on %s: %v", filename, err)
56 }
57 })
58 }
59 }
60
61 func TestParseAttributeCerts(t *testing.T) {
62 files, err := os.ReadDir("testdata")
63 if err != nil {
64 t.Fatalf("failed to read test dir: %v", err)
65 }
66 for _, file := range files {
67 filename := file.Name()
68 if strings.Contains(filename, "Signing") {
69 continue
70 }
71 if strings.HasSuffix(filename, ".json") {
72 continue
73 }
74 t.Run(filename, func(t *testing.T) {
75 filename := "testdata/" + filename
76 jsonfile := filename + ".json"
77 data, err := os.ReadFile(filename)
78 if err != nil {
79 t.Fatalf("failed to read test data %s: %v", filename, err)
80 }
81 got, err := ParseAttributeCertificate(data)
82 if err != nil {
83 t.Fatalf("failed to parse test data %s: %v", filename, err)
84 }
85 jsondata, err := os.ReadFile(jsonfile)
86 if err != nil {
87 t.Fatalf("failed to read json test data %s: %v", jsonfile, err)
88 }
89 var want AttributeCertificate
90 if err := json.Unmarshal(jsondata, &want); err != nil {
91 t.Fatalf("failed to unmarshal file %s: %v", filename, err)
92 }
93 if !reflect.DeepEqual(&want, got) {
94 t.Fatalf("%s fails to match test data", filename)
95 }
96 })
97 }
98 }