@@ -26,7 +26,80 @@ namespace MongoDB.Driver.Tests.Encryption
26
26
public class CsfleSchemaBuilderTests
27
27
{
28
28
private const string _keyIdString = "6f4af470-00d1-401f-ac39-f45902a0c0c8" ;
29
- private static Guid _keyIdExample = Guid . Parse ( _keyIdString ) ; //TODO Check if I should remove this
29
+ private static Guid _keyId = Guid . Parse ( _keyIdString ) ;
30
+
31
+ [ Fact ]
32
+ public void BasicPropertyTest ( )
33
+ {
34
+ const string collectionName = "medicalRecords.patients" ;
35
+
36
+ var builder = CsfleSchemaBuilder . Create ( schemaBuilder =>
37
+ {
38
+ schemaBuilder . Encrypt < Patient > ( collectionName , builder =>
39
+ {
40
+ builder
41
+ . EncryptMetadata ( keyId : _keyId )
42
+ . Property ( p => p . MedicalRecords , BsonType . Array ,
43
+ CsfleEncryptionAlgorithm . AEAD_AES_256_CBC_HMAC_SHA_512_Random )
44
+ . Property ( "bloodType" , BsonType . String ,
45
+ algorithm : CsfleEncryptionAlgorithm . AEAD_AES_256_CBC_HMAC_SHA_512_Random )
46
+ . Property ( p => p . Ssn , BsonType . Int32 ,
47
+ CsfleEncryptionAlgorithm . AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic )
48
+ . Property ( p => p . Insurance , innerBuilder =>
49
+ {
50
+ innerBuilder
51
+ . Property ( i => i . PolicyNumber , BsonType . Int32 ,
52
+ CsfleEncryptionAlgorithm . AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic ) ;
53
+ } ) ;
54
+ } ) ;
55
+ } ) ;
56
+
57
+ var expected = new Dictionary < string , string >
58
+ {
59
+ [ collectionName ] = """
60
+ {
61
+ "bsonType": "object",
62
+ "encryptMetadata": {
63
+ "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }]
64
+ },
65
+ "properties": {
66
+ "insurance": {
67
+ "bsonType": "object",
68
+ "properties": {
69
+ "policyNumber": {
70
+ "encrypt": {
71
+ "bsonType": "int",
72
+ "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic"
73
+ }
74
+ }
75
+ }
76
+ },
77
+ "medicalRecords": {
78
+ "encrypt": {
79
+ "bsonType": "array",
80
+ "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random"
81
+ }
82
+ },
83
+ "bloodType": {
84
+ "encrypt": {
85
+ "bsonType": "string",
86
+ "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random"
87
+ }
88
+ },
89
+ "ssn": {
90
+ "encrypt": {
91
+ "bsonType": "int",
92
+ "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic"
93
+ }
94
+ }
95
+ },
96
+ }
97
+ """
98
+ } ;
99
+
100
+ AssertOutcomeBuilder ( builder , expected ) ;
101
+ }
102
+
30
103
31
104
[ Theory ]
32
105
[ InlineData (
@@ -148,23 +221,25 @@ public void EncryptedCollection_PatternPropertyNested_works_as_expected()
148
221
{
149
222
"bsonType": "object",
150
223
"patternProperties": {
151
- "bsonType": "object",
152
- "encryptMetadata": {
153
- "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }]
154
- },
155
- "properties": {
156
- "policyNumber": {
157
- "encrypt": {
158
- "bsonType": "int",
159
- "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic"
224
+ "insurance": {
225
+ "bsonType": "object",
226
+ "encryptMetadata": {
227
+ "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }]
228
+ },
229
+ "properties": {
230
+ "policyNumber": {
231
+ "encrypt": {
232
+ "bsonType": "int",
233
+ "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic"
234
+ }
160
235
}
161
- }
162
- },
163
- "patternProperties ": {
164
- "randomRegex* ": {
165
- "encrypt ": {
166
- "bsonType ": "string",
167
- "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random"
236
+ },
237
+ "patternProperties": {
238
+ "randomRegex* ": {
239
+ "encrypt ": {
240
+ "bsonType ": "string",
241
+ "algorithm ": "AEAD_AES_256_CBC_HMAC_SHA_512-Random"
242
+ }
168
243
}
169
244
}
170
245
}
@@ -192,39 +267,34 @@ public void EncryptedCollection_PatternPropertyNestedWithString_works_as_expecte
192
267
} ) ;
193
268
194
269
var expected = """
195
- {
196
- "bsonType": "object",
197
- "patternProperties": {
198
- "bsonType": "object",
199
- "encryptMetadata": {
200
- "keyId": [
201
- {
202
- "$binary": {
203
- "base64": "b0r0cADRQB+sOfRZAqDAyA==",
204
- "subType": "04"
205
- }
206
- }
207
- ]
208
- },
209
- "properties": {
210
- "policyNumber": {
211
- "encrypt": {
212
- "bsonType": "int",
213
- "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic"
214
- }
215
- }
216
- },
217
- "patternProperties": {
218
- "randomRegex*": {
219
- "encrypt": {
220
- "bsonType": "string",
221
- "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random"
222
- }
223
- }
224
- }
225
- }
226
- }
227
- """ ;
270
+ {
271
+ "bsonType": "object",
272
+ "patternProperties": {
273
+ "insurance": {
274
+ "bsonType": "object",
275
+ "encryptMetadata": {
276
+ "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }]
277
+ },
278
+ "properties": {
279
+ "policyNumber": {
280
+ "encrypt": {
281
+ "bsonType": "int",
282
+ "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic"
283
+ }
284
+ }
285
+ },
286
+ "patternProperties": {
287
+ "randomRegex*": {
288
+ "encrypt": {
289
+ "bsonType": "string",
290
+ "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random"
291
+ }
292
+ }
293
+ }
294
+ }
295
+ }
296
+ }
297
+ """ ;
228
298
229
299
AssertOutcomeCollectionBuilder ( builder , expected ) ;
230
300
}
@@ -337,6 +407,105 @@ public void EncryptedCollection_PropertyWithString_works_as_expected(BsonType bs
337
407
AssertOutcomeCollectionBuilder ( builder , expected ) ;
338
408
}
339
409
410
+ [ Fact ]
411
+ public void EncryptedCollection_PropertyNested_works_as_expected ( )
412
+ {
413
+ Guid ? keyId = Guid . Parse ( _keyIdString ) ;
414
+ var builder = new EncryptedCollectionBuilder < Patient > ( ) ;
415
+
416
+ builder . Property ( p => p . Insurance , innerBuilder =>
417
+ {
418
+ innerBuilder
419
+ . EncryptMetadata ( keyId )
420
+ . Property ( "policyNumber" , BsonType . Int32 ,
421
+ CsfleEncryptionAlgorithm . AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic )
422
+ . PatternProperty ( "randomRegex*" , BsonType . String ,
423
+ CsfleEncryptionAlgorithm . AEAD_AES_256_CBC_HMAC_SHA_512_Random ) ;
424
+ } ) ;
425
+
426
+ var expected = """
427
+ {
428
+ "bsonType": "object",
429
+ "properties": {
430
+ "insurance": {
431
+ "bsonType": "object",
432
+ "encryptMetadata": {
433
+ "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }]
434
+ },
435
+ "properties": {
436
+ "policyNumber": {
437
+ "encrypt": {
438
+ "bsonType": "int",
439
+ "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic"
440
+ }
441
+ }
442
+ },
443
+ "patternProperties": {
444
+ "randomRegex*": {
445
+ "encrypt": {
446
+ "bsonType": "string",
447
+ "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random"
448
+ }
449
+ }
450
+ }
451
+ }
452
+ }
453
+ }
454
+ """ ;
455
+
456
+ AssertOutcomeCollectionBuilder ( builder , expected ) ;
457
+ }
458
+
459
+ [ Fact ]
460
+ public void EncryptedCollection_PropertyNestedWithString_works_as_expected ( )
461
+ {
462
+ Guid ? keyId = Guid . Parse ( _keyIdString ) ;
463
+ var builder = new EncryptedCollectionBuilder < Patient > ( ) ;
464
+
465
+ builder . Property < Insurance > ( "insurance" , innerBuilder =>
466
+ {
467
+ innerBuilder
468
+ . EncryptMetadata ( keyId )
469
+ . Property ( "policyNumber" , BsonType . Int32 ,
470
+ CsfleEncryptionAlgorithm . AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic )
471
+ . PatternProperty ( "randomRegex*" , BsonType . String ,
472
+ CsfleEncryptionAlgorithm . AEAD_AES_256_CBC_HMAC_SHA_512_Random ) ;
473
+ } ) ;
474
+
475
+ var expected = """
476
+ {
477
+ "bsonType": "object",
478
+ "properties": {
479
+ "insurance": {
480
+ "bsonType": "object",
481
+ "encryptMetadata": {
482
+ "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }]
483
+ },
484
+ "properties": {
485
+ "policyNumber": {
486
+ "encrypt": {
487
+ "bsonType": "int",
488
+ "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic"
489
+ }
490
+ }
491
+ },
492
+ "patternProperties": {
493
+ "randomRegex*": {
494
+ "encrypt": {
495
+ "bsonType": "string",
496
+ "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random"
497
+ }
498
+ }
499
+ }
500
+ }
501
+ }
502
+ }
503
+ """ ;
504
+
505
+ AssertOutcomeCollectionBuilder ( builder , expected ) ;
506
+ }
507
+
508
+
340
509
[ Fact ]
341
510
public void BasicPropertyTest ( )
342
511
{
@@ -347,7 +516,7 @@ public void BasicPropertyTest()
347
516
schemaBuilder . Encrypt < Patient > ( collectionName , builder =>
348
517
{
349
518
builder
350
- . EncryptMetadata ( keyId : _keyIdExample )
519
+ . EncryptMetadata ( keyId : _keyId )
351
520
. Property ( p => p . MedicalRecords , BsonType . Array ,
352
521
CsfleEncryptionAlgorithm . AEAD_AES_256_CBC_HMAC_SHA_512_Random )
353
522
. Property ( "bloodType" , BsonType . String ,
@@ -502,12 +671,10 @@ private void AssertOutcomeCollectionBuilder<T>(EncryptedCollectionBuilder<T> bui
502
671
* - *Property with multiple bsonType
503
672
* - *Pattern property with string
504
673
* - *Pattern property with multiple bsonType
505
- *
506
- * - Nested property with expression
507
- * - Nested property with string
508
-
509
- * - Nested pattern property with expression
510
- * - Nested pattern property with string
674
+ * - *Nested property with expression
675
+ * - *Nested property with string
676
+ * - *Nested pattern property with expression
677
+ * - *Nested pattern property with string
511
678
*
512
679
* - Multiple types in schema
513
680
* - Property and pattern property together
0 commit comments