Преглед на файлове

OC-2115: Validate Path in RenameRemoteFileOperation

masensio преди 11 години
родител
ревизия
0b95d61cec

+ 27 - 38
oc_framework-test-project/oc_framework-test-test/src/com/owncloud/android/oc_framework_test_project/test/RenameFileTest.java

@@ -1,10 +1,7 @@
 package com.owncloud.android.oc_framework_test_project.test;
 
-import java.io.File;
-
 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
 import com.owncloud.android.oc_framework.operations.RemoteOperationResult.ResultCode;
-import com.owncloud.android.oc_framework.utils.FileUtils;
 import com.owncloud.android.oc_framework_test_project.TestActivity;
 
 import android.test.ActivityInstrumentationTestCase2;
@@ -13,16 +10,16 @@ public class RenameFileTest extends ActivityInstrumentationTestCase2<TestActivit
 
 	/* Folder data to rename. This folder must exist on the account */
 	private final String mOldFolderName = "folderToRename";
-	private final String mOldFolderPath = "/folderToRename/";
+	private final String mOldFolderPath = "/folderToRename";
 	private final String mNewFolderName = "renamedFolder"; 
-	private final String mNewFolderPath = "/renameFolder";
+	private final String mNewFolderPath = "/renamedFolder";
 	
 	/* File data to rename. This file must exist on the account */
 	private final String mOldFileName = "fileToRename.png";
 	private final String mOldFilePath = "/fileToRename.png";
 	private final String mNewFileName = "renamedFile";
-	private final String mNewFilePath = "/renamedFile";
 	private final String mFileExtension = ".png";
+	private final String mNewFilePath ="/renamedFile.png";
 	
 	
 	private TestActivity mActivity;
@@ -45,49 +42,45 @@ public class RenameFileTest extends ActivityInstrumentationTestCase2<TestActivit
 	public void testRenameFolder() {
 
 		RemoteOperationResult result = mActivity.renameFile(mOldFolderName, mOldFolderPath, 
-				mNewFolderName, mNewFolderPath + FileUtils.PATH_SEPARATOR);
+				mNewFolderName, true);
 		assertTrue(result.isSuccess());
 	}
 	
 	/**
-	 * Test Rename Folder with forbidden characters : /  \  < >  :  "  |  ?  *
+	 * Test Rename Folder with forbidden characters : \  < >  :  "  |  ?  *
 	 */
 	public void testRenameFolderForbiddenChars() {
-
-		RemoteOperationResult result = mActivity.renameFile(mOldFolderName, mOldFolderPath, 
-				mNewFolderName + "//", mNewFolderPath + "//" + FileUtils.PATH_SEPARATOR);
-		assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
 		
-		result = mActivity.renameFile(mOldFolderName, mOldFolderPath, 
-				mNewFolderName + "\\", mNewFolderPath + "\\" + FileUtils.PATH_SEPARATOR);
+		RemoteOperationResult result = mActivity.renameFile(mOldFolderName, mOldFolderPath, 
+				mNewFolderName + "\\", true);
 		assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
 		
 		result = mActivity.renameFile(mOldFolderName, mOldFolderPath, 
-				mNewFolderName + "<", mNewFolderPath + "<" + FileUtils.PATH_SEPARATOR);
+				mNewFolderName + "<", true);
 		assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
 		
 		result = mActivity.renameFile(mOldFolderName, mOldFolderPath, 
-				mNewFolderName + ">", mNewFolderPath + ">" + FileUtils.PATH_SEPARATOR);
+				mNewFolderName + ">", true);
 		assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
 		
 		result = mActivity.renameFile(mOldFolderName, mOldFolderPath, 
-				mNewFolderName + ":", mNewFolderPath + ":" + FileUtils.PATH_SEPARATOR);
+				mNewFolderName + ":", true);
 		assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
 		
 		result = mActivity.renameFile(mOldFolderName, mOldFolderPath, 
-				mNewFolderName + "\"", mNewFolderPath + "\"" + FileUtils.PATH_SEPARATOR);
+				mNewFolderName + "\"", true);
 		assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
 		
 		result = mActivity.renameFile(mOldFolderName, mOldFolderPath, 
-				mNewFolderName + "|", mNewFolderPath + "|" + FileUtils.PATH_SEPARATOR);
+				mNewFolderName + "|", true);
 		assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
 		
 		result = mActivity.renameFile(mOldFolderName, mOldFolderPath, 
-				mNewFolderName + "?", mNewFolderPath + "?" + FileUtils.PATH_SEPARATOR);
+				mNewFolderName + "?", true);
 		assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
 		
 		result = mActivity.renameFile(mOldFolderName, mOldFolderPath, 
-				mNewFolderName + "*", mNewFolderPath + "*" + FileUtils.PATH_SEPARATOR);
+				mNewFolderName + "*", true);
 		assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
 	}
 	
