Skip to content
This repository was archived by the owner on Feb 28, 2024. It is now read-only.

Commit 9613b27

Browse files
author
tkm
committed
Increase code coverage, improve tests
1 parent 5645df0 commit 9613b27

File tree

5 files changed

+73
-31
lines changed

5 files changed

+73
-31
lines changed

tests/Developer/Encryption/FieldLevelEncryptionConfigBuilderTest.php

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace Mastercard\Developer\Utils;
33

4+
use InvalidArgumentException;
5+
use Mastercard\Developer\Encryption\EncryptionException;
46
use Mastercard\Developer\Encryption\FieldLevelEncryptionConfigBuilder;
57
use Mastercard\Developer\Encryption\FieldValueEncoding;
68
use Mastercard\Developer\Test\TestUtils;
@@ -63,8 +65,17 @@ public function testBuild_ShouldComputeCertificateAndKeyFingerprints_WhenFingerp
6365
$this->assertEquals('80810fc13a8319fcf0e2ec322c82a4c304b782cc3ce671176343cfe8160c2279', $config->getEncryptionCertificateFingerprint());
6466
}
6567

68+
public function testBuild_ShouldThrowEncryptionException_WhenFingerprintCanNotBeCalculated() {
69+
$this->expectException(EncryptionException::class);
70+
$this->expectExceptionMessage('Failed to compute encryption certificate fingerprint!');
71+
TestUtils::getTestFieldLevelEncryptionConfigBuilder()
72+
->withEncryptionCertificateFingerprint(null)
73+
->withEncryptionCertificate('not a certificate')
74+
->build();
75+
}
76+
6677
public function testBuild_ShouldThrowInvalidArgumentException_WhenNotDefiniteDecryptionPath() {
67-
$this->expectException(\InvalidArgumentException::class);
78+
$this->expectException(InvalidArgumentException::class);
6879
$this->expectExceptionMessage('JSON paths for decryption must point to a single item!');
6980
FieldLevelEncryptionConfigBuilder::aFieldLevelEncryptionConfig()
7081
->withDecryptionPath('$.encryptedPayloads[*]', '$.payload')
@@ -73,7 +84,7 @@ public function testBuild_ShouldThrowInvalidArgumentException_WhenNotDefiniteDec
7384
}
7485

7586
public function testBuild_ShouldThrowInvalidArgumentException_WhenMissingDecryptionKey() {
76-
$this->expectException(\InvalidArgumentException::class);
87+
$this->expectException(InvalidArgumentException::class);
7788
$this->expectExceptionMessage('Can\'t decrypt without decryption key!');
7889
FieldLevelEncryptionConfigBuilder::aFieldLevelEncryptionConfig()
7990
->withDecryptionPath('$.encryptedPayload', '$.payload')
@@ -86,7 +97,7 @@ public function testBuild_ShouldThrowInvalidArgumentException_WhenMissingDecrypt
8697
}
8798

8899
public function testBuild_ShouldThrowInvalidArgumentException_WhenNotDefiniteEncryptionPath() {
89-
$this->expectException(\InvalidArgumentException::class);
100+
$this->expectException(InvalidArgumentException::class);
90101
$this->expectExceptionMessage('JSON paths for encryption must point to a single item!');
91102
FieldLevelEncryptionConfigBuilder::aFieldLevelEncryptionConfig()
92103
->withEncryptionPath('$.payloads[*]', '$.encryptedPayload')
@@ -95,7 +106,7 @@ public function testBuild_ShouldThrowInvalidArgumentException_WhenNotDefiniteEnc
95106
}
96107

97108
public function testBuild_ShouldThrowInvalidArgumentException_WhenMissingEncryptionCertificate() {
98-
$this->expectException(\InvalidArgumentException::class);
109+
$this->expectException(InvalidArgumentException::class);
99110
$this->expectExceptionMessage('Can\'t encrypt without encryption key!');
100111
FieldLevelEncryptionConfigBuilder::aFieldLevelEncryptionConfig()
101112
->withEncryptionPath('$.payload', '$.encryptedPayload')
@@ -108,7 +119,7 @@ public function testBuild_ShouldThrowInvalidArgumentException_WhenMissingEncrypt
108119
}
109120

