summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/browser/browser_storage_dynamic_windows.js
blob: a8f791f1508630d8a789734864b37852443f3cf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const {StorageFront} = require("devtools/shared/fronts/storage");
Services.scriptloader.loadSubScript("chrome://mochitests/content/browser/devtools/server/tests/browser/storage-helpers.js", this);

const beforeReload = {
  cookies: {
    "http://test1.example.org": ["c1", "cs2", "c3", "uc1"],
    "http://sectest1.example.org": ["uc1", "cs2"]
  },
  localStorage: {
    "http://test1.example.org": ["ls1", "ls2"],
    "http://sectest1.example.org": ["iframe-u-ls1"]
  },
  sessionStorage: {
    "http://test1.example.org": ["ss1"],
    "http://sectest1.example.org": ["iframe-u-ss1", "iframe-u-ss2"]
  },
  indexedDB: {
    "http://test1.example.org": [
      JSON.stringify(["idb1", "obj1"]),
      JSON.stringify(["idb1", "obj2"]),
      JSON.stringify(["idb2", "obj3"]),
    ],
    "http://sectest1.example.org": []
  }
};

function* testStores(data, front) {
  testWindowsBeforeReload(data);

  // FIXME: Bug 1183581 - browser_storage_dynamic_windows.js IsSafeToRunScript
  //                      errors when testing reload in E10S mode
  // yield testReload(front);
  yield testAddIframe(front);
  yield testRemoveIframe(front);
}

function testWindowsBeforeReload(data) {
  for (let storageType in beforeReload) {
    ok(data[storageType], storageType + " storage actor is present");
    is(Object.keys(data[storageType].hosts).length,
       Object.keys(beforeReload[storageType]).length,
       "Number of hosts for " + storageType + "match");
    for (let host in beforeReload[storageType]) {
      ok(data[storageType].hosts[host], "Host " + host + " is present");
    }
  }
}

function markOutMatched(toBeEmptied, data, deleted) {
  if (!Object.keys(toBeEmptied).length) {
    info("Object empty");
    return;
  }
  ok(Object.keys(data).length,
     "At least one storage type should be present");
  for (let storageType in toBeEmptied) {
    if (!data[storageType]) {
      continue;
    }
    info("Testing for " + storageType);
    for (let host in data[storageType]) {
      ok(toBeEmptied[storageType][host], "Host " + host + " found");

      if (!deleted) {
        for (let item of data[storageType][host]) {
          let index = toBeEmptied[storageType][host].indexOf(item);
          ok(index > -1, "Item found - " + item);
          if (index > -1) {
            toBeEmptied[storageType][host].splice(index, 1);
          }
        }
        if (!toBeEmptied[storageType][host].length) {
          delete toBeEmptied[storageType][host];
        }
      } else {
        delete toBeEmptied[storageType][host];
      }
    }
    if (!Object.keys(toBeEmptied[storageType]).length) {
      delete toBeEmptied[storageType];
    }
  }
}

