michael@0: package org.mozilla.gecko.tests; michael@0: michael@0: import java.io.File; michael@0: michael@0: import org.json.JSONObject; michael@0: import org.mozilla.gecko.NSSBridge; michael@0: import org.mozilla.gecko.db.BrowserContract; michael@0: michael@0: import android.content.ContentResolver; michael@0: import android.content.ContentValues; michael@0: import android.content.Context; michael@0: import android.database.Cursor; michael@0: import android.net.Uri; michael@0: michael@0: public class testPasswordEncrypt extends BaseTest { michael@0: public void testPasswordEncrypt() { michael@0: Context context = (Context)getActivity(); michael@0: ContentResolver cr = context.getContentResolver(); michael@0: mAsserter.isnot(cr, null, "Found a content resolver"); michael@0: ContentValues cvs = new ContentValues(); michael@0: michael@0: blockForGeckoReady(); michael@0: michael@0: File db = new File(mProfile, "signons.sqlite"); michael@0: String dbPath = db.getPath(); michael@0: michael@0: Uri passwordUri; michael@0: cvs.put("hostname", "http://www.example.com"); michael@0: cvs.put("encryptedUsername", "username"); michael@0: cvs.put("encryptedPassword", "password"); michael@0: michael@0: // Attempt to insert into the db michael@0: passwordUri = BrowserContract.Passwords.CONTENT_URI; michael@0: Uri.Builder builder = passwordUri.buildUpon(); michael@0: passwordUri = builder.appendQueryParameter("profilePath", mProfile).build(); michael@0: michael@0: Uri uri = cr.insert(passwordUri, cvs); michael@0: Uri expectedUri = passwordUri.buildUpon().appendPath("1").build(); michael@0: mAsserter.is(uri.toString(), expectedUri.toString(), "Insert returned correct uri"); michael@0: michael@0: Cursor list = mActions.querySql(dbPath, "SELECT encryptedUsername FROM moz_logins"); michael@0: list.moveToFirst(); michael@0: String decryptedU = null; michael@0: try { michael@0: decryptedU = NSSBridge.decrypt(context, mProfile, list.getString(0)); michael@0: } catch (Exception e) { michael@0: mAsserter.ok(false, "NSSBridge.decrypt through Exception " + e, ""); // TODO: What is diag? michael@0: } michael@0: mAsserter.is(decryptedU, "username", "Username was encrypted correctly when inserting"); michael@0: michael@0: list = mActions.querySql(dbPath, "SELECT encryptedPassword, encType FROM moz_logins"); michael@0: list.moveToFirst(); michael@0: String decryptedP = null; michael@0: try { michael@0: decryptedP = NSSBridge.decrypt(context, mProfile, list.getString(0)); michael@0: } catch (Exception e) { michael@0: mAsserter.ok(false, "NSSBridge.decrypt through Exception " + e, ""); // TODO: What is diag? michael@0: } michael@0: mAsserter.is(decryptedP, "password", "Password was encrypted correctly when inserting"); michael@0: mAsserter.is(list.getInt(1), 1, "Password has correct encryption type"); michael@0: michael@0: cvs.put("encryptedUsername", "username2"); michael@0: cvs.put("encryptedPassword", "password2"); michael@0: cr.update(passwordUri, cvs, null, null); michael@0: michael@0: list = mActions.querySql(dbPath, "SELECT encryptedUsername FROM moz_logins"); michael@0: list.moveToFirst(); michael@0: try { michael@0: decryptedU = NSSBridge.decrypt(context, mProfile, list.getString(0)); michael@0: } catch (Exception e) { michael@0: mAsserter.ok(false, "NSSBridge.decrypt through Exception " + e, ""); // TODO: What is diag? michael@0: } michael@0: mAsserter.is(decryptedU, "username2", "Username was encrypted when updating"); michael@0: michael@0: list = mActions.querySql(dbPath, "SELECT encryptedPassword FROM moz_logins"); michael@0: list.moveToFirst(); michael@0: try { michael@0: decryptedP = NSSBridge.decrypt(context, mProfile, list.getString(0)); michael@0: } catch (Exception e) { michael@0: mAsserter.ok(false, "NSSBridge.decrypt through Exception " + e, ""); // TODO: What is diag? michael@0: } michael@0: mAsserter.is(decryptedP, "password2", "Password was encrypted when updating"); michael@0: michael@0: // Trying to store a password while master password is enabled should throw, michael@0: // but because Android can't send Exceptions across processes michael@0: // it just results in a null uri/cursor being returned. michael@0: toggleMasterPassword("password"); michael@0: try { michael@0: uri = cr.insert(passwordUri, cvs); michael@0: // TODO: restore this assertion -- see bug 764901 michael@0: // mAsserter.is(uri, null, "Storing a password while MP was set should fail"); michael@0: michael@0: Cursor c = cr.query(passwordUri, null, null, null, null); michael@0: // TODO: restore this assertion -- see bug 764901 michael@0: // mAsserter.is(c, null, "Querying passwords while MP was set should fail"); michael@0: } catch (Exception ex) { michael@0: // Password provider currently can not throw across process michael@0: // so we should not catch this exception here michael@0: mAsserter.ok(false, "Caught exception", ex.toString()); michael@0: } michael@0: toggleMasterPassword("password"); michael@0: } michael@0: michael@0: private void toggleMasterPassword(String passwd) { michael@0: JSONObject jsonPref = new JSONObject(); michael@0: try { michael@0: jsonPref.put("name", "privacy.masterpassword.enabled"); michael@0: jsonPref.put("type", "string"); michael@0: jsonPref.put("value", passwd); michael@0: setPreferenceAndWaitForChange(jsonPref); michael@0: } catch (Exception ex) { michael@0: mAsserter.ok(false, "exception in toggleMasterPassword", ex.toString()); michael@0: } michael@0: } michael@0: michael@0: @Override michael@0: public void tearDown() throws Exception { michael@0: // remove the entire signons.sqlite file michael@0: File profile = new File(mProfile); michael@0: File db = new File(profile, "signons.sqlite"); michael@0: if (db.delete()) { michael@0: mAsserter.dumpLog("tearDown deleted "+db.toString()); michael@0: } else { michael@0: mAsserter.dumpLog("tearDown did not delete "+db.toString()); michael@0: } michael@0: michael@0: super.tearDown(); michael@0: } michael@0: }