|
17 | 17 | use Symfony\Component\Mime\Message;
|
18 | 18 | use Symfony\Component\Mime\Part\Multipart\MixedPart;
|
19 | 19 | use Symfony\Component\Mime\Part\TextPart;
|
20 |
| -use Symfony\Component\MimePgp\Mime\Part\Multipart\PgpEncryptedPart; |
21 | 20 | use Symfony\Component\MimePgp\Mime\Part\Multipart\PgpSignedPart;
|
22 |
| -use Symfony\Component\MimePgp\Mime\Part\PgpEncryptedInitializationPart; |
23 |
| -use Symfony\Component\MimePgp\Mime\Part\PgpEncryptedMessagePart; |
24 | 21 | use Symfony\Component\MimePgp\Mime\Part\PgpKeyPart;
|
25 | 22 | use Symfony\Component\MimePgp\Mime\Part\PgpSignaturePart;
|
26 |
| -use Symfony\Component\MimePgp\PgpEncrypter; |
| 23 | +use Symfony\Component\MimePgp\PgpProcess; |
27 | 24 | use Symfony\Component\MimePgp\PgpSigner;
|
28 | 25 |
|
29 |
| -class PgpEncryptorTest extends TestCase |
| 26 | +class PgpSignerTest extends TestCase |
30 | 27 | {
|
31 | 28 | private const KEY_EMAIL_ADDRESS = 'pgp@pulli.dev';
|
32 | 29 |
|
33 | 30 | private const KEY_PASSWORD = 'test1234';
|
34 | 31 |
|
35 |
| - public function testEncrypting() |
| 32 | + public function testPgpProcessCanSignCorrectly() |
36 | 33 | {
|
37 | 34 | //Given
|
38 |
| - $encrypter = new PgpEncrypter([ |
39 |
| - self::KEY_EMAIL_ADDRESS => __DIR__ .'/_data/pgp_test_public_key.asc' |
40 |
| - ]); |
| 35 | + $process = new PgpProcess(); |
| 36 | + $tester = new PgpTestingProcess(); |
41 | 37 |
|
42 |
| - $email = (new Email()) |
43 |
| - ->from(new Address(static::KEY_EMAIL_ADDRESS, 'PuLLi')) |
44 |
| - ->to(new Address(static::KEY_EMAIL_ADDRESS, 'PuLLi')) |
45 |
| - ->text("Hello there!\n\nHow are you?") |
46 |
| - ->subject('PGP Mail'); |
47 |
| - |
48 |
| - //When |
49 |
| - $encrypted = $encrypter->encrypt($email); |
50 |
| - |
51 |
| - //Then |
52 |
| - $this->checkEncryptedMessage($encrypted); |
53 |
| - |
54 |
| - $encryptedString = $encrypted->toString(); |
55 |
| - |
56 |
| - $this->assertStringContainsString('-----BEGIN PGP MESSAGE-----', $encryptedString, 'PGP message begin is missing.'); |
57 |
| - $this->assertStringContainsString('-----END PGP MESSAGE-----', $encryptedString, 'PGP message end is missing.'); |
58 |
| - |
59 |
| - [$initiliazationPart, $encryptedMessagePart] = $encrypted->getBody()->getParts(); |
60 |
| - static::assertInstanceOf(PgpEncryptedInitializationPart::class, $initiliazationPart); |
61 |
| - static::assertInstanceOf(PgpEncryptedMessagePart::class, $encryptedMessagePart); |
62 |
| - } |
63 |
| - |
64 |
| - public function testEncryptingAndSigning() |
65 |
| - { |
66 |
| - $encrypter = new PgpEncrypter([ |
67 |
| - self::KEY_EMAIL_ADDRESS => __DIR__ .'/_data/pgp_test_public_key.asc' |
68 |
| - ]); |
69 |
| - |
70 |
| - $email = (new Email()) |
71 |
| - ->from(new Address(static::KEY_EMAIL_ADDRESS, 'PuLLi')) |
72 |
| - ->to(new Address(static::KEY_EMAIL_ADDRESS, 'PuLLi')) |
73 |
| - ->text("Hello there!\n\nHow are you?") |
74 |
| - ->subject('PGP Mail'); |
75 |
| - |
76 |
| - //When |
77 |
| - $encrypted = $encrypter->encrypt($email); |
| 38 | + // When |
| 39 | + $output = $process->sign('Hello there!', __DIR__ .'/_data/pgp_test_secret_key.asc', self::KEY_PASSWORD); |
78 | 40 |
|
79 | 41 | //Then
|
80 |
| - $this->checkEncryptedMessage($encrypted); |
81 |
| - |
82 |
| - $encryptedMessageString = $encrypted->toString(); |
83 |
| - |
84 |
| - $this->assertStringContainsString('-----BEGIN PGP MESSAGE-----', $encryptedMessageString, 'PGP message begin is missing.'); |
85 |
| - $this->assertStringContainsString('-----END PGP MESSAGE-----', $encryptedMessageString, 'PGP message end is missing.'); |
86 |
| - $this->assertStringNotContainsString('-----BEGIN PGP SIGNATURE-----', $encryptedMessageString, 'PGP Signature begin is present.'); |
87 |
| - $this->assertStringNotContainsString('-----END PGP SIGNATURE-----', $encryptedMessageString, 'PGP Signature end is present.'); |
88 |
| - |
89 |
| - [$initiliazationPart, $encryptedMessagePart] = $encrypted->getBody()->getParts(); |
90 |
| - static::assertInstanceOf(PgpEncryptedInitializationPart::class, $initiliazationPart); |
91 |
| - static::assertInstanceOf(PgpEncryptedMessagePart::class, $encryptedMessagePart); |
| 42 | + $verified = $tester->verify('Hello there!', $output, __DIR__ .'/_data/pgp_test_public_key.asc'); |
| 43 | + $this->assertTrue($verified); |
| 44 | + $verified = $tester->verify('Hello there!', $output, __DIR__ .'/_data/other_public_key.asc'); |
| 45 | + $this->assertFalse($verified); |
92 | 46 | }
|
93 | 47 |
|
94 | 48 | public function testSigningWithPublicKey()
|
@@ -136,13 +90,9 @@ public function testSigningWithPublicKey()
|
136 | 90 | $originalBody = $this->normalize($email->getBody()->toString());
|
137 | 91 | $this->assertStringContainsString($originalBody."\r\n", $body->toString(), 'Signed message does not contain the actual message.');
|
138 | 92 |
|
139 |
| - static::markTestIncomplete('Need to implement the verification process.'); |
140 |
| - // It seems the final \r\n get stripped from the $signedPartString, so add them again to verify the signature |
141 |
| - $key = $this->gpg->verify($signedPartString."\r\n", $signature); |
142 |
| - |
143 |
| - $this->assertCount(1, $key); |
144 |
| - $this->assertSame(static::KEY_EMAIL_ADDRESS, $key[0]->getUserId()->getEmail()); |
145 |
| - $this->assertTrue($key[0]->isValid(), 'Signature is not valid.'); |
| 93 | + $tester = new PgpTestingProcess(); |
| 94 | + $result = $tester->verify($signedPartString, $signature, __DIR__ .'/_data/pgp_test_public_key.asc'); |
| 95 | + $this->assertTrue($result, 'Signature is not valid.'); |
146 | 96 | }
|
147 | 97 |
|
148 | 98 | public function testSigningWithoutPublicKey()
|
@@ -184,25 +134,9 @@ public function testSigningWithoutPublicKey()
|
184 | 134 | $originalBody = $this->normalize($email->getBody()->toString());
|
185 | 135 | $this->assertStringContainsString($originalBody."\r\n", $body->toString(), 'Signed message does not contain the actual message.');
|
186 | 136 |
|
187 |
| - static::markTestIncomplete('Need to implement the verification process.'); |
188 |
| - // It seems the final \r\n get stripped from the $signedPartString, so add them again to verify the signature |
189 |
| - $key = $this->gpg->verify($signedPartString."\r\n", $signature); |
190 |
| - |
191 |
| - $this->assertCount(1, $key); |
192 |
| - $this->assertSame(static::KEY_EMAIL_ADDRESS, $key[0]->getUserId()->getEmail()); |
193 |
| - $this->assertTrue($key[0]->isValid(), 'Signature is not valid.'); |
194 |
| - } |
195 |
| - |
196 |
| - private function checkEncryptedMessage(Message $message): void |
197 |
| - { |
198 |
| - $body = $message->getBody(); |
199 |
| - |
200 |
| - $this->assertInstanceOf(PgpEncryptedPart::class, $body, 'Message body is not encrypted.'); |
201 |
| - |
202 |
| - [$initializationPart, $messagePart] = $body->getParts(); |
203 |
| - |
204 |
| - $this->assertInstanceOf(PgpEncryptedInitializationPart::class, $initializationPart, 'Is not a PGP Initialization part.'); |
205 |
| - $this->assertInstanceOf(PgpEncryptedMessagePart::class, $messagePart, 'Is not a PGP Message part.'); |
| 137 | + $tester = new PgpTestingProcess(); |
| 138 | + $result = $tester->verify($signedPartString, $signature, __DIR__ .'/_data/pgp_test_public_key.asc'); |
| 139 | + $this->assertTrue($result, 'Signature is not valid.'); |
206 | 140 | }
|
207 | 141 |
|
208 | 142 | private function normalize(string $part): string
|
|
0 commit comments