3535import org .bson .BsonValue ;
3636import org .junit .jupiter .api .AfterEach ;
3737import org .junit .jupiter .api .BeforeEach ;
38+ import org .junit .jupiter .api .DisplayName ;
3839import org .junit .jupiter .api .Test ;
3940
4041import java .util .ArrayList ;
4344
4445import static com .mongodb .ClusterFixture .isStandalone ;
4546import static com .mongodb .ClusterFixture .serverVersionAtLeast ;
46- import static com .mongodb .ClusterFixture .serverVersionLessThan ;
4747import static com .mongodb .client .Fixture .getDefaultDatabase ;
4848import static com .mongodb .client .Fixture .getDefaultDatabaseName ;
4949import static com .mongodb .client .Fixture .getMongoClient ;
5252import static com .mongodb .fixture .EncryptionFixture .getKmsProviders ;
5353import static org .junit .jupiter .api .Assertions .assertEquals ;
5454import static org .junit .jupiter .api .Assertions .assertNotNull ;
55- import static org .junit .jupiter .api .Assertions .assertTrue ;
5655import static org .junit .jupiter .api .Assumptions .assumeFalse ;
5756import static org .junit .jupiter .api .Assumptions .assumeTrue ;
5857import static util .JsonPoweredTestHelper .getTestDocument ;
5958
59+ /**
60+ * @see <a href="https://github.com/mongodb/specifications/blob/master/source/client-side-encryption/tests/README.md#12-explicit-encryption">
61+ * Client Side Encryption spec explicit encryption prose tests</a>
62+ */
6063public abstract class AbstractClientSideEncryptionExplicitEncryptionTest {
6164 private static final BsonString ENCRYPTED_INDEXED_VALUE = new BsonString ("encrypted indexed value" );
6265 private static final BsonString ENCRYPTED_UNINDEXED_VALUE = new BsonString ("encrypted unindexed value" );
@@ -74,6 +77,7 @@ public void setUp() {
7477
7578 MongoNamespace dataKeysNamespace = new MongoNamespace ("keyvault.datakeys" );
7679 BsonDocument encryptedFields = bsonDocumentFromPath ("encryptedFields.json" );
80+ BsonDocument encryptedFieldsC10 = bsonDocumentFromPath ("encryptedFields-c10.json" );
7781 BsonDocument key1Document = bsonDocumentFromPath ("keys/key1-document.json" );
7882
7983 MongoDatabase explicitEncryptionDatabase = getDefaultDatabase ();
@@ -82,6 +86,11 @@ public void setUp() {
8286 explicitEncryptionDatabase .createCollection ("explicit_encryption" ,
8387 new CreateCollectionOptions ().encryptedFields (encryptedFields ));
8488
89+ explicitEncryptionDatabase .getCollection ("explicit_encryption_c10" )
90+ .drop (new DropCollectionOptions ().encryptedFields (encryptedFieldsC10 ));
91+ explicitEncryptionDatabase .createCollection ("explicit_encryption_c10" ,
92+ new CreateCollectionOptions ().encryptedFields (encryptedFieldsC10 ));
93+
8594 MongoCollection <BsonDocument > dataKeysCollection = getMongoClient ()
8695 .getDatabase (dataKeysNamespace .getDatabaseName ())
8796 .getCollection (dataKeysNamespace .getCollectionName (), BsonDocument .class )
@@ -121,6 +130,7 @@ public void cleanUp() {
121130 }
122131
123132 @ Test
133+ @ DisplayName ("Case 1: can insert encrypted indexed and find" )
124134 public void canInsertEncryptedIndexedAndFind () {
125135 EncryptOptions encryptOptions = new EncryptOptions ("Indexed" ).keyId (key1Id ).contentionFactor (0L );
126136 BsonBinary insertPayload = clientEncryption .encrypt (ENCRYPTED_INDEXED_VALUE , encryptOptions );
@@ -138,33 +148,22 @@ public void canInsertEncryptedIndexedAndFind() {
138148 }
139149
140150 @ Test
151+ @ DisplayName ("Case 2: can insert encrypted indexed and find with non-zero contention" )
141152 public void canInsertEncryptedIndexedAndFindWithNonZeroContention () {
142- // JAVA-6237: this test inserts with contentionFactor 10 but encryptedFields.json configures the
143- // encryptedIndexed field with contention 0. Servers >= 9.0 reject this mismatch (SERVER-91887);
144- // skip until the spec prose test is corrected (DRIVERS-3547).
145- assumeTrue (serverVersionLessThan (9 , 0 ));
146153 EncryptOptions encryptOptions = new EncryptOptions ("Indexed" ).keyId (key1Id ).contentionFactor (10L );
147154 MongoCollection <BsonDocument > coll = encryptedClient .getDatabase (getDefaultDatabaseName ())
148- .getCollection ("explicit_encryption " , BsonDocument .class );
155+ .getCollection ("explicit_encryption_c10 " , BsonDocument .class );
149156
150157 for (int i = 0 ; i < 10 ; i ++) {
151158 BsonBinary insertPayload = clientEncryption .encrypt (ENCRYPTED_INDEXED_VALUE , encryptOptions );
152159 coll .insertOne (new BsonDocument ("encryptedIndexed" , insertPayload ));
153160 }
154161
155- encryptOptions = new EncryptOptions ("Indexed" ).keyId (key1Id ).queryType ("equality" ).contentionFactor (0L );
162+ // Find with matching contentionFactor returns all 10 documents.
163+ encryptOptions = new EncryptOptions ("Indexed" ).keyId (key1Id ).contentionFactor (10L ).queryType ("equality" );
156164 BsonBinary findPayload = clientEncryption .encrypt (ENCRYPTED_INDEXED_VALUE , encryptOptions );
157165
158166 List <BsonDocument > values = coll .find (new BsonDocument ("encryptedIndexed" , findPayload )).into (new ArrayList <>());
159- assertTrue (values .size () < 10 );
160- values .forEach (v ->
161- assertEquals (ENCRYPTED_INDEXED_VALUE , v .get ("encryptedIndexed" ))
162- );
163-
164- encryptOptions = new EncryptOptions ("Indexed" ).keyId (key1Id ).contentionFactor (10L ).queryType ("equality" );
165- BsonBinary findPayload2 = clientEncryption .encrypt (ENCRYPTED_INDEXED_VALUE , encryptOptions );
166-
167- values = coll .find (new BsonDocument ("encryptedIndexed" , findPayload2 )).into (new ArrayList <>());
168167
169168 assertEquals (10 , values .size ());
170169 values .forEach (v ->
@@ -173,6 +172,7 @@ public void canInsertEncryptedIndexedAndFindWithNonZeroContention() {
173172 }
174173
175174 @ Test
175+ @ DisplayName ("Case 3: can insert encrypted unindexed" )
176176 public void canInsertEncryptedUnindexed () {
177177 EncryptOptions encryptOptions = new EncryptOptions ("Unindexed" ).keyId (key1Id );
178178 MongoCollection <BsonDocument > coll = encryptedClient .getDatabase (getDefaultDatabaseName ())
@@ -188,6 +188,7 @@ public void canInsertEncryptedUnindexed() {
188188 }
189189
190190 @ Test
191+ @ DisplayName ("Case 4: can roundtrip encrypted indexed" )
191192 public void canRoundtripEncryptedIndexed () {
192193 EncryptOptions encryptOptions = new EncryptOptions ("Indexed" ).keyId (key1Id ).contentionFactor (0L );
193194
@@ -198,6 +199,7 @@ public void canRoundtripEncryptedIndexed() {
198199 }
199200
200201 @ Test
202+ @ DisplayName ("Case 5: can roundtrip encrypted unindexed" )
201203 public void canRoundtripEncryptedUnindexed () {
202204 EncryptOptions encryptOptions = new EncryptOptions ("Unindexed" ).keyId (key1Id );
203205
0 commit comments