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