Bug 725912 - Part 1: Sync key validation + tests. r=rnewman

This commit is contained in:
Chenxia Liu 2012-04-04 17:32:38 -07:00
Родитель 36252b8f57
Коммит 019b1231cb
6 изменённых файлов: 57 добавлений и 4 удалений

Просмотреть файл

@ -69,4 +69,7 @@
<!ENTITY bookmarks.folder.mobile.label 'Mobile Bookmarks'>
<!-- Notification strings -->
<!ENTITY sync.notification.oneaccount.label 'Only one &syncBrand.fullName.label; account is supported.'>
<!ENTITY sync.notification.oneaccount.label 'Only one &syncBrand.fullName.label; account is supported.'>
<!-- Incorrect settings and changing credentials. -->
<!ENTITY sync.new.recoverykey.status.incorrect 'Recovery Key incorrect. Please try again.'>

Просмотреть файл

@ -0,0 +1,9 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko.sync.setup;
public class InvalidSyncKeyException extends Exception {
private static final long serialVersionUID = -6504925951580479894L;
}

Просмотреть файл

@ -8,6 +8,7 @@ import java.util.Locale;
import org.mozilla.gecko.R;
import org.mozilla.gecko.sync.setup.Constants;
import org.mozilla.gecko.sync.setup.InvalidSyncKeyException;
import org.mozilla.gecko.sync.setup.SyncAccounts;
import android.accounts.AccountAuthenticatorActivity;
@ -24,6 +25,7 @@ import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Toast;
public class AccountActivity extends AccountAuthenticatorActivity {
private final static String LOG_TAG = "AccountActivity";
@ -105,13 +107,22 @@ public class AccountActivity extends AccountAuthenticatorActivity {
*/
public void connectClickHandler(View target) {
Log.d(LOG_TAG, "connectClickHandler for view " + target);
enableCredEntry(false);
// Validate sync key format.
try {
key = ActivityUtils.validateSyncKey(synckeyInput.getText().toString());
} catch (InvalidSyncKeyException e) {
enableCredEntry(true);
// Toast: invalid sync key format.
Toast toast = Toast.makeText(mContext, R.string.sync_new_recoverykey_status_incorrect, Toast.LENGTH_LONG);
toast.show();
return;
}
username = usernameInput.getText().toString().toLowerCase(Locale.US);
password = passwordInput.getText().toString();
key = synckeyInput.getText().toString();
if (serverCheckbox.isChecked()) {
server = serverInput.getText().toString();
}
enableCredEntry(false);
// TODO : Authenticate with Sync Service, once implemented, with
// onAuthSuccess as callback

Просмотреть файл

@ -0,0 +1,27 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko.sync.setup.activities;
import org.mozilla.gecko.sync.setup.InvalidSyncKeyException;
public class ActivityUtils {
/**
* Sync key should be a 26-character string, and can include arbitrary
* capitalization and hyphenation.
*
* @param String
* sync key Sync key entered by user in account setup.
* @return String formatted key Sync key in correct format (lower-cased and
* de-hyphenated)
* @throws InvalidSyncKeyException
*/
public static String validateSyncKey(String key) throws InvalidSyncKeyException {
String charKey = key.trim().replace("-", "").toLowerCase();
if (!charKey.matches("^[abcdefghijkmnpqrstuvwxyz23456789]{26}$")) {
throw new InvalidSyncKeyException();
}
return charKey;
}
}

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

Просмотреть файл

@ -63,3 +63,6 @@
<!-- Push tab to device strings -->
<string name="sync_new_tab">&new_tab;</string>
<!-- Incorrect settings and changing credentials. -->
<string name="sync.new.recoverykey.status.incorrect">&sync.new.recoverykey.status.incorrect;</string>