summaryrefslogtreecommitdiffstats
path: root/toolkit/components/contextualidentity/tests/unit/test_corruptedFile.js
blob: 9dfa13ae2e8b8e8f09b7a1c9d48370a5d0c2714f (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
"use strict";

const profileDir = do_get_profile();

const { ContextualIdentityService } = ChromeUtils.importESModule(
  "resource://gre/modules/ContextualIdentityService.sys.mjs"
);

const TEST_STORE_FILE_PATH = PathUtils.join(
  profileDir.path,
  "test-containers.json"
);

const BASE_URL = "http://example.org/";

const COOKIE = {
  host: BASE_URL,
  path: "/",
  name: "test",
  value: "yes",
  isSecure: false,
  isHttpOnly: false,
  isSession: true,
  expiry: 2145934800,
};

function createCookie(userContextId) {
  Services.cookies.add(
    COOKIE.host,
    COOKIE.path,
    COOKIE.name,
    COOKIE.value,
    COOKIE.isSecure,
    COOKIE.isHttpOnly,
    COOKIE.isSession,
    COOKIE.expiry,
    { userContextId },
    Ci.nsICookie.SAMESITE_NONE,
    Ci.nsICookie.SCHEME_HTTP
  );
}

function hasCookie(userContextId) {
  let found = false;
  for (let cookie of Services.cookies.getCookiesFromHost(BASE_URL, {
    userContextId,
  })) {
    if (cookie.originAttributes.userContextId == userContextId) {
      found = true;
      break;
    }
  }
  return found;
}

// Correpted file should delete all.
add_task(async function corruptedFile() {
  const thumbnailPrivateId = ContextualIdentityService._defaultIdentities
    .filter(identity => identity.name === "userContextIdInternal.thumbnail")
    .pop().userContextId;

  const webextStoragePrivateId = ContextualIdentityService._defaultIdentities
    .filter(
      identity => identity.name === "userContextIdInternal.webextStorageLocal"
    )
    .pop().userContextId;

  // Create a cookie in the default Firefox identity (userContextId 0).
  createCookie(0);

  // Create a cookie in the userContextId 1.
  createCookie(1);

  // Create a cookie in the thumbnail private userContextId.
  createCookie(thumbnailPrivateId);

  // Create a cookie in the extension storage private userContextId.
  createCookie(webextStoragePrivateId);

  ok(hasCookie(0), "We have the new cookie the default firefox identity!");
  ok(hasCookie(1), "We have the new cookie in a public identity!");
  ok(
    hasCookie(thumbnailPrivateId),
    "We have the new cookie in the thumbnail private identity!"
  );
  ok(
    hasCookie(webextStoragePrivateId),
    "We have the new cookie in the extension storage private identity!"
  );

  // Let's create a corrupted file.
  await IOUtils.writeUTF8(TEST_STORE_FILE_PATH, "{ vers", {
    tmpPath: TEST_STORE_FILE_PATH + ".tmp",
  });

  let cis =
    ContextualIdentityService.createNewInstanceForTesting(TEST_STORE_FILE_PATH);
  ok(!!cis, "We have our instance of ContextualIdentityService");

  equal(
    cis.getPublicIdentities().length,
    4,
    "We should have the default public identities"
  );

  Assert.deepEqual(
    cis.getPublicUserContextIds(),
    cis.getPublicIdentities().map(identity => identity.userContextId),
    "getPublicUserContextIds has matching user context IDs"
  );

  // Verify that when the containers.json file is being rebuilt, the computed lastUserContextId
  // is the expected one.
  equal(
    cis._lastUserContextId,
    thumbnailPrivateId,
    "Expect cis._lastUserContextId to be equal to the thumbnails userContextId"
  );

  const privThumbnailIdentity = cis.getPrivateIdentity(
    "userContextIdInternal.thumbnail"
  );
  equal(
    privThumbnailIdentity && privThumbnailIdentity.userContextId,
    thumbnailPrivateId,
    "We should have the default thumbnail private identity"
  );

  const privWebextStorageIdentity = cis.getPrivateIdentity(
    "userContextIdInternal.webextStorageLocal"
  );
  equal(
    privWebextStorageIdentity && privWebextStorageIdentity.userContextId,
    webextStoragePrivateId,
    "We should have the default extensions storage.local private identity"
  );

  // Cookie is gone!
  ok(
    !hasCookie(1),
    "We should not have the new cookie in the userContextId 1!"
  );

  // The data stored in the Firefox default userContextId (0), should have not be cleared.
  ok(
    hasCookie(0),
    "We should not have the new cookie in the default Firefox identity!"
  );

  // The data stored in the non-public userContextId (e.g. thumbnails private identity)
  // should have not be cleared.
  ok(
    hasCookie(thumbnailPrivateId),
    "We should have the new cookie in the thumbnail private userContextId!"
  );
  ok(
    hasCookie(webextStoragePrivateId),
    "We should have the new cookie in the extension storage private userContextId!"
  );

  // Verify the version of the newly created containers.json file.
  cis.save();
  const stateFileText = await IOUtils.readUTF8(TEST_STORE_FILE_PATH);
  equal(
    JSON.parse(stateFileText).version,
    cis.LAST_CONTAINERS_JSON_VERSION,
    "Expect the new containers.json file to have the expected version"
  );
});