StoreTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. <?php
  2. /**
  3. * A test script for the OpenIDStore classes.
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * LICENSE: See the COPYING file included in this distribution.
  8. *
  9. * @package OpenID
  10. * @author JanRain, Inc. <openid@janrain.com>
  11. * @copyright 2005-2008 Janrain, Inc.
  12. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache
  13. */
  14. /**
  15. * Require classes and functions to run the Store tests.
  16. */
  17. require_once 'Auth/OpenID/Association.php';
  18. require_once 'Auth/OpenID/CryptUtil.php';
  19. require_once 'Auth/OpenID/Nonce.php';
  20. require_once 'Auth/OpenID.php';
  21. function _Auth_OpenID_mkdtemp()
  22. {
  23. if (function_exists('sys_get_temp_dir')) {
  24. $dir = sys_get_temp_dir();
  25. }
  26. else {
  27. if (strpos(PHP_OS, 'WIN') === 0) {
  28. $dir = $_ENV['TMP'];
  29. if (!isset($dir)) {
  30. $dir = 'C:\Windows\Temp';
  31. }
  32. } else {
  33. $dir = @$_ENV['TMPDIR'];
  34. if (!isset($dir)) {
  35. $dir = '/tmp';
  36. }
  37. }
  38. }
  39. return Auth_OpenID_FileStore::_mkdtemp($dir);
  40. }
  41. /**
  42. * This is the host where the SQL stores' databases should be created
  43. * and destroyed.
  44. */
  45. global $_Auth_OpenID_db_test_host;
  46. $_Auth_OpenID_db_test_host = 'dbtest';
  47. /**
  48. * Generate a sufficently unique database name so many hosts can run
  49. * SQL store tests on the server at the same time and not step on each
  50. * other.
  51. */
  52. function _Auth_OpenID_getTmpDbName()
  53. {
  54. $hostname = php_uname('n');
  55. $hostname = str_replace('.', '_', $hostname);
  56. $hostname = str_replace('-', '_', $hostname);
  57. $hostname = strtolower($hostname);
  58. return sprintf("%s_%d_%s_openid_test",
  59. $hostname,
  60. getmypid(),
  61. strval(rand(1, time())));
  62. }
  63. /**
  64. * Superclass that has methods for testing OpenID stores. Subclass this to
  65. * test your own store implementation.
  66. *
  67. * @package OpenID
  68. */
  69. class Tests_Auth_OpenID_Store extends PHPUnit_Framework_TestCase {
  70. function pass() {}
  71. /**
  72. * Prepares for the SQL store tests.
  73. */
  74. function setUp()
  75. {
  76. $this->letters = Auth_OpenID_letters;
  77. $this->digits = Auth_OpenID_digits;
  78. $this->punct = Auth_OpenID_punct;
  79. $this->allowed_nonce = $this->letters . $this->digits;
  80. $this->allowed_handle = $this->letters . $this->digits . $this->punct;
  81. }
  82. /**
  83. * Generates an association with the specified parameters.
  84. */
  85. function genAssoc($now, $issued = 0, $lifetime = 600)
  86. {
  87. $sec = Auth_OpenID_CryptUtil::randomString(20);
  88. $hdl = Auth_OpenID_CryptUtil::randomString(128, $this->allowed_handle);
  89. return new Auth_OpenID_Association($hdl, $sec, $now + $issued,
  90. $lifetime, 'HMAC-SHA1');
  91. }
  92. /**
  93. * @access private
  94. */
  95. function _checkRetrieve($store, $url, $handle, $expected, $name = null)
  96. {
  97. $retrieved_assoc = $store->getAssociation($url, $handle);
  98. if ($expected === null) {
  99. $this->assertTrue($retrieved_assoc === null);
  100. } else {
  101. $this->assertTrue($expected->equal($retrieved_assoc), $name);
  102. }
  103. }
  104. function _checkRemove($store, $url, $handle, $expected, $name = null)
  105. {
  106. $present = $store->removeAssociation($url, $handle);
  107. $this->assertTrue((!$expected && !$present) ||
  108. ($expected && $present),
  109. $name);
  110. }
  111. /**
  112. * Make sure a given store has a minimum of API compliance. Call
  113. * this function with an empty store.
  114. *
  115. * Raises AssertionError if the store does not work as expected.
  116. *
  117. * OpenIDStore -> NoneType
  118. */
  119. function _testStore($store)
  120. {
  121. // Association functions
  122. $now = time();
  123. $server_url = 'http://www.myopenid.com/openid';
  124. $assoc = $this->genAssoc($now);
  125. $this->_checkRetrieve($store, $server_url, null, null,
  126. 'Make sure that a missing association returns no result');
  127. $store->storeAssociation($server_url, $assoc);
  128. $this->_checkRetrieve($store, $server_url, null, $assoc,
  129. 'Check that after storage, getting returns the same result');
  130. $this->_checkRetrieve($store, $server_url, null, $assoc,
  131. 'more than once');
  132. $store->storeAssociation($server_url, $assoc);
  133. $this->_checkRetrieve($store, $server_url, null, $assoc,
  134. 'Storing more than once has no ill effect');
  135. // Removing an association that does not exist returns not present
  136. $this->_checkRemove($store, $server_url, $assoc->handle . 'x', false,
  137. "Remove nonexistent association (1)");
  138. // Removing an association that does not exist returns not present
  139. $this->_checkRemove($store, $server_url . 'x', $assoc->handle, false,
  140. "Remove nonexistent association (2)");
  141. // Removing an association that is present returns present
  142. $this->_checkRemove($store, $server_url, $assoc->handle, true,
  143. "Remove existent association");
  144. // but not present on subsequent calls
  145. $this->_checkRemove($store, $server_url, $assoc->handle, false,
  146. "Remove nonexistent association after removal");
  147. // Put assoc back in the store
  148. $store->storeAssociation($server_url, $assoc);
  149. // More recent and expires after assoc
  150. $assoc2 = $this->genAssoc($now, $issued = 1);
  151. $store->storeAssociation($server_url, $assoc2);
  152. $this->_checkRetrieve($store, $server_url, null, $assoc2,
  153. 'After storing an association with a different handle, but the
  154. same $server_url, the handle with the later expiration is
  155. returned.');
  156. $this->_checkRetrieve($store, $server_url, $assoc->handle, $assoc,
  157. 'We can still retrieve the older association');
  158. $this->_checkRetrieve($store, $server_url, $assoc2->handle, $assoc2,
  159. 'Plus we can retrieve the association with the later expiration
  160. explicitly');
  161. $assoc3 = $this->genAssoc($now, $issued = 2, $lifetime = 100);
  162. $store->storeAssociation($server_url, $assoc3);
  163. // More recent issued time, so assoc3 is expected.
  164. $this->_checkRetrieve($store, $server_url, null, $assoc3, "(1)");
  165. $this->_checkRetrieve($store, $server_url, $assoc->handle,
  166. $assoc, "(2)");
  167. $this->_checkRetrieve($store, $server_url, $assoc2->handle,
  168. $assoc2, "(3)");
  169. $this->_checkRetrieve($store, $server_url, $assoc3->handle,
  170. $assoc3, "(4)");
  171. $this->_checkRemove($store, $server_url, $assoc2->handle, true, "(5)");
  172. $this->_checkRetrieve($store, $server_url, null, $assoc3, "(6)");
  173. $this->_checkRetrieve($store, $server_url, $assoc->handle,
  174. $assoc, "(7)");
  175. $this->_checkRetrieve($store, $server_url, $assoc2->handle,
  176. null, "(8)");
  177. $this->_checkRetrieve($store, $server_url, $assoc3->handle,
  178. $assoc3, "(9)");
  179. $this->_checkRemove($store, $server_url, $assoc2->handle,
  180. false, "(10)");
  181. $this->_checkRemove($store, $server_url, $assoc3->handle,
  182. true, "(11)");
  183. $this->_checkRetrieve($store, $server_url, null, $assoc, "(12)");
  184. $this->_checkRetrieve($store, $server_url, $assoc->handle,
  185. $assoc, "(13)");
  186. $this->_checkRetrieve($store, $server_url, $assoc2->handle,
  187. null, "(14)");
  188. $this->_checkRetrieve($store, $server_url, $assoc3->handle,
  189. null, "(15)");
  190. $this->_checkRemove($store, $server_url, $assoc2->handle,
  191. false, "(16)");
  192. $this->_checkRemove($store, $server_url, $assoc->handle,
  193. true, "(17)");
  194. $this->_checkRemove($store, $server_url, $assoc3->handle,
  195. false, "(18)");
  196. $this->_checkRetrieve($store, $server_url, null, null, "(19)");
  197. $this->_checkRetrieve($store, $server_url, $assoc->handle,
  198. null, "(20)");
  199. $this->_checkRetrieve($store, $server_url, $assoc2->handle,
  200. null, "(21)");
  201. $this->_checkRetrieve($store, $server_url,$assoc3->handle,
  202. null, "(22)");
  203. $this->_checkRemove($store, $server_url, $assoc2->handle,
  204. false, "(23)");
  205. $this->_checkRemove($store, $server_url, $assoc->handle,
  206. false, "(24)");
  207. $this->_checkRemove($store, $server_url, $assoc3->handle,
  208. false, "(25)");
  209. // Put associations into store, for two different server URLs
  210. $assoc1 = $this->genAssoc($now);
  211. $assoc2 = $this->genAssoc($now + 2);
  212. $server_url1 = "http://one.example.com/one";
  213. $server_url2 = "http://two.localhost.localdomain/two";
  214. $store->storeAssociation($server_url1, $assoc1);
  215. $store->storeAssociation($server_url2, $assoc2);
  216. // Ask for each one, make sure we get it
  217. $this->_checkRetrieve($store, $server_url1, $assoc1->handle,
  218. $assoc1, "(26)");
  219. $this->_checkRetrieve($store, $server_url2, $assoc2->handle,
  220. $assoc2, "(27)");
  221. $store->storeAssociation($server_url1, $assoc1);
  222. $store->storeAssociation($server_url2, $assoc2);
  223. // Ask for each one, make sure we get it
  224. $this->_checkRetrieve($store, $server_url1, null,
  225. $assoc1, "(28)");
  226. $this->_checkRetrieve($store, $server_url2, null,
  227. $assoc2, "(29)");
  228. // test expired associations
  229. // assoc 1: server 1, valid
  230. // assoc 2: server 1, expired
  231. // assoc 3: server 2, expired
  232. // assoc 4: server 3, valid
  233. $assocValid1 = $this->genAssoc($now, -3600, 7200);
  234. $assocValid2 = $this->genAssoc($now, -5);
  235. $assocExpired1 = $this->genAssoc($now, -7200, 3600);
  236. $assocExpired2 = $this->genAssoc($now, -7200, 3600);
  237. if (!$store->supportsCleanup()) {
  238. return;
  239. }
  240. $store->cleanupAssociations();
  241. $store->storeAssociation($server_url . '1', $assocValid1);
  242. $store->storeAssociation($server_url . '1', $assocExpired1);
  243. $store->storeAssociation($server_url . '2', $assocExpired2);
  244. $store->storeAssociation($server_url . '3', $assocValid2);
  245. $cleaned = $store->cleanupAssociations();
  246. $this->assertEquals(2, $cleaned);
  247. }
  248. function _checkUseNonce($store, $nonce, $expected, $server_url, $msg=null)
  249. {
  250. list($stamp, $salt) = Auth_OpenID_splitNonce($nonce);
  251. $actual = $store->useNonce($server_url, $stamp, $salt);
  252. $this->assertEquals(intval($expected), intval($actual), "_checkUseNonce failed: $server_url, $msg");
  253. }
  254. function _testNonce($store)
  255. {
  256. // Nonce functions
  257. $server_url = 'http://www.myopenid.com/openid';
  258. foreach (array($server_url, '') as $url) {
  259. // Random nonce (not in store)
  260. $nonce1 = Auth_OpenID_mkNonce();
  261. // A nonce is not by default
  262. $this->_checkUseNonce($store, $nonce1, true, $url, "blergx");
  263. // Once stored, cannot be stored again
  264. $this->_checkUseNonce($store, $nonce1, false, $url, 2);
  265. // And using again has the same effect
  266. $this->_checkUseNonce($store, $nonce1, false, $url, 3);
  267. // Nonces from when the universe was an hour old should
  268. // not pass these days.
  269. $old_nonce = Auth_OpenID_mkNonce(3600);
  270. $this->_checkUseNonce($store, $old_nonce, false, $url,
  271. "Old nonce ($old_nonce) passed.");
  272. }
  273. }
  274. function _testNonceCleanup($store) {
  275. if (!$store->supportsCleanup()) {
  276. return;
  277. }
  278. $server_url = 'http://www.myopenid.com/openid';
  279. $now = time();
  280. $old_nonce1 = Auth_OpenID_mkNonce($now - 20000);
  281. $old_nonce2 = Auth_OpenID_mkNonce($now - 10000);
  282. $recent_nonce = Auth_OpenID_mkNonce($now - 600);
  283. global $Auth_OpenID_SKEW;
  284. $orig_skew = $Auth_OpenID_SKEW;
  285. $Auth_OpenID_SKEW = 0;
  286. $store->cleanupNonces();
  287. // Set SKEW high so stores will keep our nonces.
  288. $Auth_OpenID_SKEW = 100000;
  289. $params = Auth_OpenID_splitNonce($old_nonce1);
  290. array_unshift($params, $server_url);
  291. $this->assertTrue(call_user_func_array(array($store, 'useNonce'), $params));
  292. $params = Auth_OpenID_splitNonce($old_nonce2);
  293. array_unshift($params, $server_url);
  294. $this->assertTrue(call_user_func_array(array($store, 'useNonce'), $params));
  295. $params = Auth_OpenID_splitNonce($recent_nonce);
  296. array_unshift($params, $server_url);
  297. $this->assertTrue(call_user_func_array(array($store, 'useNonce'), $params));
  298. $Auth_OpenID_SKEW = 3600;
  299. $cleaned = $store->cleanupNonces();
  300. $this->assertEquals(2, $cleaned); // , "Cleaned %r nonces." % (cleaned,)
  301. $Auth_OpenID_SKEW = 100000;
  302. // A roundabout method of checking that the old nonces were
  303. // cleaned is to see if we're allowed to add them again.
  304. $params = Auth_OpenID_splitNonce($old_nonce1);
  305. array_unshift($params, $server_url);
  306. $this->assertTrue(call_user_func_array(array($store, 'useNonce'), $params));
  307. $params = Auth_OpenID_splitNonce($old_nonce2);
  308. array_unshift($params, $server_url);
  309. $this->assertTrue(call_user_func_array(array($store, 'useNonce'), $params));
  310. // The recent nonce wasn't cleaned, so it should still fail.
  311. $params = Auth_OpenID_splitNonce($recent_nonce);
  312. array_unshift($params, $server_url);
  313. $this->assertFalse(call_user_func_array(array($store, 'useNonce'), $params));
  314. $Auth_OpenID_SKEW = $orig_skew;
  315. }
  316. }
  317. /**
  318. * Class that tests all of the stores included with the OpenID library
  319. *
  320. * @package OpenID
  321. */
  322. class Tests_Auth_OpenID_Included_StoreTest extends Tests_Auth_OpenID_Store {
  323. function test_memstore()
  324. {
  325. require_once 'Tests/Auth/OpenID/MemStore.php';
  326. $store = new Tests_Auth_OpenID_MemStore();
  327. $this->_testStore($store);
  328. $this->_testNonce($store);
  329. $this->_testNonceCleanup($store);
  330. }
  331. function test_filestore()
  332. {
  333. require_once 'Auth/OpenID/FileStore.php';
  334. $temp_dir = _Auth_OpenID_mkdtemp();
  335. if (!$temp_dir) {
  336. trigger_error('Could not create temporary directory ' .
  337. 'with Auth_OpenID_FileStore::_mkdtemp',
  338. E_USER_WARNING);
  339. return null;
  340. }
  341. $store = new Auth_OpenID_FileStore($temp_dir);
  342. $this->_testStore($store);
  343. $this->_testNonce($store);
  344. $this->_testNonceCleanup($store);
  345. $store->destroy();
  346. }
  347. function test_postgresqlstore()
  348. {
  349. // If the postgres extension isn't loaded or loadable, succeed
  350. // because we can't run the test.
  351. if (!(extension_loaded('pgsql')) ||
  352. !(@include_once 'DB.php')) {
  353. print "(not testing PostGreSQL store)";
  354. $this->pass();
  355. return;
  356. }
  357. require_once 'Auth/OpenID/PostgreSQLStore.php';
  358. global $_Auth_OpenID_db_test_host;
  359. $temp_db_name = _Auth_OpenID_getTmpDbName();
  360. $connect_db_name = 'test_master';
  361. $dsn = array(
  362. 'phptype' => 'pgsql',
  363. 'username' => 'openid_test',
  364. 'password' => '',
  365. 'hostspec' => $_Auth_OpenID_db_test_host,
  366. 'database' => $connect_db_name
  367. );
  368. $allowed_failures = 5;
  369. $result = null;
  370. $sleep_time = 1.0;
  371. $sql = sprintf("CREATE DATABASE %s", $temp_db_name);
  372. for ($failures = 0; $failures < $allowed_failures; $failures++) {
  373. $template_db =& DB::connect($dsn);
  374. if (PEAR::isError($template_db)) {
  375. $result =& $template_db;
  376. } else {
  377. // Try to create the test database.
  378. $result = $template_db->query($sql);
  379. $template_db->disconnect();
  380. unset($template_db);
  381. if (!PEAR::isError($result)) {
  382. break;
  383. }
  384. }
  385. $sleep_time *= ((mt_rand(1, 100) / 100.0) + 1.5);
  386. print "Failed to create database $temp_db_name.\n".
  387. "Waiting $sleep_time before trying again\n";
  388. $int_sleep = floor($sleep_time);
  389. $frac_sleep = $sleep_time - $int_sleep;
  390. sleep($int_sleep);
  391. usleep($frac_sleep * 1000000.0);
  392. }
  393. if ($failures == $allowed_failures) {
  394. $this->pass("Temporary database creation failed after $failures ".
  395. " tries ('$temp_db_name'): " . $result->getMessage());
  396. return;
  397. }
  398. // Disconnect from template1 and reconnect to the temporary
  399. // testing database.
  400. $dsn['database'] = $temp_db_name;
  401. $db =& DB::connect($dsn);
  402. if (PEAR::isError($db)) {
  403. $this->fail("Temporary database connection failed " .
  404. " ('$temp_db_name'): " . $db->getMessage());
  405. return;
  406. }
  407. $store = new Auth_OpenID_PostgreSQLStore($db);
  408. $this->assertFalse($store->tableExists($store->nonces_table_name));
  409. $this->assertFalse($store->tableExists($store->associations_table_name));
  410. $store->createTables();
  411. $this->assertTrue($store->tableExists($store->nonces_table_name));
  412. $this->assertTrue($store->tableExists($store->associations_table_name));
  413. $this->_testStore($store);
  414. $this->_testNonce($store);
  415. $this->_testNonceCleanup($store);
  416. $db->disconnect();
  417. unset($db);
  418. // Connect to template1 again so we can drop the temporary
  419. // database.
  420. $dsn['database'] = $connect_db_name;
  421. $template_db =& DB::connect($dsn);
  422. if (PEAR::isError($template_db)) {
  423. $this->fail("Template database connection (to drop " .
  424. "temporary database) failed: " .
  425. $template_db->getMessage());
  426. return;
  427. }
  428. $result = $template_db->query(sprintf("DROP DATABASE %s",
  429. $temp_db_name));
  430. if (PEAR::isError($result)) {
  431. $this->fail("Dropping temporary database failed: " .
  432. $result->getMessage());
  433. return;
  434. }
  435. $template_db->disconnect();
  436. unset($template_db);
  437. }
  438. function test_sqlitestore()
  439. {
  440. // If the sqlite extension isn't loaded or loadable, succeed
  441. // because we can't run the test.
  442. if (!(extension_loaded('sqlite')) ||
  443. !(@include_once 'DB.php')) {
  444. print "(not testing SQLite store)";
  445. $this->pass();
  446. return;
  447. }
  448. require_once 'Auth/OpenID/SQLiteStore.php';
  449. $temp_dir = _Auth_OpenID_mkdtemp();
  450. if (!$temp_dir) {
  451. trigger_error('Could not create temporary directory ' .
  452. 'with Auth_OpenID_FileStore::_mkdtemp',
  453. E_USER_WARNING);
  454. return null;
  455. }
  456. $dsn = 'sqlite:///' . urlencode($temp_dir) . '/php_openid_storetest.db';
  457. $db =& DB::connect($dsn);
  458. if (PEAR::isError($db)) {
  459. $this->pass("SQLite database connection failed: " .
  460. $db->getMessage());
  461. } else {
  462. $store = new Auth_OpenID_SQLiteStore($db);
  463. $this->assertTrue($store->createTables(), "Table creation failed");
  464. $this->_testStore($store);
  465. $this->_testNonce($store);
  466. $this->_testNonceCleanup($store);
  467. }
  468. $db->disconnect();
  469. unset($db);
  470. unset($store);
  471. unlink($temp_dir . '/php_openid_storetest.db');
  472. rmdir($temp_dir);
  473. }
  474. function test_mysqlstore()
  475. {
  476. // If the mysql extension isn't loaded or loadable, succeed
  477. // because we can't run the test.
  478. if (!(extension_loaded('mysql')) ||
  479. !(@include_once 'DB.php')) {
  480. print "(not testing MySQL store)";
  481. $this->pass();
  482. return;
  483. }
  484. require_once 'Auth/OpenID/MySQLStore.php';
  485. global $_Auth_OpenID_db_test_host;
  486. $dsn = array(
  487. 'phptype' => 'mysql',
  488. 'username' => 'openid_test',
  489. 'password' => '',
  490. 'hostspec' => $_Auth_OpenID_db_test_host
  491. );
  492. $db =& DB::connect($dsn);
  493. if (PEAR::isError($db)) {
  494. print "MySQL database connection failed: " .
  495. $db->getMessage();
  496. $this->pass();
  497. return;
  498. }
  499. $temp_db_name = _Auth_OpenID_getTmpDbName();
  500. $result = $db->query("CREATE DATABASE $temp_db_name");
  501. if (PEAR::isError($result)) {
  502. $this->pass("Error creating MySQL temporary database: " .
  503. $result->getMessage());
  504. return;
  505. }
  506. $db->query("USE $temp_db_name");
  507. $store = new Auth_OpenID_MySQLStore($db);
  508. $store->createTables();
  509. $this->_testStore($store);
  510. $this->_testNonce($store);
  511. $this->_testNonceCleanup($store);
  512. $db->query("DROP DATABASE $temp_db_name");
  513. }
  514. function test_mdb2store()
  515. {
  516. // The MDB2 test can use any database engine. MySQL is chosen
  517. // arbitrarily.
  518. if (!(extension_loaded('mysql') ||
  519. (function_exists('dl') && @dl('mysql.' . PHP_SHLIB_SUFFIX))) ||
  520. !(@include_once 'MDB2.php')) {
  521. print "(not testing MDB2 store)";
  522. $this->pass();
  523. return;
  524. }
  525. require_once 'Auth/OpenID/MDB2Store.php';
  526. global $_Auth_OpenID_db_test_host;
  527. $dsn = array(
  528. 'phptype' => 'mysql',
  529. 'username' => 'openid_test',
  530. 'password' => '',
  531. 'hostspec' => $_Auth_OpenID_db_test_host
  532. );
  533. $db =& MDB2::connect($dsn);
  534. if (PEAR::isError($db)) {
  535. print "MySQL database connection failed: " .
  536. $db->getMessage();
  537. $this->pass();
  538. return;
  539. }
  540. $temp_db_name = _Auth_OpenID_getTmpDbName();
  541. $result = $db->query("CREATE DATABASE $temp_db_name");
  542. if (PEAR::isError($result)) {
  543. $this->pass("Error creating MySQL temporary database: " .
  544. $result->getMessage());
  545. return;
  546. }
  547. $db->query("USE $temp_db_name");
  548. $store =& new Auth_OpenID_MDB2Store($db);
  549. if (!$store->createTables()) {
  550. $this->fail("Failed to create tables");
  551. return;
  552. }
  553. $this->_testStore($store);
  554. $this->_testNonce($store);
  555. $this->_testNonceCleanup($store);
  556. $db->query("DROP DATABASE $temp_db_name");
  557. }
  558. }
  559. /**
  560. * This is the host that the store test will use
  561. */
  562. global $_Auth_OpenID_memcache_test_host;
  563. $_Auth_OpenID_memcache_test_host = 'localhost';
  564. class Tests_Auth_OpenID_MemcachedStore_Test extends Tests_Auth_OpenID_Store {
  565. function test_memcache()
  566. {
  567. // If the memcache extension isn't loaded or loadable, succeed
  568. // because we can't run the test.
  569. if (!extension_loaded('memcache')) {
  570. print "(skipping memcache store tests)";
  571. $this->pass();
  572. return;
  573. }
  574. require_once 'Auth/OpenID/MemcachedStore.php';
  575. global $_Auth_OpenID_memcache_test_host;
  576. $memcached = new Memcache();
  577. if (!$memcached->connect($_Auth_OpenID_memcache_test_host)) {
  578. print "(skipping memcache store tests - couldn't connect)";
  579. $this->pass();
  580. } else {
  581. $store = new Auth_OpenID_MemcachedStore($memcached);
  582. $this->_testStore($store);
  583. $this->_testNonce($store);
  584. $this->_testNonceCleanup($store);
  585. $memcached->close();
  586. }
  587. }
  588. }
  589. class Tests_Auth_OpenID_StoreTest extends PHPUnit_Framework_TestSuite {
  590. function getName()
  591. {
  592. return "Tests_Auth_OpenID_StoreTest";
  593. }
  594. function Tests_Auth_OpenID_StoreTest()
  595. {
  596. $this->addTestSuite('Tests_Auth_OpenID_Included_StoreTest');
  597. $this->addTestSuite('Tests_Auth_OpenID_MemcachedStore_Test');
  598. }
  599. }