/Tests/Auth/OpenID/Included/StoreTest.php

https://github.com/easybib/php-openid · PHP · 298 lines · 215 code · 62 blank · 21 comment · 22 complexity · df6b5d4cc04fbb25a24341c052f190d1 MD5 · raw file

  1. <?php
  2. /**
  3. * Tests_Auth_OpenID_Store
  4. */
  5. require_once dirname(__FILE__) . '/../Store.php';
  6. /**
  7. * Class that tests all of the stores included with the OpenID library
  8. *
  9. * @package OpenID
  10. */
  11. class Tests_Auth_OpenID_Included_StoreTest extends Tests_Auth_OpenID_Store {
  12. function test_memstore()
  13. {
  14. require_once 'Tests/Auth/OpenID/MemStore.php';
  15. $store = new Tests_Auth_OpenID_MemStore();
  16. $this->_testStore($store);
  17. $this->_testNonce($store);
  18. $this->_testNonceCleanup($store);
  19. }
  20. function test_filestore()
  21. {
  22. require_once 'Auth/OpenID/Store/File.php';
  23. $temp_dir = _Auth_OpenID_mkdtemp();
  24. if (!$temp_dir) {
  25. $this->fail('Could not create temporary directory ' .
  26. 'with Auth_OpenID_FileStore::_mkdtemp');
  27. return;
  28. }
  29. $store = new Auth_OpenID_Store_File($temp_dir);
  30. $this->_testStore($store);
  31. $this->_testNonce($store);
  32. $this->_testNonceCleanup($store);
  33. $store->destroy();
  34. }
  35. function test_postgresqlstore()
  36. {
  37. // If the postgres extension isn't loaded or loadable, succeed
  38. // because we can't run the test.
  39. if (!(extension_loaded('pgsql')) ||
  40. !(@include_once 'DB.php')) {
  41. $this->markTestSkipped("not testing PostGreSQL store");
  42. return;
  43. }
  44. require_once 'Auth/OpenID/Store/PostgreSQL.php';
  45. $temp_db_name = _Auth_OpenID_getTmpDbName();
  46. $connect_db_name = 'test_master';
  47. $dsn = array(
  48. 'phptype' => 'pgsql',
  49. 'username' => 'openid_test',
  50. 'password' => '',
  51. 'hostspec' => $GLOBALS['_Auth_OpenID_db_test_host'],
  52. 'database' => $connect_db_name
  53. );
  54. $allowed_failures = 5;
  55. $result = null;
  56. $sleep_time = 1.0;
  57. $sql = sprintf("CREATE DATABASE %s", $temp_db_name);
  58. for ($failures = 0; $failures < $allowed_failures; $failures++) {
  59. $template_db = DB::connect($dsn);
  60. if (PEAR::isError($template_db)) {
  61. $result = $template_db;
  62. } else {
  63. // Try to create the test database.
  64. $result = $template_db->query($sql);
  65. $template_db->disconnect();
  66. unset($template_db);
  67. if (!PEAR::isError($result)) {
  68. break;
  69. }
  70. }
  71. $sleep_time *= ((mt_rand(1, 100) / 100.0) + 1.5);
  72. echo "Failed to create database $temp_db_name.\n"
  73. . "Waiting $sleep_time before trying again\n";
  74. $int_sleep = floor($sleep_time);
  75. $frac_sleep = $sleep_time - $int_sleep;
  76. sleep($int_sleep);
  77. usleep($frac_sleep * 1000000.0);
  78. }
  79. if ($failures == $allowed_failures) {
  80. $this->pass("Temporary database creation failed after $failures ".
  81. " tries ('$temp_db_name'): " . $result->getMessage());
  82. return;
  83. }
  84. // Disconnect from template1 and reconnect to the temporary
  85. // testing database.
  86. $dsn['database'] = $temp_db_name;
  87. $db = DB::connect($dsn);
  88. if (PEAR::isError($db)) {
  89. $this->fail("Temporary database connection failed " .
  90. " ('$temp_db_name'): " . $db->getMessage());
  91. return;
  92. }
  93. $store = new Auth_OpenID_Store_PostgreSQL($db);
  94. $this->assertFalse($store->tableExists($store->nonces_table_name));
  95. $this->assertFalse($store->tableExists($store->associations_table_name));
  96. $store->createTables();
  97. $this->assertTrue($store->tableExists($store->nonces_table_name));
  98. $this->assertTrue($store->tableExists($store->associations_table_name));
  99. $this->_testStore($store);
  100. $this->_testNonce($store);
  101. $this->_testNonceCleanup($store);
  102. $db->disconnect();
  103. unset($db);
  104. // Connect to template1 again so we can drop the temporary
  105. // database.
  106. $dsn['database'] = $connect_db_name;
  107. $template_db = DB::connect($dsn);
  108. if (PEAR::isError($template_db)) {
  109. $this->fail("Template database connection (to drop " .
  110. "temporary database) failed: " .
  111. $template_db->getMessage());
  112. return;
  113. }
  114. $result = $template_db->query(sprintf("DROP DATABASE %s",
  115. $temp_db_name));
  116. if (PEAR::isError($result)) {
  117. $this->fail("Dropping temporary database failed: " .
  118. $result->getMessage());
  119. return;
  120. }
  121. $template_db->disconnect();
  122. unset($template_db);
  123. }
  124. function test_sqlitestore()
  125. {
  126. // If the sqlite extension isn't loaded or loadable, succeed
  127. // because we can't run the test.
  128. if (!(extension_loaded('sqlite')) ||
  129. !(@include_once 'DB.php')) {
  130. $this->markTestSkipped("not testing SQLite store");
  131. return;
  132. }
  133. require_once 'Auth/OpenID/Store/SQLite.php';
  134. $temp_dir = _Auth_OpenID_mkdtemp();
  135. if (!$temp_dir) {
  136. $this->fail('Could not create temporary directory ' .
  137. 'with Auth_OpenID_FileStore::_mkdtemp');
  138. return;
  139. }
  140. $dsn = 'sqlite:///' . urlencode($temp_dir) . '/php_openid_storetest.db';
  141. $db = DB::connect($dsn);
  142. if (PEAR::isError($db)) {
  143. $this->fail("SQLite database connection failed: " .
  144. $db->getMessage());
  145. } else {
  146. $store = new Auth_OpenID_Store_SQLite($db);
  147. $this->assertTrue($store->createTables(), "Table creation failed");
  148. $this->_testStore($store);
  149. $this->_testNonce($store);
  150. $this->_testNonceCleanup($store);
  151. }
  152. $db->disconnect();
  153. unset($db);
  154. unset($store);
  155. unlink($temp_dir . '/php_openid_storetest.db');
  156. rmdir($temp_dir);
  157. }
  158. function test_mysqlstore()
  159. {
  160. // If the mysql extension isn't loaded or loadable, succeed
  161. // because we can't run the test.
  162. if (!(extension_loaded('mysql')) ||
  163. !(@include_once 'DB.php')) {
  164. $this->markTestSkipped("not testing MySQL store");
  165. return;
  166. }
  167. require_once 'Auth/OpenID/Store/MySQL.php';
  168. $dsn = array(
  169. 'phptype' => 'mysql',
  170. 'username' => 'openid_test',
  171. 'password' => '',
  172. 'hostspec' => $GLOBALS['_Auth_OpenID_db_test_host']
  173. );
  174. $db = DB::connect($dsn);
  175. if (PEAR::isError($db)) {
  176. $this->markTestSkipped("MySQL database connection failed: " .
  177. $db->getMessage());
  178. return;
  179. }
  180. $temp_db_name = _Auth_OpenID_getTmpDbName();
  181. $result = $db->query("CREATE DATABASE $temp_db_name");
  182. if (PEAR::isError($result)) {
  183. $this->fail("Error creating MySQL temporary database: " .
  184. $result->getMessage());
  185. return;
  186. }
  187. $db->query("USE $temp_db_name");
  188. $store = new Auth_OpenID_Store_MySQL($db);
  189. $store->createTables();
  190. $this->_testStore($store);
  191. $this->_testNonce($store);
  192. $this->_testNonceCleanup($store);
  193. $db->query("DROP DATABASE $temp_db_name");
  194. }
  195. function test_mdb2store()
  196. {
  197. // The MDB2 test can use any database engine. MySQL is chosen
  198. // arbitrarily.
  199. if (!(extension_loaded('mysql') ||
  200. @dl('mysql.' . PHP_SHLIB_SUFFIX)) ||
  201. !(@include_once 'MDB2.php')) {
  202. $this->markTestSkipped("not testing MDB2 store");
  203. return;
  204. }
  205. require_once 'Auth/OpenID/Store/MDB2.php';
  206. $dsn = array(
  207. 'phptype' => 'mysql',
  208. 'username' => 'openid_test',
  209. 'password' => '',
  210. 'hostspec' => $GLOBALS['_Auth_OpenID_db_test_host']
  211. );
  212. $db = MDB2::connect($dsn);
  213. if (PEAR::isError($db)) {
  214. $this->fail("MySQL database connection failed: " .
  215. $db->getMessage());
  216. return;
  217. }
  218. $temp_db_name = _Auth_OpenID_getTmpDbName();
  219. $result = $db->query("CREATE DATABASE $temp_db_name");
  220. if (PEAR::isError($result)) {
  221. $this->pass("Error creating MySQL temporary database: " .
  222. $result->getMessage());
  223. return;
  224. }
  225. $db->query("USE $temp_db_name");
  226. $store = new Auth_OpenID_MDB2Store($db);
  227. if (!$store->createTables()) {
  228. $this->fail("Failed to create tables");
  229. return;
  230. }
  231. $this->_testStore($store);
  232. $this->_testNonce($store);
  233. $this->_testNonceCleanup($store);
  234. $db->query("DROP DATABASE $temp_db_name");
  235. }
  236. }