gecko/mobile/android/base/tests/testPasswordEncrypt.java.in
2012-03-12 10:06:10 -07:00

113 lines
5.1 KiB
Java

#filter substitution
package @ANDROID_PACKAGE_NAME@.tests;
import @ANDROID_PACKAGE_NAME@.*;
import android.app.Activity;
import android.content.ContentValues;
import android.content.ContentResolver;
import android.database.Cursor;
import android.content.Context;
import android.net.Uri;
import java.io.File;
import java.lang.reflect.Method;
public class testPasswordEncrypt extends BaseTest {
public void testPasswordEncrypt() {
setTestType("mochitest");
Context context = (Context)getActivity();
ContentResolver cr = context.getContentResolver();
ContentValues cvs = new ContentValues();
Actions.EventExpecter contentEventExpecter = mActions.expectGeckoEvent("Gecko:Ready");
contentEventExpecter.blockForEvent();
File db = new File(mProfile, "signons.sqlite");
String dbPath = db.getPath();
Uri passwordUri;
try {
ClassLoader classLoader = getActivity().getClassLoader();
Class pwds = classLoader.loadClass("org.mozilla.gecko.db.BrowserContract$Passwords");
Class nss = classLoader.loadClass("org.mozilla.gecko.NSSBridge");
Class contextClass = classLoader.loadClass("android.content.Context");
Class stringClass = classLoader.loadClass("java.lang.String");
Class appshell = classLoader.loadClass("org.mozilla.gecko.GeckoAppShell");
Method loadNSSLibs = appshell.getMethod("loadNSSLibs", contextClass, stringClass);
Method decrypt = nss.getMethod("decrypt", contextClass, stringClass, stringClass);
Method encrypt = nss.getMethod("encrypt", contextClass, stringClass, stringClass);
cvs.put("hostname", "http://www.example.com");
cvs.put("encryptedUsername", "username");
cvs.put("encryptedPassword", "password");
// Attempt to insert into the db
passwordUri = (Uri)pwds.getField("CONTENT_URI").get(null);
Uri.Builder builder = passwordUri.buildUpon();
passwordUri = builder.appendQueryParameter("profilePath", mProfile).build();
// This should fail the first time round because there is no pw database
// Wait for gecko to reply and then we'll try again
contentEventExpecter = mActions.expectGeckoEvent("Passwords:Init:Return");
Uri uri = cr.insert(passwordUri, cvs);
contentEventExpecter.blockForEvent();
mAsserter.is(uri, null, "Insert returned null correctly");
uri = cr.insert(passwordUri, cvs);
Uri expectedUri = passwordUri.buildUpon().appendPath("1").build();
mAsserter.is(uri.toString(), expectedUri.toString(), "Insert returned correct uri");
Cursor list = mActions.querySql(dbPath, "SELECT encryptedUsername FROM moz_logins");
String resourcePath = getActivity().getApplication().getPackageResourcePath();
loadNSSLibs.invoke(null, (Context)getActivity(), resourcePath);
list.moveToFirst();
String decryptedU = (String)decrypt.invoke(null, context, mProfile, list.getString(0));
mAsserter.is(decryptedU, "username", "Username was encrypted correctly when inserting");
list = mActions.querySql(dbPath, "SELECT encryptedPassword FROM moz_logins");
list.moveToFirst();
String decryptedP = (String)decrypt.invoke(null, context, mProfile, list.getString(0));
mAsserter.is(decryptedP, "password", "Password was encrypted correctly when inserting");
cvs.put("encryptedUsername", "username2");
cvs.put("encryptedPassword", "password2");
cr.update(passwordUri, cvs, null, null);
list = mActions.querySql(dbPath, "SELECT encryptedUsername FROM moz_logins");
list.moveToFirst();
decryptedU = (String)decrypt.invoke(null, context, mProfile, list.getString(0));
mAsserter.is(decryptedU, "username2", "Username was encrypted when updating");
list = mActions.querySql(dbPath, "SELECT encryptedPassword FROM moz_logins");
list.moveToFirst();
decryptedP = (String)decrypt.invoke(null, context, mProfile, list.getString(0));
mAsserter.is(decryptedP, "password2", "Password was encrypted when updating");
} catch(ClassNotFoundException ex) {
mAsserter.ok(false, "Error getting class", ex.toString());
return;
} catch(NoSuchFieldException ex) {
mAsserter.ok(false, "Error getting field", ex.toString());
return;
} catch(IllegalAccessException ex) {
mAsserter.ok(false, "Error using field", ex.toString());
return;
} catch(java.lang.NoSuchMethodException ex) {
mAsserter.ok(false, "Error getting method", ex.toString());
return;
} catch(java.lang.reflect.InvocationTargetException ex) {
mAsserter.ok(false, "Error invoking method", ex.toString());
return;
}
}
public void tearDown() throws Exception {
super.tearDown();
// remove the entire signons.sqlite file
File profile = new File(mProfile);
File db = new File(profile, "signons.sqlite");
db.delete();
}
}