110121
public function testBuild_ShouldThrowInvalidArgumentException_WhenMissingOaepPaddingDigestAlgorithm() {
111-
$this->expectException(\InvalidArgumentException::class);
122+
$this->expectException(InvalidArgumentException::class);
112123
$this->expectExceptionMessage('The digest algorithm for OAEP cannot be empty!');
113124
FieldLevelEncryptionConfigBuilder::aFieldLevelEncryptionConfig()
114125
->withEncryptedValueFieldName('encryptedValue')
@@ -119,15 +130,15 @@ public function testBuild_ShouldThrowInvalidArgumentException_WhenMissingOaepPad
119130
}
120131

121132
public function testBuild_ShouldThrowInvalidArgumentException_WhenUnsupportedOaepPaddingDigestAlgorithm() {
122-
$this->expectException(\InvalidArgumentException::class);
133+
$this->expectException(InvalidArgumentException::class);
123134
$this->expectExceptionMessage('Unsupported OAEP digest algorithm: SHA-720!');
124135
FieldLevelEncryptionConfigBuilder::aFieldLevelEncryptionConfig()
125136
->withOaepPaddingDigestAlgorithm('SHA-720')
126137
->build();
127138
}
128139

129140
public function testBuild_ShouldThrowInvalidArgumentException_WhenMissingEncryptedValueFieldName() {
130-
$this->expectException(\InvalidArgumentException::class);
141+
$this->expectException(InvalidArgumentException::class);
131142
$this->expectExceptionMessage('Encrypted value field name cannot be empty!');
132143
FieldLevelEncryptionConfigBuilder::aFieldLevelEncryptionConfig()
133144
->withOaepPaddingDigestAlgorithm('SHA-512')
@@ -138,7 +149,7 @@ public function testBuild_ShouldThrowInvalidArgumentException_WhenMissingEncrypt
138149
}
139150

140151
public function testBuild_ShouldThrowInvalidArgumentException_WhenMissingBothEncryptedKeyFieldNameAndHeaderName() {
141-
$this->expectException(\InvalidArgumentException::class);
152+
$this->expectException(InvalidArgumentException::class);
142153
$this->expectExceptionMessage('At least one of encrypted key field name or encrypted key header name must be set!');
143154
FieldLevelEncryptionConfigBuilder::aFieldLevelEncryptionConfig()
144155
->withOaepPaddingDigestAlgorithm('SHA-512')
@@ -149,7 +160,7 @@ public function testBuild_ShouldThrowInvalidArgumentException_WhenMissingBothEnc
149160
}
150161

151162
public function testBuild_ShouldThrowInvalidArgumentException_WhenMissingBothIvFieldNameAndHeaderName() {
152-
$this->expectException(\InvalidArgumentException::class);
163+
$this->expectException(InvalidArgumentException::class);
153164
$this->expectExceptionMessage('At least one of IV field name or IV header name must be set!');
154165
FieldLevelEncryptionConfigBuilder::aFieldLevelEncryptionConfig()
155166
->withOaepPaddingDigestAlgorithm('SHA-512')
@@ -160,7 +171,7 @@ public function testBuild_ShouldThrowInvalidArgumentException_WhenMissingBothIvF
160171
}
161172

162173
public function testBuild_ShouldThrowInvalidArgumentException_WhenMissingFieldValueEncoding() {
163-
$this->expectException(\InvalidArgumentException::class);
174+
$this->expectException(InvalidArgumentException::class);
164175
$this->expectExceptionMessage('Value encoding for fields and headers cannot be empty!');
165176
FieldLevelEncryptionConfigBuilder::aFieldLevelEncryptionConfig()
166177
->withOaepPaddingDigestAlgorithm('SHA-512')
@@ -171,7 +182,7 @@ public function testBuild_ShouldThrowInvalidArgumentException_WhenMissingFieldVa
171182
}
172183