@@ -96,49 +89,45 @@ public class RenameFileTest extends ActivityInstrumentationTestCase2<TestActivit
 	 */
 	public void testRenameFile() {
 		RemoteOperationResult result = mActivity.renameFile(mOldFileName, mOldFilePath, 
-				mNewFileName + mFileExtension, mNewFilePath + mFileExtension);
+				mNewFileName + mFileExtension, false);
 		assertTrue(result.isSuccess());
 	}
 	
 	
 	/**
-	 * Test Rename Folder with forbidden characters: /  \  < >  :  "  |  ?  *
+	 * Test Rename Folder with forbidden characters: \  < >  :  "  |  ?  *
 	 */
-	public void testRenameFileForbiddenChars() {
+	public void testRenameFileForbiddenChars() {		
 		RemoteOperationResult result = mActivity.renameFile(mOldFileName, mOldFilePath, 
-				mNewFileName + "//" + mFileExtension, mNewFilePath + "//" + mFileExtension);
-		assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
-		
-		result = mActivity.renameFile(mOldFileName, mOldFilePath, 
-				mNewFileName + "\\" + mFileExtension, mNewFilePath + "\\" + mFileExtension);
+				mNewFileName + "\\" + mFileExtension, false);
 		assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
 		
 		result = mActivity.renameFile(mOldFileName, mOldFilePath, 
-				mNewFileName + "<" + mFileExtension, mNewFilePath + "<" + mFileExtension);
+				mNewFileName + "<" + mFileExtension, false);
 		assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
 		
 		result = mActivity.renameFile(mOldFileName, mOldFilePath, 
-				mNewFileName + ">" + mFileExtension, mNewFilePath + ">" + mFileExtension);
+				mNewFileName + ">" + mFileExtension, false);
 		assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
 		
 		result = mActivity.renameFile(mOldFileName, mOldFilePath, 
-				mNewFileName + ":" + mFileExtension, mNewFilePath + ":" + mFileExtension);
+				mNewFileName + ":" + mFileExtension, false);
 		assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
 		
 		result = mActivity.renameFile(mOldFileName, mOldFilePath, 
-				mNewFileName + "\"" + mFileExtension, mNewFilePath + "\"" + mFileExtension);
+				mNewFileName + "\"" + mFileExtension, false);
 		assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
 		
 		result = mActivity.renameFile(mOldFileName, mOldFilePath, 
-				mNewFileName + "|" + mFileExtension, mNewFilePath + "|" + mFileExtension);
+				mNewFileName + "|" + mFileExtension, false);
 		assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
 		
 		result = mActivity.renameFile(mOldFileName, mOldFilePath, 
-				mNewFileName + "?" + mFileExtension, mNewFilePath + "?" + mFileExtension);
+				mNewFileName + "?" + mFileExtension, false);
 		assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
 		
 		result = mActivity.renameFile(mOldFileName, mOldFilePath, 
-				mNewFileName + "*" + mFileExtension, mNewFilePath + "*" + mFileExtension);
+				mNewFileName + "*" + mFileExtension, false);
 		assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
 		
 	}
@@ -148,10 +137,10 @@ public class RenameFileTest extends ActivityInstrumentationTestCase2<TestActivit
 	 * Restore initial conditions
 	 */
 	public void testRestoreInitialConditions() {
-		RemoteOperationResult result = mActivity.renameFile(mNewFolderName, mNewFolderPath, mOldFolderName, mOldFolderPath);
+		RemoteOperationResult result = mActivity.renameFile(mNewFolderName, mNewFolderPath, mOldFolderName, true);
 		assertTrue(result.isSuccess());
 		
-		result = mActivity.renameFile(mNewFileName + mFileExtension, mNewFilePath + mFileExtension, mOldFileName, mOldFilePath);
+		result = mActivity.renameFile(mNewFileName + mFileExtension, mNewFilePath, mOldFileName, false);
 		assertTrue(result.isSuccess());
 	}
 	

