Skip to content

Commit e51710f

Browse files
committed
Prose test for unique index on keyAltNames
1 parent 022c294 commit e51710f

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

tests/SpecTests/ClientSideEncryptionSpecTest.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use MongoDB\Driver\Exception\ConnectionTimeoutException;
1616
use MongoDB\Driver\Exception\EncryptionException;
1717
use MongoDB\Driver\Exception\RuntimeException;
18+
use MongoDB\Driver\Exception\ServerException;
1819
use MongoDB\Driver\Monitoring\CommandFailedEvent;
1920
use MongoDB\Driver\Monitoring\CommandStartedEvent;
2021
use MongoDB\Driver\Monitoring\CommandSubscriber;
@@ -1403,6 +1404,93 @@ static function (self $test, ClientEncryption $clientEncryption, Client $encrypt
14031404
];
14041405
}
14051406

1407+
/**
1408+
* Prose test 13: Unique Index on keyAltNames
1409+
*
1410+
* @see https://github.com/mongodb/specifications/blob/master/source/client-side-encryption/tests/README.rst#unique-index-on-keyaltnames
1411+
* @dataProvider provideUniqueIndexOnKeyAltNamesTests
1412+
*/
1413+
public function testUniqueIndexOnKeyAltNames(Closure $test): void
1414+
{
1415+
// Test setup
1416+
$client = static::createTestClient();
1417+
1418+
// Ensure that the key vault is dropped with a majority write concern
1419+
self::insertKeyVaultData($client, []);
1420+
1421+
$client->selectCollection('keyvault', 'datakeys')->createIndex(
1422+
['keyAltNames' => 1],
1423+
[
1424+
'unique' => true,
1425+
'partialFilterExpression' => ['keyAltNames' => ['$exists' => true]],
1426+
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
1427+
],
1428+
);
1429+
1430+
$clientEncryption = new ClientEncryption([
1431+
'keyVaultClient' => $client->getManager(),
1432+
'keyVaultNamespace' => 'keyvault.datakeys',
1433+
'kmsProviders' => ['local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)]],
1434+
]);
1435+
1436+
$clientEncryption->createDataKey('local', ['keyAltNames' => ['def']]);
1437+
1438+
$test($this, $client, $clientEncryption);
1439+
}
1440+
1441+
public static function provideUniqueIndexOnKeyAltNamesTests()
1442+
{
1443+
// See: https://github.com/mongodb/specifications/blob/master/source/client-side-encryption/tests/README.rst#case-1-createdatakey
1444+
yield 'Case 1: createDataKey()' => [
1445+
static function (self $test, Client $client, ClientEncryption $clientEncryption): void {
1446+
$clientEncryption->createDataKey('local', ['keyAltNames' => ['abc']]);
1447+
1448+
try {
1449+
$clientEncryption->createDataKey('local', ['keyAltNames' => ['abc']]);
1450+
$test->fail('Expected exception to be thrown');
1451+
} catch (ServerException $e) {
1452+
$test->assertSame(11000 /* DuplicateKey */, $e->getCode());
1453+
}
1454+
1455+
try {
1456+
$clientEncryption->createDataKey('local', ['keyAltNames' => ['def']]);
1457+
$test->fail('Expected exception to be thrown');
1458+
} catch (ServerException $e) {
1459+
$test->assertSame(11000 /* DuplicateKey */, $e->getCode());
1460+
}
1461+
},
1462+
];
1463+
1464+
// See: https://github.com/mongodb/specifications/blob/master/source/client-side-encryption/tests/README.rst#case-2-addkeyaltname
1465+
yield 'Case 2: addKeyAltName()' => [
1466+
static function (self $test, Client $client, ClientEncryption $clientEncryption): void {
1467+
$keyId = $clientEncryption->createDataKey('local');
1468+
1469+
$keyBeforeUpdate = $clientEncryption->addKeyAltName($keyId, 'abc');
1470+
$test->assertObjectNotHasAttribute('keyAltNames', $keyBeforeUpdate);
1471+
1472+
$keyBeforeUpdate = $clientEncryption->addKeyAltName($keyId, 'abc');
1473+
$test->assertObjectHasAttribute('keyAltNames', $keyBeforeUpdate);
1474+
$test->assertIsArray($keyBeforeUpdate->keyAltNames);
1475+
$test->assertContains('abc', $keyBeforeUpdate->keyAltNames);
1476+
1477+
try {
1478+
$clientEncryption->addKeyAltName($keyId, 'def');
1479+
$test->fail('Expected exception to be thrown');
1480+
} catch (ServerException $e) {
1481+
$test->assertSame(11000 /* DuplicateKey */, $e->getCode());
1482+
}
1483+
1484+
$originalKeyId = $clientEncryption->getKeyByAltName('def')->_id;
1485+
1486+
$originalKeyBeforeUpdate = $clientEncryption->addKeyAltName($originalKeyId, 'def');
1487+
$test->assertObjectHasAttribute('keyAltNames', $originalKeyBeforeUpdate);
1488+
$test->assertIsArray($originalKeyBeforeUpdate->keyAltNames);
1489+
$test->assertContains('def', $originalKeyBeforeUpdate->keyAltNames);
1490+
},
1491+
];
1492+
}
1493+
14061494
/**
14071495
* Prose test 14: Decryption Events
14081496
*

0 commit comments

Comments
 (0)