summaryrefslogtreecommitdiffstats
path: root/toolkit/components/alerts/test/test_invalid_utf16.html
blob: a4f862238c83d85b8b1f21eaaabcfaa83585b544 (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
<!DOCTYPE HTML>
<html>

<head>
  <meta charset="utf-8">
  <title>Test for stability when providing invalid UTF-16 strings</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css" />
  <script>
    const Cc = SpecialPowers.Cc;
    const Ci = SpecialPowers.Ci;
    let notifier = Cc["@mozilla.org/alerts-service;1"].getService(Ci.nsIAlertsService);;
    let notification = Cc["@mozilla.org/alert-notification;1"]

    function buildObserver(alertName) {
      let resolve;
      let reject;
      let promise = new Promise((res, rej) => {resolve = res; reject = rej});

      let success = false;
      function observe(aSubject, aTopic, aData) {
        if (aTopic == "alertshow") {
          success = true;
          notifier.closeAlert(alertName);
        } else if (aTopic == "alertfinished") {
          ok(true, "alertfinished");
          if (success) {
            resolve();
          } else {
            reject();
          }
        }
      }

      return { promise, observe };
    };

    function buildAlert(options) {
      let alert = notification.createInstance(
        Ci.nsIAlertNotification
      );
      alert.init(
        options.name,
        options.imageURL,
        options.title,
        options.text,
        options.textClickable,
        options.cookie,
        options.dir,
        options.lang,
        options.data,
        options.principal,
        options.inPrivateBrowsing,
        options.requireInteraction,
        options.silent,
        options.vibrate || []
      );
      if (options.actions) {
        alert.actions = options.actions;
      }
      return alert;
    }

    async function runTest(options) {
      let alert = buildAlert(options)
      const { promise, observe } = buildObserver(options.name);
      notifier.showAlert(alert, observe);
      await promise;
    }

    let invalidUtf16 = String.fromCharCode(0xdfff);

    add_task(async function test_invalid_utf16_name() {
      let name = invalidUtf16;
      let alert = buildAlert({name});

      // Extract the alert name to ensure it was not forced to be valid UTF-16.
      ok(name == alert.name, "Notification name was not forced to be valid UTF-16");

      const { promise, observe } = buildObserver(name);
      notifier.showAlert(alert, observe);
      await promise;

      ok(true, "Notification shown with invalid UTF-16 name");
    });

    add_task(async function test_invalid_utf16_title() {
      let name = "invalid title";
      let title = invalidUtf16;
      await runTest({name, title});

      ok(true, "Notification shown with invalid UTF-16 title");
    });

    add_task(async function test_invalid_utf16_body() {
      let name = "invalid body";
      let text = invalidUtf16;
      await runTest({name, text});

      ok(true, "Notification shown with invalid UTF-16 body");
    });

    add_task(async function test_invalid_utf16_image_url() {
      let name = "invalid image URL";
      let imageURL = invalidUtf16;
      await runTest({name, imageURL});

      ok(true, "Notification shown with invalid UTF-16 image url");
    });

    add_task(async function test_invalid_utf16_data() {
      let name = "invalid data";
      let data = invalidUtf16;
      await runTest({name, data});

      ok(true, "Notification shown with invalid UTF-16 data");
    });

    // At time of writing, actions are a Windows only, privileged feature.
    add_task(async function test_invalid_utf16_action_body() {
      let name = "invalid action body";
      let actions = [
        { action: invalidUtf16 },
      ];
      await runTest({name, actions});

      ok(true, "Notification shown with invalid UTF-16 action body");
    });

    // At time of writing, actions are a Windows only, privileged feature.
    add_task(async function test_invalid_utf16_action_title() {
      let name = "invalid action title";
      let actions = [
        {title:invalidUtf16},
      ];
      await runTest({name, actions});

      ok(true, "Notification shown with invalid UTF-16 action title");
    });

    // At time of writing, actions are a Windows only, privileged feature.
    add_task(async function test_invalid_utf16_action_image_url() {
      let name = "invalid action image URL";
      let actions = [
        { iconURL: invalidUtf16 },
      ];
      await runTest({name, actions});

      ok(true, "Notification shown with invalid UTF-16 action image URL");
    });
  </script>
</head>

<body>
  <p id="display"></p>
  <div id="content" style="display: none"></div>
  <pre id="test"></pre>
</body>

</html>