summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/test/browser_webconsole_autocomplete_and_selfxss.js
blob: d0c6eb6732c266dcc641c0507347abbf99a03dd5 (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
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const TEST_URI = "data:text/html;charset=utf-8,<p>test for bug 642615";

XPCOMUtils.defineLazyServiceGetter(this, "clipboardHelper",
                                   "@mozilla.org/widget/clipboardhelper;1",
                                   "nsIClipboardHelper");
var WebConsoleUtils = require("devtools/client/webconsole/utils").Utils;

add_task(function* () {
  yield loadTab(TEST_URI);

  let hud = yield openConsole();

  yield consoleOpened(hud);
});

function consoleOpened(HUD) {
  let deferred = promise.defer();

  let jsterm = HUD.jsterm;
  let stringToCopy = "foobazbarBug642615";

  jsterm.clearOutput();

  ok(!jsterm.completeNode.value, "no completeNode.value");

  jsterm.setInputValue("doc");

  let completionValue;

  // wait for key "u"
  function onCompletionValue() {
    completionValue = jsterm.completeNode.value;

    // Arguments: expected, setup, success, failure.
    waitForClipboard(
      stringToCopy,
      function () {
        clipboardHelper.copyString(stringToCopy);
      },
      onClipboardCopy,
      finishTest);
  }

  function onClipboardCopy() {
    testSelfXss();

    jsterm.setInputValue("docu");
    info("wait for completion update after clipboard paste");
    updateEditUIVisibility();
    jsterm.once("autocomplete-updated", onClipboardPaste);
    goDoCommand("cmd_paste");
  }

  // Self xss prevention tests (bug 994134)
  function testSelfXss() {
    info("Self-xss paste tests");
    WebConsoleUtils.usageCount = 0;
    is(WebConsoleUtils.usageCount, 0, "Test for usage count getter");
    // Input some commands to check if usage counting is working
    for (let i = 0; i <= 3; i++) {
      jsterm.setInputValue(i);
      jsterm.execute();
    }
    is(WebConsoleUtils.usageCount, 4, "Usage count incremented");
    WebConsoleUtils.usageCount = 0;
    updateEditUIVisibility();

    let oldVal = jsterm.getInputValue();
    goDoCommand("cmd_paste");
    let notificationbox = jsterm.hud.document.getElementById("webconsole-notificationbox");
    let notification = notificationbox.getNotificationWithValue("selfxss-notification");
    ok(notification, "Self-xss notification shown");
    is(oldVal, jsterm.getInputValue(), "Paste blocked by self-xss prevention");

    // Allow pasting
    jsterm.setInputValue("allow pasting");
    let evt = document.createEvent("KeyboardEvent");
    evt.initKeyEvent("keyup", true, true, window,
                     0, 0, 0, 0,
                     0, " ".charCodeAt(0));
    jsterm.inputNode.dispatchEvent(evt);
    jsterm.setInputValue("");
    goDoCommand("cmd_paste");
    isnot("", jsterm.getInputValue(), "Paste works");
  }
  function onClipboardPaste() {
    ok(!jsterm.completeNode.value, "no completion value after paste");

    info("wait for completion update after undo");
    jsterm.once("autocomplete-updated", onCompletionValueAfterUndo);

    // Get out of the webconsole event loop.
    executeSoon(() => {
      goDoCommand("cmd_undo");
    });
  }

  function onCompletionValueAfterUndo() {
    is(jsterm.completeNode.value, completionValue,
       "same completeNode.value after undo");

    info("wait for completion update after clipboard paste (ctrl-v)");
    jsterm.once("autocomplete-updated", () => {
      ok(!jsterm.completeNode.value,
         "no completion value after paste (ctrl-v)");

      // using executeSoon() to get out of the webconsole event loop.
      executeSoon(deferred.resolve);
    });

    // Get out of the webconsole event loop.
    executeSoon(() => {
      EventUtils.synthesizeKey("v", {accelKey: true});
    });
  }

  info("wait for completion value after typing 'docu'");
  jsterm.once("autocomplete-updated", onCompletionValue);

  EventUtils.synthesizeKey("u", {});

  return deferred.promise;
}