@@ -5,6 +5,7 @@ package config
5
5
6
6
import (
7
7
"context"
8
+ "crypto/tls"
8
9
"encoding/json"
9
10
"errors"
10
11
"os"
@@ -511,6 +512,62 @@ func TestSerializeJSON(t *testing.T) {
511
512
}
512
513
}
513
514
515
+ func TestCreateTLSConfig (t * testing.T ) {
516
+ tests := []struct {
517
+ name string
518
+ caCertFile * string
519
+ clientCertFile * string
520
+ clientKeyFile * string
521
+ wantErrContains string
522
+ want func (* tls.Config , * testing.T )
523
+ }{
524
+ {
525
+ name : "no-input" ,
526
+ want : func (result * tls.Config , t * testing.T ) {
527
+ require .Nil (t , result .Certificates )
528
+ require .Nil (t , result .RootCAs )
529
+ },
530
+ },
531
+ {
532
+ name : "only-cacert-provided" ,
533
+ caCertFile : ptr (filepath .Join (".." , "testdata" , "ca.crt" )),
534
+ want : func (result * tls.Config , t * testing.T ) {
535
+ require .Nil (t , result .Certificates )
536
+ require .NotNil (t , result .RootCAs )
537
+ },
538
+ },
539
+ {
540
+ name : "nonexistent-cacert-file" ,
541
+ caCertFile : ptr ("nowhere.crt" ),
542
+ wantErrContains : "open nowhere.crt:" ,
543
+ },
544
+ {
545
+ name : "nonexistent-clientcert-file" ,
546
+ clientCertFile : ptr ("nowhere.crt" ),
547
+ clientKeyFile : ptr ("nowhere.crt" ),
548
+ wantErrContains : "could not use client certificate: open nowhere.crt:" ,
549
+ },
550
+ {
551
+ name : "bad-cacert-file" ,
552
+ caCertFile : ptr (filepath .Join (".." , "testdata" , "bad_cert.crt" )),
553
+ wantErrContains : "could not create certificate authority chain from certificate" ,
554
+ },
555
+ }
556
+
557
+ for _ , tt := range tests {
558
+ t .Run (tt .name , func (t * testing.T ) {
559
+ got , err := createTLSConfig (tt .caCertFile , tt .clientCertFile , tt .clientKeyFile )
560
+
561
+ if tt .wantErrContains != "" {
562
+ require .Contains (t , err .Error (), tt .wantErrContains )
563
+ } else {
564
+ require .NoError (t , err )
565
+ tt .want (got , t )
566
+ }
567
+ })
568
+ }
569
+ }
570
+
514
571
func ptr [T any ](v T ) * T {
515
572
return & v
516
573
}
0 commit comments