You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

296 lines
9.2 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Federation\Tests;
  26. use OCA\Federation\DbHandler;
  27. use OCA\Federation\TrustedServers;
  28. use OCP\IDBConnection;
  29. use OCP\IL10N;
  30. use Test\TestCase;
  31. /**
  32. * @group DB
  33. */
  34. class DbHandlerTest extends TestCase {
  35. /** @var DbHandler */
  36. private $dbHandler;
  37. /** @var IL10N | \PHPUnit_Framework_MockObject_MockObject */
  38. private $il10n;
  39. /** @var IDBConnection */
  40. private $connection;
  41. /** @var string */
  42. private $dbTable = 'trusted_servers';
  43. public function setUp() {
  44. parent::setUp();
  45. $this->connection = \OC::$server->getDatabaseConnection();
  46. $this->il10n = $this->getMockBuilder(IL10N::class)->getMock();
  47. $this->dbHandler = new DbHandler(
  48. $this->connection,
  49. $this->il10n
  50. );
  51. $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
  52. $result = $query->execute()->fetchAll();
  53. $this->assertEmpty($result, 'we need to start with a empty trusted_servers table');
  54. }
  55. public function tearDown() {
  56. parent::tearDown();
  57. $query = $this->connection->getQueryBuilder()->delete($this->dbTable);
  58. $query->execute();
  59. }
  60. /**
  61. * @dataProvider dataTestAddServer
  62. *
  63. * @param string $url passed to the method
  64. * @param string $expectedUrl the url we expect to be written to the db
  65. * @param string $expectedHash the hash value we expect to be written to the db
  66. */
  67. public function testAddServer($url, $expectedUrl, $expectedHash) {
  68. $id = $this->dbHandler->addServer($url);
  69. $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
  70. $result = $query->execute()->fetchAll();
  71. $this->assertSame(1, count($result));
  72. $this->assertSame($expectedUrl, $result[0]['url']);
  73. $this->assertSame($id, (int)$result[0]['id']);
  74. $this->assertSame($expectedHash, $result[0]['url_hash']);
  75. $this->assertSame(TrustedServers::STATUS_PENDING, (int)$result[0]['status']);
  76. }
  77. public function dataTestAddServer() {
  78. return [
  79. ['http://owncloud.org', 'http://owncloud.org', sha1('owncloud.org')],
  80. ['https://owncloud.org', 'https://owncloud.org', sha1('owncloud.org')],
  81. ['http://owncloud.org/', 'http://owncloud.org', sha1('owncloud.org')],
  82. ];
  83. }
  84. public function testRemove() {
  85. $id1 = $this->dbHandler->addServer('server1');
  86. $id2 = $this->dbHandler->addServer('server2');
  87. $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
  88. $result = $query->execute()->fetchAll();
  89. $this->assertSame(2, count($result));
  90. $this->assertSame('server1', $result[0]['url']);
  91. $this->assertSame('server2', $result[1]['url']);
  92. $this->assertSame($id1, (int)$result[0]['id']);
  93. $this->assertSame($id2, (int)$result[1]['id']);
  94. $this->dbHandler->removeServer($id2);
  95. $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
  96. $result = $query->execute()->fetchAll();
  97. $this->assertSame(1, count($result));
  98. $this->assertSame('server1', $result[0]['url']);
  99. $this->assertSame($id1, (int)$result[0]['id']);
  100. }
  101. public function testGetServerById() {
  102. $this->dbHandler->addServer('server1');
  103. $id = $this->dbHandler->addServer('server2');
  104. $result = $this->dbHandler->getServerById($id);
  105. $this->assertSame('server2', $result['url']);
  106. }
  107. public function testGetAll() {
  108. $id1 = $this->dbHandler->addServer('server1');
  109. $id2 = $this->dbHandler->addServer('server2');
  110. $result = $this->dbHandler->getAllServer();
  111. $this->assertSame(2, count($result));
  112. $this->assertSame('server1', $result[0]['url']);
  113. $this->assertSame('server2', $result[1]['url']);
  114. $this->assertSame($id1, (int)$result[0]['id']);
  115. $this->assertSame($id2, (int)$result[1]['id']);
  116. }
  117. /**
  118. * @dataProvider dataTestServerExists
  119. *
  120. * @param string $serverInTable
  121. * @param string $checkForServer
  122. * @param bool $expected
  123. */
  124. public function testServerExists($serverInTable, $checkForServer, $expected) {
  125. $this->dbHandler->addServer($serverInTable);
  126. $this->assertSame($expected,
  127. $this->dbHandler->serverExists($checkForServer)
  128. );
  129. }
  130. public function dataTestServerExists() {
  131. return [
  132. ['server1', 'server1', true],
  133. ['server1', 'http://server1', true],
  134. ['server1', 'server2', false]
  135. ];
  136. }
  137. public function testAddToken() {
  138. $this->dbHandler->addServer('server1');
  139. $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
  140. $result = $query->execute()->fetchAll();
  141. $this->assertSame(1, count($result));
  142. $this->assertSame(null, $result[0]['token']);
  143. $this->dbHandler->addToken('http://server1', 'token');
  144. $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
  145. $result = $query->execute()->fetchAll();
  146. $this->assertSame(1, count($result));
  147. $this->assertSame('token', $result[0]['token']);
  148. }
  149. public function testGetToken() {
  150. $this->dbHandler->addServer('server1');
  151. $this->dbHandler->addToken('http://server1', 'token');
  152. $this->assertSame('token',
  153. $this->dbHandler->getToken('https://server1')
  154. );
  155. }
  156. public function testAddSharedSecret() {
  157. $this->dbHandler->addServer('server1');
  158. $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
  159. $result = $query->execute()->fetchAll();
  160. $this->assertSame(1, count($result));
  161. $this->assertSame(null, $result[0]['shared_secret']);
  162. $this->dbHandler->addSharedSecret('http://server1', 'secret');
  163. $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
  164. $result = $query->execute()->fetchAll();
  165. $this->assertSame(1, count($result));
  166. $this->assertSame('secret', $result[0]['shared_secret']);
  167. }
  168. public function testGetSharedSecret() {
  169. $this->dbHandler->addServer('server1');
  170. $this->dbHandler->addSharedSecret('http://server1', 'secret');
  171. $this->assertSame('secret',
  172. $this->dbHandler->getSharedSecret('https://server1')
  173. );
  174. }
  175. public function testSetServerStatus() {
  176. $this->dbHandler->addServer('server1');
  177. $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
  178. $result = $query->execute()->fetchAll();
  179. $this->assertSame(1, count($result));
  180. $this->assertSame(TrustedServers::STATUS_PENDING, (int)$result[0]['status']);
  181. $this->dbHandler->setServerStatus('http://server1', TrustedServers::STATUS_OK);
  182. $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
  183. $result = $query->execute()->fetchAll();
  184. $this->assertSame(1, count($result));
  185. $this->assertSame(TrustedServers::STATUS_OK, (int)$result[0]['status']);
  186. }
  187. public function testGetServerStatus() {
  188. $this->dbHandler->addServer('server1');
  189. $this->dbHandler->setServerStatus('http://server1', TrustedServers::STATUS_OK);
  190. $this->assertSame(TrustedServers::STATUS_OK,
  191. $this->dbHandler->getServerStatus('https://server1')
  192. );
  193. // test sync token
  194. $this->dbHandler->setServerStatus('http://server1', TrustedServers::STATUS_OK, 'token1234567890');
  195. $servers = $this->dbHandler->getAllServer();
  196. $this->assertSame('token1234567890', $servers[0]['sync_token']);
  197. }
  198. /**
  199. * hash should always be computed with the normalized URL
  200. *
  201. * @dataProvider dataTestHash
  202. *
  203. * @param string $url
  204. * @param string $expected
  205. */
  206. public function testHash($url, $expected) {
  207. $this->assertSame($expected,
  208. $this->invokePrivate($this->dbHandler, 'hash', [$url])
  209. );
  210. }
  211. public function dataTestHash() {
  212. return [
  213. ['server1', sha1('server1')],
  214. ['http://server1', sha1('server1')],
  215. ['https://server1', sha1('server1')],
  216. ['http://server1/', sha1('server1')],
  217. ];
  218. }
  219. /**
  220. * @dataProvider dataTestNormalizeUrl
  221. *
  222. * @param string $url
  223. * @param string $expected
  224. */
  225. public function testNormalizeUrl($url, $expected) {
  226. $this->assertSame($expected,
  227. $this->invokePrivate($this->dbHandler, 'normalizeUrl', [$url])
  228. );
  229. }
  230. public function dataTestNormalizeUrl() {
  231. return [
  232. ['owncloud.org', 'owncloud.org'],
  233. ['http://owncloud.org', 'owncloud.org'],
  234. ['https://owncloud.org', 'owncloud.org'],
  235. ['https://owncloud.org//mycloud', 'owncloud.org/mycloud'],
  236. ['https://owncloud.org/mycloud/', 'owncloud.org/mycloud'],
  237. ];
  238. }
  239. /**
  240. * @dataProvider providesAuth
  241. */
  242. public function testAuth($expectedResult, $user, $password) {
  243. if ($expectedResult) {
  244. $this->dbHandler->addServer('url1');
  245. $this->dbHandler->addSharedSecret('url1', $password);
  246. }
  247. $result = $this->dbHandler->auth($user, $password);
  248. $this->assertEquals($expectedResult, $result);
  249. }
  250. public function providesAuth() {
  251. return [
  252. [false, 'foo', ''],
  253. [true, 'system', '123456789'],
  254. ];
  255. }
  256. }