-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcert_test.go
83 lines (66 loc) · 1.49 KB
/
cert_test.go
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
package main
import (
"crypto/x509"
"encoding/pem"
"flag"
"io/ioutil"
"path/filepath"
"testing"
"time"
"github.com/andreyvit/diff"
)
func TestMatchRoots(t *testing.T) {
roots := x509.NewCertPool()
rootsPEM, err := ioutil.ReadFile("testdata/ca-bundle.crt")
if err != nil {
t.Fatal(err)
}
if !roots.AppendCertsFromPEM(rootsPEM) {
t.Fatal(err)
}
certPEM, err := ioutil.ReadFile("testdata/addtrust.crt")
if err != nil {
t.Fatal(err)
}
certData, _ := pem.Decode(certPEM)
cert, err := x509.ParseCertificate(certData.Bytes)
if err != nil {
t.Fatal(err)
}
if !matchRoots(cert, roots) {
t.Errorf("cert is not matched as root")
}
}
var update = flag.Bool("update", false, "update golden files")
func TestCertString(t *testing.T) {
testFiles := []string{
"addtrust.crt",
"letsencrypt-fullchain.pem",
}
// Set timezone to fixate serialization of datetime
loc, err := time.LoadLocation("UTC")
if err != nil {
t.Fatal(err)
}
time.Local = loc
for _, f := range testFiles {
filename := filepath.Join("testdata", f)
golden := filepath.Join("testdata", f+".golden")
certs, err := load(filename)
if err != nil {
t.Fatal(err)
}
got := certs.String()
if *update {
ioutil.WriteFile(golden, []byte(got), 0644)
}
wantBytes, err := ioutil.ReadFile(golden)
if err != nil {
t.Fatal(err)
}
want := string(wantBytes)
if got != want {
t.Errorf("%s serialization doesn't match golden file %s:\n%v", filename, golden, diff.LineDiff(want, got))
}
}
}