mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 18:59:06 +01:00
Resolve merge conflicts.
# Conflicts: # briar-android/build.gradle # briar-android/src/test/java/org/briarproject/briar/android/login/SetupActivityTest.java
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
package org.briarproject.briar.android.login;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.support.design.widget.TextInputLayout;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
@@ -28,12 +26,8 @@ import static org.briarproject.bramble.api.crypto.PasswordStrengthEstimator.QUIT
|
||||
import static org.briarproject.bramble.api.crypto.PasswordStrengthEstimator.QUITE_WEAK;
|
||||
import static org.briarproject.bramble.api.crypto.PasswordStrengthEstimator.STRONG;
|
||||
import static org.briarproject.bramble.api.crypto.PasswordStrengthEstimator.WEAK;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.timeout;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
@@ -43,8 +37,6 @@ import static org.mockito.Mockito.when;
|
||||
packageName = "org.briarproject.briar")
|
||||
public class ChangePasswordActivityTest {
|
||||
|
||||
private static final int TIMEOUT_MS = 10 * 1000;
|
||||
|
||||
private TestChangePasswordActivity changePasswordActivity;
|
||||
private TextInputLayout passwordConfirmationWrapper;
|
||||
private EditText currentPassword;
|
||||
@@ -55,8 +47,6 @@ public class ChangePasswordActivityTest {
|
||||
|
||||
@Mock
|
||||
private PasswordController passwordController;
|
||||
@Mock
|
||||
private SetupController setupController;
|
||||
@Captor
|
||||
private ArgumentCaptor<ResultHandler<Boolean>> resultCaptor;
|
||||
|
||||
@@ -108,12 +98,9 @@ public class ChangePasswordActivityTest {
|
||||
|
||||
@Test
|
||||
public void testChangePasswordUI() {
|
||||
PasswordController mockedPasswordController = this.passwordController;
|
||||
SetupController mockedSetupController = this.setupController;
|
||||
changePasswordActivity.setPasswordController(mockedPasswordController);
|
||||
changePasswordActivity.setSetupController(mockedSetupController);
|
||||
changePasswordActivity.setPasswordController(passwordController);
|
||||
// Mock strong password strength answer
|
||||
when(mockedSetupController.estimatePasswordStrength(anyString()))
|
||||
when(passwordController.estimatePasswordStrength(anyString()))
|
||||
.thenReturn(STRONG);
|
||||
String curPass = "old.password";
|
||||
String safePass = "really.safe.password";
|
||||
@@ -125,7 +112,7 @@ public class ChangePasswordActivityTest {
|
||||
changePasswordButton.performClick();
|
||||
// Verify that the controller's method was called with the correct
|
||||
// params and get the callback
|
||||
verify(mockedPasswordController, times(1))
|
||||
verify(passwordController, times(1))
|
||||
.changePassword(eq(curPass), eq(safePass),
|
||||
resultCaptor.capture());
|
||||
// execute the callbacks
|
||||
@@ -133,89 +120,39 @@ public class ChangePasswordActivityTest {
|
||||
assertEquals(changePasswordActivity.isFinishing(), true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPasswordChange() {
|
||||
PasswordController passwordController =
|
||||
changePasswordActivity.getPasswordController();
|
||||
SetupController setupController =
|
||||
changePasswordActivity.getSetupController();
|
||||
// mock a resulthandler
|
||||
ResultHandler<Void> resultHandler =
|
||||
(ResultHandler<Void>) mock(ResultHandler.class);
|
||||
setupController.storeAuthorInfo("nick", "some.old.pass", resultHandler);
|
||||
// blocking verification call with timeout that waits until the mocked
|
||||
// result gets called with handle 0L, the expected value
|
||||
verify(resultHandler, timeout(TIMEOUT_MS).times(1)).onResult(null);
|
||||
SharedPreferences prefs = changePasswordActivity
|
||||
.getSharedPreferences("db", Context.MODE_PRIVATE);
|
||||
// Confirm database key
|
||||
assertTrue(prefs.contains("key"));
|
||||
String oldKey = prefs.getString("key", null);
|
||||
// mock a resulthandler
|
||||
ResultHandler<Boolean> resultHandler2 =
|
||||
(ResultHandler<Boolean>) mock(ResultHandler.class);
|
||||
passwordController.changePassword("some.old.pass", "some.strong.pass",
|
||||
resultHandler2);
|
||||
// blocking verification call with timeout that waits until the mocked
|
||||
// result gets called with handle 0L, the expected value
|
||||
verify(resultHandler2, timeout(TIMEOUT_MS).times(1)).onResult(true);
|
||||
// Confirm database key
|
||||
assertTrue(prefs.contains("key"));
|
||||
assertNotEquals(oldKey, prefs.getString("key", null));
|
||||
// Note that Robolectric uses its own persistant storage that it
|
||||
// wipes clean after each test run, no need to clean up manually.
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStrengthMeter() {
|
||||
SetupController controller =
|
||||
changePasswordActivity.getSetupController();
|
||||
|
||||
String strongPass = "very.strong.password.123";
|
||||
String weakPass = "we";
|
||||
String quiteStrongPass = "quite.strong";
|
||||
|
||||
float val = controller.estimatePasswordStrength(strongPass);
|
||||
assertTrue(val == STRONG);
|
||||
val = controller.estimatePasswordStrength(weakPass);
|
||||
assertTrue(val < WEAK && val > NONE);
|
||||
val = controller.estimatePasswordStrength(quiteStrongPass);
|
||||
assertTrue(val < STRONG && val > QUITE_WEAK);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStrengthMeterUI() {
|
||||
Assert.assertNotNull(changePasswordActivity);
|
||||
// replace the setup controller with our mocked copy
|
||||
SetupController mockedController = this.setupController;
|
||||
changePasswordActivity.setSetupController(mockedController);
|
||||
// replace the password controller with our mocked copy
|
||||
changePasswordActivity.setPasswordController(passwordController);
|
||||
// Mock answers for UI testing only
|
||||
when(mockedController.estimatePasswordStrength("strong")).thenReturn(
|
||||
when(passwordController.estimatePasswordStrength("strong")).thenReturn(
|
||||
STRONG);
|
||||
when(mockedController.estimatePasswordStrength("qstrong")).thenReturn(
|
||||
when(passwordController.estimatePasswordStrength("qstrong")).thenReturn(
|
||||
QUITE_STRONG);
|
||||
when(mockedController.estimatePasswordStrength("qweak")).thenReturn(
|
||||
when(passwordController.estimatePasswordStrength("qweak")).thenReturn(
|
||||
QUITE_WEAK);
|
||||
when(mockedController.estimatePasswordStrength("weak")).thenReturn(
|
||||
when(passwordController.estimatePasswordStrength("weak")).thenReturn(
|
||||
WEAK);
|
||||
when(mockedController.estimatePasswordStrength("empty")).thenReturn(
|
||||
when(passwordController.estimatePasswordStrength("empty")).thenReturn(
|
||||
NONE);
|
||||
// Test the meters progress and color for several values
|
||||
testStrengthMeter("strong", STRONG, StrengthMeter.GREEN);
|
||||
Mockito.verify(mockedController, Mockito.times(1))
|
||||
Mockito.verify(passwordController, Mockito.times(1))
|
||||
.estimatePasswordStrength(eq("strong"));
|
||||
testStrengthMeter("qstrong", QUITE_STRONG, StrengthMeter.LIME);
|
||||
Mockito.verify(mockedController, Mockito.times(1))
|
||||
Mockito.verify(passwordController, Mockito.times(1))
|
||||
.estimatePasswordStrength(eq("qstrong"));
|
||||
testStrengthMeter("qweak", QUITE_WEAK, StrengthMeter.YELLOW);
|
||||
Mockito.verify(mockedController, Mockito.times(1))
|
||||
Mockito.verify(passwordController, Mockito.times(1))
|
||||
.estimatePasswordStrength(eq("qweak"));
|
||||
testStrengthMeter("weak", WEAK, StrengthMeter.ORANGE);
|
||||
Mockito.verify(mockedController, Mockito.times(1))
|
||||
Mockito.verify(passwordController, Mockito.times(1))
|
||||
.estimatePasswordStrength(eq("weak"));
|
||||
// Not sure this should be the correct behaviour on an empty input ?
|
||||
testStrengthMeter("empty", NONE, StrengthMeter.RED);
|
||||
Mockito.verify(mockedController, Mockito.times(1))
|
||||
Mockito.verify(passwordController, Mockito.times(1))
|
||||
.estimatePasswordStrength(eq("empty"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
package org.briarproject.briar.android.login;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
|
||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||
import org.briarproject.bramble.api.crypto.PasswordStrengthEstimator;
|
||||
import org.briarproject.bramble.api.db.DatabaseConfig;
|
||||
import org.briarproject.bramble.test.BrambleMockTestCase;
|
||||
import org.briarproject.bramble.test.ImmediateExecutor;
|
||||
import org.briarproject.briar.android.controller.handler.ResultHandler;
|
||||
import org.jmock.Expectations;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import static junit.framework.Assert.assertFalse;
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
import static org.briarproject.bramble.test.TestUtils.getSecretKey;
|
||||
|
||||
public class PasswordControllerImplTest extends BrambleMockTestCase {
|
||||
|
||||
private final SharedPreferences briarPrefs =
|
||||
context.mock(SharedPreferences.class);
|
||||
private final DatabaseConfig databaseConfig =
|
||||
context.mock(DatabaseConfig.class);
|
||||
private final CryptoComponent crypto = context.mock(CryptoComponent.class);
|
||||
private final PasswordStrengthEstimator estimator =
|
||||
context.mock(PasswordStrengthEstimator.class);
|
||||
private final SharedPreferences.Editor editor =
|
||||
context.mock(SharedPreferences.Editor.class);
|
||||
|
||||
private final Executor cryptoExecutor = new ImmediateExecutor();
|
||||
|
||||
private final String oldPassword = "some.old.pass";
|
||||
private final String newPassword = "some.new.pass";
|
||||
private final String oldEncryptedHex = "010203";
|
||||
private final String newEncryptedHex = "020304";
|
||||
private final byte[] oldEncryptedBytes = new byte[] {1, 2, 3};
|
||||
private final byte[] newEncryptedBytes = new byte[] {2, 3, 4};
|
||||
private final byte[] keyBytes = getSecretKey().getBytes();
|
||||
|
||||
@Test
|
||||
public void testChangePasswordReturnsTrue() {
|
||||
context.checking(new Expectations() {{
|
||||
// Look up the encrypted DB key
|
||||
oneOf(briarPrefs).getString("key", null);
|
||||
will(returnValue(oldEncryptedHex));
|
||||
// Decrypt and re-encrypt the key
|
||||
oneOf(crypto).decryptWithPassword(oldEncryptedBytes, oldPassword);
|
||||
will(returnValue(keyBytes));
|
||||
oneOf(crypto).encryptWithPassword(keyBytes, newPassword);
|
||||
will(returnValue(newEncryptedBytes));
|
||||
// Store the re-encrypted key
|
||||
oneOf(briarPrefs).edit();
|
||||
will(returnValue(editor));
|
||||
oneOf(editor).putString("key", newEncryptedHex);
|
||||
oneOf(editor).apply();
|
||||
}});
|
||||
|
||||
PasswordControllerImpl p = new PasswordControllerImpl(briarPrefs,
|
||||
databaseConfig, cryptoExecutor, crypto, estimator);
|
||||
|
||||
final AtomicBoolean capturedResult = new AtomicBoolean(false);
|
||||
p.changePassword(oldPassword, newPassword,
|
||||
new ResultHandler<Boolean>() {
|
||||
@Override
|
||||
public void onResult(Boolean result) {
|
||||
capturedResult.set(result);
|
||||
}
|
||||
});
|
||||
assertTrue(capturedResult.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChangePasswordReturnsFalseIfOldPasswordIsWrong() {
|
||||
context.checking(new Expectations() {{
|
||||
// Look up the encrypted DB key
|
||||
oneOf(briarPrefs).getString("key", null);
|
||||
will(returnValue(oldEncryptedHex));
|
||||
// Try to decrypt the key - the password is wrong
|
||||
oneOf(crypto).decryptWithPassword(oldEncryptedBytes, oldPassword);
|
||||
will(returnValue(null));
|
||||
}});
|
||||
|
||||
PasswordControllerImpl p = new PasswordControllerImpl(briarPrefs,
|
||||
databaseConfig, cryptoExecutor, crypto, estimator);
|
||||
|
||||
final AtomicBoolean capturedResult = new AtomicBoolean(true);
|
||||
p.changePassword(oldPassword, newPassword,
|
||||
new ResultHandler<Boolean>() {
|
||||
@Override
|
||||
public void onResult(Boolean result) {
|
||||
capturedResult.set(result);
|
||||
}
|
||||
});
|
||||
assertFalse(capturedResult.get());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package org.briarproject.briar.android.login;
|
||||
|
||||
import android.support.design.widget.TextInputLayout;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.TestBriarApplication;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static org.briarproject.bramble.api.crypto.PasswordStrengthEstimator.NONE;
|
||||
import static org.briarproject.bramble.api.crypto.PasswordStrengthEstimator.QUITE_STRONG;
|
||||
import static org.briarproject.bramble.api.crypto.PasswordStrengthEstimator.QUITE_WEAK;
|
||||
import static org.briarproject.bramble.api.crypto.PasswordStrengthEstimator.STRONG;
|
||||
import static org.briarproject.bramble.api.crypto.PasswordStrengthEstimator.WEAK;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.robolectric.shadows.support.v4.SupportFragmentTestUtil.startFragment;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(sdk = 21, application = TestBriarApplication.class,
|
||||
packageName = "org.briarproject.briar")
|
||||
public class PasswordFragmentTest {
|
||||
|
||||
private PasswordFragment passwordFragment = new PasswordFragment();
|
||||
private EditText passwordEntry;
|
||||
private EditText passwordConfirmation;
|
||||
private TextInputLayout passwordConfirmationWrapper;
|
||||
private StrengthMeter strengthMeter;
|
||||
private Button createAccountButton;
|
||||
|
||||
@Mock
|
||||
private SetupController setupController;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
startFragment(passwordFragment, SetupActivity.class);
|
||||
|
||||
View v = passwordFragment.getView();
|
||||
passwordEntry = (EditText) v.findViewById(R.id.password_entry);
|
||||
passwordConfirmation = (EditText) v.findViewById(R.id.password_confirm);
|
||||
passwordConfirmationWrapper =
|
||||
(TextInputLayout) v.findViewById(R.id.password_confirm_wrapper);
|
||||
strengthMeter = (StrengthMeter) v.findViewById(R.id.strength_meter);
|
||||
createAccountButton = (Button) v.findViewById(R.id.next);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateAccountUI() {
|
||||
String safePass = "really.safe.password";
|
||||
|
||||
passwordFragment.setupController = setupController;
|
||||
when(setupController.needsDozeWhitelisting()).thenReturn(false);
|
||||
when(setupController.estimatePasswordStrength(safePass))
|
||||
.thenReturn(STRONG);
|
||||
|
||||
passwordEntry.setText(safePass);
|
||||
passwordConfirmation.setText(safePass);
|
||||
// Confirm that the create account button is clickable
|
||||
assertEquals(createAccountButton.isEnabled(), true);
|
||||
createAccountButton.performClick();
|
||||
|
||||
// assert controller has been called properly
|
||||
verify(setupController, times(1)).setPassword(safePass);
|
||||
verify(setupController, times(1)).showDozeOrCreateAccount();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStrengthMeterUI() {
|
||||
// Test the meters' progress and color for several values
|
||||
testStrengthMeter("1234567890ab", STRONG, StrengthMeter.GREEN);
|
||||
testStrengthMeter("123456789", QUITE_STRONG, StrengthMeter.LIME);
|
||||
testStrengthMeter("123456", QUITE_WEAK, StrengthMeter.YELLOW);
|
||||
testStrengthMeter("123", WEAK, StrengthMeter.ORANGE);
|
||||
testStrengthMeter("", NONE, StrengthMeter.RED);
|
||||
}
|
||||
|
||||
private void testStrengthMeter(String pass, float strength, int color) {
|
||||
passwordEntry.setText(pass);
|
||||
assertEquals(strengthMeter.getProgress(),
|
||||
(int) (strengthMeter.getMax() * strength));
|
||||
assertEquals(color, strengthMeter.getColor());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testPasswordMatchUI() {
|
||||
// Password mismatch
|
||||
passwordEntry.setText("really.safe.password");
|
||||
passwordConfirmation.setText("really.safe.pass");
|
||||
assertEquals(createAccountButton.isEnabled(), false);
|
||||
assertEquals(passwordConfirmationWrapper.getError(),
|
||||
passwordFragment.getString(R.string.passwords_do_not_match));
|
||||
// Button enabled
|
||||
passwordEntry.setText("really.safe.pass");
|
||||
passwordConfirmation.setText("really.safe.pass");
|
||||
// Confirm that the password mismatch error message is not visible
|
||||
assertNotEquals(passwordConfirmationWrapper.getError(),
|
||||
passwordFragment.getString(R.string.passwords_do_not_match));
|
||||
// Passwords match, so button should be enabled
|
||||
assertEquals(createAccountButton.isEnabled(), true);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,29 +1,18 @@
|
||||
package org.briarproject.briar.android.login;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.support.design.widget.TextInputLayout;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.TestBriarApplication;
|
||||
import org.briarproject.briar.android.controller.handler.ResultHandler;
|
||||
import org.briarproject.briar.android.navdrawer.NavDrawerActivity;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.Robolectric;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.shadows.ShadowActivity;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static org.briarproject.bramble.api.crypto.PasswordStrengthEstimator.NONE;
|
||||
@@ -48,95 +37,18 @@ import static org.robolectric.Shadows.shadowOf;
|
||||
packageName = "org.briarproject.briar")
|
||||
public class SetupActivityTest {
|
||||
|
||||
private static final int TIMEOUT_MS = 10 * 1000;
|
||||
|
||||
private TestSetupActivity setupActivity;
|
||||
private SetupActivity setupActivity;
|
||||
private TextInputLayout nicknameEntryWrapper;
|
||||
private TextInputLayout passwordConfirmationWrapper;
|
||||
private EditText nicknameEntry;
|
||||
private EditText passwordEntry;
|
||||
private EditText passwordConfirmation;
|
||||
private StrengthMeter strengthMeter;
|
||||
private Button createAccountButton;
|
||||
|
||||
@Mock
|
||||
private SetupController setupController;
|
||||
@Captor
|
||||
private ArgumentCaptor<ResultHandler<Void>> authorCaptor;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
setupActivity = Robolectric.setupActivity(TestSetupActivity.class);
|
||||
setupActivity = Robolectric.setupActivity(SetupActivity.class);
|
||||
nicknameEntryWrapper = (TextInputLayout) setupActivity
|
||||
.findViewById(R.id.nickname_entry_wrapper);
|
||||
passwordConfirmationWrapper = (TextInputLayout) setupActivity
|
||||
.findViewById(R.id.password_confirm_wrapper);
|
||||
nicknameEntry =
|
||||
(EditText) setupActivity.findViewById(R.id.nickname_entry);
|
||||
passwordEntry =
|
||||
(EditText) setupActivity.findViewById(R.id.password_entry);
|
||||
passwordConfirmation =
|
||||
(EditText) setupActivity.findViewById(R.id.password_confirm);
|
||||
strengthMeter =
|
||||
(StrengthMeter) setupActivity.findViewById(R.id.strength_meter);
|
||||
createAccountButton =
|
||||
(Button) setupActivity.findViewById(R.id.create_account);
|
||||
}
|
||||
|
||||
private void testStrengthMeter(String pass, float strength, int color) {
|
||||
passwordEntry.setText(pass);
|
||||
assertEquals(strengthMeter.getProgress(),
|
||||
(int) (strengthMeter.getMax() * strength));
|
||||
assertEquals(color, strengthMeter.getColor());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPasswordMatchUI() {
|
||||
// Password mismatch
|
||||
passwordEntry.setText("really.safe.password");
|
||||
passwordConfirmation.setText("really.safe.pass");
|
||||
assertEquals(createAccountButton.isEnabled(), false);
|
||||
assertEquals(passwordConfirmationWrapper.getError(),
|
||||
setupActivity.getString(R.string.passwords_do_not_match));
|
||||
// Button enabled
|
||||
passwordEntry.setText("really.safe.pass");
|
||||
passwordConfirmation.setText("really.safe.pass");
|
||||
// Confirm that the password mismatch error message is not visible
|
||||
Assert.assertNotEquals(passwordConfirmationWrapper.getError(),
|
||||
setupActivity.getString(R.string.passwords_do_not_match));
|
||||
// Nick has not been set, expect the button to be disabled
|
||||
assertEquals(createAccountButton.isEnabled(), false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateAccountUI() {
|
||||
SetupController mockedController = this.setupController;
|
||||
setupActivity.setController(mockedController);
|
||||
// Mock strong password strength answer
|
||||
when(mockedController.estimatePasswordStrength(anyString())).thenReturn(
|
||||
STRONG);
|
||||
String safePass = "really.safe.password";
|
||||
String nick = "nick.nickerton";
|
||||
passwordEntry.setText(safePass);
|
||||
passwordConfirmation.setText(safePass);
|
||||
nicknameEntry.setText(nick);
|
||||
// Confirm that the create account button is clickable
|
||||
assertEquals(createAccountButton.isEnabled(), true);
|
||||
createAccountButton.performClick();
|
||||
// Verify that the controller's method was called with the correct
|
||||
// params and get the callback
|
||||
verify(mockedController, times(1))
|
||||
.storeAuthorInfo(eq(nick), eq(safePass),
|
||||
authorCaptor.capture());
|
||||
authorCaptor.getValue().onResult(null);
|
||||
// execute the callback
|
||||
assertEquals(setupActivity.isFinishing(), true);
|
||||
// Confirm that the correct Activity has been started
|
||||
ShadowActivity shadowActivity = shadowOf(setupActivity);
|
||||
Intent intent = shadowActivity.peekNextStartedActivity();
|
||||
assertEquals(intent.getComponent().getClassName(),
|
||||
NavDrawerActivity.class.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -148,74 +60,4 @@ public class SetupActivityTest {
|
||||
assertEquals(nicknameEntryWrapper.getError(),
|
||||
setupActivity.getString(R.string.name_too_long));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAccountCreation() {
|
||||
SetupController controller = setupActivity.getController();
|
||||
// mock a resulthandler
|
||||
ResultHandler<Void> resultHandler =
|
||||
(ResultHandler<Void>) mock(ResultHandler.class);
|
||||
controller.storeAuthorInfo("nick", "some.strong.pass", resultHandler);
|
||||
// blocking verification call with timeout that waits until the mocked
|
||||
// result gets called with handle 0L, the expected value
|
||||
verify(resultHandler, timeout(TIMEOUT_MS).times(1)).onResult(null);
|
||||
SharedPreferences prefs =
|
||||
setupActivity.getSharedPreferences("db", Context.MODE_PRIVATE);
|
||||
// Confirm database key
|
||||
assertTrue(prefs.contains("key"));
|
||||
// Note that Robolectric uses its own persistant storage that it
|
||||
// wipes clean after each test run, no need to clean up manually.
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStrengthMeter() {
|
||||
SetupController controller = setupActivity.getController();
|
||||
|
||||
String strongPass = "very.strong.password.123";
|
||||
String weakPass = "we";
|
||||
String quiteStrongPass = "quite.strong";
|
||||
|
||||
float val = controller.estimatePasswordStrength(strongPass);
|
||||
assertTrue(val == STRONG);
|
||||
val = controller.estimatePasswordStrength(weakPass);
|
||||
assertTrue(val < WEAK && val > NONE);
|
||||
val = controller.estimatePasswordStrength(quiteStrongPass);
|
||||
assertTrue(val < STRONG && val > QUITE_WEAK);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStrengthMeterUI() {
|
||||
Assert.assertNotNull(setupActivity);
|
||||
// replace the setup controller with our mocked copy
|
||||
SetupController mockedController = this.setupController;
|
||||
setupActivity.setController(mockedController);
|
||||
// Mock answers for UI testing only
|
||||
when(mockedController.estimatePasswordStrength("strong")).thenReturn(
|
||||
STRONG);
|
||||
when(mockedController.estimatePasswordStrength("qstrong")).thenReturn(
|
||||
QUITE_STRONG);
|
||||
when(mockedController.estimatePasswordStrength("qweak")).thenReturn(
|
||||
QUITE_WEAK);
|
||||
when(mockedController.estimatePasswordStrength("weak")).thenReturn(
|
||||
WEAK);
|
||||
when(mockedController.estimatePasswordStrength("empty")).thenReturn(
|
||||
NONE);
|
||||
// Test the meters progress and color for several values
|
||||
testStrengthMeter("strong", STRONG, StrengthMeter.GREEN);
|
||||
Mockito.verify(mockedController, Mockito.times(1))
|
||||
.estimatePasswordStrength(eq("strong"));
|
||||
testStrengthMeter("qstrong", QUITE_STRONG, StrengthMeter.LIME);
|
||||
Mockito.verify(mockedController, Mockito.times(1))
|
||||
.estimatePasswordStrength(eq("qstrong"));
|
||||
testStrengthMeter("qweak", QUITE_WEAK, StrengthMeter.YELLOW);
|
||||
Mockito.verify(mockedController, Mockito.times(1))
|
||||
.estimatePasswordStrength(eq("qweak"));
|
||||
testStrengthMeter("weak", WEAK, StrengthMeter.ORANGE);
|
||||
Mockito.verify(mockedController, Mockito.times(1))
|
||||
.estimatePasswordStrength(eq("weak"));
|
||||
// Not sure this should be the correct behaviour on an empty input ?
|
||||
testStrengthMeter("empty", NONE, StrengthMeter.RED);
|
||||
Mockito.verify(mockedController, Mockito.times(1))
|
||||
.estimatePasswordStrength(eq("empty"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
package org.briarproject.briar.android.login;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
|
||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||
import org.briarproject.bramble.api.crypto.PasswordStrengthEstimator;
|
||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||
import org.briarproject.bramble.api.db.DatabaseConfig;
|
||||
import org.briarproject.bramble.test.BrambleMockTestCase;
|
||||
import org.briarproject.bramble.test.ImmediateExecutor;
|
||||
import org.briarproject.briar.android.controller.handler.ResultHandler;
|
||||
import org.jmock.Expectations;
|
||||
import org.jmock.lib.legacy.ClassImposteriser;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
|
||||
import static org.briarproject.bramble.test.TestUtils.getSecretKey;
|
||||
import static org.briarproject.bramble.util.StringUtils.getRandomString;
|
||||
|
||||
public class SetupControllerImplTest extends BrambleMockTestCase {
|
||||
|
||||
private final SharedPreferences briarPrefs =
|
||||
context.mock(SharedPreferences.class);
|
||||
private final DatabaseConfig databaseConfig =
|
||||
context.mock(DatabaseConfig.class);
|
||||
private final CryptoComponent crypto = context.mock(CryptoComponent.class);
|
||||
private final PasswordStrengthEstimator estimator =
|
||||
context.mock(PasswordStrengthEstimator.class);
|
||||
private final SharedPreferences.Editor editor =
|
||||
context.mock(SharedPreferences.Editor.class);
|
||||
private final SetupActivity setupActivity;
|
||||
|
||||
private final Executor cryptoExecutor = new ImmediateExecutor();
|
||||
|
||||
private final String authorName = getRandomString(MAX_AUTHOR_NAME_LENGTH);
|
||||
private final String password = "some.strong.pass";
|
||||
private final String encryptedHex = "010203";
|
||||
private final byte[] encryptedBytes = new byte[] {1, 2, 3};
|
||||
private final SecretKey key = getSecretKey();
|
||||
|
||||
public SetupControllerImplTest() {
|
||||
context.setImposteriser(ClassImposteriser.INSTANCE);
|
||||
setupActivity = context.mock(SetupActivity.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateAccount() {
|
||||
context.checking(new Expectations() {{
|
||||
// Setting the author name shows the password fragment
|
||||
oneOf(setupActivity).showPasswordFragment();
|
||||
// Generate a database key
|
||||
oneOf(crypto).generateSecretKey();
|
||||
will(returnValue(key));
|
||||
// Attach the author name and database key to the database config
|
||||
oneOf(databaseConfig).setLocalAuthorName(authorName);
|
||||
oneOf(databaseConfig).setEncryptionKey(key);
|
||||
// Encrypt the key with the password
|
||||
oneOf(crypto).encryptWithPassword(key.getBytes(), password);
|
||||
will(returnValue(encryptedBytes));
|
||||
// Store the encrypted key
|
||||
oneOf(briarPrefs).edit();
|
||||
will(returnValue(editor));
|
||||
oneOf(editor).putString("key", encryptedHex);
|
||||
oneOf(editor).apply();
|
||||
}});
|
||||
|
||||
SetupControllerImpl s = new SetupControllerImpl(briarPrefs,
|
||||
databaseConfig, cryptoExecutor, crypto, estimator);
|
||||
s.setSetupActivity(setupActivity);
|
||||
|
||||
final AtomicBoolean called = new AtomicBoolean(false);
|
||||
s.setAuthorName(authorName);
|
||||
s.setPassword(password);
|
||||
s.createAccount(new ResultHandler<Void>() {
|
||||
@Override
|
||||
public void onResult(Void result) {
|
||||
called.set(true);
|
||||
}
|
||||
});
|
||||
assertTrue(called.get());
|
||||
}
|
||||
}
|
||||
@@ -1,24 +1,13 @@
|
||||
package org.briarproject.briar.android.login;
|
||||
|
||||
/**
|
||||
* This class exposes the PasswordController and SetupController and offers the
|
||||
* possibility to override them.
|
||||
* This class exposes the PasswordController and offers the possibility to
|
||||
* replace it.
|
||||
*/
|
||||
public class TestChangePasswordActivity extends ChangePasswordActivity {
|
||||
|
||||
public PasswordController getPasswordController() {
|
||||
return passwordController;
|
||||
}
|
||||
|
||||
public SetupController getSetupController() {
|
||||
return setupController;
|
||||
}
|
||||
|
||||
public void setPasswordController(PasswordController passwordController) {
|
||||
this.passwordController = passwordController;
|
||||
}
|
||||
|
||||
public void setSetupController(SetupController setupController) {
|
||||
this.setupController = setupController;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
package org.briarproject.briar.android.login;
|
||||
|
||||
/**
|
||||
* This class exposes the SetupController and offers the possibility to
|
||||
* override it.
|
||||
*/
|
||||
public class TestSetupActivity extends SetupActivity {
|
||||
|
||||
SetupController getController() {
|
||||
return setupController;
|
||||
}
|
||||
|
||||
void setController(SetupController setupController) {
|
||||
this.setupController = setupController;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user