@@ -11,14 +11,13 @@ import (
11
11
"strings"
12
12
"testing"
13
13
14
- "reflect"
15
-
16
14
"encoding/base64"
17
15
18
16
"github.com/mongodb/mongo-go-driver/bson"
19
17
. "github.com/mongodb/mongo-go-driver/core/auth"
20
18
"github.com/mongodb/mongo-go-driver/core/description"
21
19
"github.com/mongodb/mongo-go-driver/core/wiremessage"
20
+ "github.com/mongodb/mongo-go-driver/internal"
22
21
)
23
22
24
23
func TestPlainAuthenticator_Fails (t * testing.T ) {
@@ -30,17 +29,21 @@ func TestPlainAuthenticator_Fails(t *testing.T) {
30
29
}
31
30
32
31
resps := make (chan wiremessage.WireMessage , 1 )
33
- resps <- makeReply (t , bson .NewDocument (
32
+ resps <- internal . MakeReply (t , bson .NewDocument (
34
33
bson .EC .Int32 ("ok" , 1 ),
35
34
bson .EC .Int32 ("conversationId" , 1 ),
36
35
bson .EC .Binary ("payload" , []byte {}),
37
36
bson .EC .Int32 ("code" , 143 ),
38
37
bson .EC .Boolean ("done" , true )),
39
38
)
40
39
41
- c := & conn { written : make (chan wiremessage.WireMessage , 1 ), readResp : resps }
40
+ c := & internal. ChannelConn { Written : make (chan wiremessage.WireMessage , 1 ), ReadResp : resps }
42
41
43
- err := authenticator .Auth (context .Background (), description.Server {}, c )
42
+ err := authenticator .Auth (context .Background (), description.Server {
43
+ WireVersion : & description.VersionRange {
44
+ Max : 6 ,
45
+ },
46
+ }, c )
44
47
if err == nil {
45
48
t .Fatalf ("expected an error but got none" )
46
49
}
@@ -60,22 +63,26 @@ func TestPlainAuthenticator_Extra_server_message(t *testing.T) {
60
63
}
61
64
62
65
resps := make (chan wiremessage.WireMessage , 2 )
63
- resps <- makeReply (t , bson .NewDocument (
66
+ resps <- internal . MakeReply (t , bson .NewDocument (
64
67
bson .EC .Int32 ("ok" , 1 ),
65
68
bson .EC .Int32 ("conversationId" , 1 ),
66
69
bson .EC .Binary ("payload" , []byte {}),
67
70
bson .EC .Boolean ("done" , false )),
68
71
)
69
- resps <- makeReply (t , bson .NewDocument (
72
+ resps <- internal . MakeReply (t , bson .NewDocument (
70
73
bson .EC .Int32 ("ok" , 1 ),
71
74
bson .EC .Int32 ("conversationId" , 1 ),
72
75
bson .EC .Binary ("payload" , []byte {}),
73
76
bson .EC .Boolean ("done" , true )),
74
77
)
75
78
76
- c := & conn { written : make (chan wiremessage.WireMessage , 1 ), readResp : resps }
79
+ c := & internal. ChannelConn { Written : make (chan wiremessage.WireMessage , 1 ), ReadResp : resps }
77
80
78
- err := authenticator .Auth (context .Background (), description.Server {}, c )
81
+ err := authenticator .Auth (context .Background (), description.Server {
82
+ WireVersion : & description.VersionRange {
83
+ Max : 6 ,
84
+ },
85
+ }, c )
79
86
if err == nil {
80
87
t .Fatalf ("expected an error but got none" )
81
88
}
@@ -95,36 +102,33 @@ func TestPlainAuthenticator_Succeeds(t *testing.T) {
95
102
}
96
103
97
104
resps := make (chan wiremessage.WireMessage , 1 )
98
- resps <- makeReply (t , bson .NewDocument (
105
+ resps <- internal . MakeReply (t , bson .NewDocument (
99
106
bson .EC .Int32 ("ok" , 1 ),
100
107
bson .EC .Int32 ("conversationId" , 1 ),
101
108
bson .EC .Binary ("payload" , []byte {}),
102
109
bson .EC .Boolean ("done" , true )),
103
110
)
104
111
105
- c := & conn { written : make (chan wiremessage.WireMessage , 1 ), readResp : resps }
112
+ c := & internal. ChannelConn { Written : make (chan wiremessage.WireMessage , 1 ), ReadResp : resps }
106
113
107
- err := authenticator .Auth (context .Background (), description.Server {}, c )
114
+ err := authenticator .Auth (context .Background (), description.Server {
115
+ WireVersion : & description.VersionRange {
116
+ Max : 6 ,
117
+ },
118
+ }, c )
108
119
if err != nil {
109
120
t .Fatalf ("expected no error but got \" %s\" " , err )
110
121
}
111
122
112
- if len (c .written ) != 1 {
113
- t .Fatalf ("expected 1 messages to be sent but had %d" , len (c .written ))
123
+ if len (c .Written ) != 1 {
124
+ t .Fatalf ("expected 1 messages to be sent but had %d" , len (c .Written ))
114
125
}
115
126
116
- saslStartRequest := (<- c .written ).(wiremessage.Query )
117
127
payload , _ := base64 .StdEncoding .DecodeString ("AHVzZXIAcGVuY2ls" )
118
- expectedCmd , err := bson .NewDocument (
128
+ expectedCmd := bson .NewDocument (
119
129
bson .EC .Int32 ("saslStart" , 1 ),
120
130
bson .EC .String ("mechanism" , "PLAIN" ),
121
131
bson .EC .Binary ("payload" , payload ),
122
- ).MarshalBSON ()
123
- if err != nil {
124
- t .Fatalf ("couldn't marshal bson: %v" , err )
125
- }
126
-
127
- if ! reflect .DeepEqual (saslStartRequest .Query , bson .Reader (expectedCmd )) {
128
- t .Fatalf ("saslStart command was incorrect. got %v; want %v" , saslStartRequest .Query , bson .Reader (expectedCmd ))
129
- }
132
+ )
133
+ compareResponses (t , <- c .Written , expectedCmd , "$external" )
130
134
}
0 commit comments