summaryrefslogtreecommitdiffstats
path: root/toolkit/modules/tests/xpcshell/test_PrivacyLevel.js
blob: b392e4a8ae7796f74c78bddf8c27c714a29a9e73 (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
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const kPref = "browser.sessionstore.privacy_level";

// Collect data from all sites (http and https).
const PRIVACY_NONE = 0;
// Collect data from unencrypted sites (http), only.
const PRIVACY_ENCRYPTED = 1;
// Collect no data.
const PRIVACY_FULL = 2;

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

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

const kFormInsecureData = {
  url: "http://example.com/",
  other: "hello",
};

const kFormSecureData = {
  url: "https://example.com/",
  other: "secure hello",
};

add_task(async function test_PrivacyLevelAndFilter() {
  Services.prefs.setIntPref(kPref, PRIVACY_NONE);
  Assert.ok(
    PrivacyLevel.check(
      "http://example.com/",
      "Can save http page with no privacy"
    )
  );
  Assert.ok(
    PrivacyLevel.check("https://example.com/"),
    "Can save https page with no privacy"
  );

  Assert.deepEqual(
    PrivacyFilter.filterFormData(kFormInsecureData),
    kFormInsecureData,
    "Filtered data matches for insecure data with no privacy."
  );

  Assert.deepEqual(
    PrivacyFilter.filterFormData(kFormSecureData),
    kFormSecureData,
    "Filtered data matches for secure data with no privacy."
  );

  // Specialcase: empty object.
  Assert.equal(
    PrivacyFilter.filterFormData({}),
    null,
    "Filtering an empty object returns null."
  );

  Services.prefs.setIntPref(kPref, PRIVACY_ENCRYPTED);
  Assert.ok(
    PrivacyLevel.check(
      "http://example.com/",
      "Can save http page with encrypted privacy"
    )
  );
  Assert.ok(
    !PrivacyLevel.check("https://example.com/"),
    "Can't save https page with encrypted privacy"
  );

  Assert.deepEqual(
    PrivacyFilter.filterFormData(kFormInsecureData),
    kFormInsecureData,
    "Filtered data matches for insecure data with encrypted privacy."
  );

  Assert.deepEqual(
    PrivacyFilter.filterFormData(kFormSecureData),
    null,
    "Filtered data matches for secure data with encrypted privacy."
  );

  Services.prefs.setIntPref(kPref, PRIVACY_FULL);
  Assert.ok(
    !PrivacyLevel.check(
      "http://example.com/",
      "Can't save http page with full privacy"
    )
  );
  Assert.ok(
    !PrivacyLevel.check("https://example.com/"),
    "Can't save https page with full privacy"
  );

  Assert.deepEqual(
    PrivacyFilter.filterFormData(kFormInsecureData),
    null,
    "No filtered data for insecure data with full privacy."
  );

  Assert.deepEqual(
    PrivacyFilter.filterFormData(kFormSecureData),
    null,
    "No filtered data for secure data with full privacy."
  );
});