16
16
17
17
package com.mongodb.client.internal
18
18
19
+
19
20
import com.mongodb.ReadPreference
20
21
import com.mongodb.ServerAddress
21
22
import com.mongodb.connection.ClusterId
@@ -58,9 +59,6 @@ class CryptConnectionSpecification extends Specification {
58
59
def crypt = Mock (Crypt )
59
60
def cryptConnection = new CryptConnection (wrappedConnection, crypt)
60
61
def codec = new DocumentCodec ()
61
- def maxBatchCount = 4
62
- def maxDocumentSize = 1024 * 16_000
63
- def maxMessageSize = 1024 * 16_000
64
62
def encryptedCommand = toRaw(new BsonDocument (' find' , new BsonString (' test' ))
65
63
.append(' ssid' , new BsonBinary (6 as byte , new byte [10 ])))
66
64
@@ -85,7 +83,7 @@ class CryptConnectionSpecification extends Specification {
85
83
then :
86
84
_ * wrappedConnection. getDescription() >> {
87
85
new ConnectionDescription (new ConnectionId (new ServerId (new ClusterId (), new ServerAddress ())), 8 , STANDALONE ,
88
- maxBatchCount, maxDocumentSize, maxMessageSize , [])
86
+ 1000 , 1024 * 16_000, 1024 * 48_000 , [])
89
87
}
90
88
1 * crypt. encrypt(' db' , toRaw(new BsonDocument (' find' , new BsonString (' test' ))
91
89
.append(' filter' , new BsonDocument (' ssid' , new BsonString (' 555-55-5555' ))))) >> {
@@ -101,16 +99,13 @@ class CryptConnectionSpecification extends Specification {
101
99
response == rawToDocument(decryptedResponse)
102
100
}
103
101
104
- def ' should encrypt and decrypt a command with a splittable payload ' () {
102
+ def ' should split at 2 MiB ' () {
105
103
given :
106
104
def wrappedConnection = Mock (Connection )
107
105
def crypt = Mock (Crypt )
108
106
def cryptConnection = new CryptConnection (wrappedConnection, crypt)
109
107
def codec = new DocumentCodec ()
110
- def maxBatchCount = 4
111
- def maxDocumentSize = 1024 * 16_000
112
- def maxMessageSize = 1024 * 16_000
113
- def bytes = new byte [1024 * 10_000]
108
+ def bytes = new byte [2097152 - 85 ]
114
109
def payload = new SplittablePayload (INSERT , [
115
110
new BsonDocumentWrapper (new Document (' _id' , 1 ). append(' ssid' , ' 555-55-5555' ). append(' b' , bytes), codec),
116
111
new BsonDocumentWrapper (new Document (' _id' , 2 ). append(' ssid' , ' 666-66-6666' ). append(' b' , bytes), codec)
@@ -124,6 +119,7 @@ class CryptConnectionSpecification extends Specification {
124
119
125
120
def encryptedResponse = toRaw(new BsonDocument (' ok' , new BsonInt32 (1 )))
126
121
def decryptedResponse = encryptedResponse
122
+
127
123
when :
128
124
def response = cryptConnection. command(' db' ,
129
125
new BsonDocumentWrapper (new Document (' insert' , ' test' ), codec),
@@ -135,7 +131,7 @@ class CryptConnectionSpecification extends Specification {
135
131
then :
136
132
_ * wrappedConnection. getDescription() >> {
137
133
new ConnectionDescription (new ConnectionId (new ServerId (new ClusterId (), new ServerAddress ())), 8 , STANDALONE ,
138
- maxBatchCount, maxDocumentSize, maxMessageSize , [])
134
+ 1000 , 1024 * 16_000, 1024 * 48_000 , [])
139
135
}
140
136
1 * crypt. encrypt(' db' ,
141
137
toRaw(new BsonDocument (' insert' , new BsonString (' test' )). append(' documents' ,
@@ -157,6 +153,87 @@ class CryptConnectionSpecification extends Specification {
157
153
payload. getPosition() == 1
158
154
}
159
155
156
+ def ' should split at maxBatchCount' () {
157
+ given :
158
+ def wrappedConnection = Mock (Connection )
159
+ def crypt = Mock (Crypt )
160
+ def cryptConnection = new CryptConnection (wrappedConnection, crypt)
161
+ def codec = new DocumentCodec ()
162
+ def maxBatchCount = 2
163
+ def payload = new SplittablePayload (INSERT , [
164
+ new BsonDocumentWrapper (new Document (' _id' , 1 ), codec),
165
+ new BsonDocumentWrapper (new Document (' _id' , 2 ), codec),
166
+ new BsonDocumentWrapper (new Document (' _id' , 3 ), codec)
167
+ ])
168
+ def encryptedCommand = toRaw(new BsonDocument (' insert' , new BsonString (' test' )). append(' documents' , new BsonArray (
169
+ [
170
+ new BsonDocument (' _id' , new BsonInt32 (1 )),
171
+ new BsonDocument (' _id' , new BsonInt32 (2 )),
172
+
173
+ ])))
174
+
175
+ def encryptedResponse = toRaw(new BsonDocument (' ok' , new BsonInt32 (1 )))
176
+ def decryptedResponse = encryptedResponse
177
+
178
+ when :
179
+ def response = cryptConnection. command(' db' ,
180
+ new BsonDocumentWrapper (new Document (' insert' , ' test' ), codec),
181
+ new NoOpFieldNameValidator (), ReadPreference . primary(), new BsonDocumentCodec (),
182
+ NoOpSessionContext . INSTANCE , true ,
183
+ payload,
184
+ new NoOpFieldNameValidator ())
185
+
186
+ then :
187
+ _ * wrappedConnection. getDescription() >> {
188
+ new ConnectionDescription (new ConnectionId (new ServerId (new ClusterId (), new ServerAddress ())), 8 , STANDALONE ,
189
+ maxBatchCount, 1024 * 16_000, 1024 * 48_000, [])
190
+ }
191
+ 1 * crypt. encrypt(' db' ,
192
+ toRaw(new BsonDocument (' insert' , new BsonString (' test' )). append(' documents' ,
193
+ new BsonArray ([
194
+ new BsonDocument (' _id' , new BsonInt32 (1 )),
195
+ new BsonDocument (' _id' , new BsonInt32 (2 ))
196
+ ])))) >> {
197
+ encryptedCommand
198
+ }
199
+ 1 * wrappedConnection. command(' db' , encryptedCommand, _ as NoOpFieldNameValidator , ReadPreference . primary(),
200
+ _ as RawBsonDocumentCodec , NoOpSessionContext . INSTANCE , true , null , null ) >> {
201
+ encryptedResponse
202
+ }
203
+ 1 * crypt. decrypt(encryptedResponse) >> {
204
+ decryptedResponse
205
+ }
206
+ response == rawToBsonDocument(decryptedResponse)
207
+ payload. getPosition() == 2
208
+ }
209
+
210
+ def ' should throw if command document is large than 2 MiB' () {
211
+ given :
212
+ def wrappedConnection = Mock (Connection )
213
+ def crypt = Mock (Crypt )
214
+ def cryptConnection = new CryptConnection (wrappedConnection, crypt)
215
+ def codec = new DocumentCodec ()
216
+ def bytes = new byte [2097152 - 84 ]
217
+ def payload = new SplittablePayload (INSERT , [
218
+ new BsonDocumentWrapper (new Document (' _id' , 1 ). append(' ssid' , ' 555-55-5555' ). append(' b' , bytes), codec),
219
+ ])
220
+
221
+ when :
222
+ cryptConnection. command(' db' ,
223
+ new BsonDocumentWrapper (new Document (' insert' , ' test' ), codec),
224
+ new NoOpFieldNameValidator (), ReadPreference . primary(), new BsonDocumentCodec (),
225
+ NoOpSessionContext . INSTANCE , true ,
226
+ payload,
227
+ new NoOpFieldNameValidator ())
228
+
229
+ then :
230
+ _ * wrappedConnection. getDescription() >> {
231
+ new ConnectionDescription (new ConnectionId (new ServerId (new ClusterId (), new ServerAddress ())), 8 , STANDALONE ,
232
+ 1000 , 1024 * 16_000, 1024 * 48_000, [])
233
+ }
234
+ thrown(BsonMaximumSizeExceededException )
235
+ }
236
+
160
237
RawBsonDocument toRaw (BsonDocument document ) {
161
238
def buffer = new BasicOutputBuffer ()
162
239
def writer = new BsonBinaryWriter (buffer)
0 commit comments