Skip to content

Commit 984992f

Browse files
authored
Merge pull request symfony#4 from Spomky/test/fix-verification-and-decryption
Add PgpTestingProcess for testing encryption and signing
2 parents ed5b465 + 9424d4f commit 984992f

File tree

4 files changed

+302
-83
lines changed

4 files changed

+302
-83
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\MimePgp\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Mime\Address;
16+
use Symfony\Component\Mime\Email;
17+
use Symfony\Component\Mime\Message;
18+
use Symfony\Component\MimePgp\Mime\Part\Multipart\PgpEncryptedPart;
19+
use Symfony\Component\MimePgp\Mime\Part\PgpEncryptedInitializationPart;
20+
use Symfony\Component\MimePgp\Mime\Part\PgpEncryptedMessagePart;
21+
use Symfony\Component\MimePgp\PgpEncrypter;
22+
use Symfony\Component\MimePgp\PgpProcess;
23+
24+
class PgpEncrypterTest extends TestCase
25+
{
26+
private const KEY_EMAIL_ADDRESS = 'pgp@pulli.dev';
27+
28+
private const KEY_PASSWORD = 'test1234';
29+
30+
public function testPgpProcessCanEncryptCorrectly()
31+
{
32+
//Given
33+
$process = new PgpProcess();
34+
$tester = new PgpTestingProcess();
35+
36+
// When
37+
$output = $process->encrypt('Hello there!', [self::KEY_EMAIL_ADDRESS => __DIR__ .'/_data/pgp_test_public_key.asc']);
38+
39+
//Then
40+
$decrypted = $tester->decrypt($output, __DIR__ .'/_data/pgp_test_public_key.asc', self::KEY_PASSWORD);
41+
$this->assertSame('Hello there!', $decrypted);
42+
}
43+
44+
public function testEncrypting()
45+
{
46+
//Given
47+
$encrypter = new PgpEncrypter([
48+
self::KEY_EMAIL_ADDRESS => __DIR__ .'/_data/pgp_test_public_key.asc'
49+
]);
50+
51+
$email = (new Email())
52+
->from(new Address(static::KEY_EMAIL_ADDRESS, 'PuLLi'))
53+
->to(new Address(static::KEY_EMAIL_ADDRESS, 'PuLLi'))
54+
->text("Hello there!\n\nHow are you?")
55+
->subject('PGP Mail');
56+
57+
//When
58+
$encrypted = $encrypter->encrypt($email);
59+
60+
//Then
61+
$this->checkEncryptedMessage($encrypted);
62+
63+
$encryptedString = $encrypted->toString();
64+
65+
$this->assertStringContainsString('-----BEGIN PGP MESSAGE-----', $encryptedString, 'PGP message begin is missing.');
66+
$this->assertStringContainsString('-----END PGP MESSAGE-----', $encryptedString, 'PGP message end is missing.');
67+
68+
[$initiliazationPart, $encryptedMessagePart] = $encrypted->getBody()->getParts();
69+
static::assertInstanceOf(PgpEncryptedInitializationPart::class, $initiliazationPart);
70+
static::assertInstanceOf(PgpEncryptedMessagePart::class, $encryptedMessagePart);
71+
72+
$tester = new PgpTestingProcess();
73+
$result = $tester->decrypt($encryptedMessagePart->toString(), __DIR__ .'/_data/pgp_test_secret_key.asc', self::KEY_PASSWORD);
74+
$this->assertStringContainsString('Hello there!', $result, 'Unable to decrypt message.');
75+
}
76+
77+
public function testEncryptingAndSigning()
78+
{
79+
$encrypter = new PgpEncrypter([
80+
self::KEY_EMAIL_ADDRESS => __DIR__ .'/_data/pgp_test_public_key.asc'
81+
]);
82+
83+
$email = (new Email())
84+
->from(new Address(static::KEY_EMAIL_ADDRESS, 'PuLLi'))
85+
->to(new Address(static::KEY_EMAIL_ADDRESS, 'PuLLi'))
86+
->text("Hello there!\n\nHow are you?")
87+
->subject('PGP Mail');
88+
89+
//When
90+
$encrypted = $encrypter->encrypt($email);
91+
92+
//Then
93+
$this->checkEncryptedMessage($encrypted);
94+
95+
$encryptedMessageString = $encrypted->toString();
96+
97+
$this->assertStringContainsString('-----BEGIN PGP MESSAGE-----', $encryptedMessageString, 'PGP message begin is missing.');
98+
$this->assertStringContainsString('-----END PGP MESSAGE-----', $encryptedMessageString, 'PGP message end is missing.');
99+
$this->assertStringNotContainsString('-----BEGIN PGP SIGNATURE-----', $encryptedMessageString, 'PGP Signature begin is present.');
100+
$this->assertStringNotContainsString('-----END PGP SIGNATURE-----', $encryptedMessageString, 'PGP Signature end is present.');
101+
102+
[$initiliazationPart, $encryptedMessagePart] = $encrypted->getBody()->getParts();
103+
static::assertInstanceOf(PgpEncryptedInitializationPart::class, $initiliazationPart);
104+
static::assertInstanceOf(PgpEncryptedMessagePart::class, $encryptedMessagePart);
105+
106+
$tester = new PgpTestingProcess();
107+
$result = $tester->decrypt($encryptedMessagePart->toString(), __DIR__ .'/_data/pgp_test_secret_key.asc', self::KEY_PASSWORD);
108+
$this->assertStringContainsString('Hello there!', $result, 'Signature is not valid.');
109+
}
110+
111+
private function checkEncryptedMessage(Message $message): void
112+
{
113+
$body = $message->getBody();
114+
115+
$this->assertInstanceOf(PgpEncryptedPart::class, $body, 'Message body is not encrypted.');
116+
117+
[$initializationPart, $messagePart] = $body->getParts();
118+
119+
$this->assertInstanceOf(PgpEncryptedInitializationPart::class, $initializationPart, 'Is not a PGP Initialization part.');
120+
$this->assertInstanceOf(PgpEncryptedMessagePart::class, $messagePart, 'Is not a PGP Message part.');
121+
}
122+
123+
private function normalize(string $part): string
124+
{
125+
return str_replace("\n", "\r\n", str_replace(["\r\n", "\r"], "\n", $part));
126+
}
127+
}