+ 3 - 3
oc_framework-test-project/src/com/owncloud/android/oc_framework_test_project/TestActivity.java

@@ -87,14 +87,14 @@ public class TestActivity extends Activity {
 	 * @param oldName			Old name of the file.
      * @param oldRemotePath		Old remote path of the file. For folders it starts and ends by "/"
      * @param newName			New name to set as the name of file.
-     * @param newRemotePath		New remote path to move the file, for folders it starts and ends by "/"
+     * @param isFolder			'true' for folder and 'false' for files
      * 
      * @return
      */
 
-	public RemoteOperationResult renameFile(String oldName, String oldRemotePath, String newName, String newRemotePath) {
+	public RemoteOperationResult renameFile(String oldName, String oldRemotePath, String newName, boolean isFolder) {
 		
-		RenameRemoteFileOperation renameOperation = new RenameRemoteFileOperation(oldName, oldRemotePath, newName, newRemotePath);
+		RenameRemoteFileOperation renameOperation = new RenameRemoteFileOperation(oldName, oldRemotePath, newName, isFolder);
 		RemoteOperationResult result = renameOperation.execute(mClient);
 		
 		return result;

+ 13 - 4
oc_framework/src/com/owncloud/android/oc_framework/operations/remote/RenameRemoteFileOperation.java

@@ -1,5 +1,7 @@
 package com.owncloud.android.oc_framework.operations.remote;
 
+import java.io.File;
+
 import org.apache.jackrabbit.webdav.client.methods.DavMethodBase;
 
 import android.util.Log;
@@ -35,15 +37,21 @@ public class RenameRemoteFileOperation extends RemoteOperation {
      * Constructor
      * 
      * @param oldName			Old name of the file.
-     * @param oldRemotePath		Old remote path of the file. For folders it starts and ends by "/"
+     * @param oldRemotePath		Old remote path of the file. 
      * @param newName			New name to set as the name of file.
-     * @param newRemotePath		New remote path to move the file, for folders it starts and ends by "/"
+     * @param isFolder			'true' for folder and 'false' for files
      */
-	public RenameRemoteFileOperation(String oldName, String oldRemotePath, String newName, String newRemotePath) {
+	public RenameRemoteFileOperation(String oldName, String oldRemotePath, String newName, boolean isFolder) {
 		mOldName = oldName;
 		mOldRemotePath = oldRemotePath;
 		mNewName = newName;
-		mNewRemotePath = newRemotePath;
+		
+        String parent = (new File(mOldRemotePath)).getParent();
+        parent = (parent.endsWith(FileUtils.PATH_SEPARATOR)) ? parent : parent + FileUtils.PATH_SEPARATOR; 
+        mNewRemotePath =  parent + mNewName;
+        if (isFolder) {
+            mNewRemotePath += FileUtils.PATH_SEPARATOR;
+        }
 	}
 
 	 /**
@@ -66,6 +74,7 @@ public class RenameRemoteFileOperation extends RemoteOperation {
                 return new RemoteOperationResult(ResultCode.OK);
             }
         
+            
             // check if a file with the new name already exists
             if (client.existsFile(mNewRemotePath)) {
             	return new RemoteOperationResult(ResultCode.INVALID_OVERWRITE);

+ 2 - 2
src/com/owncloud/android/operations/RenameFileOperation.java

@@ -98,8 +98,8 @@ public class RenameFileOperation extends RemoteOperation {
                 return new RemoteOperationResult(ResultCode.INVALID_OVERWRITE);
             }
             
-            RenameRemoteFileOperation operation = new RenameRemoteFileOperation(mFile.getFileName(), mFile.getRemotePath(), mNewName, 
-                    mNewRemotePath);
+            RenameRemoteFileOperation operation = new RenameRemoteFileOperation(mFile.getFileName(), mFile.getRemotePath(), 
+                    mNewName, mFile.isFolder());
             result = operation.execute(client);
 
             if (result.isSuccess()) {