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