Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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 | /** |
michael@0 | 5 | * This file tests the validity of various triggers that add remove hosts from moz_hosts |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | XPCOMUtils.defineLazyServiceGetter(this, "gHistory", |
michael@0 | 9 | "@mozilla.org/browser/history;1", |
michael@0 | 10 | "mozIAsyncHistory"); |
michael@0 | 11 | |
michael@0 | 12 | // add some visits and remove them, add a bookmark, |
michael@0 | 13 | // change its uri, then remove it, and |
michael@0 | 14 | // for each change check that moz_hosts has correctly been updated. |
michael@0 | 15 | |
michael@0 | 16 | function isHostInMozPlaces(aURI) |
michael@0 | 17 | { |
michael@0 | 18 | let stmt = DBConn().createStatement( |
michael@0 | 19 | "SELECT url " |
michael@0 | 20 | + "FROM moz_places " |
michael@0 | 21 | + "WHERE url = :host" |
michael@0 | 22 | ); |
michael@0 | 23 | let result = false; |
michael@0 | 24 | stmt.params.host = aURI.spec; |
michael@0 | 25 | while(stmt.executeStep()) { |
michael@0 | 26 | if (stmt.row.url == aURI.spec) { |
michael@0 | 27 | result = true; |
michael@0 | 28 | break; |
michael@0 | 29 | } |
michael@0 | 30 | } |
michael@0 | 31 | stmt.finalize(); |
michael@0 | 32 | return result; |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | function isHostInMozHosts(aURI, aTyped, aPrefix) |
michael@0 | 36 | { |
michael@0 | 37 | let stmt = DBConn().createStatement( |
michael@0 | 38 | "SELECT host, typed, prefix " |
michael@0 | 39 | + "FROM moz_hosts " |
michael@0 | 40 | + "WHERE host = fixup_url(:host) " |
michael@0 | 41 | + "AND frecency NOTNULL " |
michael@0 | 42 | ); |
michael@0 | 43 | let result = false; |
michael@0 | 44 | stmt.params.host = aURI.host; |
michael@0 | 45 | if (stmt.executeStep()) { |
michael@0 | 46 | result = aTyped == stmt.row.typed && aPrefix == stmt.row.prefix; |
michael@0 | 47 | } |
michael@0 | 48 | stmt.finalize(); |
michael@0 | 49 | return result; |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | let urls = [{uri: NetUtil.newURI("http://visit1.mozilla.org"), |
michael@0 | 53 | expected: "visit1.mozilla.org", |
michael@0 | 54 | typed: 0, |
michael@0 | 55 | prefix: null |
michael@0 | 56 | }, |
michael@0 | 57 | {uri: NetUtil.newURI("http://visit2.mozilla.org"), |
michael@0 | 58 | expected: "visit2.mozilla.org", |
michael@0 | 59 | typed: 0, |
michael@0 | 60 | prefix: null |
michael@0 | 61 | }, |
michael@0 | 62 | {uri: NetUtil.newURI("http://www.foo.mozilla.org"), |
michael@0 | 63 | expected: "foo.mozilla.org", |
michael@0 | 64 | typed: 1, |
michael@0 | 65 | prefix: "www." |
michael@0 | 66 | }, |
michael@0 | 67 | ]; |
michael@0 | 68 | |
michael@0 | 69 | const NEW_URL = "http://different.mozilla.org/"; |
michael@0 | 70 | |
michael@0 | 71 | add_task(function test_moz_hosts_update() |
michael@0 | 72 | { |
michael@0 | 73 | let places = []; |
michael@0 | 74 | urls.forEach(function(url) { |
michael@0 | 75 | let place = { uri: url.uri, |
michael@0 | 76 | title: "test for " + url.url, |
michael@0 | 77 | transition: url.typed ? TRANSITION_TYPED : undefined }; |
michael@0 | 78 | places.push(place); |
michael@0 | 79 | }); |
michael@0 | 80 | |
michael@0 | 81 | yield promiseAddVisits(places); |
michael@0 | 82 | |
michael@0 | 83 | do_check_true(isHostInMozHosts(urls[0].uri, urls[0].typed, urls[0].prefix)); |
michael@0 | 84 | do_check_true(isHostInMozHosts(urls[1].uri, urls[1].typed, urls[1].prefix)); |
michael@0 | 85 | do_check_true(isHostInMozHosts(urls[2].uri, urls[2].typed, urls[2].prefix)); |
michael@0 | 86 | }); |
michael@0 | 87 | |
michael@0 | 88 | add_task(function test_remove_places() |
michael@0 | 89 | { |
michael@0 | 90 | for (let idx in urls) { |
michael@0 | 91 | PlacesUtils.history.removePage(urls[idx].uri); |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | yield promiseClearHistory(); |
michael@0 | 95 | |
michael@0 | 96 | for (let idx in urls) { |
michael@0 | 97 | do_check_false(isHostInMozHosts(urls[idx].uri, urls[idx].typed, urls[idx].prefix)); |
michael@0 | 98 | } |
michael@0 | 99 | }); |
michael@0 | 100 | |
michael@0 | 101 | add_task(function test_bookmark_changes() |
michael@0 | 102 | { |
michael@0 | 103 | let testUri = NetUtil.newURI("http://test.mozilla.org"); |
michael@0 | 104 | |
michael@0 | 105 | let itemId = PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId, |
michael@0 | 106 | testUri, |
michael@0 | 107 | PlacesUtils.bookmarks.DEFAULT_INDEX, |
michael@0 | 108 | "bookmark title"); |
michael@0 | 109 | |
michael@0 | 110 | do_check_true(isHostInMozPlaces(testUri)); |
michael@0 | 111 | |
michael@0 | 112 | // Change the hostname |
michael@0 | 113 | PlacesUtils.bookmarks.changeBookmarkURI(itemId, NetUtil.newURI(NEW_URL)); |
michael@0 | 114 | |
michael@0 | 115 | yield promiseClearHistory(); |
michael@0 | 116 | |
michael@0 | 117 | let newUri = NetUtil.newURI(NEW_URL); |
michael@0 | 118 | do_check_true(isHostInMozPlaces(newUri)); |
michael@0 | 119 | do_check_true(isHostInMozHosts(newUri, false, null)); |
michael@0 | 120 | do_check_false(isHostInMozHosts(NetUtil.newURI("http://test.mozilla.org"), false, null)); |
michael@0 | 121 | }); |
michael@0 | 122 | |
michael@0 | 123 | add_task(function test_bookmark_removal() |
michael@0 | 124 | { |
michael@0 | 125 | let itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.unfiledBookmarksFolderId, |
michael@0 | 126 | PlacesUtils.bookmarks.DEFAULT_INDEX); |
michael@0 | 127 | let newUri = NetUtil.newURI(NEW_URL); |
michael@0 | 128 | PlacesUtils.bookmarks.removeItem(itemId); |
michael@0 | 129 | yield promiseClearHistory(); |
michael@0 | 130 | |
michael@0 | 131 | do_check_false(isHostInMozHosts(newUri, false, null)); |
michael@0 | 132 | }); |
michael@0 | 133 | |
michael@0 | 134 | add_task(function test_moz_hosts_typed_update() |
michael@0 | 135 | { |
michael@0 | 136 | const TEST_URI = NetUtil.newURI("http://typed.mozilla.com"); |
michael@0 | 137 | let places = [{ uri: TEST_URI |
michael@0 | 138 | , title: "test for " + TEST_URI.spec |
michael@0 | 139 | }, |
michael@0 | 140 | { uri: TEST_URI |
michael@0 | 141 | , title: "test for " + TEST_URI.spec |
michael@0 | 142 | , transition: TRANSITION_TYPED |
michael@0 | 143 | }]; |
michael@0 | 144 | |
michael@0 | 145 | yield promiseAddVisits(places); |
michael@0 | 146 | |
michael@0 | 147 | do_check_true(isHostInMozHosts(TEST_URI, true, null)); |
michael@0 | 148 | yield promiseClearHistory(); |
michael@0 | 149 | }); |
michael@0 | 150 | |
michael@0 | 151 | add_task(function test_moz_hosts_www_remove() |
michael@0 | 152 | { |
michael@0 | 153 | function test_removal(aURIToRemove, aURIToKeep, aCallback) { |
michael@0 | 154 | let places = [{ uri: aURIToRemove |
michael@0 | 155 | , title: "test for " + aURIToRemove.spec |
michael@0 | 156 | , transition: TRANSITION_TYPED |
michael@0 | 157 | }, |
michael@0 | 158 | { uri: aURIToKeep |
michael@0 | 159 | , title: "test for " + aURIToKeep.spec |
michael@0 | 160 | , transition: TRANSITION_TYPED |
michael@0 | 161 | }]; |
michael@0 | 162 | |
michael@0 | 163 | yield promiseAddVisits(places); |
michael@0 | 164 | print("removing " + aURIToRemove.spec + " keeping " + aURIToKeep); |
michael@0 | 165 | dump_table("moz_hosts"); |
michael@0 | 166 | dump_table("moz_places"); |
michael@0 | 167 | PlacesUtils.history.removePage(aURIToRemove); |
michael@0 | 168 | let prefix = /www/.test(aURIToKeep.spec) ? "www." : null; |
michael@0 | 169 | dump_table("moz_hosts"); |
michael@0 | 170 | dump_table("moz_places"); |
michael@0 | 171 | do_check_true(isHostInMozHosts(aURIToKeep, true, prefix)); |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | const TEST_URI = NetUtil.newURI("http://rem.mozilla.com"); |
michael@0 | 175 | const TEST_WWW_URI = NetUtil.newURI("http://www.rem.mozilla.com"); |
michael@0 | 176 | yield test_removal(TEST_URI, TEST_WWW_URI); |
michael@0 | 177 | yield test_removal(TEST_WWW_URI, TEST_URI); |
michael@0 | 178 | yield promiseClearHistory(); |
michael@0 | 179 | }); |
michael@0 | 180 | |
michael@0 | 181 | add_task(function test_moz_hosts_ftp_matchall() |
michael@0 | 182 | { |
michael@0 | 183 | const TEST_URI_1 = NetUtil.newURI("ftp://www.mozilla.com/"); |
michael@0 | 184 | const TEST_URI_2 = NetUtil.newURI("ftp://mozilla.com/"); |
michael@0 | 185 | |
michael@0 | 186 | yield promiseAddVisits([{ uri: TEST_URI_1, transition: TRANSITION_TYPED }, |
michael@0 | 187 | { uri: TEST_URI_2, transition: TRANSITION_TYPED }]); |
michael@0 | 188 | |
michael@0 | 189 | do_check_true(isHostInMozHosts(TEST_URI_1, true, "ftp://")); |
michael@0 | 190 | }); |
michael@0 | 191 | |
michael@0 | 192 | add_task(function test_moz_hosts_ftp_not_matchall() |
michael@0 | 193 | { |
michael@0 | 194 | const TEST_URI_1 = NetUtil.newURI("http://mozilla.com/"); |
michael@0 | 195 | const TEST_URI_2 = NetUtil.newURI("ftp://mozilla.com/"); |
michael@0 | 196 | |
michael@0 | 197 | yield promiseAddVisits([{ uri: TEST_URI_1, transition: TRANSITION_TYPED }, |
michael@0 | 198 | { uri: TEST_URI_2, transition: TRANSITION_TYPED }]); |
michael@0 | 199 | |
michael@0 | 200 | do_check_true(isHostInMozHosts(TEST_URI_1, true, null)); |
michael@0 | 201 | }); |
michael@0 | 202 | |
michael@0 | 203 | add_task(function test_moz_hosts_update_2() |
michael@0 | 204 | { |
michael@0 | 205 | // Check that updating trigger takes into account prefixes for different |
michael@0 | 206 | // rev_hosts. |
michael@0 | 207 | const TEST_URI_1 = NetUtil.newURI("https://www.google.it/"); |
michael@0 | 208 | const TEST_URI_2 = NetUtil.newURI("https://google.it/"); |
michael@0 | 209 | let places = [{ uri: TEST_URI_1 |
michael@0 | 210 | , transition: TRANSITION_TYPED |
michael@0 | 211 | }, |
michael@0 | 212 | { uri: TEST_URI_2 |
michael@0 | 213 | }]; |
michael@0 | 214 | yield promiseAddVisits(places); |
michael@0 | 215 | |
michael@0 | 216 | do_check_true(isHostInMozHosts(TEST_URI_1, true, "https://www.")); |
michael@0 | 217 | }); |
michael@0 | 218 | |
michael@0 | 219 | function run_test() |
michael@0 | 220 | { |
michael@0 | 221 | run_next_test(); |
michael@0 | 222 | } |