|
| 1 | +package identifier |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/x509" |
| 5 | + "crypto/x509/pkix" |
| 6 | + "net" |
| 7 | + "net/netip" |
| 8 | + "slices" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +// TestFromX509 tests FromCert and FromCSR, which are fromX509's public |
| 13 | +// wrappers. |
| 14 | +func TestFromX509(t *testing.T) { |
| 15 | + cases := []struct { |
| 16 | + name string |
| 17 | + subject pkix.Name |
| 18 | + dnsNames []string |
| 19 | + ipAddresses []net.IP |
| 20 | + want []ACMEIdentifier |
| 21 | + }{ |
| 22 | + { |
| 23 | + name: "no explicit CN", |
| 24 | + dnsNames: []string{"a.com"}, |
| 25 | + want: []ACMEIdentifier{NewDNS("a.com")}, |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "explicit uppercase CN", |
| 29 | + subject: pkix.Name{CommonName: "A.com"}, |
| 30 | + dnsNames: []string{"a.com"}, |
| 31 | + want: []ACMEIdentifier{NewDNS("a.com")}, |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "no explicit CN, uppercase SAN", |
| 35 | + dnsNames: []string{"A.com"}, |
| 36 | + want: []ACMEIdentifier{NewDNS("a.com")}, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "duplicate SANs", |
| 40 | + dnsNames: []string{"b.com", "b.com", "a.com", "a.com"}, |
| 41 | + want: []ACMEIdentifier{NewDNS("a.com"), NewDNS("b.com")}, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "explicit CN not found in SANs", |
| 45 | + subject: pkix.Name{CommonName: "a.com"}, |
| 46 | + dnsNames: []string{"b.com"}, |
| 47 | + want: []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 | + want: []ACMEIdentifier{NewDNS("a.com"), NewIP(netip.MustParseAddr("192.168.1.1"))}, |
| 54 | + }, |
| 55 | + } |
| 56 | + for _, tc := range cases { |
| 57 | + t.Run("cert/"+tc.name, func(t *testing.T) { |
| 58 | + t.Parallel() |
| 59 | + got := FromCert(&x509.Certificate{Subject: tc.subject, DNSNames: tc.dnsNames, IPAddresses: tc.ipAddresses}) |
| 60 | + if !slices.Equal(got, tc.want) { |
| 61 | + t.Errorf("FromCert() got %#v, but want %#v", got, tc.want) |
| 62 | + } |
| 63 | + }) |
| 64 | + t.Run("csr/"+tc.name, func(t *testing.T) { |
| 65 | + t.Parallel() |
| 66 | + got := FromCSR(&x509.CertificateRequest{Subject: tc.subject, DNSNames: tc.dnsNames, IPAddresses: tc.ipAddresses}) |
| 67 | + if !slices.Equal(got, tc.want) { |
| 68 | + t.Errorf("FromCSR() got %#v, but want %#v", got, tc.want) |
| 69 | + } |
| 70 | + }) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestNormalize(t *testing.T) { |
| 75 | + cases := []struct { |
| 76 | + name string |
| 77 | + idents []ACMEIdentifier |
| 78 | + want []ACMEIdentifier |
| 79 | + }{ |
| 80 | + { |
| 81 | + name: "convert to lowercase", |
| 82 | + idents: []ACMEIdentifier{ |
| 83 | + {Type: TypeDNS, Value: "AlPha.example.coM"}, |
| 84 | + {Type: TypeIP, Value: "fe80::CAFE"}, |
| 85 | + }, |
| 86 | + want: []ACMEIdentifier{ |
| 87 | + {Type: TypeDNS, Value: "alpha.example.com"}, |
| 88 | + {Type: TypeIP, Value: "fe80::cafe"}, |
| 89 | + }, |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "sort", |
| 93 | + idents: []ACMEIdentifier{ |
| 94 | + {Type: TypeDNS, Value: "foobar.com"}, |
| 95 | + {Type: TypeDNS, Value: "bar.com"}, |
| 96 | + {Type: TypeDNS, Value: "baz.com"}, |
| 97 | + {Type: TypeDNS, Value: "a.com"}, |
| 98 | + {Type: TypeIP, Value: "fe80::cafe"}, |
| 99 | + {Type: TypeIP, Value: "2001:db8::1dea"}, |
| 100 | + {Type: TypeIP, Value: "192.168.1.1"}, |
| 101 | + }, |
| 102 | + want: []ACMEIdentifier{ |
| 103 | + {Type: TypeDNS, Value: "a.com"}, |
| 104 | + {Type: TypeDNS, Value: "bar.com"}, |
| 105 | + {Type: TypeDNS, Value: "baz.com"}, |
| 106 | + {Type: TypeDNS, Value: "foobar.com"}, |
| 107 | + {Type: TypeIP, Value: "192.168.1.1"}, |
| 108 | + {Type: TypeIP, Value: "2001:db8::1dea"}, |
| 109 | + {Type: TypeIP, Value: "fe80::cafe"}, |
| 110 | + }, |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "de-duplicate", |
| 114 | + idents: []ACMEIdentifier{ |
| 115 | + {Type: TypeDNS, Value: "AlPha.example.coM"}, |
| 116 | + {Type: TypeIP, Value: "fe80::CAFE"}, |
| 117 | + {Type: TypeDNS, Value: "alpha.example.com"}, |
| 118 | + {Type: TypeIP, Value: "fe80::cafe"}, |
| 119 | + NewIP(netip.MustParseAddr("fe80:0000:0000:0000:0000:0000:0000:cafe")), |
| 120 | + }, |
| 121 | + want: []ACMEIdentifier{ |
| 122 | + {Type: TypeDNS, Value: "alpha.example.com"}, |
| 123 | + {Type: TypeIP, Value: "fe80::cafe"}, |
| 124 | + }, |
| 125 | + }, |
| 126 | + { |
| 127 | + name: "DNS before IP", |
| 128 | + idents: []ACMEIdentifier{ |
| 129 | + {Type: TypeIP, Value: "fe80::cafe"}, |
| 130 | + {Type: TypeDNS, Value: "alpha.example.com"}, |
| 131 | + }, |
| 132 | + want: []ACMEIdentifier{ |
| 133 | + {Type: TypeDNS, Value: "alpha.example.com"}, |
| 134 | + {Type: TypeIP, Value: "fe80::cafe"}, |
| 135 | + }, |
| 136 | + }, |
| 137 | + } |
| 138 | + for _, tc := range cases { |
| 139 | + t.Run(tc.name, func(t *testing.T) { |
| 140 | + t.Parallel() |
| 141 | + got := Normalize(tc.idents) |
| 142 | + if !slices.Equal(got, tc.want) { |
| 143 | + t.Errorf("Got %#v, but want %#v", got, tc.want) |
| 144 | + } |
| 145 | + }) |
| 146 | + } |
| 147 | +} |
0 commit comments