|
25 | 25 | using MongoDB.Driver.Tests.Specifications.client_side_encryption;
|
26 | 26 | using MongoDB.Libmongocrypt;
|
27 | 27 | using Xunit;
|
| 28 | +using Moq; |
| 29 | +using System.Collections.Generic; |
| 30 | +using System.Threading; |
28 | 31 |
|
29 | 32 | namespace MongoDB.Driver.Tests.Encryption
|
30 | 33 | {
|
@@ -64,6 +67,141 @@ public async Task CreateDataKey_should_correctly_handle_input_arguments()
|
64 | 67 | }
|
65 | 68 | }
|
66 | 69 |
|
| 70 | + [Fact] |
| 71 | + public async Task CreateEncryptedCollection_should_handle_input_arguments() |
| 72 | + { |
| 73 | + const string kmsProvider = "local"; |
| 74 | + const string collectionName = "collName"; |
| 75 | + var createCollectionOptions = new CreateCollectionOptions(); |
| 76 | + var database = Mock.Of<IMongoDatabase>(); |
| 77 | + |
| 78 | + var dataKeyOptions = new DataKeyOptions(); |
| 79 | + |
| 80 | + using (var subject = CreateSubject()) |
| 81 | + { |
| 82 | + ShouldBeArgumentException(Record.Exception(() => subject.CreateEncryptedCollection(database: null, collectionName, createCollectionOptions, kmsProvider, dataKeyOptions)), expectedParamName: "database"); |
| 83 | + ShouldBeArgumentException(await Record.ExceptionAsync(() => subject.CreateEncryptedCollectionAsync(database: null, collectionName, createCollectionOptions, kmsProvider, dataKeyOptions)), expectedParamName: "database"); |
| 84 | + |
| 85 | + ShouldBeArgumentException(Record.Exception(() => subject.CreateEncryptedCollection(database, collectionName: null, createCollectionOptions, kmsProvider, dataKeyOptions)), expectedParamName: "collectionName"); |
| 86 | + ShouldBeArgumentException(await Record.ExceptionAsync(() => subject.CreateEncryptedCollectionAsync(database, collectionName: null, createCollectionOptions, kmsProvider, dataKeyOptions)), expectedParamName: "collectionName"); |
| 87 | + |
| 88 | + ShouldBeArgumentException(Record.Exception(() => subject.CreateEncryptedCollection(database, collectionName: collectionName, createCollectionOptions: null, kmsProvider, dataKeyOptions)), expectedParamName: "createCollectionOptions"); |
| 89 | + ShouldBeArgumentException(await Record.ExceptionAsync(() => subject.CreateEncryptedCollectionAsync(database, collectionName, createCollectionOptions: null, kmsProvider, dataKeyOptions)), expectedParamName: "createCollectionOptions"); |
| 90 | + |
| 91 | + ShouldBeArgumentException(Record.Exception(() => subject.CreateEncryptedCollection(database, collectionName: collectionName, createCollectionOptions, kmsProvider: null, dataKeyOptions)), expectedParamName: "kmsProvider"); |
| 92 | + ShouldBeArgumentException(await Record.ExceptionAsync(() => subject.CreateEncryptedCollectionAsync(database, collectionName, createCollectionOptions, kmsProvider: null, dataKeyOptions)), expectedParamName: "kmsProvider"); |
| 93 | + |
| 94 | + ShouldBeArgumentException(Record.Exception(() => subject.CreateEncryptedCollection(database, collectionName: collectionName, createCollectionOptions, kmsProvider, dataKeyOptions: null)), expectedParamName: "dataKeyOptions"); |
| 95 | + ShouldBeArgumentException(await Record.ExceptionAsync(() => subject.CreateEncryptedCollectionAsync(database, collectionName, createCollectionOptions, kmsProvider, dataKeyOptions: null)), expectedParamName: "dataKeyOptions"); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + [Fact] |
| 100 | + public async Task CreateEncryptedCollection_should_handle_save_generated_key_when_second_key_failed() |
| 101 | + { |
| 102 | + const string kmsProvider = "local"; |
| 103 | + const string collectionName = "collName"; |
| 104 | + const string encryptedFieldsStr = "{ fields : [{ keyId : null }, { keyId : null }] }"; |
| 105 | + var database = Mock.Of<IMongoDatabase>(d => d.DatabaseNamespace == new DatabaseNamespace("db")); |
| 106 | + |
| 107 | + var dataKeyOptions = new DataKeyOptions(); |
| 108 | + |
| 109 | + var mockCollection = new Mock<IMongoCollection<BsonDocument>>(); |
| 110 | + mockCollection |
| 111 | + .SetupSequence(c => c.InsertOne(It.IsAny<BsonDocument>(), It.IsAny<InsertOneOptions>(), It.IsAny<CancellationToken>())) |
| 112 | + .Pass() |
| 113 | + .Throws(new Exception("test")); |
| 114 | + mockCollection |
| 115 | + .SetupSequence(c => c.InsertOneAsync(It.IsAny<BsonDocument>(), It.IsAny<InsertOneOptions>(), It.IsAny<CancellationToken>())) |
| 116 | + .Returns(Task.CompletedTask) |
| 117 | + .Throws(new Exception("test")); |
| 118 | + var mockDatabase = new Mock<IMongoDatabase>(); |
| 119 | + mockDatabase.Setup(c => c.GetCollection<BsonDocument>(It.IsAny<string>(), It.IsAny<MongoCollectionSettings>())).Returns(mockCollection.Object); |
| 120 | + var client = new Mock<IMongoClient>(); |
| 121 | + client.Setup(c => c.GetDatabase(It.IsAny<string>(), It.IsAny<MongoDatabaseSettings>())).Returns(mockDatabase.Object); |
| 122 | + |
| 123 | + using (var subject = CreateSubject(client.Object)) |
| 124 | + { |
| 125 | + var createCollectionOptions = new CreateCollectionOptions() { EncryptedFields = BsonDocument.Parse(encryptedFieldsStr) }; |
| 126 | + var exception = Record.Exception(() => subject.CreateEncryptedCollection(database, collectionName, createCollectionOptions, kmsProvider, dataKeyOptions)); |
| 127 | + AssertResults(exception.InnerException, createCollectionOptions); |
| 128 | + |
| 129 | + createCollectionOptions = new CreateCollectionOptions() { EncryptedFields = BsonDocument.Parse(encryptedFieldsStr) }; |
| 130 | + exception = await Record.ExceptionAsync(() => subject.CreateEncryptedCollectionAsync(database, collectionName, createCollectionOptions, kmsProvider, dataKeyOptions)); |
| 131 | + AssertResults(exception.InnerException, createCollectionOptions); |
| 132 | + } |
| 133 | + |
| 134 | + void AssertResults(Exception ex, CreateCollectionOptions createCollectionOptions) |
| 135 | + { |
| 136 | + ex.Should().BeOfType<Exception>().Which.Message.Should().Be("test"); |
| 137 | + var fields = createCollectionOptions.EncryptedFields["fields"].AsBsonArray; |
| 138 | + fields[0].AsBsonDocument["keyId"].Should().BeOfType<BsonBinaryData>(); // pass |
| 139 | + /* |
| 140 | + - If generating `D` resulted in an error `E`, the entire |
| 141 | + `CreateEncryptedCollection` must now fail with error `E`. Return the |
| 142 | + partially-formed `EF'` with the error so that the caller may know what |
| 143 | + datakeys have already been created by the helper. |
| 144 | + */ |
| 145 | + fields[1].AsBsonDocument["keyId"].Should().BeOfType<BsonNull>(); // throw |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + [Theory] |
| 150 | + [InlineData(null, "There are no encrypted fields defined for the collection.")] |
| 151 | + [InlineData("{}", "{}")] |
| 152 | + [InlineData("{ a : 1 }", "{ a : 1 }")] |
| 153 | + [InlineData("{ fields : { } }", "{ fields: { } }")] |
| 154 | + [InlineData("{ fields : [] }", "{ fields: [] }")] |
| 155 | + [InlineData("{ fields : [{ a : 1 }] }", "{ fields: [{ a : 1 }] }")] |
| 156 | + [InlineData("{ fields : [{ keyId : 1 }] }", "{ fields: [{ keyId : 1 }] }")] |
| 157 | + [InlineData("{ fields : [{ keyId : null }] }", "{ fields: [{ keyId : '#binary_generated#' }] }")] |
| 158 | + [InlineData("{ fields : [{ keyId : null }, { keyId : null }] }", "{ fields: [{ keyId : '#binary_generated#' }, { keyId : '#binary_generated#' }] }")] |
| 159 | + [InlineData("{ fields : [{ keyId : 3 }, { keyId : null }] }", "{ fields: [{ keyId : 3 }, { keyId : '#binary_generated#' }] }")] |
| 160 | + public async Task CreateEncryptedCollection_should_handle_various_encryptedFields(string encryptedFieldsStr, string expectedResult) |
| 161 | + { |
| 162 | + const string kmsProvider = "local"; |
| 163 | + const string collectionName = "collName"; |
| 164 | + var database = Mock.Of<IMongoDatabase>(d => d.DatabaseNamespace == new DatabaseNamespace("db")); |
| 165 | + |
| 166 | + var dataKeyOptions = new DataKeyOptions(); |
| 167 | + |
| 168 | + using (var subject = CreateSubject()) |
| 169 | + { |
| 170 | + if (BsonDocument.TryParse(expectedResult, out var encryptedFields)) |
| 171 | + { |
| 172 | + var createCollectionOptions = new CreateCollectionOptions() { EncryptedFields = encryptedFieldsStr != null ? BsonDocument.Parse(encryptedFieldsStr) : null }; |
| 173 | + subject.CreateEncryptedCollection(database, collectionName: collectionName, createCollectionOptions, kmsProvider: kmsProvider, dataKeyOptions); |
| 174 | + createCollectionOptions.EncryptedFields.WithComparer(new EncryptedFieldsComparer()).Should().Be(encryptedFields.DeepClone()); |
| 175 | + |
| 176 | + createCollectionOptions = new CreateCollectionOptions() { EncryptedFields = encryptedFieldsStr != null ? BsonDocument.Parse(encryptedFieldsStr) : null }; |
| 177 | + await subject.CreateEncryptedCollectionAsync(database, collectionName: collectionName, createCollectionOptions, kmsProvider: kmsProvider, dataKeyOptions); |
| 178 | + createCollectionOptions.EncryptedFields.WithComparer(new EncryptedFieldsComparer()).Should().Be(encryptedFields.DeepClone()); |
| 179 | + } |
| 180 | + else |
| 181 | + { |
| 182 | + var createCollectionOptions = new CreateCollectionOptions() { EncryptedFields = encryptedFieldsStr != null ? BsonDocument.Parse(encryptedFieldsStr) : null }; |
| 183 | + AssertInvalidOperationException(Record.Exception(() => subject.CreateEncryptedCollection(database, collectionName: collectionName, createCollectionOptions, kmsProvider: kmsProvider, dataKeyOptions)), expectedResult); |
| 184 | + |
| 185 | + createCollectionOptions = new CreateCollectionOptions() { EncryptedFields = encryptedFieldsStr != null ? BsonDocument.Parse(encryptedFieldsStr) : null }; |
| 186 | + AssertInvalidOperationException(await Record.ExceptionAsync(() => subject.CreateEncryptedCollectionAsync(database, collectionName: collectionName, createCollectionOptions, kmsProvider: kmsProvider, dataKeyOptions)), expectedResult); |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + void AssertInvalidOperationException(Exception ex, string message) => ex.Should().BeOfType<InvalidOperationException>().Which.Message.Should().Be(message); |
| 191 | + } |
| 192 | + |
| 193 | + private class EncryptedFieldsComparer : IEqualityComparer<BsonDocument> |
| 194 | + { |
| 195 | + public bool Equals(BsonDocument x, BsonDocument y) => BsonValueEquivalencyComparer.Compare(x, y, (a, b) => |
| 196 | + { |
| 197 | + if (a is BsonDocument aDocument && aDocument.TryGetValue("keyId", out var aKeyId) && aKeyId.IsBsonBinaryData && |
| 198 | + b is BsonDocument bDocument && bDocument.TryGetValue("keyId", out var bKeyId) && bKeyId == "#binary_generated#") |
| 199 | + { |
| 200 | + bDocument["keyId"] = aDocument["keyId"]; |
| 201 | + } |
| 202 | + }); |
| 203 | + public int GetHashCode(BsonDocument obj) => obj.GetHashCode(); |
| 204 | + } |
67 | 205 |
|
68 | 206 | [Fact]
|
69 | 207 | public void CryptClient_should_be_initialized()
|
@@ -167,10 +305,10 @@ public async Task RewrapManyDataKey_should_correctly_handle_input_arguments()
|
167 | 305 | }
|
168 | 306 |
|
169 | 307 | // private methods
|
170 |
| - private ClientEncryption CreateSubject() |
| 308 | + private ClientEncryption CreateSubject(IMongoClient client = null) |
171 | 309 | {
|
172 | 310 | var clientEncryptionOptions = new ClientEncryptionOptions(
|
173 |
| - DriverTestConfiguration.Client, |
| 311 | + client ?? DriverTestConfiguration.Client, |
174 | 312 | __keyVaultCollectionNamespace,
|
175 | 313 | kmsProviders: EncryptionTestHelper.GetKmsProviders(filter: "local"));
|
176 | 314 |
|
|
0 commit comments