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() {
9 let profile
= do_get_profile();
11 // Start the cookieservice, to force creation of a database.
12 Services
.cookies
.sessionCookies
;
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(
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(
58 false, // isBrowserElement
59 { partitionKey
: "(https,example.com)" },
60 Ci
.nsICookie
.SAMESITE_NONE
,
61 Ci
.nsICookie
.SAMESITE_NONE
,
62 Ci
.nsICookie
.SCHEME_UNSET
,
65 schema14db
.insertCookie(valid1stCHIPS
);
67 // Create a valid unpartitioned cookie.
68 let unpartitionedCookie
= new Cookie(
79 false, // isBrowserElement
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(
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(
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
);
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();
144 const count
= stmt
.getInt32(0);
145 Assert
.equal(count
, 5);
147 dbConnection
.close();
151 await
promise_load_profile();
153 // Check the number of unpartitioned cookies is correct, and we only have
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();
192 const count
= stmt
.getInt32(0);
193 Assert
.equal(count
, 0);
195 dbConnection
.close();
199 Services
.cookies
.removeAll();