src/Symfony/Component/MimePgp/Tests/PgpEncryptorTest.php renamed to src/Symfony/Component/MimePgp/Tests/PgpSignerTest.php

Lines changed: 17 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -17,78 +17,32 @@
1717
use Symfony\Component\Mime\Message;
1818
use Symfony\Component\Mime\Part\Multipart\MixedPart;
1919
use Symfony\Component\Mime\Part\TextPart;
20-
use Symfony\Component\MimePgp\Mime\Part\Multipart\PgpEncryptedPart;
2120
use Symfony\Component\MimePgp\Mime\Part\Multipart\PgpSignedPart;
22-
use Symfony\Component\MimePgp\Mime\Part\PgpEncryptedInitializationPart;
23-
use Symfony\Component\MimePgp\Mime\Part\PgpEncryptedMessagePart;
2421
use Symfony\Component\MimePgp\Mime\Part\PgpKeyPart;
2522
use Symfony\Component\MimePgp\Mime\Part\PgpSignaturePart;
26-
use Symfony\Component\MimePgp\PgpEncrypter;
23+
use Symfony\Component\MimePgp\PgpProcess;
2724
use Symfony\Component\MimePgp\PgpSigner;
2825

29-
class PgpEncryptorTest extends TestCase
26+
class PgpSignerTest extends TestCase
3027
{
3128
private const KEY_EMAIL_ADDRESS = 'pgp@pulli.dev';
3229

3330
private const KEY_PASSWORD = 'test1234';
3431

35-
public function testEncrypting()
32+
public function testPgpProcessCanSignCorrectly()
3633
{
3734
//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();
4137

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);
7840

7941
//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);
9246
}
9347

9448
public function testSigningWithPublicKey()
@@ -136,13 +90,9 @@ public function testSigningWithPublicKey()
13690
$originalBody = $this->normalize($email->getBody()->toString());
13791
$this->assertStringContainsString($originalBody."\r\n", $body->toString(), 'Signed message does not contain the actual message.');
13892

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.');
14696
}
14797

14898
public function testSigningWithoutPublicKey()
@@ -184,25 +134,9 @@ public function testSigningWithoutPublicKey()
184134
$originalBody = $this->normalize($email->getBody()->toString());
185135
$this->assertStringContainsString($originalBody."\r\n", $body->toString(), 'Signed message does not contain the actual message.');
186136

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.');
206140
}
207141

208142
private function normalize(string $part): string

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy