ProcessTest.php 10 KB

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