173184
public function testBuild_ShouldThrowInvalidArgumentException_WhenEncryptedKeyAndIvHeaderNamesNotBothSetOrUnset() {
174-
$this->expectException(\InvalidArgumentException::class);
185+
$this->expectException(InvalidArgumentException::class);
175186
$this->expectExceptionMessage('IV header name and encrypted key header name must be both set or both unset!');
176187
FieldLevelEncryptionConfigBuilder::aFieldLevelEncryptionConfig()
177188
->withOaepPaddingDigestAlgorithm('SHA-512')
@@ -184,7 +195,7 @@ public function testBuild_ShouldThrowInvalidArgumentException_WhenEncryptedKeyAn
184195
}
185196

186197
public function testBuild_ShouldThrowInvalidArgumentException_WhenEncryptedKeyAndIvFieldNamesNotBothSetOrUnset() {
187-
$this->expectException(\InvalidArgumentException::class);
198+
$this->expectException(InvalidArgumentException::class);
188199
$this->expectExceptionMessage('IV field name and encrypted key field name must be both set or both unset!');
189200
FieldLevelEncryptionConfigBuilder::aFieldLevelEncryptionConfig()
190201
->withOaepPaddingDigestAlgorithm('SHA-512')
@@ -195,4 +206,4 @@ public function testBuild_ShouldThrowInvalidArgumentException_WhenEncryptedKeyAn
195206
->withFieldValueEncoding(FieldValueEncoding::HEX)
196207
->build();
197208
}
198-
}
209+
}

tests/Developer/Encryption/FieldLevelEncryptionTest.php

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Mastercard\Developer\Encryption;
44

