ZF-5514: implemented patch and adapted test backend
[zend/radio.git] / tests / Zend / InfoCard / ProcessTest.php
blobb53497b45249991063b9330de3dc2afd846cedb3
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
15 * @category Zend
16 * @package Zend_InfoCard
17 * @subpackage UnitTests
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id$
23 // Call Zend_InfoCard_ProcessTest::main() if this source file is executed directly.
24 if (!defined("PHPUnit_MAIN_METHOD")) {
25 define("PHPUnit_MAIN_METHOD", "Zend_InfoCard_ProcessTest::main");
28 /**
29 * Test helper
31 require_once dirname(__FILE__) . '/../../TestHelper.php';
33 require_once "PHPUnit/Framework/TestCase.php";
34 require_once "PHPUnit/Framework/TestSuite.php";
36 require_once 'Zend/InfoCard.php';
37 require_once 'Zend/InfoCard/Adapter/Default.php';
39 /**
40 * @category Zend
41 * @package Zend_InfoCard
42 * @subpackage UnitTests
43 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
44 * @license http://framework.zend.com/license/new-bsd New BSD License
45 * @group Zend_InfoCard
47 class Zend_InfoCard_ProcessTest extends PHPUnit_Framework_TestCase
49 protected $_xmlDocument;
51 /**
52 * Runs the test methods of this class.
54 * @access public
55 * @static
57 public static function main()
59 require_once "PHPUnit/TextUI/TestRunner.php";
61 $suite = new PHPUnit_Framework_TestSuite("Zend_InfoCard_ProcessTest");
62 $result = PHPUnit_TextUI_TestRunner::run($suite);
65 public function setUp()
67 $this->tokenDocument = dirname(__FILE__) . '/_files/encryptedtoken.xml';
68 $this->sslPubKey = dirname(__FILE__) . '/_files/ssl_pub.cert';
69 $this->sslPrvKey = dirname(__FILE__) . '/_files/ssl_private.cert';
70 $this->loadXmlDocument();
71 $_SERVER['SERVER_NAME'] = "192.168.1.105";
72 $_SERVER['SERVER_PORT'] = 80;
75 public function loadXmlDocument()
77 $this->_xmlDocument = file_get_contents($this->tokenDocument);
80 public function testCertificatePairs()
82 try {
83 $infoCard = new Zend_InfoCard();
84 } catch (Zend_InfoCard_Exception $e) {
85 $message = $e->getMessage();
86 if (preg_match('/requires.+mcrypt/', $message)) {
87 $this->markTestSkipped($message);
88 } else {
89 throw $e;
93 $key_id = $infoCard->addCertificatePair($this->sslPrvKey, $this->sslPubKey);
95 $this->assertTrue((bool)$key_id);
97 $key_pair = $infoCard->getCertificatePair($key_id);
99 $this->assertTrue(!empty($key_pair['public']));
100 $this->assertTrue(!empty($key_pair['private']));
101 $this->assertTrue(!empty($key_pair['type_uri']));
103 $infoCard->removeCertificatePair($key_id);
105 $failed = false;
107 try {
108 $key_pair = $infoCard->getCertificatePair($key_id);
109 } catch(Zend_InfoCard_Exception $e) {
110 $failed = true;
113 $this->assertTrue($failed);
115 try {
116 $infoCard->addCertificatePair("I don't exist", "I don't exist");
117 } catch(Zend_InfoCard_Exception $e) {
118 $this->assertTrue(true);
119 } catch(Exception $e) {
120 $this->assertFalse(true);
123 $key_id = $infoCard->addCertificatePair($this->sslPrvKey, $this->sslPubKey, Zend_InfoCard_Cipher::ENC_RSA_OAEP_MGF1P, "foo");
125 try {
126 $key_id = $infoCard->addCertificatePair($this->sslPrvKey, $this->sslPubKey, Zend_InfoCard_Cipher::ENC_RSA_OAEP_MGF1P, "foo");
127 } catch(Zend_InfoCard_Exception $e) {
128 $this->assertTrue(true);
129 } catch(Exception $e) {
130 $this->assertFalse(true);
133 $this->assertTrue(!empty($key_id));
135 try {
136 $infoCard->removeCertificatePair($key_id);
137 $infoCard->addCertificatePair($this->sslPrvKey, $this->sslPubKey, "Doesn't Exist", "foo");
138 } catch(Zend_InfoCard_Exception $e) {
139 $this->assertTrue(true);
140 } catch(Exception $e) {
141 $this->assertFalse(true);
145 public function testStandAloneProcess()
147 if (version_compare(PHP_VERSION, '5.2.0', '<')) {
148 $this->markTestSkipped('DOMDocument::C14N() not available until PHP 5.2.0');
151 try {
152 $infoCard = new Zend_InfoCard();
153 } catch (Zend_InfoCard_Exception $e) {
154 $message = $e->getMessage();
155 if (preg_match('/requires.+mcrypt/', $message)) {
156 $this->markTestSkipped($message);
157 } else {
158 throw $e;
162 $infoCard->addCertificatePair($this->sslPrvKey, $this->sslPubKey);
164 $claims = $infoCard->process($this->_xmlDocument);
166 $this->assertTrue($claims instanceof Zend_InfoCard_Claims);
169 public function testPlugins()
171 if (version_compare(PHP_VERSION, '5.2.0', '<')) {
172 $this->markTestSkipped('DOMDocument::C14N() not available until PHP 5.2.0');
175 $adapter = new _Zend_InfoCard_Test_Adapter();
177 try {
178 $infoCard = new Zend_InfoCard();
179 } catch (Zend_InfoCard_Exception $e) {
180 $message = $e->getMessage();
181 if (preg_match('/requires.+mcrypt/', $message)) {
182 $this->markTestSkipped($message);
183 } else {
184 throw $e;
188 $infoCard->setAdapter($adapter);
190 $result = $infoCard->getAdapter() instanceof Zend_InfoCard_Adapter_Interface;
192 $this->assertTrue($result);
193 $this->assertTrue($infoCard->getAdapter() instanceof _Zend_InfoCard_Test_Adapter);
195 $infoCard->addCertificatePair($this->sslPrvKey, $this->sslPubKey);
197 $claims = $infoCard->process($this->_xmlDocument);
199 $pki_object = new Zend_InfoCard_Cipher_Pki_Adapter_Rsa(Zend_InfoCard_Cipher_Pki_Adapter_Abstract::NO_PADDING);
201 $infoCard->setPkiCipherObject($pki_object);
203 $this->assertTrue($pki_object === $infoCard->getPkiCipherObject());
205 $sym_object = new Zend_InfoCard_Cipher_Symmetric_Adapter_Aes256cbc();
207 $infoCard->setSymCipherObject($sym_object);
209 $this->assertTrue($sym_object === $infoCard->getSymCipherObject());
212 public function testClaims()
214 if (version_compare(PHP_VERSION, '5.2.0', '<')) {
215 $this->markTestSkipped('DOMDocument::C14N() not available until PHP 5.2.0');
218 try {
219 $infoCard = new Zend_InfoCard();
220 } catch (Zend_InfoCard_Exception $e) {
221 $message = $e->getMessage();
222 if (preg_match('/requires.+mcrypt/', $message)) {
223 $this->markTestSkipped($message);
224 } else {
225 throw $e;
229 $infoCard->addCertificatePair($this->sslPrvKey, $this->sslPubKey);
231 $claims = $infoCard->process($this->_xmlDocument);
233 $this->assertTrue($claims instanceof Zend_InfoCard_Claims);
235 $this->assertFalse($claims->isValid());
237 $this->assertSame($claims->getCode(), Zend_InfoCard_Claims::RESULT_VALIDATION_FAILURE);
239 $errormsg = $claims->getErrorMsg();
240 $this->assertTrue(!empty($errormsg));
243 @$claims->forceValid();
245 $this->assertTrue($claims->isValid());
247 $this->assertSame($claims->emailaddress, "john@zend.com");
248 $this->assertSame($claims->givenname, "John");
249 $this->assertSame($claims->surname, "Coggeshall");
250 $this->assertSame($claims->getCardID(), "rW1/y9BuncoBK4WSipF2hHYParxxgMHk6ANBrhz1Zr4=");
251 $this->assertSame($claims->getClaim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"), "john@zend.com");
252 $this->assertSame($claims->getDefaultNamespace(), "http://schemas.xmlsoap.org/ws/2005/05/identity/claims");
254 try {
255 unset($claims->givenname);
256 } catch(Zend_InfoCard_Exception $e) {
258 } catch(Exception $e) {
259 $this->assertFalse(true);
263 try {
264 $claims->givenname = "Test";
265 } catch(Zend_InfoCard_Exception $e) {
267 } catch(Exception $e) {
268 $this->assertFalse(true);
271 $this->assertTrue(isset($claims->givenname));
274 public function testDefaultAdapter()
276 $adapter = new Zend_InfoCard_Adapter_Default();
278 $this->assertTrue($adapter->storeAssertion(1, 2, array(3)));
279 $this->assertFalse($adapter->retrieveAssertion(1, 2));
280 $this->assertTrue(is_null($adapter->removeAssertion(1, 2)));
283 public function testTransforms()
285 $trans = new Zend_InfoCard_Xml_Security_Transform();
287 try {
288 $trans->addTransform("foo");
289 $this->fail("Expected Exception Not Thrown");
290 } catch(Exception $e) {
291 /* yay */
294 $this->assertTrue(is_array($trans->getTransformList()));
299 class _Zend_InfoCard_Test_Adapter
300 extends PHPUnit_Framework_TestCase
301 implements Zend_InfoCard_Adapter_Interface
303 public function storeAssertion($assertionURI, $assertionID, $conditions)
305 $this->assertTrue(!empty($assertionURI));
306 $this->assertTrue(!empty($assertionID));
307 $this->assertTrue(!empty($conditions));
308 return true;
311 public function retrieveAssertion($assertionURI, $assertionID)
313 $this->assertTrue(!empty($assertionURI));
314 $this->assertTrue(!empty($assertionID));
315 return false;
318 public function removeAssertion($asserionURI, $assertionID)
320 $this->assertTrue(!empty($assertionURI));
321 $this->asserTrue(!empty($assertionID));
325 // Call Zend_InfoCard_ProcessTest::main() if this source file is executed directly.
326 if (PHPUnit_MAIN_METHOD == "Zend_InfoCard_ProcessTest::main") {
327 Zend_InfoCard_ProcessTest::main();