Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / netwerk / test / unit / test_remove_invalid_first_party_partitioned_cookie.js
blob177300feb29dca7d40e0f19825efed4c17c8b0a5
1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
4 // The test ensure we remove first-party partitioned cookies that don't have
5 // partitioned attribute.
7 add_task(async function run_test() {
8 // Set up a profile.
9 let profile = do_get_profile();
11 // Start the cookieservice, to force creation of a database.
12 Services.cookies.sessionCookies;
14 // Close the profile.
15 await promise_close_profile();
17 // Create a schema 14 database.
18 let schema14db = new CookieDatabaseConnection(
19 do_get_cookie_file(profile),
23 let now = Math.round(Date.now() / 1000);
25 // Create an invalid first-party partitioned cookie.
26 let invalidFPCookie = new Cookie(
27 "invalid",
28 "bad",
29 "example.com",
30 "/",
31 now + 34560000,
32 now,
33 now,
34 false, // isSession
35 true, // isSecure
36 false, // isHttpOnly
37 false, // isBrowserElement
38 { partitionKey: "(https,example.com)" },
39 Ci.nsICookie.SAMESITE_NONE,
40 Ci.nsICookie.SAMESITE_NONE,
41 Ci.nsICookie.SCHEME_UNSET,
42 false // isPartitioned
44 schema14db.insertCookie(invalidFPCookie);
46 // Create a valid first-party partitioned cookie(CHIPS).
47 let valid1stCHIPS = new Cookie(
48 "valid1stCHIPS",
49 "good",
50 "example.com",
51 "/",
52 now + 34560000,
53 now,
54 now,
55 false, // isSession
56 true, // isSecure
57 false, // isHttpOnly
58 false, // isBrowserElement
59 { partitionKey: "(https,example.com)" },
60 Ci.nsICookie.SAMESITE_NONE,
61 Ci.nsICookie.SAMESITE_NONE,
62 Ci.nsICookie.SCHEME_UNSET,
63 true // isPartitioned
65 schema14db.insertCookie(valid1stCHIPS);
67 // Create a valid unpartitioned cookie.
68 let unpartitionedCookie = new Cookie(
69 "valid",
70 "good",
71 "example.com",
72 "/",
73 now + 34560000,
74 now,
75 now,
76 false, // isSession
77 true, // isSecure
78 false, // isHttpOnly
79 false, // isBrowserElement
80 {},
81 Ci.nsICookie.SAMESITE_NONE,
82 Ci.nsICookie.SAMESITE_NONE,
83 Ci.nsICookie.SCHEME_UNSET,
84 false // isPartitioned
86 schema14db.insertCookie(unpartitionedCookie);
88 // Create valid third-party partitioned TCP cookie.
89 let valid3rdTCPCookie = new Cookie(
90 "valid3rdTCP",
91 "good",
92 "example.com",
93 "/",
94 now + 34560000,
95 now,
96 now,
97 false, // isSession
98 true, // isSecure
99 false, // isHttpOnly
100 false, // isBrowserElement
101 { partitionKey: "(https,example.org)" },
102 Ci.nsICookie.SAMESITE_NONE,
103 Ci.nsICookie.SAMESITE_NONE,
104 Ci.nsICookie.SCHEME_UNSET,
105 false // isPartitioned
107 schema14db.insertCookie(valid3rdTCPCookie);
109 // Create valid third-party partitioned CHIPS cookie.
110 let valid3rdCHIPSCookie = new Cookie(
111 "valid3rdCHIPS",
112 "good",
113 "example.com",
114 "/",
115 now + 34560000,
116 now,
117 now,
118 false, // isSession
119 true, // isSecure
120 false, // isHttpOnly
121 false, // isBrowserElement
122 { partitionKey: "(https,example.org)" },
123 Ci.nsICookie.SAMESITE_NONE,
124 Ci.nsICookie.SAMESITE_NONE,
125 Ci.nsICookie.SCHEME_UNSET,
126 true // isPartitioned
128 schema14db.insertCookie(valid3rdCHIPSCookie);
130 schema14db.close();
131 schema14db = null;
133 // Check if we have the right testing entries
135 const dbConnection = Services.storage.openDatabase(
136 do_get_cookie_file(profile)
138 const stmt = dbConnection.createStatement(
139 "SELECT count(name) FROM moz_cookies WHERE host = 'example.com';"
141 const success = stmt.executeStep();
142 Assert.ok(success);
144 const count = stmt.getInt32(0);
145 Assert.equal(count, 5);
146 stmt.finalize();
147 dbConnection.close();
150 // Reload profile.
151 await promise_load_profile();
153 // Check the number of unpartitioned cookies is correct, and we only have
154 // good cookies.
155 let cookies = Services.cookies.getCookiesFromHost("example.com", {});
156 Assert.equal(cookies.length, 1);
157 for (const cookie of cookies) {
158 Assert.equal(cookie.value, "good");
161 // Check the number of first-party partitioned cookies is correct, and we only
162 // have good cookies.
163 cookies = Services.cookies.getCookiesFromHost("example.com", {
164 partitionKey: "(https,example.com)",
166 Assert.equal(cookies.length, 1);
167 for (const cookie of cookies) {
168 Assert.equal(cookie.value, "good");
171 // Check the number of third-party partitioned cookies is correct, and we only
172 // have good cookies.
173 cookies = Services.cookies.getCookiesFromHost("example.com", {
174 partitionKey: "(https,example.org)",
176 Assert.equal(cookies.length, 2);
177 for (const cookie of cookies) {
178 Assert.equal(cookie.value, "good");
181 // Ensure the invalid cookies is gone in the DB.
183 const dbConnection = Services.storage.openDatabase(
184 do_get_cookie_file(profile)
186 const stmt = dbConnection.createStatement(
187 "SELECT count(name) FROM moz_cookies WHERE value = 'bad';"
189 const success = stmt.executeStep();
190 Assert.ok(success);
192 const count = stmt.getInt32(0);
193 Assert.equal(count, 0);
194 stmt.finalize();
195 dbConnection.close();
198 // Cleanup
199 Services.cookies.removeAll();
200 do_close_profile();