Skip to content

Commit 43ee2a5

Browse files
committed
Encryption Bundle tests
1 parent 7c00526 commit 43ee2a5

File tree

13 files changed

+405
-4
lines changed

13 files changed

+405
-4
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* The MIT License (MIT)
7+
*
8+
* Copyright (c) 2014-2017 Spomky-Labs
9+
*
10+
* This software may be modified and distributed under the terms
11+
* of the MIT license. See the LICENSE file for details.
12+
*/
13+
14+
use Symfony\Component\Config\Loader\LoaderInterface;
15+
use Symfony\Component\HttpKernel\Kernel;
16+
17+
/**
18+
* Class AppKernel.
19+
*/
20+
final class AppKernel extends Kernel
21+
{
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function registerBundles()
26+
{
27+
$bundles = [
28+
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
29+
new Jose\Bundle\JoseFramework\JoseFrameworkBundle(),
30+
new Jose\Bundle\Checker\CheckerBundle(),
31+
new Jose\Bundle\Encryption\EncryptionBundle(),
32+
new Jose\Bundle\Encryption\Tests\TestBundle\TestBundle(),
33+
];
34+
35+
return $bundles;
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function getCacheDir()
42+
{
43+
return sys_get_temp_dir().'/Jose/Bundle/Test';
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
public function registerContainerConfiguration(LoaderInterface $loader)
50+
{
51+
$loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml');
52+
}
53+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* The MIT License (MIT)
7+
*
8+
* Copyright (c) 2014-2017 Spomky-Labs
9+
*
10+
* This software may be modified and distributed under the terms
11+
* of the MIT license. See the LICENSE file for details.
12+
*/
13+
14+
namespace Jose\Bundle\Encryption\Tests;
15+
16+
use Jose\Component\Encryption\JWEBuilder;
17+
use Jose\Component\Encryption\JWEBuilderFactory;
18+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
19+
20+
/**
21+
* @group Bundle
22+
* @group Functional
23+
*/
24+
final class JWEBuilderTest extends WebTestCase
25+
{
26+
public function testJWEBuilderFactoryIsAvailable()
27+
{
28+
$client = static::createClient();
29+
$container = $client->getContainer();
30+
self::assertNotNull($container);
31+
self::assertTrue($container->has(JWEBuilderFactory::class));
32+
}
33+
34+
public function testJWEBuilderFactoryCanCreateAJWEBuilder()
35+
{
36+
$client = static::createClient();
37+
38+
/** @var JWEBuilderFactory $jweFactory */
39+
$jweFactory = $client->getContainer()->get(JWEBuilderFactory::class);
40+
41+
$jwe = $jweFactory->create(['RSA1_5'], ['A256GCM'], ['DEF']);
42+
43+
self::assertInstanceOf(JWEBuilder::class, $jwe);
44+
}
45+
46+
public function testJWEBuilderFromConfigurationIsAvailable()
47+
{
48+
$client = static::createClient();
49+
$container = $client->getContainer();
50+
self::assertTrue($container->has('jose.jwe_builder.builder1'));
51+
52+
$jwe = $container->get('jose.jwe_builder.builder1');
53+
self::assertInstanceOf(JWEBuilder::class, $jwe);
54+
}
55+
56+
public function testJWEBuilderFromExternalBundleExtensionIsAvailable()
57+
{
58+
$client = static::createClient();
59+
$container = $client->getContainer();
60+
self::assertTrue($container->has('jose.jwe_builder.builder2'));
61+
62+
$jwe = $container->get('jose.jwe_builder.builder2');
63+
self::assertInstanceOf(JWEBuilder::class, $jwe);
64+
}
65+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* The MIT License (MIT)
7+
*
8+
* Copyright (c) 2014-2017 Spomky-Labs
9+
*
10+
* This software may be modified and distributed under the terms
11+
* of the MIT license. See the LICENSE file for details.
12+
*/
13+
14+
namespace Jose\Bundle\Encryption\Tests;
15+
16+
use Jose\Component\Core\JWK;
17+
use Jose\Component\Encryption\JWEBuilder;
18+
use Jose\Component\Encryption\JWELoader;
19+
use Jose\Component\Encryption\Serializer\CompactSerializer;
20+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
21+
22+
/**
23+
* @group Bundle
24+
* @group Functional
25+
*/
26+
final class JWEComputationTest extends WebTestCase
27+
{
28+
public function testCreateAndLoadAToken()
29+
{
30+
$client = static::createClient();
31+
$container = $client->getContainer();
32+
33+
$jwk = JWK::create([
34+
'kty' => 'oct',
35+
'k' => '3pWc2vAZpHoV7XmCT-z2hWhdQquwQwW5a3XTojbf87c',
36+
]);
37+
38+
/** @var JWEBuilder $builder */
39+
$builder = $container->get('jose.jwe_builder.builder1');
40+
41+
/** @var JWELoader $loader */
42+
$loader = $container->get('jose.jwe_loader.loader1');
43+
44+
/** @var CompactSerializer $serializer */
45+
$serializer = $container->get(CompactSerializer::class);
46+
47+
$jwe = $builder
48+
->create()
49+
->withPayload('Hello World!')
50+
->withSharedProtectedHeaders([
51+
'alg' => 'A256KW',
52+
'enc' => 'A256CBC-HS512',
53+
'exp' => time() + 3600,
54+
])
55+
->addRecipient($jwk)
56+
->build();
57+
$token = $serializer->serialize($jwe, 0);
58+
59+
$loaded = $loader->load($token);
60+
$loaded = $loader->decryptUsingKey($loaded, $jwk, $index);
61+
self::assertEquals(0, $index);
62+
self::assertEquals('Hello World!', $loaded->getPayload());
63+
}
64+
65+
/**
66+
* @expectedException \InvalidArgumentException
67+
* @expectedExceptionMessage Unable to decrypt the JWE.
68+
*/
69+
public function testUnableToLoadAnExpiredToken()
70+
{
71+
$client = static::createClient();
72+
$container = $client->getContainer();
73+
74+
$jwk = JWK::create([
75+
'kty' => 'oct',
76+
'k' => '3pWc2vAZpHoV7XmCT-z2hWhdQquwQwW5a3XTojbf87c',
77+
]);
78+
79+
/** @var JWEBuilder $builder */
80+
$builder = $container->get('jose.jwe_builder.builder1');
81+
82+
/** @var JWELoader $loader */
83+
$loader = $container->get('jose.jwe_loader.loader1');
84+
85+
/** @var CompactSerializer $serializer */
86+
$serializer = $container->get(CompactSerializer::class);
87+
88+
$jwe = $builder
89+
->create()
90+
->withPayload('Hello World!')
91+
->withSharedProtectedHeaders([
92+
'alg' => 'A256KW',
93+
'enc' => 'A256CBC-HS512',
94+
'exp' => time() - 3600,
95+
])
96+
->addRecipient($jwk)
97+
->build();
98+
$token = $serializer->serialize($jwe, 0);
99+
100+
$loaded = $loader->load($token);
101+
$loader->decryptUsingKey($loaded, $jwk, $index);
102+
}
103+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* The MIT License (MIT)
7+
*
8+
* Copyright (c) 2014-2017 Spomky-Labs
9+
*
10+
* This software may be modified and distributed under the terms
11+
* of the MIT license. See the LICENSE file for details.
12+
*/
13+
14+
namespace Jose\Bundle\Encryption\Tests;
15+
16+
use Jose\Component\Encryption\JWELoader;
17+
use Jose\Component\Encryption\JWELoaderFactory;
18+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
19+
20+
/**
21+
* @group Bundle
22+
* @group Functional
23+
*/
24+
final class JWELoaderTest extends WebTestCase
25+
{
26+
public function testJWELoaderFactoryIsAvailable()
27+
{
28+
$client = static::createClient();
29+
$container = $client->getContainer();
30+
self::assertNotNull($container);
31+
self::assertTrue($container->has(JWELoaderFactory::class));
32+
}
33+
34+
public function testJWELoaderFactoryCanCreateAJWELoader()
35+
{
36+
$client = static::createClient();
37+
38+
/** @var JWELoaderFactory $jweFactory */
39+
$jweFactory = $client->getContainer()->get(JWELoaderFactory::class);
40+
41+
$jwe = $jweFactory->create(['RSA1_5'], ['A256GCM'], ['DEF'], ['iat', 'exp', 'nbf'], ['jwe_compact', 'jwe_json_general', 'jwe_json_flattened']);
42+
43+
self::assertInstanceOf(JWELoader::class, $jwe);
44+
}
45+
46+
public function testJWELoaderFromConfigurationIsAvailable()
47+
{
48+
$client = static::createClient();
49+
$container = $client->getContainer();
50+
self::assertTrue($container->has('jose.jwe_loader.loader1'));
51+
52+
$jwe = $container->get('jose.jwe_loader.loader1');
53+
self::assertInstanceOf(JWELoader::class, $jwe);
54+
}
55+
56+
public function testJWELoaderFromExternalBundleExtensionIsAvailable()
57+
{
58+
$client = static::createClient();
59+
$container = $client->getContainer();
60+
self::assertTrue($container->has('jose.jwe_loader.loader2'));
61+
62+
$jwe = $container->get('jose.jwe_loader.loader2');
63+
self::assertInstanceOf(JWELoader::class, $jwe);
64+
}
65+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* The MIT License (MIT)
7+
*
8+
* Copyright (c) 2014-2017 Spomky-Labs
9+
*
10+
* This software may be modified and distributed under the terms
11+
* of the MIT license. See the LICENSE file for details.
12+
*/
13+
14+
namespace Jose\Bundle\Encryption\Tests\TestBundle\DependencyInjection;
15+
16+
use Jose\Bundle\JoseFramework\Helper\ConfigurationHelper;
17+
use Symfony\Component\Config\FileLocator;
18+
use Symfony\Component\DependencyInjection\ContainerBuilder;
19+
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
20+
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
21+
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
22+
23+
/**
24+
* Class TestExtension.
25+
*/
26+
final class TestExtension extends Extension implements PrependExtensionInterface
27+
{
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function load(array $configs, ContainerBuilder $container)
32+
{
33+
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
34+
$loader->load('services.yml');
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
public function prepend(ContainerBuilder $container)
41+
{
42+
ConfigurationHelper::addJWEBuilder($container, 'builder2', ['RSA-OAEP-256'], ['A128GCM'], ['DEF'], true);
43+
ConfigurationHelper::addJWELoader($container, 'loader2', ['RSA-OAEP-256'], ['A128GCM'], ['DEF'], ['exp', 'iat', 'nbf'], ['jwe_compact', 'jwe_json_general', 'jwe_json_flattened'], true);
44+
}
45+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
services:
2+
_defaults:
3+
autowire: true
4+
autoconfigure: true
5+
public: true
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* The MIT License (MIT)
7+
*
8+
* Copyright (c) 2014-2017 Spomky-Labs
9+
*
10+
* This software may be modified and distributed under the terms
11+
* of the MIT license. See the LICENSE file for details.
12+
*/
13+
14+
namespace Jose\Bundle\Encryption\Tests\TestBundle;
15+
16+
use Symfony\Component\HttpKernel\Bundle\Bundle;
17+
18+
/**
19+
* Class TestBundle.
20+
*/
21+
final class TestBundle extends Bundle
22+
{
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
framework:
2+
test: ~
3+
secret: test
4+
session:
5+
storage_id: session.storage.mock_file
6+
router:
7+
resource: "%kernel.root_dir%/config/routing.yml"
8+
strict_requirements: ~
9+
fragments: ~
10+
http_method_override: true
11+
12+
jose:
13+
jwe_builders:
14+
builder1:
15+
is_public: true
16+
key_encryption_algorithms: ['RSA-OAEP-256', 'A256KW']
17+
content_encryption_algorithms: ['A256CBC-HS512']
18+
jwe_loaders:
19+
loader1:
20+
key_encryption_algorithms: ['RSA-OAEP-256', 'A256KW']
21+
content_encryption_algorithms: ['A256CBC-HS512']
22+
header_checkers: ['iat', 'exp', 'nbf']
23+
serializers: ['jwe_compact', 'jwe_json_general', 'jwe_json_flattened']
24+
is_public: true

src/Bundle/Encryption/Tests/config/routing.yml

Whitespace-only changes.

0 commit comments

Comments
 (0)