5+
use InvalidArgumentException;
56
use Mastercard\Developer\Test\TestUtils;
67
use Mastercard\Developer\Utils\EncryptionUtils;
78
use Mastercard\Developer\Utils\StringUtils;
@@ -12,7 +13,7 @@ class FieldLevelEncryptionTest extends TestCase {
1213

1314
public function testConstruct_ShouldBePrivate() {
1415
// GIVEN
15-
$class = new ReflectionClass('Mastercard\Developer\Encryption\FieldLevelEncryption');
16+
$class = new ReflectionClass(FieldLevelEncryption::class);
1617
$constructor = $class->getConstructor();
1718

1819
// WHEN
@@ -27,11 +28,11 @@ public function testConstruct_ShouldBePrivate() {
2728
}
2829

2930
private static function callEncryptBytes($params) {
30-
return TestUtils::callPrivateStatic('\FieldLevelEncryption', 'encryptBytes', $params);
31+
return TestUtils::callPrivateStatic(FieldLevelEncryption::class, 'encryptBytes', $params);
3132
}
3233

3334
private static function callDecryptBytes($params) {
34-
return TestUtils::callPrivateStatic('\FieldLevelEncryption', 'decryptBytes', $params);
35+
return TestUtils::callPrivateStatic(FieldLevelEncryption::class, 'decryptBytes', $params);
3536
}
3637

3738
private function assertDecryptedPayloadEquals($expectedPayload, $encryptedPayload, $config) {
@@ -320,13 +321,29 @@ public function testEncryptPayload_ShouldThrowInvalidArgumentException_WhenOutPa
320321
->build();
321322

322323
// THEN
323-
$this->expectException(\InvalidArgumentException::class);
324+
$this->expectException(InvalidArgumentException::class);
324325
$this->expectExceptionMessage('Parent path not found in payload: \'$[\'parentNotInPayload\']\'!');
325326

326327
// WHEN
327328
FieldLevelEncryption::encryptPayload($payload, $config);
328329
}
329330

331+
public function testEncryptPayload_ShouldThrowInvalidArgumentException_WhenPayloadIsNotAnObject() {
332+
333+
// GIVEN
334+
$payload = '[]';
335+
$config = TestUtils::getTestFieldLevelEncryptionConfigBuilder()
336+
->withEncryptionPath('', '')
337+
->build();
338+
339+
// THEN
340+
$this->expectException(InvalidArgumentException::class);
341+
$this->expectExceptionMessage('An object was expected!');
342+
343+
// WHEN
344+
FieldLevelEncryption::encryptPayload($payload, $config);
345+
}
346+
330347
public function testEncryptPayload_ShouldThrowInvalidArgumentException_WhenOutPathIsPathToJsonPrimitive() {
331348

332349
// GIVEN
@@ -340,7 +357,7 @@ public function testEncryptPayload_ShouldThrowInvalidArgumentException_WhenOutPa
340357
->build();
341358

342359
// THEN
343-
$this->expectException(\InvalidArgumentException::class);
360+
$this->expectException(InvalidArgumentException::class);
344361
$this->expectExceptionMessage('JSON object expected at path: \'encryptedData\'!');
345362

346363
// WHEN
@@ -899,7 +916,7 @@ public function testDecryptPayload_ShouldThrowInvalidArgumentException_WhenOutPa
899916
->build();
900917

901918
// THEN
902-
$this->expectException(\InvalidArgumentException::class);
919+
$this->expectException(InvalidArgumentException::class);
903920
$this->expectExceptionMessage('Parent path not found in payload: \'$[\'parentNotInPayload\']\'!');
904921

905922
// WHEN
@@ -923,7 +940,7 @@ public function testDecryptPayload_ShouldThrowInvalidArgumentException_WhenOutPa
923940
->build();
924941

925942
// THEN
926-
$this->expectException(\InvalidArgumentException::class);
943+
$this->expectException(InvalidArgumentException::class);
927944
$this->expectExceptionMessage('JSON object expected at path: \'data\'!');
928945

929946
// WHEN
@@ -939,7 +956,7 @@ public function testDecryptPayload_ShouldThrowInvalidArgumentException_WhenInPat
939956
->build();
940957

941958
// THEN
942-
$this->expectException(\InvalidArgumentException::class);
959+
$this->expectException(InvalidArgumentException::class);
943960
$this->expectExceptionMessage('JSON object expected at path: \'encryptedData\'!');
944961

945962
// WHEN
@@ -1278,7 +1295,7 @@ public function testDecryptPayload_ShouldThrowInvalidArgumentException_WhenEncry
12781295
->build();
12791296

12801297
// THEN
1281-
$this->expectException(\InvalidArgumentException::class);
1298+
$this->expectException(InvalidArgumentException::class);
12821299
$this->expectExceptionMessage('Encryption params have to be set when not stored in HTTP payloads!');
12831300

12841301
// WHEN

tests/Developer/Encryption/FileEncryptionParamsTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
class FileEncryptionParamsTest extends TestCase {
99

1010
private static function callWrapSecretKey($params) {
11-
return TestUtils::callPrivateStatic('\FieldLevelEncryptionParams', 'wrapSecretKey', $params);
11+
return TestUtils::callPrivateStatic(FieldLevelEncryptionParams::class, 'wrapSecretKey', $params);
1212
}
1313

1414
private static function callUnwrapSecretKey($params) {
15-
return TestUtils::callPrivateStatic('\FieldLevelEncryptionParams', 'unwrapSecretKey', $params);
15+
return TestUtils::callPrivateStatic(FieldLevelEncryptionParams::class, 'unwrapSecretKey', $params);
1616
}
17-
17+
1818
public function testGenerate_Nominal() {
1919

2020
// GIVEN
@@ -109,4 +109,4 @@ public function testUnwrapSecretKey_InteroperabilityTest_OaepSha512() {
109109
$expectedKeyBytes = base64_decode('mZzmzoURXI3Vk0vdsPkcFw==');
110110
$this->assertEquals($expectedKeyBytes, $unwrappedKeyBytes);
111111
}
112-
}
112+
}

tests/Developer/Json/JsonPathTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ public function testGetElementKey() {
9696
$this->assertEquals('obj2', $jsonElementKey4);
9797
}
9898

99+
public function testGetElementKey_ShouldThrowInvalidArgumentException_WhenPathIsInvalid() {
100+
$this->expectException(\InvalidArgumentException::class);
101+
$this->expectExceptionMessage('Unsupported JSON path: $invalidPath!');
102+
JsonPath::getElementKey('$invalidPath');
103+
}
104+
99105
public function testGetParent() {
100106

101107
// GIVEN
@@ -123,6 +129,12 @@ public function testGetParent() {
123129
$this->assertEquals('$', $parentJsonPath5);
124130
}
125131

132+
public function testGetParent_ShouldThrowInvalidArgumentException_WhenPathIsInvalid() {
133+
$this->expectException(\InvalidArgumentException::class);
134+
$this->expectExceptionMessage('Unsupported JSON path: $invalidPath!');
135+
JsonPath::getParentPath('$invalidPath');
136+
}
137+
126138
public function testFind() {
127139

128140
// GIVEN
@@ -147,6 +159,7 @@ public function testFind() {
147159
$jsonElement5 = JsonPath::find($jsonObject, '$.child3.grandchild1');
148160
$jsonElement6 = JsonPath::find($jsonObject, '$.child3.grandchild2');
149161
$jsonElement7 = JsonPath::find($jsonObject, '$.not.existing.path');
162+
$jsonElement8 = JsonPath::find($jsonObject, '$invalidPath');
150163

151164
// THEN
152165
$this->assertSame($jsonObject, $jsonElement1);
@@ -156,6 +169,7 @@ public function testFind() {
156169
$this->assertSame($jsonObject->child3->grandchild1, $jsonElement5);
157170
$this->assertSame($jsonObject->child3->grandchild2, $jsonElement6);
158171
$this->assertNull($jsonElement7);
172+
$this->assertNull($jsonElement8);
159173
}
160174

161175
public function testDelete() {

tests/Developer/Test/TestUtils.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,33 @@
22

33
namespace Mastercard\Developer\Test;
44

5-
use Mastercard\Developer\Encryption\EncryptionException;
65
use Mastercard\Developer\Encryption\FieldLevelEncryptionConfigBuilder;
76
use Mastercard\Developer\Encryption\FieldValueEncoding;
87
use Mastercard\Developer\Utils\EncryptionUtils;
8+
use ReflectionClass;
99

1010
class TestUtils {
1111

1212
public static function callPrivateStatic($className, $functionName, $args) {
1313
if (is_string($args) || is_null($args)) {
1414
$args = array($args);
1515
}
16-
$class = new \ReflectionClass('Mastercard\Developer\Encryption' . $className);
16+
$class = new ReflectionClass($className);
1717
$method = $class->getMethod($functionName);
1818
$method->setAccessible(true);
1919
return $method->invokeArgs(null, $args);
2020
}
2121

2222
public static function getTestEncryptionCertificate() {
23-
return EncryptionUtils::loadEncryptionCertificate('./resources/Certificates/test_certificate-2048.pem');
23+
return EncryptionUtils::loadEncryptionCertificate(dirname(__DIR__, 3).'/resources/Certificates/test_certificate-2048.pem');
2424
}
2525

2626
public static function getTestInvalidEncryptionCertificate() {
2727
return "not a certificate!";
2828
}
2929

3030
public static function getTestDecryptionKey() {
31-
return EncryptionUtils::loadDecryptionKey('./resources/Keys/Pkcs8/test_key_pkcs8-2048.der');
31+
return EncryptionUtils::loadDecryptionKey(dirname(__DIR__, 3).'/resources/Keys/Pkcs8/test_key_pkcs8-2048.der');
3232
}
3333

3434
public static function getTestFieldLevelEncryptionConfigBuilder() {
@@ -46,4 +46,4 @@ public static function getTestFieldLevelEncryptionConfigBuilder() {
4646
->withEncryptionKeyFingerprint('761b003c1eade3a5490e5000d37887baa5e6ec0e226c07706e599451fc032a79')
4747
->withFieldValueEncoding(FieldValueEncoding::HEX);
4848
}
49-
}
49+
}

0 commit comments

Comments
 (0)