55 "crypto/x509/pkix"
66 "net"
77 "reflect"
8+ "slices"
89 "testing"
910)
1011
@@ -14,13 +15,15 @@ func TestNewCertInfo(t *testing.T) {
1415 san string
1516 usage string
1617 extUsage string
18+ isCA bool
1719 expect * CertInfo
1820 }{
1921 {
2022 subject : "CN=root-ca/C=CN/ST=Beijing/L=Haidian/O=Root Inc" ,
2123 san : "localhost,127.0.0.1,1.1.1.1,china.com" ,
22- usage : "cRLSign" ,
24+ usage : "cRLSign,keyCertSign,digitalSignature " ,
2325 extUsage : "" ,
26+ isCA : true ,
2427 expect : & CertInfo {
2528 Subject : & pkix.Name {
2629 CommonName : "root-ca" ,
@@ -31,15 +34,16 @@ func TestNewCertInfo(t *testing.T) {
3134 },
3235 DNSNames : []string {"localhost" , "china.com" },
3336 IPAddrs : []net.IP {net .IPv4 (127 , 0 , 0 , 1 ), net .IPv4 (1 , 1 , 1 , 1 )},
34- KeyUsage : x509 .KeyUsageDigitalSignature | x509 .KeyUsageKeyEncipherment | x509 .KeyUsageCertSign | x509 . KeyUsageCRLSign ,
35- ExtKeyUsage : []x509.ExtKeyUsage {x509 . ExtKeyUsageServerAuth , x509 . ExtKeyUsageClientAuth },
37+ KeyUsage : x509 .KeyUsageDigitalSignature | x509 .KeyUsageCRLSign | x509 .KeyUsageCertSign ,
38+ ExtKeyUsage : []x509.ExtKeyUsage {},
3639 },
3740 },
3841 {
39- subject : " CN= root-ca/ C =CN / ST = Beijing / L= Haidian/ O = Root Inc " ,
40- san : "DNS:localhost,IP: 127.0.0.1 , DNS: localhost,IP:1.1.1.1, china.com, 127.0.0.1" ,
41- usage : "cRLSign , keyCertSign " ,
42- extUsage : " clientAuth ,serverAuth " ,
42+ subject : " CN = root-ca /C =CN /ST = Beijing/L = Haidian/O= Root Inc " ,
43+ san : "localhost,127.0.0.1,1.1.1.1,china.com" ,
44+ usage : "cRLSign,keyCertSign,digitalSignature" ,
45+ extUsage : "" ,
46+ isCA : true ,
4347 expect : & CertInfo {
4448 Subject : & pkix.Name {
4549 CommonName : "root-ca" ,
@@ -50,65 +54,73 @@ func TestNewCertInfo(t *testing.T) {
5054 },
5155 DNSNames : []string {"localhost" , "china.com" },
5256 IPAddrs : []net.IP {net .IPv4 (127 , 0 , 0 , 1 ), net .IPv4 (1 , 1 , 1 , 1 )},
53- KeyUsage : x509 .KeyUsageDigitalSignature | x509 .KeyUsageKeyEncipherment | x509 .KeyUsageCertSign | x509 . KeyUsageCRLSign ,
54- ExtKeyUsage : []x509.ExtKeyUsage {x509 . ExtKeyUsageClientAuth , x509 . ExtKeyUsageServerAuth },
57+ KeyUsage : x509 .KeyUsageDigitalSignature | x509 .KeyUsageCRLSign | x509 .KeyUsageCertSign ,
58+ ExtKeyUsage : []x509.ExtKeyUsage {},
5559 },
5660 },
5761 {
58- subject : " CN= root-ca/ C = / ST = Beijing / L= Haidian/ O = Root Inc /O=Union Inc " ,
62+ subject : "CN=china/C=CN/ST= Beijing/L= Haidian/O=China Inc" ,
5963 san : "DNS:localhost,IP: 127.0.0.1 , DNS: localhost,IP:1.1.1.1, china.com, 127.0.0.1" ,
60- usage : "cRLSign , keyCertSign " ,
64+ usage : "digitalSignature , keyEncipherment " ,
6165 extUsage : " clientAuth ,serverAuth " ,
66+ isCA : false ,
6267 expect : & CertInfo {
6368 Subject : & pkix.Name {
64- CommonName : "root-ca" ,
69+ CommonName : "china" ,
70+ Country : []string {"CN" },
6571 Province : []string {"Beijing" },
6672 Locality : []string {"Haidian" },
67- Organization : []string {"Root Inc" , "Union Inc" },
73+ Organization : []string {"China Inc" },
6874 },
6975 DNSNames : []string {"localhost" , "china.com" },
7076 IPAddrs : []net.IP {net .IPv4 (127 , 0 , 0 , 1 ), net .IPv4 (1 , 1 , 1 , 1 )},
71- KeyUsage : x509 .KeyUsageDigitalSignature | x509 .KeyUsageKeyEncipherment | x509 . KeyUsageCertSign | x509 . KeyUsageCRLSign ,
77+ KeyUsage : x509 .KeyUsageDigitalSignature | x509 .KeyUsageKeyEncipherment ,
7278 ExtKeyUsage : []x509.ExtKeyUsage {x509 .ExtKeyUsageClientAuth , x509 .ExtKeyUsageServerAuth },
7379 },
7480 },
7581 {
76- subject : " CN= Root-CA/ C = / ST = Beijing / L= Haidian/ O = Root Inc /O=Union Inc " ,
77- usage : "cRLSign , keyCertSign " ,
82+ subject : " CN= china/ C =CN / ST = Beijing / L= Haidian/ O = China Inc " ,
83+ san : "DNS:localhost,IP: 127.0.0.1 , DNS: localhost,IP:1.1.1.1, china.com, 127.0.0.1" ,
84+ usage : "digitalSignature , keyEncipherment " ,
7885 extUsage : " clientAuth ,serverAuth " ,
86+ isCA : false ,
7987 expect : & CertInfo {
8088 Subject : & pkix.Name {
81- CommonName : "Root-CA" ,
89+ CommonName : "china" ,
90+ Country : []string {"CN" },
8291 Province : []string {"Beijing" },
8392 Locality : []string {"Haidian" },
84- Organization : []string {"Root Inc" , "Union Inc" },
93+ Organization : []string {"China Inc" },
8594 },
86- DNSNames : []string {"root-ca" },
87- KeyUsage : x509 .KeyUsageDigitalSignature | x509 .KeyUsageKeyEncipherment | x509 .KeyUsageCertSign | x509 .KeyUsageCRLSign ,
95+ DNSNames : []string {"localhost" , "china.com" },
96+ IPAddrs : []net.IP {net .IPv4 (127 , 0 , 0 , 1 ), net .IPv4 (1 , 1 , 1 , 1 )},
97+ KeyUsage : x509 .KeyUsageDigitalSignature | x509 .KeyUsageKeyEncipherment ,
8898 ExtKeyUsage : []x509.ExtKeyUsage {x509 .ExtKeyUsageClientAuth , x509 .ExtKeyUsageServerAuth },
8999 },
90100 },
91101 {
92- subject : " CN= 192.168.122.10/ C = / ST = Beijing / L= Haidian/ O = Root Inc /O=Union Inc " ,
93- usage : "cRLSign , keyCertSign " ,
102+ subject : " CN= Union/ C = / ST = Beijing / L= Haidian/ O = Root Inc /O=Union Inc " ,
103+ san : "192.168.122.10" ,
104+ usage : " , keyCertSign " ,
94105 extUsage : " clientAuth ,serverAuth " ,
106+ isCA : false ,
95107 expect : & CertInfo {
96108 Subject : & pkix.Name {
97- CommonName : "192.168.122.10 " ,
109+ CommonName : "Union " ,
98110 Province : []string {"Beijing" },
99111 Locality : []string {"Haidian" },
100112 Organization : []string {"Root Inc" , "Union Inc" },
101113 },
102114 DNSNames : nil , // either nil or remove this assignment
103115 IPAddrs : []net.IP {net .IPv4 (192 , 168 , 122 , 10 )},
104- KeyUsage : x509 .KeyUsageDigitalSignature | x509 . KeyUsageKeyEncipherment | x509 . KeyUsageCertSign | x509 . KeyUsageCRLSign ,
116+ KeyUsage : x509 .KeyUsageCertSign ,
105117 ExtKeyUsage : []x509.ExtKeyUsage {x509 .ExtKeyUsageClientAuth , x509 .ExtKeyUsageServerAuth },
106118 },
107119 },
108120 }
109121
110122 for _ , test := range tests {
111- certInfo , err := NewCertInfo (0 , test .subject , test .san , test .usage , test .extUsage , true )
123+ certInfo , err := NewCertInfo (0 , test .subject , test .san , test .usage , test .extUsage , test . isCA )
112124 if err != nil {
113125 t .Errorf ("something wrong" )
114126 }
@@ -129,7 +141,7 @@ func TestNewCertInfo(t *testing.T) {
129141 t .Errorf ("failed NewCertInfo.KeyUsage:\n \t actual: %v\n \t expect: %v\n " , certInfo .KeyUsage , test .expect .KeyUsage )
130142 }
131143
132- if ! reflect . DeepEqual (certInfo .ExtKeyUsage , test .expect .ExtKeyUsage ) {
144+ if ! slices . Equal (certInfo .ExtKeyUsage , test .expect .ExtKeyUsage ) {
133145 t .Errorf ("failed NewCertInfo.ExtKeyUsage:\n \t actual: %v\n \t expect: %v\n " , certInfo .ExtKeyUsage , test .expect .ExtKeyUsage )
134146 }
135147 }
0 commit comments