ProcessTest.php 10 KB

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