summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/sessionstorage/test_cookieSession.html
blob: 01bd596ee3d70dac240457b58f4100887f2fe778 (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
<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 sessionStorage.
  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(aValue, aNext) {
  SpecialPowers.pushPermissions([{'type': 'cookie', 'allow': aValue, 'context': document}], pushPermissionAndTest);
}

function pushPermissionAndTest() {
  var test = tests.shift();
  if (test) {
    document.getElementById('testframe').onload = test;
    /* After every permission change, an iframe has to be reloaded,
       otherwise this test causes failures in b2g (oop) mochitest, because
       the permission changes don't seem to be always picked up
       by the code that excercises it */
    document.getElementById('testframe').contentWindow.location.reload();
  } else {
    ok(false, 'should not be reached');
    SimpleTest.finish();
  }
}

function startTest() {
  pushCookie(SpecialPowers.Ci.nsICookiePermission.ACCESS_DEFAULT);
}

var tests = [
  function test1() {
    sessionStorage.setItem("persistent1", "persistent value 1");
    sessionStorage.setItem("persistent2", "persistent value 2");

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

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

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

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

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

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

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

    pushCookie(SpecialPowers.Ci.nsICookiePermission.ACCESS_DEFAULT);
  },

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

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

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

    pushCookie(SpecialPowers.Ci.nsICookiePermission.ACCESS_DEFAULT);
  },

  function test5() {
    sessionStorage.clear();

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

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

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

    pushCookie(SpecialPowers.Ci.nsICookiePermission.ACCESS_DEFAULT);
  },

  function test7() {
    SimpleTest.finish();
  }
];

SimpleTest.waitForExplicitFinish();

</script>

</head>

<body onload="startTest();">
<iframe id="testframe" src="data:text/html;charset=utf-8,"></iframe>
</body>
</html>