-
Notifications
You must be signed in to change notification settings - Fork 0
/
cert_test.go
876 lines (846 loc) · 20.4 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
package main
import (
"context"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"log"
"math/big"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"reflect"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
var (
host = "localhost"
port = "8443"
addr = host + ":" + port
)
func getTime(value string, loc *time.Location) time.Time {
t, _ := time.Parse(time.RFC3339, value)
return t.In(loc)
}
func getNotBefore(loc *time.Location) time.Time {
return getTime("2023-01-01T09:00:00+09:00", loc)
}
func getNotAfter(loc *time.Location) time.Time {
return time.Now().Truncate(time.Hour).In(loc).Add(24 * time.Hour)
}
func getCurrentTime(loc *time.Location) time.Time {
return time.Now().Truncate(time.Hour).In(loc)
}
func TestMain(m *testing.M) {
server, tempDir, err := setup(addr)
if err != nil {
log.Fatal("failed to setup: ", err)
}
code := m.Run()
if err := teardown(server, tempDir); err != nil {
log.Fatal("failed to teardown: ", err)
}
os.Exit(code)
}
func setup(addr string) (*http.Server, string, error) {
if err := setupEnv(); err != nil {
return nil, "", fmt.Errorf("failed to set environment valiable: %w", err)
}
tempDir, certFile, keyFile, err := setupPath()
if err != nil {
return nil, "", fmt.Errorf("failed to create temp dir: %w", err)
}
if err := setupCert(certFile, keyFile); err != nil {
return nil, "", fmt.Errorf("failed to create certificate: %w", err)
}
server := setupServer(addr)
ch := make(chan error, 1)
go func() {
if err := server.ListenAndServeTLS(certFile, keyFile); err != http.ErrServerClosed {
ch <- err
}
close(ch)
}()
if err := waitServer(addr, 5*time.Second); err != nil {
return nil, "", fmt.Errorf("failed to start server: %w", err)
}
select {
case err := <-ch:
if err != nil {
return nil, "", fmt.Errorf("failed to run server: %w", err)
}
default:
}
return server, tempDir, nil
}
func setupEnv() error {
err := os.Setenv("TZ", "Asia/Tokyo")
if err != nil {
return err
}
return nil
}
func setupPath() (tempDir, certFile, keyFile string, err error) {
tempDir, err = os.MkdirTemp("", "test")
if err != nil {
return "", "", "", err
}
return tempDir, filepath.Join(tempDir, "cert.pem"), filepath.Join(tempDir, "key.pem"), nil
}
func setupCert(certFile, keyFile string) error {
// create private key
privKey, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
return err
}
// configure certificate
serialNumber, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128))
if err != nil {
return err
}
tmpl := x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{CommonName: "local test CA"},
NotBefore: getNotBefore(time.Local),
NotAfter: getNotAfter(time.Local),
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
}
// create certificate
derBytes, err := x509.CreateCertificate(rand.Reader, &tmpl, &tmpl, &privKey.PublicKey, privKey)
if err != nil {
return err
}
// save certificate in pem
certOut, err := os.Create(certFile)
if err != nil {
return err
}
if err := pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}); err != nil {
return err
}
certOut.Close()
// convert private key
privBytes, err := x509.MarshalPKCS8PrivateKey(privKey)
if err != nil {
return err
}
// save private key in pem
keyOut, err := os.Create(keyFile)
if err != nil {
return err
}
if err := pem.Encode(keyOut, &pem.Block{Type: "PRIVATE KEY", Bytes: privBytes}); err != nil {
return err
}
keyOut.Close()
return nil
}
func setupServer(addr string) *http.Server {
handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "test server")
})
tlsConfig := &tls.Config{
MinVersion: tls.VersionTLS12,
InsecureSkipVerify: true, // #nosec G402
}
server := &http.Server{
Addr: addr,
Handler: handler,
TLSConfig: tlsConfig,
ReadHeaderTimeout: 5 * time.Second,
}
return server
}
func waitServer(addr string, timeout time.Duration) error {
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
conn, err := tls.Dial("tcp", addr, &tls.Config{
InsecureSkipVerify: true, // #nosec G402
})
if err == nil {
conn.Close()
return nil
}
time.Sleep(100 * time.Millisecond)
}
return fmt.Errorf("cannot start server in %v", timeout)
}
func teardown(server *http.Server, tempDir string) error {
defer os.RemoveAll(tempDir)
if err := server.Shutdown(context.Background()); err != nil {
return fmt.Errorf("cannot shutdown server: %w", err)
}
return nil
}
func Test_getCertList(t *testing.T) {
ctx := context.Background()
type args struct {
ctx context.Context
addrs []string
timeout time.Duration
location *time.Location
insecure bool
}
tests := []struct {
name string
args args
want []*certInfo
wantErr bool
}{
{
name: "basic",
args: args{
ctx: ctx,
addrs: []string{addr},
timeout: 5 * time.Second,
location: time.Local,
insecure: true,
},
want: []*certInfo{
{
DomainName: host,
AccessPort: port,
IPAddresses: []net.IP{net.ParseIP("::1"), net.ParseIP("127.0.0.1")},
Issuer: "CN=local test CA",
CommonName: "local test CA",
SANs: []string{},
NotBefore: getNotBefore(time.Local),
NotAfter: getNotAfter(time.Local),
CurrentTime: getCurrentTime(time.Local),
DaysLeft: 1,
},
},
wantErr: false,
},
{
name: "utc",
args: args{
ctx: ctx,
addrs: []string{addr},
timeout: 5 * time.Second,
location: time.UTC,
insecure: true,
},
want: []*certInfo{
{
DomainName: host,
AccessPort: port,
IPAddresses: []net.IP{net.ParseIP("::1"), net.ParseIP("127.0.0.1")},
Issuer: "CN=local test CA",
CommonName: "local test CA",
SANs: []string{},
NotBefore: getNotBefore(time.UTC),
NotAfter: getNotAfter(time.UTC),
CurrentTime: getCurrentTime(time.UTC),
DaysLeft: 1,
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := getCertList(tt.args.ctx, tt.args.addrs, tt.args.timeout, tt.args.insecure, tt.args.location)
if (err != nil) != tt.wantErr {
t.Errorf("getCertList() error = %v, wantErr %v", err, tt.wantErr)
return
}
for _, g := range got {
for _, w := range tt.want {
if !reflect.DeepEqual(g.DomainName, w.DomainName) {
t.Errorf("DoaminName = %v, want %v", g.DomainName, w.DomainName)
}
if !reflect.DeepEqual(g.AccessPort, w.AccessPort) {
t.Errorf("AccessPort = %v, want %v", g.AccessPort, w.AccessPort)
}
if diff := cmp.Diff(g.IPAddresses, w.IPAddresses); diff != "" {
t.Error(diff)
}
if !reflect.DeepEqual(g.Issuer, w.Issuer) {
t.Errorf("Issuer = %v, want %v", g.Issuer, w.Issuer)
}
if !reflect.DeepEqual(g.CommonName, w.CommonName) {
t.Errorf("CommonName = %v, want %v", g.CommonName, w.CommonName)
}
if !reflect.DeepEqual(g.SANs, w.SANs) {
t.Errorf("SANs = %v, want %v", g.SANs, w.SANs)
}
if !reflect.DeepEqual(g.NotBefore, w.NotBefore) {
t.Errorf("NotBefore = %v, want %v", g.NotBefore, w.NotBefore)
}
if !reflect.DeepEqual(g.NotAfter, w.NotAfter) {
t.Errorf("NotAfter = %v, want %v", g.NotAfter, w.NotAfter)
}
}
}
})
}
}
func Test_newConnector(t *testing.T) {
type args struct {
addr string
timeout time.Duration
location *time.Location
insecure bool
}
tests := []struct {
name string
args args
want *connector
wantErr bool
}{
{
name: "basic",
args: args{
addr: addr,
timeout: 5 * time.Second,
location: time.Local,
insecure: false,
},
want: &connector{
addr: addr,
host: host,
port: port,
timeout: 5 * time.Second,
location: time.Local,
tlsConfig: &tls.Config{
ServerName: host,
MinVersion: tls.VersionTLS12,
InsecureSkipVerify: false, // #nosec G402
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := newConnector(tt.args.addr, tt.args.timeout, tt.args.insecure, tt.args.location)
if (err != nil) != tt.wantErr {
t.Errorf("newConnector() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got.addr, tt.want.addr) {
t.Errorf("addr = %v, want %v", got.addr, tt.want.addr)
}
if !reflect.DeepEqual(got.host, tt.want.host) {
t.Errorf("host = %v, want %v", got.host, tt.want.host)
}
if !reflect.DeepEqual(got.port, tt.want.port) {
t.Errorf("port = %v, want %v", got.port, tt.want.port)
}
if !reflect.DeepEqual(got.timeout, tt.want.timeout) {
t.Errorf("timeout = %v, want %v", got.timeout, tt.want.timeout)
}
if !reflect.DeepEqual(got.location, tt.want.location) {
t.Errorf("location = %v, want %v", got.location, tt.want.location)
}
if !reflect.DeepEqual(got.tlsConfig, tt.want.tlsConfig) {
t.Errorf("tlsConfig = %v, want %v", got.tlsConfig, tt.want.tlsConfig)
}
})
}
}
func Test_connector_lookupIP(t *testing.T) {
ctx := context.Background()
type fields struct {
addr string
host string
port string
ips []net.IP
timeout time.Duration
tlsConfig *tls.Config
tlsConn *tls.Conn
}
type args struct {
ctx context.Context
}
tests := []struct {
name string
fields fields
args args
want []net.IP
}{
{
name: "basic",
fields: fields{
addr: addr,
host: host,
port: port,
ips: nil,
timeout: 5 * time.Second,
tlsConfig: nil,
tlsConn: nil,
},
args: args{
ctx: ctx,
},
want: []net.IP{net.ParseIP("::1"), net.ParseIP("127.0.0.1")},
},
{
name: "empty",
fields: fields{
addr: addr,
host: "dummy",
port: port,
ips: nil,
timeout: 5 * time.Second,
tlsConfig: nil,
tlsConn: nil,
},
args: args{
ctx: ctx,
},
want: []net.IP{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ipMap.Delete(tt.fields.host)
c := &connector{
addr: tt.fields.addr,
host: tt.fields.host,
port: tt.fields.port,
ips: tt.fields.ips,
timeout: tt.fields.timeout,
tlsConfig: tt.fields.tlsConfig,
tlsConn: tt.fields.tlsConn,
}
c.lookupIP(tt.args.ctx)
if diff := cmp.Diff(c.ips, tt.want); diff != "" {
t.Error(diff)
}
})
}
}
func Test_connector_getTLSConn(t *testing.T) {
ctx := context.Background()
type fields struct {
addr string
host string
port string
ips []net.IP
timeout time.Duration
tlsConfig *tls.Config
tlsConn *tls.Conn
}
type args struct {
ctx context.Context
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
{
name: "basic",
fields: fields{
addr: addr,
host: host,
port: port,
ips: nil,
timeout: 5 * time.Second,
tlsConfig: &tls.Config{
ServerName: host,
MinVersion: tls.VersionTLS12,
InsecureSkipVerify: true, // #nosec G402
},
tlsConn: nil,
},
args: args{
ctx: ctx,
},
wantErr: false,
},
{
name: "error",
fields: fields{
addr: ":443",
host: host,
port: port,
ips: nil,
timeout: 5 * time.Second,
tlsConfig: &tls.Config{
ServerName: host,
MinVersion: tls.VersionTLS12,
InsecureSkipVerify: true, // #nosec G402
},
tlsConn: nil,
},
args: args{
ctx: ctx,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
connMap.Delete(tt.fields.host)
c := &connector{
addr: tt.fields.addr,
host: tt.fields.host,
port: tt.fields.port,
ips: tt.fields.ips,
timeout: tt.fields.timeout,
tlsConfig: tt.fields.tlsConfig,
tlsConn: tt.fields.tlsConn,
}
if err := c.getTLSConn(tt.args.ctx); (err != nil) != tt.wantErr {
t.Errorf("connector.getTLSConn() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func Test_connector_getServerCert(t *testing.T) {
ctx := context.Background()
type fields struct {
addr string
host string
port string
ips []net.IP
timeout time.Duration
location *time.Location
tlsConfig *tls.Config
tlsConn *tls.Conn
}
tests := []struct {
name string
fields fields
want *certInfo
wantErr bool
}{
{
name: "basic",
fields: fields{
addr: addr,
host: host,
port: port,
ips: []net.IP{},
timeout: 5 * time.Second,
location: time.Local,
tlsConfig: &tls.Config{
ServerName: host,
MinVersion: tls.VersionTLS12,
InsecureSkipVerify: true, // #nosec G402
},
},
want: &certInfo{
DomainName: host,
AccessPort: port,
IPAddresses: []net.IP{},
Issuer: "CN=local test CA",
CommonName: "local test CA",
SANs: []string{},
NotBefore: getNotBefore(time.Local),
NotAfter: getNotAfter(time.Local),
CurrentTime: getCurrentTime(time.Local),
DaysLeft: 1,
},
wantErr: false,
},
{
name: "utc",
fields: fields{
addr: addr,
host: host,
port: port,
ips: []net.IP{},
timeout: 5 * time.Second,
location: time.UTC,
tlsConfig: &tls.Config{
ServerName: host,
MinVersion: tls.VersionTLS12,
InsecureSkipVerify: true, // #nosec G402
},
},
want: &certInfo{
DomainName: host,
AccessPort: port,
IPAddresses: []net.IP{},
Issuer: "CN=local test CA",
CommonName: "local test CA",
SANs: []string{},
NotBefore: getNotBefore(time.UTC),
NotAfter: getNotAfter(time.UTC),
CurrentTime: getCurrentTime(time.UTC),
DaysLeft: 1,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &connector{
addr: tt.fields.addr,
host: tt.fields.host,
port: tt.fields.port,
ips: tt.fields.ips,
timeout: tt.fields.timeout,
location: tt.fields.location,
tlsConfig: tt.fields.tlsConfig,
tlsConn: tt.fields.tlsConn,
}
if err := c.getTLSConn(ctx); err != nil {
t.Fatal(err)
}
got, err := c.getServerCert()
if (err != nil) != tt.wantErr {
t.Errorf("connector.getServerCert() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got.DomainName, tt.want.DomainName) {
t.Errorf("DoaminName = %v, want %v", got.DomainName, tt.want.DomainName)
}
if !reflect.DeepEqual(got.AccessPort, tt.want.AccessPort) {
t.Errorf("AccessPort = %v, want %v", got.AccessPort, tt.want.AccessPort)
}
if !reflect.DeepEqual(got.IPAddresses, tt.want.IPAddresses) {
t.Errorf("IPAddresses = %v, want %v", got.IPAddresses, tt.want.IPAddresses)
}
if !reflect.DeepEqual(got.Issuer, tt.want.Issuer) {
t.Errorf("Issuer = %v, want %v", got.Issuer, tt.want.Issuer)
}
if !reflect.DeepEqual(got.CommonName, tt.want.CommonName) {
t.Errorf("CommonName = %v, want %v", got.CommonName, tt.want.CommonName)
}
if !reflect.DeepEqual(got.SANs, tt.want.SANs) {
t.Errorf("SANs = %v, want %v", got.SANs, tt.want.SANs)
}
if !reflect.DeepEqual(got.NotBefore, tt.want.NotBefore) {
t.Errorf("NotBefore = %v, want %v", got.NotBefore, tt.want.NotBefore)
}
if !reflect.DeepEqual(got.NotAfter, tt.want.NotAfter) {
t.Errorf("NotAfter = %v, want %v", got.NotAfter, tt.want.NotAfter)
}
})
}
}
func Test_daysLeft(t *testing.T) {
type args struct {
notAfter time.Time
now time.Time
}
tests := []struct {
name string
args args
want int
}{
{
name: "full year difference",
args: args{
notAfter: time.Date(2024, 1, 1, 9, 0, 0, 0, time.UTC),
now: time.Date(2023, 1, 1, 9, 0, 0, 0, time.UTC),
},
want: 365,
},
{
name: "one day before new year",
args: args{
notAfter: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC),
now: time.Date(2023, 12, 31, 0, 0, 0, 0, time.UTC),
},
want: 1,
},
{
name: "one day difference",
args: args{
notAfter: time.Date(2023, 1, 2, 9, 0, 0, 0, time.UTC),
now: time.Date(2023, 1, 1, 9, 0, 0, 0, time.UTC),
},
want: 1,
},
{
name: "end of year",
args: args{
notAfter: time.Date(2023, 12, 31, 23, 59, 59, 0, time.UTC),
now: time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC),
},
want: 364,
},
{
name: "same day and time",
args: args{
notAfter: time.Date(2023, 1, 1, 9, 0, 0, 0, time.UTC),
now: time.Date(2023, 1, 1, 9, 0, 0, 0, time.UTC),
},
want: 0,
},
{
name: "one second difference",
args: args{
notAfter: time.Date(2023, 1, 1, 9, 0, 1, 0, time.UTC),
now: time.Date(2023, 1, 1, 9, 0, 0, 0, time.UTC),
},
want: 0,
},
{
name: "less than one day",
args: args{
notAfter: time.Date(2024, 1, 1, 9, 0, 0, 0, time.UTC),
now: time.Date(2023, 12, 31, 9, 0, 1, 0, time.UTC),
},
want: 0,
},
{
name: "last second",
args: args{
notAfter: time.Date(2024, 1, 1, 9, 0, 0, 0, time.UTC),
now: time.Date(2023, 12, 31, 8, 59, 59, 0, time.UTC),
},
want: 1,
},
{
name: "leap year difference",
args: args{
notAfter: time.Date(2024, 12, 31, 23, 59, 59, 0, time.UTC),
now: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC),
},
want: 365,
},
{
name: "end of leap year",
args: args{
notAfter: time.Date(2024, 2, 29, 0, 0, 0, 0, time.UTC),
now: time.Date(2024, 2, 28, 0, 0, 0, 0, time.UTC),
},
want: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := daysLeft(tt.args.notAfter, tt.args.now); got != tt.want {
t.Errorf("daysLeft() = %v, want %v", got, tt.want)
}
})
}
}
func Test_ensureDefaultPort(t *testing.T) {
type args struct {
addr string
}
tests := []struct {
name string
args args
want string
}{
{
name: "basic",
args: args{
addr: addr,
},
want: addr,
},
{
name: "default port",
args: args{
addr: host,
},
want: host + ":443",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ensureDefaultPort(tt.args.addr); got != tt.want {
t.Errorf("ensureDefaultPort() = %v, want %v", got, tt.want)
}
})
}
}
func Test_ensureHostPort(t *testing.T) {
type args struct {
addr string
}
tests := []struct {
name string
args args
wantHost string
wantPort string
wantErr bool
}{
{
name: "basic",
args: args{
addr: addr,
},
wantHost: host,
wantPort: port,
wantErr: false,
},
{
name: "invalid addr error",
args: args{
addr: "localhost::443",
},
wantHost: "",
wantPort: "",
wantErr: true,
},
{
name: "port out of range error",
args: args{
addr: "localhost:65536",
},
wantHost: "",
wantPort: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotHost, gotPort, err := ensureHostPort(tt.args.addr)
if (err != nil) != tt.wantErr {
t.Errorf("ensureHostPort() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotHost != tt.wantHost {
t.Errorf("ensureHostPort() gotHost = %v, want %v", gotHost, tt.wantHost)
}
if gotPort != tt.wantPort {
t.Errorf("ensureHostPort() gotPort = %v, want %v", gotPort, tt.wantPort)
}
})
}
}
func Test_getSANs(t *testing.T) {
uri, err := url.Parse("example.com/")
if err != nil {
t.Fatal(err)
}
type args struct {
cert *x509.Certificate
}
tests := []struct {
name string
args args
want []string
}{
{
name: "basic",
args: args{
cert: &x509.Certificate{
DNSNames: []string{"example.com", "www.example.com"},
EmailAddresses: []string{"[email protected]", "[email protected]"},
IPAddresses: []net.IP{net.ParseIP("192.168.10.10"), net.ParseIP("192.168.10.20")},
URIs: []*url.URL{uri},
},
},
want: []string{"example.com", "www.example.com", "[email protected]", "[email protected]", "192.168.10.10", "192.168.10.20", "example.com/"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getSANs(tt.args.cert); !reflect.DeepEqual(got, tt.want) {
t.Errorf("getSANs() = %v, want %v", got, tt.want)
}
})
}
}