|
10 | 10 | use MongoDB\BSON\Document;
|
11 | 11 | use MongoDB\BSON\UTCDateTime;
|
12 | 12 | use MongoDB\Driver\ClientEncryption;
|
| 13 | +use MongoDB\Driver\Exception\EncryptionException; |
13 | 14 | use MultipleIterator;
|
14 | 15 |
|
15 | 16 | use function base64_decode;
|
@@ -346,6 +347,87 @@ public function testCase5_CanRunAnAggregationExpressionInsideExpr(string $type,
|
346 | 347 | $this->assertMultipleDocumentsMatch($expectedDocuments, $cursor);
|
347 | 348 | }
|
348 | 349 |
|
| 350 | + /** |
| 351 | + * @see https://github.com/mongodb/specifications/blob/master/source/client-side-encryption/tests/README.rst#case-6-encrypting-a-document-greater-than-the-maximum-errors |
| 352 | + * @dataProvider provideTypeAndRangeOpts |
| 353 | + */ |
| 354 | + public function testCase6_EncryptingADocumentGreaterThanTheMaximumErrors(string $type, array $rangeOpts): void |
| 355 | + { |
| 356 | + if ($type === 'DecimalNoPrecision' || $type === 'DoubleNoPrecision') { |
| 357 | + $this->markTestSkipped('Test is not applicable to "NoPrecision" types'); |
| 358 | + } |
| 359 | + |
| 360 | + $this->setUpWithTypeAndRangeOpts($type, $rangeOpts); |
| 361 | + |
| 362 | + $encryptOpts = [ |
| 363 | + 'keyId' => $this->key1Id, |
| 364 | + 'algorithm' => ClientEncryption::ALGORITHM_RANGE_PREVIEW, |
| 365 | + 'contentionFactor' => 0, |
| 366 | + 'rangeOpts' => $rangeOpts, |
| 367 | + ]; |
| 368 | + |
| 369 | + $cast = self::getCastCallableForType($type); |
| 370 | + |
| 371 | + $this->expectException(EncryptionException::class); |
| 372 | + $this->expectExceptionMessage('Value must be greater than or equal to the minimum value and less than or equal to the maximum value'); |
| 373 | + $this->clientEncryption->encrypt($cast(201), $encryptOpts); |
| 374 | + } |
| 375 | + |
| 376 | + /** |
| 377 | + * @see https://github.com/mongodb/specifications/blob/master/source/client-side-encryption/tests/README.rst#case-7-encrypting-a-document-of-a-different-type-errors |
| 378 | + * @dataProvider provideTypeAndRangeOpts |
| 379 | + */ |
| 380 | + public function testCase7_EncryptingADocumentOfADifferentTypeErrors(string $type, array $rangeOpts): void |
| 381 | + { |
| 382 | + if ($type === 'DecimalNoPrecision' || $type === 'DoubleNoPrecision') { |
| 383 | + $this->markTestSkipped('Test is not applicable to "NoPrecision" types'); |
| 384 | + } |
| 385 | + |
| 386 | + $this->setUpWithTypeAndRangeOpts($type, $rangeOpts); |
| 387 | + |
| 388 | + $encryptOpts = [ |
| 389 | + 'keyId' => $this->key1Id, |
| 390 | + 'algorithm' => ClientEncryption::ALGORITHM_RANGE_PREVIEW, |
| 391 | + 'contentionFactor' => 0, |
| 392 | + 'rangeOpts' => $rangeOpts, |
| 393 | + ]; |
| 394 | + |
| 395 | + $value = $type === 'Int' ? 6.0 : 6; |
| 396 | + |
| 397 | + $this->expectException(EncryptionException::class); |
| 398 | + $this->expectExceptionMessage('expected matching \'min\' and value type'); |
| 399 | + $this->clientEncryption->encrypt($value, $encryptOpts); |
| 400 | + } |
| 401 | + |
| 402 | + /** |
| 403 | + * @see https://github.com/mongodb/specifications/blob/master/source/client-side-encryption/tests/README.rst#case-8-setting-precision-errors-if-the-type-is-not-a-double |
| 404 | + * @dataProvider provideTypeAndRangeOpts |
| 405 | + */ |
| 406 | + public function testCase8_SettingPrecisionErrorsIfTheTypeIsNotADouble(string $type, array $rangeOpts): void |
| 407 | + { |
| 408 | + if ($type === 'DecimalNoPrecision' || $type === 'DecimalPrecision' || $type === 'DoubleNoPrecision' || $type === 'DoublePrecision') { |
| 409 | + $this->markTestSkipped('Test is not applicable to floating point types'); |
| 410 | + } |
| 411 | + |
| 412 | + $this->setUpWithTypeAndRangeOpts($type, $rangeOpts); |
| 413 | + |
| 414 | + $encryptOpts = [ |
| 415 | + 'keyId' => $this->key1Id, |
| 416 | + 'algorithm' => ClientEncryption::ALGORITHM_RANGE_PREVIEW, |
| 417 | + 'contentionFactor' => 0, |
| 418 | + /* Note: this test intentionally uses a fixed value for rangeOpts. |
| 419 | + * This works because libmongocrypt checks "precision" before types |
| 420 | + * for "min" and "max". */ |
| 421 | + 'rangeOpts' => ['min' => 0, 'max' => 200, 'sparsity' => 1, 'precision' => 2], |
| 422 | + ]; |
| 423 | + |
| 424 | + $cast = self::getCastCallableForType($type); |
| 425 | + |
| 426 | + $this->expectException(EncryptionException::class); |
| 427 | + $this->expectExceptionMessage('expected \'precision\' to be set with double or decimal128 index'); |
| 428 | + $this->clientEncryption->encrypt($cast(6), $encryptOpts); |
| 429 | + } |
| 430 | + |
349 | 431 | private function assertMultipleDocumentsMatch(array $expectedDocuments, Iterator $actualDocuments): void
|
350 | 432 | {
|
351 | 433 | $mi = new MultipleIterator(MultipleIterator::MIT_NEED_ANY);
|
|
0 commit comments