function testAddIframe(front) {
  info("Testing if new iframe addition works properly");
  return new Promise(resolve => {
    let shouldBeEmpty = {
      localStorage: {
        "https://sectest1.example.org": ["iframe-s-ls1"]
      },
      sessionStorage: {
        "https://sectest1.example.org": ["iframe-s-ss1"]
      },
      cookies: {
        "https://sectest1.example.org": [
          getCookieId("cs2", ".example.org", "/"),
          getCookieId("sc1", "sectest1.example.org",
                      "/browser/devtools/server/tests/browser/")
        ],
        "http://sectest1.example.org": [
          getCookieId("sc1", "sectest1.example.org",
                      "/browser/devtools/server/tests/browser/")
        ]
      },
      indexedDB: {
        // empty because indexed db creation happens after the page load, so at
        // the time of window-ready, there was no indexed db present.
        "https://sectest1.example.org": []
      },
      Cache: {
        "https://sectest1.example.org": []
      }
    };

    let onStoresUpdate = data => {
      info("checking if the hosts list is correct for this iframe addition");

      markOutMatched(shouldBeEmpty, data.added);

      ok(!data.changed || !data.changed.cookies ||
         !data.changed.cookies["https://sectest1.example.org"],
         "Nothing got changed for cookies");
      ok(!data.changed || !data.changed.localStorage ||
         !data.changed.localStorage["https://sectest1.example.org"],
         "Nothing got changed for local storage");
      ok(!data.changed || !data.changed.sessionStorage ||
         !data.changed.sessionStorage["https://sectest1.example.org"],
         "Nothing got changed for session storage");
      ok(!data.changed || !data.changed.indexedDB ||
         !data.changed.indexedDB["https://sectest1.example.org"],
         "Nothing got changed for indexed db");

      ok(!data.deleted || !data.deleted.cookies ||
         !data.deleted.cookies["https://sectest1.example.org"],
         "Nothing got deleted for cookies");
      ok(!data.deleted || !data.deleted.localStorage ||
         !data.deleted.localStorage["https://sectest1.example.org"],
         "Nothing got deleted for local storage");
      ok(!data.deleted || !data.deleted.sessionStorage ||
         !data.deleted.sessionStorage["https://sectest1.example.org"],
         "Nothing got deleted for session storage");
      ok(!data.deleted || !data.deleted.indexedDB ||
         !data.deleted.indexedDB["https://sectest1.example.org"],
         "Nothing got deleted for indexed db");

      if (!Object.keys(shouldBeEmpty).length) {
        info("Everything to be received is received.");
        endTestReloaded();
      }
    };

    let endTestReloaded = () => {
      front.off("stores-update", onStoresUpdate);
      resolve();
    };

    front.on("stores-update", onStoresUpdate);

    let iframe = content.document.createElement("iframe");
    iframe.src = ALT_DOMAIN_SECURED + "storage-secured-iframe.html";
    content.document.querySelector("body").appendChild(iframe);
  });
}

function testRemoveIframe(front) {
  info("Testing if iframe removal works properly");
  return new Promise(resolve => {
    let shouldBeEmpty = {
      localStorage: {
        "http://sectest1.example.org": []
      },
      sessionStorage: {
        "http://sectest1.example.org": []
      },
      Cache: {
        "http://sectest1.example.org": []
      },
      indexedDB: {
        "http://sectest1.example.org": []
      }
    };

    let onStoresUpdate = data => {
      info("checking if the hosts list is correct for this iframe deletion");

      markOutMatched(shouldBeEmpty, data.deleted, true);

      ok(!data.deleted.cookies || !data.deleted.cookies["sectest1.example.org"],
        "Nothing got deleted for Cookies as " +
        "the same hostname is still present");

      ok(!data.changed || !data.changed.cookies ||
         !data.changed.cookies["http://sectest1.example.org"],
         "Nothing got changed for cookies");
      ok(!data.changed || !data.changed.localStorage ||
         !data.changed.localStorage["http://sectest1.example.org"],
         "Nothing got changed for local storage");
      ok(!data.changed || !data.changed.sessionStorage ||
         !data.changed.sessionStorage["http://sectest1.example.org"],
         "Nothing got changed for session storage");

      ok(!data.added || !data.added.cookies ||
         !data.added.cookies["http://sectest1.example.org"],
         "Nothing got added for cookies");
      ok(!data.added || !data.added.localStorage ||
         !data.added.localStorage["http://sectest1.example.org"],
         "Nothing got added for local storage");
      ok(!data.added || !data.added.sessionStorage ||
         !data.added.sessionStorage["http://sectest1.example.org"],
         "Nothing got added for session storage");

      if (!Object.keys(shouldBeEmpty).length) {
        info("Everything to be received is received.");
        endTestReloaded();
      }
    };

    let endTestReloaded = () => {
      front.off("stores-update", onStoresUpdate);
      resolve();
    };

    front.on("stores-update", onStoresUpdate);

    for (let iframe of content.document.querySelectorAll("iframe")) {
      if (iframe.src.startsWith("http:")) {
        iframe.remove();
        break;
      }
    }
  });
}

add_task(function* () {
  yield openTabAndSetupStorage(MAIN_DOMAIN + "storage-dynamic-windows.html");

  initDebuggerServer();
  let client = new DebuggerClient(DebuggerServer.connectPipe());
  let form = yield connectDebuggerClient(client);
  let front = StorageFront(client, form);
  let data = yield front.listStores();
  yield testStores(data, front);

  yield clearStorage();

  // Forcing GC/CC to get rid of docshells and windows created by this test.
  forceCollections();
  yield client.close();
  forceCollections();
  DebuggerServer.destroy();
  forceCollections();
});