summaryrefslogtreecommitdiffstats
path: root/dom/tests/browser/browser_persist_cookies.js
blob: 60a70d6e886525aa99cd089c8bf2939f356f7978 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const TEST_PATH = getRootDirectory(gTestPath).replace(
  "chrome://mochitests/content",
  "https://example.org"
);
const TEST_PATH2 = getRootDirectory(gTestPath).replace(
  "chrome://mochitests/content",
  "https://example.com"
);

var MockFilePicker = SpecialPowers.MockFilePicker;
MockFilePicker.init(window.browsingContext);

registerCleanupFunction(async function () {
  info("Running the cleanup code");
  MockFilePicker.cleanup();
  Services.obs.removeObserver(checkRequest, "http-on-modify-request");
  SpecialPowers.clearUserPref("network.cookie.sameSite.laxByDefault");
  if (gTestDir && gTestDir.exists()) {
    // On Windows, sometimes nsIFile.remove() throws, probably because we're
    // still writing to the directory we're trying to remove, despite
    // waiting for the download to complete. Just retry a bit later...
    let succeeded = false;
    while (!succeeded) {
      try {
        gTestDir.remove(true);
        succeeded = true;
      } catch (ex) {
        await new Promise(requestAnimationFrame);
      }
    }
  }
});

let gTestDir = null;

function checkRequest(subject) {
  let httpChannel = subject.QueryInterface(Ci.nsIHttpChannel);
  let spec = httpChannel.URI.spec;
  // Ignore initial requests for page that sets cookies and its favicon, which may not have
  // cookies.
  if (
    httpChannel.URI.host == "example.org" &&
    !spec.endsWith("favicon.ico") &&
    !spec.includes("redirect.sjs")
  ) {
    let cookie = httpChannel.getRequestHeader("cookie");
    is(
      cookie.trim(),
      "normalCookie=true",
      "Should have correct cookie in request for " + spec
    );
  }
}

function createTemporarySaveDirectory() {
  var saveDir = Services.dirsvc.get("TmpD", Ci.nsIFile);
  saveDir.append("testsavedir");
  if (!saveDir.exists()) {
    info("create testsavedir!");
    saveDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o755);
  }
  info("return from createTempSaveDir: " + saveDir.path);
  return saveDir;
}

add_task(async function () {
  // Use nsICookieService.BEHAVIOR_REJECT_TRACKER to avoid cookie partitioning.
  // In this test case, if the cookie is partitioned, there will be no cookie
  // nsICookieServicebeing sent to compare.
  await SpecialPowers.pushPrefEnv({
    set: [
      ["network.cookie.cookieBehavior", 4],
      // Bug 1617611: Fix all the tests broken by "cookies SameSite=lax by default"
      ["network.cookie.sameSite.laxByDefault", false],
    ],
  });

  await BrowserTestUtils.withNewTab("about:blank", async function (browser) {
    Services.obs.addObserver(checkRequest, "http-on-modify-request");
    BrowserTestUtils.startLoadingURIString(
      browser,
      TEST_PATH + "set-samesite-cookies-and-redirect.sjs"
    );
    // Test that the original document load doesn't send same-site cookies.
    await BrowserTestUtils.browserLoaded(
      browser,
      true,
      TEST_PATH2 + "set-samesite-cookies-and-redirect.sjs"
    );
    // Now check the saved page.
    // Create the folder the link will be saved into.
    gTestDir = createTemporarySaveDirectory();
    let destFile = gTestDir.clone();

    MockFilePicker.displayDirectory = gTestDir;
    let fileName;
    MockFilePicker.showCallback = function (fp) {
      info("showCallback");
      fileName = fp.defaultString;
      info("fileName: " + fileName);
      destFile.append(fileName);
      info("path: " + destFile.path);
      MockFilePicker.setFiles([destFile]);
      MockFilePicker.filterIndex = 0; // kSaveAsType_Complete
      info("done showCallback");
    };
    saveBrowser(browser);
    let dls = await Downloads.getList(Downloads.PUBLIC);
    await new Promise((resolve, reject) => {
      dls.addView({
        onDownloadChanged(download) {
          if (download.succeeded) {
            dls.removeView(this);
            dls.removeFinished();
            resolve();
          } else if (download.error) {
            reject("Download failed");
          }
        },
      });
    });
  });
});