2
0

ProcessTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  15. * @category Zend
  16. * @package Zend_InfoCard
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. // Call Zend_InfoCard_ProcessTest::main() if this source file is executed directly.
  22. if (!defined("PHPUnit_MAIN_METHOD")) {
  23. define("PHPUnit_MAIN_METHOD", "Zend_InfoCard_ProcessTest::main");
  24. }
  25. /**
  26. * Test helper
  27. */
  28. require_once dirname(__FILE__) . '/../../TestHelper.php';
  29. require_once "PHPUnit/Framework/TestCase.php";
  30. require_once "PHPUnit/Framework/TestSuite.php";
  31. require_once 'Zend/InfoCard.php';
  32. require_once 'Zend/InfoCard/Adapter/Default.php';
  33. class Zend_InfoCard_ProcessTest extends PHPUnit_Framework_TestCase
  34. {
  35. protected $_xmlDocument;
  36. /**
  37. * Runs the test methods of this class.
  38. *
  39. * @access public
  40. * @static
  41. */
  42. public static function main()
  43. {
  44. require_once "PHPUnit/TextUI/TestRunner.php";
  45. $suite = new PHPUnit_Framework_TestSuite("Zend_InfoCard_ProcessTest");
  46. $result = PHPUnit_TextUI_TestRunner::run($suite);
  47. }
  48. public function setUp()
  49. {
  50. $this->tokenDocument = dirname(__FILE__) . '/_files/encryptedtoken.xml';
  51. $this->sslPubKey = dirname(__FILE__) . '/_files/ssl_pub.cert';
  52. $this->sslPrvKey = dirname(__FILE__) . '/_files/ssl_private.cert';
  53. $this->loadXmlDocument();
  54. $_SERVER['SERVER_NAME'] = "192.168.1.105";
  55. $_SERVER['SERVER_PORT'] = 80;
  56. }
  57. public function loadXmlDocument()
  58. {
  59. $this->_xmlDocument = file_get_contents($this->tokenDocument);
  60. }
  61. public function testCertificatePairs()
  62. {
  63. try {
  64. $infoCard = new Zend_InfoCard();
  65. } catch (Zend_InfoCard_Exception $e) {
  66. $message = $e->getMessage();
  67. if (preg_match('/requires.+mcrypt/', $message)) {
  68. $this->markTestSkipped($message);
  69. } else {
  70. throw $e;
  71. }
  72. }
  73. $key_id = $infoCard->addCertificatePair($this->sslPrvKey, $this->sslPubKey);
  74. $this->assertTrue((bool)$key_id);
  75. $key_pair = $infoCard->getCertificatePair($key_id);
  76. $this->assertTrue(!empty($key_pair['public']));
  77. $this->assertTrue(!empty($key_pair['private']));
  78. $this->assertTrue(!empty($key_pair['type_uri']));
  79. $infoCard->removeCertificatePair($key_id);
  80. $failed = false;
  81. try {
  82. $key_pair = $infoCard->getCertificatePair($key_id);
  83. } catch(Zend_InfoCard_Exception $e) {
  84. $failed = true;
  85. }
  86. $this->assertTrue($failed);
  87. try {
  88. $infoCard->addCertificatePair("I don't exist", "I don't exist");
  89. } catch(Zend_InfoCard_Exception $e) {
  90. $this->assertTrue(true);
  91. } catch(Exception $e) {
  92. $this->assertFalse(true);
  93. }
  94. $key_id = $infoCard->addCertificatePair($this->sslPrvKey, $this->sslPubKey, Zend_InfoCard_Cipher::ENC_RSA_OAEP_MGF1P, "foo");
  95. try {
  96. $key_id = $infoCard->addCertificatePair($this->sslPrvKey, $this->sslPubKey, Zend_InfoCard_Cipher::ENC_RSA_OAEP_MGF1P, "foo");
  97. } catch(Zend_InfoCard_Exception $e) {
  98. $this->assertTrue(true);
  99. } catch(Exception $e) {
  100. $this->assertFalse(true);
  101. }
  102. $this->assertTrue(!empty($key_id));
  103. try {
  104. $infoCard->removeCertificatePair($key_id);
  105. $infoCard->addCertificatePair($this->sslPrvKey, $this->sslPubKey, "Doesn't Exist", "foo");
  106. } catch(Zend_InfoCard_Exception $e) {
  107. $this->assertTrue(true);
  108. } catch(Exception $e) {
  109. $this->assertFalse(true);
  110. }
  111. }
  112. public function testStandAloneProcess()
  113. {
  114. if (version_compare(PHP_VERSION, '5.2.0', '<')) {
  115. $this->markTestSkipped('DOMDocument::C14N() not available until PHP 5.2.0');
  116. }
  117. try {
  118. $infoCard = new Zend_InfoCard();
  119. } catch (Zend_InfoCard_Exception $e) {
  120. $message = $e->getMessage();
  121. if (preg_match('/requires.+mcrypt/', $message)) {
  122. $this->markTestSkipped($message);
  123. } else {
  124. throw $e;
  125. }
  126. }
  127. $infoCard->addCertificatePair($this->sslPrvKey, $this->sslPubKey);
  128. $claims = $infoCard->process($this->_xmlDocument);
  129. $this->assertTrue($claims instanceof Zend_InfoCard_Claims);
  130. }
  131. public function testPlugins()
  132. {
  133. if (version_compare(PHP_VERSION, '5.2.0', '<')) {
  134. $this->markTestSkipped('DOMDocument::C14N() not available until PHP 5.2.0');
  135. }
  136. $adapter = new _Zend_InfoCard_Test_Adapter();
  137. try {
  138. $infoCard = new Zend_InfoCard();
  139. } catch (Zend_InfoCard_Exception $e) {
  140. $message = $e->getMessage();
  141. if (preg_match('/requires.+mcrypt/', $message)) {
  142. $this->markTestSkipped($message);
  143. } else {
  144. throw $e;
  145. }
  146. }
  147. $infoCard->setAdapter($adapter);
  148. $result = $infoCard->getAdapter() instanceof Zend_InfoCard_Adapter_Interface;
  149. $this->assertTrue($result);
  150. $this->assertTrue($infoCard->getAdapter() instanceof _Zend_InfoCard_Test_Adapter);
  151. $infoCard->addCertificatePair($this->sslPrvKey, $this->sslPubKey);
  152. $claims = $infoCard->process($this->_xmlDocument);
  153. $pki_object = new Zend_InfoCard_Cipher_Pki_Adapter_Rsa(Zend_InfoCard_Cipher_Pki_Adapter_Abstract::NO_PADDING);
  154. $infoCard->setPkiCipherObject($pki_object);
  155. $this->assertTrue($pki_object === $infoCard->getPkiCipherObject());
  156. $sym_object = new Zend_InfoCard_Cipher_Symmetric_Adapter_Aes256cbc();
  157. $infoCard->setSymCipherObject($sym_object);
  158. $this->assertTrue($sym_object === $infoCard->getSymCipherObject());
  159. }
  160. public function testClaims()
  161. {
  162. if (version_compare(PHP_VERSION, '5.2.0', '<')) {
  163. $this->markTestSkipped('DOMDocument::C14N() not available until PHP 5.2.0');
  164. }
  165. try {
  166. $infoCard = new Zend_InfoCard();
  167. } catch (Zend_InfoCard_Exception $e) {
  168. $message = $e->getMessage();
  169. if (preg_match('/requires.+mcrypt/', $message)) {
  170. $this->markTestSkipped($message);
  171. } else {
  172. throw $e;
  173. }
  174. }
  175. $infoCard->addCertificatePair($this->sslPrvKey, $this->sslPubKey);
  176. $claims = $infoCard->process($this->_xmlDocument);
  177. $this->assertTrue($claims instanceof Zend_InfoCard_Claims);
  178. $this->assertFalse($claims->isValid());
  179. $this->assertSame($claims->getCode(), Zend_InfoCard_Claims::RESULT_VALIDATION_FAILURE);
  180. $errormsg = $claims->getErrorMsg();
  181. $this->assertTrue(!empty($errormsg));
  182. @$claims->forceValid();
  183. $this->assertTrue($claims->isValid());
  184. $this->assertSame($claims->emailaddress, "john@zend.com");
  185. $this->assertSame($claims->givenname, "John");
  186. $this->assertSame($claims->surname, "Coggeshall");
  187. $this->assertSame($claims->getCardID(), "rW1/y9BuncoBK4WSipF2hHYParxxgMHk6ANBrhz1Zr4=");
  188. $this->assertSame($claims->getClaim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"), "john@zend.com");
  189. $this->assertSame($claims->getDefaultNamespace(), "http://schemas.xmlsoap.org/ws/2005/05/identity/claims");
  190. try {
  191. unset($claims->givenname);
  192. } catch(Zend_InfoCard_Exception $e) {
  193. } catch(Exception $e) {
  194. $this->assertFalse(true);
  195. }
  196. try {
  197. $claims->givenname = "Test";
  198. } catch(Zend_InfoCard_Exception $e) {
  199. } catch(Exception $e) {
  200. $this->assertFalse(true);
  201. }
  202. $this->assertTrue(isset($claims->givenname));
  203. }
  204. public function testDefaultAdapter()
  205. {
  206. $adapter = new Zend_InfoCard_Adapter_Default();
  207. $this->assertTrue($adapter->storeAssertion(1, 2, array(3)));
  208. $this->assertFalse($adapter->retrieveAssertion(1, 2));
  209. $this->assertTrue(is_null($adapter->removeAssertion(1, 2)));
  210. }
  211. public function testTransforms()
  212. {
  213. $trans = new Zend_InfoCard_Xml_Security_Transform();
  214. try {
  215. $trans->addTransform("foo");
  216. $this->fail("Expected Exception Not Thrown");
  217. } catch(Exception $e) {
  218. /* yay */
  219. }
  220. $this->assertTrue(is_array($trans->getTransformList()));
  221. }
  222. }
  223. class _Zend_InfoCard_Test_Adapter
  224. extends PHPUnit_Framework_TestCase
  225. implements Zend_InfoCard_Adapter_Interface
  226. {
  227. public function storeAssertion($assertionURI, $assertionID, $conditions)
  228. {
  229. $this->assertTrue(!empty($assertionURI));
  230. $this->assertTrue(!empty($assertionID));
  231. $this->assertTrue(!empty($conditions));
  232. return true;
  233. }
  234. public function retrieveAssertion($assertionURI, $assertionID)
  235. {
  236. $this->assertTrue(!empty($assertionURI));
  237. $this->assertTrue(!empty($assertionID));
  238. return false;
  239. }
  240. public function removeAssertion($asserionURI, $assertionID)
  241. {
  242. $this->assertTrue(!empty($assertionURI));
  243. $this->asserTrue(!empty($assertionID));
  244. }
  245. }
  246. // Call Zend_InfoCard_ProcessTest::main() if this source file is executed directly.
  247. if (PHPUnit_MAIN_METHOD == "Zend_InfoCard_ProcessTest::main") {
  248. Zend_InfoCard_ProcessTest::main();
  249. }