OC-2067: Adapt CreateRemoteFolderOperation to detect Invalid Characters

This commit is contained in:
masensio 2013-11-13 18:44:25 +01:00
parent 89b37b7508
commit ea0c6a71c5
9 changed files with 88 additions and 50 deletions

View file

@ -7,7 +7,9 @@
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-sdk
android:minSdkVersion="8"

View file

@ -2,9 +2,9 @@
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>
<classpathentry combineaccessrules="false" exported="true" kind="src" path="/oc_framework-test-project"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
<classpathentry combineaccessrules="false" kind="src" path="/oc_framework-test-project"/>
<classpathentry kind="output" path="bin/classes"/>
</classpath>

View file

@ -4,10 +4,10 @@ import java.text.SimpleDateFormat;
import java.util.Date;
import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
import com.owncloud.android.oc_framework.operations.RemoteOperationResult.ResultCode;
import com.owncloud.android.oc_framework_test_project.TestActivity;
import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;
public class CreateFolderTest extends ActivityInstrumentationTestCase2<TestActivity> {
@ -34,17 +34,47 @@ public class CreateFolderTest extends ActivityInstrumentationTestCase2<TestActiv
boolean createFullPath = true;
RemoteOperationResult result = mActivity.createFolder(remotePath, createFullPath);
Log.d("test CreateFolder", "-----------------------" + result.getCode().name());
Log.d("test CreateFolder", "-----------------------" + result.getLogMessage());
assertTrue(result.isSuccess());
assertTrue(result.isSuccess() || result.getCode() == ResultCode.TIMEOUT);
}
public void testCreateFolderSpecialCharacters() {
String remotePath = "/test^^SpecialCharacters" + mCurrentDate;
public void testCreateFolderSpecialCharacters() {
boolean createFullPath = true;
String remotePath = "/testSpecialCharacters_//" + mCurrentDate;
RemoteOperationResult result = mActivity.createFolder(remotePath, createFullPath);
assertFalse(result.isSuccess());
assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
remotePath = "/testSpecialCharacters_\\" + mCurrentDate;
result = mActivity.createFolder(remotePath, createFullPath);
assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
remotePath = "/testSpecialCharacters_<" + mCurrentDate;
result = mActivity.createFolder(remotePath, createFullPath);
assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
remotePath = "/testSpecialCharacters_>" + mCurrentDate;
result = mActivity.createFolder(remotePath, createFullPath);
assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
remotePath = "/testSpecialCharacters_:" + mCurrentDate;
result = mActivity.createFolder(remotePath, createFullPath);
assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
remotePath = "/testSpecialCharacters_\"" + mCurrentDate;
result = mActivity.createFolder(remotePath, createFullPath);
assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
remotePath = "/testSpecialCharacters_|" + mCurrentDate;
result = mActivity.createFolder(remotePath, createFullPath);
assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
remotePath = "/testSpecialCharacters_?" + mCurrentDate;
result = mActivity.createFolder(remotePath, createFullPath);
assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
remotePath = "/testSpecialCharacters_*" + mCurrentDate;
result = mActivity.createFolder(remotePath, createFullPath);
assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
}

View file

@ -2,8 +2,8 @@ package com.owncloud.android.oc_framework_test_project;
import java.io.IOException;
import com.owncloud.android.oc_framework.authentication.AccountUtils.AccountNotFoundException;
import com.owncloud.android.oc_framework.network.OwnCloudClientUtils;
import com.owncloud.android.oc_framework.accounts.AccountUtils.AccountNotFoundException;
import com.owncloud.android.oc_framework.network.webdav.OwnCloudClientFactory;
import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
import com.owncloud.android.oc_framework.operations.remote.CreateRemoteFolderOperation;
@ -34,12 +34,12 @@ public class TestActivity extends Activity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
// This account must exists on the simulator / device
String accountHost = "beta.owncloud.com";
String accountUser = "masensio";
String accountName = accountUser + "@"+ accountHost;
String accountPass = "masensio";
String accountType = "owncloud";
String authorities = "org.owncloud";
AccountManager am = AccountManager.get(this);
@ -51,19 +51,19 @@ public class TestActivity extends Activity {
}
}
if (mAccount == null) {
mAccount = new Account(accountName, accountType);
am.addAccountExplicitly(mAccount, accountPass, null);
am.setUserData(mAccount, "oc_version", "5.0.14");
am.setUserData(mAccount, "oc_base_url", "http://beta.owncloud.com/owncloud");
} else {
// if (mAccount == null) {
// mAccount = new Account(accountName, accountType);
// am.addAccountExplicitly(mAccount, accountPass, null);
// am.setUserData(mAccount, "oc_version", "5.0.14");
// am.setUserData(mAccount, "oc_base_url", "http://beta.owncloud.com/owncloud");
// } else {
Log.d(TAG, "oc_version --->"+ am.getUserData(mAccount, "oc_version") );
Log.d(TAG, "oc_base_url --->"+ am.getUserData(mAccount, "oc_base_url") );
}
// }
try {
mClient = OwnCloudClientUtils.createOwnCloudClient(mAccount, this.getApplicationContext(), authorities);
mClient = OwnCloudClientFactory.createOwnCloudClient(mAccount, this.getApplicationContext());
} catch (OperationCanceledException e) {
Log.e(TAG, "Error while trying to access to " + mAccount.name, e);
e.printStackTrace();

View file

@ -89,7 +89,8 @@ public class RemoteOperationResult implements Serializable {
ACCOUNT_NOT_FOUND,
ACCOUNT_EXCEPTION,
ACCOUNT_NOT_NEW,
ACCOUNT_NOT_THE_SAME
ACCOUNT_NOT_THE_SAME,
INVALID_CHARACTER_IN_NAME
}
private boolean mSuccess = false;
@ -307,6 +308,8 @@ public class RemoteOperationResult implements Serializable {
} else if (mCode == ResultCode.ACCOUNT_NOT_THE_SAME) {
return "Authenticated with a different account than the one updating";
} else if (mCode == ResultCode.INVALID_CHARACTER_IN_NAME) {
return "The file name contains an forbidden character";
}
return "Operation finished with HTTP status code " + mHttpCode + " (" + (isSuccess() ? "success" : "fail") + ")";

View file

@ -1,7 +1,5 @@
package com.owncloud.android.oc_framework.operations.remote;
import java.io.File;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.jackrabbit.webdav.client.methods.MkColMethod;
@ -11,6 +9,8 @@ import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
import com.owncloud.android.oc_framework.network.webdav.WebdavUtils;
import com.owncloud.android.oc_framework.operations.RemoteOperation;
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;
@ -28,7 +28,7 @@ public class CreateRemoteFolderOperation extends RemoteOperation {
private static final int READ_TIMEOUT = 10000;
private static final int CONNECTION_TIMEOUT = 5000;
private static final String PATH_SEPARATOR = "/";
protected String mRemotePath;
protected boolean mCreateFullPath;
@ -54,26 +54,32 @@ public class CreateRemoteFolderOperation extends RemoteOperation {
RemoteOperationResult result = null;
MkColMethod mkcol = null;
try {
mkcol = new MkColMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath));
int status = client.executeMethod(mkcol, READ_TIMEOUT, CONNECTION_TIMEOUT);
if (!mkcol.succeeded() && mkcol.getStatusCode() == HttpStatus.SC_CONFLICT && mCreateFullPath) {
result = createParentFolder(getParentPath(), client);
status = client.executeMethod(mkcol, READ_TIMEOUT, CONNECTION_TIMEOUT); // second (and last) try
}
result = new RemoteOperationResult(mkcol.succeeded(), status, mkcol.getResponseHeaders());
Log.d(TAG, "Create directory " + mRemotePath + ": " + result.getLogMessage());
client.exhaustResponse(mkcol.getResponseBodyAsStream());
} catch (Exception e) {
result = new RemoteOperationResult(e);
Log.e(TAG, "Create directory " + mRemotePath + ": " + result.getLogMessage(), e);
} finally {
if (mkcol != null)
mkcol.releaseConnection();
boolean noInvalidChars = FileUtils.validateName(mRemotePath);
if (noInvalidChars) {
try {
mkcol = new MkColMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath));
int status = client.executeMethod(mkcol, READ_TIMEOUT, CONNECTION_TIMEOUT);
if (!mkcol.succeeded() && mkcol.getStatusCode() == HttpStatus.SC_CONFLICT && mCreateFullPath) {
result = createParentFolder(FileUtils.getParentPath(mRemotePath), client);
status = client.executeMethod(mkcol, READ_TIMEOUT, CONNECTION_TIMEOUT); // second (and last) try
}
result = new RemoteOperationResult(mkcol.succeeded(), status, mkcol.getResponseHeaders());
Log.d(TAG, "Create directory " + mRemotePath + ": " + result.getLogMessage());
client.exhaustResponse(mkcol.getResponseBodyAsStream());
} catch (Exception e) {
result = new RemoteOperationResult(e);
Log.e(TAG, "Create directory " + mRemotePath + ": " + result.getLogMessage(), e);
} finally {
if (mkcol != null)
mkcol.releaseConnection();
}
} else {
result = new RemoteOperationResult(ResultCode.INVALID_CHARACTER_IN_NAME);
}
return result;
}
@ -84,10 +90,6 @@ public class CreateRemoteFolderOperation extends RemoteOperation {
return operation.execute(client);
}
private String getParentPath() {
String parentPath = new File(mRemotePath).getParent();
parentPath = parentPath.endsWith(PATH_SEPARATOR) ? parentPath : parentPath + PATH_SEPARATOR;
return parentPath;
}
}

View file

@ -9,5 +9,5 @@
# Project target.
target=android-19
android.library.reference.1=actionbarsherlock/library
android.library.reference.1=actionbarsherlock\\library
android.library.reference.2=oc_framework

View file

@ -1,4 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry combineaccessrules="false" kind="src" path="/owncloud-android"/>
<classpathentry kind="output" path="bin/classes"/>
</classpath>

View file

@ -20,8 +20,8 @@ package com.owncloud.android.test;
import android.test.AndroidTestCase;
import com.owncloud.android.authentication.AccountUtils;
import com.owncloud.android.utils.OwnCloudVersion;
import com.owncloud.android.oc_framework.accounts.AccountUtils;
import com.owncloud.android.oc_framework.utils.OwnCloudVersion;
public class AccountUtilsTest extends AndroidTestCase {