toolkit/components/places/tests/favicons/test_replaceFaviconDataFromDataURL.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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 * Tests for mozIAsyncFavicons::replaceFaviconData()
michael@0 6 */
michael@0 7
michael@0 8 let iconsvc = PlacesUtils.favicons;
michael@0 9 let histsvc = PlacesUtils.history;
michael@0 10
michael@0 11 let originalFavicon = {
michael@0 12 file: do_get_file("favicon-normal16.png"),
michael@0 13 uri: uri(do_get_file("favicon-normal16.png")),
michael@0 14 data: readFileData(do_get_file("favicon-normal16.png")),
michael@0 15 mimetype: "image/png"
michael@0 16 };
michael@0 17
michael@0 18 let uniqueFaviconId = 0;
michael@0 19 function createFavicon(fileName) {
michael@0 20 let tempdir = Services.dirsvc.get("TmpD", Ci.nsILocalFile);
michael@0 21
michael@0 22 // remove any existing file at the path we're about to copy to
michael@0 23 let outfile = tempdir.clone();
michael@0 24 outfile.append(fileName);
michael@0 25 try { outfile.remove(false); } catch (e) {}
michael@0 26
michael@0 27 originalFavicon.file.copyToFollowingLinks(tempdir, fileName);
michael@0 28
michael@0 29 let stream = Cc["@mozilla.org/network/file-output-stream;1"]
michael@0 30 .createInstance(Ci.nsIFileOutputStream);
michael@0 31 stream.init(outfile, 0x02 | 0x08 | 0x10, 0600, 0);
michael@0 32
michael@0 33 // append some data that sniffers/encoders will ignore that will distinguish
michael@0 34 // the different favicons we'll create
michael@0 35 uniqueFaviconId++;
michael@0 36 let uniqueStr = "uid:" + uniqueFaviconId;
michael@0 37 stream.write(uniqueStr, uniqueStr.length);
michael@0 38 stream.close();
michael@0 39
michael@0 40 do_check_eq(outfile.leafName.substr(0, fileName.length), fileName);
michael@0 41
michael@0 42 return {
michael@0 43 file: outfile,
michael@0 44 uri: uri(outfile),
michael@0 45 data: readFileData(outfile),
michael@0 46 mimetype: "image/png"
michael@0 47 };
michael@0 48 }
michael@0 49
michael@0 50 function createDataURLForFavicon(favicon) {
michael@0 51 return "data:" + favicon.mimetype + ";base64," + toBase64(favicon.data);
michael@0 52 }
michael@0 53
michael@0 54 function checkCallbackSucceeded(callbackMimetype, callbackData, sourceMimetype, sourceData) {
michael@0 55 do_check_eq(callbackMimetype, sourceMimetype);
michael@0 56 do_check_true(compareArrays(callbackData, sourceData));
michael@0 57 }
michael@0 58
michael@0 59 function run_test() {
michael@0 60 // check that the favicon loaded correctly
michael@0 61 do_check_eq(originalFavicon.data.length, 286);
michael@0 62 run_next_test();
michael@0 63 };
michael@0 64
michael@0 65 add_task(function test_replaceFaviconDataFromDataURL_validHistoryURI() {
michael@0 66 do_log_info("test replaceFaviconDataFromDataURL for valid history uri");
michael@0 67
michael@0 68 let pageURI = uri("http://test1.bar/");
michael@0 69 yield promiseAddVisits(pageURI);
michael@0 70
michael@0 71 let favicon = createFavicon("favicon1.png");
michael@0 72 iconsvc.replaceFaviconDataFromDataURL(favicon.uri, createDataURLForFavicon(favicon));
michael@0 73
michael@0 74 let deferSetAndFetchFavicon = Promise.defer();
michael@0 75 iconsvc.setAndFetchFaviconForPage(pageURI, favicon.uri, true,
michael@0 76 PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE,
michael@0 77 function test_replaceFaviconDataFromDataURL_validHistoryURI_check(aURI, aDataLen, aData, aMimeType) {
michael@0 78 checkCallbackSucceeded(aMimeType, aData, favicon.mimetype, favicon.data);
michael@0 79 checkFaviconDataForPage(
michael@0 80 pageURI, favicon.mimetype, favicon.data,
michael@0 81 function test_replaceFaviconDataFromDataURL_validHistoryURI_callback() {
michael@0 82 favicon.file.remove(false);
michael@0 83 deferSetAndFetchFavicon.resolve();
michael@0 84 });
michael@0 85 });
michael@0 86 yield deferSetAndFetchFavicon.promise;
michael@0 87
michael@0 88 yield promiseClearHistory();
michael@0 89 });
michael@0 90
michael@0 91 add_task(function test_replaceFaviconDataFromDataURL_overrideDefaultFavicon() {
michael@0 92 do_log_info("test replaceFaviconDataFromDataURL to override a later setAndFetchFaviconForPage");
michael@0 93
michael@0 94 let pageURI = uri("http://test2.bar/");
michael@0 95 yield promiseAddVisits(pageURI);
michael@0 96
michael@0 97 let firstFavicon = createFavicon("favicon2.png");
michael@0 98 let secondFavicon = createFavicon("favicon3.png");
michael@0 99
michael@0 100 iconsvc.replaceFaviconDataFromDataURL(firstFavicon.uri, createDataURLForFavicon(secondFavicon));
michael@0 101
michael@0 102 let deferSetAndFetchFavicon = Promise.defer();
michael@0 103 iconsvc.setAndFetchFaviconForPage(
michael@0 104 pageURI, firstFavicon.uri, true,
michael@0 105 PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE,
michael@0 106 function test_replaceFaviconDataFromDataURL_overrideDefaultFavicon_check(aURI, aDataLen, aData, aMimeType) {
michael@0 107 checkCallbackSucceeded(aMimeType, aData, secondFavicon.mimetype, secondFavicon.data);
michael@0 108 checkFaviconDataForPage(
michael@0 109 pageURI, secondFavicon.mimetype, secondFavicon.data,
michael@0 110 function test_replaceFaviconDataFromDataURL_overrideDefaultFavicon_callback() {
michael@0 111 firstFavicon.file.remove(false);
michael@0 112 secondFavicon.file.remove(false);
michael@0 113 deferSetAndFetchFavicon.resolve();
michael@0 114 });
michael@0 115 });
michael@0 116 yield deferSetAndFetchFavicon.promise;
michael@0 117
michael@0 118 yield promiseClearHistory();
michael@0 119 });
michael@0 120
michael@0 121 add_task(function test_replaceFaviconDataFromDataURL_replaceExisting() {
michael@0 122 do_log_info("test replaceFaviconDataFromDataURL to override a previous setAndFetchFaviconForPage");
michael@0 123
michael@0 124 let pageURI = uri("http://test3.bar");
michael@0 125 yield promiseAddVisits(pageURI);
michael@0 126
michael@0 127 let firstFavicon = createFavicon("favicon4.png");
michael@0 128 let secondFavicon = createFavicon("favicon5.png");
michael@0 129
michael@0 130 let deferSetAndFetchFavicon = Promise.defer();
michael@0 131 iconsvc.setAndFetchFaviconForPage(
michael@0 132 pageURI, firstFavicon.uri, true,
michael@0 133 PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE,
michael@0 134 function test_replaceFaviconDataFromDataURL_replaceExisting_firstSet_check(aURI, aDataLen, aData, aMimeType) {
michael@0 135 checkCallbackSucceeded(aMimeType, aData, firstFavicon.mimetype, firstFavicon.data);
michael@0 136 checkFaviconDataForPage(
michael@0 137 pageURI, firstFavicon.mimetype, firstFavicon.data,
michael@0 138 function test_replaceFaviconDataFromDataURL_replaceExisting_firstCallback() {
michael@0 139 iconsvc.replaceFaviconDataFromDataURL(firstFavicon.uri, createDataURLForFavicon(secondFavicon));
michael@0 140 checkFaviconDataForPage(
michael@0 141 pageURI, secondFavicon.mimetype, secondFavicon.data,
michael@0 142 function test_replaceFaviconDataFromDataURL_replaceExisting_secondCallback() {
michael@0 143 firstFavicon.file.remove(false);
michael@0 144 secondFavicon.file.remove(false);
michael@0 145 deferSetAndFetchFavicon.resolve();
michael@0 146 });
michael@0 147 });
michael@0 148 });
michael@0 149 yield deferSetAndFetchFavicon.promise;
michael@0 150
michael@0 151 yield promiseClearHistory();
michael@0 152 });
michael@0 153
michael@0 154 add_task(function test_replaceFaviconDataFromDataURL_unrelatedReplace() {
michael@0 155 do_log_info("test replaceFaviconDataFromDataURL to not make unrelated changes");
michael@0 156
michael@0 157 let pageURI = uri("http://test4.bar/");
michael@0 158 yield promiseAddVisits(pageURI);
michael@0 159
michael@0 160 let favicon = createFavicon("favicon6.png");
michael@0 161 let unrelatedFavicon = createFavicon("favicon7.png");
michael@0 162
michael@0 163 iconsvc.replaceFaviconDataFromDataURL(unrelatedFavicon.uri, createDataURLForFavicon(unrelatedFavicon));
michael@0 164
michael@0 165 let deferSetAndFetchFavicon = Promise.defer();
michael@0 166 iconsvc.setAndFetchFaviconForPage(
michael@0 167 pageURI, favicon.uri, true,
michael@0 168 PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE,
michael@0 169 function test_replaceFaviconDataFromDataURL_unrelatedReplace_check(aURI, aDataLen, aData, aMimeType) {
michael@0 170 checkCallbackSucceeded(aMimeType, aData, favicon.mimetype, favicon.data);
michael@0 171 checkFaviconDataForPage(
michael@0 172 pageURI, favicon.mimetype, favicon.data,
michael@0 173 function test_replaceFaviconDataFromDataURL_unrelatedReplace_callback() {
michael@0 174 favicon.file.remove(false);
michael@0 175 unrelatedFavicon.file.remove(false);
michael@0 176 deferSetAndFetchFavicon.resolve();
michael@0 177 });
michael@0 178 });
michael@0 179 yield deferSetAndFetchFavicon.promise;
michael@0 180
michael@0 181 yield promiseClearHistory();
michael@0 182 });
michael@0 183
michael@0 184 add_task(function test_replaceFaviconDataFromDataURL_badInputs() {
michael@0 185 do_log_info("test replaceFaviconDataFromDataURL to throw on bad inputs");
michael@0 186
michael@0 187 let favicon = createFavicon("favicon8.png");
michael@0 188
michael@0 189 let ex = null;
michael@0 190 try {
michael@0 191 iconsvc.replaceFaviconDataFromDataURL(favicon.uri, "");
michael@0 192 } catch (e) {
michael@0 193 ex = e;
michael@0 194 } finally {
michael@0 195 do_check_true(!!ex);
michael@0 196 }
michael@0 197
michael@0 198 ex = null;
michael@0 199 try {
michael@0 200 iconsvc.replaceFaviconDataFromDataURL(null, createDataURLForFavicon(favicon));
michael@0 201 } catch (e) {
michael@0 202 ex = e;
michael@0 203 } finally {
michael@0 204 do_check_true(!!ex);
michael@0 205 }
michael@0 206
michael@0 207 favicon.file.remove(false);
michael@0 208
michael@0 209 yield promiseClearHistory();
michael@0 210 });
michael@0 211
michael@0 212 add_task(function test_replaceFaviconDataFromDataURL_twiceReplace() {
michael@0 213 do_log_info("test replaceFaviconDataFromDataURL on multiple replacements");
michael@0 214
michael@0 215 let pageURI = uri("http://test5.bar/");
michael@0 216 yield promiseAddVisits(pageURI);
michael@0 217
michael@0 218 let firstFavicon = createFavicon("favicon9.png");
michael@0 219 let secondFavicon = createFavicon("favicon10.png");
michael@0 220
michael@0 221 iconsvc.replaceFaviconDataFromDataURL(firstFavicon.uri, createDataURLForFavicon(firstFavicon));
michael@0 222 iconsvc.replaceFaviconDataFromDataURL(firstFavicon.uri, createDataURLForFavicon(secondFavicon));
michael@0 223
michael@0 224 let deferSetAndFetchFavicon = Promise.defer();
michael@0 225 iconsvc.setAndFetchFaviconForPage(
michael@0 226 pageURI, firstFavicon.uri, true,
michael@0 227 PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE,
michael@0 228 function test_replaceFaviconDataFromDataURL_twiceReplace_check(aURI, aDataLen, aData, aMimeType) {
michael@0 229 checkCallbackSucceeded(aMimeType, aData, secondFavicon.mimetype, secondFavicon.data);
michael@0 230 checkFaviconDataForPage(
michael@0 231 pageURI, secondFavicon.mimetype, secondFavicon.data,
michael@0 232 function test_replaceFaviconDataFromDataURL_twiceReplace_callback() {
michael@0 233 firstFavicon.file.remove(false);
michael@0 234 secondFavicon.file.remove(false);
michael@0 235 deferSetAndFetchFavicon.resolve();
michael@0 236 });
michael@0 237 });
michael@0 238 yield deferSetAndFetchFavicon.promise;
michael@0 239
michael@0 240 yield promiseClearHistory();
michael@0 241 });
michael@0 242
michael@0 243 add_task(function test_replaceFaviconDataFromDataURL_afterRegularAssign() {
michael@0 244 do_log_info("test replaceFaviconDataFromDataURL after replaceFaviconData");
michael@0 245
michael@0 246 let pageURI = uri("http://test6.bar/");
michael@0 247 yield promiseAddVisits(pageURI);
michael@0 248
michael@0 249 let firstFavicon = createFavicon("favicon11.png");
michael@0 250 let secondFavicon = createFavicon("favicon12.png");
michael@0 251
michael@0 252 iconsvc.replaceFaviconData(
michael@0 253 firstFavicon.uri, firstFavicon.data, firstFavicon.data.length,
michael@0 254 firstFavicon.mimetype);
michael@0 255 iconsvc.replaceFaviconDataFromDataURL(firstFavicon.uri, createDataURLForFavicon(secondFavicon));
michael@0 256
michael@0 257 let deferSetAndFetchFavicon = Promise.defer();
michael@0 258 iconsvc.setAndFetchFaviconForPage(
michael@0 259 pageURI, firstFavicon.uri, true,
michael@0 260 PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE,
michael@0 261 function test_replaceFaviconDataFromDataURL_afterRegularAssign_check(aURI, aDataLen, aData, aMimeType) {
michael@0 262 checkCallbackSucceeded(aMimeType, aData, secondFavicon.mimetype, secondFavicon.data);
michael@0 263 checkFaviconDataForPage(
michael@0 264 pageURI, secondFavicon.mimetype, secondFavicon.data,
michael@0 265 function test_replaceFaviconDataFromDataURL_afterRegularAssign_callback() {
michael@0 266 firstFavicon.file.remove(false);
michael@0 267 secondFavicon.file.remove(false);
michael@0 268 deferSetAndFetchFavicon.resolve();
michael@0 269 });
michael@0 270 });
michael@0 271 yield deferSetAndFetchFavicon.promise;
michael@0 272
michael@0 273 yield promiseClearHistory();
michael@0 274 });
michael@0 275
michael@0 276 add_task(function test_replaceFaviconDataFromDataURL_beforeRegularAssign() {
michael@0 277 do_log_info("test replaceFaviconDataFromDataURL before replaceFaviconData");
michael@0 278
michael@0 279 let pageURI = uri("http://test7.bar/");
michael@0 280 yield promiseAddVisits(pageURI);
michael@0 281
michael@0 282 let firstFavicon = createFavicon("favicon13.png");
michael@0 283 let secondFavicon = createFavicon("favicon14.png");
michael@0 284
michael@0 285 iconsvc.replaceFaviconDataFromDataURL(firstFavicon.uri, createDataURLForFavicon(firstFavicon));
michael@0 286 iconsvc.replaceFaviconData(
michael@0 287 firstFavicon.uri, secondFavicon.data, secondFavicon.data.length,
michael@0 288 secondFavicon.mimetype);
michael@0 289
michael@0 290 let deferSetAndFetchFavicon = Promise.defer();
michael@0 291 iconsvc.setAndFetchFaviconForPage(
michael@0 292 pageURI, firstFavicon.uri, true,
michael@0 293 PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE,
michael@0 294 function test_replaceFaviconDataFromDataURL_beforeRegularAssign_check(aURI, aDataLen, aData, aMimeType) {
michael@0 295 checkCallbackSucceeded(aMimeType, aData, secondFavicon.mimetype, secondFavicon.data);
michael@0 296 checkFaviconDataForPage(
michael@0 297 pageURI, secondFavicon.mimetype, secondFavicon.data,
michael@0 298 function test_replaceFaviconDataFromDataURL_beforeRegularAssign_callback() {
michael@0 299 firstFavicon.file.remove(false);
michael@0 300 secondFavicon.file.remove(false);
michael@0 301 deferSetAndFetchFavicon.resolve();
michael@0 302 });
michael@0 303 });
michael@0 304 yield deferSetAndFetchFavicon.promise;
michael@0 305
michael@0 306 yield promiseClearHistory();
michael@0 307 });
michael@0 308
michael@0 309 /* toBase64 copied from image/test/unit/test_encoder_png.js */
michael@0 310
michael@0 311 /* Convert data (an array of integers) to a Base64 string. */
michael@0 312 const toBase64Table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' +
michael@0 313 '0123456789+/';
michael@0 314 const base64Pad = '=';
michael@0 315 function toBase64(data) {
michael@0 316 let result = '';
michael@0 317 let length = data.length;
michael@0 318 let i;
michael@0 319 // Convert every three bytes to 4 ascii characters.
michael@0 320 for (i = 0; i < (length - 2); i += 3) {
michael@0 321 result += toBase64Table[data[i] >> 2];
michael@0 322 result += toBase64Table[((data[i] & 0x03) << 4) + (data[i+1] >> 4)];
michael@0 323 result += toBase64Table[((data[i+1] & 0x0f) << 2) + (data[i+2] >> 6)];
michael@0 324 result += toBase64Table[data[i+2] & 0x3f];
michael@0 325 }
michael@0 326
michael@0 327 // Convert the remaining 1 or 2 bytes, pad out to 4 characters.
michael@0 328 if (length%3) {
michael@0 329 i = length - (length%3);
michael@0 330 result += toBase64Table[data[i] >> 2];
michael@0 331 if ((length%3) == 2) {
michael@0 332 result += toBase64Table[((data[i] & 0x03) << 4) + (data[i+1] >> 4)];
michael@0 333 result += toBase64Table[(data[i+1] & 0x0f) << 2];
michael@0 334 result += base64Pad;
michael@0 335 } else {
michael@0 336 result += toBase64Table[(data[i] & 0x03) << 4];
michael@0 337 result += base64Pad + base64Pad;
michael@0 338 }
michael@0 339 }
michael@0 340
michael@0 341 return result;
michael@0 342 }

mercurial