ProcessTest.php 10.0 KB

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