browser/base/content/test/general/browser_star_hsts.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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 let secureURL = "https://example.com/browser/browser/base/content/test/general/browser_star_hsts.sjs";
michael@0 6 let unsecureURL = "http://example.com/browser/browser/base/content/test/general/browser_star_hsts.sjs";
michael@0 7
michael@0 8 add_task(function* test_star_redirect() {
michael@0 9 registerCleanupFunction(function() {
michael@0 10 // Ensure to remove example.com from the HSTS list.
michael@0 11 let sss = Cc["@mozilla.org/ssservice;1"]
michael@0 12 .getService(Ci.nsISiteSecurityService);
michael@0 13 sss.removeState(Ci.nsISiteSecurityService.HEADER_HSTS,
michael@0 14 NetUtil.newURI("http://example.com/"), 0);
michael@0 15 PlacesUtils.bookmarks.removeFolderChildren(PlacesUtils.unfiledBookmarksFolderId);
michael@0 16 gBrowser.removeCurrentTab();
michael@0 17 });
michael@0 18
michael@0 19 let tab = gBrowser.selectedTab = gBrowser.addTab();
michael@0 20 // This will add the page to the HSTS cache.
michael@0 21 yield promiseTabLoadEvent(tab, secureURL, secureURL);
michael@0 22 // This should transparently be redirected to the secure page.
michael@0 23 yield promiseTabLoadEvent(tab, unsecureURL, secureURL);
michael@0 24
michael@0 25 yield promiseStarState(BookmarkingUI.STATUS_UNSTARRED);
michael@0 26
michael@0 27 let promiseBookmark = promiseOnItemAdded(gBrowser.currentURI);
michael@0 28 BookmarkingUI.star.click();
michael@0 29 // This resolves on the next tick, so the star should have already been
michael@0 30 // updated at that point.
michael@0 31 yield promiseBookmark;
michael@0 32
michael@0 33 is(BookmarkingUI.status, BookmarkingUI.STATUS_STARRED, "The star is starred");
michael@0 34 });
michael@0 35
michael@0 36 /**
michael@0 37 * Waits for the star to reflect the expected state.
michael@0 38 */
michael@0 39 function promiseStarState(aValue) {
michael@0 40 let deferred = Promise.defer();
michael@0 41 let expectedStatus = aValue ? BookmarkingUI.STATUS_STARRED
michael@0 42 : BookmarkingUI.STATUS_UNSTARRED;
michael@0 43 (function checkState() {
michael@0 44 if (BookmarkingUI.status == BookmarkingUI.STATUS_UPDATING ||
michael@0 45 BookmarkingUI.status != expectedStatus) {
michael@0 46 info("Waiting for star button change.");
michael@0 47 setTimeout(checkState, 1000);
michael@0 48 } else {
michael@0 49 deferred.resolve();
michael@0 50 }
michael@0 51 })();
michael@0 52 return deferred.promise;
michael@0 53 }
michael@0 54
michael@0 55 /**
michael@0 56 * Starts a load in an existing tab and waits for it to finish (via some event).
michael@0 57 *
michael@0 58 * @param aTab
michael@0 59 * The tab to load into.
michael@0 60 * @param aUrl
michael@0 61 * The url to load.
michael@0 62 * @param [optional] aFinalURL
michael@0 63 * The url to wait for, same as aURL if not defined.
michael@0 64 * @return {Promise} resolved when the event is handled.
michael@0 65 */
michael@0 66 function promiseTabLoadEvent(aTab, aURL, aFinalURL)
michael@0 67 {
michael@0 68 if (!aFinalURL)
michael@0 69 aFinalURL = aURL;
michael@0 70 let deferred = Promise.defer();
michael@0 71 info("Wait for load tab event");
michael@0 72 aTab.linkedBrowser.addEventListener("load", function load(event) {
michael@0 73 if (event.originalTarget != aTab.linkedBrowser.contentDocument ||
michael@0 74 event.target.location.href == "about:blank" ||
michael@0 75 event.target.location.href != aFinalURL) {
michael@0 76 info("skipping spurious load event");
michael@0 77 return;
michael@0 78 }
michael@0 79 aTab.linkedBrowser.removeEventListener("load", load, true);
michael@0 80 info("Tab load event received");
michael@0 81 deferred.resolve();
michael@0 82 }, true, true);
michael@0 83 aTab.linkedBrowser.loadURI(aURL);
michael@0 84 return deferred.promise;
michael@0 85 }
michael@0 86
michael@0 87 /**
michael@0 88 * Waits for a bookmark to be added for the given uri.
michael@0 89 */
michael@0 90 function promiseOnItemAdded(aExpectedURI) {
michael@0 91 let defer = Promise.defer();
michael@0 92 let bookmarksObserver = {
michael@0 93 onItemAdded: function (aItemId, aFolderId, aIndex, aItemType, aURI) {
michael@0 94 info("Added a bookmark to " + aURI.spec);
michael@0 95 PlacesUtils.bookmarks.removeObserver(bookmarksObserver);
michael@0 96 if (aURI.equals(aExpectedURI))
michael@0 97 defer.resolve();
michael@0 98 else
michael@0 99 defer.reject(new Error("Added an unexpected bookmark"));
michael@0 100 },
michael@0 101 onBeginUpdateBatch: function () {},
michael@0 102 onEndUpdateBatch: function () {},
michael@0 103 onItemRemoved: function () {},
michael@0 104 onItemChanged: function () {},
michael@0 105 onItemVisited: function () {},
michael@0 106 onItemMoved: function () {},
michael@0 107 QueryInterface: XPCOMUtils.generateQI([
michael@0 108 Ci.nsINavBookmarkObserver,
michael@0 109 ])
michael@0 110 };
michael@0 111 info("Waiting for a bookmark to be added");
michael@0 112 PlacesUtils.bookmarks.addObserver(bookmarksObserver, false);
michael@0 113 return defer.promise;
michael@0 114 }

mercurial