Debian Codegraph @

crypto/x509util/crt_test.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 package x509util
2
3 import (
4 "crypto/x509"
5 "encoding/pem"
6 "io/ioutil"
7 "net"
8 "net/url"
9 "testing"
10
11 "github.com/smallstep/assert"
12 )
13
14 func TestFingerprint(t *testing.T) {
15 tests := []struct {
16 name string
17 fn string
18 want string
19 }{
•20 {"ok", "testdata/ca.crt", "6908751f68290d4573ae0be39a98c8b9b7b7d4e8b2a6694b7509946626adfe98"},
21 }
22 for _, tt := range tests {
23 t.Run(tt.name, func(t *testing.T) {
24 cert := mustParseCertificate(t, tt.fn)
25 if got := Fingerprint(cert); got != tt.want {
26 t.Errorf("Fingerprint() = %v, want %v", got, tt.want)
27 }
28 })
29 }
30 }
31
32 func TestEncodedFingerprint(t *testing.T) {
33 tests := []struct {
34 name string
35 fn string
36 encoding FingerprintEncoding
37 want string
38 }{
•39 {"hex", "testdata/ca.crt", HexFingerprint, "6908751f68290d4573ae0be39a98c8b9b7b7d4e8b2a6694b7509946626adfe98"},
40 {"base64", "testdata/ca.crt", Base64Fingerprint, "aQh1H2gpDUVzrgvjmpjIube31OiypmlLdQmUZiat/pg="},
41 {"base64url", "testdata/ca.crt", Base64UrlFingerprint, "aQh1H2gpDUVzrgvjmpjIube31OiypmlLdQmUZiat_pg="},
42 }
43 for _, tt := range tests {
44 t.Run(tt.name, func(t *testing.T) {
45 cert := mustParseCertificate(t, tt.fn)
46 if got := EncodedFingerprint(cert, tt.encoding); got != tt.want {
47 t.Errorf("EncodedFingerprint() = %v, want %v", got, tt.want)
48 }
49 })
50 }
51 }
52
53 func mustParseCertificate(t *testing.T, filename string) *x509.Certificate {
54 pemData, err := ioutil.ReadFile(filename)
55 if err != nil {
56 t.Fatalf("failed to read %s: %v", filename, err)
57 }
58 block, rest := pem.Decode([]byte(pemData))
59 if block == nil || len(rest) > 0 {
60 t.Fatalf("failed to decode PEM in %s", filename)
61 }
62 cert, err := x509.ParseCertificate(block.Bytes)
63 if err != nil {
64 t.Fatalf("failed to parse certificate in %s: %v", filename, err)
65 }
66 return cert
67 }
68
69 func TestSplitSANs(t *testing.T) {
70 u1, err := url.Parse("https://ca.smallstep.com")
71 assert.FatalError(t, err)
72 u2, err := url.Parse("https://google.com/index.html")
73 assert.FatalError(t, err)
74 u3, err := url.Parse("urn:uuid:ddfe62ba-7e99-4bc1-83b3-8f57fe3e9959")
75 assert.FatalError(t, err)
76 u4, err := url.Parse("mailto:[email protected]")
77 assert.FatalError(t, err)
78 tests := []struct {
79 name string
80 sans, dns, emails []string
81 ips []net.IP
82 uris []*url.URL
83 }{
84 {name: "empty", sans: []string{}, dns: []string{}, ips: []net.IP{}, emails: []string{}, uris: []*url.URL{}},
85 {
86 name: "all-dns",
87 sans: []string{"foo.internal", "bar.internal"},
88 dns: []string{"foo.internal", "bar.internal"},
89 ips: []net.IP{},
90 emails: []string{},
91 uris: []*url.URL{},
92 },
93 {
94 name: "all-ip",
95 sans: []string{"0.0.0.0", "127.0.0.1", "::1", "2001:0db8:0000:0000:0000:8a2e:0370:7334", "2001:db8::8a2e:370:7334"},
96 dns: []string{},
97 ips: []net.IP{net.ParseIP("0.0.0.0"), net.ParseIP("127.0.0.1"), net.ParseIP("::1"), net.ParseIP("2001:0db8:0000:0000:0000:8a2e:0370:7334"), net.ParseIP("2001:db8::8a2e:370:7334")},
98 emails: []string{},
99 uris: []*url.URL{},
100 },
101 {
102 name: "all-email",
103 sans: []string{"[email protected]", "[email protected]"},
104 dns: []string{},
105 ips: []net.IP{},
106 emails: []string{"[email protected]", "[email protected]"},
107 uris: []*url.URL{},
108 },
109 {
110 name: "all-uri",
111 sans: []string{"https://ca.smallstep.com", "https://google.com/index.html", "urn:uuid:ddfe62ba-7e99-4bc1-83b3-8f57fe3e9959", "mailto:[email protected]"},
112 dns: []string{},
113 ips: []net.IP{},
114 emails: []string{},
115 uris: []*url.URL{u1, u2, u3, u4},
116 },
117 {
118 name: "mix",
119 sans: []string{"foo.internal", "https://ca.smallstep.com", "[email protected]", "urn:uuid:ddfe62ba-7e99-4bc1-83b3-8f57fe3e9959", "[email protected]", "1.1.1.1", "bar.internal", "https://google.com/index.html", "mailto:[email protected]", "2102:446:c001:d65e:ab1a:bf20:4b26:31f7"},
120 dns: []string{"foo.internal", "bar.internal"},
121 ips: []net.IP{net.ParseIP("1.1.1.1"), net.ParseIP("2102:446:c001:d65e:ab1a:bf20:4b26:31f7")},
122 emails: []string{"[email protected]", "[email protected]"},
123 uris: []*url.URL{u1, u3, u2, u4},
124 },
125 }
126 for _, tt := range tests {
127 t.Run(tt.name, func(t *testing.T) {
128 dns, ips, emails, uris := SplitSANs(tt.sans)
129 assert.Equals(t, dns, tt.dns)
130 assert.Equals(t, ips, tt.ips)
131 assert.Equals(t, emails, tt.emails)
132 assert.Equals(t, uris, tt.uris)
133 })
134 }
135 }