updater.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <?php
  2. /**
  3. * @author Lukas Reschke <lukas@owncloud.com>
  4. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  5. *
  6. * @copyright Copyright (c) 2015, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC;
  23. use OCP\IConfig;
  24. use OCP\ILogger;
  25. use OC\IntegrityCheck\Checker;
  26. class UpdaterTest extends \Test\TestCase {
  27. /** @var IConfig */
  28. private $config;
  29. /** @var HTTPHelper */
  30. private $httpHelper;
  31. /** @var ILogger */
  32. private $logger;
  33. /** @var Updater */
  34. private $updater;
  35. /** @var Checker */
  36. private $checker;
  37. public function setUp() {
  38. parent::setUp();
  39. $this->config = $this->getMockBuilder('\\OCP\\IConfig')
  40. ->disableOriginalConstructor()
  41. ->getMock();
  42. $this->httpHelper = $this->getMockBuilder('\\OC\\HTTPHelper')
  43. ->disableOriginalConstructor()
  44. ->getMock();
  45. $this->logger = $this->getMockBuilder('\\OCP\\ILogger')
  46. ->disableOriginalConstructor()
  47. ->getMock();
  48. $this->checker = $this->getMockBuilder('\OC\IntegrityCheck\Checker')
  49. ->disableOriginalConstructor()
  50. ->getMock();
  51. $this->updater = new Updater(
  52. $this->httpHelper,
  53. $this->config,
  54. $this->checker,
  55. $this->logger
  56. );
  57. }
  58. /**
  59. * @param string $baseUrl
  60. * @return string
  61. */
  62. private function buildUpdateUrl($baseUrl) {
  63. return $baseUrl . '?version='.implode('x', \OC_Util::getVersion()).'xinstalledatxlastupdatedatx'.\OC_Util::getChannel().'x'.\OC_Util::getEditionString().'x';
  64. }
  65. /**
  66. * @return array
  67. */
  68. public function versionCompatibilityTestData() {
  69. return [
  70. ['1', '2', '1', true],
  71. ['2', '2', '2', true],
  72. ['6.0.5.0', '6.0.6.0', '5.0', true],
  73. ['5.0.6.0', '7.0.4.0', '6.0', false],
  74. // allow upgrading within the same major release
  75. ['8.0.0.0', '8.0.0.0', '8.0', true],
  76. ['8.0.0.0', '8.0.0.4', '8.0', true],
  77. ['8.0.0.0', '8.0.1.0', '8.0', true],
  78. ['8.0.0.0', '8.0.2.0', '8.0', true],
  79. // does not allow downgrading within the same major release
  80. ['8.0.1.0', '8.0.0.0', '8.0', false],
  81. ['8.0.2.0', '8.0.1.0', '8.0', false],
  82. ['8.0.0.4', '8.0.0.0', '8.0', false],
  83. // allows upgrading within the patch version
  84. ['8.0.0.0', '8.0.0.1', '8.0', true],
  85. ['8.0.0.0', '8.0.0.2', '8.0', true],
  86. // does not allow downgrading within the same major release
  87. ['8.0.0.1', '8.0.0.0', '8.0', false],
  88. ['8.0.0.2', '8.0.0.0', '8.0', false],
  89. // allow upgrading to the next major release
  90. ['8.0.0.0', '8.1.0.0', '8.0', true],
  91. ['8.0.0.0', '8.1.1.0', '8.0', true],
  92. ['8.0.0.0', '8.1.1.5', '8.0', true],
  93. ['8.0.0.2', '8.1.1.5', '8.0', true],
  94. ['8.1.0.0', '8.2.0.0', '8.1', true],
  95. ['8.1.0.2', '8.2.0.4', '8.1', true],
  96. ['8.1.0.5', '8.2.0.1', '8.1', true],
  97. ['8.1.0.0', '8.2.1.0', '8.1', true],
  98. ['8.1.0.2', '8.2.1.5', '8.1', true],
  99. ['8.1.0.5', '8.2.1.1', '8.1', true],
  100. // does not allow downgrading to the previous major release
  101. ['8.1.0.0', '8.0.0.0', '7.0', false],
  102. ['8.1.1.0', '8.0.0.0', '7.0', false],
  103. // does not allow skipping major releases
  104. ['8.0.0.0', '8.2.0.0', '8.1', false],
  105. ['8.0.0.0', '8.2.1.0', '8.1', false],
  106. ['8.0.0.0', '9.0.1.0', '8.2', false],
  107. ['8.0.0.0', '10.0.0.0', '9.3', false],
  108. // allows updating to the next major release
  109. ['8.2.0.0', '9.0.0.0', '8.2', true],
  110. ['8.2.0.0', '9.0.0.0', '8.2', true],
  111. ['8.2.0.0', '9.0.1.0', '8.2', true],
  112. ['8.2.0.0', '9.0.1.1', '8.2', true],
  113. ['8.2.0.2', '9.0.1.1', '8.2', true],
  114. ['8.2.2.0', '9.0.1.0', '8.2', true],
  115. ['8.2.2.2', '9.0.1.1', '8.2', true],
  116. ['9.0.0.0', '9.1.0.0', '9.0', true],
  117. ['9.0.0.0', '9.1.0.2', '9.0', true],
  118. ['9.0.0.2', '9.1.0.1', '9.0', true],
  119. ['9.1.0.0', '9.2.0.0', '9.1', true],
  120. ['9.2.0.0', '9.3.0.0', '9.2', true],
  121. ['9.3.0.0', '10.0.0.0', '9.3', true],
  122. // does not allow updating to the next major release (first number)
  123. ['9.0.0.0', '8.2.0.0', '8.1', false],
  124. // other cases
  125. ['8.0.0.0', '8.1.5.0', '8.0', true],
  126. ['8.2.0.0', '9.0.0.0', '8.2', true],
  127. ['8.2.0.0', '9.1.0.0', '9.0', false],
  128. ['9.0.0.0', '8.1.0.0', '8.0', false],
  129. ['9.0.0.0', '8.0.0.0', '7.0', false],
  130. ['9.1.0.0', '8.0.0.0', '7.0', false],
  131. ['8.2.0.0', '8.1.0.0', '8.0', false],
  132. ];
  133. }
  134. public function testSetSimulateStepEnabled() {
  135. $this->updater->setSimulateStepEnabled(true);
  136. $this->assertSame(true, $this->invokePrivate($this->updater, 'simulateStepEnabled'));
  137. $this->updater->setSimulateStepEnabled(false);
  138. $this->assertSame(false, $this->invokePrivate($this->updater, 'simulateStepEnabled'));
  139. }
  140. public function testSetUpdateStepEnabled() {
  141. $this->updater->setUpdateStepEnabled(true);
  142. $this->assertSame(true, $this->invokePrivate($this->updater, 'updateStepEnabled'));
  143. $this->updater->setUpdateStepEnabled(false);
  144. $this->assertSame(false, $this->invokePrivate($this->updater, 'updateStepEnabled'));
  145. }
  146. public function testSetSkip3rdPartyAppsDisable() {
  147. $this->updater->setSkip3rdPartyAppsDisable(true);
  148. $this->assertSame(true, $this->invokePrivate($this->updater, 'skip3rdPartyAppsDisable'));
  149. $this->updater->setSkip3rdPartyAppsDisable(false);
  150. $this->assertSame(false, $this->invokePrivate($this->updater, 'skip3rdPartyAppsDisable'));
  151. }
  152. /**
  153. * @dataProvider versionCompatibilityTestData
  154. *
  155. * @param string $oldVersion
  156. * @param string $newVersion
  157. * @param string $allowedVersion
  158. * @param bool $result
  159. */
  160. public function testIsUpgradePossible($oldVersion, $newVersion, $allowedVersion, $result) {
  161. $this->assertSame($result, $this->updater->isUpgradePossible($oldVersion, $newVersion, $allowedVersion));
  162. }
  163. public function testCheckInCache() {
  164. $expectedResult = [
  165. 'version' => '8.0.4.2',
  166. 'versionstring' => 'ownCloud 8.0.4',
  167. 'url' => 'https://download.owncloud.org/community/owncloud-8.0.4.zip',
  168. 'web' => 'http://doc.owncloud.org/server/8.0/admin_manual/maintenance/upgrade.html',
  169. ];
  170. $this->config
  171. ->expects($this->at(0))
  172. ->method('getAppValue')
  173. ->with('core', 'lastupdatedat')
  174. ->will($this->returnValue(time()));
  175. $this->config
  176. ->expects($this->at(1))
  177. ->method('getAppValue')
  178. ->with('core', 'lastupdateResult')
  179. ->will($this->returnValue(json_encode($expectedResult)));
  180. $this->assertSame($expectedResult, $this->updater->check());
  181. }
  182. public function testCheckWithoutUpdateUrl() {
  183. $expectedResult = [
  184. 'version' => '8.0.4.2',
  185. 'versionstring' => 'ownCloud 8.0.4',
  186. 'url' => 'https://download.owncloud.org/community/owncloud-8.0.4.zip',
  187. 'web' => 'http://doc.owncloud.org/server/8.0/admin_manual/maintenance/upgrade.html',
  188. ];
  189. $this->config
  190. ->expects($this->at(0))
  191. ->method('getAppValue')
  192. ->with('core', 'lastupdatedat')
  193. ->will($this->returnValue(0));
  194. $this->config
  195. ->expects($this->at(1))
  196. ->method('setAppValue')
  197. ->with('core', 'lastupdatedat', $this->isType('integer'));
  198. $this->config
  199. ->expects($this->at(3))
  200. ->method('getAppValue')
  201. ->with('core', 'installedat')
  202. ->will($this->returnValue('installedat'));
  203. $this->config
  204. ->expects($this->at(4))
  205. ->method('getAppValue')
  206. ->with('core', 'lastupdatedat')
  207. ->will($this->returnValue('lastupdatedat'));
  208. $this->config
  209. ->expects($this->at(5))
  210. ->method('setAppValue')
  211. ->with('core', 'lastupdateResult', json_encode($expectedResult));
  212. $updateXml = '<?xml version="1.0"?>
  213. <owncloud>
  214. <version>8.0.4.2</version>
  215. <versionstring>ownCloud 8.0.4</versionstring>
  216. <url>https://download.owncloud.org/community/owncloud-8.0.4.zip</url>
  217. <web>http://doc.owncloud.org/server/8.0/admin_manual/maintenance/upgrade.html</web>
  218. </owncloud>';
  219. $this->httpHelper
  220. ->expects($this->once())
  221. ->method('getUrlContent')
  222. ->with($this->buildUpdateUrl('https://updates.owncloud.com/server/'))
  223. ->will($this->returnValue($updateXml));
  224. $this->assertSame($expectedResult, $this->updater->check());
  225. }
  226. public function testCheckWithInvalidXml() {
  227. $this->config
  228. ->expects($this->at(0))
  229. ->method('getAppValue')
  230. ->with('core', 'lastupdatedat')
  231. ->will($this->returnValue(0));
  232. $this->config
  233. ->expects($this->at(1))
  234. ->method('setAppValue')
  235. ->with('core', 'lastupdatedat', $this->isType('integer'));
  236. $this->config
  237. ->expects($this->at(3))
  238. ->method('getAppValue')
  239. ->with('core', 'installedat')
  240. ->will($this->returnValue('installedat'));
  241. $this->config
  242. ->expects($this->at(4))
  243. ->method('getAppValue')
  244. ->with('core', 'lastupdatedat')
  245. ->will($this->returnValue('lastupdatedat'));
  246. $this->config
  247. ->expects($this->at(5))
  248. ->method('setAppValue')
  249. ->with('core', 'lastupdateResult', 'false');
  250. $updateXml = 'Invalid XML Response!';
  251. $this->httpHelper
  252. ->expects($this->once())
  253. ->method('getUrlContent')
  254. ->with($this->buildUpdateUrl('https://updates.owncloud.com/server/'))
  255. ->will($this->returnValue($updateXml));
  256. $this->assertSame([], $this->updater->check());
  257. }
  258. public function testCheckWithUpdateUrl() {
  259. $expectedResult = [
  260. 'version' => '8.0.4.2',
  261. 'versionstring' => 'ownCloud 8.0.4',
  262. 'url' => 'https://download.owncloud.org/community/owncloud-8.0.4.zip',
  263. 'web' => 'http://doc.owncloud.org/server/8.0/admin_manual/maintenance/upgrade.html',
  264. ];
  265. $this->config
  266. ->expects($this->at(0))
  267. ->method('getAppValue')
  268. ->with('core', 'lastupdatedat')
  269. ->will($this->returnValue(0));
  270. $this->config
  271. ->expects($this->at(1))
  272. ->method('setAppValue')
  273. ->with('core', 'lastupdatedat', $this->isType('integer'));
  274. $this->config
  275. ->expects($this->at(3))
  276. ->method('getAppValue')
  277. ->with('core', 'installedat')
  278. ->will($this->returnValue('installedat'));
  279. $this->config
  280. ->expects($this->at(4))
  281. ->method('getAppValue')
  282. ->with('core', 'lastupdatedat')
  283. ->will($this->returnValue('lastupdatedat'));
  284. $this->config
  285. ->expects($this->at(5))
  286. ->method('setAppValue')
  287. ->with('core', 'lastupdateResult', json_encode($expectedResult));
  288. $updateXml = '<?xml version="1.0"?>
  289. <owncloud>
  290. <version>8.0.4.2</version>
  291. <versionstring>ownCloud 8.0.4</versionstring>
  292. <url>https://download.owncloud.org/community/owncloud-8.0.4.zip</url>
  293. <web>http://doc.owncloud.org/server/8.0/admin_manual/maintenance/upgrade.html</web>
  294. </owncloud>';
  295. $this->httpHelper
  296. ->expects($this->once())
  297. ->method('getUrlContent')
  298. ->with($this->buildUpdateUrl('https://myupdater.com/'))
  299. ->will($this->returnValue($updateXml));
  300. $this->assertSame($expectedResult, $this->updater->check('https://myupdater.com/'));
  301. }
  302. public function testCheckWithEmptyValidXmlResponse() {
  303. $expectedResult = [
  304. 'version' => '',
  305. 'versionstring' => '',
  306. 'url' => '',
  307. 'web' => '',
  308. ];
  309. $this->config
  310. ->expects($this->at(0))
  311. ->method('getAppValue')
  312. ->with('core', 'lastupdatedat')
  313. ->will($this->returnValue(0));
  314. $this->config
  315. ->expects($this->at(1))
  316. ->method('setAppValue')
  317. ->with('core', 'lastupdatedat', $this->isType('integer'));
  318. $this->config
  319. ->expects($this->at(3))
  320. ->method('getAppValue')
  321. ->with('core', 'installedat')
  322. ->will($this->returnValue('installedat'));
  323. $this->config
  324. ->expects($this->at(4))
  325. ->method('getAppValue')
  326. ->with('core', 'lastupdatedat')
  327. ->will($this->returnValue('lastupdatedat'));
  328. $updateXml = '<?xml version="1.0"?>
  329. <owncloud>
  330. <version></version>
  331. <versionstring></versionstring>
  332. <url></url>
  333. <web></web>
  334. </owncloud>';
  335. $this->httpHelper
  336. ->expects($this->once())
  337. ->method('getUrlContent')
  338. ->with($this->buildUpdateUrl('https://updates.owncloud.com/server/'))
  339. ->will($this->returnValue($updateXml));
  340. $this->assertSame($expectedResult, $this->updater->check());
  341. }
  342. public function testCheckWithEmptyInvalidXmlResponse() {
  343. $expectedResult = [];
  344. $this->config
  345. ->expects($this->at(0))
  346. ->method('getAppValue')
  347. ->with('core', 'lastupdatedat')
  348. ->will($this->returnValue(0));
  349. $this->config
  350. ->expects($this->at(1))
  351. ->method('setAppValue')
  352. ->with('core', 'lastupdatedat', $this->isType('integer'));
  353. $this->config
  354. ->expects($this->at(3))
  355. ->method('getAppValue')
  356. ->with('core', 'installedat')
  357. ->will($this->returnValue('installedat'));
  358. $this->config
  359. ->expects($this->at(4))
  360. ->method('getAppValue')
  361. ->with('core', 'lastupdatedat')
  362. ->will($this->returnValue('lastupdatedat'));
  363. $this->config
  364. ->expects($this->at(5))
  365. ->method('setAppValue')
  366. ->with('core', 'lastupdateResult', json_encode($expectedResult));
  367. $updateXml = '';
  368. $this->httpHelper
  369. ->expects($this->once())
  370. ->method('getUrlContent')
  371. ->with($this->buildUpdateUrl('https://updates.owncloud.com/server/'))
  372. ->will($this->returnValue($updateXml));
  373. $this->assertSame($expectedResult, $this->updater->check());
  374. }
  375. }