Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
1 package org.mozilla.gecko.tests;
3 import java.io.File;
5 import org.json.JSONObject;
6 import org.mozilla.gecko.NSSBridge;
7 import org.mozilla.gecko.db.BrowserContract;
9 import android.content.ContentResolver;
10 import android.content.ContentValues;
11 import android.content.Context;
12 import android.database.Cursor;
13 import android.net.Uri;
15 public class testPasswordEncrypt extends BaseTest {
16 public void testPasswordEncrypt() {
17 Context context = (Context)getActivity();
18 ContentResolver cr = context.getContentResolver();
19 mAsserter.isnot(cr, null, "Found a content resolver");
20 ContentValues cvs = new ContentValues();
22 blockForGeckoReady();
24 File db = new File(mProfile, "signons.sqlite");
25 String dbPath = db.getPath();
27 Uri passwordUri;
28 cvs.put("hostname", "http://www.example.com");
29 cvs.put("encryptedUsername", "username");
30 cvs.put("encryptedPassword", "password");
32 // Attempt to insert into the db
33 passwordUri = BrowserContract.Passwords.CONTENT_URI;
34 Uri.Builder builder = passwordUri.buildUpon();
35 passwordUri = builder.appendQueryParameter("profilePath", mProfile).build();
37 Uri uri = cr.insert(passwordUri, cvs);
38 Uri expectedUri = passwordUri.buildUpon().appendPath("1").build();
39 mAsserter.is(uri.toString(), expectedUri.toString(), "Insert returned correct uri");
41 Cursor list = mActions.querySql(dbPath, "SELECT encryptedUsername FROM moz_logins");
42 list.moveToFirst();
43 String decryptedU = null;
44 try {
45 decryptedU = NSSBridge.decrypt(context, mProfile, list.getString(0));
46 } catch (Exception e) {
47 mAsserter.ok(false, "NSSBridge.decrypt through Exception " + e, ""); // TODO: What is diag?
48 }
49 mAsserter.is(decryptedU, "username", "Username was encrypted correctly when inserting");
51 list = mActions.querySql(dbPath, "SELECT encryptedPassword, encType FROM moz_logins");
52 list.moveToFirst();
53 String decryptedP = null;
54 try {
55 decryptedP = NSSBridge.decrypt(context, mProfile, list.getString(0));
56 } catch (Exception e) {
57 mAsserter.ok(false, "NSSBridge.decrypt through Exception " + e, ""); // TODO: What is diag?
58 }
59 mAsserter.is(decryptedP, "password", "Password was encrypted correctly when inserting");
60 mAsserter.is(list.getInt(1), 1, "Password has correct encryption type");
62 cvs.put("encryptedUsername", "username2");
63 cvs.put("encryptedPassword", "password2");
64 cr.update(passwordUri, cvs, null, null);
66 list = mActions.querySql(dbPath, "SELECT encryptedUsername FROM moz_logins");
67 list.moveToFirst();
68 try {
69 decryptedU = NSSBridge.decrypt(context, mProfile, list.getString(0));
70 } catch (Exception e) {
71 mAsserter.ok(false, "NSSBridge.decrypt through Exception " + e, ""); // TODO: What is diag?
72 }
73 mAsserter.is(decryptedU, "username2", "Username was encrypted when updating");
75 list = mActions.querySql(dbPath, "SELECT encryptedPassword FROM moz_logins");
76 list.moveToFirst();
77 try {
78 decryptedP = NSSBridge.decrypt(context, mProfile, list.getString(0));
79 } catch (Exception e) {
80 mAsserter.ok(false, "NSSBridge.decrypt through Exception " + e, ""); // TODO: What is diag?
81 }
82 mAsserter.is(decryptedP, "password2", "Password was encrypted when updating");
84 // Trying to store a password while master password is enabled should throw,
85 // but because Android can't send Exceptions across processes
86 // it just results in a null uri/cursor being returned.
87 toggleMasterPassword("password");
88 try {
89 uri = cr.insert(passwordUri, cvs);
90 // TODO: restore this assertion -- see bug 764901
91 // mAsserter.is(uri, null, "Storing a password while MP was set should fail");
93 Cursor c = cr.query(passwordUri, null, null, null, null);
94 // TODO: restore this assertion -- see bug 764901
95 // mAsserter.is(c, null, "Querying passwords while MP was set should fail");
96 } catch (Exception ex) {
97 // Password provider currently can not throw across process
98 // so we should not catch this exception here
99 mAsserter.ok(false, "Caught exception", ex.toString());
100 }
101 toggleMasterPassword("password");
102 }
104 private void toggleMasterPassword(String passwd) {
105 JSONObject jsonPref = new JSONObject();
106 try {
107 jsonPref.put("name", "privacy.masterpassword.enabled");
108 jsonPref.put("type", "string");
109 jsonPref.put("value", passwd);
110 setPreferenceAndWaitForChange(jsonPref);
111 } catch (Exception ex) {
112 mAsserter.ok(false, "exception in toggleMasterPassword", ex.toString());
113 }
114 }
116 @Override
117 public void tearDown() throws Exception {
118 // remove the entire signons.sqlite file
119 File profile = new File(mProfile);
120 File db = new File(profile, "signons.sqlite");
121 if (db.delete()) {
122 mAsserter.dumpLog("tearDown deleted "+db.toString());
123 } else {
124 mAsserter.dumpLog("tearDown did not delete "+db.toString());
125 }
127 super.tearDown();
128 }
129 }