summaryrefslogtreecommitdiffstats
path: root/toolkit/components/cleardata/tests/unit/test_identity_credential_storage.js
blob: 13369fc78754ac3a53a5927d8c7ec0149d68a619 (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
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

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

XPCOMUtils.defineLazyServiceGetter(
  this,
  "IdentityCredentialStorageService",
  "@mozilla.org/browser/identity-credential-storage-service;1",
  "nsIIdentityCredentialStorageService"
);

do_get_profile();

add_task(async function test_deleteByRange() {
  Services.prefs.setBoolPref(
    "dom.security.credentialmanagement.identity.enabled",
    true
  );
  const expiry = Date.now() + 24 * 60 * 60;
  let rpPrincipal = Services.scriptSecurityManager.createContentPrincipal(
    Services.io.newURI("https://rp.com/"),
    {}
  );
  let idpPrincipal = Services.scriptSecurityManager.createContentPrincipal(
    Services.io.newURI("https://idp.com/"),
    {}
  );
  const credentialID = "ID";

  // Test initial value
  let registered = {};
  let allowLogout = {};
  IdentityCredentialStorageService.getState(
    rpPrincipal,
    idpPrincipal,
    credentialID,
    registered,
    allowLogout
  );
  Assert.ok(!registered.value, "Should not be registered initially.");
  Assert.ok(!allowLogout.value, "Should not allow logout initially.");

  // Set and read a value
  IdentityCredentialStorageService.setState(
    rpPrincipal,
    idpPrincipal,
    credentialID,
    true,
    true
  );

  IdentityCredentialStorageService.getState(
    rpPrincipal,
    idpPrincipal,
    credentialID,
    registered,
    allowLogout
  );
  Assert.ok(registered.value, "Should be registered by set.");
  Assert.ok(allowLogout.value, "Should now allow logout by set.");

  let from = Date.now() + 60 * 60;
  await new Promise(aResolve => {
    Services.clearData.deleteDataInTimeRange(
      from * 1000,
      expiry * 1000,
      true /* user request */,
      Ci.nsIClearDataService.CLEAR_CREDENTIAL_MANAGER_STATE,
      value => {
        Assert.equal(value, 0);
        aResolve();
      }
    );
  });

  IdentityCredentialStorageService.getState(
    rpPrincipal,
    idpPrincipal,
    credentialID,
    registered,
    allowLogout
  );

  Assert.ok(
    registered.value,
    "Should be existing since the value is not deleted"
  );

  from = Date.now() - 60 * 60;

  await new Promise(aResolve => {
    Services.clearData.deleteDataInTimeRange(
      from * 1000,
      expiry * 1000,
      true /* user request */,
      Ci.nsIClearDataService.CLEAR_CREDENTIAL_MANAGER_STATE,
      value => {
        Assert.equal(value, 0);
        aResolve();
      }
    );
  });

  IdentityCredentialStorageService.getState(
    rpPrincipal,
    idpPrincipal,
    credentialID,
    registered,
    allowLogout
  );
  Assert.ok(!registered.value, "Should not be existing");

  Services.prefs.clearUserPref(
    "dom.security.credentialmanagement.identity.enabled"
  );
});