Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | package org.mozilla.gecko.background.db; |
michael@0 | 5 | |
michael@0 | 6 | import java.io.IOException; |
michael@0 | 7 | import java.util.ArrayList; |
michael@0 | 8 | |
michael@0 | 9 | import org.json.simple.JSONArray; |
michael@0 | 10 | import org.json.simple.JSONObject; |
michael@0 | 11 | import org.json.simple.parser.ParseException; |
michael@0 | 12 | import org.mozilla.gecko.background.helpers.AndroidSyncTestCase; |
michael@0 | 13 | import org.mozilla.gecko.background.sync.helpers.HistoryHelpers; |
michael@0 | 14 | import org.mozilla.gecko.sync.ExtendedJSONObject; |
michael@0 | 15 | import org.mozilla.gecko.sync.NonArrayJSONException; |
michael@0 | 16 | import org.mozilla.gecko.sync.NonObjectJSONException; |
michael@0 | 17 | import org.mozilla.gecko.sync.Utils; |
michael@0 | 18 | import org.mozilla.gecko.sync.repositories.NullCursorException; |
michael@0 | 19 | import org.mozilla.gecko.sync.repositories.android.AndroidBrowserHistoryDataExtender; |
michael@0 | 20 | import org.mozilla.gecko.sync.repositories.android.RepoUtils; |
michael@0 | 21 | import org.mozilla.gecko.sync.repositories.domain.HistoryRecord; |
michael@0 | 22 | |
michael@0 | 23 | import android.database.Cursor; |
michael@0 | 24 | |
michael@0 | 25 | public class TestAndroidBrowserHistoryDataExtender extends AndroidSyncTestCase { |
michael@0 | 26 | |
michael@0 | 27 | protected AndroidBrowserHistoryDataExtender extender; |
michael@0 | 28 | protected static final String LOG_TAG = "SyncHistoryVisitsTest"; |
michael@0 | 29 | |
michael@0 | 30 | public void setUp() { |
michael@0 | 31 | extender = new AndroidBrowserHistoryDataExtender(getApplicationContext()); |
michael@0 | 32 | extender.wipe(); |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | public void tearDown() { |
michael@0 | 36 | extender.close(); |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | public void testStoreFetch() throws NullCursorException, NonObjectJSONException, IOException, ParseException { |
michael@0 | 40 | String guid = Utils.generateGuid(); |
michael@0 | 41 | extender.store(Utils.generateGuid(), null); |
michael@0 | 42 | extender.store(guid, null); |
michael@0 | 43 | extender.store(Utils.generateGuid(), null); |
michael@0 | 44 | |
michael@0 | 45 | Cursor cur = null; |
michael@0 | 46 | try { |
michael@0 | 47 | cur = extender.fetch(guid); |
michael@0 | 48 | assertEquals(1, cur.getCount()); |
michael@0 | 49 | assertTrue(cur.moveToFirst()); |
michael@0 | 50 | assertEquals(guid, cur.getString(0)); |
michael@0 | 51 | } finally { |
michael@0 | 52 | if (cur != null) { |
michael@0 | 53 | cur.close(); |
michael@0 | 54 | } |
michael@0 | 55 | } |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | public void testVisitsForGUID() throws NonArrayJSONException, NonObjectJSONException, IOException, ParseException, NullCursorException { |
michael@0 | 59 | String guid = Utils.generateGuid(); |
michael@0 | 60 | JSONArray visits = new ExtendedJSONObject("{ \"visits\": [ { \"key\" : \"value\" } ] }").getArray("visits"); |
michael@0 | 61 | |
michael@0 | 62 | extender.store(Utils.generateGuid(), null); |
michael@0 | 63 | extender.store(guid, visits); |
michael@0 | 64 | extender.store(Utils.generateGuid(), null); |
michael@0 | 65 | |
michael@0 | 66 | JSONArray fetchedVisits = extender.visitsForGUID(guid); |
michael@0 | 67 | assertEquals(1, fetchedVisits.size()); |
michael@0 | 68 | assertEquals("value", ((JSONObject)fetchedVisits.get(0)).get("key")); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | public void testDeleteHandlesBadGUIDs() { |
michael@0 | 72 | String evilGUID = "' or '1'='1"; |
michael@0 | 73 | extender.store(Utils.generateGuid(), null); |
michael@0 | 74 | extender.store(Utils.generateGuid(), null); |
michael@0 | 75 | extender.store(evilGUID, null); |
michael@0 | 76 | extender.delete(evilGUID); |
michael@0 | 77 | |
michael@0 | 78 | Cursor cur = null; |
michael@0 | 79 | try { |
michael@0 | 80 | cur = extender.fetchAll(); |
michael@0 | 81 | assertEquals(cur.getCount(), 2); |
michael@0 | 82 | assertTrue(cur.moveToFirst()); |
michael@0 | 83 | while (!cur.isAfterLast()) { |
michael@0 | 84 | String guid = RepoUtils.getStringFromCursor(cur, AndroidBrowserHistoryDataExtender.COL_GUID); |
michael@0 | 85 | assertFalse(evilGUID.equals(guid)); |
michael@0 | 86 | cur.moveToNext(); |
michael@0 | 87 | } |
michael@0 | 88 | } catch (NullCursorException e) { |
michael@0 | 89 | e.printStackTrace(); |
michael@0 | 90 | fail("Should not have null cursor."); |
michael@0 | 91 | } finally { |
michael@0 | 92 | if (cur != null) { |
michael@0 | 93 | cur.close(); |
michael@0 | 94 | } |
michael@0 | 95 | } |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | public void testStoreFetchHandlesBadGUIDs() { |
michael@0 | 99 | String evilGUID = "' or '1'='1"; |
michael@0 | 100 | extender.store(Utils.generateGuid(), null); |
michael@0 | 101 | extender.store(Utils.generateGuid(), null); |
michael@0 | 102 | extender.store(evilGUID, null); |
michael@0 | 103 | |
michael@0 | 104 | Cursor cur = null; |
michael@0 | 105 | try { |
michael@0 | 106 | cur = extender.fetch(evilGUID); |
michael@0 | 107 | assertEquals(1, cur.getCount()); |
michael@0 | 108 | assertTrue(cur.moveToFirst()); |
michael@0 | 109 | while (!cur.isAfterLast()) { |
michael@0 | 110 | String guid = RepoUtils.getStringFromCursor(cur, AndroidBrowserHistoryDataExtender.COL_GUID); |
michael@0 | 111 | assertEquals(evilGUID, guid); |
michael@0 | 112 | cur.moveToNext(); |
michael@0 | 113 | } |
michael@0 | 114 | } catch (NullCursorException e) { |
michael@0 | 115 | e.printStackTrace(); |
michael@0 | 116 | fail("Should not have null cursor."); |
michael@0 | 117 | } finally { |
michael@0 | 118 | if (cur != null) { |
michael@0 | 119 | cur.close(); |
michael@0 | 120 | } |
michael@0 | 121 | } |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | public void testBulkInsert() throws NullCursorException { |
michael@0 | 125 | ArrayList<HistoryRecord> records = new ArrayList<HistoryRecord>(); |
michael@0 | 126 | records.add(HistoryHelpers.createHistory1()); |
michael@0 | 127 | records.add(HistoryHelpers.createHistory2()); |
michael@0 | 128 | extender.bulkInsert(records); |
michael@0 | 129 | |
michael@0 | 130 | for (HistoryRecord record : records) { |
michael@0 | 131 | HistoryRecord toCompare = (HistoryRecord) record.copyWithIDs(record.guid, record.androidID); |
michael@0 | 132 | toCompare.visits = extender.visitsForGUID(record.guid); |
michael@0 | 133 | assertEquals(record.visits.size(), toCompare.visits.size()); |
michael@0 | 134 | assertTrue(record.equals(toCompare)); |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | // Now insert existing records, changing one, and add another record. |
michael@0 | 138 | records.get(0).title = "test"; |
michael@0 | 139 | records.add(HistoryHelpers.createHistory3()); |
michael@0 | 140 | extender.bulkInsert(records); |
michael@0 | 141 | |
michael@0 | 142 | for (HistoryRecord record : records) { |
michael@0 | 143 | HistoryRecord toCompare = (HistoryRecord) record.copyWithIDs(record.guid, record.androidID); |
michael@0 | 144 | toCompare.visits = extender.visitsForGUID(record.guid); |
michael@0 | 145 | assertEquals(record.visits.size(), toCompare.visits.size()); |
michael@0 | 146 | assertTrue(record.equals(toCompare)); |
michael@0 | 147 | } |
michael@0 | 148 | } |
michael@0 | 149 | } |