Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Comment thread
stIncMale marked this conversation as resolved.
Comment thread
stIncMale marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.bson.BsonValue;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
Expand All @@ -43,7 +44,6 @@

import static com.mongodb.ClusterFixture.isStandalone;
import static com.mongodb.ClusterFixture.serverVersionAtLeast;
import static com.mongodb.ClusterFixture.serverVersionLessThan;
import static com.mongodb.client.Fixture.getDefaultDatabase;
import static com.mongodb.client.Fixture.getDefaultDatabaseName;
import static com.mongodb.client.Fixture.getMongoClient;
Expand All @@ -52,11 +52,14 @@
import static com.mongodb.fixture.EncryptionFixture.getKmsProviders;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import static util.JsonPoweredTestHelper.getTestDocument;

/**
* @see <a href="https://github.com/mongodb/specifications/blob/master/source/client-side-encryption/tests/README.md#12-explicit-encryption">
* Client Side Encryption spec explicit encryption prose tests</a>
*/
public abstract class AbstractClientSideEncryptionExplicitEncryptionTest {
private static final BsonString ENCRYPTED_INDEXED_VALUE = new BsonString("encrypted indexed value");
private static final BsonString ENCRYPTED_UNINDEXED_VALUE = new BsonString("encrypted unindexed value");
Expand All @@ -74,6 +77,7 @@ public void setUp() {

MongoNamespace dataKeysNamespace = new MongoNamespace("keyvault.datakeys");
BsonDocument encryptedFields = bsonDocumentFromPath("encryptedFields.json");
BsonDocument encryptedFieldsC10 = bsonDocumentFromPath("encryptedFields-c10.json");
BsonDocument key1Document = bsonDocumentFromPath("keys/key1-document.json");

MongoDatabase explicitEncryptionDatabase = getDefaultDatabase();
Expand All @@ -82,6 +86,11 @@ public void setUp() {
explicitEncryptionDatabase.createCollection("explicit_encryption",
new CreateCollectionOptions().encryptedFields(encryptedFields));

explicitEncryptionDatabase.getCollection("explicit_encryption_c10")
.drop(new DropCollectionOptions().encryptedFields(encryptedFieldsC10));
explicitEncryptionDatabase.createCollection("explicit_encryption_c10",
new CreateCollectionOptions().encryptedFields(encryptedFieldsC10));

MongoCollection<BsonDocument> dataKeysCollection = getMongoClient()
.getDatabase(dataKeysNamespace.getDatabaseName())
.getCollection(dataKeysNamespace.getCollectionName(), BsonDocument.class)
Expand Down Expand Up @@ -121,6 +130,7 @@ public void cleanUp() {
}

@Test
@DisplayName("Case 1: can insert encrypted indexed and find")
public void canInsertEncryptedIndexedAndFind() {
EncryptOptions encryptOptions = new EncryptOptions("Indexed").keyId(key1Id).contentionFactor(0L);
BsonBinary insertPayload = clientEncryption.encrypt(ENCRYPTED_INDEXED_VALUE, encryptOptions);
Expand All @@ -138,33 +148,22 @@ public void canInsertEncryptedIndexedAndFind() {
}

@Test
@DisplayName("Case 2: can insert encrypted indexed and find with non-zero contention")
public void canInsertEncryptedIndexedAndFindWithNonZeroContention() {
// JAVA-6237: this test inserts with contentionFactor 10 but encryptedFields.json configures the
// encryptedIndexed field with contention 0. Servers >= 9.0 reject this mismatch (SERVER-91887);
// skip until the spec prose test is corrected (DRIVERS-3547).
assumeTrue(serverVersionLessThan(9, 0));
EncryptOptions encryptOptions = new EncryptOptions("Indexed").keyId(key1Id).contentionFactor(10L);
MongoCollection<BsonDocument> coll = encryptedClient.getDatabase(getDefaultDatabaseName())
.getCollection("explicit_encryption", BsonDocument.class);
.getCollection("explicit_encryption_c10", BsonDocument.class);

for (int i = 0; i < 10; i++) {
BsonBinary insertPayload = clientEncryption.encrypt(ENCRYPTED_INDEXED_VALUE, encryptOptions);
coll.insertOne(new BsonDocument("encryptedIndexed", insertPayload));
}

encryptOptions = new EncryptOptions("Indexed").keyId(key1Id).queryType("equality").contentionFactor(0L);
// Find with matching contentionFactor returns all 10 documents.
encryptOptions = new EncryptOptions("Indexed").keyId(key1Id).contentionFactor(10L).queryType("equality");
BsonBinary findPayload = clientEncryption.encrypt(ENCRYPTED_INDEXED_VALUE, encryptOptions);

List<BsonDocument> values = coll.find(new BsonDocument("encryptedIndexed", findPayload)).into(new ArrayList<>());
assertTrue(values.size() < 10);
values.forEach(v ->
assertEquals(ENCRYPTED_INDEXED_VALUE, v.get("encryptedIndexed"))
);

encryptOptions = new EncryptOptions("Indexed").keyId(key1Id).contentionFactor(10L).queryType("equality");
BsonBinary findPayload2 = clientEncryption.encrypt(ENCRYPTED_INDEXED_VALUE, encryptOptions);

values = coll.find(new BsonDocument("encryptedIndexed", findPayload2)).into(new ArrayList<>());

assertEquals(10, values.size());
values.forEach(v ->
Expand All @@ -173,6 +172,7 @@ public void canInsertEncryptedIndexedAndFindWithNonZeroContention() {
}

@Test
@DisplayName("Case 3: can insert encrypted unindexed")
public void canInsertEncryptedUnindexed() {
EncryptOptions encryptOptions = new EncryptOptions("Unindexed").keyId(key1Id);
MongoCollection<BsonDocument> coll = encryptedClient.getDatabase(getDefaultDatabaseName())
Expand All @@ -188,6 +188,7 @@ public void canInsertEncryptedUnindexed() {
}

@Test
@DisplayName("Case 4: can roundtrip encrypted indexed")
public void canRoundtripEncryptedIndexed() {
EncryptOptions encryptOptions = new EncryptOptions("Indexed").keyId(key1Id).contentionFactor(0L);

Expand All @@ -198,6 +199,7 @@ public void canRoundtripEncryptedIndexed() {
}

@Test
@DisplayName("Case 5: can roundtrip encrypted unindexed")
public void canRoundtripEncryptedUnindexed() {
EncryptOptions encryptOptions = new EncryptOptions("Unindexed").keyId(key1Id);

Expand Down