@@ -6,9 +6,11 @@ import (
6
6
"crypto/x509"
7
7
"errors"
8
8
"fmt"
9
+ "io/ioutil"
9
10
"net"
10
11
"os"
11
12
"reflect"
13
+ "strings"
12
14
"testing"
13
15
"time"
14
16
@@ -17,6 +19,7 @@ import (
17
19
"go.mongodb.org/mongo-driver/bson/bsoncodec"
18
20
"go.mongodb.org/mongo-driver/event"
19
21
"go.mongodb.org/mongo-driver/internal"
22
+ "go.mongodb.org/mongo-driver/internal/testutil/assert"
20
23
"go.mongodb.org/mongo-driver/mongo/readconcern"
21
24
"go.mongodb.org/mongo-driver/mongo/readpref"
22
25
"go.mongodb.org/mongo-driver/mongo/writeconcern"
@@ -459,6 +462,42 @@ func TestClientOptions(t *testing.T) {
459
462
})
460
463
}
461
464
})
465
+ t .Run ("loadCACert" , func (t * testing.T ) {
466
+ caData := readFile (t , "testdata/ca.pem" )
467
+ keyData := readFile (t , "testdata/ca-key.pem" )
468
+ noCertErr := errors .New ("no CERTIFICATE section found" )
469
+ malformedErr := errors .New ("invalid .pem file" )
470
+
471
+ testCases := []struct {
472
+ name string
473
+ data []byte
474
+ err error
475
+ }{
476
+ {"file with certificate succeeds" , caData , nil },
477
+ {"empty file errors" , []byte {}, noCertErr },
478
+ {"file with no certificate errors" , keyData , noCertErr },
479
+ {"file with malformed data errors" , []byte {1 , 2 , 3 }, malformedErr },
480
+ }
481
+ for _ , tc := range testCases {
482
+ t .Run (tc .name , func (t * testing.T ) {
483
+ _ , err := loadCACert (tc .data )
484
+ if tc .err == nil {
485
+ assert .Nil (t , err , "loadCACert error: %v" , err )
486
+ return
487
+ }
488
+
489
+ assert .NotNil (t , err , "expected error %v, got nil" , tc .err )
490
+ containsMsg := strings .Contains (err .Error (), tc .err .Error ())
491
+ assert .True (t , containsMsg , "expected error %v, got %v" , tc .err , err )
492
+ })
493
+ }
494
+ })
495
+ }
496
+
497
+ func readFile (t * testing.T , path string ) []byte {
498
+ data , err := ioutil .ReadFile (path )
499
+ assert .Nil (t , err , "ReadFile error for %s: %v" , path , err )
500
+ return data
462
501
}
463
502
464
503
type testDialer struct {
0 commit comments