@@ -10,67 +10,126 @@ import (
10
10
"github.com/letsencrypt/boulder/test"
11
11
)
12
12
13
- func TestNormalize (t * testing.T ) {
14
- idents := []ACMEIdentifier {
15
- {Type : "DNS" , Value : "foobar.com" },
16
- {Type : "DNS" , Value : "fooBAR.com" },
17
- {Type : "DNS" , Value : "baz.com" },
18
- {Type : "DNS" , Value : "foobar.com" },
19
- {Type : "DNS" , Value : "bar.com" },
20
- {Type : "DNS" , Value : "bar.com" },
21
- {Type : "DNS" , Value : "a.com" },
22
- }
23
- expected := []ACMEIdentifier {
24
- {Type : "DNS" , Value : "a.com" },
25
- {Type : "DNS" , Value : "bar.com" },
26
- {Type : "DNS" , Value : "baz.com" },
27
- {Type : "DNS" , Value : "foobar.com" },
28
- }
29
- u := Normalize (idents )
30
- test .AssertDeepEquals (t , expected , u )
31
- }
32
-
33
- // TestFromCSR covers TestFromCert as well, because their logic is exactly the same.
34
- func TestFromCSR (t * testing.T ) {
13
+ // TestFromCertAndCSR covers both FromCert and FromCSR; their logic is exactly the same.
14
+ func TestFromCertAndCSR (t * testing.T ) {
35
15
cases := []struct {
36
16
name string
37
- csr * x509.CertificateRequest
17
+ subject pkix.Name
18
+ dnsNames []string
19
+ ipAddresses []net.IP
38
20
expectedIdents []ACMEIdentifier
39
21
}{
40
22
{
41
- "no explicit CN" ,
42
- & x509.CertificateRequest {DNSNames : []string {"a.com" }},
43
- []ACMEIdentifier {NewDNS ("a.com" )},
23
+ name : "no explicit CN" ,
24
+ dnsNames : []string {"a.com" },
25
+ expectedIdents : []ACMEIdentifier {NewDNS ("a.com" )},
26
+ },
27
+ {
28
+ name : "explicit uppercase CN" ,
29
+ subject : pkix.Name {CommonName : "A.com" },
30
+ dnsNames : []string {"a.com" },
31
+ expectedIdents : []ACMEIdentifier {NewDNS ("a.com" )},
32
+ },
33
+ {
34
+ name : "no explicit CN, uppercase SAN" ,
35
+ dnsNames : []string {"A.com" },
36
+ expectedIdents : []ACMEIdentifier {NewDNS ("a.com" )},
44
37
},
45
38
{
46
- "explicit uppercase CN " ,
47
- & x509. CertificateRequest { Subject : pkix. Name { CommonName : "A .com"}, DNSNames : [] string { "a.com" } },
48
- []ACMEIdentifier {NewDNS ("a.com" )},
39
+ name : "duplicate SANs " ,
40
+ dnsNames : [] string { "b.com" , "b .com", "a.com" , "a.com" },
41
+ expectedIdents : []ACMEIdentifier {NewDNS ("a.com" ), NewDNS ( "b .com" )},
49
42
},
50
43
{
51
- "no explicit CN, uppercase SAN" ,
52
- & x509.CertificateRequest {DNSNames : []string {"A.com" }},
53
- []ACMEIdentifier {NewDNS ("a.com" )},
44
+ name : "explicit CN not found in SANs" ,
45
+ subject : pkix.Name {CommonName : "a.com" },
46
+ dnsNames : []string {"b.com" },
47
+ expectedIdents : []ACMEIdentifier {NewDNS ("a.com" ), NewDNS ("b.com" )},
48
+ },
49
+ {
50
+ name : "mix of DNSNames and IPAddresses" ,
51
+ dnsNames : []string {"a.com" },
52
+ ipAddresses : []net.IP {{192 , 168 , 1 , 1 }},
53
+ expectedIdents : []ACMEIdentifier {NewDNS ("a.com" ), NewIP (netip .MustParseAddr ("192.168.1.1" ))},
54
+ },
55
+ }
56
+ for _ , tc := range cases {
57
+ t .Run (tc .name , func (t * testing.T ) {
58
+ cert := & x509.Certificate {Subject : tc .subject , DNSNames : tc .dnsNames , IPAddresses : tc .ipAddresses }
59
+ csr := & x509.CertificateRequest {Subject : tc .subject , DNSNames : tc .dnsNames , IPAddresses : tc .ipAddresses }
60
+ test .AssertDeepEquals (t , FromCert (cert ), tc .expectedIdents )
61
+ test .AssertDeepEquals (t , FromCSR (csr ), tc .expectedIdents )
62
+ })
63
+ }
64
+ }
65
+
66
+ func TestNormalize (t * testing.T ) {
67
+ cases := []struct {
68
+ name string
69
+ idents []ACMEIdentifier
70
+ expected []ACMEIdentifier
71
+ }{
72
+ {
73
+ name : "convert to lowercase" ,
74
+ idents : []ACMEIdentifier {
75
+ {Type : TypeDNS , Value : "AlPha.example.coM" },
76
+ {Type : TypeIP , Value : "fe80::CAFE" },
77
+ },
78
+ expected : []ACMEIdentifier {
79
+ {Type : TypeDNS , Value : "alpha.example.com" },
80
+ {Type : TypeIP , Value : "fe80::cafe" },
81
+ },
54
82
},
55
83
{
56
- "duplicate SANs" ,
57
- & x509.CertificateRequest {DNSNames : []string {"b.com" , "b.com" , "a.com" , "a.com" }},
58
- []ACMEIdentifier {NewDNS ("a.com" ), NewDNS ("b.com" )},
84
+ name : "sort" ,
85
+ idents : []ACMEIdentifier {
86
+ {Type : TypeDNS , Value : "foobar.com" },
87
+ {Type : TypeDNS , Value : "bar.com" },
88
+ {Type : TypeDNS , Value : "baz.com" },
89
+ {Type : TypeDNS , Value : "a.com" },
90
+ {Type : TypeIP , Value : "fe80::cafe" },
91
+ {Type : TypeIP , Value : "2001:db8::1dea" },
92
+ {Type : TypeIP , Value : "192.168.1.1" },
93
+ },
94
+ expected : []ACMEIdentifier {
95
+ {Type : TypeDNS , Value : "a.com" },
96
+ {Type : TypeDNS , Value : "bar.com" },
97
+ {Type : TypeDNS , Value : "baz.com" },
98
+ {Type : TypeDNS , Value : "foobar.com" },
99
+ {Type : TypeIP , Value : "192.168.1.1" },
100
+ {Type : TypeIP , Value : "2001:db8::1dea" },
101
+ {Type : TypeIP , Value : "fe80::cafe" },
102
+ },
59
103
},
60
104
{
61
- "explicit CN not found in SANs" ,
62
- & x509.CertificateRequest {Subject : pkix.Name {CommonName : "a.com" }, DNSNames : []string {"b.com" }},
63
- []ACMEIdentifier {NewDNS ("a.com" ), NewDNS ("b.com" )},
105
+ name : "de-duplicate" ,
106
+ idents : []ACMEIdentifier {
107
+ {Type : TypeDNS , Value : "AlPha.example.coM" },
108
+ {Type : TypeIP , Value : "fe80::CAFE" },
109
+ {Type : TypeDNS , Value : "alpha.example.com" },
110
+ {Type : TypeIP , Value : "fe80::cafe" },
111
+ NewIP (netip .MustParseAddr ("fe80:0000:0000:0000:0000:0000:0000:cafe" )),
112
+ },
113
+ expected : []ACMEIdentifier {
114
+ {Type : TypeDNS , Value : "alpha.example.com" },
115
+ {Type : TypeIP , Value : "fe80::cafe" },
116
+ },
64
117
},
65
118
{
66
- "mix of DNSNames and IPAddresses" ,
67
- & x509.CertificateRequest {DNSNames : []string {"a.com" }, IPAddresses : []net.IP {{192 , 168 , 1 , 1 }}},
68
- []ACMEIdentifier {NewDNS ("a.com" ), NewIP (netip .MustParseAddr ("192.168.1.1" ))},
119
+ name : "DNS before IP" ,
120
+ idents : []ACMEIdentifier {
121
+ {Type : TypeIP , Value : "fe80::cafe" },
122
+ {Type : TypeDNS , Value : "alpha.example.com" },
123
+ },
124
+ expected : []ACMEIdentifier {
125
+ {Type : TypeDNS , Value : "alpha.example.com" },
126
+ {Type : TypeIP , Value : "fe80::cafe" },
127
+ },
69
128
},
70
129
}
71
130
for _ , tc := range cases {
72
131
t .Run (tc .name , func (t * testing.T ) {
73
- test .AssertDeepEquals (t , FromCSR ( tc .csr ), tc .expectedIdents )
132
+ test .AssertDeepEquals (t , tc .expected , Normalize ( tc .idents ) )
74
133
})
75
134
}
76
135
}
0 commit comments