UploadIT.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Tobias Kaminsky
  5. * Copyright (C) 2020 Tobias Kaminsky
  6. * Copyright (C) 2020 Nextcloud GmbH
  7. * Copyright (C) 2020 Chris Narkiewicz <hello@ezaquarii.com>
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. package com.owncloud.android;
  23. import com.nextcloud.client.account.UserAccountManagerImpl;
  24. import com.nextcloud.client.device.BatteryStatus;
  25. import com.nextcloud.client.device.PowerManagementService;
  26. import com.nextcloud.client.network.Connectivity;
  27. import com.nextcloud.client.network.ConnectivityService;
  28. import com.owncloud.android.datamodel.OCFile;
  29. import com.owncloud.android.datamodel.UploadsStorageManager;
  30. import com.owncloud.android.db.OCUpload;
  31. import com.owncloud.android.files.services.FileUploader;
  32. import com.owncloud.android.files.services.NameCollisionPolicy;
  33. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  34. import com.owncloud.android.operations.RefreshFolderOperation;
  35. import com.owncloud.android.operations.RemoveFileOperation;
  36. import com.owncloud.android.operations.UploadFileOperation;
  37. import com.owncloud.android.utils.FileStorageUtils;
  38. import org.junit.After;
  39. import org.junit.Before;
  40. import org.junit.Test;
  41. import java.io.File;
  42. import java.io.IOException;
  43. import androidx.annotation.NonNull;
  44. import static junit.framework.TestCase.assertEquals;
  45. import static junit.framework.TestCase.assertFalse;
  46. import static junit.framework.TestCase.assertTrue;
  47. /**
  48. * Tests related to file uploads
  49. */
  50. public class UploadIT extends AbstractOnServerIT {
  51. private static final String FOLDER = "/testUpload/";
  52. private UploadsStorageManager uploadsStorageManager =
  53. new UploadsStorageManager(UserAccountManagerImpl.fromContext(targetContext),
  54. targetContext.getContentResolver());
  55. private ConnectivityService connectivityServiceMock = new ConnectivityService() {
  56. @Override
  57. public boolean isInternetWalled() {
  58. return false;
  59. }
  60. @Override
  61. public Connectivity getConnectivity() {
  62. return Connectivity.CONNECTED_WIFI;
  63. }
  64. };
  65. private PowerManagementService powerManagementServiceMock = new PowerManagementService() {
  66. @Override
  67. public boolean isPowerSavingEnabled() {
  68. return false;
  69. }
  70. @Override
  71. public boolean isPowerSavingExclusionAvailable() {
  72. return false;
  73. }
  74. @NonNull
  75. @Override
  76. public BatteryStatus getBattery() {
  77. return new BatteryStatus(false, 0);
  78. }
  79. };
  80. @Before
  81. public void before() throws IOException {
  82. // make sure that every file is available, even after tests that remove source file
  83. createDummyFiles();
  84. }
  85. @After
  86. public void after() {
  87. RemoteOperationResult result = new RefreshFolderOperation(getStorageManager().getFileByPath("/"),
  88. System.currentTimeMillis() / 1000L,
  89. false,
  90. true,
  91. getStorageManager(),
  92. user,
  93. targetContext)
  94. .execute(client);
  95. // cleanup only if folder exists
  96. if (result.isSuccess() && getStorageManager().getFileByDecryptedRemotePath(FOLDER) != null) {
  97. new RemoveFileOperation(getStorageManager().getFileByDecryptedRemotePath(FOLDER),
  98. false,
  99. account,
  100. false,
  101. targetContext,
  102. getStorageManager())
  103. .execute(client);
  104. }
  105. }
  106. @Test
  107. public void testEmptyUpload() {
  108. OCUpload ocUpload = new OCUpload(FileStorageUtils.getTemporalPath(account.name) + "/empty.txt",
  109. FOLDER + "empty.txt",
  110. account.name);
  111. uploadOCUpload(ocUpload);
  112. }
  113. @Test
  114. public void testNonEmptyUpload() {
  115. OCUpload ocUpload = new OCUpload(FileStorageUtils.getTemporalPath(account.name) + "/nonEmpty.txt",
  116. FOLDER + "nonEmpty.txt",
  117. account.name);
  118. uploadOCUpload(ocUpload);
  119. }
  120. @Test
  121. public void testUploadWithCopy() {
  122. OCUpload ocUpload = new OCUpload(FileStorageUtils.getTemporalPath(account.name) + "/nonEmpty.txt",
  123. FOLDER + "nonEmpty.txt",
  124. account.name);
  125. uploadOCUpload(ocUpload, FileUploader.LOCAL_BEHAVIOUR_COPY);
  126. File originalFile = new File(FileStorageUtils.getTemporalPath(account.name) + "/nonEmpty.txt");
  127. OCFile uploadedFile = fileDataStorageManager.getFileByDecryptedRemotePath(FOLDER + "nonEmpty.txt");
  128. assertTrue(originalFile.exists());
  129. assertTrue(new File(uploadedFile.getStoragePath()).exists());
  130. verifyStoragePath(uploadedFile);
  131. }
  132. @Test
  133. public void testUploadWithMove() {
  134. OCUpload ocUpload = new OCUpload(FileStorageUtils.getTemporalPath(account.name) + "/nonEmpty.txt",
  135. FOLDER + "nonEmpty.txt",
  136. account.name);
  137. uploadOCUpload(ocUpload, FileUploader.LOCAL_BEHAVIOUR_MOVE);
  138. File originalFile = new File(FileStorageUtils.getTemporalPath(account.name) + "/nonEmpty.txt");
  139. OCFile uploadedFile = fileDataStorageManager.getFileByDecryptedRemotePath(FOLDER + "nonEmpty.txt");
  140. assertFalse(originalFile.exists());
  141. assertTrue(new File(uploadedFile.getStoragePath()).exists());
  142. verifyStoragePath(uploadedFile);
  143. }
  144. @Test
  145. public void testUploadWithForget() {
  146. OCUpload ocUpload = new OCUpload(FileStorageUtils.getTemporalPath(account.name) + "/nonEmpty.txt",
  147. FOLDER + "nonEmpty.txt",
  148. account.name);
  149. uploadOCUpload(ocUpload, FileUploader.LOCAL_BEHAVIOUR_FORGET);
  150. File originalFile = new File(FileStorageUtils.getTemporalPath(account.name) + "/nonEmpty.txt");
  151. OCFile uploadedFile = fileDataStorageManager.getFileByDecryptedRemotePath(FOLDER + "nonEmpty.txt");
  152. assertTrue(originalFile.exists());
  153. assertFalse(new File(uploadedFile.getStoragePath()).exists());
  154. assertTrue(uploadedFile.getStoragePath().isEmpty());
  155. }
  156. @Test
  157. public void testUploadWithDelete() {
  158. OCUpload ocUpload = new OCUpload(FileStorageUtils.getTemporalPath(account.name) + "/nonEmpty.txt",
  159. FOLDER + "nonEmpty.txt",
  160. account.name);
  161. uploadOCUpload(ocUpload, FileUploader.LOCAL_BEHAVIOUR_DELETE);
  162. File originalFile = new File(FileStorageUtils.getTemporalPath(account.name) + "/nonEmpty.txt");
  163. OCFile uploadedFile = fileDataStorageManager.getFileByDecryptedRemotePath(FOLDER + "nonEmpty.txt");
  164. assertFalse(originalFile.exists());
  165. assertFalse(new File(uploadedFile.getStoragePath()).exists());
  166. assertTrue(uploadedFile.getStoragePath().isEmpty());
  167. }
  168. @Test
  169. public void testChunkedUpload() {
  170. OCUpload ocUpload = new OCUpload(FileStorageUtils.getTemporalPath(account.name) + "/chunkedFile.txt",
  171. FOLDER + "chunkedFile.txt", account.name);
  172. uploadOCUpload(ocUpload);
  173. }
  174. @Test
  175. public void testUploadInNonExistingFolder() {
  176. OCUpload ocUpload = new OCUpload(FileStorageUtils.getTemporalPath(account.name) + "/empty.txt",
  177. FOLDER + "2/3/4/1.txt", account.name);
  178. uploadOCUpload(ocUpload);
  179. }
  180. @Test
  181. public void testUploadOnChargingOnlyButNotCharging() {
  182. OCUpload ocUpload = new OCUpload(FileStorageUtils.getTemporalPath(account.name) + "/empty.txt",
  183. FOLDER + "notCharging.txt", account.name);
  184. ocUpload.setWhileChargingOnly(true);
  185. UploadFileOperation newUpload = new UploadFileOperation(
  186. uploadsStorageManager,
  187. connectivityServiceMock,
  188. powerManagementServiceMock,
  189. user,
  190. null,
  191. ocUpload,
  192. NameCollisionPolicy.DEFAULT,
  193. FileUploader.LOCAL_BEHAVIOUR_COPY,
  194. targetContext,
  195. false,
  196. true,
  197. getStorageManager()
  198. );
  199. newUpload.setRemoteFolderToBeCreated();
  200. newUpload.addRenameUploadListener(() -> {
  201. // dummy
  202. });
  203. RemoteOperationResult result = newUpload.execute(client);
  204. assertFalse(result.toString(), result.isSuccess());
  205. assertEquals(RemoteOperationResult.ResultCode.DELAYED_FOR_CHARGING, result.getCode());
  206. }
  207. @Test
  208. public void testUploadOnChargingOnlyAndCharging() {
  209. PowerManagementService powerManagementServiceMock = new PowerManagementService() {
  210. @Override
  211. public boolean isPowerSavingEnabled() {
  212. return false;
  213. }
  214. @Override
  215. public boolean isPowerSavingExclusionAvailable() {
  216. return false;
  217. }
  218. @NonNull
  219. @Override
  220. public BatteryStatus getBattery() {
  221. return new BatteryStatus(true, 100);
  222. }
  223. };
  224. OCUpload ocUpload = new OCUpload(FileStorageUtils.getTemporalPath(account.name) + "/empty.txt",
  225. FOLDER + "charging.txt", account.name);
  226. ocUpload.setWhileChargingOnly(true);
  227. UploadFileOperation newUpload = new UploadFileOperation(
  228. uploadsStorageManager,
  229. connectivityServiceMock,
  230. powerManagementServiceMock,
  231. user,
  232. null,
  233. ocUpload,
  234. NameCollisionPolicy.DEFAULT,
  235. FileUploader.LOCAL_BEHAVIOUR_COPY,
  236. targetContext,
  237. false,
  238. true,
  239. getStorageManager()
  240. );
  241. newUpload.setRemoteFolderToBeCreated();
  242. newUpload.addRenameUploadListener(() -> {
  243. // dummy
  244. });
  245. RemoteOperationResult result = newUpload.execute(client);
  246. assertTrue(result.toString(), result.isSuccess());
  247. }
  248. @Test
  249. public void testUploadOnWifiOnlyButNoWifi() {
  250. ConnectivityService connectivityServiceMock = new ConnectivityService() {
  251. @Override
  252. public boolean isInternetWalled() {
  253. return false;
  254. }
  255. @Override
  256. public Connectivity getConnectivity() {
  257. return new Connectivity(true, false, false, true);
  258. }
  259. };
  260. OCUpload ocUpload = new OCUpload(FileStorageUtils.getTemporalPath(account.name) + "/empty.txt",
  261. FOLDER + "noWifi.txt", account.name);
  262. ocUpload.setUseWifiOnly(true);
  263. UploadFileOperation newUpload = new UploadFileOperation(
  264. uploadsStorageManager,
  265. connectivityServiceMock,
  266. powerManagementServiceMock,
  267. user,
  268. null,
  269. ocUpload,
  270. NameCollisionPolicy.DEFAULT,
  271. FileUploader.LOCAL_BEHAVIOUR_COPY,
  272. targetContext,
  273. true,
  274. false,
  275. getStorageManager()
  276. );
  277. newUpload.setRemoteFolderToBeCreated();
  278. newUpload.addRenameUploadListener(() -> {
  279. // dummy
  280. });
  281. RemoteOperationResult result = newUpload.execute(client);
  282. assertFalse(result.toString(), result.isSuccess());
  283. assertEquals(RemoteOperationResult.ResultCode.DELAYED_FOR_WIFI, result.getCode());
  284. }
  285. @Test
  286. public void testUploadOnWifiOnlyAndWifi() {
  287. OCUpload ocUpload = new OCUpload(FileStorageUtils.getTemporalPath(account.name) + "/empty.txt",
  288. FOLDER + "wifi.txt", account.name);
  289. ocUpload.setWhileChargingOnly(true);
  290. UploadFileOperation newUpload = new UploadFileOperation(
  291. uploadsStorageManager,
  292. connectivityServiceMock,
  293. powerManagementServiceMock,
  294. user,
  295. null,
  296. ocUpload,
  297. NameCollisionPolicy.DEFAULT,
  298. FileUploader.LOCAL_BEHAVIOUR_COPY,
  299. targetContext,
  300. true,
  301. false,
  302. getStorageManager()
  303. );
  304. newUpload.setRemoteFolderToBeCreated();
  305. newUpload.addRenameUploadListener(() -> {
  306. // dummy
  307. });
  308. RemoteOperationResult result = newUpload.execute(client);
  309. assertTrue(result.toString(), result.isSuccess());
  310. // cleanup
  311. new RemoveFileOperation(getStorageManager().getFileByDecryptedRemotePath(FOLDER),
  312. false,
  313. account,
  314. false,
  315. targetContext,
  316. getStorageManager())
  317. .execute(client);
  318. }
  319. @Test
  320. public void testUploadOnWifiOnlyButMeteredWifi() {
  321. ConnectivityService connectivityServiceMock = new ConnectivityService() {
  322. @Override
  323. public boolean isInternetWalled() {
  324. return false;
  325. }
  326. @Override
  327. public Connectivity getConnectivity() {
  328. return new Connectivity(true, true, true, true);
  329. }
  330. };
  331. OCUpload ocUpload = new OCUpload(FileStorageUtils.getTemporalPath(account.name) + "/empty.txt",
  332. FOLDER + "noWifi.txt",
  333. account.name);
  334. ocUpload.setUseWifiOnly(true);
  335. UploadFileOperation newUpload = new UploadFileOperation(
  336. uploadsStorageManager,
  337. connectivityServiceMock,
  338. powerManagementServiceMock,
  339. user,
  340. null,
  341. ocUpload,
  342. NameCollisionPolicy.DEFAULT,
  343. FileUploader.LOCAL_BEHAVIOUR_COPY,
  344. targetContext,
  345. true,
  346. false,
  347. getStorageManager()
  348. );
  349. newUpload.setRemoteFolderToBeCreated();
  350. newUpload.addRenameUploadListener(() -> {
  351. // dummy
  352. });
  353. RemoteOperationResult result = newUpload.execute(client);
  354. assertFalse(result.toString(), result.isSuccess());
  355. assertEquals(RemoteOperationResult.ResultCode.DELAYED_FOR_WIFI, result.getCode());
  356. }
  357. private void verifyStoragePath(OCFile file) {
  358. assertEquals(FileStorageUtils.getSavePath(account.name) + FOLDER + file.getDecryptedFileName(),
  359. file.getStoragePath());
  360. }
  361. }