diff --git a/tests/JWTTest.php b/tests/JWTTest.php index 2516ec0d..46ea7ef0 100644 --- a/tests/JWTTest.php +++ b/tests/JWTTest.php @@ -19,12 +19,6 @@ public function setExpectedException($exceptionName, $message = '', $code = null } } - public function testEncodeDecode() - { - $msg = JWT::encode('abc', 'my_key'); - $this->assertEquals(JWT::decode($msg, 'my_key', array('HS256')), 'abc'); - } - public function testDecodeFromPython() { $msg = 'eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.Iio6aHR0cDovL2FwcGxpY2F0aW9uL2NsaWNreT9ibGFoPTEuMjMmZi5vbz00NTYgQUMwMDAgMTIzIg.E_U8X2YpMT5K1cEiT_3-IvBYfrdIFIeVYeOqre_Z5Cg'; @@ -217,18 +211,6 @@ public function testEmptyKeyFails() JWT::decode($encoded, '', array('HS256')); } - public function testRSEncodeDecode() - { - $privKey = openssl_pkey_new(array('digest_alg' => 'sha256', - 'private_key_bits' => 1024, - 'private_key_type' => OPENSSL_KEYTYPE_RSA)); - $msg = JWT::encode('abc', $privKey, 'RS256'); - $pubKey = openssl_pkey_get_details($privKey); - $pubKey = $pubKey['key']; - $decoded = JWT::decode($msg, $pubKey, array('RS256')); - $this->assertEquals($decoded, 'abc'); - } - public function testKIDChooser() { $keys = array('1' => 'my_key', '2' => 'my_key2'); @@ -285,35 +267,46 @@ public function testInvalidSignatureEncoding() JWT::decode($msg, 'secret', array('HS256')); } - /** - * @runInSeparateProcess - */ - public function testEncodeAndDecodeEcdsaToken() + public function testHSEncodeDecode() { - $privateKey = file_get_contents(__DIR__ . '/ecdsa-private.pem'); - $payload = array('foo' => 'bar'); - $encoded = JWT::encode($payload, $privateKey, 'ES256'); - - // Verify decoding succeeds - $publicKey = file_get_contents(__DIR__ . '/ecdsa-public.pem'); - $decoded = JWT::decode($encoded, $publicKey, array('ES256')); + $msg = JWT::encode('abc', 'my_key'); + $this->assertEquals(JWT::decode($msg, 'my_key', array('HS256')), 'abc'); + } - $this->assertEquals('bar', $decoded->foo); + public function testRSEncodeDecode() + { + $privKey = openssl_pkey_new(array('digest_alg' => 'sha256', + 'private_key_bits' => 1024, + 'private_key_type' => OPENSSL_KEYTYPE_RSA)); + $msg = JWT::encode('abc', $privKey, 'RS256'); + $pubKey = openssl_pkey_get_details($privKey); + $pubKey = $pubKey['key']; + $decoded = JWT::decode($msg, $pubKey, array('RS256')); + $this->assertEquals($decoded, 'abc'); } /** * @runInSeparateProcess + * @dataProvider provideEncodeDecode */ - public function testEncodeAndDecodeEcdsa384Token() + public function testEncodeDecode($privateKeyFile, $publicKeyFile, $alg) { - $privateKey = file_get_contents(__DIR__ . '/ecdsa384-private.pem'); + $privateKey = file_get_contents($privateKeyFile); $payload = array('foo' => 'bar'); - $encoded = JWT::encode($payload, $privateKey, 'ES384'); + $encoded = JWT::encode($payload, $privateKey, $alg); // Verify decoding succeeds - $publicKey = file_get_contents(__DIR__ . '/ecdsa384-public.pem'); - $decoded = JWT::decode($encoded, $publicKey, array('ES384')); + $publicKey = file_get_contents($publicKeyFile); + $decoded = JWT::decode($encoded, $publicKey, array($alg)); $this->assertEquals('bar', $decoded->foo); } + + public function provideEncodeDecode() + { + return array( + array(__DIR__ . '/ecdsa-private.pem', __DIR__ . '/ecdsa-public.pem', 'ES256'), + array(__DIR__ . '/ecdsa384-private.pem', __DIR__ . '/ecdsa384-public.pem', 'ES384'), + ); + } }