summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_cookies_partition_counting.js
blob: 26bd56e1f6bdf76e0495be716a244ad127beec70 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

add_setup(function test_setup() {
  // FOG needs a profile directory to put its data in.
  do_get_profile();

  // FOG needs to be initialized in order for data to flow.
  Services.fog.initializeFOG();
});

add_task(async function test_purge_counting() {
  let profile = do_get_profile();
  let dbFile = do_get_cookie_file(profile);
  Assert.ok(!dbFile.exists());

  let schema12db = new CookieDatabaseConnection(dbFile, 12);
  let now = Date.now() * 1000; // date in microseconds

  // add some non-partitioned cookies for key
  let cookieNum1 = 1;
  let hostNonPartitioned = "cookie-host-non-partitioned.com";
  for (let i = 0; i < cookieNum1; i++) {
    let cookie = new Cookie(
      "cookie-name" + i,
      "cookie-value" + i,
      hostNonPartitioned,
      "/", // path
      now, // expiry
      now, // needed to get the cookie by the db init
      now, // creation time
      false, // session
      false, // secure
      false, // http-only
      false, // inBrowserElement
      {} // OA
    );
    schema12db.insertCookie(cookie);
  }

  // add some non-partitioned cookies different key
  let cookieNum2 = 10;
  for (let i = 0; i < cookieNum2; i++) {
    let cookie = new Cookie(
      "cookie-name" + i,
      "cookie-value" + i,
      hostNonPartitioned,
      "/", // path
      now, // expiry
      now, // needed to get the cookie by the db init
      now, // creation time
      false, // session
      false, // secure
      false, // http-only
      false, // inBrowserElement
      { userContextId: 8 } // OA
    );
    schema12db.insertCookie(cookie);
  }

  // add some partitioned cookies
  let hostPartitioned = "host-partitioned.com";
  let cookieNum3 = 3;
  for (let i = 0; i < cookieNum3; i++) {
    let cookie = new Cookie(
      "cookie-name" + i,
      "cookie-value" + i,
      hostPartitioned,
      "/", // path
      now, // expiry
      now, // needed to get the cookie by the db init
      now, // creation time
      false, // session
      false, // secure
      false, // http-only
      false, // inBrowserElement
      { partitionKey: "(https,example.com)" }
    );
    schema12db.insertCookie(cookie);
  }

  // add some partitioned with different OA
  let cookieNum4 = 35;
  for (let i = 0; i < cookieNum4; i++) {
    let cookie = new Cookie(
      "cookie-name" + i,
      "cookie-value" + i,
      hostPartitioned,
      "/", // path
      now, // expiry
      now, // needed to get the cookie by the db init
      now, // creation time
      false, // session
      false, // secure
      false, // http-only
      false, // inBrowserElement
      { partitionKey: "(https,example.com)", userContextId: 7 }
    );
    schema12db.insertCookie(cookie);
  }

  let allCookieCount = cookieNum1 + cookieNum2 + cookieNum3 + cookieNum4;
  Assert.equal(do_count_cookies_in_db(schema12db.db), allCookieCount);

  // startup the cookie service and check the cookie counts
  let cookieCountNonPart =
    Services.cookies.countCookiesFromHost(hostNonPartitioned); // includes expired cookies
  Assert.equal(cookieCountNonPart, cookieNum1);
  let cookieCountNonPartOA = Services.cookies.getCookiesFromHost(
    hostNonPartitioned,
    { userContextId: 8 }
  ).length; // includes expired cookies
  Assert.equal(cookieCountNonPartOA, cookieNum2);
  let cookieCountPart = Services.cookies.getCookiesFromHost(hostPartitioned, {
    partitionKey: "(https,example.com)",
  }).length; // includes expired cookies
  Assert.equal(cookieCountPart, cookieNum3);
  let cookieCountPartOA = Services.cookies.getCookiesFromHost(hostPartitioned, {
    partitionKey: "(https,example.com)",
    userContextId: 7,
  }).length; // includes expired cookies
  Assert.equal(cookieCountPartOA, cookieNum4);

  // trigger the collection
  Services.obs.notifyObservers(null, "idle-daily");

  // check telem fired for all cookie count
  let cct = await Glean.networking.cookieCountTotal.testGetValue();
  Assert.equal(cct.sum, allCookieCount, "All cookies telem");

  // check telem for all un/partitioned counts
  let ccp = await Glean.networking.cookieCountPartitioned.testGetValue();
  Assert.equal(
    ccp.sum,
    cookieNum3 + cookieNum4,
    "All partitioned cookies telem"
  );

  let ccu = await Glean.networking.cookieCountUnpartitioned.testGetValue();
  Assert.equal(
    ccu.sum,
    cookieNum1 + cookieNum2,
    "All unpartitioned cookies telem"
  );

  // check telem for part by key (host+OA)
  // Note: With the decided histogram layout we see the buckets
  // (used for indexing the histogram's buckets)
  let histPartByKey =
    await Glean.networking.cookieCountPartByKey.testGetValue();
  Assert.equal(histPartByKey.values[2], 1, "Partitioned bucket 2 has 1 value");
  Assert.equal(
    histPartByKey.values[31],
    1,
    "Partitioned bucket 31 has 1 value"
  );
  Assert.equal(
    histPartByKey.sum,
    cookieNum3 + cookieNum4,
    "Partitioned bucket sums correctly"
  );

  // check telem for unpart by key (host+OA)
  let histUnpartByKey =
    await Glean.networking.cookieCountUnpartByKey.testGetValue();
  Assert.equal(
    histUnpartByKey.values[1],
    1,
    "Unpartitioned bucket 1 has 1 value"
  );
  Assert.equal(
    histUnpartByKey.values[8],
    1,
    "Unpartitioned bucket 8 has 1 value"
  );
  Assert.equal(
    histUnpartByKey.sum,
    cookieNum1 + cookieNum2,
    "Unpartitioned bucket sums correctly"
  );

  schema12db.close();
});