summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/localstorage/test_cookieSession.html
blob: 1fdd6f4d5656fe874d42e7ed818b3f92dfa70312 (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
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>cookie per-session only test</title>

<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />

<script type="text/javascript">

/*
  Set cookie access to be just per session and store to the localStorage.
  Content stored must prevail only for session of the browser, so it must
  be accessible in another window we try to access that key in the same
  storage.
 */

function pushCookie(aPermission, aNext) {
  SpecialPowers.pushPermissions([{'type': 'cookie', 'allow': aPermission, 'context': document}], aNext);
}

function test1() {
  localStorage.setItem("persistent1", "persistent value 1");
  localStorage.setItem("persistent2", "persistent value 2");

  pushCookie(SpecialPowers.Ci.nsICookiePermission.ACCESS_SESSION, test1_b);
}

function test1_b() {
  localStorage.setItem("session only", "session value");
  parent.is(localStorage.getItem("session only"), "session value");
  parent.is(localStorage.getItem("persistent1"), "persistent value 1");
  parent.is(localStorage.getItem("persistent2"), "persistent value 2");

  window.location.search = '?2';
}

function test2()
{
  parent.is(localStorage.getItem("session only"), "session value", "Value present when cookies in session-only mode");
  parent.is(localStorage.getItem("persistent1"), "persistent value 1", "Persistent value present");
  parent.is(localStorage.getItem("persistent2"), "persistent value 2", "Persistent value present");

  localStorage.setItem("persistent1", "changed persistent value 1");
  localStorage.removeItem("persistent2");

  parent.is(localStorage.getItem("session only"), "session value", "Value present when cookies in session-only mode");
  parent.is(localStorage.getItem("persistent1"), "changed persistent value 1", "Persistent value present");
  parent.is(localStorage.getItem("persistent2"), null, "Persistent value removed");

  // This clear has to delete only changes made in session only mode
  localStorage.clear();

  parent.is(localStorage.getItem("session only"), null, "Value not present when cookies in session-only mode after delete");
  parent.is(localStorage.getItem("persistent1"), null, "Persistent value not present in session only after delete");
  parent.is(localStorage.getItem("persistent2"), null, "Persistent value not present in session only after delete");

  localStorage.setItem("session only 2", "must be deleted on drop of session-only cookies permissions");

  pushCookie(SpecialPowers.Ci.nsICookiePermission.ACCESS_DEFAULT, function() { window.location.search = '?3'; });
}

function test3() {
  parent.is(localStorage.getItem("session only"), null, "No value when cookies are in default mode");
  parent.is(localStorage.getItem("session only 2"), null, "No value when cookies are in default mode");
  parent.is(localStorage.getItem("persistent1"), "persistent value 1", "Persistent value present");
  parent.is(localStorage.getItem("persistent2"), "persistent value 2", "Persistent value present");

  pushCookie(SpecialPowers.Ci.nsICookiePermission.ACCESS_SESSION, function() { window.location.search = '?4'; });
}

function test4() {
  parent.is(localStorage.getItem("session only"), null, "Value not present when cookies in session-only mode after delete");
  parent.is(localStorage.getItem("session only 2"), null, "Value not present when cookies in session-only mode after delete");
  parent.is(localStorage.getItem("persistent1"), "persistent value 1", "Persistent value present again");
  parent.is(localStorage.getItem("persistent2"), "persistent value 2", "Persistent value present again");

  pushCookie(SpecialPowers.Ci.nsICookiePermission.ACCESS_DEFAULT, function() { window.location.search = '?5'; });
}

function test5() {
  localStorage.clear();

  parent.is(localStorage.getItem("session only"), null, "No value when cookies are in default mode");
  parent.is(localStorage.getItem("persistent1"), null, "Persistent value not present after delete");
  parent.is(localStorage.getItem("persistent2"), null, "Persistent value not present after delete");

  pushCookie(SpecialPowers.Ci.nsICookiePermission.ACCESS_SESSION, function() { window.location.search = '?6'; });
}

function test6() {
  parent.is(localStorage.getItem("session only"), null, "Value not present when cookies in session-only mode after delete");
  parent.is(localStorage.getItem("session only 2"), null, "No value when cookies are in default mode");
  parent.is(localStorage.getItem("persistent1"), null, "Persistent value not present in session only after delete");
  parent.is(localStorage.getItem("persistent2"), null, "Persistent value not present in session only after delete");

  parent.SimpleTest.finish();
}

function startTest() {
  switch (location.search) {
    case '?1':
      test1();
      break;
    case '?2':
      test2();
      break;
    case '?3':
      test3();
      break;
    case '?4':
      test4();
      break;
    case '?5':
      test5();
      break;
    case '?6':
      test6();
      break;
    default:
      SimpleTest.waitForExplicitFinish();
      var iframe = document.createElement('iframe');
      iframe.src = 'test_cookieSession.html?1';
      document.body.appendChild(iframe);
  }
}
</script>

</head>

<body onload="startTest()">
</body>
</html>