@@ -3233,24 +3233,27 @@ func TestClientSideEncryptionProse(t *testing.T) {
32333233 return encryptedClient , clientEncryption
32343234 }
32353235
3236+ foo := bson.RawValue {Type : bson .TypeString , Value : bsoncore .AppendString (nil , "foo" )}
3237+ bar := bson.RawValue {Type : bson .TypeString , Value : bsoncore .AppendString (nil , "bar" )}
3238+ baz := bson.RawValue {Type : bson .TypeString , Value : bsoncore .AppendString (nil , "baz" )}
3239+
32363240 mt .Run ("Case 1: can find a document by prefix" , func (mt * mtest.T ) {
32373241 encryptedClient , clientEncryption := testSetup ()
32383242 defer clientEncryption .Close (context .Background ())
32393243 defer encryptedClient .Disconnect (context .Background ())
32403244
3241- foo := bson.RawValue {Type : bson .TypeString , Value : bsoncore .AppendString (nil , "foo" )}
32423245 eo := options .Encrypt ().
32433246 SetKeyID (key1ID ).
32443247 SetAlgorithm ("TextPreview" ).
32453248 SetQueryType ("prefixPreview" ).
32463249 SetContentionFactor (0 ).
32473250 SetTextOptions (options .Text ().
3251+ SetCaseSensitive (true ).
3252+ SetDiacriticSensitive (true ).
32483253 SetPrefix (options.PrefixOptions {
32493254 StrMaxQueryLength : 10 ,
32503255 StrMinQueryLength : 2 ,
3251- }).
3252- SetCaseSensitive (true ).
3253- SetDiacriticSensitive (true ))
3256+ }))
32543257 payload , err := clientEncryption .Encrypt (context .Background (), foo , eo )
32553258 require .NoError (mt , err , "error in Encrypt: %v" , err )
32563259 coll := encryptedClient .Database ("db" ).Collection ("prefix-suffix" )
@@ -3277,19 +3280,18 @@ func TestClientSideEncryptionProse(t *testing.T) {
32773280 defer clientEncryption .Close (context .Background ())
32783281 defer encryptedClient .Disconnect (context .Background ())
32793282
3280- baz := bson.RawValue {Type : bson .TypeString , Value : bsoncore .AppendString (nil , "baz" )}
32813283 eo := options .Encrypt ().
32823284 SetKeyID (key1ID ).
32833285 SetAlgorithm ("TextPreview" ).
32843286 SetQueryType ("suffixPreview" ).
32853287 SetContentionFactor (0 ).
32863288 SetTextOptions (options .Text ().
3289+ SetCaseSensitive (true ).
3290+ SetDiacriticSensitive (true ).
32873291 SetSuffix (options.SuffixOptions {
32883292 StrMaxQueryLength : 10 ,
32893293 StrMinQueryLength : 2 ,
3290- }).
3291- SetCaseSensitive (true ).
3292- SetDiacriticSensitive (true ))
3294+ }))
32933295 payload , err := clientEncryption .Encrypt (context .Background (), baz , eo )
32943296 require .NoError (mt , err , "error in Encrypt: %v" , err )
32953297 coll := encryptedClient .Database ("db" ).Collection ("prefix-suffix" )
@@ -3310,6 +3312,156 @@ func TestClientSideEncryptionProse(t *testing.T) {
33103312 require .Equal (mt , 0 , got .Id )
33113313 require .Equal (mt , "foobarbaz" , got .EncryptedText )
33123314 })
3315+ mt .Run ("Case 3: assert no document found by prefix" , func (mt * mtest.T ) {
3316+ encryptedClient , clientEncryption := testSetup ()
3317+ defer clientEncryption .Close (context .Background ())
3318+ defer encryptedClient .Disconnect (context .Background ())
3319+
3320+ eo := options .Encrypt ().
3321+ SetKeyID (key1ID ).
3322+ SetAlgorithm ("TextPreview" ).
3323+ SetQueryType ("prefixPreview" ).
3324+ SetContentionFactor (0 ).
3325+ SetTextOptions (options .Text ().
3326+ SetCaseSensitive (true ).
3327+ SetDiacriticSensitive (true ).
3328+ SetPrefix (options.PrefixOptions {
3329+ StrMaxQueryLength : 10 ,
3330+ StrMinQueryLength : 2 ,
3331+ }))
3332+ payload , err := clientEncryption .Encrypt (context .Background (), baz , eo )
3333+ require .NoError (mt , err , "error in Encrypt: %v" , err )
3334+ coll := encryptedClient .Database ("db" ).Collection ("prefix-suffix" )
3335+ _ , err = coll .FindOne (context .Background (), bson.D {
3336+ {"$expr" , bson.D {
3337+ {"$encStrStartsWith" , bson.D {
3338+ {"input" , "$encryptedText" },
3339+ {"prefix" , payload },
3340+ }},
3341+ }},
3342+ }).Raw ()
3343+ require .Error (mt , err , mongo .ErrNoDocuments )
3344+ })
3345+ mt .Run ("Case 4: assert no document found by suffix" , func (mt * mtest.T ) {
3346+ encryptedClient , clientEncryption := testSetup ()
3347+ defer clientEncryption .Close (context .Background ())
3348+ defer encryptedClient .Disconnect (context .Background ())
3349+
3350+ eo := options .Encrypt ().
3351+ SetKeyID (key1ID ).
3352+ SetAlgorithm ("TextPreview" ).
3353+ SetQueryType ("suffixPreview" ).
3354+ SetContentionFactor (0 ).
3355+ SetTextOptions (options .Text ().
3356+ SetCaseSensitive (true ).
3357+ SetDiacriticSensitive (true ).
3358+ SetSuffix (options.SuffixOptions {
3359+ StrMaxQueryLength : 10 ,
3360+ StrMinQueryLength : 2 ,
3361+ }))
3362+ payload , err := clientEncryption .Encrypt (context .Background (), foo , eo )
3363+ require .NoError (mt , err , "error in Encrypt: %v" , err )
3364+ coll := encryptedClient .Database ("db" ).Collection ("prefix-suffix" )
3365+ _ , err = coll .FindOne (context .Background (), bson.D {
3366+ {"$expr" , bson.D {
3367+ {"$encStrEndsWith" , bson.D {
3368+ {"input" , "$encryptedText" },
3369+ {"suffix" , payload },
3370+ }},
3371+ }},
3372+ }).Raw ()
3373+ require .Error (mt , err , mongo .ErrNoDocuments )
3374+ })
3375+ mt .Run ("Case 5: can find a document by substring" , func (mt * mtest.T ) {
3376+ encryptedClient , clientEncryption := testSetup ()
3377+ defer clientEncryption .Close (context .Background ())
3378+ defer encryptedClient .Disconnect (context .Background ())
3379+
3380+ eo := options .Encrypt ().
3381+ SetKeyID (key1ID ).
3382+ SetAlgorithm ("TextPreview" ).
3383+ SetQueryType ("substringPreview" ).
3384+ SetContentionFactor (0 ).
3385+ SetTextOptions (options .Text ().
3386+ SetCaseSensitive (true ).
3387+ SetDiacriticSensitive (true ).
3388+ SetSubstring (options.SubstringOptions {
3389+ StrMaxLength : 10 ,
3390+ StrMaxQueryLength : 10 ,
3391+ StrMinQueryLength : 2 ,
3392+ }))
3393+ payload , err := clientEncryption .Encrypt (context .Background (), bar , eo )
3394+ require .NoError (mt , err , "error in Encrypt: %v" , err )
3395+ coll := encryptedClient .Database ("db" ).Collection ("substring" )
3396+ res := coll .FindOne (context .Background (), bson.D {
3397+ {"$expr" , bson.D {
3398+ {"$encStrContains" , bson.D {
3399+ {"input" , "$encryptedText" },
3400+ {"substring" , payload },
3401+ }},
3402+ }},
3403+ })
3404+ require .NoError (mt , err , "error in FindOne: %v" , err )
3405+ var got struct {
3406+ Id int `bson:"_id"`
3407+ EncryptedText string `bson:"encryptedText"`
3408+ }
3409+ err = res .Decode (& got )
3410+ require .NoError (mt , err , "error decoding result: %v" , err )
3411+ require .Equal (mt , 0 , got .Id )
3412+ require .Equal (mt , "foobarbaz" , got .EncryptedText )
3413+ })
3414+ mt .Run ("Case 6: assert no document found by substring" , func (mt * mtest.T ) {
3415+ encryptedClient , clientEncryption := testSetup ()
3416+ defer clientEncryption .Close (context .Background ())
3417+ defer encryptedClient .Disconnect (context .Background ())
3418+
3419+ qux := bson.RawValue {Type : bson .TypeString , Value : bsoncore .AppendString (nil , "qux" )}
3420+ eo := options .Encrypt ().
3421+ SetKeyID (key1ID ).
3422+ SetAlgorithm ("TextPreview" ).
3423+ SetQueryType ("substringPreview" ).
3424+ SetContentionFactor (0 ).
3425+ SetTextOptions (options .Text ().
3426+ SetCaseSensitive (true ).
3427+ SetDiacriticSensitive (true ).
3428+ SetSubstring (options.SubstringOptions {
3429+ StrMaxLength : 10 ,
3430+ StrMaxQueryLength : 10 ,
3431+ StrMinQueryLength : 2 ,
3432+ }))
3433+ payload , err := clientEncryption .Encrypt (context .Background (), qux , eo )
3434+ require .NoError (mt , err , "error in Encrypt: %v" , err )
3435+ coll := encryptedClient .Database ("db" ).Collection ("substring" )
3436+ _ , err = coll .FindOne (context .Background (), bson.D {
3437+ {"$expr" , bson.D {
3438+ {"$encStrContains" , bson.D {
3439+ {"input" , "$encryptedText" },
3440+ {"suffix" , payload },
3441+ }},
3442+ }},
3443+ }).Raw ()
3444+ require .Error (mt , err , mongo .ErrNoDocuments )
3445+ })
3446+ mt .Run ("Case 7: assert contentionFactor is required" , func (mt * mtest.T ) {
3447+ encryptedClient , clientEncryption := testSetup ()
3448+ defer clientEncryption .Close (context .Background ())
3449+ defer encryptedClient .Disconnect (context .Background ())
3450+
3451+ eo := options .Encrypt ().
3452+ SetKeyID (key1ID ).
3453+ SetAlgorithm ("TextPreview" ).
3454+ SetQueryType ("prefixPreview" ).
3455+ SetTextOptions (options .Text ().
3456+ SetCaseSensitive (true ).
3457+ SetDiacriticSensitive (true ).
3458+ SetPrefix (options.PrefixOptions {
3459+ StrMaxQueryLength : 10 ,
3460+ StrMinQueryLength : 2 ,
3461+ }))
3462+ _ , err := clientEncryption .Encrypt (context .Background (), baz , eo )
3463+ require .ErrorContains (mt , err , "contention factor is required for textPreview algorithm" )
3464+ })
33133465 })
33143466}
33153467
0 commit comments