1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 package org.mozilla.gecko.tests;
6 
7 import java.io.File;
8 
9 import org.mozilla.gecko.db.BrowserContract;
10 import org.mozilla.gecko.db.BrowserContract.GeckoDisabledHosts;
11 import org.mozilla.gecko.db.BrowserContract.Passwords;
12 
13 import android.content.ContentResolver;
14 import android.content.ContentValues;
15 import android.content.Context;
16 import android.database.Cursor;
17 import android.net.Uri;
18 
19 /**
20  * A basic password contentprovider test.
21  * - inserts a password when the database is not yet set up
22  * - inserts a password
23  * - updates a password
24  * - deletes a password
25  * - inserts a disabled host
26  * - queries for disabled host
27  */
28 public class testPasswordProvider extends BaseTest {
29     private static final String DB_NAME = "signons.sqlite";
30 
testPasswordProvider()31     public void testPasswordProvider() {
32         Context context = (Context)getActivity();
33         ContentResolver cr = context.getContentResolver();
34         ContentValues[] cvs = new ContentValues[1];
35         cvs[0] = new ContentValues();
36 
37         blockForGeckoReady();
38 
39         cvs[0].put("hostname", "http://www.example.com");
40         cvs[0].put("httpRealm", "http://www.example.com");
41         cvs[0].put("formSubmitURL", "http://www.example.com");
42         cvs[0].put("usernameField", "usernameField");
43         cvs[0].put("passwordField", "passwordField");
44         cvs[0].put("encryptedUsername", "username");
45         cvs[0].put("encryptedPassword", "password");
46         cvs[0].put("encType", "1");
47 
48         // Attempt to insert into the db
49         Uri passwordUri = Passwords.CONTENT_URI;
50         Uri.Builder builder = passwordUri.buildUpon();
51         passwordUri = builder.appendQueryParameter("profilePath", mProfile).build();
52 
53         Uri uri = cr.insert(passwordUri, cvs[0]);
54         Uri expectedUri = passwordUri.buildUpon().appendPath("1").build();
55         mAsserter.is(uri.toString(), expectedUri.toString(), "Insert returned correct uri");
56         Cursor c = cr.query(passwordUri, null, null, null, null);
57         SqliteCompare(c, cvs);
58 
59         cvs[0].put("usernameField", "usernameField2");
60         cvs[0].put("passwordField", "passwordField2");
61 
62         int numUpdated = cr.update(passwordUri, cvs[0], null, null);
63         mAsserter.is(1, numUpdated, "Correct number updated");
64         c = cr.query(passwordUri, null, null, null, null);
65         SqliteCompare(c, cvs);
66 
67         int numDeleted = cr.delete(passwordUri, null, null);
68         mAsserter.is(1, numDeleted, "Correct number deleted");
69         cvs = new ContentValues[0];
70         c = cr.query(passwordUri, null, null, null, null);
71         SqliteCompare(c, cvs);
72 
73         ContentValues values = new ContentValues();
74         values.put("hostname", "http://www.example.com");
75 
76         // Attempt to insert into the db.
77         Uri disabledHostUri = GeckoDisabledHosts.CONTENT_URI;
78         builder = disabledHostUri.buildUpon();
79         disabledHostUri = builder.appendQueryParameter("profilePath", mProfile).build();
80 
81         uri = cr.insert(disabledHostUri, values);
82         expectedUri = disabledHostUri.buildUpon().appendPath("1").build();
83         mAsserter.is(uri.toString(), expectedUri.toString(), "Insert returned correct uri");
84         Cursor cursor = cr.query(disabledHostUri, null, null, null, null);
85         assertNotNull(cursor);
86         assertEquals(1, cursor.getCount());
87         cursor.moveToFirst();
88         CursorMatches(cursor, values);
89     }
90 
91     @Override
tearDown()92     public void tearDown() throws Exception {
93         // remove the entire signons.sqlite file
94         File profile = new File(mProfile);
95         File db = new File(profile, "signons.sqlite");
96         if (db.delete()) {
97             mAsserter.dumpLog("tearDown deleted "+db.toString());
98         } else {
99             mAsserter.dumpLog("tearDown did not delete "+db.toString());
100         }
101 
102         super.tearDown();
103     }
104 }
105