DbHandlerTest.php 9.8 KB

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