Skip to content

Commit 601169e

Browse files
committed
Corrections plus more tests
1 parent ef5faa9 commit 601169e

File tree

2 files changed

+294
-11
lines changed

2 files changed

+294
-11
lines changed

src/MongoDB.Driver/Encryption/CsfleSchemaBuilder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public EncryptedCollectionBuilder<TDocument> PatternProperty(
103103
/// </summary>
104104
public EncryptedCollectionBuilder<TDocument> PatternProperty(
105105
string pattern,
106-
BsonType[] bsonTypes,
106+
IEnumerable<BsonType> bsonTypes,
107107
CsfleEncryptionAlgorithm? algorithm = null,
108108
Guid? keyId = null)
109109
{
@@ -150,7 +150,7 @@ public EncryptedCollectionBuilder<TDocument> Property<TField>(
150150
/// </summary>
151151
public EncryptedCollectionBuilder<TDocument> Property<TField>(
152152
Expression<Func<TDocument, TField>> path,
153-
BsonType[] bsonTypes,
153+
IEnumerable<BsonType> bsonTypes,
154154
CsfleEncryptionAlgorithm? algorithm = null,
155155
Guid? keyId = null)
156156
=> Property(new ExpressionFieldDefinition<TDocument, TField>(path), bsonTypes, algorithm, keyId);
@@ -170,7 +170,7 @@ public EncryptedCollectionBuilder<TDocument> Property(
170170
/// </summary>
171171
public EncryptedCollectionBuilder<TDocument> Property(
172172
FieldDefinition<TDocument> path,
173-
BsonType[] bsonTypes,
173+
IEnumerable<BsonType> bsonTypes,
174174
CsfleEncryptionAlgorithm? algorithm = null,
175175
Guid? keyId = null)
176176
{
@@ -205,7 +205,7 @@ public EncryptedCollectionBuilder<TDocument> Property<TField>(
205205
internal BsonDocument Build() => _schema;
206206

207207
private static BsonDocument CreateEncryptDocument(
208-
BsonType[] bsonTypes,
208+
IEnumerable<BsonType> bsonTypes,
209209
CsfleEncryptionAlgorithm? algorithm = null,
210210
Guid? keyId = null)
211211
{

tests/MongoDB.Driver.Tests/Encryption/CsfleSchemaBuilderTests.cs

Lines changed: 290 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,209 @@ namespace MongoDB.Driver.Tests.Encryption
2525
{
2626
public class CsfleSchemaBuilderTests
2727
{
28-
private static Guid _keyIdExample = Guid.Parse("6f4af470-00d1-401f-ac39-f45902a0c0c8");
2928
private const string _keyIdString = "6f4af470-00d1-401f-ac39-f45902a0c0c8";
29+
private static Guid _keyIdExample = Guid.Parse(_keyIdString); //TODO Check if I should remove this
30+
31+
[Theory]
32+
[InlineData(
33+
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
34+
null,
35+
""" "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random" """)]
36+
[InlineData(
37+
null,
38+
_keyIdString,
39+
""" "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }] """)]
40+
public void EncryptedCollection_Metadata_works_as_expected(CsfleEncryptionAlgorithm? algorithm, string keyString, string expectedContent)
41+
{
42+
Guid? keyId = keyString is null ? null : Guid.Parse(keyString);
43+
var builder = new EncryptedCollectionBuilder<Patient>();
44+
45+
builder.EncryptMetadata(keyId, algorithm);
46+
47+
var expected = $$"""
48+
{
49+
"bsonType": "object",
50+
"encryptMetadata": {
51+
{{expectedContent}}
52+
}
53+
}
54+
""";
55+
56+
AssertOutcomeCollectionBuilder(builder, expected);
57+
}
58+
59+
[Theory]
60+
[InlineData(BsonType.Array,
61+
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
62+
null,
63+
""" "bsonType": "array", "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random" """)]
64+
[InlineData(BsonType.Array,
65+
null,
66+
_keyIdString,
67+
""" "bsonType": "array", "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }] """)]
68+
[InlineData(BsonType.Array,
69+
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
70+
_keyIdString,
71+
""" "bsonType": "array", "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random", "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }] """)]
72+
public void EncryptedCollection_PatternProperty_works_as_expected(BsonType bsonType, CsfleEncryptionAlgorithm? algorithm, string keyString, string expectedContent)
73+
{
74+
Guid? keyId = keyString is null ? null : Guid.Parse(keyString);
75+
var builder = new EncryptedCollectionBuilder<Patient>();
76+
77+
builder.PatternProperty("randomRegex*", bsonType, algorithm, keyId);
78+
79+
var expected = $$"""
80+
{
81+
"bsonType": "object",
82+
"patternProperties": {
83+
"randomRegex*": {
84+
"encrypt": {
85+
{{expectedContent}}
86+
}
87+
}
88+
}
89+
}
90+
""";
91+
92+
AssertOutcomeCollectionBuilder(builder, expected);
93+
}
94+
95+
[Theory]
96+
[InlineData(new[] {BsonType.Array, BsonType.String},
97+
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
98+
null,
99+
""" "bsonType": ["array", "string"], "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random" """)]
100+
[InlineData(new[] {BsonType.Array, BsonType.String},
101+
null,
102+
_keyIdString,
103+
""" "bsonType": ["array", "string"], "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }] """)]
104+
[InlineData(new[] {BsonType.Array, BsonType.String},
105+
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
106+
_keyIdString,
107+
""" "bsonType": ["array", "string"], "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random", "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }] """)]
108+
public void EncryptedCollection_PatternPropertyWithMultipleBsonTypes_works_as_expected(IEnumerable<BsonType> bsonTypes, CsfleEncryptionAlgorithm? algorithm, string keyString, string expectedContent)
109+
{
110+
Guid? keyId = keyString is null ? null : Guid.Parse(keyString);
111+
var builder = new EncryptedCollectionBuilder<Patient>();
112+
113+
builder.PatternProperty("randomRegex*", bsonTypes, algorithm, keyId);
114+
115+
var expected = $$"""
116+
{
117+
"bsonType": "object",
118+
"patternProperties": {
119+
"randomRegex*": {
120+
"encrypt": {
121+
{{expectedContent}}
122+
}
123+
}
124+
}
125+
}
126+
""";
127+
128+
AssertOutcomeCollectionBuilder(builder, expected);
129+
}
130+
131+
[Fact]
132+
public void EncryptedCollection_PatternPropertyNested_works_as_expected()
133+
{
134+
Guid? keyId = Guid.Parse(_keyIdString);
135+
var builder = new EncryptedCollectionBuilder<Patient>();
136+
137+
builder.PatternProperty(p => p.Insurance, innerBuilder =>
138+
{
139+
innerBuilder
140+
.EncryptMetadata(keyId)
141+
.Property("policyNumber", BsonType.Int32,
142+
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic)
143+
.PatternProperty("randomRegex*", BsonType.String,
144+
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random);
145+
});
146+
147+
var expected = """
148+
{
149+
"bsonType": "object",
150+
"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"
160+
}
161+
}
162+
},
163+
"patternProperties": {
164+
"randomRegex*": {
165+
"encrypt": {
166+
"bsonType": "string",
167+
"algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random"
168+
}
169+
}
170+
}
171+
}
172+
}
173+
""";
174+
175+
AssertOutcomeCollectionBuilder(builder, expected);
176+
}
177+
178+
[Fact]
179+
public void EncryptedCollection_PatternPropertyNestedWithString_works_as_expected()
180+
{
181+
Guid? keyId = Guid.Parse(_keyIdString);
182+
var builder = new EncryptedCollectionBuilder<Patient>();
183+
184+
builder.PatternProperty<Insurance>("insurance", innerBuilder =>
185+
{
186+
innerBuilder
187+
.EncryptMetadata(keyId)
188+
.Property("policyNumber", BsonType.Int32,
189+
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic)
190+
.PatternProperty("randomRegex*", BsonType.String,
191+
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random);
192+
});
193+
194+
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+
""";
228+
229+
AssertOutcomeCollectionBuilder(builder, expected);
230+
}
30231

31232
[Theory]
32233
[InlineData(BsonType.Array,
@@ -64,6 +265,78 @@ public void EncryptedCollection_PropertyWithExpression_works_as_expected(BsonTyp
64265
AssertOutcomeCollectionBuilder(builder, expected);
65266
}
66267

268+
[Theory]
269+
[InlineData(new[] {BsonType.Array, BsonType.String},
270+
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
271+
null,
272+
""" "bsonType": ["array", "string"], "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random" """)]
273+
[InlineData(new[] {BsonType.Array, BsonType.String},
274+
null,
275+
_keyIdString,
276+
""" "bsonType": ["array", "string"], "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }] """)]
277+
[InlineData(new[] {BsonType.Array, BsonType.String},
278+
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
279+
_keyIdString,
280+
""" "bsonType": ["array", "string"], "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random", "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }] """)]
281+
public void EncryptedCollection_PropertyWithMultipleBsonTypes_works_as_expected(IEnumerable<BsonType> bsonTypes, CsfleEncryptionAlgorithm? algorithm, string keyString, string expectedContent)
282+
{
283+
Guid? keyId = keyString is null ? null : Guid.Parse(keyString);
284+
var builder = new EncryptedCollectionBuilder<Patient>();
285+
286+
builder.Property(p => p.MedicalRecords, bsonTypes, algorithm, keyId);
287+
288+
var expected = $$"""
289+
{
290+
"bsonType": "object",
291+
"properties": {
292+
"medicalRecords": {
293+
"encrypt": {
294+
{{expectedContent}}
295+
}
296+
}
297+
}
298+
}
299+
""";
300+
301+
AssertOutcomeCollectionBuilder(builder, expected);
302+
}
303+
304+
[Theory]
305+
[InlineData(BsonType.Array,
306+
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
307+
null,
308+
""" "bsonType": "array", "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random" """)]
309+
[InlineData(BsonType.Array,
310+
null,
311+
_keyIdString,
312+
""" "bsonType": "array", "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }] """)]
313+
[InlineData(BsonType.Array,
314+
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
315+
_keyIdString,
316+
""" "bsonType": "array", "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random", "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }] """)]
317+
public void EncryptedCollection_PropertyWithString_works_as_expected(BsonType bsonType, CsfleEncryptionAlgorithm? algorithm, string keyString, string expectedContent)
318+
{
319+
Guid? keyId = keyString is null ? null : Guid.Parse(keyString);
320+
var builder = new EncryptedCollectionBuilder<Patient>();
321+
322+
builder.Property("medicalRecords", bsonType, algorithm, keyId);
323+
324+
var expected = $$"""
325+
{
326+
"bsonType": "object",
327+
"properties": {
328+
"medicalRecords": {
329+
"encrypt": {
330+
{{expectedContent}}
331+
}
332+
}
333+
}
334+
}
335+
""";
336+
337+
AssertOutcomeCollectionBuilder(builder, expected);
338+
}
339+
67340
[Fact]
68341
public void BasicPropertyTest()
69342
{
@@ -223,18 +496,28 @@ private void AssertOutcomeCollectionBuilder<T>(EncryptedCollectionBuilder<T> bui
223496

224497
/** To test:
225498
* - Metadata
226-
* - Property with expression
227-
* - Property with string
228-
* - Property with single bsonType
229-
* - Property with multiple bsonType
499+
* - *Property with expression
500+
* - *Property with string
501+
* - *Property with single bsonType
502+
* - *Property with multiple bsonType
503+
* - *Pattern property with string
504+
* - *Pattern property with multiple bsonType
505+
*
230506
* - Nested property with expression
231507
* - Nested property with string
232-
* - Pattern property with string
233-
* - Pattern property with multiple bsonType
508+
234509
* - Nested pattern property with expression
235510
* - Nested pattern property with string
511+
*
236512
* - Multiple types in schema
237513
* - Property and pattern property together
514+
* - Do it with BsonDocument....?
515+
*
516+
* ERRORS
517+
* - No empty properties and empty pattern properties?
518+
* - No empty schema
519+
* - Wrong string
520+
* - Empty bson type array
238521
*/
239522

240523
internal class Patient

0 commit comments

Comments
 (0)