diff options
Diffstat (limited to 'devtools/shared/webconsole')
63 files changed, 9720 insertions, 0 deletions
diff --git a/devtools/shared/webconsole/GenerateDataFromWebIdls.py b/devtools/shared/webconsole/GenerateDataFromWebIdls.py new file mode 100644 index 0000000000..c02f747c2a --- /dev/null +++ b/devtools/shared/webconsole/GenerateDataFromWebIdls.py @@ -0,0 +1,133 @@ +""" +This script parses mozilla-central's WebIDL bindings and writes a JSON-formatted +subset of the function bindings to several files: +- "devtools/server/actors/webconsole/webidl-pure-allowlist.js" (for eager evaluation processing) +- "devtools/server/actors/webconsole/webidl-deprecated-list.js" + +Run this script via + +> ./mach python devtools/shared/webconsole/GeneratePureDOMFunctions.py + +with a mozconfig that references a built non-artifact build. +""" + +from __future__ import absolute_import, unicode_literals, print_function +from os import path, remove, system +import json +import WebIDL +import buildconfig + +# This is an explicit list of interfaces to load [Pure] and [Constant] +# annotation for. There are a bunch of things that are pure in other interfaces +# that we don't care about in the context of the devtools. +PURE_INTERFACE_ALLOWLIST = set( + [ + "Document", + "Node", + "DOMTokenList", + "Element", + "Performance", + "URLSearchParams", + "FormData", + "Headers", + ] +) + +# This is an explicit list of interfaces to exclude. +DEPRECATED_INTERFACE__EXCLUDE_LIST = set( + [ + "External", + "TestExampleInterface", + "TestInterface", + "TestJSImplInterface", + "TestingDeprecatedInterface", + ] +) + +FILE_TEMPLATE = """\ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +// This file is automatically generated by the GenerateDataFromWebIdls.py +// script. Do not modify it manually. +"use strict"; + +module.exports = %(data)s; +""" + +pure_output_file = path.join( + buildconfig.topsrcdir, "devtools/server/actors/webconsole/webidl-pure-allowlist.js" +) +deprecated_output_file = path.join( + buildconfig.topsrcdir, "devtools/server/actors/webconsole/webidl-deprecated-list.js" +) + +input_file = path.join(buildconfig.topobjdir, "dom/bindings/file-lists.json") + +if not path.isfile(input_file): + raise Exception( + "Script must be run with a mozconfig referencing a non-artifact OBJDIR" + ) + +file_list = json.load(open(input_file)) + +parser = WebIDL.Parser() +for filepath in file_list["webidls"]: + with open(filepath, "r", encoding="utf8") as f: + parser.parse(f.read(), filepath) +results = parser.finish() + +pure_output = {} +deprecated_output = {} +for result in results: + if isinstance(result, WebIDL.IDLInterface): + iface = result.identifier.name + + for member in result.members: + name = member.identifier.name + + # We only care about methods because eager evaluation assumes that + # all getter functions are side-effect-free. + if member.isMethod() and member.affects == "Nothing": + if ( + PURE_INTERFACE_ALLOWLIST and not iface in PURE_INTERFACE_ALLOWLIST + ) or name.startswith("_"): + continue + if not iface in pure_output: + pure_output[iface] = [] + if member.isStatic(): + pure_output[iface].append([name]) + else: + pure_output[iface].append(["prototype", name]) + if ( + not iface in DEPRECATED_INTERFACE__EXCLUDE_LIST + and (member.isMethod() or member.isAttr()) + and member.getExtendedAttribute("Deprecated") + ): + if not iface in deprecated_output: + deprecated_output[iface] = [] + if member.isStatic(): + deprecated_output[iface].append([name]) + else: + deprecated_output[iface].append(["prototype", name]) + +with open(pure_output_file, "w") as f: + f.write(FILE_TEMPLATE % {"data": json.dumps(pure_output, indent=2, sort_keys=True)}) +print("Successfully generated", pure_output_file) + +with open(deprecated_output_file, "w") as f: + f.write( + FILE_TEMPLATE + % {"data": json.dumps(deprecated_output, indent=2, sort_keys=True)} + ) +print("Successfully generated", deprecated_output_file) + +print("Formatting files...") +system("./mach eslint --fix " + pure_output_file + " " + deprecated_output_file) +print("Files are now properly formatted") + +# Parsing the idls generate a parser.out file that we don't have any use of. +if path.exists("parser.out"): + remove("parser.out") +print("DONE") diff --git a/devtools/shared/webconsole/GenerateReservedWordsJS.py b/devtools/shared/webconsole/GenerateReservedWordsJS.py new file mode 100644 index 0000000000..4ecc35feff --- /dev/null +++ b/devtools/shared/webconsole/GenerateReservedWordsJS.py @@ -0,0 +1,41 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +from __future__ import absolute_import +import re +import sys + + +def read_reserved_word_list(filename): + macro_pat = re.compile(r"^\s*MACRO\(([^,]+), *[^,]+, *[^\)]+\)\s*\\?$") + + reserved_word_list = [] + with open(filename, "r") as f: + for line in f: + m = macro_pat.search(line) + if m: + reserved_word_list.append(m.group(1)) + + assert len(reserved_word_list) != 0 + + return reserved_word_list + + +def line(opt, s): + opt["output"].write("{}\n".format(s)) + + +def main(output, reserved_words_h): + reserved_word_list = read_reserved_word_list(reserved_words_h) + opt = {"output": output} + + line(opt, "const JS_RESERVED_WORDS = [") + for word in reserved_word_list: + line(opt, ' "{}",'.format(word)) + line(opt, "];") + line(opt, "module.exports = JS_RESERVED_WORDS;") + + +if __name__ == "__main__": + main(sys.stdout, *sys.argv[1:]) diff --git a/devtools/shared/webconsole/js-property-provider.js b/devtools/shared/webconsole/js-property-provider.js new file mode 100644 index 0000000000..7376ea6c9b --- /dev/null +++ b/devtools/shared/webconsole/js-property-provider.js @@ -0,0 +1,1173 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +"use strict"; + +const DevToolsUtils = require("devtools/shared/DevToolsUtils"); + +const { + evalWithDebugger, +} = require("devtools/server/actors/webconsole/eval-with-debugger"); + +if (!isWorker) { + loader.lazyRequireGetter( + this, + "getSyntaxTrees", + "devtools/shared/webconsole/parser-helper", + true + ); +} +loader.lazyRequireGetter( + this, + "Reflect", + "resource://gre/modules/reflect.jsm", + true +); + +// Provide an easy way to bail out of even attempting an autocompletion +// if an object has way too many properties. Protects against large objects +// with numeric values that wouldn't be tallied towards MAX_AUTOCOMPLETIONS. +const MAX_AUTOCOMPLETE_ATTEMPTS = (exports.MAX_AUTOCOMPLETE_ATTEMPTS = 100000); +// Prevent iterating over too many properties during autocomplete suggestions. +const MAX_AUTOCOMPLETIONS = (exports.MAX_AUTOCOMPLETIONS = 1500); + +/** + * Provides a list of properties, that are possible matches based on the passed + * Debugger.Environment/Debugger.Object and inputValue. + * + * @param {Object} An object of the following shape: + * - {Object} dbgObject + * When the debugger is not paused this Debugger.Object wraps + * the scope for autocompletion. + * It is null if the debugger is paused. + * - {Object} environment + * When the debugger is paused this Debugger.Environment is the + * scope for autocompletion. + * It is null if the debugger is not paused. + * - {String} inputValue + * Value that should be completed. + * - {Number} cursor (defaults to inputValue.length). + * Optional offset in the input where the cursor is located. If this is + * omitted then the cursor is assumed to be at the end of the input + * value. + * - {Array} authorizedEvaluations (defaults to []). + * Optional array containing all the different properties access that the engine + * can execute in order to retrieve its result's properties. + * ⚠️ This should be set to true *ONLY* on user action as it may cause side-effects + * in the content page ⚠️ + * - {WebconsoleActor} webconsoleActor + * A reference to a webconsole actor which we can use to retrieve the last + * evaluation result or create a debuggee value. + * - {String}: selectedNodeActor + * The actor id of the selected node in the inspector. + * - {Array<string>}: expressionVars + * Optional array containing variable defined in the expression. Those variables + * are extracted from CodeMirror state. + * @returns null or object + * If the inputValue is an unsafe getter and invokeUnsafeGetter is false, the + * following form is returned: + * + * { + * isUnsafeGetter: true, + * getterPath: {Array<String>} An array of the property chain leading to the + * getter. Example: ["x", "myGetter"] + * } + * + * If no completion valued could be computed, and the input is not an unsafe + * getter, null is returned. + * + * Otherwise an object with the following form is returned: + * { + * matches: Set<string> + * matchProp: Last part of the inputValue that was used to find + * the matches-strings. + * isElementAccess: Boolean set to true if the evaluation is an element + * access (e.g. `window["addEvent`). + * } + */ +// eslint-disable-next-line complexity +function JSPropertyProvider({ + dbgObject, + environment, + inputValue, + cursor, + authorizedEvaluations = [], + webconsoleActor, + selectedNodeActor, + expressionVars = [], +}) { + if (cursor === undefined) { + cursor = inputValue.length; + } + + inputValue = inputValue.substring(0, cursor); + + // Analyse the inputValue and find the beginning of the last part that + // should be completed. + const inputAnalysis = analyzeInputString(inputValue); + + if (!shouldInputBeAutocompleted(inputAnalysis)) { + return null; + } + + let { + lastStatement, + isElementAccess, + mainExpression, + matchProp, + isPropertyAccess, + } = inputAnalysis; + + // Eagerly evaluate the main expression and return the results properties. + // e.g. `obj.func().a` will evaluate `obj.func()` and return properties matching `a`. + // NOTE: this is only useful when the input has a property access. + if (webconsoleActor && shouldInputBeEagerlyEvaluated(inputAnalysis)) { + const eagerResponse = evalWithDebugger( + mainExpression, + { eager: true, selectedNodeActor }, + webconsoleActor + ); + + const ret = eagerResponse?.result?.return; + + // Only send matches if eager evaluation returned something meaningful + if (ret && ret !== undefined) { + const matches = + typeof ret != "object" + ? getMatchedProps(ret, matchProp) + : getMatchedPropsInDbgObject(ret, matchProp); + + return prepareReturnedObject({ + matches, + search: matchProp, + isElementAccess, + }); + } + } + + // AST representation of the expression before the last access char (`.` or `[`). + let astExpression; + const startQuoteRegex = /^('|"|`)/; + const env = environment || dbgObject.asEnvironment(); + + // Catch literals like [1,2,3] or "foo" and return the matches from + // their prototypes. + // Don't run this is a worker, migrating to acorn should allow this + // to run in a worker - Bug 1217198. + if (!isWorker && isPropertyAccess) { + const syntaxTrees = getSyntaxTrees(mainExpression); + const lastTree = syntaxTrees[syntaxTrees.length - 1]; + const lastBody = lastTree?.body[lastTree.body.length - 1]; + + // Finding the last expression since we've sliced up until the dot. + // If there were parse errors this won't exist. + if (lastBody) { + if (!lastBody.expression) { + return null; + } + + astExpression = lastBody.expression; + let matchingObject; + + if (astExpression.type === "ArrayExpression") { + matchingObject = getContentPrototypeObject(env, "Array"); + } else if ( + astExpression.type === "Literal" && + typeof astExpression.value === "string" + ) { + matchingObject = getContentPrototypeObject(env, "String"); + } else if ( + astExpression.type === "Literal" && + Number.isFinite(astExpression.value) + ) { + // The parser rightfuly indicates that we have a number in some cases (e.g. `1.`), + // but we don't want to return Number proto properties in that case since + // the result would be invalid (i.e. `1.toFixed()` throws). + // So if the expression value is an integer, it should not end with `{Number}.` + // (but the following are fine: `1..`, `(1.).`). + if ( + !Number.isInteger(astExpression.value) || + /\d[^\.]{0}\.$/.test(lastStatement) === false + ) { + matchingObject = getContentPrototypeObject(env, "Number"); + } else { + return null; + } + } + + if (matchingObject) { + let search = matchProp; + + let elementAccessQuote; + if (isElementAccess && startQuoteRegex.test(matchProp)) { + elementAccessQuote = matchProp[0]; + search = matchProp.replace(startQuoteRegex, ""); + } + + let props = getMatchedPropsInDbgObject(matchingObject, search); + + if (isElementAccess) { + props = wrapMatchesInQuotes(props, elementAccessQuote); + } + + return { + isElementAccess, + matchProp, + matches: props, + }; + } + } + } + + // We are completing a variable / a property lookup. + let properties = []; + + if (astExpression) { + if (isPropertyAccess) { + properties = getPropertiesFromAstExpression(astExpression); + + if (properties === null) { + return null; + } + } + } else { + properties = lastStatement.split("."); + if (isElementAccess) { + const lastPart = properties[properties.length - 1]; + const openBracketIndex = lastPart.lastIndexOf("["); + matchProp = lastPart.substr(openBracketIndex + 1); + properties[properties.length - 1] = lastPart.substring( + 0, + openBracketIndex + ); + } else { + matchProp = properties.pop().trimLeft(); + } + } + + let search = matchProp; + let elementAccessQuote; + if (isElementAccess && startQuoteRegex.test(search)) { + elementAccessQuote = search[0]; + search = search.replace(startQuoteRegex, ""); + } + + let obj = dbgObject; + if (properties.length === 0) { + const environmentProperties = getMatchedPropsInEnvironment(env, search); + const expressionVariables = new Set( + expressionVars.filter(variableName => variableName.startsWith(matchProp)) + ); + + for (const prop of environmentProperties) { + expressionVariables.add(prop); + } + + return { + isElementAccess, + matchProp, + matches: expressionVariables, + }; + } + + let firstProp = properties.shift(); + if (typeof firstProp == "string") { + firstProp = firstProp.trim(); + } + + if (firstProp === "this") { + // Special case for 'this' - try to get the Object from the Environment. + // No problem if it throws, we will just not autocomplete. + try { + obj = env.object; + } catch (e) { + // Ignore. + } + } else if (firstProp === "$_" && webconsoleActor) { + obj = webconsoleActor.getLastConsoleInputEvaluation(); + } else if (firstProp === "$0" && selectedNodeActor && webconsoleActor) { + const actor = webconsoleActor.conn.getActor(selectedNodeActor); + if (actor) { + try { + obj = webconsoleActor.makeDebuggeeValue(actor.rawNode); + } catch (e) { + // Ignore. + } + } + } else if (hasArrayIndex(firstProp)) { + obj = getArrayMemberProperty(null, env, firstProp); + } else { + obj = getVariableInEnvironment(env, firstProp); + } + + if (!isObjectUsable(obj)) { + return null; + } + + // We get the rest of the properties recursively starting from the + // Debugger.Object that wraps the first property + for (let [index, prop] of properties.entries()) { + if (typeof prop === "string") { + prop = prop.trim(); + } + + if (prop === undefined || prop === null || prop === "") { + return null; + } + + const propPath = [firstProp].concat(properties.slice(0, index + 1)); + const authorized = authorizedEvaluations.some( + x => JSON.stringify(x) === JSON.stringify(propPath) + ); + + if (!authorized && DevToolsUtils.isUnsafeGetter(obj, prop)) { + // If we try to access an unsafe getter, return its name so we can consume that + // on the frontend. + return { + isUnsafeGetter: true, + getterPath: propPath, + }; + } + + if (hasArrayIndex(prop)) { + // The property to autocomplete is a member of array. For example + // list[i][j]..[n]. Traverse the array to get the actual element. + obj = getArrayMemberProperty(obj, null, prop); + } else { + obj = DevToolsUtils.getProperty(obj, prop, authorized); + } + + if (!isObjectUsable(obj)) { + return null; + } + } + + const matches = + typeof obj != "object" + ? getMatchedProps(obj, search) + : getMatchedPropsInDbgObject(obj, search); + return prepareReturnedObject({ + matches, + search, + isElementAccess, + elementAccessQuote, + }); +} + +function shouldInputBeEagerlyEvaluated({ lastStatement }) { + const inComputedProperty = + lastStatement.lastIndexOf("[") !== -1 && + lastStatement.lastIndexOf("[") > lastStatement.lastIndexOf("]"); + + const hasPropertyAccess = + lastStatement.includes(".") || lastStatement.includes("["); + + return hasPropertyAccess && !inComputedProperty; +} + +function shouldInputBeAutocompleted(inputAnalysisState) { + const { err, state, lastStatement } = inputAnalysisState; + + // There was an error analysing the string. + if (err) { + return false; + } + + // If the current state is not STATE_NORMAL, then we are inside of an string + // which means that no completion is possible. + if (state != STATE_NORMAL) { + return false; + } + + // Don't complete on just an empty string. + if (lastStatement.trim() == "") { + return false; + } + + if ( + NO_AUTOCOMPLETE_PREFIXES.some(prefix => + lastStatement.startsWith(prefix + " ") + ) + ) { + return false; + } + + return true; +} + +function hasArrayIndex(str) { + return /\[\d+\]$/.test(str); +} + +const STATE_NORMAL = Symbol("STATE_NORMAL"); +const STATE_QUOTE = Symbol("STATE_QUOTE"); +const STATE_DQUOTE = Symbol("STATE_DQUOTE"); +const STATE_TEMPLATE_LITERAL = Symbol("STATE_TEMPLATE_LITERAL"); +const STATE_ESCAPE_QUOTE = Symbol("STATE_ESCAPE_QUOTE"); +const STATE_ESCAPE_DQUOTE = Symbol("STATE_ESCAPE_DQUOTE"); +const STATE_ESCAPE_TEMPLATE_LITERAL = Symbol("STATE_ESCAPE_TEMPLATE_LITERAL"); +const STATE_SLASH = Symbol("STATE_SLASH"); +const STATE_INLINE_COMMENT = Symbol("STATE_INLINE_COMMENT"); +const STATE_MULTILINE_COMMENT = Symbol("STATE_MULTILINE_COMMENT"); +const STATE_MULTILINE_COMMENT_CLOSE = Symbol("STATE_MULTILINE_COMMENT_CLOSE"); +const STATE_QUESTION_MARK = Symbol("STATE_QUESTION_MARK"); + +const OPEN_BODY = "{[(".split(""); +const CLOSE_BODY = "}])".split(""); +const OPEN_CLOSE_BODY = { + "{": "}", + "[": "]", + "(": ")", +}; + +const NO_AUTOCOMPLETE_PREFIXES = ["var", "const", "let", "function", "class"]; +const OPERATOR_CHARS_SET = new Set(";,:=<>+-*%|&^~!".split("")); + +/** + * Analyses a given string to find the last statement that is interesting for + * later completion. + * + * @param string str + * A string to analyse. + * + * @returns object + * If there was an error in the string detected, then a object like + * + * { err: "ErrorMesssage" } + * + * is returned, otherwise a object like + * + * { + * state: STATE_NORMAL|STATE_QUOTE|STATE_DQUOTE, + * lastStatement: the last statement in the string, + * isElementAccess: boolean that indicates if the lastStatement has an open + * element access (e.g. `x["match`). + * isPropertyAccess: boolean indicating if we are accessing property + * (e.g `true` in `var a = {b: 1};a.b`) + * matchProp: The part of the expression that should match the properties + * on the mainExpression (e.g. `que` when expression is `document.body.que`) + * mainExpression: The part of the expression before any property access, + * (e.g. `a.b` if expression is `a.b.`) + * expressionBeforePropertyAccess: The part of the expression before property access + * (e.g `var a = {b: 1};a` if expression is `var a = {b: 1};a.b`) + * } + */ +// eslint-disable-next-line complexity +function analyzeInputString(str) { + // work variables. + const bodyStack = []; + let state = STATE_NORMAL; + let previousNonWhitespaceChar; + let lastStatement = ""; + let currentIndex = -1; + let dotIndex; + let pendingWhitespaceChars = ""; + const TIMEOUT = 2500; + const startingTime = Date.now(); + + // Use a string iterator in order to handle character with a length >= 2 (e.g. 😎). + for (const c of str) { + // We are possibly dealing with a very large string that would take a long time to + // analyze (and freeze the process). If the function has been running for more than + // a given time, we stop the analysis (this isn't too bad because the only + // consequence is that we won't provide autocompletion items). + if (Date.now() - startingTime > TIMEOUT) { + return { + err: "timeout", + }; + } + + currentIndex += 1; + let resetLastStatement = false; + const isWhitespaceChar = c.trim() === ""; + switch (state) { + // Normal JS state. + case STATE_NORMAL: + if (lastStatement.endsWith("?.") && /\d/.test(c)) { + // If the current char is a number, the engine will consider we're not + // performing an optional chaining, but a ternary (e.g. x ?.4 : 2). + lastStatement = ""; + } + + // Storing the index of dot of the input string + if (c === ".") { + dotIndex = currentIndex; + } + + // If the last characters were spaces, and the current one is not. + if (pendingWhitespaceChars && !isWhitespaceChar) { + // If we have a legitimate property/element access, or potential optional + // chaining call, we append the spaces. + if (c === "[" || c === "." || c === "?") { + lastStatement = lastStatement + pendingWhitespaceChars; + } else { + // if not, we can be sure the statement was over, and we can start a new one. + lastStatement = ""; + } + pendingWhitespaceChars = ""; + } + + if (c == '"') { + state = STATE_DQUOTE; + } else if (c == "'") { + state = STATE_QUOTE; + } else if (c == "`") { + state = STATE_TEMPLATE_LITERAL; + } else if (c == "/") { + state = STATE_SLASH; + } else if (c == "?") { + state = STATE_QUESTION_MARK; + } else if (OPERATOR_CHARS_SET.has(c)) { + // If the character is an operator, we can update the current statement. + resetLastStatement = true; + } else if (isWhitespaceChar) { + // If the previous char isn't a dot or opening bracket, and the current computed + // statement is not a variable/function/class declaration, we track the number + // of consecutive spaces, so we can re-use them at some point (or drop them). + if ( + previousNonWhitespaceChar !== "." && + previousNonWhitespaceChar !== "[" && + !NO_AUTOCOMPLETE_PREFIXES.includes(lastStatement) + ) { + pendingWhitespaceChars += c; + continue; + } + } else if (OPEN_BODY.includes(c)) { + // When opening a bracket or a parens, we store the current statement, in order + // to be able to retrieve it later. + bodyStack.push({ + token: c, + lastStatement, + index: currentIndex, + }); + // And we compute a new statement. + resetLastStatement = true; + } else if (CLOSE_BODY.includes(c)) { + const last = bodyStack.pop(); + if (!last || OPEN_CLOSE_BODY[last.token] != c) { + return { + err: "syntax error", + }; + } + if (c == "}") { + resetLastStatement = true; + } else { + lastStatement = last.lastStatement; + } + } + break; + + // Escaped quote + case STATE_ESCAPE_QUOTE: + state = STATE_QUOTE; + break; + case STATE_ESCAPE_DQUOTE: + state = STATE_DQUOTE; + break; + case STATE_ESCAPE_TEMPLATE_LITERAL: + state = STATE_TEMPLATE_LITERAL; + break; + + // Double quote state > " < + case STATE_DQUOTE: + if (c == "\\") { + state = STATE_ESCAPE_DQUOTE; + } else if (c == "\n") { + return { + err: "unterminated string literal", + }; + } else if (c == '"') { + state = STATE_NORMAL; + } + break; + + // Template literal state > ` < + case STATE_TEMPLATE_LITERAL: + if (c == "\\") { + state = STATE_ESCAPE_TEMPLATE_LITERAL; + } else if (c == "`") { + state = STATE_NORMAL; + } + break; + + // Single quote state > ' < + case STATE_QUOTE: + if (c == "\\") { + state = STATE_ESCAPE_QUOTE; + } else if (c == "\n") { + return { + err: "unterminated string literal", + }; + } else if (c == "'") { + state = STATE_NORMAL; + } + break; + case STATE_SLASH: + if (c == "/") { + state = STATE_INLINE_COMMENT; + } else if (c == "*") { + state = STATE_MULTILINE_COMMENT; + } else { + lastStatement = ""; + state = STATE_NORMAL; + } + break; + + case STATE_INLINE_COMMENT: + if (c === "\n") { + state = STATE_NORMAL; + resetLastStatement = true; + } + break; + + case STATE_MULTILINE_COMMENT: + if (c === "*") { + state = STATE_MULTILINE_COMMENT_CLOSE; + } + break; + + case STATE_MULTILINE_COMMENT_CLOSE: + if (c === "/") { + state = STATE_NORMAL; + resetLastStatement = true; + } else { + state = STATE_MULTILINE_COMMENT; + } + break; + + case STATE_QUESTION_MARK: + state = STATE_NORMAL; + if (c === "?") { + // If we have a nullish coalescing operator, we start a new statement + resetLastStatement = true; + } else if (c !== ".") { + // If we're not dealing with optional chaining (?.), it means we have a ternary, + // so we are starting a new statement that includes the current character. + lastStatement = ""; + } else { + dotIndex = currentIndex; + } + break; + } + + if (!isWhitespaceChar) { + previousNonWhitespaceChar = c; + } + if (resetLastStatement) { + lastStatement = ""; + } else { + lastStatement = lastStatement + c; + } + + // We update all the open stacks lastStatement so they are up-to-date. + bodyStack.forEach(stack => { + if (stack.token !== "}") { + stack.lastStatement = stack.lastStatement + c; + } + }); + } + + let isElementAccess = false; + let lastOpeningBracketIndex = -1; + if (bodyStack.length === 1 && bodyStack[0].token === "[") { + lastStatement = bodyStack[0].lastStatement; + lastOpeningBracketIndex = bodyStack[0].index; + isElementAccess = true; + + if ( + state === STATE_DQUOTE || + state === STATE_QUOTE || + state === STATE_TEMPLATE_LITERAL || + state === STATE_ESCAPE_QUOTE || + state === STATE_ESCAPE_DQUOTE || + state === STATE_ESCAPE_TEMPLATE_LITERAL + ) { + state = STATE_NORMAL; + } + } else if (pendingWhitespaceChars) { + lastStatement = ""; + } + + const lastCompletionCharIndex = isElementAccess + ? lastOpeningBracketIndex + : dotIndex; + + const stringBeforeLastCompletionChar = str.slice(0, lastCompletionCharIndex); + + const isPropertyAccess = + lastCompletionCharIndex && lastCompletionCharIndex > 0; + + // Compute `isOptionalAccess`, so that we can use it + // later for computing `expressionBeforePropertyAccess`. + //Check `?.` before `[` for element access ( e.g `a?.["b` or `a ?. ["b` ) + // and `?` before `.` for regular property access ( e.g `a?.b` or `a ?. b` ) + const optionalElementAccessRegex = /\?\.\s*$/; + const isOptionalAccess = isElementAccess + ? optionalElementAccessRegex.test(stringBeforeLastCompletionChar) + : isPropertyAccess && + str.slice(lastCompletionCharIndex - 1, lastCompletionCharIndex + 1) === + "?."; + + // Get the filtered string for the properties (e.g if `document.qu` then `qu`) + const matchProp = isPropertyAccess + ? str.slice(lastCompletionCharIndex + 1).trimLeft() + : null; + + const expressionBeforePropertyAccess = isPropertyAccess + ? str.slice( + 0, + // For optional access, we can take all the chars before the last "?" char. + isOptionalAccess + ? stringBeforeLastCompletionChar.lastIndexOf("?") + : lastCompletionCharIndex + ) + : str; + + let mainExpression = lastStatement; + if (isPropertyAccess) { + if (isOptionalAccess) { + // Strip anything before the last `?`. + mainExpression = mainExpression.slice(0, mainExpression.lastIndexOf("?")); + } else { + mainExpression = mainExpression.slice( + 0, + -1 * (str.length - lastCompletionCharIndex) + ); + } + } + + mainExpression = mainExpression.trim(); + + return { + state, + isElementAccess, + isPropertyAccess, + expressionBeforePropertyAccess, + lastStatement, + mainExpression, + matchProp, + }; +} + +/** + * For a given environment and constructor name, returns its Debugger.Object wrapped + * prototype. + * + * @param {Environment} env + * @param {String} name: Name of the constructor object we want the prototype of. + * @returns {Debugger.Object|null} the prototype, or null if it not found. + */ +function getContentPrototypeObject(env, name) { + // Retrieve the outermost environment to get the global object. + let outermostEnv = env; + while (outermostEnv?.parent) { + outermostEnv = outermostEnv.parent; + } + + const constructorObj = DevToolsUtils.getProperty(outermostEnv.object, name); + if (!constructorObj) { + return null; + } + + return DevToolsUtils.getProperty(constructorObj, "prototype"); +} + +/** + * @param {Object} ast: An AST representing a property access (e.g. `foo.bar["baz"].x`) + * @returns {Array|null} An array representing the property access + * (e.g. ["foo", "bar", "baz", "x"]). + */ +function getPropertiesFromAstExpression(ast) { + let result = []; + if (!ast) { + return result; + } + const { type, property, object, name, expression } = ast; + if (type === "ThisExpression") { + result.unshift("this"); + } else if (type === "Identifier" && name) { + result.unshift(name); + } else if (type === "OptionalExpression" && expression) { + result = (getPropertiesFromAstExpression(expression) || []).concat(result); + } else if ( + type === "MemberExpression" || + type === "OptionalMemberExpression" + ) { + if (property) { + if (property.type === "Identifier" && property.name) { + result.unshift(property.name); + } else if (property.type === "Literal") { + result.unshift(property.value); + } + } + if (object) { + result = (getPropertiesFromAstExpression(object) || []).concat(result); + } + } else { + return null; + } + return result; +} + +function wrapMatchesInQuotes(matches, quote = `"`) { + return new Set( + [...matches].map(p => { + // Escape as a double-quoted string literal + p = JSON.stringify(p); + + // We don't have to do anything more when using double quotes + if (quote == `"`) { + return p; + } + + // Remove surrounding double quotes + p = p.slice(1, -1); + + // Unescape inner double quotes (all must be escaped, so no need to count backslashes) + p = p.replace(/\\(?=")/g, ""); + + // Escape the specified quote (assuming ' or `, which are treated literally in regex) + p = p.replace(new RegExp(quote, "g"), "\\$&"); + + // Template literals treat ${ specially, escape it + if (quote == "`") { + p = p.replace(/\${/g, "\\$&"); + } + + // Surround the result with quotes + return `${quote}${p}${quote}`; + }) + ); +} + +/** + * Get the array member of obj for the given prop. For example, given + * prop='list[0][1]' the element at [0][1] of obj.list is returned. + * + * @param object obj + * The object to operate on. Should be null if env is passed. + * @param object env + * The Environment to operate in. Should be null if obj is passed. + * @param string prop + * The property to return. + * @return null or Object + * Returns null if the property couldn't be located. Otherwise the array + * member identified by prop. + */ +function getArrayMemberProperty(obj, env, prop) { + // First get the array. + const propWithoutIndices = prop.substr(0, prop.indexOf("[")); + + if (env) { + obj = getVariableInEnvironment(env, propWithoutIndices); + } else { + obj = DevToolsUtils.getProperty(obj, propWithoutIndices); + } + + if (!isObjectUsable(obj)) { + return null; + } + + // Then traverse the list of indices to get the actual element. + let result; + const arrayIndicesRegex = /\[[^\]]*\]/g; + while ((result = arrayIndicesRegex.exec(prop)) !== null) { + const indexWithBrackets = result[0]; + const indexAsText = indexWithBrackets.substr( + 1, + indexWithBrackets.length - 2 + ); + const index = parseInt(indexAsText, 10); + + if (isNaN(index)) { + return null; + } + + obj = DevToolsUtils.getProperty(obj, index); + + if (!isObjectUsable(obj)) { + return null; + } + } + + return obj; +} + +/** + * Check if the given Debugger.Object can be used for autocomplete. + * + * @param Debugger.Object object + * The Debugger.Object to check. + * @return boolean + * True if further inspection into the object is possible, or false + * otherwise. + */ +function isObjectUsable(object) { + if (object == null) { + return false; + } + + if (typeof object == "object" && object.class == "DeadObject") { + return false; + } + + return true; +} + +/** + * @see getExactMatchImpl() + */ +function getVariableInEnvironment(environment, name) { + return getExactMatchImpl(environment, name, DebuggerEnvironmentSupport); +} + +function prepareReturnedObject({ + matches, + search, + isElementAccess, + elementAccessQuote, +}) { + if (isElementAccess) { + // If it's an element access, we need to wrap properties in quotes (either the one + // the user already typed, or `"`). + + matches = wrapMatchesInQuotes(matches, elementAccessQuote); + } else if (!isWorker) { + // If we're not performing an element access, we need to check that the property + // are suited for a dot access. (Reflect.jsm is not available in worker context yet, + // see Bug 1507181). + for (const match of matches) { + try { + // In order to know if the property is suited for dot notation, we use Reflect + // to parse an expression where we try to access the property with a dot. If it + // throws, this means that we need to do an element access instead. + Reflect.parse(`({${match}: true})`); + } catch (e) { + matches.delete(match); + } + } + } + + return { isElementAccess, matchProp: search, matches }; +} + +/** + * @see getMatchedPropsImpl() + */ +function getMatchedPropsInEnvironment(environment, match) { + return getMatchedPropsImpl(environment, match, DebuggerEnvironmentSupport); +} + +/** + * @see getMatchedPropsImpl() + */ +function getMatchedPropsInDbgObject(dbgObject, match) { + return getMatchedPropsImpl(dbgObject, match, DebuggerObjectSupport); +} + +/** + * @see getMatchedPropsImpl() + */ +function getMatchedProps(obj, match) { + if (typeof obj != "object") { + obj = obj.constructor.prototype; + } + return getMatchedPropsImpl(obj, match, JSObjectSupport); +} + +/** + * Get all properties in the given object (and its parent prototype chain) that + * match a given prefix. + * + * @param {Mixed} obj + * Object whose properties we want to filter. + * @param {string} match + * Filter for properties that match this string. + * @returns {Set} List of matched properties. + */ +function getMatchedPropsImpl(obj, match, { chainIterator, getProperties }) { + const matches = new Set(); + let numProps = 0; + + const insensitiveMatching = match && match[0].toUpperCase() !== match[0]; + const propertyMatches = prop => { + return insensitiveMatching + ? prop.toLocaleLowerCase().startsWith(match.toLocaleLowerCase()) + : prop.startsWith(match); + }; + + // We need to go up the prototype chain. + const iter = chainIterator(obj); + for (obj of iter) { + const props = getProperties(obj); + if (!props) { + continue; + } + numProps += props.length; + + // If there are too many properties to event attempt autocompletion, + // or if we have already added the max number, then stop looping + // and return the partial set that has already been discovered. + if ( + numProps >= MAX_AUTOCOMPLETE_ATTEMPTS || + matches.size >= MAX_AUTOCOMPLETIONS + ) { + break; + } + + for (let i = 0; i < props.length; i++) { + const prop = props[i]; + if (!propertyMatches(prop)) { + continue; + } + + // If it is an array index, we can't take it. + // This uses a trick: converting a string to a number yields NaN if + // the operation failed, and NaN is not equal to itself. + // eslint-disable-next-line no-self-compare + if (+prop != +prop) { + matches.add(prop); + } + + if (matches.size >= MAX_AUTOCOMPLETIONS) { + break; + } + } + } + + return matches; +} + +/** + * Returns a property value based on its name from the given object, by + * recursively checking the object's prototype. + * + * @param object obj + * An object to look the property into. + * @param string name + * The property that is looked up. + * @returns object|undefined + * A Debugger.Object if the property exists in the object's prototype + * chain, undefined otherwise. + */ +function getExactMatchImpl(obj, name, { chainIterator, getProperty }) { + // We need to go up the prototype chain. + const iter = chainIterator(obj); + for (obj of iter) { + const prop = getProperty(obj, name, obj); + if (prop) { + return prop.value; + } + } + return undefined; +} + +var JSObjectSupport = { + chainIterator: function*(obj) { + while (obj) { + yield obj; + try { + obj = Object.getPrototypeOf(obj); + } catch (error) { + // The above can throw e.g. for some proxy objects. + return; + } + } + }, + + getProperties: function(obj) { + try { + return Object.getOwnPropertyNames(obj); + } catch (error) { + // The above can throw e.g. for some proxy objects. + return null; + } + }, + + getProperty: function() { + // getProperty is unsafe with raw JS objects. + throw new Error("Unimplemented!"); + }, +}; + +var DebuggerObjectSupport = { + chainIterator: function*(obj) { + while (obj) { + yield obj; + try { + obj = obj.proto; + } catch (error) { + // The above can throw e.g. for some proxy objects. + return; + } + } + }, + + getProperties: function(obj) { + try { + return obj.getOwnPropertyNames(); + } catch (error) { + // The above can throw e.g. for some proxy objects. + return null; + } + }, + + getProperty: function(obj, name, rootObj) { + // This is left unimplemented in favor to DevToolsUtils.getProperty(). + throw new Error("Unimplemented!"); + }, +}; + +var DebuggerEnvironmentSupport = { + chainIterator: function*(obj) { + while (obj) { + yield obj; + obj = obj.parent; + } + }, + + getProperties: function(obj) { + const names = obj.names(); + + // Include 'this' in results (in sorted order) + for (let i = 0; i < names.length; i++) { + if (i === names.length - 1 || names[i + 1] > "this") { + names.splice(i + 1, 0, "this"); + break; + } + } + + return names; + }, + + getProperty: function(obj, name) { + let result; + // Try/catch since name can be anything, and getVariable throws if + // it's not a valid ECMAScript identifier name + try { + // TODO: we should use getVariableDescriptor() here - bug 725815. + result = obj.getVariable(name); + } catch (e) { + // Ignore. + } + + // FIXME: Need actual UI, bug 941287. + if ( + result == null || + (typeof result == "object" && + (result.optimizedOut || result.missingArguments)) + ) { + return null; + } + return { value: result }; + }, +}; + +exports.JSPropertyProvider = DevToolsUtils.makeInfallible(JSPropertyProvider); + +// Export a version that will throw (for tests) +exports.FallibleJSPropertyProvider = JSPropertyProvider; + +// Export analyzeInputString (for tests) +exports.analyzeInputString = analyzeInputString; diff --git a/devtools/shared/webconsole/moz.build b/devtools/shared/webconsole/moz.build new file mode 100644 index 0000000000..75aff117b1 --- /dev/null +++ b/devtools/shared/webconsole/moz.build @@ -0,0 +1,33 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +if CONFIG["OS_TARGET"] != "Android": + MOCHITEST_CHROME_MANIFESTS += ["test/chrome/chrome.ini"] + XPCSHELL_TESTS_MANIFESTS += ["test/xpcshell/xpcshell.ini"] + BROWSER_CHROME_MANIFESTS += ["test/browser/browser.ini"] + +# Compute where to put transpiled files into omni.ja package +# All DevTools modules are used via resource://devtools/ URI +# See devtools/shared/jar.mn for how this resource is mapped into jar package. +base = FINAL_TARGET_FILES.chrome.devtools.modules + +# Now, navigate to the right sub-directory into devtools root modules folder +for dir in RELATIVEDIR.split("/"): + base = base[dir] +base += ["!reserved-js-words.js"] + +GeneratedFile( + "reserved-js-words.js", + script="GenerateReservedWordsJS.py", + inputs=["/js/src/frontend/ReservedWords.h"], +) + +DevToolsModules( + "js-property-provider.js", + "network-helper.js", + "parser-helper.js", + "throttle.js", +) diff --git a/devtools/shared/webconsole/network-helper.js b/devtools/shared/webconsole/network-helper.js new file mode 100644 index 0000000000..0f1a66975a --- /dev/null +++ b/devtools/shared/webconsole/network-helper.js @@ -0,0 +1,857 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/* + * Software License Agreement (BSD License) + * + * Copyright (c) 2007, Parakey Inc. + * All rights reserved. + * + * Redistribution and use of this software in source and binary forms, + * with or without modification, are permitted provided that the + * following conditions are met: + * + * * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * * Neither the name of Parakey Inc. nor the names of its + * contributors may be used to endorse or promote products + * derived from this software without specific prior + * written permission of Parakey Inc. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * Creator: + * Joe Hewitt + * Contributors + * John J. Barton (IBM Almaden) + * Jan Odvarko (Mozilla Corp.) + * Max Stepanov (Aptana Inc.) + * Rob Campbell (Mozilla Corp.) + * Hans Hillen (Paciello Group, Mozilla) + * Curtis Bartley (Mozilla Corp.) + * Mike Collins (IBM Almaden) + * Kevin Decker + * Mike Ratcliffe (Comartis AG) + * Hernan Rodríguez Colmeiro + * Austin Andrews + * Christoph Dorn + * Steven Roussey (AppCenter Inc, Network54) + * Mihai Sucan (Mozilla Corp.) + */ + +"use strict"; + +const { components, Cc, Ci } = require("chrome"); +loader.lazyImporter(this, "NetUtil", "resource://gre/modules/NetUtil.jsm"); +const DevToolsUtils = require("devtools/shared/DevToolsUtils"); +const Services = require("Services"); + +// The cache used in the `nsIURL` function. +const gNSURLStore = new Map(); + +// "Lax", "Strict" and "None" are special values of the SameSite cookie +// attribute that should not be translated. +const COOKIE_SAMESITE = { + LAX: "Lax", + STRICT: "Strict", + NONE: "None", +}; + +/** + * Helper object for networking stuff. + * + * Most of the following functions have been taken from the Firebug source. They + * have been modified to match the Firefox coding rules. + */ +var NetworkHelper = { + /** + * Converts text with a given charset to unicode. + * + * @param string text + * Text to convert. + * @param string charset + * Charset to convert the text to. + * @returns string + * Converted text. + */ + convertToUnicode: function(text, charset) { + const conv = Cc[ + "@mozilla.org/intl/scriptableunicodeconverter" + ].createInstance(Ci.nsIScriptableUnicodeConverter); + try { + conv.charset = charset || "UTF-8"; + return conv.ConvertToUnicode(text); + } catch (ex) { + return text; + } + }, + + /** + * Reads all available bytes from stream and converts them to charset. + * + * @param nsIInputStream stream + * @param string charset + * @returns string + * UTF-16 encoded string based on the content of stream and charset. + */ + readAndConvertFromStream: function(stream, charset) { + let text = null; + try { + text = NetUtil.readInputStreamToString(stream, stream.available()); + return this.convertToUnicode(text, charset); + } catch (err) { + return text; + } + }, + + /** + * Reads the posted text from request. + * + * @param nsIHttpChannel request + * @param string charset + * The content document charset, used when reading the POSTed data. + * @returns string or null + * Returns the posted string if it was possible to read from request + * otherwise null. + */ + readPostTextFromRequest: function(request, charset) { + if (request instanceof Ci.nsIUploadChannel) { + const iStream = request.uploadStream; + + let isSeekableStream = false; + if (iStream instanceof Ci.nsISeekableStream) { + isSeekableStream = true; + } + + let prevOffset; + if (isSeekableStream) { + prevOffset = iStream.tell(); + iStream.seek(Ci.nsISeekableStream.NS_SEEK_SET, 0); + } + + // Read data from the stream. + const text = this.readAndConvertFromStream(iStream, charset); + + // Seek locks the file, so seek to the beginning only if necko hasn't + // read it yet, since necko doesn't seek to 0 before reading (at lest + // not till 459384 is fixed). + if (isSeekableStream && prevOffset == 0) { + iStream.seek(Ci.nsISeekableStream.NS_SEEK_SET, 0); + } + return text; + } + return null; + }, + + /** + * Reads the posted text from the page's cache. + * + * @param nsIDocShell docShell + * @param string charset + * @returns string or null + * Returns the posted string if it was possible to read from + * docShell otherwise null. + */ + readPostTextFromPage: function(docShell, charset) { + const webNav = docShell.QueryInterface(Ci.nsIWebNavigation); + return this.readPostTextFromPageViaWebNav(webNav, charset); + }, + + /** + * Reads the posted text from the page's cache, given an nsIWebNavigation + * object. + * + * @param nsIWebNavigation webNav + * @param string charset + * @returns string or null + * Returns the posted string if it was possible to read from + * webNav, otherwise null. + */ + readPostTextFromPageViaWebNav: function(webNav, charset) { + if (webNav instanceof Ci.nsIWebPageDescriptor) { + const descriptor = webNav.currentDescriptor; + + if ( + descriptor instanceof Ci.nsISHEntry && + descriptor.postData && + descriptor instanceof Ci.nsISeekableStream + ) { + descriptor.seek(Ci.nsISeekableStream.NS_SEEK_SET, 0); + + return this.readAndConvertFromStream(descriptor, charset); + } + } + return null; + }, + + /** + * Gets the topFrameElement that is associated with request. This + * works in single-process and multiprocess contexts. It may cross + * the content/chrome boundary. + * + * @param nsIHttpChannel request + * @returns Element|null + * The top frame element for the given request. + */ + getTopFrameForRequest: function(request) { + try { + return this.getRequestLoadContext(request).topFrameElement; + } catch (ex) { + // request loadContent is not always available. + } + return null; + }, + + /** + * Gets the nsIDOMWindow that is associated with request. + * + * @param nsIHttpChannel request + * @returns nsIDOMWindow or null + */ + getWindowForRequest: function(request) { + try { + return this.getRequestLoadContext(request).associatedWindow; + } catch (ex) { + // TODO: bug 802246 - getWindowForRequest() throws on b2g: there is no + // associatedWindow property. + } + return null; + }, + + /** + * Gets the nsILoadContext that is associated with request. + * + * @param nsIHttpChannel request + * @returns nsILoadContext or null + */ + getRequestLoadContext: function(request) { + try { + return request.notificationCallbacks.getInterface(Ci.nsILoadContext); + } catch (ex) { + // Ignore. + } + + try { + return request.loadGroup.notificationCallbacks.getInterface( + Ci.nsILoadContext + ); + } catch (ex) { + // Ignore. + } + + return null; + }, + + /** + * Determines whether the request has been made for the top level document. + * + * @param nsIHttpChannel request + * @returns Boolean True if the request represents the top level document. + */ + isTopLevelLoad: function(request) { + if (request instanceof Ci.nsIChannel) { + const loadInfo = request.loadInfo; + if (loadInfo?.isTopLevelLoad) { + return request.loadFlags & Ci.nsIChannel.LOAD_DOCUMENT_URI; + } + } + + return false; + }, + + /** + * Loads the content of url from the cache. + * + * @param string url + * URL to load the cached content for. + * @param string charset + * Assumed charset of the cached content. Used if there is no charset + * on the channel directly. + * @param function callback + * Callback that is called with the loaded cached content if available + * or null if something failed while getting the cached content. + */ + loadFromCache: function(url, charset, callback) { + const channel = NetUtil.newChannel({ + uri: url, + loadUsingSystemPrincipal: true, + }); + + // Ensure that we only read from the cache and not the server. + channel.loadFlags = + Ci.nsIRequest.LOAD_FROM_CACHE | + Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE | + Ci.nsICachingChannel.LOAD_BYPASS_LOCAL_CACHE_IF_BUSY; + + NetUtil.asyncFetch(channel, (inputStream, statusCode, request) => { + if (!components.isSuccessCode(statusCode)) { + callback(null); + return; + } + + // Try to get the encoding from the channel. If there is none, then use + // the passed assumed charset. + const requestChannel = request.QueryInterface(Ci.nsIChannel); + const contentCharset = requestChannel.contentCharset || charset; + + // Read the content of the stream using contentCharset as encoding. + callback(this.readAndConvertFromStream(inputStream, contentCharset)); + }); + }, + + /** + * Parse a raw Cookie header value. + * + * @param string header + * The raw Cookie header value. + * @return array + * Array holding an object for each cookie. Each object holds the + * following properties: name and value. + */ + parseCookieHeader: function(header) { + const cookies = header.split(";"); + const result = []; + + cookies.forEach(function(cookie) { + const equal = cookie.indexOf("="); + const name = cookie.substr(0, equal); + const value = cookie.substr(equal + 1); + result.push({ + name: unescape(name.trim()), + value: unescape(value.trim()), + }); + }); + + return result; + }, + + /** + * Parse a raw Set-Cookie header value. + * + * @param string header + * The raw Set-Cookie header value. + * @return array + * Array holding an object for each cookie. Each object holds the + * following properties: name, value, secure (boolean), httpOnly + * (boolean), path, domain, samesite and expires (ISO date string). + */ + parseSetCookieHeader: function(header) { + function parseSameSiteAttribute(attribute) { + attribute = attribute.toLowerCase(); + switch (attribute) { + case COOKIE_SAMESITE.LAX.toLowerCase(): + return COOKIE_SAMESITE.LAX; + case COOKIE_SAMESITE.STRICT.toLowerCase(): + return COOKIE_SAMESITE.STRICT; + default: + return COOKIE_SAMESITE.NONE; + } + } + + const rawCookies = header.split(/\r\n|\n|\r/); + const cookies = []; + + rawCookies.forEach(function(cookie) { + const equal = cookie.indexOf("="); + const name = unescape(cookie.substr(0, equal).trim()); + const parts = cookie.substr(equal + 1).split(";"); + const value = unescape(parts.shift().trim()); + + cookie = { name: name, value: value }; + + parts.forEach(function(part) { + part = part.trim(); + if (part.toLowerCase() == "secure") { + cookie.secure = true; + } else if (part.toLowerCase() == "httponly") { + cookie.httpOnly = true; + } else if (part.indexOf("=") > -1) { + const pair = part.split("="); + pair[0] = pair[0].toLowerCase(); + if (pair[0] == "path" || pair[0] == "domain") { + cookie[pair[0]] = pair[1]; + } else if (pair[0] == "samesite") { + cookie[pair[0]] = parseSameSiteAttribute(pair[1]); + } else if (pair[0] == "expires") { + try { + pair[1] = pair[1].replace(/-/g, " "); + cookie.expires = new Date(pair[1]).toISOString(); + } catch (ex) { + // Ignore. + } + } + } + }); + + cookies.push(cookie); + }); + + return cookies; + }, + + // This is a list of all the mime category maps jviereck could find in the + // firebug code base. + mimeCategoryMap: { + "text/plain": "txt", + "text/html": "html", + "text/xml": "xml", + "text/xsl": "txt", + "text/xul": "txt", + "text/css": "css", + "text/sgml": "txt", + "text/rtf": "txt", + "text/x-setext": "txt", + "text/richtext": "txt", + "text/javascript": "js", + "text/jscript": "txt", + "text/tab-separated-values": "txt", + "text/rdf": "txt", + "text/xif": "txt", + "text/ecmascript": "js", + "text/vnd.curl": "txt", + "text/x-json": "json", + "text/x-js": "txt", + "text/js": "txt", + "text/vbscript": "txt", + "view-source": "txt", + "view-fragment": "txt", + "application/xml": "xml", + "application/xhtml+xml": "xml", + "application/atom+xml": "xml", + "application/rss+xml": "xml", + "application/vnd.mozilla.maybe.feed": "xml", + "application/javascript": "js", + "application/x-javascript": "js", + "application/x-httpd-php": "txt", + "application/rdf+xml": "xml", + "application/ecmascript": "js", + "application/http-index-format": "txt", + "application/json": "json", + "application/x-js": "txt", + "application/x-mpegurl": "txt", + "application/vnd.apple.mpegurl": "txt", + "multipart/mixed": "txt", + "multipart/x-mixed-replace": "txt", + "image/svg+xml": "svg", + "application/octet-stream": "bin", + "image/jpeg": "image", + "image/jpg": "image", + "image/gif": "image", + "image/png": "image", + "image/bmp": "image", + "application/x-shockwave-flash": "flash", + "video/x-flv": "flash", + "audio/mpeg3": "media", + "audio/x-mpeg-3": "media", + "video/mpeg": "media", + "video/x-mpeg": "media", + "video/vnd.mpeg.dash.mpd": "xml", + "audio/ogg": "media", + "application/ogg": "media", + "application/x-ogg": "media", + "application/x-midi": "media", + "audio/midi": "media", + "audio/x-mid": "media", + "audio/x-midi": "media", + "music/crescendo": "media", + "audio/wav": "media", + "audio/x-wav": "media", + "text/json": "json", + "application/x-json": "json", + "application/json-rpc": "json", + "application/x-web-app-manifest+json": "json", + "application/manifest+json": "json", + }, + + /** + * Check if the given MIME type is a text-only MIME type. + * + * @param string mimeType + * @return boolean + */ + isTextMimeType: function(mimeType) { + if (mimeType.indexOf("text/") == 0) { + return true; + } + + // XML and JSON often come with custom MIME types, so in addition to the + // standard "application/xml" and "application/json", we also look for + // variants like "application/x-bigcorp+xml". For JSON we allow "+json" and + // "-json" as suffixes. + if (/^application\/\w+(?:[\.-]\w+)*(?:\+xml|[-+]json)$/.test(mimeType)) { + return true; + } + + const category = this.mimeCategoryMap[mimeType] || null; + switch (category) { + case "txt": + case "js": + case "json": + case "css": + case "html": + case "svg": + case "xml": + return true; + + default: + return false; + } + }, + + /** + * Takes a securityInfo object of nsIRequest, the nsIRequest itself and + * extracts security information from them. + * + * @param object securityInfo + * The securityInfo object of a request. If null channel is assumed + * to be insecure. + * @param object httpActivity + * The httpActivity object for the request with at least members + * { private, hostname }. + * + * @return object + * Returns an object containing following members: + * - state: The security of the connection used to fetch this + * request. Has one of following string values: + * * "insecure": the connection was not secure (only http) + * * "weak": the connection has minor security issues + * * "broken": secure connection failed (e.g. expired cert) + * * "secure": the connection was properly secured. + * If state == broken: + * - errorMessage: error code string. + * If state == secure: + * - protocolVersion: one of TLSv1, TLSv1.1, TLSv1.2, TLSv1.3. + * - cipherSuite: the cipher suite used in this connection. + * - cert: information about certificate used in this connection. + * See parseCertificateInfo for the contents. + * - hsts: true if host uses Strict Transport Security, + * false otherwise + * - hpkp: true if host uses Public Key Pinning, false otherwise + * If state == weak: Same as state == secure and + * - weaknessReasons: list of reasons that cause the request to be + * considered weak. See getReasonsForWeakness. + */ + parseSecurityInfo: function(securityInfo, httpActivity) { + const info = { + state: "insecure", + }; + + // The request did not contain any security info. + if (!securityInfo) { + return info; + } + + /** + * Different scenarios to consider here and how they are handled: + * - request is HTTP, the connection is not secure + * => securityInfo is null + * => state === "insecure" + * + * - request is HTTPS, the connection is secure + * => .securityState has STATE_IS_SECURE flag + * => state === "secure" + * + * - request is HTTPS, the connection has security issues + * => .securityState has STATE_IS_INSECURE flag + * => .errorCode is an NSS error code. + * => state === "broken" + * + * - request is HTTPS, the connection was terminated before the security + * could be validated + * => .securityState has STATE_IS_INSECURE flag + * => .errorCode is NOT an NSS error code. + * => .errorMessage is not available. + * => state === "insecure" + * + * - request is HTTPS but it uses a weak cipher or old protocol, see + * https://hg.mozilla.org/mozilla-central/annotate/def6ed9d1c1a/ + * security/manager/ssl/nsNSSCallbacks.cpp#l1233 + * - request is mixed content (which makes no sense whatsoever) + * => .securityState has STATE_IS_BROKEN flag + * => .errorCode is NOT an NSS error code + * => .errorMessage is not available + * => state === "weak" + */ + + const wpl = Ci.nsIWebProgressListener; + const NSSErrorsService = Cc["@mozilla.org/nss_errors_service;1"].getService( + Ci.nsINSSErrorsService + ); + if (!NSSErrorsService.isNSSErrorCode(securityInfo.errorCode)) { + const state = securityInfo.securityState; + + let uri = null; + if (httpActivity.channel?.URI) { + uri = httpActivity.channel.URI; + } + if (uri && !uri.schemeIs("https") && !uri.schemeIs("wss")) { + // it is not enough to look at the transport security info - + // schemes other than https and wss are subject to + // downgrade/etc at the scheme level and should always be + // considered insecure + info.state = "insecure"; + } else if (state & wpl.STATE_IS_SECURE) { + // The connection is secure if the scheme is sufficient + info.state = "secure"; + } else if (state & wpl.STATE_IS_BROKEN) { + // The connection is not secure, there was no error but there's some + // minor security issues. + info.state = "weak"; + info.weaknessReasons = this.getReasonsForWeakness(state); + } else if (state & wpl.STATE_IS_INSECURE) { + // This was most likely an https request that was aborted before + // validation. Return info as info.state = insecure. + return info; + } else { + DevToolsUtils.reportException( + "NetworkHelper.parseSecurityInfo", + "Security state " + state + " has no known STATE_IS_* flags." + ); + return info; + } + + // Cipher suite. + info.cipherSuite = securityInfo.cipherName; + + // Key exchange group name. + info.keaGroupName = securityInfo.keaGroupName; + + // Certificate signature scheme. + info.signatureSchemeName = securityInfo.signatureSchemeName; + + // Protocol version. + info.protocolVersion = this.formatSecurityProtocol( + securityInfo.protocolVersion + ); + + // Certificate. + info.cert = this.parseCertificateInfo(securityInfo.serverCert); + + // Certificate transparency status. + info.certificateTransparency = securityInfo.certificateTransparencyStatus; + + // HSTS and HPKP if available. + if (httpActivity.hostname) { + const sss = Cc["@mozilla.org/ssservice;1"].getService( + Ci.nsISiteSecurityService + ); + + // SiteSecurityService uses different storage if the channel is + // private. Thus we must give isSecureURI correct flags or we + // might get incorrect results. + const flags = httpActivity.private + ? Ci.nsISocketProvider.NO_PERMANENT_STORAGE + : 0; + + if (!uri) { + // isSecureURI only cares about the host, not the scheme. + const host = httpActivity.hostname; + uri = Services.io.newURI("https://" + host); + } + + info.hsts = sss.isSecureURI(sss.HEADER_HSTS, uri, flags); + info.hpkp = sss.isSecureURI(sss.STATIC_PINNING, uri, flags); + } else { + DevToolsUtils.reportException( + "NetworkHelper.parseSecurityInfo", + "Could not get HSTS/HPKP status as hostname is not available." + ); + info.hsts = false; + info.hpkp = false; + } + } else { + // The connection failed. + info.state = "broken"; + info.errorMessage = securityInfo.errorCodeString; + } + + return info; + }, + + /** + * Takes an nsIX509Cert and returns an object with certificate information. + * + * @param nsIX509Cert cert + * The certificate to extract the information from. + * @return object + * An object with following format: + * { + * subject: { commonName, organization, organizationalUnit }, + * issuer: { commonName, organization, organizationUnit }, + * validity: { start, end }, + * fingerprint: { sha1, sha256 } + * } + */ + parseCertificateInfo: function(cert) { + const info = {}; + if (cert) { + info.subject = { + commonName: cert.commonName, + organization: cert.organization, + organizationalUnit: cert.organizationalUnit, + }; + + info.issuer = { + commonName: cert.issuerCommonName, + organization: cert.issuerOrganization, + organizationUnit: cert.issuerOrganizationUnit, + }; + + info.validity = { + start: cert.validity.notBeforeLocalDay, + end: cert.validity.notAfterLocalDay, + }; + + info.fingerprint = { + sha1: cert.sha1Fingerprint, + sha256: cert.sha256Fingerprint, + }; + } else { + DevToolsUtils.reportException( + "NetworkHelper.parseCertificateInfo", + "Secure connection established without certificate." + ); + } + + return info; + }, + + /** + * Takes protocolVersion of TransportSecurityInfo object and returns + * human readable description. + * + * @param Number version + * One of nsITransportSecurityInfo version constants. + * @return string + * One of TLSv1, TLSv1.1, TLSv1.2, TLSv1.3 if @param version + * is valid, Unknown otherwise. + */ + formatSecurityProtocol: function(version) { + switch (version) { + case Ci.nsITransportSecurityInfo.TLS_VERSION_1: + return "TLSv1"; + case Ci.nsITransportSecurityInfo.TLS_VERSION_1_1: + return "TLSv1.1"; + case Ci.nsITransportSecurityInfo.TLS_VERSION_1_2: + return "TLSv1.2"; + case Ci.nsITransportSecurityInfo.TLS_VERSION_1_3: + return "TLSv1.3"; + default: + DevToolsUtils.reportException( + "NetworkHelper.formatSecurityProtocol", + "protocolVersion " + version + " is unknown." + ); + return "Unknown"; + } + }, + + /** + * Takes the securityState bitfield and returns reasons for weak connection + * as an array of strings. + * + * @param Number state + * nsITransportSecurityInfo.securityState. + * + * @return Array[String] + * List of weakness reasons. A subset of { cipher } where + * * cipher: The cipher suite is consireded to be weak (RC4). + */ + getReasonsForWeakness: function(state) { + const wpl = Ci.nsIWebProgressListener; + + // If there's non-fatal security issues the request has STATE_IS_BROKEN + // flag set. See https://hg.mozilla.org/mozilla-central/file/44344099d119 + // /security/manager/ssl/nsNSSCallbacks.cpp#l1233 + const reasons = []; + + if (state & wpl.STATE_IS_BROKEN) { + const isCipher = state & wpl.STATE_USES_WEAK_CRYPTO; + + if (isCipher) { + reasons.push("cipher"); + } + + if (!isCipher) { + DevToolsUtils.reportException( + "NetworkHelper.getReasonsForWeakness", + "STATE_IS_BROKEN without a known reason. Full state was: " + state + ); + } + } + + return reasons; + }, + + /** + * Parse a url's query string into its components + * + * @param string queryString + * The query part of a url + * @return array + * Array of query params {name, value} + */ + parseQueryString: function(queryString) { + // Make sure there's at least one param available. + // Be careful here, params don't necessarily need to have values, so + // no need to verify the existence of a "=". + if (!queryString) { + return null; + } + + // Turn the params string into an array containing { name: value } tuples. + const paramsArray = queryString + .replace(/^[?&]/, "") + .split("&") + .map(e => { + const param = e.split("="); + return { + name: param[0] + ? NetworkHelper.convertToUnicode(unescape(param[0])) + : "", + value: param[1] + ? NetworkHelper.convertToUnicode(unescape(param[1])) + : "", + }; + }); + + return paramsArray; + }, + + /** + * Helper for getting an nsIURL instance out of a string. + */ + nsIURL: function(url, store = gNSURLStore) { + if (store.has(url)) { + return store.get(url); + } + + const uri = Services.io.newURI(url).QueryInterface(Ci.nsIURL); + store.set(url, uri); + return uri; + }, +}; + +for (const prop of Object.getOwnPropertyNames(NetworkHelper)) { + exports[prop] = NetworkHelper[prop]; +} diff --git a/devtools/shared/webconsole/parser-helper.js b/devtools/shared/webconsole/parser-helper.js new file mode 100644 index 0000000000..c3a02fea96 --- /dev/null +++ b/devtools/shared/webconsole/parser-helper.js @@ -0,0 +1,68 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +"use strict"; + +const DevToolsUtils = require("devtools/shared/DevToolsUtils"); +loader.lazyRequireGetter( + this, + "Reflect", + "resource://gre/modules/reflect.jsm", + true +); + +/** + * Gets a collection of parser methods for a specified source. + * + * @param string source + * The source text content. + * @param boolean logExceptions + */ +function getSyntaxTrees(source, logExceptions) { + // The source may not necessarily be JS, in which case we need to extract + // all the scripts. Fastest/easiest way is with a regular expression. + // Don't worry, the rules of using a <script> tag are really strict, + // this will work. + const regexp = /<script[^>]*?(?:>([^]*?)<\/script\s*>|\/>)/gim; + const syntaxTrees = []; + const scriptMatches = []; + let scriptMatch; + + if (source.match(/^\s*</)) { + // First non whitespace character is <, so most definitely HTML. + while ((scriptMatch = regexp.exec(source))) { + // Contents are captured at index 1 or nothing: Self-closing scripts + // won't capture code content + scriptMatches.push(scriptMatch[1] || ""); + } + } + + // If there are no script matches, send the whole source directly to the + // reflection API to generate the AST nodes. + if (!scriptMatches.length) { + // Reflect.parse throws when encounters a syntax error. + try { + syntaxTrees.push(Reflect.parse(source)); + } catch (e) { + if (logExceptions) { + DevToolsUtils.reportException("Parser:get", e); + } + } + } else { + // Generate the AST nodes for each script. + for (const script of scriptMatches) { + // Reflect.parse throws when encounters a syntax error. + try { + syntaxTrees.push(Reflect.parse(script)); + } catch (e) { + if (logExceptions) { + DevToolsUtils.reportException("Parser:get", e); + } + } + } + } + + return syntaxTrees; +} + +exports.getSyntaxTrees = getSyntaxTrees; diff --git a/devtools/shared/webconsole/test/browser/.eslintrc.js b/devtools/shared/webconsole/test/browser/.eslintrc.js new file mode 100644 index 0000000000..2eba290f7d --- /dev/null +++ b/devtools/shared/webconsole/test/browser/.eslintrc.js @@ -0,0 +1,6 @@ +"use strict"; + +module.exports = { + // Extend from the shared list of defined globals for mochitests. + extends: "../../../../.eslintrc.mochitests.js", +}; diff --git a/devtools/shared/webconsole/test/browser/browser.ini b/devtools/shared/webconsole/test/browser/browser.ini new file mode 100644 index 0000000000..1b66699410 --- /dev/null +++ b/devtools/shared/webconsole/test/browser/browser.ini @@ -0,0 +1,13 @@ +[DEFAULT] +tags = devtools +subsuite = devtools +support-files = + head.js + data.json + data.json^headers^ + network_requests_iframe.html + !/devtools/client/shared/test/shared-head.js + !/devtools/client/shared/test/telemetry-test-helpers.js + +[browser_commands_registration.js] +[browser_network_longstring.js] diff --git a/devtools/shared/webconsole/test/browser/browser_commands_registration.js b/devtools/shared/webconsole/test/browser/browser_commands_registration.js new file mode 100644 index 0000000000..c8d410f8d7 --- /dev/null +++ b/devtools/shared/webconsole/test/browser/browser_commands_registration.js @@ -0,0 +1,199 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +// Test for Web Console commands registration. + +add_task(async function() { + const tab = await addTab("data:text/html,<div id=quack></div>"); + const target = await getTargetForTab(tab); + + const webConsoleFront = await target.getFront("console"); + + // Fetch WebConsoleCommands so that it is available for next Content Tasks + await ContentTask.spawn(gBrowser.selectedBrowser, null, function() { + const { require } = ChromeUtils.import( + "resource://devtools/shared/Loader.jsm" + ); + const { + WebConsoleCommands, + } = require("devtools/server/actors/webconsole/utils"); + + // Bind the symbol on this in order to make it available for next tasks + this.WebConsoleCommands = WebConsoleCommands; + }); + + await registerNewCommand(webConsoleFront); + await wrapCommand(webConsoleFront); + await unregisterCommand(webConsoleFront); + await registerAccessor(webConsoleFront); + await unregisterAfterOverridingTwice(webConsoleFront); +}); + +async function evaluateJSAndCheckResult(webConsoleFront, input, expected) { + const response = await webConsoleFront.evaluateJSAsync(input); + checkObject(response, expected); +} + +async function registerNewCommand(webConsoleFront) { + await ContentTask.spawn(gBrowser.selectedBrowser, null, function() { + this.WebConsoleCommands.register("setFoo", (owner, value) => { + owner.window.foo = value; + return "ok"; + }); + + ok( + this.WebConsoleCommands.hasCommand("setFoo"), + "The command should be registered" + ); + }); + + const command = "setFoo('bar')"; + await evaluateJSAndCheckResult(webConsoleFront, command, { + input: command, + result: "ok", + }); + + await ContentTask.spawn(gBrowser.selectedBrowser, null, function() { + is(content.top.foo, "bar", "top.foo should equal to 'bar'"); + }); +} + +async function wrapCommand(webConsoleFront) { + await ContentTask.spawn(gBrowser.selectedBrowser, null, function() { + const origKeys = this.WebConsoleCommands.getCommand("keys"); + + const newKeys = (...args) => { + const [, arg0] = args; + if (arg0 === ">o_/") { + return "bang!"; + } + return origKeys(...args); + }; + + this.WebConsoleCommands.register("keys", newKeys); + is( + this.WebConsoleCommands.getCommand("keys"), + newKeys, + "the keys() command should have been replaced" + ); + + this.origKeys = origKeys; + }); + + await evaluateJSAndCheckResult(webConsoleFront, "keys('>o_/')", { + result: "bang!", + }); + + await evaluateJSAndCheckResult(webConsoleFront, "keys({foo: 'bar'})", { + result: { + _grip: { + class: "Array", + preview: { + items: ["foo"], + }, + }, + }, + }); + + await ContentTask.spawn(gBrowser.selectedBrowser, null, function() { + this.WebConsoleCommands.register("keys", this.origKeys); + is( + this.WebConsoleCommands.getCommand("keys"), + this.origKeys, + "the keys() command should be restored" + ); + delete this.origKeys; + }); +} + +async function unregisterCommand(webConsoleFront) { + await ContentTask.spawn(gBrowser.selectedBrowser, null, function() { + this.WebConsoleCommands.unregister("setFoo"); + }); + + await evaluateJSAndCheckResult(webConsoleFront, "setFoo", { + input: "setFoo", + result: { + type: "undefined", + }, + exceptionMessage: /setFoo is not defined/, + }); +} + +async function registerAccessor(webConsoleFront) { + await ContentTask.spawn(gBrowser.selectedBrowser, null, function() { + this.WebConsoleCommands.register("$foo", { + get(owner) { + const foo = owner.window.document.getElementById("quack"); + return owner.makeDebuggeeValue(foo); + }, + }); + }); + + const command = "$foo.textContent = '>o_/'"; + await evaluateJSAndCheckResult(webConsoleFront, command, { + input: command, + result: ">o_/", + }); + + await ContentTask.spawn(gBrowser.selectedBrowser, null, function() { + is( + content.document.getElementById("quack").textContent, + ">o_/", + '#foo textContent should equal to ">o_/"' + ); + this.WebConsoleCommands.unregister("$foo"); + ok( + !this.WebConsoleCommands.hasCommand("$foo"), + "$foo should be unregistered" + ); + }); +} + +async function unregisterAfterOverridingTwice(webConsoleFront) { + await ContentTask.spawn(gBrowser.selectedBrowser, null, function() { + this.WebConsoleCommands.register("keys", (owner, obj) => "command 1"); + }); + + info("checking the value of the first override"); + await evaluateJSAndCheckResult(webConsoleFront, "keys('foo');", { + result: "command 1", + }); + + await ContentTask.spawn(gBrowser.selectedBrowser, null, function() { + const orig = this.WebConsoleCommands.getCommand("keys"); + this.WebConsoleCommands.register("keys", (owner, obj) => { + if (obj === "quack") { + return "bang!"; + } + return orig(owner, obj); + }); + }); + + info("checking the values after the second override"); + await evaluateJSAndCheckResult(webConsoleFront, "keys({});", { + result: "command 1", + }); + await evaluateJSAndCheckResult(webConsoleFront, "keys('quack');", { + result: "bang!", + }); + + await ContentTask.spawn(gBrowser.selectedBrowser, null, function() { + this.WebConsoleCommands.unregister("keys"); + }); + + info( + "checking the value after unregistration (should restore " + + "the original command)" + ); + await evaluateJSAndCheckResult(webConsoleFront, "keys({});", { + result: { + _grip: { + class: "Array", + preview: { items: [] }, + }, + }, + }); +} diff --git a/devtools/shared/webconsole/test/browser/browser_network_longstring.js b/devtools/shared/webconsole/test/browser/browser_network_longstring.js new file mode 100644 index 0000000000..84eb9e07e1 --- /dev/null +++ b/devtools/shared/webconsole/test/browser/browser_network_longstring.js @@ -0,0 +1,269 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +// Test that the network actor uses the LongStringActor + +const { DevToolsServer } = require("devtools/server/devtools-server"); +const LONG_STRING_LENGTH = 400; +const LONG_STRING_INITIAL_LENGTH = 400; +let ORIGINAL_LONG_STRING_LENGTH, ORIGINAL_LONG_STRING_INITIAL_LENGTH; + +add_task(async function() { + const tab = await addTab(URL_ROOT + "network_requests_iframe.html"); + + const target = await getTargetForTab(tab); + const { client } = target; + const webConsoleFront = await target.getFront("console"); + + await webConsoleFront.startListeners(["NetworkActivity"]); + + // Override the default long string settings to lower values. + // This is done from the parent process's DevToolsServer as the LongString + // actor is being created from the parent process as network requests are + // watched from the parent process. + ORIGINAL_LONG_STRING_LENGTH = DevToolsServer.LONG_STRING_LENGTH; + ORIGINAL_LONG_STRING_INITIAL_LENGTH = + DevToolsServer.LONG_STRING_INITIAL_LENGTH; + + DevToolsServer.LONG_STRING_LENGTH = LONG_STRING_LENGTH; + DevToolsServer.LONG_STRING_INITIAL_LENGTH = LONG_STRING_INITIAL_LENGTH; + + info("test network POST request"); + + const onNetworkEvent = webConsoleFront.once("serverNetworkEvent"); + const updates = []; + let netActor = null; + const onAllNetworkEventUpdateReceived = new Promise(resolve => { + const onNetworkEventUpdate = packet => { + updates.push(packet.updateType); + assertNetworkEventUpdate(netActor, packet); + + if ( + updates.includes("responseContent") && + updates.includes("eventTimings") + ) { + client.off("networkEventUpdate", onNetworkEventUpdate); + resolve(); + } + }; + client.on("networkEventUpdate", onNetworkEventUpdate); + }); + + await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function() { + content.wrappedJSObject.testXhrPost(); + }); + + info("Waiting for networkEvent"); + const netEvent = await onNetworkEvent; + netActor = assertNetworkEvent(client, webConsoleFront, netEvent); + + info("Waiting for all networkEventUpdate"); + await onAllNetworkEventUpdateReceived; + const requestHeaders = await webConsoleFront.getRequestHeaders(netActor); + assertRequestHeaders(requestHeaders); + const requestCookies = await webConsoleFront.getRequestCookies(netActor); + assertRequestCookies(requestCookies); + const requestPostData = await webConsoleFront.getRequestPostData(netActor); + assertRequestPostData(requestPostData); + const responseHeaders = await webConsoleFront.getResponseHeaders(netActor); + assertResponseHeaders(responseHeaders); + const responseCookies = await webConsoleFront.getResponseCookies(netActor); + assertResponseCookies(responseCookies); + const responseContent = await webConsoleFront.getResponseContent(netActor); + assertResponseContent(responseContent); + const eventTimings = await webConsoleFront.getEventTimings(netActor); + assertEventTimings(eventTimings); + + await target.destroy(); + + DevToolsServer.LONG_STRING_LENGTH = ORIGINAL_LONG_STRING_LENGTH; + DevToolsServer.LONG_STRING_INITIAL_LENGTH = ORIGINAL_LONG_STRING_INITIAL_LENGTH; +}); + +function assertNetworkEvent(client, webConsoleFront, packet) { + info("checking the network event packet"); + + const netActor = packet.eventActor; + + checkObject(netActor, { + actor: /[a-z]/, + startedDateTime: /^\d+\-\d+\-\d+T.+$/, + url: /data\.json/, + method: "POST", + }); + + return netActor.actor; +} + +function assertNetworkEventUpdate(netActor, packet) { + info("received networkEventUpdate " + packet.updateType); + is(packet.from, netActor, "networkEventUpdate actor"); + + let expectedPacket = null; + + switch (packet.updateType) { + case "requestHeaders": + case "responseHeaders": + ok(packet.headers > 0, "headers > 0"); + ok(packet.headersSize > 0, "headersSize > 0"); + break; + case "requestCookies": + expectedPacket = { + cookies: 3, + }; + break; + case "requestPostData": + ok(packet.dataSize > 0, "dataSize > 0"); + break; + case "responseStart": + expectedPacket = { + response: { + httpVersion: /^HTTP\/\d\.\d$/, + status: "200", + statusText: "OK", + headersSize: /^\d+$/, + }, + }; + break; + case "securityInfo": + expectedPacket = { + state: "insecure", + }; + break; + case "responseCookies": + expectedPacket = { + cookies: 0, + }; + break; + case "responseContent": + expectedPacket = { + mimeType: "application/json", + contentSize: /^\d+$/, + }; + break; + case "eventTimings": + expectedPacket = { + totalTime: /^\d+$/, + }; + break; + default: + ok(false, "unknown network event update type: " + packet.updateType); + return; + } + + if (expectedPacket) { + info("checking the packet content"); + checkObject(packet, expectedPacket); + } +} + +function assertRequestHeaders(response) { + info("checking request headers"); + + ok(response.headers.length > 0, "request headers > 0"); + ok(response.headersSize > 0, "request headersSize > 0"); + + checkHeadersOrCookies(response.headers, { + Referer: /network_requests_iframe\.html/, + Cookie: /bug768096/, + }); +} + +function assertRequestCookies(response) { + info("checking request cookies"); + + is(response.cookies.length, 3, "request cookies length"); + + checkHeadersOrCookies(response.cookies, { + foobar: "fooval", + omgfoo: "bug768096", + badcookie: "bug826798=st3fan", + }); +} + +function assertRequestPostData(response) { + info("checking request POST data"); + + checkObject(response, { + postData: { + text: { + type: "longString", + initial: /^Hello world! foobaz barr.+foobaz barrfo$/, + length: 563, + actor: /[a-z]/, + }, + }, + postDataDiscarded: false, + }); + + is( + response.postData.text.initial.length, + LONG_STRING_INITIAL_LENGTH, + "postData text initial length" + ); +} + +function assertResponseHeaders(response) { + info("checking response headers"); + + ok(response.headers.length > 0, "response headers > 0"); + ok(response.headersSize > 0, "response headersSize > 0"); + + checkHeadersOrCookies(response.headers, { + "content-type": /^application\/(json|octet-stream)$/, + "content-length": /^\d+$/, + "x-very-short": "hello world", + "x-very-long": { + type: "longString", + length: 521, + initial: /^Lorem ipsum.+\. Donec vitae d$/, + actor: /[a-z]/, + }, + }); +} + +function assertResponseCookies(response) { + info("checking response cookies"); + + is(response.cookies.length, 0, "response cookies length"); +} + +function assertResponseContent(response) { + info("checking response content"); + + checkObject(response, { + content: { + text: { + type: "longString", + initial: /^\{ id: "test JSON data"(.|\r|\n)+ barfoo ba$/g, + length: 1070, + actor: /[a-z]/, + }, + }, + contentDiscarded: false, + }); + + is( + response.content.text.initial.length, + LONG_STRING_INITIAL_LENGTH, + "content initial length" + ); +} + +function assertEventTimings(response) { + info("checking event timings"); + + checkObject(response, { + timings: { + blocked: /^-1|\d+$/, + dns: /^-1|\d+$/, + connect: /^-1|\d+$/, + send: /^-1|\d+$/, + wait: /^-1|\d+$/, + receive: /^-1|\d+$/, + }, + totalTime: /^\d+$/, + }); +} diff --git a/devtools/shared/webconsole/test/browser/data.json b/devtools/shared/webconsole/test/browser/data.json new file mode 100644 index 0000000000..d46085c124 --- /dev/null +++ b/devtools/shared/webconsole/test/browser/data.json @@ -0,0 +1,3 @@ +{ id: "test JSON data", myArray: [ "foo", "bar", "baz", "biff" ], + veryLong: "foo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo bar" +} diff --git a/devtools/shared/webconsole/test/browser/data.json^headers^ b/devtools/shared/webconsole/test/browser/data.json^headers^ new file mode 100644 index 0000000000..bb6b45500f --- /dev/null +++ b/devtools/shared/webconsole/test/browser/data.json^headers^ @@ -0,0 +1,3 @@ +Content-Type: application/json +x-very-long: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse a ipsum massa. Phasellus at elit dictum libero laoreet sagittis. Phasellus condimentum ultricies imperdiet. Nam eu ligula justo, ut tincidunt quam. Etiam sollicitudin, tortor sed egestas blandit, sapien sem tincidunt nulla, eu luctus libero odio quis leo. Nam elit massa, mattis quis blandit ac, facilisis vitae arcu. Donec vitae dictum neque. Proin ornare nisl at lectus commodo iaculis eget eget est. Quisque scelerisque vestibulum quam sed interdum. +x-very-short: hello world diff --git a/devtools/shared/webconsole/test/browser/head.js b/devtools/shared/webconsole/test/browser/head.js new file mode 100644 index 0000000000..2ce4d01998 --- /dev/null +++ b/devtools/shared/webconsole/test/browser/head.js @@ -0,0 +1,70 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +"use strict"; + +/* import-globals-from ../../../../client/shared/test/shared-head.js */ + +Services.scriptloader.loadSubScript( + "chrome://mochitests/content/browser/devtools/client/shared/test/shared-head.js", + this +); + +async function getTargetForTab(tab) { + const target = await TargetFactory.forTab(tab); + info("Attaching to the active tab."); + await target.attach(); + return target; +} + +function checkObject(object, expected) { + for (const name of Object.keys(expected)) { + const expectedValue = expected[name]; + const value = object[name]; + checkValue(name, value, expectedValue); + } +} + +function checkValue(name, value, expected) { + if (expected === null) { + is(value, null, "'" + name + "' is null"); + } else if (value === null) { + ok(false, "'" + name + "' is null"); + } else if (value === undefined) { + ok(false, "'" + name + "' is undefined"); + } else if ( + typeof expected == "string" || + typeof expected == "number" || + typeof expected == "boolean" + ) { + is(value, expected, "property '" + name + "'"); + } else if (expected instanceof RegExp) { + ok(expected.test(value), name + ": " + expected + " matched " + value); + } else if (Array.isArray(expected)) { + info("checking array for property '" + name + "'"); + checkObject(value, expected); + } else if (typeof expected == "object") { + info("checking object for property '" + name + "'"); + checkObject(value, expected); + } +} + +function checkHeadersOrCookies(array, expected) { + const foundHeaders = {}; + + for (const elem of array) { + if (!(elem.name in expected)) { + continue; + } + foundHeaders[elem.name] = true; + info("checking value of header " + elem.name); + checkValue(elem.name, elem.value, expected[elem.name]); + } + + for (const header in expected) { + if (!(header in foundHeaders)) { + ok(false, header + " was not found"); + } + } +} diff --git a/devtools/shared/webconsole/test/browser/network_requests_iframe.html b/devtools/shared/webconsole/test/browser/network_requests_iframe.html new file mode 100644 index 0000000000..4e96364b06 --- /dev/null +++ b/devtools/shared/webconsole/test/browser/network_requests_iframe.html @@ -0,0 +1,66 @@ +<!DOCTYPE HTML> +<html> + <head> + <meta charset="utf-8"> + <title>Console HTTP test page</title> + <!-- Any copyright is dedicated to the Public Domain. + - http://creativecommons.org/publicdomain/zero/1.0/ --> + <script type="text/javascript"><!-- + "use strict"; + let setAllowAllCookies = false; + + function makeXhr(method, url, requestBody, callback) { + // On the first call, allow all cookies and set cookies, then resume the actual test + if (!setAllowAllCookies) { + SpecialPowers.pushPrefEnv({"set": [["network.cookie.cookieBehavior", 0]]}, + function() { + setAllowAllCookies = true; + setCookies(); + makeXhrCallback(method, url, requestBody, callback); + }); + } else { + makeXhrCallback(method, url, requestBody, callback); + } + } + + function makeXhrCallback(method, url, requestBody, callback) { + const xmlhttp = new XMLHttpRequest(); + xmlhttp.open(method, url, true); + if (callback) { + xmlhttp.onreadystatechange = function() { + if (xmlhttp.readyState == 4) { + callback(); + } + }; + } + xmlhttp.send(requestBody); + } + + /* exported testXhrGet */ + function testXhrGet(callback) { + makeXhr("get", "data.json", null, callback); + } + + /* exported testXhrPost */ + function testXhrPost(callback) { + const body = "Hello world! " + "foobaz barr".repeat(50); + makeXhr("post", "data.json", body, callback); + } + + function setCookies() { + document.cookie = "foobar=fooval"; + document.cookie = "omgfoo=bug768096"; + document.cookie = "badcookie=bug826798=st3fan"; + } + </script> + </head> + <body> + <h1>Web Console HTTP Logging Testpage</h1> + <h2>This page is used to test the HTTP logging.</h2> + + <form action="?" method="post"> + <input name="name" type="text" value="foo bar"><br> + <input name="age" type="text" value="144"><br> + </form> + </body> +</html> diff --git a/devtools/shared/webconsole/test/chrome/.eslintrc.js b/devtools/shared/webconsole/test/chrome/.eslintrc.js new file mode 100644 index 0000000000..660a3bf0a9 --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/.eslintrc.js @@ -0,0 +1,6 @@ +"use strict"; + +module.exports = { + // Extend from the shared list of defined globals for mochitests. + extends: "plugin:mozilla/chrome-test", +}; diff --git a/devtools/shared/webconsole/test/chrome/chrome.ini b/devtools/shared/webconsole/test/chrome/chrome.ini new file mode 100644 index 0000000000..e38c97a81b --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/chrome.ini @@ -0,0 +1,40 @@ +[DEFAULT] +tags = devtools +support-files = + common.js + data.json + data.json^headers^ + helper_serviceworker.js + network_requests_iframe.html + sandboxed_iframe.html + console-test-worker.js + !/browser/base/content/test/general/browser_star_hsts.sjs + +[test_basics.html] +[test_bug819670_getter_throws.html] +[test_cached_messages.html] +[test_commands_other.html] +[test_consoleapi.html] +[test_consoleapi_innerID.html] +[test_console_assert.html] +[test_console_group_styling.html] +[test_console_serviceworker.html] +[test_console_serviceworker_cached.html] +[test_console_styling.html] +[test_console_timestamp.html] +[test_console_worker.html] +[test_file_uri.html] +[test_jsterm.html] +[test_jsterm_autocomplete.html] +[test_jsterm_last_result.html] +[test_jsterm_queryselector.html] +[test_network_get.html] +skip-if = verify +[test_network_post.html] +[test_network_security-hsts.html] +[test_nsiconsolemessage.html] +[test_object_actor.html] +[test_object_actor_native_getters.html] +[test_object_actor_native_getters_lenient_this.html] +[test_page_errors.html] +[test_throw.html] diff --git a/devtools/shared/webconsole/test/chrome/common.js b/devtools/shared/webconsole/test/chrome/common.js new file mode 100644 index 0000000000..810ac0eed6 --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/common.js @@ -0,0 +1,281 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +"use strict"; + +/* exported attachConsole, attachConsoleToTab, attachConsoleToWorker, + closeDebugger, checkConsoleAPICalls, checkRawHeaders, runTests, nextTest, Ci, Cc, + withActiveServiceWorker, Services, consoleAPICall */ + +const { require } = ChromeUtils.import("resource://devtools/shared/Loader.jsm"); +const { DevToolsServer } = require("devtools/server/devtools-server"); +// eslint-disable-next-line mozilla/reject-some-requires +const { DevToolsClient } = require("devtools/client/devtools-client"); + +const Services = require("Services"); + +function initCommon() { + // Services.prefs.setBoolPref("devtools.debugger.log", true); +} + +function initDevToolsServer() { + DevToolsServer.init(); + DevToolsServer.registerAllActors(); + DevToolsServer.allowChromeProcess = true; +} + +async function connectToDebugger() { + initCommon(); + initDevToolsServer(); + + const transport = DevToolsServer.connectPipe(); + const client = new DevToolsClient(transport); + + await client.connect(); + return client; +} + +function attachConsole(listeners) { + return _attachConsole(listeners); +} +function attachConsoleToTab(listeners) { + return _attachConsole(listeners, true); +} +function attachConsoleToWorker(listeners) { + return _attachConsole(listeners, true, true); +} + +var _attachConsole = async function(listeners, attachToTab, attachToWorker) { + try { + const client = await connectToDebugger(); + + function waitForMessage(target) { + return new Promise(resolve => { + target.addEventListener("message", resolve, { once: true }); + }); + } + + // Fetch the console actor out of the expected target + // ParentProcessTarget / WorkerTarget / FrameTarget + let target, worker; + if (!attachToTab) { + const targetDescriptor = await client.mainRoot.getMainProcess(); + target = await targetDescriptor.getTarget(); + } else { + const targetDescriptor = await client.mainRoot.getTab(); + target = await targetDescriptor.getTarget(); + if (attachToWorker) { + const workerName = "console-test-worker.js#" + new Date().getTime(); + worker = new Worker(workerName); + await waitForMessage(worker); + + // listWorkers only works if the browsing context target actor is attached + await target.attach(); + const { workers } = await target.listWorkers(); + target = workers.filter(w => w.url == workerName)[0]; + if (!target) { + console.error( + "listWorkers failed. Unable to find the worker actor\n" + ); + return null; + } + } + } + + // Attach the Target and the target thread in order to instantiate the console client. + await target.attach(); + await target.attachThread(); + + const webConsoleFront = await target.getFront("console"); + + // By default the console isn't listening for anything, + // request listeners from here + const response = await webConsoleFront.startListeners(listeners); + return { + state: { + dbgClient: client, + webConsoleFront, + actor: webConsoleFront.actor, + // Keep a strong reference to the Worker to avoid it being + // GCd during the test (bug 1237492). + // eslint-disable-next-line camelcase + _worker_ref: worker, + }, + response, + }; + } catch (error) { + console.error( + `attachConsole failed: ${error.error} ${error.message} - ` + error.stack + ); + } + return null; +}; + +function closeDebugger(state, callback) { + const onClose = state.dbgClient.close(); + + state.dbgClient = null; + state.client = null; + + if (typeof callback === "function") { + onClose.then(callback); + } + return onClose; +} + +function checkConsoleAPICalls(consoleCalls, expectedConsoleCalls) { + is( + consoleCalls.length, + expectedConsoleCalls.length, + "received correct number of console calls" + ); + expectedConsoleCalls.forEach(function(message, index) { + info("checking received console call #" + index); + checkConsoleAPICall(consoleCalls[index], expectedConsoleCalls[index]); + }); +} + +function checkConsoleAPICall(call, expected) { + is( + call.arguments?.length || 0, + expected.arguments?.length || 0, + "number of arguments" + ); + + checkObject(call, expected); +} + +function checkObject(object, expected) { + if (object && object.getGrip) { + object = object.getGrip(); + } + + for (const name of Object.keys(expected)) { + const expectedValue = expected[name]; + const value = object[name]; + checkValue(name, value, expectedValue); + } +} + +function checkValue(name, value, expected) { + if (expected === null) { + ok(!value, "'" + name + "' is null"); + } else if (value === undefined) { + ok(false, "'" + name + "' is undefined"); + } else if (value === null) { + ok(false, "'" + name + "' is null"); + } else if ( + typeof expected == "string" || + typeof expected == "number" || + typeof expected == "boolean" + ) { + is(value, expected, "property '" + name + "'"); + } else if (expected instanceof RegExp) { + ok(expected.test(value), name + ": " + expected + " matched " + value); + } else if (Array.isArray(expected)) { + info("checking array for property '" + name + "'"); + checkObject(value, expected); + } else if (typeof expected == "object") { + info("checking object for property '" + name + "'"); + checkObject(value, expected); + } +} + +function checkHeadersOrCookies(array, expected) { + const foundHeaders = {}; + + for (const elem of array) { + if (!(elem.name in expected)) { + continue; + } + foundHeaders[elem.name] = true; + info("checking value of header " + elem.name); + checkValue(elem.name, elem.value, expected[elem.name]); + } + + for (const header in expected) { + if (!(header in foundHeaders)) { + ok(false, header + " was not found"); + } + } +} + +function checkRawHeaders(text, expected) { + const headers = text.split(/\r\n|\n|\r/); + const arr = []; + for (const header of headers) { + const index = header.indexOf(": "); + if (index < 0) { + continue; + } + arr.push({ + name: header.substr(0, index), + value: header.substr(index + 2), + }); + } + + checkHeadersOrCookies(arr, expected); +} + +var gTestState = {}; + +function runTests(tests, endCallback) { + function* driver() { + let lastResult, sendToNext; + for (let i = 0; i < tests.length; i++) { + gTestState.index = i; + const fn = tests[i]; + info("will run test #" + i + ": " + fn.name); + lastResult = fn(sendToNext, lastResult); + sendToNext = yield lastResult; + } + yield endCallback(sendToNext, lastResult); + } + gTestState.driver = driver(); + return gTestState.driver.next(); +} + +function nextTest(message) { + return gTestState.driver.next(message); +} + +function withActiveServiceWorker(win, url, scope) { + const opts = {}; + if (scope) { + opts.scope = scope; + } + return win.navigator.serviceWorker.register(url, opts).then(swr => { + if (swr.active) { + return swr; + } + + // Unfortunately we can't just use navigator.serviceWorker.ready promise + // here. If the service worker is for a scope that does not cover the window + // then the ready promise will never resolve. Instead monitor the service + // workers state change events to determine when its activated. + return new Promise(resolve => { + const sw = swr.waiting || swr.installing; + sw.addEventListener("statechange", function stateHandler(evt) { + if (sw.state === "activated") { + sw.removeEventListener("statechange", stateHandler); + resolve(swr); + } + }); + }); + }); +} + +/** + * + * @param {Front} consoleFront + * @param {Function} consoleCall: A function which calls the consoleAPI, e.g. : + * `() => top.console.log("test")`. + * @returns {Promise} A promise that will be resolved with the packet sent by the server + * in response to the consoleAPI call. + */ +function consoleAPICall(consoleFront, consoleCall) { + const onConsoleAPICall = consoleFront.once("consoleAPICall"); + consoleCall(); + return onConsoleAPICall; +} diff --git a/devtools/shared/webconsole/test/chrome/console-test-worker.js b/devtools/shared/webconsole/test/chrome/console-test-worker.js new file mode 100644 index 0000000000..b3091faf8f --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/console-test-worker.js @@ -0,0 +1,19 @@ +"use strict"; + +console.log("Log from worker init"); + +function f() { + const a = 1; + const b = 2; + const c = 3; + return { a, b, c }; +} + +self.onmessage = function(event) { + if (event.data == "ping") { + f(); + postMessage("pong"); + } +}; + +postMessage("load"); diff --git a/devtools/shared/webconsole/test/chrome/data.json b/devtools/shared/webconsole/test/chrome/data.json new file mode 100644 index 0000000000..d46085c124 --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/data.json @@ -0,0 +1,3 @@ +{ id: "test JSON data", myArray: [ "foo", "bar", "baz", "biff" ], + veryLong: "foo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo barfoo bar" +} diff --git a/devtools/shared/webconsole/test/chrome/data.json^headers^ b/devtools/shared/webconsole/test/chrome/data.json^headers^ new file mode 100644 index 0000000000..bb6b45500f --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/data.json^headers^ @@ -0,0 +1,3 @@ +Content-Type: application/json +x-very-long: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse a ipsum massa. Phasellus at elit dictum libero laoreet sagittis. Phasellus condimentum ultricies imperdiet. Nam eu ligula justo, ut tincidunt quam. Etiam sollicitudin, tortor sed egestas blandit, sapien sem tincidunt nulla, eu luctus libero odio quis leo. Nam elit massa, mattis quis blandit ac, facilisis vitae arcu. Donec vitae dictum neque. Proin ornare nisl at lectus commodo iaculis eget eget est. Quisque scelerisque vestibulum quam sed interdum. +x-very-short: hello world diff --git a/devtools/shared/webconsole/test/chrome/helper_serviceworker.js b/devtools/shared/webconsole/test/chrome/helper_serviceworker.js new file mode 100644 index 0000000000..5f0f520819 --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/helper_serviceworker.js @@ -0,0 +1,21 @@ +"use strict"; + +console.log("script evaluation"); + +addEventListener("install", function(evt) { + console.log("install event"); +}); + +addEventListener("activate", function(evt) { + console.log("activate event"); +}); + +addEventListener("fetch", function(evt) { + console.log("fetch event: " + evt.request.url); + evt.respondWith(new Response("Hello world")); +}); + +addEventListener("message", function(evt) { + console.log("message event: " + evt.data.message); + evt.source.postMessage({ type: "PONG" }); +}); diff --git a/devtools/shared/webconsole/test/chrome/network_requests_iframe.html b/devtools/shared/webconsole/test/chrome/network_requests_iframe.html new file mode 100644 index 0000000000..c380941a1e --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/network_requests_iframe.html @@ -0,0 +1,66 @@ +<!DOCTYPE HTML> +<html> + <head> + <meta charset="utf-8"> + <title>Console HTTP test page</title> + <!-- Any copyright is dedicated to the Public Domain. + - http://creativecommons.org/publicdomain/zero/1.0/ --> + <script type="text/javascript"><!-- + "use strict"; + let setAllowAllCookies = false; + + function makeXhr(method, url, requestBody, callback) { + // On the first call, allow all cookies and set cookies, then resume the actual test + if (!setAllowAllCookies) { + SpecialPowers.pushPrefEnv({"set": [["network.cookie.cookieBehavior", 0]]}, + function() { + setAllowAllCookies = true; + setCookies(); + makeXhrCallback(method, url, requestBody, callback); + }); + } else { + makeXhrCallback(method, url, requestBody, callback); + } + } + + function makeXhrCallback(method, url, requestBody, callback) { + const xmlhttp = new XMLHttpRequest(); + xmlhttp.open(method, url, true); + if (callback) { + xmlhttp.onreadystatechange = function() { + if (xmlhttp.readyState == 4) { + callback(); + } + }; + } + xmlhttp.send(requestBody); + } + + /* exported testXhrGet */ + function testXhrGet(callback) { + makeXhr("get", "data.json", null, callback); + } + + /* exported testXhrPost */ + function testXhrPost(callback) { + const body = "Hello world! " + (new Array(50)).join("foobaz barr"); + makeXhr("post", "data.json", body, callback); + } + + function setCookies() { + document.cookie = "foobar=fooval"; + document.cookie = "omgfoo=bug768096"; + document.cookie = "badcookie=bug826798=st3fan"; + } + </script> + </head> + <body> + <h1>Web Console HTTP Logging Testpage</h1> + <h2>This page is used to test the HTTP logging.</h2> + + <form action="?" method="post"> + <input name="name" type="text" value="foo bar"><br> + <input name="age" type="text" value="144"><br> + </form> + </body> +</html> diff --git a/devtools/shared/webconsole/test/chrome/sandboxed_iframe.html b/devtools/shared/webconsole/test/chrome/sandboxed_iframe.html new file mode 100644 index 0000000000..55a6224b50 --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/sandboxed_iframe.html @@ -0,0 +1,8 @@ +<html> +<head><title>Sandboxed iframe</title></head> +<body> + <iframe id="sandboxed-iframe" + sandbox="allow-scripts" + srcdoc="<script>var foobarObject = {bug1051224: 'sandboxed'};</script>"></iframe> +</body> +</html> diff --git a/devtools/shared/webconsole/test/chrome/test_basics.html b/devtools/shared/webconsole/test/chrome/test_basics.html new file mode 100644 index 0000000000..97ca75e775 --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/test_basics.html @@ -0,0 +1,64 @@ +<!DOCTYPE HTML> +<html lang="en"> +<head> + <meta charset="utf8"> + <title>Basic Web Console Actor tests</title> + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="common.js"></script> + <!-- Any copyright is dedicated to the Public Domain. + - http://creativecommons.org/publicdomain/zero/1.0/ --> +</head> +<body> +<p>Basic Web Console Actor tests</p> + +<script class="testbody" type="text/javascript"> +"use strict"; + +SimpleTest.waitForExplicitFinish(); + +async function startTest() +{ + removeEventListener("load", startTest); + + let {state, response} = await attachConsoleToTab(["PageError"]); + is(response.startedListeners.length, 1, "startedListeners.length"); + is(response.startedListeners[0], "PageError", "startedListeners: PageError"); + ok(response.nativeConsoleAPI, "nativeConsoleAPI"); + + await closeDebugger(state); + top.console_ = top.console; + top.console = { lolz: "foo" }; + ({state, response} = await attachConsoleToTab(["PageError", "ConsoleAPI", "foo"])); + + const startedListeners = response.startedListeners; + is(startedListeners.length, 2, "startedListeners.length"); + isnot(startedListeners.indexOf("PageError"), -1, "startedListeners: PageError"); + isnot(startedListeners.indexOf("ConsoleAPI"), -1, + "startedListeners: ConsoleAPI"); + is(startedListeners.indexOf("foo"), -1, "startedListeners: no foo"); + ok(!response.nativeConsoleAPI, "!nativeConsoleAPI"); + + top.console = top.console_; + response = await state.webConsoleFront.stopListeners(["ConsoleAPI", "foo"]); + + is(response.stoppedListeners.length, 1, "stoppedListeners.length"); + is(response.stoppedListeners[0], "ConsoleAPI", "stoppedListeners: ConsoleAPI"); + await closeDebugger(state); + ({state, response} = await attachConsoleToTab(["ConsoleAPI"])); + + is(response.startedListeners.length, 1, "startedListeners.length"); + is(response.startedListeners[0], "ConsoleAPI", "startedListeners: ConsoleAPI"); + ok(response.nativeConsoleAPI, "nativeConsoleAPI"); + + top.console = top.console_; + delete top.console_; + + closeDebugger(state, function() { + SimpleTest.finish(); + }); +} + +addEventListener("load", startTest); +</script> +</body> +</html> diff --git a/devtools/shared/webconsole/test/chrome/test_bug819670_getter_throws.html b/devtools/shared/webconsole/test/chrome/test_bug819670_getter_throws.html new file mode 100644 index 0000000000..aa34705eb5 --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/test_bug819670_getter_throws.html @@ -0,0 +1,52 @@ +<!DOCTYPE HTML> +<html lang="en"> + <head> + <meta charset="utf8"> + <title>Test for Bug 819670 - Web console object inspection does not handle native getters throwing very well</title> + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="common.js"></script> + <!-- Any copyright is dedicated to the Public Domain. + - http://creativecommons.org/publicdomain/zero/1.0/ --> + </head> + <body> + <p>Test for Bug 819670 - Web console object inspection does not handle native getters throwing very well</p> + + <script class="testbody" type="text/javascript"> + "use strict"; + + SimpleTest.waitForExplicitFinish(); + addEventListener("load", startTest); + + async function startTest() { + removeEventListener("load", startTest); + const {state} = await attachConsoleToTab([]); + const evaluationResponse = await state.webConsoleFront.evaluateJSAsync("document.__proto__"); + checkObject(evaluationResponse, { + input: "document.__proto__", + result: { + type: "object", + actor: /[a-z]/ + } + }); + + ok(!evaluationResponse.exception, "no eval exception"); + ok(!evaluationResponse.helperResult, "no helper result"); + + const response = await evaluationResponse.result.getPrototypeAndProperties(); + ok(! response.error, "no response error"); + + const props = response.ownProperties; + ok(props, "response properties available"); + + if (props) { + // eslint-disable-next-line no-proto + const expectedProps = Object.getOwnPropertyNames(document.__proto__); + isDeeply(Object.keys(props), expectedProps, "Same own properties."); + } + + await closeDebugger(state); + SimpleTest.finish(); + } + </script> + </body> +</html> diff --git a/devtools/shared/webconsole/test/chrome/test_cached_messages.html b/devtools/shared/webconsole/test/chrome/test_cached_messages.html new file mode 100644 index 0000000000..45ff466d78 --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/test_cached_messages.html @@ -0,0 +1,221 @@ +<!DOCTYPE HTML> +<html lang="en"> +<head> + <meta charset="utf8"> + <title>Test for cached messages</title> + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="common.js"></script> + <!-- Any copyright is dedicated to the Public Domain. + - http://creativecommons.org/publicdomain/zero/1.0/ --> +</head> +<body> +<p>Test for cached messages</p> + +<script class="testbody" type="application/javascript"> +"use strict"; + +const { MESSAGE_CATEGORY } = require("devtools/shared/constants"); + +const previousEnabled = window.docShell.cssErrorReportingEnabled; +window.docShell.cssErrorReportingEnabled = true; + +SimpleTest.registerCleanupFunction(() => { + window.docShell.cssErrorReportingEnabled = previousEnabled; +}); + +var ConsoleAPIStorage = Cc["@mozilla.org/consoleAPI-storage;1"] + .getService(Ci.nsIConsoleAPIStorage); +let expectedConsoleCalls = []; +let expectedPageErrors = []; + +function doPageErrors() { + Services.console.reset(); + + expectedPageErrors = [ + { + errorMessage: /fooColor/, + sourceName: /.+/, + category: MESSAGE_CATEGORY.CSS_PARSER, + timeStamp: /^\d+$/, + error: false, + warning: true, + }, + { + errorMessage: /doTheImpossible/, + sourceName: /.+/, + category: "chrome javascript", + timeStamp: /^\d+$/, + error: true, + warning: false, + }, + ]; + + let container = document.createElement("script"); + document.body.appendChild(container); + container.textContent = "document.body.style.color = 'fooColor';"; + document.body.removeChild(container); + + SimpleTest.expectUncaughtException(); + + container = document.createElement("script"); + document.body.appendChild(container); + container.textContent = "document.doTheImpossible();"; + document.body.removeChild(container); +} + +function doConsoleCalls() { + ConsoleAPIStorage.clearEvents(); + + top.console.log("foobarBaz-log", undefined); + top.console.info("foobarBaz-info", null); + top.console.warn("foobarBaz-warn", document.body); + + expectedConsoleCalls = [ + { + level: "log", + filename: /test_cached_messages/, + functionName: "doConsoleCalls", + timeStamp: /^\d+$/, + arguments: ["foobarBaz-log", { type: "undefined" }], + }, + { + level: "info", + filename: /test_cached_messages/, + functionName: "doConsoleCalls", + timeStamp: /^\d+$/, + arguments: ["foobarBaz-info", { type: "null" }], + }, + { + level: "warn", + filename: /test_cached_messages/, + functionName: "doConsoleCalls", + timeStamp: /^\d+$/, + arguments: ["foobarBaz-warn", { type: "object", actor: /[a-z]/ }], + }, + ]; +} +</script> + +<script class="testbody" type="text/javascript"> +"use strict"; + +SimpleTest.waitForExplicitFinish(); +var {ConsoleServiceListener} = + require("devtools/server/actors/webconsole/listeners/console-service"); +var {ConsoleAPIListener} = + require("devtools/server/actors/webconsole/listeners/console-api"); + +let consoleAPIListener, consoleServiceListener; +let consoleAPICalls = 0; +let pageErrors = 0; + +async function onConsoleAPICall(message) { + for (const msg of expectedConsoleCalls) { + if (msg.functionName == message.functionName && + msg.filename.test(message.filename)) { + consoleAPICalls++; + break; + } + } + if (consoleAPICalls == expectedConsoleCalls.length) { + await checkConsoleAPICache(); + } +} + +async function onConsoleServiceMessage(message) { + if (!(message instanceof Ci.nsIScriptError)) { + return; + } + for (const msg of expectedPageErrors) { + if (msg.category == message.category && + msg.errorMessage.test(message.errorMessage)) { + pageErrors++; + break; + } + } + if (pageErrors == expectedPageErrors.length) { + await testPageErrors(); + } +} + +function startTest() { + removeEventListener("load", startTest); + + consoleAPIListener = new ConsoleAPIListener(top, onConsoleAPICall); + consoleAPIListener.init(); + + doConsoleCalls(); +} + +async function checkConsoleAPICache() { + consoleAPIListener.destroy(); + consoleAPIListener = null; + const {state} = await attachConsole(["ConsoleAPI"]); + const response = await state.webConsoleFront.getCachedMessages(["ConsoleAPI"]); + onCachedConsoleAPI(state, response); +} + +function onCachedConsoleAPI(state, response) { + const msgs = response.messages; + info("cached console messages: " + msgs.length); + + ok(msgs.length >= expectedConsoleCalls.length, + "number of cached console messages"); + + for (const {message} of msgs) { + for (const expected of expectedConsoleCalls) { + if (expected.functionName == message.functionName && + expected.filename.test(message.filename)) { + expectedConsoleCalls.splice(expectedConsoleCalls.indexOf(expected)); + checkConsoleAPICall(message, expected); + break; + } + } + } + + is(expectedConsoleCalls.length, 0, "all expected messages have been found"); + + closeDebugger(state, function() { + consoleServiceListener = new ConsoleServiceListener(null, onConsoleServiceMessage); + consoleServiceListener.init(); + doPageErrors(); + }); +} + +async function testPageErrors() { + consoleServiceListener.destroy(); + consoleServiceListener = null; + const {state} = await attachConsole(["PageError"]); + const response = await state.webConsoleFront.getCachedMessages(["PageError"]); + onCachedPageErrors(state, response); +} + +function onCachedPageErrors(state, response) { + const msgs = response.messages; + info("cached page errors: " + msgs.length); + + ok(msgs.length >= expectedPageErrors.length, + "number of cached page errors"); + + for (const {pageError} of msgs) { + for (const expected of expectedPageErrors) { + if (expected.category == pageError.category && + expected.errorMessage.test(pageError.errorMessage)) { + expectedPageErrors.splice(expectedPageErrors.indexOf(expected)); + checkObject(pageError, expected); + break; + } + } + } + + is(expectedPageErrors.length, 0, "all expected messages have been found"); + + closeDebugger(state, function() { + SimpleTest.finish(); + }); +} + +addEventListener("load", startTest); +</script> +</body> +</html> diff --git a/devtools/shared/webconsole/test/chrome/test_commands_other.html b/devtools/shared/webconsole/test/chrome/test_commands_other.html new file mode 100644 index 0000000000..702d01f354 --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/test_commands_other.html @@ -0,0 +1,84 @@ +<!DOCTYPE HTML> +<html lang="en"> +<head> + <meta charset="utf8"> + <title>Test for the other command helpers</title> + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="common.js"></script> + <!-- Any copyright is dedicated to the Public Domain. + - http://creativecommons.org/publicdomain/zero/1.0/ --> +</head> +<body> +<p>Test for the querySelector / querySelectorAll helpers</p> + +<script class="testbody" type="text/javascript"> +"use strict"; + +SimpleTest.waitForExplicitFinish(); +let gState; +let gWin; +const tests = [ + async function keys() { + const response = await evaluateJS("keys({foo: 'bar'})"); + checkObject(response, { + result: { + class: "Array", + preview: { + items: ["foo"] + } + } + }); + nextTest(); + }, + async function values() { + const response = await evaluateJS("values({foo: 'bar'})"); + checkObject(response, { + result: { + class: "Array", + preview: { + items: ["bar"] + } + } + }); + nextTest(); + }, +]; + +function evaluateJS(input) { + return gState.webConsoleFront.evaluateJSAsync(input); +} + +async function startTest() { + info ("Content window opened, attaching console to it"); + + const systemPrincipal = Cc["@mozilla.org/systemprincipal;1"].createInstance(Ci.nsIPrincipal); + ok (!gWin.document.nodePrincipal.equals(systemPrincipal), + "The test document is not using the system principal"); + + const {state} = await attachConsoleToTab([]); + gState = state; + runTests(tests, testEnd); +} + +function testEnd() { + gWin.close(); + gWin = null; + closeDebugger(gState, function() { + gState = null; + SimpleTest.finish(); + }); +} + +const load = async function () { + removeEventListener("load", load); + + // Open a content window to test XRay functionality on built in functions. + gWin = window.open("javascript:'<html><body>dummy</body></html>';"); + info ("Waiting for content window to load"); + gWin.onload = startTest; +}; +addEventListener("load", load); + +</script> +</body> +</html> diff --git a/devtools/shared/webconsole/test/chrome/test_console_assert.html b/devtools/shared/webconsole/test/chrome/test_console_assert.html new file mode 100644 index 0000000000..f847d5f5d8 --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/test_console_assert.html @@ -0,0 +1,106 @@ +<!DOCTYPE HTML> +<html> +<head> + <meta charset="utf-8"> + <title>Test for console.group styling with %c</title> + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="common.js"></script> + <!-- Any copyright is dedicated to the Public Domain. + - http://creativecommons.org/publicdomain/zero/1.0/ --> + <script> +"use strict"; + +window.onload = async function () { + SimpleTest.waitForExplicitFinish(); + let state; + try { + state = (await attachConsole(["ConsoleAPI"])).state; + const {webConsoleFront} = state; + + await testFalseAssert(webConsoleFront); + await testFalsyAssert(webConsoleFront); + await testUndefinedAssert(webConsoleFront); + await testNullAssert(webConsoleFront); + await testTrueAssert(webConsoleFront); + + } catch (e) { + ok(false, `Error thrown: ${e.message}`); + } + + await closeDebugger(state); + SimpleTest.finish(); +}; + +async function testFalseAssert(consoleFront) { + info(`Testing console.assert(false)`); + const packet = await consoleAPICall( + consoleFront, + () => top.console.assert(false, "assertion is false") + ); + + checkConsoleAPICall(packet.message, { + arguments: ["assertion is false"] + }); +} + +async function testFalsyAssert(consoleFront) { + info(`Testing console.assert(0")`); + const packet = await consoleAPICall( + consoleFront, + () => top.console.assert(0, "assertion is false") + ); + + checkConsoleAPICall(packet.message, { + arguments: ["assertion is false"] + }); +} + +async function testUndefinedAssert(consoleFront) { + info(`Testing console.assert(undefined)`); + const packet = await consoleAPICall( + consoleFront, + () => top.console.assert(undefined, "assertion is false") + ); + + checkConsoleAPICall(packet.message, { + arguments: ["assertion is false"] + }); +} + +async function testNullAssert(consoleFront) { + info(`Testing console.assert(null)`); + const packet = await consoleAPICall( + consoleFront, + () => top.console.assert(null, "assertion is false") + ); + + checkConsoleAPICall(packet.message, { + arguments: ["assertion is false"] + }); +} + +async function testTrueAssert(consoleFront) { + info(`Testing console.assert(true)`); + const onConsoleApiCall = consoleAPICall( + consoleFront, + () => top.console.assert(true, "assertion is false") + ); + + const TIMEOUT = Symbol(); + const onTimeout = new Promise(resolve => setTimeout(() => resolve(TIMEOUT), 1000)); + + const res = await Promise.race([onConsoleApiCall, onTimeout]); + is(res, TIMEOUT, + "There was no consoleAPICall event in response to a truthy console.assert"); +} + + </script> +</head> +<body> + <p id="display"></p> + <div id="content" style="display: none"> + </div> + <pre id="test"> + </pre> +</body> +</html> diff --git a/devtools/shared/webconsole/test/chrome/test_console_group_styling.html b/devtools/shared/webconsole/test/chrome/test_console_group_styling.html new file mode 100644 index 0000000000..62ad760614 --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/test_console_group_styling.html @@ -0,0 +1,121 @@ +<!DOCTYPE HTML> +<html> +<head> + <meta charset="utf-8"> + <title>Test for console.group styling with %c</title> + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="common.js"></script> + <!-- Any copyright is dedicated to the Public Domain. + - http://creativecommons.org/publicdomain/zero/1.0/ --> + <script> +"use strict"; + +window.onload = async function () { + SimpleTest.waitForExplicitFinish(); + let state; + try { + state = (await attachConsole(["ConsoleAPI"])).state + const consoleFront = state.webConsoleFront; + + await testSingleCustomStyleGroup(consoleFront); + await testSingleCustomStyleGroupCollapsed(consoleFront); + await testMultipleCustomStyleGroup(consoleFront); + await testMultipleCustomStyleGroupCollapsed(consoleFront); + await testFormatterWithNoStyleGroup(consoleFront); + await testFormatterWithNoStyleGroupCollapsed(consoleFront); + } catch (e) { + ok(false, `Error thrown: ${e.message}`); + } + + await closeDebugger(state); + SimpleTest.finish(); +}; + +async function testSingleCustomStyleGroup(consoleFront) { + info("Testing console.group with a custom style"); + const packet = await consoleAPICall( + consoleFront, + () => top.console.group("%cfoobar", "color:red") + ); + + checkConsoleAPICall(packet.message, { + arguments: ["foobar"], + styles: ["color:red"] + }); +} + +async function testSingleCustomStyleGroupCollapsed(consoleFront) { + info("Testing console.groupCollapsed with a custom style"); + const packet = await consoleAPICall( + consoleFront, + () => top.console.groupCollapsed("%cfoobaz", "color:blue") + ); + + checkConsoleAPICall(packet.message, { + arguments: ["foobaz"], + styles: ["color:blue"] + }); +} + +async function testMultipleCustomStyleGroup(consoleFront) { + info("Testing console.group with multiple custom styles"); + const packet = await consoleAPICall( + consoleFront, + () => top.console.group("%cfoo%cbar", "color:red", "color:blue") + ); + + checkConsoleAPICall(packet.message, { + arguments: ["foo", "bar"], + styles: ["color:red", "color:blue"] + }); +} + +async function testMultipleCustomStyleGroupCollapsed(consoleFront) { + info("Testing console.groupCollapsed with multiple custom styles"); + const packet = await consoleAPICall( + consoleFront, + () => top.console.group("%cfoo%cbaz", "color:red", "color:green") + ); + + checkConsoleAPICall(packet.message, { + arguments: ["foo", "baz"], + styles: ["color:red", "color:green"] + }); +} + +async function testFormatterWithNoStyleGroup(consoleFront) { + info("Testing console.group with one formatter but no style"); + const packet = await consoleAPICall( + consoleFront, + () => top.console.group("%cfoobar") + ); + + checkConsoleAPICall(packet.message, { + arguments: ["%cfoobar"], + styles: [] + }); +} + +async function testFormatterWithNoStyleGroupCollapsed(consoleFront) { + info("Testing console.groupCollapsed with one formatter but no style"); + const packet = await consoleAPICall( + consoleFront, + () => top.console.groupCollapsed("%cfoobaz") + ); + + checkConsoleAPICall(packet.message, { + arguments: ["%cfoobaz"], + styles: [] + }); +} + + </script> +</head> +<body> + <p id="display"></p> + <div id="content" style="display: none"> + </div> + <pre id="test"> + </pre> +</body> +</html> diff --git a/devtools/shared/webconsole/test/chrome/test_console_serviceworker.html b/devtools/shared/webconsole/test/chrome/test_console_serviceworker.html new file mode 100644 index 0000000000..79d1e2e3a2 --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/test_console_serviceworker.html @@ -0,0 +1,202 @@ +<!DOCTYPE HTML> +<html lang="en"> +<head> + <meta charset="utf8"> + <title>Test for the Console API and Service Workers</title> + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="common.js"></script> + <!-- Any copyright is dedicated to the Public Domain. + - http://creativecommons.org/publicdomain/zero/1.0/ --> +</head> +<body> +<p>Test for the Console API and Service Workers</p> + +<script class="testbody" type="text/javascript"> +"use strict"; + +SimpleTest.waitForExplicitFinish(); + +// Utils functions +function withFrame(url) { + return new Promise(resolve => { + const iframe = document.createElement("iframe"); + iframe.onload = function () { + resolve(iframe); + }; + iframe.src = url; + document.body.appendChild(iframe); + }); +} + +function navigateFrame(iframe, url) { + return new Promise(resolve => { + iframe.onload = function () { + resolve(iframe); + }; + iframe.src = url; + }); +} + +function forceReloadFrame(iframe) { + return new Promise(resolve => { + iframe.onload = function () { + resolve(iframe); + }; + iframe.contentWindow.location.reload(true); + }); +} + +function messageServiceWorker(win, scope, message) { + return win.navigator.serviceWorker.getRegistration(scope).then(swr => { + return new Promise(resolve => { + win.navigator.serviceWorker.onmessage = evt => { + resolve(); + }; + const sw = swr.active || swr.waiting || swr.installing; + sw.postMessage({ type: "PING", message: message }); + }); + }); +} + +function unregisterServiceWorker(win) { + return win.navigator.serviceWorker.ready.then(swr => { + return swr.unregister(); + }); +} + +// Test +const BASE_URL = "https://example.com/chrome/devtools/shared/webconsole/test/chrome/"; +const SERVICE_WORKER_URL = BASE_URL + "helper_serviceworker.js"; +const SCOPE = BASE_URL + "foo/"; +const NONSCOPE_FRAME_URL = BASE_URL + "sandboxed_iframe.html"; +const SCOPE_FRAME_URL = SCOPE + "fake.html"; +const SCOPE_FRAME_URL2 = SCOPE + "whatsit.html"; +const MESSAGE = 'Tic Tock'; + +const expectedConsoleCalls = [ + { + level: "log", + filename: /helper_serviceworker/, + arguments: ['script evaluation'], + }, + { + level: "log", + filename: /helper_serviceworker/, + arguments: ['install event'], + }, + { + level: "log", + filename: /helper_serviceworker/, + arguments: ['activate event'], + }, + { + level: "log", + filename: /helper_serviceworker/, + arguments: ['fetch event: ' + SCOPE_FRAME_URL], + }, + { + level: "log", + filename: /helper_serviceworker/, + arguments: ['fetch event: ' + SCOPE_FRAME_URL2], + }, +]; +let consoleCalls = []; + +const startTest = async function () { + removeEventListener("load", startTest); + + await new Promise(resolve => { + SpecialPowers.pushPrefEnv({"set": [ + ["dom.serviceWorkers.enabled", true], + ["devtools.webconsole.filter.serviceworkers", true] + ]}, resolve); + }); + + const {state, response} = await attachConsoleToTab(["ConsoleAPI"]); + onAttach(state, response); +}; +addEventListener("load", startTest); + +const onAttach = async function (state, response) { + onConsoleAPICall = onConsoleAPICall.bind(null, state); + state.webConsoleFront.on("consoleAPICall", onConsoleAPICall); + + let currentFrame; + try { + // First, we need a frame from which to register our script. This + // will not trigger any console calls. + info("Loading a non-scope frame from which to register a service worker."); + currentFrame = await withFrame(NONSCOPE_FRAME_URL); + + // Now register the service worker and wait for it to become + // activate. This should trigger 3 console calls; 1 for script + // evaluation, 1 for the install event, and 1 for the activate + // event. These console calls are received because we called + // register(), not because we are in scope for the worker. + info("Registering the service worker"); + await withActiveServiceWorker(currentFrame.contentWindow, + SERVICE_WORKER_URL, SCOPE); + ok(!currentFrame.contentWindow.navigator.serviceWorker.controller, + 'current frame should not be controlled'); + + // Now that the service worker is activate, lets navigate our frame. + // This will trigger 1 more console call for the fetch event. + info("Service worker registered. Navigating frame."); + await navigateFrame(currentFrame, SCOPE_FRAME_URL); + ok(currentFrame.contentWindow.navigator.serviceWorker.controller, + 'navigated frame should be controlled'); + + // We now have a controlled frame. Lets perform a non-navigation fetch. + // This should produce another console call for the fetch event. + info("Frame navigated. Calling fetch()."); + await currentFrame.contentWindow.fetch(SCOPE_FRAME_URL2); + + // Now force refresh our controlled frame. This will cause the frame + // to bypass the service worker and become an uncontrolled frame. It + // also happens to make the frame display a 404 message because the URL + // does not resolve to a real resource. This is ok, as we really only + // care about the frame being non-controlled, but still having a location + // that matches our service worker scope so we can provide its not + // incorrectly getting console calls. + info("Completed fetch(). Force refreshing to get uncontrolled frame."); + await forceReloadFrame(currentFrame); + ok(!currentFrame.contentWindow.navigator.serviceWorker.controller, + 'current frame should not be controlled after force refresh'); + is(currentFrame.contentWindow.location.toString(), SCOPE_FRAME_URL, + 'current frame should still have in-scope location URL even though it got 404'); + + // Now postMessage() the service worker to trigger its message event + // handler. This will generate 1 or 2 to console.log() statements + // depending on if the worker thread needs to spin up again. In either + // case, though, we should not get any console calls because we don't + // have a controlled or registering document. + info("Completed force refresh. Messaging service worker."); + await messageServiceWorker(currentFrame.contentWindow, SCOPE, MESSAGE); + + info("Done messaging service worker. Unregistering service worker."); + await unregisterServiceWorker(currentFrame.contentWindow); + + info('Service worker unregistered. Checking console calls.'); + state.webConsoleFront.off("consoleAPICall", onConsoleAPICall); + checkConsoleAPICalls(consoleCalls, expectedConsoleCalls); + } catch(error) { + ok(false, 'unexpected error: ' + error); + } finally { + if (currentFrame) { + currentFrame.remove(); + currentFrame = null; + } + consoleCalls = []; + closeDebugger(state, function() { + SimpleTest.finish(); + }); + } +}; + +function onConsoleAPICall(state, packet) { + info("received message level: " + packet.message.level); + consoleCalls.push(packet.message); +} +</script> +</body> +</html> diff --git a/devtools/shared/webconsole/test/chrome/test_console_serviceworker_cached.html b/devtools/shared/webconsole/test/chrome/test_console_serviceworker_cached.html new file mode 100644 index 0000000000..9f94f82f93 --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/test_console_serviceworker_cached.html @@ -0,0 +1,119 @@ +<!DOCTYPE HTML> +<html lang="en"> +<head> + <meta charset="utf8"> + <title>Test for getCachedMessages and Service Workers</title> + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="common.js"></script> + <!-- Any copyright is dedicated to the Public Domain. + - http://creativecommons.org/publicdomain/zero/1.0/ --> +</head> +<body> +<p>Test for getCachedMessages and Service Workers</p> + +<script class="testbody" type="text/javascript"> +"use strict"; + +SimpleTest.waitForExplicitFinish(); + +const BASE_URL = "https://example.com/chrome/devtools/shared/webconsole/test/chrome/"; +const SERVICE_WORKER_URL = BASE_URL + "helper_serviceworker.js"; +const FRAME_URL = BASE_URL + "sandboxed_iframe.html"; + +const firstTabExpectedCalls = [ + { + message: { + level: "log", + filename: /helper_serviceworker/, + arguments: ['script evaluation'], + } + }, { + message: { + level: "log", + filename: /helper_serviceworker/, + arguments: ['install event'], + } + }, { + message: { + level: "log", + filename: /helper_serviceworker/, + arguments: ['activate event'], + } + }, +]; + +const secondTabExpectedCalls = [ + { + message: { + level: "log", + filename: /helper_serviceworker/, + arguments: ['fetch event: ' + FRAME_URL], + } + } +]; + +const startTest = async function () { + removeEventListener("load", startTest); + + await new Promise(resolve => { + SpecialPowers.pushPrefEnv({"set": [ + ["dom.serviceWorkers.enabled", true], + ["devtools.webconsole.filter.serviceworkers", true] + ]}, resolve); + }); + + info("Adding a tab and attaching a service worker"); + const tab1 = await addTab(FRAME_URL); + const swr = await withActiveServiceWorker(tab1.linkedBrowser.contentWindow, + SERVICE_WORKER_URL); + + info("Attaching console to tab 1"); + let {state} = await attachConsoleToTab(["ConsoleAPI"]); + let calls = await state.webConsoleFront.getCachedMessages(["ConsoleAPI"]); + checkConsoleAPICalls(calls.messages, firstTabExpectedCalls); + await closeDebugger(state); + + // Because this tab is being added after the original messages happened, + // they shouldn't show up in a call to getCachedMessages. + // However, there is a fetch event which is logged due to loading the tab. + info("Adding a new tab at the same URL"); + + await addTab(FRAME_URL); + info("Attaching console to tab 2"); + state = (await attachConsoleToTab(["ConsoleAPI"])).state; + calls = await state.webConsoleFront.getCachedMessages(["ConsoleAPI"]); + checkConsoleAPICalls(calls.messages, secondTabExpectedCalls); + await closeDebugger(state); + + await swr.unregister(); + + SimpleTest.finish(); +}; +addEventListener("load", startTest); + +// This test needs to add tabs that are controlled by a service worker +// so use some special powers to dig around and find gBrowser +const {gBrowser} = SpecialPowers._getTopChromeWindow(SpecialPowers.window); + +SimpleTest.registerCleanupFunction(() => { + while (gBrowser.tabs.length > 1) { + gBrowser.removeCurrentTab(); + } +}); + +function addTab(url) { + info("Adding a new tab with URL: '" + url + "'"); + return new Promise(resolve => { + const tab = gBrowser.selectedTab = gBrowser.addTab(url, { + triggeringPrincipal: Services.scriptSecurityManager.getSystemPrincipal(), + }); + gBrowser.selectedBrowser.addEventListener("load", function () { + info("URL '" + url + "' loading complete"); + resolve(tab); + }, {capture: true, once: true}); + }); +} + +</script> +</body> +</html> diff --git a/devtools/shared/webconsole/test/chrome/test_console_styling.html b/devtools/shared/webconsole/test/chrome/test_console_styling.html new file mode 100644 index 0000000000..4a501dd22e --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/test_console_styling.html @@ -0,0 +1,128 @@ +<!DOCTYPE HTML> +<html lang="en"> +<head> + <meta charset="utf8"> + <title>Test for console.log styling with %c</title> + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="common.js"></script> + <!-- Any copyright is dedicated to the Public Domain. + - http://creativecommons.org/publicdomain/zero/1.0/ --> +</head> +<body> +<p>Test for console.log styling with %c</p> + +<script class="testbody" type="text/javascript"> +"use strict"; + +SimpleTest.waitForExplicitFinish(); + +let expectedConsoleCalls = []; + +function doConsoleCalls(aState) +{ + top.console.log("%cOne formatter with no styles"); + top.console.log("%cOne formatter", "color: red"); + top.console.log("%cTwo formatters%cEach with an arg", + "color: red", "background: red"); + top.console.log("%c%cTwo formatters next to each other", + "color: red", "background: red"); + top.console.log("%c%c%cThree formatters next to each other", + "color: red", "background: red", "font-size: 150%"); + top.console.log("%c%cTwo formatters%cWith a third separated", + "color: red", "background: red", "font-size: 150%"); + top.console.log("%cOne formatter", "color: red", + "Second arg with no styles"); + top.console.log("%cOne formatter", "color: red", + "%cSecond formatter is ignored", "background: blue") + + expectedConsoleCalls = [ + { + level: "log", + styles: /^$/, + arguments: ["%cOne formatter with no styles"], + }, + { + level: "log", + styles: /^color: red$/, + arguments: ["One formatter"], + }, + { + level: "log", + styles: /^color: red,background: red$/, + arguments: ["Two formatters", "Each with an arg"], + }, + { + level: "log", + styles: /^background: red$/, + arguments: ["Two formatters next to each other"], + }, + { + level: "log", + styles: /^font-size: 150%$/, + arguments: ["Three formatters next to each other"], + }, + { + level: "log", + styles: /^background: red,font-size: 150%$/, + arguments: ["Two formatters", "With a third separated"], + }, + { + level: "log", + styles: /^color: red$/, + arguments: ["One formatter", "Second arg with no styles"], + }, + { + level: "log", + styles: /^color: red$/, + arguments: ["One formatter", + "%cSecond formatter is ignored", + "background: blue"], + }, + ]; +} + +async function startTest() +{ + removeEventListener("load", startTest); + + const {state, response} = await attachConsoleToTab(["ConsoleAPI"]); + onAttach(state, response); +} + +function onAttach(aState, aResponse) +{ + onConsoleAPICall = onConsoleAPICall.bind(null, aState); + aState.webConsoleFront.on("consoleAPICall", onConsoleAPICall); + doConsoleCalls(aState.actor); +} + +let consoleCalls = []; + +function onConsoleAPICall(aState, aPacket) +{ + info("received message level: " + aPacket.message.level); + + consoleCalls.push(aPacket.message); + if (consoleCalls.length != expectedConsoleCalls.length) { + return; + } + + aState.webConsoleFront.off("consoleAPICall", onConsoleAPICall); + + expectedConsoleCalls.forEach(function(aMessage, aIndex) { + info("checking received console call #" + aIndex); + checkConsoleAPICall(consoleCalls[aIndex], expectedConsoleCalls[aIndex]); + }); + + + consoleCalls = []; + + closeDebugger(aState, function() { + SimpleTest.finish(); + }); +} + +addEventListener("load", startTest); +</script> +</body> +</html> diff --git a/devtools/shared/webconsole/test/chrome/test_console_timestamp.html b/devtools/shared/webconsole/test/chrome/test_console_timestamp.html new file mode 100644 index 0000000000..a64de0d7b2 --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/test_console_timestamp.html @@ -0,0 +1,48 @@ +<!DOCTYPE HTML> +<html> +<head> + <meta charset="utf-8"> + <title>Test for console.group styling with %c</title> + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="common.js"></script> + <!-- Any copyright is dedicated to the Public Domain. + - http://creativecommons.org/publicdomain/zero/1.0/ --> + <script> +"use strict"; + +window.onload = async function () { + SimpleTest.waitForExplicitFinish(); + let state; + try { + state = (await attachConsole(["ConsoleAPI"])).state; + const consoleFront = state.webConsoleFront; + + info("Testing console.log packet timestamp correctness"); + const clientLogTimestamp = Date.now(); + const packet = await consoleAPICall(consoleFront, + () => top.console.log("test")); + const packetReceivedTimestamp = Date.now(); + + const {timeStamp} = packet.message; + ok(clientLogTimestamp <= timeStamp && timeStamp <= packetReceivedTimestamp, + "console.log message timestamp is between the expected time range " + + `(${clientLogTimestamp} <= ${timeStamp} <= ${packetReceivedTimestamp})` + ); + } catch (e) { + ok(false, `Error thrown: ${e.message}`); + } + + await closeDebugger(state); + SimpleTest.finish(); +}; + + </script> +</head> +<body> + <p id="display"></p> + <div id="content" style="display: none"> + </div> + <pre id="test"> + </pre> +</body> +</html> diff --git a/devtools/shared/webconsole/test/chrome/test_console_worker.html b/devtools/shared/webconsole/test/chrome/test_console_worker.html new file mode 100644 index 0000000000..fe89d3dbed --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/test_console_worker.html @@ -0,0 +1,79 @@ +<!DOCTYPE HTML> +<html lang="en"> +<head> + <meta charset="utf8"> + <title>Test for the Console API and Workers</title> + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="common.js"></script> + <!-- Any copyright is dedicated to the Public Domain. + - http://creativecommons.org/publicdomain/zero/1.0/ --> +</head> +<body> +<p>Test for the Console API and Workers</p> + +<script class="testbody" type="text/javascript"> +"use strict"; + +SimpleTest.waitForExplicitFinish(); + +const expectedCachedConsoleCalls = [{ + message:{ + level: "log", + filename: /console-test-worker/, + arguments: ['Log from worker init'], + } +}]; + +const expectedConsoleAPICalls = [{ + message: { + level: "log", + arguments: ['Log was requested from worker'], + } +}]; + +window.onload = async function () { + const {state} = await attachConsoleToWorker(["ConsoleAPI"]); + + await testCachedMessages(state); + await testConsoleAPI(state); + + closeDebugger(state, function() { + SimpleTest.finish(); + }); +}; + +const testCachedMessages = async function (state) { + info("testCachedMessages entered"); + return new Promise(resolve => { + const onCachedConsoleAPI = (response) => { + const consoleCalls = response.messages; + + info('Received cached response. Checking console calls.'); + checkConsoleAPICalls(consoleCalls, expectedCachedConsoleCalls); + resolve(); + }; + state.webConsoleFront.getCachedMessages(["ConsoleAPI"]).then(onCachedConsoleAPI); + }) +}; + +const testConsoleAPI = async function (state) { + info("testConsoleAPI entered"); + return new Promise(resolve => { + const onConsoleAPICall = (packet) => { + info("received message level: " + packet.message.level); + checkConsoleAPICalls([packet], expectedConsoleAPICalls); + state.webConsoleFront.off("consoleAPICall", onConsoleAPICall); + resolve(); + } + + info("testConsoleAPI: adding listener for consoleAPICall"); + state.webConsoleFront.on("consoleAPICall", onConsoleAPICall); + + state.webConsoleFront.evaluateJSAsync("console.log('Log was requested from worker')", + () => { }); + }); +}; + +</script> +</body> +</html> diff --git a/devtools/shared/webconsole/test/chrome/test_consoleapi.html b/devtools/shared/webconsole/test/chrome/test_consoleapi.html new file mode 100644 index 0000000000..7030728de4 --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/test_consoleapi.html @@ -0,0 +1,234 @@ +<!DOCTYPE HTML> +<html lang="en"> +<head> + <meta charset="utf8"> + <title>Test for the Console API</title> + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="common.js"></script> + <!-- Any copyright is dedicated to the Public Domain. + - http://creativecommons.org/publicdomain/zero/1.0/ --> +</head> +<body> +<p>Test for the Console API</p> + +<script class="testbody" type="text/javascript"> +"use strict"; + +SimpleTest.waitForExplicitFinish(); + +let expectedConsoleCalls = []; + +function doConsoleCalls(aState) +{ + const longString = (new Array(DevToolsServer.LONG_STRING_LENGTH + 2)).join("a"); + + top.console.log("foobarBaz-log", undefined); + + top.console.log("Float from not a number: %f", "foo"); + top.console.log("Float from string: %f", "1.2"); + top.console.log("Float from number: %f", 1.3); + + top.console.info("foobarBaz-info", null); + top.console.warn("foobarBaz-warn", top.document.documentElement); + top.console.debug(null); + top.console.trace(); + top.console.dir(top.document, top.location); + top.console.log("foo", longString); + + const sandbox = new Cu.Sandbox(null, { invisibleToDebugger: true }); + const sandboxObj = sandbox.eval("new Object"); + top.console.log(sandboxObj); + + function fromAsmJS() { + top.console.error("foobarBaz-asmjs-error", undefined); + } + + (function(global, foreign) { + "use asm"; + function inAsmJS2() { foreign.fromAsmJS() } + function inAsmJS1() { inAsmJS2() } + return inAsmJS1 + })(null, { fromAsmJS:fromAsmJS })(); + + expectedConsoleCalls = [ + { + level: "log", + filename: /test_consoleapi/, + functionName: "doConsoleCalls", + timeStamp: /^\d+$/, + arguments: ["foobarBaz-log", { type: "undefined" }], + }, + { + level: "log", + arguments: ["Float from not a number: NaN"], + }, + { + level: "log", + arguments: ["Float from string: 1.200000"], + }, + { + level: "log", + arguments: ["Float from number: 1.300000"], + }, + { + level: "info", + filename: /test_consoleapi/, + functionName: "doConsoleCalls", + timeStamp: /^\d+$/, + arguments: ["foobarBaz-info", { type: "null" }], + }, + { + level: "warn", + filename: /test_consoleapi/, + functionName: "doConsoleCalls", + timeStamp: /^\d+$/, + arguments: ["foobarBaz-warn", { type: "object", actor: /[a-z]/ }], + }, + { + level: "debug", + filename: /test_consoleapi/, + functionName: "doConsoleCalls", + timeStamp: /^\d+$/, + arguments: [{ type: "null" }], + }, + { + level: "trace", + filename: /test_consoleapi/, + functionName: "doConsoleCalls", + timeStamp: /^\d+$/, + stacktrace: [ + { + filename: /test_consoleapi/, + functionName: "doConsoleCalls", + }, + { + filename: /test_consoleapi/, + functionName: "onAttach", + }, + ], + }, + { + level: "dir", + filename: /test_consoleapi/, + functionName: "doConsoleCalls", + timeStamp: /^\d+$/, + arguments: [ + { + type: "object", + actor: /[a-z]/, + class: "HTMLDocument", + }, + { + type: "object", + actor: /[a-z]/, + class: "Location", + } + ], + }, + { + level: "log", + filename: /test_consoleapi/, + functionName: "doConsoleCalls", + timeStamp: /^\d+$/, + arguments: [ + "foo", + { + type: "longString", + initial: longString.substring(0, + DevToolsServer.LONG_STRING_INITIAL_LENGTH), + length: longString.length, + actor: /[a-z]/, + }, + ], + }, + { + level: "log", + filename: /test_consoleapi/, + functionName: "doConsoleCalls", + timeStamp: /^\d+$/, + arguments: [ + { + type: "object", + actor: /[a-z]/, + class: "InvisibleToDebugger: Object", + }, + ], + }, + { + level: "error", + filename: /test_consoleapi/, + functionName: "fromAsmJS", + timeStamp: /^\d+$/, + arguments: ["foobarBaz-asmjs-error", { type: "undefined" }], + + stacktrace: [ + { + filename: /test_consoleapi/, + functionName: "fromAsmJS", + }, + { + filename: /test_consoleapi/, + functionName: "inAsmJS2", + }, + { + filename: /test_consoleapi/, + functionName: "inAsmJS1", + }, + { + filename: /test_consoleapi/, + functionName: "doConsoleCalls", + }, + { + filename: /test_consoleapi/, + functionName: "onAttach", + }, + ], + }, + ]; +} + +async function startTest() +{ + removeEventListener("load", startTest); + + const {state, response} = await attachConsoleToTab(["ConsoleAPI"]); + onAttach(state, response); +} + +function onAttach(aState, aResponse) +{ + onConsoleAPICall = onConsoleAPICall.bind(null, aState); + aState.webConsoleFront.on("consoleAPICall", onConsoleAPICall); + doConsoleCalls(aState.actor); +} + +let consoleCalls = []; + +function onConsoleAPICall(aState, aPacket) +{ + info("received message level: " + aPacket.message.level); + + consoleCalls.push(aPacket.message); + if (consoleCalls.length != expectedConsoleCalls.length) { + return; + } + + aState.webConsoleFront.off("consoleAPICall", onConsoleAPICall); + + expectedConsoleCalls.forEach(function(aMessage, aIndex) { + info("checking received console call #" + aIndex); + checkConsoleAPICall(consoleCalls[aIndex], expectedConsoleCalls[aIndex]); + }); + + + consoleCalls = []; + + closeDebugger(aState, function() { + SimpleTest.finish(); + }); +} + +addEventListener("load", startTest); +</script> +</body> +</html> diff --git a/devtools/shared/webconsole/test/chrome/test_consoleapi_innerID.html b/devtools/shared/webconsole/test/chrome/test_consoleapi_innerID.html new file mode 100644 index 0000000000..19b92e1d0d --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/test_consoleapi_innerID.html @@ -0,0 +1,164 @@ +<!DOCTYPE HTML> +<html lang="en"> +<head> + <meta charset="utf8"> + <title>Test for the innerID property of the Console API</title> + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="common.js"></script> + <!-- Any copyright is dedicated to the Public Domain. + - http://creativecommons.org/publicdomain/zero/1.0/ --> +</head> +<body> +<p>Test for the Console API</p> + +<script class="testbody" type="text/javascript"> +"use strict"; + +SimpleTest.waitForExplicitFinish(); + +let expectedConsoleCalls = []; + +function doConsoleCalls(aState) +{ + const { ConsoleAPI } = ChromeUtils.import("resource://gre/modules/Console.jsm"); + const console = new ConsoleAPI({ + innerID: window.windowGlobalChild.innerWindowId + }); + + const longString = (new Array(DevToolsServer.LONG_STRING_LENGTH + 2)).join("a"); + + console.log("foobarBaz-log", undefined); + console.info("foobarBaz-info", null); + console.warn("foobarBaz-warn", top.document.documentElement); + console.debug(null); + console.trace(); + console.dir(top.document, top.location); + console.log("foo", longString); + + expectedConsoleCalls = [ + { + level: "log", + filename: /test_consoleapi/, + functionName: "doConsoleCalls", + timeStamp: /^\d+$/, + arguments: ["foobarBaz-log", { type: "undefined" }], + }, + { + level: "info", + filename: /test_consoleapi/, + functionName: "doConsoleCalls", + timeStamp: /^\d+$/, + arguments: ["foobarBaz-info", { type: "null" }], + }, + { + level: "warn", + filename: /test_consoleapi/, + functionName: "doConsoleCalls", + timeStamp: /^\d+$/, + arguments: ["foobarBaz-warn", { type: "object", actor: /[a-z]/ }], + }, + { + level: "debug", + filename: /test_consoleapi/, + functionName: "doConsoleCalls", + timeStamp: /^\d+$/, + arguments: [{ type: "null" }], + }, + { + level: "trace", + filename: /test_consoleapi/, + functionName: "doConsoleCalls", + timeStamp: /^\d+$/, + stacktrace: [ + { + filename: /test_consoleapi/, + functionName: "doConsoleCalls", + }, + { + filename: /test_consoleapi/, + functionName: "onAttach", + }, + ], + }, + { + level: "dir", + filename: /test_consoleapi/, + functionName: "doConsoleCalls", + timeStamp: /^\d+$/, + arguments: [ + { + type: "object", + actor: /[a-z]/, + class: "HTMLDocument", + }, + { + type: "object", + actor: /[a-z]/, + class: "Location", + } + ], + }, + { + level: "log", + filename: /test_consoleapi/, + functionName: "doConsoleCalls", + timeStamp: /^\d+$/, + arguments: [ + "foo", + { + type: "longString", + initial: longString.substring(0, + DevToolsServer.LONG_STRING_INITIAL_LENGTH), + length: longString.length, + actor: /[a-z]/, + }, + ], + }, + ]; +} + +async function startTest() +{ + removeEventListener("load", startTest); + + const {state} = await attachConsoleToTab(["ConsoleAPI"]); + onAttach(state); +} + +function onAttach(aState) +{ + onConsoleAPICall = onConsoleAPICall.bind(null, aState); + aState.webConsoleFront.on("consoleAPICall", onConsoleAPICall); + doConsoleCalls(aState.actor); +} + +let consoleCalls = []; + +function onConsoleAPICall(aState, aPacket) +{ + info("received message level: " + aPacket.message.level); + + consoleCalls.push(aPacket.message); + if (consoleCalls.length != expectedConsoleCalls.length) { + return; + } + + aState.webConsoleFront.off("consoleAPICall", onConsoleAPICall); + + expectedConsoleCalls.forEach(function(aMessage, aIndex) { + info("checking received console call #" + aIndex); + checkConsoleAPICall(consoleCalls[aIndex], expectedConsoleCalls[aIndex]); + }); + + + consoleCalls = []; + + closeDebugger(aState, function() { + SimpleTest.finish(); + }); +} + +addEventListener("load", startTest); +</script> +</body> +</html> diff --git a/devtools/shared/webconsole/test/chrome/test_file_uri.html b/devtools/shared/webconsole/test/chrome/test_file_uri.html new file mode 100644 index 0000000000..23eb45b9fe --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/test_file_uri.html @@ -0,0 +1,110 @@ +<!DOCTYPE HTML> +<html lang="en"> +<head> + <meta charset="utf8"> + <title>Test for file activity tracking</title> + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="common.js"></script> + <!-- Any copyright is dedicated to the Public Domain. + - http://creativecommons.org/publicdomain/zero/1.0/ --> +</head> +<body> +<p>Test for file activity tracking</p> + +<script class="testbody" type="text/javascript"> +"use strict"; + +SimpleTest.waitForExplicitFinish(); + +const {NetUtil} = ChromeUtils.import("resource://gre/modules/NetUtil.jsm"); +const {FileUtils} = ChromeUtils.import("resource://gre/modules/FileUtils.jsm"); + +let gState; +let gTmpFile; + +function doFileActivity() +{ + info("doFileActivity"); + const fileContent = "<p>hello world from bug 798764"; + + gTmpFile = FileUtils.getFile("TmpD", ["bug798764.html"]); + gTmpFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE); + + const fout = FileUtils.openSafeFileOutputStream(gTmpFile, + FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE | FileUtils.MODE_TRUNCATE); + + const converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]. + createInstance(Ci.nsIScriptableUnicodeConverter); + converter.charset = "UTF-8"; + const fileContentStream = converter.convertToInputStream(fileContent); + + NetUtil.asyncCopy(fileContentStream, fout, addIframe); +} + +function addIframe(aStatus) +{ + ok(Components.isSuccessCode(aStatus), + "the temporary file was saved successfully"); + + const iframe = document.createElement("iframe"); + iframe.src = NetUtil.newURI(gTmpFile).spec; + document.body.appendChild(iframe); +} + +async function startTest() +{ + removeEventListener("load", startTest); + + const {state} = await attachConsole(["FileActivity"]); + onAttach(state); +} + +function onAttach(aState) +{ + gState = aState; + gState.webConsoleFront.on("fileActivity", onFileActivity); + doFileActivity(); +} + +function onFileActivity(aPacket) +{ + gState.webConsoleFront.off("fileActivity", onFileActivity); + + info("aPacket.uri: " + aPacket.uri); + ok(/\bbug798764\b.*\.html$/.test(aPacket.uri), "file URI match"); + + testEnd(); +} + +function testEnd() +{ + if (gTmpFile) { + SimpleTest.executeSoon(function() { + try { + gTmpFile.remove(false); + } + catch (ex) { + if (ex.name != "NS_ERROR_FILE_IS_LOCKED") { + throw ex; + } + // Sometimes remove() throws because the file is not unlocked soon + // enough. + } + gTmpFile = null; + }); + } + + if (gState) { + closeDebugger(gState, function() { + gState = null; + SimpleTest.finish(); + }); + } else { + SimpleTest.finish(); + } +} + +addEventListener("load", startTest); +</script> +</body> +</html> diff --git a/devtools/shared/webconsole/test/chrome/test_jsterm.html b/devtools/shared/webconsole/test/chrome/test_jsterm.html new file mode 100644 index 0000000000..65e7b519d8 --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/test_jsterm.html @@ -0,0 +1,273 @@ +<!DOCTYPE HTML> +<html lang="en"> +<head> + <meta charset="utf8"> + <title>Test for JavaScript terminal functionality</title> + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="common.js"></script> + <!-- Any copyright is dedicated to the Public Domain. + - http://creativecommons.org/publicdomain/zero/1.0/ --> +</head> +<body> +<p>Test for JavaScript terminal functionality</p> + +<iframe id="content-iframe" src="http://example.com/chrome/devtools/shared/webconsole/test/chrome/sandboxed_iframe.html"></iframe> + +<script class="testbody" type="text/javascript"> +"use strict"; + +SimpleTest.waitForExplicitFinish(); + +let gState; + +const {MAX_AUTOCOMPLETE_ATTEMPTS,MAX_AUTOCOMPLETIONS} = require("devtools/shared/webconsole/js-property-provider"); + + +function evaluateJS(input, options = {}) { + return gState.webConsoleFront.evaluateJSAsync(input, options); +} + +async function startTest() +{ + removeEventListener("load", startTest); + + const {state} = await attachConsoleToTab(["PageError"]); + onAttach(state); +} + +function onAttach(aState) +{ + top.foobarObject = Object.create(null); + top.foobarObject.foo = 1; + top.foobarObject.foobar = 2; + top.foobarObject.foobaz = 3; + top.foobarObject.omg = 4; + top.foobarObject.omgfoo = 5; + top.foobarObject.strfoo = "foobarz"; + top.foobarObject.omgstr = "foobarz" + + (new Array(DevToolsServer.LONG_STRING_LENGTH * 2)).join("abb"); + + top.largeObject1 = Object.create(null); + for (let i = 0; i < MAX_AUTOCOMPLETE_ATTEMPTS + 1; i++) { + top.largeObject1['a' + i] = i; + } + + top.largeObject2 = Object.create(null); + for (let i = 0; i < MAX_AUTOCOMPLETIONS * 2; i++) { + top.largeObject2['a' + i] = i; + } + + gState = aState; + + const tests = [doSimpleEval, doWindowEval, doEvalWithException, + doEvalWithHelper, doEvalString, doEvalLongString, + doEvalWithBinding, doEvalWithBindingFrame, + forceLexicalInit]; + + runTests(tests, testEnd); +} + +async function doSimpleEval() { + info("test eval '2+2'"); + const response = await evaluateJS("2+2"); + checkObject(response, { + input: "2+2", + result: 4, + }); + + ok(!response.exception, "no eval exception"); + ok(!response.helperResult, "no helper result"); + + nextTest(); +} + +async function doWindowEval() { + info("test eval 'document'"); + const response = await evaluateJS("document"); + checkObject(response, { + input: "document", + result: { + type: "object", + class: "HTMLDocument", + actor: /[a-z]/, + }, + }); + + ok(!response.exception, "no eval exception"); + ok(!response.helperResult, "no helper result"); + + nextTest(); +} + +async function doEvalWithException() { + info("test eval with exception"); + const response = await evaluateJS("window.doTheImpossible()"); + checkObject(response, { + input: "window.doTheImpossible()", + result: { + type: "undefined", + }, + exceptionMessage: /doTheImpossible/, + }); + + ok(response.exception, "js eval exception"); + ok(!response.helperResult, "no helper result"); + + nextTest(); +} + +async function doEvalWithHelper() { + info("test eval with helper"); + const response = await evaluateJS("clear()"); + checkObject(response, { + input: "clear()", + result: { + type: "undefined", + }, + helperResult: { type: "clearOutput" }, + }); + + ok(!response.exception, "no eval exception"); + + nextTest(); +} + +async function doEvalString() { + const response = await evaluateJS("window.foobarObject.strfoo"); + checkObject(response, { + input: "window.foobarObject.strfoo", + result: "foobarz", + }); + + nextTest(); +} + +async function doEvalLongString() { + const response = await evaluateJS("window.foobarObject.omgstr"); + const str = top.foobarObject.omgstr; + const initial = str.substring(0, DevToolsServer.LONG_STRING_INITIAL_LENGTH); + + checkObject(response, { + input: "window.foobarObject.omgstr", + result: { + type: "longString", + initial: initial, + length: str.length, + }, + }); + + nextTest(); +} + +async function doEvalWithBinding() { + const response = await evaluateJS("document;"); + const documentActor = response.result.actorID; + + info("running a command with _self as document using selectedObjectActor"); + const selectedObjectSame = await evaluateJS("_self === document", { + selectedObjectActor: documentActor + }); + checkObject(selectedObjectSame, { + result: true + }); + + nextTest(); +} + +async function doEvalWithBindingFrame() { + const frameWin = top.document.querySelector("iframe").contentWindow; + frameWin.fooFrame = { bar: 1 }; + + const response = await evaluateJS( + "document.querySelector('iframe').contentWindow.fooFrame" + ); + const iframeObjectActor = response.result.actorID; + ok(iframeObjectActor, "There is an actor associated with the response"); + + await evaluateJS("this.temp1 = _self;", { + selectedObjectActor: iframeObjectActor + }); + ok(top.temp1 && top.temp1.bar === 1, + "Global matches the top global with bindObjectActor"); + ok(!frameWin.temp1, + "Global doesn't match the object's global with bindObjectActor"); + + nextTest() +} + +async function forceLexicalInit() { + info("test that failed let/const bindings are initialized to undefined"); + + const testData = [ + { + stmt: "let foopie = wubbalubadubdub", + vars: ["foopie"] + }, + { + stmt: "let {z, w={n}=null} = {}", + vars: ["z", "w"] + }, + { + stmt: "let [a, b, c] = null", + vars: ["a", "b", "c"] + }, + { + stmt: "const nein1 = rofl, nein2 = copter", + vars: ["nein1", "nein2"] + }, + { + stmt: "const {ha} = null", + vars: ["ha"] + }, + { + stmt: "const [haw=[lame]=null] = []", + vars: ["haw"] + }, + { + stmt: "const [rawr, wat=[lame]=null] = []", + vars: ["rawr", "haw"] + }, + { + stmt: "let {zzz: xyz=99, zwz: wb} = nexistepas()", + vars: ["xyz", "wb"] + }, + { + stmt: "let {c3pdoh=101} = null", + vars: ["c3pdoh"] + } + ]; + + for (const data of testData) { + const response = await evaluateJS(data.stmt); + checkObject(response, { + input: data.stmt, + result: undefined, + }); + ok(response.exception, "expected exception"); + for (const varName of data.vars) { + const response2 = await evaluateJS(varName); + checkObject(response2, { + input: varName, + result: undefined, + }); + ok(!response2.exception, "unexpected exception"); + } + } + + nextTest(); +} + +function testEnd() +{ + // If this is the first run, reload the page and do it again. + // Otherwise, end the test. + closeDebugger(gState, function() { + gState = null; + SimpleTest.finish(); + }); +} + +addEventListener("load", startTest); +</script> +</body> +</html> diff --git a/devtools/shared/webconsole/test/chrome/test_jsterm_autocomplete.html b/devtools/shared/webconsole/test/chrome/test_jsterm_autocomplete.html new file mode 100644 index 0000000000..8f250d84a7 --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/test_jsterm_autocomplete.html @@ -0,0 +1,626 @@ +<!DOCTYPE HTML> +<html lang="en"> +<head> + <meta charset="utf8"> + <title>Test for JavaScript terminal functionality</title> + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="common.js"></script> + <!-- Any copyright is dedicated to the Public Domain. + - http://creativecommons.org/publicdomain/zero/1.0/ --> +</head> +<body> +<p>Test for JavaScript terminal autocomplete functionality</p> + +<script class="testbody" type="text/javascript"> + "use strict"; + + SimpleTest.waitForExplicitFinish(); + const { + MAX_AUTOCOMPLETE_ATTEMPTS, + MAX_AUTOCOMPLETIONS + } = require("devtools/shared/webconsole/js-property-provider"); + const RESERVED_JS_KEYWORDS = require("devtools/shared/webconsole/reserved-js-words"); + + + addEventListener("load", startTest); + + async function startTest() { + // First run the tests with a tab as a target. + let {state} = await attachConsoleToTab(["PageError"]); + await performTests({state, isWorker: false}); + + // Then run the tests with a worker as a target. + state = (await attachConsoleToWorker(["PageError"])).state; + await performTests({state, isWorker: true}); + + SimpleTest.finish(); + } + + async function performTests({state, isWorker}) { + // Set up the global variables needed to test autocompletion in the target. + const script = ` + // This is for workers so autocomplete acts the same + if (!this.window) { + window = this; + } + + window.foobarObject = Object.create(null); + window.foobarObject.foo = 1; + window.foobarObject.foobar = 2; + window.foobarObject.foobaz = 3; + window.foobarObject.omg = 4; + window.foobarObject.omgfoo = 5; + window.foobarObject.strfoo = "foobarz"; + window.foobarObject.omgstr = "foobarz" + + (new Array(${DevToolsServer.LONG_STRING_LENGTH})).join("abb"); + window.largeObject1 = Object.create(null); + for (let i = 0; i < ${MAX_AUTOCOMPLETE_ATTEMPTS + 1}; i++) { + window.largeObject1['a' + i] = i; + } + + window.largeObject2 = Object.create(null); + for (let i = 0; i < ${MAX_AUTOCOMPLETIONS * 2}; i++) { + window.largeObject2['a' + i] = i; + } + + window.proxy1 = new Proxy({foo: 1}, { + getPrototypeOf() { throw new Error() } + }); + window.proxy2 = new Proxy(Object.create(Object.create(null, {foo:{}})), { + ownKeys() { throw new Error() } + }); + window.emojiObject = Object.create(null); + window.emojiObject["😎"] = "😎"; + + window.insensitiveTestCase = Object.create(null, Object.getOwnPropertyDescriptors({ + PROP: "", + Prop: "", + prop: "", + PRÖP: "", + pröp: "", + })); + + window.elementAccessTestCase = Object.create(null, Object.getOwnPropertyDescriptors({ + bar: "", + BAR: "", + dataTest: "", + "data-test": "", + 'da"ta"test': "", + 'da\`ta\`test': "", + "da'ta'test": "", + })); + + window.varify = true; + `; + await state.webConsoleFront.evaluateJSAsync(script); + + const tests = [ + doAutocomplete1, + doAutocomplete2, + doAutocomplete3, + doAutocomplete4, + doAutocompleteLarge1, + doAutocompleteLarge2, + doAutocompleteProxyThrowsPrototype, + doAutocompleteProxyThrowsOwnKeys, + doAutocompleteDotSurroundedBySpaces, + doAutocompleteAfterOr, + doInsensitiveAutocomplete, + doElementAccessAutocomplete, + doAutocompleteAfterOperator, + dontAutocompleteAfterDeclaration, + doKeywordsAutocomplete, + dontAutocomplete, + ]; + + if (!isWorker) { + // `Cu` is not defined in workers, then we can't test `Cu.Sandbox` + tests.push(doAutocompleteSandbox); + // Some cases are handled in worker context because we can't use parser.js. + // See Bug 1507181. + tests.push( + doAutocompleteArray, + doAutocompleteString, + doAutocompleteCommands, + doAutocompleteBracketSurroundedBySpaces, + ); + } + + for (const test of tests) { + await test(state.webConsoleFront); + } + + // Null out proxy1 and proxy2: the proxy handlers use scripted functions + // that can keep the debugger sandbox alive longer than necessary via their + // environment chain (due to the webconsole helper functions defined there). + await state.webConsoleFront.evaluateJSAsync(`this.proxy1 = null; this.proxy2 = null;`); + + await closeDebugger(state); + } + + async function doAutocomplete1(webConsoleFront) { + info("test autocomplete for 'window.foo'"); + const response = await webConsoleFront.autocomplete("window.foo"); + const matches = response.matches; + + is(response.matchProp, "foo", "matchProp"); + is(matches.length, 1, "matches.length"); + is(matches[0], "foobarObject", "matches[0]"); + } + + async function doAutocomplete2(webConsoleFront) { + info("test autocomplete for 'window.foobarObject.'"); + const response = await webConsoleFront.autocomplete("window.foobarObject."); + const matches = response.matches; + + ok(!response.matchProp, "matchProp"); + is(matches.length, 7, "matches.length"); + checkObject(matches, + ["foo", "foobar", "foobaz", "omg", "omgfoo", "omgstr", "strfoo"]); + } + + async function doAutocomplete3(webConsoleFront) { + // Check that completion suggestions are offered inside the string. + info("test autocomplete for 'dump(window.foobarObject.)'"); + const response = await webConsoleFront.autocomplete("dump(window.foobarObject.)", 25); + const matches = response.matches; + + ok(!response.matchProp, "matchProp"); + is(matches.length, 7, "matches.length"); + checkObject(matches, + ["foo", "foobar", "foobaz", "omg", "omgfoo", "omgstr", "strfoo"]); + } + + async function doAutocomplete4(webConsoleFront) { + // Check that completion requests can have no suggestions. + info("test autocomplete for 'dump(window.foobarObject.)'"); + const response = await webConsoleFront.autocomplete("dump(window.foobarObject.)"); + ok(!response.matchProp, "matchProp"); + is(response.matches, null, "matches is null"); + } + + async function doAutocompleteLarge1(webConsoleFront) { + // Check that completion requests with too large objects will + // have no suggestions. + info("test autocomplete for 'window.largeObject1.'"); + const response = await webConsoleFront.autocomplete("window.largeObject1."); + ok(!response.matchProp, "matchProp"); + info (response.matches.join("|")); + is(response.matches.length, 0, "Bailed out with too many properties"); + } + + async function doAutocompleteLarge2(webConsoleFront) { + // Check that completion requests with pretty large objects will + // have MAX_AUTOCOMPLETIONS suggestions + info("test autocomplete for 'window.largeObject2.'"); + const response = await webConsoleFront.autocomplete("window.largeObject2."); + ok(!response.matchProp, "matchProp"); + is(response.matches.length, MAX_AUTOCOMPLETIONS, "matches.length is MAX_AUTOCOMPLETIONS"); + } + + async function doAutocompleteProxyThrowsPrototype(webConsoleFront) { + // Check that completion provides own properties even if [[GetPrototypeOf]] throws. + info("test autocomplete for 'window.proxy1.'"); + const response = await webConsoleFront.autocomplete("window.proxy1."); + ok(!response.matchProp, "matchProp"); + is(response.matches.length, 1, "matches.length"); + checkObject(response.matches, ["foo"]); + } + + async function doAutocompleteProxyThrowsOwnKeys(webConsoleFront) { + // Check that completion provides inherited properties even if [[OwnPropertyKeys]] throws. + info("test autocomplete for 'window.proxy2.'"); + const response = await webConsoleFront.autocomplete("window.proxy2."); + ok(!response.matchProp, "matchProp"); + is(response.matches.length, 1, "matches.length"); + checkObject(response.matches, ["foo"]); + } + + async function doAutocompleteSandbox(webConsoleFront) { + // Check that completion provides inherited properties even if [[OwnPropertyKeys]] throws. + info("test autocomplete for 'Cu.Sandbox.'"); + const response = await webConsoleFront.autocomplete("Cu.Sandbox."); + ok(!response.matchProp, "matchProp"); + const keys = Object.getOwnPropertyNames(Object.prototype).sort(); + is(response.matches.length, keys.length, "matches.length"); + // checkObject(response.matches, keys); + is(response.matches.join(" - "), keys.join(" - ")); + } + + async function doAutocompleteArray(webConsoleFront) { + info("test autocomplete for [1,2,3]"); + const response = await webConsoleFront.autocomplete("[1,2,3]."); + let {matches} = response; + + ok(matches.length > 0, "There are completion results for the array"); + ok(matches.includes("length") && matches.includes("filter"), + "Array autocomplete contains expected results"); + + info("test autocomplete for '[] . '"); + matches = (await webConsoleFront.autocomplete("[] . ")).matches; + ok(matches.length > 1); + ok(matches.includes("length") && matches.includes("filter"), + "Array autocomplete contains expected results"); + ok(!matches.includes("copy"), "Array autocomplete does not contain helpers"); + + info("test autocomplete for '[1,2,3]['"); + matches = (await webConsoleFront.autocomplete("[1,2,3][")).matches; + ok(matches.length > 1); + ok(matches.includes('"length"') && matches.includes('"filter"'), + "Array autocomplete contains expected results, surrounded by quotes"); + + info("test autocomplete for '[1,2,3]['"); + matches = (await webConsoleFront.autocomplete("[1,2,3]['")).matches; + ok(matches.length > 1); + ok(matches.includes("'length'") && matches.includes("'filter'"), + "Array autocomplete contains expected results, surrounded by quotes"); + + info("test autocomplete for '[1,2,3][l"); + matches = (await webConsoleFront.autocomplete("[1,2,3][l")).matches; + ok(matches.length >= 1); + ok(matches.includes('"length"'), + "Array autocomplete contains expected results, surrounded by quotes"); + + info("test autocomplete for '[1,2,3]['l"); + matches = (await webConsoleFront.autocomplete("[1,2,3]['l")).matches; + ok(matches.length >= 1); + ok(matches.includes("'length'"), + "Array autocomplete contains expected results, surrounded by quotes"); + } + + async function doAutocompleteString(webConsoleFront) { + info(`test autocomplete for "foo".`); + const response = await webConsoleFront.autocomplete(`"foo".`); + let {matches} = response; + + ok(matches.length > 0, "There are completion results for the string"); + ok(matches.includes("substr") && matches.includes("trim"), + "String autocomplete contains expected results"); + + info("test autocomplete for `foo`["); + matches = (await webConsoleFront.autocomplete("`foo`[")).matches; + ok(matches.length > 1, "autocomplete string with bracket works"); + ok(matches.includes('"substr"') && matches.includes('"trim"'), + "String autocomplete contains expected results, surrounded by quotes"); + } + + async function doAutocompleteDotSurroundedBySpaces(webConsoleFront) { + info("test autocomplete for 'window.foobarObject\n .'"); + let {matches} = await webConsoleFront.autocomplete("window.foobarObject\n ."); + is(matches.length, 7); + checkObject(matches, + ["foo", "foobar", "foobaz", "omg", "omgfoo", "omgstr", "strfoo"]); + + info("test autocomplete for 'window.foobarObject\n .o'"); + matches = (await webConsoleFront.autocomplete("window.foobarObject\n .o")).matches; + is(matches.length, 3); + checkObject(matches, ["omg", "omgfoo", "omgstr"]); + + info("test autocomplete for 'window.foobarObject\n .\n s'"); + matches = (await webConsoleFront.autocomplete("window.foobarObject\n .\n s")).matches; + is(matches.length, 1); + checkObject(matches, ["strfoo"]); + + info("test autocomplete for 'window.foobarObject\n . '"); + matches = (await webConsoleFront.autocomplete("window.foobarObject\n . ")).matches; + is(matches.length, 7); + checkObject(matches, + ["foo", "foobar", "foobaz", "omg", "omgfoo", "omgstr", "strfoo"]); + + matches = + (await webConsoleFront.autocomplete("window.foobarObject. foo ; window.foo")).matches; + is(matches.length, 1); + checkObject(matches, ["foobarObject"]); + } + + async function doAutocompleteBracketSurroundedBySpaces(webConsoleFront) { + const wrap = (arr, quote = `"`) => arr.map(x => `${quote}${x}${quote}`); + let matches = await getAutocompleteMatches(webConsoleFront, "window.foobarObject\n [") + is(matches.length, 7); + checkObject(matches, + wrap(["foo", "foobar", "foobaz", "omg", "omgfoo", "omgstr", "strfoo"])); + + matches = await getAutocompleteMatches(webConsoleFront, "window.foobarObject\n ['o") + is(matches.length, 3); + checkObject(matches, wrap(["omg", "omgfoo", "omgstr"], "'")); + + matches = await getAutocompleteMatches(webConsoleFront, "window.foobarObject\n [\n s"); + is(matches.length, 1); + checkObject(matches, [`"strfoo"`]); + + matches = await getAutocompleteMatches(webConsoleFront, "window.foobarObject\n [ "); + is(matches.length, 7); + checkObject(matches, + wrap(["foo", "foobar", "foobaz", "omg", "omgfoo", "omgstr", "strfoo"])); + + matches = await getAutocompleteMatches(webConsoleFront, "window.emojiObject [ '"); + is(matches.length, 1); + checkObject(matches, [`'😎'`]); + } + + async function doAutocompleteAfterOr(webConsoleFront) { + info("test autocomplete for 'true || foo'"); + const {matches} = await webConsoleFront.autocomplete("true || foobar"); + is(matches.length, 1, "autocomplete returns expected results"); + is(matches.join("-"), "foobarObject"); + } + + async function doInsensitiveAutocomplete(webConsoleFront) { + info("test autocomplete for 'window.insensitiveTestCase.'"); + let {matches} = await webConsoleFront.autocomplete("window.insensitiveTestCase."); + is(matches.join("-"), "prop-pröp-Prop-PROP-PRÖP", + "autocomplete returns the expected items, in the expected order"); + + info("test autocomplete for 'window.insensitiveTestCase.p'"); + matches = (await webConsoleFront.autocomplete("window.insensitiveTestCase.p")).matches; + is(matches.join("-"), "prop-pröp-Prop-PROP-PRÖP", + "autocomplete is case-insensitive when first letter is lowercased"); + + info("test autocomplete for 'window.insensitiveTestCase.pRoP'"); + matches = (await webConsoleFront.autocomplete("window.insensitiveTestCase.pRoP")).matches; + is(matches.join("-"), "prop-Prop-PROP", + "autocomplete is case-insensitive when first letter is lowercased"); + + info("test autocomplete for 'window.insensitiveTestCase.P'"); + matches = (await webConsoleFront.autocomplete("window.insensitiveTestCase.P")).matches; + is(matches.join("-"), "Prop-PROP-PRÖP", + "autocomplete is case-sensitive when first letter is uppercased"); + + info("test autocomplete for 'window.insensitiveTestCase.PROP'"); + matches = (await webConsoleFront.autocomplete("window.insensitiveTestCase.PROP")).matches; + is(matches.join("-"), "PROP", + "autocomplete is case-sensitive when first letter is uppercased"); + + info("test autocomplete for 'window.insensitiveTestCase.prö'"); + matches = (await webConsoleFront.autocomplete("window.insensitiveTestCase.prö")).matches; + is(matches.join("-"), "pröp-PRÖP", "expected result with lowercase diacritic"); + + info("test autocomplete for 'window.insensitiveTestCase.PRÖ'"); + matches = (await webConsoleFront.autocomplete("window.insensitiveTestCase.PRÖ")).matches; + is(matches.join("-"), "PRÖP", "expected result with uppercase diacritic"); + } + + async function doElementAccessAutocomplete(webConsoleFront) { + info("test autocomplete for 'window.elementAccessTestCase['"); + let res = (await webConsoleFront.autocomplete("window.elementAccessTestCase[")); + is( + res.matches.join("|"), + `"bar"|"da'ta'test"|"da\\"ta\\"test"|"da\`ta\`test"|"data-test"|"dataTest"|"BAR"`, + "autocomplete returns the expected items, wrapped in quotes"); + is(res.isElementAccess, true); + + info("test autocomplete for 'window.elementAccessTestCase[d'"); + res = await webConsoleFront.autocomplete("window.elementAccessTestCase[d"); + is( + res.matches.join("|"), + `"da'ta'test"|"da\\"ta\\"test"|"da\`ta\`test"|"data-test"|"dataTest"`, + "autocomplete returns the expected filtered items"); + is(res.isElementAccess, true); + + info(`test autocomplete for 'window.elementAccessTestCase["d'`); + res = await webConsoleFront.autocomplete(`window.elementAccessTestCase["d`); + is( + res.matches.join("|"), + `"da'ta'test"|"da\\"ta\\"test"|"da\`ta\`test"|"data-test"|"dataTest"`, + "autocomplete returns the expected items, wrapped in quotes"); + is(res.isElementAccess, true); + + info(`test autocomplete for 'window.elementAccessTestCase["data-`); + res = await webConsoleFront.autocomplete(`window.elementAccessTestCase["data-`); + is(res.matches.join("|"), `"data-test"`, + "autocomplete returns the expected items, wrapped in quotes"); + is(res.isElementAccess, true); + + info(`test autocomplete for 'window.elementAccessTestCase['d'`); + res = await webConsoleFront.autocomplete(`window.elementAccessTestCase['d`); + is( + res.matches.join("|"), + `'da"ta"test'|'da\\'ta\\'test'|'da\`ta\`test'|'data-test'|'dataTest'`, + "autocomplete returns the expected items, wrapped in the same quotes the user entered"); + is(res.isElementAccess, true); + + info("test autocomplete for 'window.elementAccessTestCase[`d'"); + res = await webConsoleFront.autocomplete("window.elementAccessTestCase[`d"); + is( + res.matches.join("|"), + "`da'ta'test`|`da\"ta\"test`|`da\\`ta\\`test`|`data-test`|`dataTest`", + "autocomplete returns the expected items, wrapped in the same quotes the user entered"); + is(res.isElementAccess, true); + + info(`test autocomplete for '['`); + res = await webConsoleFront.autocomplete(`[`); + is(res.matches, null, "it does not return anything"); + + info(`test autocomplete for '[1,2,3'`); + res = await webConsoleFront.autocomplete(`[1,2,3`); + is(res.matches, null, "it does not return anything"); + + info(`test autocomplete for '["'`); + res = await webConsoleFront.autocomplete(`["`); + is(res.matches, null, "it does not return anything"); + + info(`test autocomplete for '[;'`); + res = await webConsoleFront.autocomplete(`[;`); + is(res.matches, null, "it does not return anything"); + } + + async function doAutocompleteCommands(webConsoleFront) { + info("test autocomplete for 'c'"); + let matches = (await webConsoleFront.autocomplete("c")).matches; + ok(matches.includes("clear"), "commands are returned"); + + info("test autocomplete for 's'"); + matches = (await webConsoleFront.autocomplete("s")).matches; + is(matches.includes("screenshot"), false, "screenshot is not returned"); + + info("test autocomplete for ':s'"); + matches = (await webConsoleFront.autocomplete(":s")).matches; + is(matches.includes(":screenshot"), true, "screenshot is returned"); + + info("test autocomplete for 'window.c'"); + matches = (await webConsoleFront.autocomplete("window.c")).matches; + ok(!matches.includes("clear"), "commands are not returned"); + + info("test autocomplete for 'window[c'"); + matches = (await webConsoleFront.autocomplete("window[c")).matches; + ok(!matches.includes("clear"), "commands are not returned"); + + info(`test autocomplete for 'window["c'`); + matches = (await webConsoleFront.autocomplete(`window["c`)).matches; + ok(!matches.includes("clear"), "commands are not returned"); + + info(`test autocomplete for 'window["c'`); + matches = (await webConsoleFront.autocomplete(`window["c`)).matches; + ok(!matches.includes("clear"), "commands are not returned"); + + info(`test autocomplete for 'window[";c'`); + matches = (await webConsoleFront.autocomplete(`window[";c`)).matches; + ok(!matches.includes("clear"), "commands are not returned"); + + info(`test autocomplete for 'window[;c'`); + matches = (await webConsoleFront.autocomplete(`window[;c`)).matches; + ok(!matches.includes("clear"), "commands are not returned"); + } + + async function doAutocompleteAfterOperator(webConsoleFront) { + const inputs = [ + "true;foob", + "true,foob", + "({key:foob", + "a=foob", + "if(a<foob", + "if(a>foob", + "1+foob", + "1-foob", + "++foob", + "--foob", + "1*foob", + "2**foob", + "1/foob", + "1%foob", + "1|foob", + "1&foob", + "1^foob", + "~foob", + "1<<foob", + "1>>foob", + "1>>>foob", + "false||foob", + "false&&foob", + "x=true?foob", + "x=false?1:foob", + "!foob", + "false??foob", + ]; + + for (const input of inputs) { + info(`test autocomplete for "${input}"`); + const matches = (await webConsoleFront.autocomplete(input)).matches; + ok(matches.includes("foobarObject"), `Expected autocomplete result for ${input}"`); + } + } + + async function dontAutocompleteAfterDeclaration(webConsoleFront) { + info("test autocomplete for 'var win'"); + let matches = (await webConsoleFront.autocomplete("var win")).matches; + is(matches, null, "no autocompletion on a var declaration"); + + info("test autocomplete for 'const win'"); + matches = (await webConsoleFront.autocomplete("const win")).matches; + is(matches, null, "no autocompletion on a const declaration"); + + info("test autocomplete for 'let win'"); + matches = (await webConsoleFront.autocomplete("let win")).matches; + is(matches, null, "no autocompletion on a let declaration"); + + info("test autocomplete for 'function win'"); + matches = (await webConsoleFront.autocomplete("function win")).matches; + is(matches, null, "no autocompletion on a function declaration"); + + info("test autocomplete for 'class win'"); + matches = (await webConsoleFront.autocomplete("class win")).matches; + is(matches, null, "no autocompletion on a class declaration"); + + info("test autocomplete for 'const win = win'"); + matches = (await webConsoleFront.autocomplete("const win = win")).matches; + ok(matches.includes("window"), "autocompletion still happens after the `=` sign"); + + info("test autocomplete for 'in var'"); + matches = (await webConsoleFront.autocomplete("in var")).matches; + ok(matches.includes("varify"), + "autocompletion still happens with a property name starting with 'var'"); +} + +async function doKeywordsAutocomplete(webConsoleFront) { + info("test autocomplete for 'func'"); + let matches = (await webConsoleFront.autocomplete("func")).matches; + ok(matches.includes("function"), "keywords are returned"); + + info("test autocomplete for ':func'"); + matches = (await webConsoleFront.autocomplete(":func")).matches; + is(!matches.includes("function"), true, + "'function' is not returned when prefixed with ':'"); + + info("test autocomplete for 'window.func'"); + matches = (await webConsoleFront.autocomplete("window.func")).matches; + ok(!matches.includes("function"), + "'function' is not returned when doing a property access"); + + info("test autocomplete for 'window[func'"); + matches = (await webConsoleFront.autocomplete("window[func")).matches; + ok(!matches.includes("function"), + "'function' is not returned when doing an element access"); + } + + async function dontAutocomplete(webConsoleFront) { + const inputs = [ + "", + " ", + "\n", + "\n ", + " \n ", + " \n", + "true;", + "true,", + "({key:", + "a=", + "if(a<", + "if(a>", + "1+", + "1-", + "++", + "--", + "1*", + "2**", + "1/", + "1%", + "1|", + "1&", + "1^", + "~", + "1<<", + "1>>", + "1>>>", + "false||", + "false&&", + "x=true?", + "x=false?1:", + "!", + ...RESERVED_JS_KEYWORDS.map(keyword => `${keyword} `), + ...RESERVED_JS_KEYWORDS.map(keyword => `${keyword} `), + ]; + for (const input of inputs) { + info(`test autocomplete for "${input}"`); + const matches = (await webConsoleFront.autocomplete(input)).matches; + is(matches, null, `No autocomplete result for ${input}"`); + } + } + + async function getAutocompleteMatches(webConsoleFront, input) { + info(`test autocomplete for "${input}"`); + const res = (await webConsoleFront.autocomplete(input)); + return res.matches; + } +</script> +</body> +</html> diff --git a/devtools/shared/webconsole/test/chrome/test_jsterm_last_result.html b/devtools/shared/webconsole/test/chrome/test_jsterm_last_result.html new file mode 100644 index 0000000000..a61a7bb851 --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/test_jsterm_last_result.html @@ -0,0 +1,122 @@ +<!DOCTYPE HTML> +<html lang="en"> +<head> + <meta charset="utf8"> + <title>Test for the $_ getter</title> + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="common.js"></script> + <!-- Any copyright is dedicated to the Public Domain. + - http://creativecommons.org/publicdomain/zero/1.0/ --> +</head> +<body> +<p>Test for the $_ getter</p> + +<iframe id="content-iframe" src="http://example.com/chrome/devtools/shared/webconsole/test/chrome/sandboxed_iframe.html"></iframe> + +<script class="testbody" type="text/javascript"> +"use strict"; + +SimpleTest.waitForExplicitFinish(); +let gState; + +function evaluateJS(input) { + return gState.webConsoleFront.evaluateJSAsync(input); +} + +async function startTest() +{ + removeEventListener("load", startTest); + gState = (await attachConsoleToTab([])).state; + const tests = [checkUndefinedResult,checkAdditionResult,checkObjectResult]; + runTests(tests, testEnd); +} + +const checkUndefinedResult = async function () { + info ("$_ returns undefined if nothing has evaluated yet"); + const response = await evaluateJS("$_"); + basicResultCheck(response, "$_", undefined); + nextTest(); +}; + +const checkAdditionResult = async function () { + info ("$_ returns last value and performs basic arithmetic"); + let response = await evaluateJS("2+2"); + basicResultCheck(response, "2+2", 4); + + response = await evaluateJS("$_"); + basicResultCheck(response, "$_", 4); + + response = await evaluateJS("$_ + 2"); + basicResultCheck(response, "$_ + 2", 6); + + response = await evaluateJS("$_ + 4"); + basicResultCheck(response, "$_ + 4", 10); + + nextTest(); +}; + +const checkObjectResult = async function () { + info ("$_ has correct references to objects"); + + let response = await evaluateJS("var foo = {bar:1}; foo;"); + basicResultCheck(response, "var foo = {bar:1}; foo;", { + type: "object", + class: "Object", + actor: /[a-z]/, + }); + checkObject(response.result.getGrip().preview.ownProperties, { + bar: { + value: 1 + } + }); + + response = await evaluateJS("$_"); + basicResultCheck(response, "$_", { + type: "object", + class: "Object", + actor: /[a-z]/, + }); + checkObject(response.result.getGrip().preview.ownProperties, { + bar: { + value: 1 + } + }); + + top.foo.bar = 2; + + response = await evaluateJS("$_"); + basicResultCheck(response, "$_", { + type: "object", + class: "Object", + actor: /[a-z]/, + }); + checkObject(response.result.getGrip().preview.ownProperties, { + bar: { + value: 2 + } + }); + + nextTest(); +}; + +function basicResultCheck(response, input, output) { + checkObject(response, { + input: input, + result: output, + }); + ok(!response.exception, "no eval exception"); + ok(!response.helperResult, "no helper result"); +} + +function testEnd() +{ + closeDebugger(gState, function() { + gState = null; + SimpleTest.finish(); + }); +} + +addEventListener("load", startTest); +</script> +</body> +</html> diff --git a/devtools/shared/webconsole/test/chrome/test_jsterm_queryselector.html b/devtools/shared/webconsole/test/chrome/test_jsterm_queryselector.html new file mode 100644 index 0000000000..fb6086c46b --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/test_jsterm_queryselector.html @@ -0,0 +1,159 @@ +<!DOCTYPE HTML> +<html lang="en"> +<head> + <meta charset="utf8"> + <title>Test for the querySelector / querySelectorAll helpers</title> + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="common.js"></script> + <!-- Any copyright is dedicated to the Public Domain. + - http://creativecommons.org/publicdomain/zero/1.0/ --> +</head> +<body> +<p>Test for the querySelector / querySelectorAll helpers</p> + +<script class="testbody" type="text/javascript"> +"use strict"; + +SimpleTest.waitForExplicitFinish(); +let gState; +let gWin; + +function evaluateJS(input) { + return gState.webConsoleFront.evaluateJSAsync(input); +} + +async function startTest() { + info ("Content window opened, attaching console to it"); + + const systemPrincipal = Cc["@mozilla.org/systemprincipal;1"].createInstance(Ci.nsIPrincipal); + ok (!gWin.document.nodePrincipal.equals(systemPrincipal), + "The test document is not using the system principal"); + + gState = (await attachConsoleToTab([])).state; + const tests = [ + setupWindow, + checkQuerySelector, + checkQuerySelectorAll, + checkQuerySelectorAllNotExist, + checkQuerySelectorException, + checkQuerySelectorAllException + ]; + runTests(tests, testEnd); +} + +const setupWindow = async function () { + info ("Shimming window functions for the content privileged tab"); + await evaluateJS("document.querySelector = function() { throw 'should not call qS'; }"); + await evaluateJS("document.querySelectorAll = function() { throw 'should not call qSA'; }"); + nextTest(); +}; + +const checkQuerySelector = async function () { + info ("$ returns an DOMNode"); + const response = await evaluateJS("$('body')"); + basicResultCheck(response, "$('body')", { + type: "object", + class: "HTMLBodyElement", + preview: { + kind: "DOMNode", + nodeName: "body" + } + }); + nextTest(); +}; + +const checkQuerySelectorAll = async function () { + info ("$$ returns an array"); + const response = await evaluateJS("$$('body')"); + basicResultCheck(response, "$$('body')", { + type: "object", + class: "Array", + preview: { + length: 1 + } + }); + nextTest(); +}; + +const checkQuerySelectorAllNotExist = async function () { + info ("$$ returns an array even if query yields no results"); + const response = await evaluateJS("$$('foobar')"); + basicResultCheck(response, "$$('foobar')", { + type: "object", + class: "Array", + preview: { + length: 0 + } + }); + nextTest(); +}; + +const checkQuerySelectorException = async function () { + info ("$ returns an exception if an invalid selector was provided"); + const response = await evaluateJS("$(':foo')"); + checkObject(response, { + input: "$(':foo')", + exceptionMessage: "SyntaxError: Document.querySelector: ':foo' is not a valid selector", + exception: { + type: "object", + class: "DOMException", + preview: { + kind: "DOMException", + name: "SyntaxError", + message: "Document.querySelector: ':foo' is not a valid selector" + } + } + }); + nextTest(); +}; + +const checkQuerySelectorAllException = async function () { + info ("$$ returns an exception if an invalid selector was provided"); + const response = await evaluateJS("$$(':foo')"); + checkObject(response, { + input: "$$(':foo')", + exceptionMessage: "SyntaxError: Document.querySelectorAll: ':foo' is not a valid selector", + exception: { + type: "object", + class: "DOMException", + preview: { + kind: "DOMException", + name: "SyntaxError", + message: "Document.querySelectorAll: ':foo' is not a valid selector" + } + } + }); + nextTest(); +}; + +function basicResultCheck(response, input, output) { + checkObject(response, { + input: input, + result: output, + }); + ok(!response.exception, "no eval exception"); + ok(!response.helperResult, "no helper result"); +} + +function testEnd() { + gWin.close(); + gWin = null; + closeDebugger(gState, function() { + gState = null; + SimpleTest.finish(); + }); +} + +const load = async function () { + removeEventListener("load", load); + + // Open a content window to test XRay functionality on built in functions. + gWin = window.open("javascript:'<html><body>dummy</body></html>';"); + info ("Waiting for content window to load"); + gWin.onload = startTest; +}; +addEventListener("load", load); + +</script> +</body> +</html> diff --git a/devtools/shared/webconsole/test/chrome/test_network_get.html b/devtools/shared/webconsole/test/chrome/test_network_get.html new file mode 100644 index 0000000000..9f3b9c2a83 --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/test_network_get.html @@ -0,0 +1,259 @@ +<!DOCTYPE HTML> +<html lang="en"> +<head> + <meta charset="utf8"> + <title>Test for the network actor (GET request)</title> + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="common.js"></script> + <!-- Any copyright is dedicated to the Public Domain. + - http://creativecommons.org/publicdomain/zero/1.0/ --> +</head> +<body> +<p>Test for the network actor (GET request)</p> + +<iframe src="http://example.com/chrome/devtools/shared/webconsole/test/chrome/network_requests_iframe.html"></iframe> + +<script class="testbody" type="text/javascript"> +"use strict"; + +SimpleTest.waitForExplicitFinish(); + +async function startTest() +{ + removeEventListener("load", startTest); + const {state} = await attachConsoleToTab(["NetworkActivity"]); + onAttach(state); +} + +function onAttach(aState) +{ + info("test network GET request"); + + onNetworkEvent = onNetworkEvent.bind(null, aState); + aState.webConsoleFront.on("serverNetworkEvent", onNetworkEvent); + onNetworkEventUpdate = onNetworkEventUpdate.bind(null, aState); + aState.dbgClient.on("networkEventUpdate", onNetworkEventUpdate); + + const iframe = document.querySelector("iframe").contentWindow; + iframe.wrappedJSObject.testXhrGet(); +} + +function onNetworkEvent(aState, aPacket) +{ + info("checking the network event packet"); + + const netActor = aPacket.eventActor; + + checkObject(netActor, { + actor: /[a-z]/, + startedDateTime: /^\d+\-\d+\-\d+T.+$/, + url: /data\.json/, + method: "GET", + }); + + aState.netActor = netActor.actor; + + aState.webConsoleFront.off("serverNetworkEvent", onNetworkEvent); +} + +const updates = []; + +function onNetworkEventUpdate(aState, aPacket) +{ + info("received networkEventUpdate " + aPacket.updateType); + is(aPacket.from, aState.netActor, "networkEventUpdate actor"); + + updates.push(aPacket.updateType); + + let expectedPacket = null; + + switch (aPacket.updateType) { + case "requestHeaders": + case "responseHeaders": + ok(aPacket.headers > 0, "headers > 0"); + ok(aPacket.headersSize > 0, "headersSize > 0"); + break; + case "requestCookies": + expectedPacket = { + cookies: 3, + }; + break; + case "requestPostData": + ok(false, "got unexpected requestPostData"); + break; + case "responseStart": + expectedPacket = { + response: { + httpVersion: /^HTTP\/\d\.\d$/, + status: "200", + statusText: "OK", + headersSize: /^\d+$/, + }, + }; + break; + case "securityInfo": + expectedPacket = { + state: "insecure", + }; + break; + case "responseCookies": + expectedPacket = { + cookies: 0, + }; + break; + case "responseContent": + expectedPacket = { + mimeType: "application/json", + contentSize: 1070, + }; + break; + case "eventTimings": + expectedPacket = { + totalTime: /^\d+$/, + }; + break; + default: + ok(false, "unknown network event update type: " + + aPacket.updateType); + return; + } + + if (expectedPacket) { + info("checking the packet content"); + checkObject(aPacket, expectedPacket); + } + + if (updates.indexOf("responseContent") > -1 && + updates.indexOf("eventTimings") > -1) { + aState.dbgClient.off("networkEventUpdate", + onNetworkEventUpdate); + + onRequestHeaders = onRequestHeaders.bind(null, aState); + aState.webConsoleFront.getRequestHeaders(aState.netActor, + onRequestHeaders); + } +} + +function onRequestHeaders(aState, aResponse) +{ + info("checking request headers"); + + ok(aResponse.headers.length > 0, "request headers > 0"); + ok(aResponse.headersSize > 0, "request headersSize > 0"); + ok(!!aResponse.rawHeaders, "request rawHeaders available"); + + checkHeadersOrCookies(aResponse.headers, { + Referer: /network_requests_iframe\.html/, + Cookie: /bug768096/, + }); + + checkRawHeaders(aResponse.rawHeaders, { + Referer: /network_requests_iframe\.html/, + Cookie: /bug768096/, + }); + + onRequestCookies = onRequestCookies.bind(null, aState); + aState.webConsoleFront.getRequestCookies(aState.netActor, + onRequestCookies); +} + +function onRequestCookies(aState, aResponse) +{ + info("checking request cookies"); + + is(aResponse.cookies.length, 3, "request cookies length"); + + checkHeadersOrCookies(aResponse.cookies, { + foobar: "fooval", + omgfoo: "bug768096", + badcookie: "bug826798=st3fan", + }); + + onRequestPostData = onRequestPostData.bind(null, aState); + aState.webConsoleFront.getRequestPostData(aState.netActor, + onRequestPostData); +} + +function onRequestPostData(aState, aResponse) +{ + info("checking request POST data"); + + ok(!aResponse.postData.text, "no request POST data"); + ok(!aResponse.postDataDiscarded, "request POST data was not discarded"); + + onResponseHeaders = onResponseHeaders.bind(null, aState); + aState.webConsoleFront.getResponseHeaders(aState.netActor, + onResponseHeaders); +} + +function onResponseHeaders(aState, aResponse) +{ + info("checking response headers"); + + ok(aResponse.headers.length > 0, "response headers > 0"); + ok(aResponse.headersSize > 0, "response headersSize > 0"); + ok(!!aResponse.rawHeaders, "response rawHeaders available"); + + checkHeadersOrCookies(aResponse.headers, { + "content-type": /^application\/(json|octet-stream)$/, + "content-length": /^\d+$/, + }); + + checkRawHeaders(aResponse.rawHeaders, { + "content-type": /^application\/(json|octet-stream)$/, + "content-length": /^\d+$/, + }); + + onResponseCookies = onResponseCookies.bind(null, aState); + aState.webConsoleFront.getResponseCookies(aState.netActor, + onResponseCookies); +} + +function onResponseCookies(aState, aResponse) +{ + info("checking response cookies"); + + is(aResponse.cookies.length, 0, "response cookies length"); + + onResponseContent = onResponseContent.bind(null, aState); + aState.webConsoleFront.getResponseContent(aState.netActor, + onResponseContent); +} + +function onResponseContent(aState, aResponse) +{ + info("checking response content"); + + ok(aResponse.content.text, "response content text"); + ok(!aResponse.contentDiscarded, "response content was not discarded"); + + onEventTimings = onEventTimings.bind(null, aState); + aState.webConsoleFront.getEventTimings(aState.netActor, + onEventTimings); +} + +function onEventTimings(aState, aResponse) +{ + info("checking event timings"); + + checkObject(aResponse, { + timings: { + blocked: /^-1|\d+$/, + dns: /^-1|\d+$/, + connect: /^-1|\d+$/, + send: /^-1|\d+$/, + wait: /^-1|\d+$/, + receive: /^-1|\d+$/, + }, + totalTime: /^\d+$/, + }); + + closeDebugger(aState, function() { + SimpleTest.finish(); + }); +} + +addEventListener("load", startTest); +</script> +</body> +</html> diff --git a/devtools/shared/webconsole/test/chrome/test_network_post.html b/devtools/shared/webconsole/test/chrome/test_network_post.html new file mode 100644 index 0000000000..fb350ca1cb --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/test_network_post.html @@ -0,0 +1,270 @@ +<!DOCTYPE HTML> +<html lang="en"> +<head> + <meta charset="utf8"> + <title>Test for the network actor (POST request)</title> + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="common.js"></script> + <!-- Any copyright is dedicated to the Public Domain. + - http://creativecommons.org/publicdomain/zero/1.0/ --> +</head> +<body> +<p>Test for the network actor (POST request)</p> + +<iframe src="http://example.com/chrome/devtools/shared/webconsole/test/chrome/network_requests_iframe.html"></iframe> + +<script class="testbody" type="text/javascript"> +"use strict"; + +SimpleTest.waitForExplicitFinish(); + +async function startTest() +{ + removeEventListener("load", startTest); + + const {state} = await attachConsoleToTab(["NetworkActivity"]); + onAttach(state); +} + +function onAttach(aState) +{ + info("test network POST request"); + + onNetworkEvent = onNetworkEvent.bind(null, aState); + aState.webConsoleFront.on("serverNetworkEvent", onNetworkEvent); + onNetworkEventUpdate = onNetworkEventUpdate.bind(null, aState); + aState.dbgClient.on("networkEventUpdate", onNetworkEventUpdate); + + const iframe = document.querySelector("iframe").contentWindow; + iframe.wrappedJSObject.testXhrPost(); +} + +function onNetworkEvent(aState, aPacket) +{ + info("checking the network event packet"); + + const netActor = aPacket.eventActor; + + checkObject(netActor, { + actor: /[a-z]/, + startedDateTime: /^\d+\-\d+\-\d+T.+$/, + url: /data\.json/, + method: "POST", + }); + + aState.netActor = netActor.actor; + + aState.webConsoleFront.off("serverNetworkEvent", onNetworkEvent); +} + +const updates = []; + +function onNetworkEventUpdate(aState, aPacket) +{ + info("received networkEventUpdate " + aPacket.updateType); + is(aPacket.from, aState.netActor, "networkEventUpdate actor"); + + updates.push(aPacket.updateType); + + let expectedPacket = null; + + switch (aPacket.updateType) { + case "requestHeaders": + case "responseHeaders": + ok(aPacket.headers > 0, "headers > 0"); + ok(aPacket.headersSize > 0, "headersSize > 0"); + break; + case "requestCookies": + expectedPacket = { + cookies: 3, + }; + break; + case "requestPostData": + ok(aPacket.dataSize > 0, "dataSize > 0"); + break; + case "responseStart": + expectedPacket = { + response: { + httpVersion: /^HTTP\/\d\.\d$/, + status: "200", + statusText: "OK", + headersSize: /^\d+$/, + }, + }; + break; + case "securityInfo": + expectedPacket = { + state: "insecure", + }; + break; + case "responseCookies": + expectedPacket = { + cookies: 0, + }; + break; + case "responseContent": + expectedPacket = { + mimeType: "application/json", + contentSize: /^\d+$/, + }; + break; + case "eventTimings": + expectedPacket = { + totalTime: /^\d+$/, + }; + break; + default: + ok(false, "unknown network event update type: " + + aPacket.updateType); + return; + } + + if (expectedPacket) { + info("checking the packet content"); + checkObject(aPacket, expectedPacket); + } + + if (updates.indexOf("responseContent") > -1 && + updates.indexOf("eventTimings") > -1) { + aState.dbgClient.off("networkEventUpdate", + onNetworkEvent); + + onRequestHeaders = onRequestHeaders.bind(null, aState); + aState.webConsoleFront.getRequestHeaders(aState.netActor, + onRequestHeaders); + } +} + +function onRequestHeaders(aState, aResponse) +{ + info("checking request headers"); + + ok(aResponse.headers.length > 0, "request headers > 0"); + ok(aResponse.headersSize > 0, "request headersSize > 0"); + ok(!!aResponse.rawHeaders.length, "request rawHeaders available"); + + checkHeadersOrCookies(aResponse.headers, { + Referer: /network_requests_iframe\.html/, + Cookie: /bug768096/, + }); + + checkRawHeaders(aResponse.rawHeaders, { + Referer: /network_requests_iframe\.html/, + Cookie: /bug768096/, + }); + + onRequestCookies = onRequestCookies.bind(null, aState); + aState.webConsoleFront.getRequestCookies(aState.netActor, + onRequestCookies); +} + +function onRequestCookies(aState, aResponse) +{ + info("checking request cookies"); + + is(aResponse.cookies.length, 3, "request cookies length"); + + checkHeadersOrCookies(aResponse.cookies, { + foobar: "fooval", + omgfoo: "bug768096", + badcookie: "bug826798=st3fan", + }); + + onRequestPostData = onRequestPostData.bind(null, aState); + aState.webConsoleFront.getRequestPostData(aState.netActor, + onRequestPostData); +} + +function onRequestPostData(aState, aResponse) +{ + info("checking request POST data"); + + checkObject(aResponse, { + postData: { + text: /^Hello world! foobaz barr.+foobaz barr$/, + }, + postDataDiscarded: false, + }); + + is(aResponse.postData.text.length, 552, "postData text length"); + + onResponseHeaders = onResponseHeaders.bind(null, aState); + aState.webConsoleFront.getResponseHeaders(aState.netActor, + onResponseHeaders); +} + +function onResponseHeaders(aState, aResponse) +{ + info("checking response headers"); + + ok(aResponse.headers.length > 0, "response headers > 0"); + ok(aResponse.headersSize > 0, "response headersSize > 0"); + ok(!!aResponse.rawHeaders, "response rawHeaders available"); + + checkHeadersOrCookies(aResponse.headers, { + "content-type": /^application\/(json|octet-stream)$/, + "content-length": /^\d+$/, + }); + + checkRawHeaders(aResponse.rawHeaders, { + "content-type": /^application\/(json|octet-stream)$/, + "content-length": /^\d+$/, + }); + + onResponseCookies = onResponseCookies.bind(null, aState); + aState.webConsoleFront.getResponseCookies(aState.netActor, + onResponseCookies); +} + +function onResponseCookies(aState, aResponse) +{ + info("checking response cookies"); + + is(aResponse.cookies.length, 0, "response cookies length"); + + onResponseContent = onResponseContent.bind(null, aState); + aState.webConsoleFront.getResponseContent(aState.netActor, + onResponseContent); +} + +function onResponseContent(aState, aResponse) +{ + info("checking response content"); + + checkObject(aResponse, { + content: { + text: /"test JSON data"/, + }, + contentDiscarded: false, + }); + + onEventTimings = onEventTimings.bind(null, aState); + aState.webConsoleFront.getEventTimings(aState.netActor, + onEventTimings); +} + +function onEventTimings(aState, aResponse) +{ + info("checking event timings"); + + checkObject(aResponse, { + timings: { + blocked: /^-1|\d+$/, + dns: /^-1|\d+$/, + connect: /^-1|\d+$/, + send: /^-1|\d+$/, + wait: /^-1|\d+$/, + receive: /^-1|\d+$/, + }, + totalTime: /^\d+$/, + }); + + closeDebugger(aState, function() { + SimpleTest.finish(); + }); +} + +addEventListener("load", startTest); +</script> +</body> +</html> diff --git a/devtools/shared/webconsole/test/chrome/test_network_security-hsts.html b/devtools/shared/webconsole/test/chrome/test_network_security-hsts.html new file mode 100644 index 0000000000..2eef4cef41 --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/test_network_security-hsts.html @@ -0,0 +1,102 @@ +<!DOCTYPE HTML> +<html lang="en"> +<head> + <meta charset="utf8"> + <title>Test for the network actor (HSTS detection)</title> + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="common.js"></script> + <!-- Any copyright is dedicated to the Public Domain. + - http://creativecommons.org/publicdomain/zero/1.0/ --> +</head> +<body> +<p>Test for the network actor (HSTS detection)</p> + +<iframe src="https://example.com/chrome/devtools/shared/webconsole/test/chrome/network_requests_iframe.html"></iframe> + +<script class="testbody" type="text/javascript"> +"use strict"; + +SimpleTest.waitForExplicitFinish(); + +let gCurrentTestCase = -1; +const TEST_CASES = [ + { + desc: "no HSTS", + url: "https://example.com", + usesHSTS: false, + }, + { + desc: "HSTS from this response", + url: "https://example.com/"+ + "browser/browser/base/content/test/general/browser_star_hsts.sjs", + usesHSTS: true, + }, + { + desc: "stored HSTS from previous response", + url: "https://example.com/", + usesHSTS: true, + } +]; + +async function startTest() +{ + + SimpleTest.registerCleanupFunction(() => { + // Reset HSTS state. + const gSSService = Cc["@mozilla.org/ssservice;1"] + .getService(Ci.nsISiteSecurityService); + + const gIOService = Services.io; + + const uri = gIOService.newURI(TEST_CASES[0].url); + gSSService.resetState(Ci.nsISiteSecurityService.HEADER_HSTS, uri, 0); + }); + + info("Test detection of HTTP Strict Transport Security."); + removeEventListener("load", startTest); + const {state} = await attachConsoleToTab(["NetworkActivity"]); + onAttach(state); +} + +function onAttach(aState) +{ + onNetworkEventUpdate = onNetworkEventUpdate.bind(null, aState); + aState.dbgClient.on("networkEventUpdate", onNetworkEventUpdate); + + runNextCase(aState); +} + +function runNextCase(aState) { + gCurrentTestCase++; + if (gCurrentTestCase === TEST_CASES.length) { + info("Tests ran. Cleaning up."); + closeDebugger(aState, SimpleTest.finish); + return; + } + + const { desc, url } = TEST_CASES[gCurrentTestCase]; + info("Testing site with " + desc); + + const iframe = document.querySelector("iframe").contentWindow; + iframe.wrappedJSObject.makeXhrCallback("GET", url); +} + +function onNetworkEventUpdate(aState, aPacket) +{ + function onSecurityInfo(packet) { + const data = TEST_CASES[gCurrentTestCase]; + is(packet.securityInfo.hsts, data.usesHSTS, + "Strict Transport Security detected correctly."); + + runNextCase(aState); + } + + if (aPacket.updateType === "securityInfo") { + aState.webConsoleFront.getSecurityInfo(aPacket.from, onSecurityInfo); + } +} + +addEventListener("load", startTest); +</script> +</body> +</html> diff --git a/devtools/shared/webconsole/test/chrome/test_nsiconsolemessage.html b/devtools/shared/webconsole/test/chrome/test_nsiconsolemessage.html new file mode 100644 index 0000000000..594dee904f --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/test_nsiconsolemessage.html @@ -0,0 +1,74 @@ +<!DOCTYPE HTML> +<html lang="en"> +<head> + <meta charset="utf8"> + <title>Test for nsIConsoleMessages</title> + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="common.js"></script> + <!-- Any copyright is dedicated to the Public Domain. + - http://creativecommons.org/publicdomain/zero/1.0/ --> +</head> +<body> +<p>Make sure that nsIConsoleMessages are logged. See bug 859756.</p> + +<script class="testbody" type="text/javascript"> +"use strict"; +SimpleTest.waitForExplicitFinish(); + +let expectedMessages = []; + +async function startTest() +{ + removeEventListener("load", startTest); + const {state} = await attachConsole(["PageError"]); + onAttach(state); +} + +function onAttach(aState) +{ + onLogMessage = onLogMessage.bind(null, aState); + aState.webConsoleFront.on("logMessage", onLogMessage); + + expectedMessages = [{ + message: "hello world! bug859756", + timeStamp: /^\d+$/, + }]; + + Services.console.logStringMessage("hello world! bug859756"); + + info("waiting for messages"); +} + +const receivedMessages = []; + +function onLogMessage(aState, aPacket) +{ + info("received message: " + aPacket.message); + + let found = false; + for (const expected of expectedMessages) { + if (expected.message == aPacket.message) { + found = true; + break; + } + } + if (!found) { + return; + } + + receivedMessages.push(aPacket); + if (receivedMessages.length != expectedMessages.length) { + return; + } + + aState.webConsoleFront.off("logMessage", onLogMessage); + + checkObject(receivedMessages, expectedMessages); + + closeDebugger(aState, () => SimpleTest.finish()); +} + +addEventListener("load", startTest); +</script> +</body> +</html> diff --git a/devtools/shared/webconsole/test/chrome/test_object_actor.html b/devtools/shared/webconsole/test/chrome/test_object_actor.html new file mode 100644 index 0000000000..64879f172c --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/test_object_actor.html @@ -0,0 +1,159 @@ +<!DOCTYPE HTML> +<html lang="en"> +<head> + <meta charset="utf8"> + <title>Test for the object actor</title> + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="common.js"></script> + <!-- Any copyright is dedicated to the Public Domain. + - http://creativecommons.org/publicdomain/zero/1.0/ --> +</head> +<body> +<p>Test for the object actor</p> + +<script class="testbody" type="text/javascript"> +"use strict"; + +SimpleTest.waitForExplicitFinish(); + +SpecialPowers.pushPrefEnv({ + "set": [["security.allow_eval_with_system_principal", true]] +}); + +async function startTest() { + removeEventListener("load", startTest); + + const longString = (new Array(DevToolsServer.LONG_STRING_LENGTH + 3)).join("\u0629"); + createTestGlobalVariable(longString); + + const {state} = await attachConsoleToTab(["ConsoleAPI"]); + const onConsoleApiCall = state.webConsoleFront.once("consoleAPICall"); + top.console.log("hello", top.wrappedJSObject.foobarObject); + const {message} = await onConsoleApiCall; + + info("checking the console API call packet"); + checkConsoleAPICall(message, { + level: "log", + filename: /test_object_actor/, + functionName: "startTest", + arguments: ["hello", { + type: "object", + actor: /[a-z]/, + }], + }); + + info("inspecting object properties"); + const {ownProperties} = await message.arguments[1].getPrototypeAndProperties(); + + const expectedProps = { + "abArray": { + value: { + type: "object", + class: "Array", + actor: /[a-z]/, + }, + }, + "foo": { + configurable: true, + enumerable: true, + writable: true, + value: 1, + }, + "foobar": { + value: "hello", + }, + "foobaz": { + value: { + type: "object", + class: "HTMLDocument", + actor: /[a-z]/, + }, + }, + "getterAndSetter": { + get: { + type: "object", + class: "Function", + actor: /[a-z]/, + }, + set: { + type: "object", + class: "Function", + actor: /[a-z]/, + }, + }, + "longStringObj": { + value: { + type: "object", + class: "Object", + actor: /[a-z]/, + }, + }, + "notInspectable": { + value: { + type: "object", + class: "Object", + actor: /[a-z]/, + }, + }, + "omg": { + value: { type: "null" }, + }, + "omgfn": { + value: { + type: "object", + class: "Function", + actor: /[a-z]/, + }, + }, + "tamarbuta": { + value: { + type: "longString", + initial: longString.substring(0, + DevToolsServer.LONG_STRING_INITIAL_LENGTH), + length: longString.length, + }, + }, + "testfoo": { + value: false, + }, + }; + is(Object.keys(ownProperties).length, Object.keys(expectedProps).length, + "number of enumerable properties"); + checkObject(ownProperties, expectedProps); + + await closeDebugger(state); + SimpleTest.finish(); +} + + +function createTestGlobalVariable(longString) { + // Here we put the objects in the correct window, to avoid having them all + // wrapped by proxies for cross-compartment access. + const foobarObject = top.Object.create(null); + foobarObject.tamarbuta = longString; + foobarObject.foo = 1; + foobarObject.foobar = "hello"; + foobarObject.omg = null; + foobarObject.testfoo = false; + foobarObject.notInspectable = top.Object.create(null); + foobarObject.omgfn = new top.Function("return 'myResult'"); + foobarObject.abArray = new top.Array("a", "b"); + foobarObject.foobaz = top.document; + + top.Object.defineProperty(foobarObject, "getterAndSetter", { + enumerable: true, + get: new top.Function("return 'foo';"), + set: new top.Function("1+2"), + }); + + foobarObject.longStringObj = top.Object.create(null); + foobarObject.longStringObj.toSource = new top.Function("'" + longString + "'"); + foobarObject.longStringObj.toString = new top.Function("'" + longString + "'"); + foobarObject.longStringObj.boom = "explode"; + top.wrappedJSObject.foobarObject = foobarObject; +} + +addEventListener("load", startTest); +</script> +</body> +</html> diff --git a/devtools/shared/webconsole/test/chrome/test_object_actor_native_getters.html b/devtools/shared/webconsole/test/chrome/test_object_actor_native_getters.html new file mode 100644 index 0000000000..d066ff5bf1 --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/test_object_actor_native_getters.html @@ -0,0 +1,76 @@ +<!DOCTYPE HTML> +<html lang="en"> +<head> + <meta charset="utf8"> + <title>Test for the native getters in object actors</title> + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="common.js"></script> + <!-- Any copyright is dedicated to the Public Domain. + - http://creativecommons.org/publicdomain/zero/1.0/ --> +</head> +<body> +<p>Test for the native getters in object actors</p> + +<script class="testbody" type="text/javascript"> +"use strict"; + +SimpleTest.waitForExplicitFinish(); + +async function startTest() { + removeEventListener("load", startTest); + const {state} = await attachConsoleToTab(["ConsoleAPI"]); + + const onConsoleAPICall = state.webConsoleFront.once("consoleAPICall"); + top.console.log("hello", document); + const {message} = await onConsoleAPICall; + + info("checking the console API call packet"); + checkConsoleAPICall(message, { + level: "log", + filename: /test_object_actor/, + functionName: "startTest", + arguments: ["hello", { + type: "object", + actor: /[a-z]/, + }], + }); + + info("inspecting object properties"); + const args = message.arguments; + const {ownProperties, safeGetterValues} = await args[1].getPrototypeAndProperties(); + + const expectedProps = { + "location": { + get: { + type: "object", + class: "Function", + actor: /[a-z]/, + }, + }, + }; + ok(Object.keys(ownProperties).length >= Object.keys(expectedProps).length, + "number of properties"); + + info("check ownProperties"); + checkObject(ownProperties, expectedProps); + + info("check safeGetterValues"); + checkObject(safeGetterValues, { + "title": { + getterValue: /native getters in object actors/, + getterPrototypeLevel: 2, + }, + "styleSheets": { + getterValue: /Front for obj\//, + getterPrototypeLevel: 2, + }, + }); + + await closeDebugger(state); + SimpleTest.finish(); +} + +addEventListener("load", startTest); +</script> +</body> +</html> diff --git a/devtools/shared/webconsole/test/chrome/test_object_actor_native_getters_lenient_this.html b/devtools/shared/webconsole/test/chrome/test_object_actor_native_getters_lenient_this.html new file mode 100644 index 0000000000..eba977349f --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/test_object_actor_native_getters_lenient_this.html @@ -0,0 +1,55 @@ +<!DOCTYPE HTML> +<html lang="en"> +<head> + <meta charset="utf8"> + <title>Test that WebIDL attributes with the LenientThis extended attribute + do not appear in the wrong objects</title> + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="common.js"></script> + <!-- Any copyright is dedicated to the Public Domain. + - http://creativecommons.org/publicdomain/zero/1.0/ --> +</head> +<body> +<p>Test for the native getters in object actors</p> + +<script class="testbody" type="text/javascript"> +"use strict"; + +SimpleTest.waitForExplicitFinish(); + +async function startTest() { + removeEventListener("load", startTest); + const {state} = await attachConsoleToTab(["ConsoleAPI"]); + + const onConsoleApiCall = state.webConsoleFront.once("consoleAPICall"); + const docAsProto = Object.create(document); + top.console.log("hello", docAsProto); + const {message} = await onConsoleApiCall; + + info("checking the console API call packet"); + checkConsoleAPICall(message, { + level: "log", + filename: /test_object_actor/, + functionName: "startTest", + arguments: ["hello", { + type: "object", + actor: /[a-z]/, + }], + }); + + info("inspecting object properties"); + const args = message.arguments; + + const {ownProperties, safeGetterValues} = await args[1].getPrototypeAndProperties(); + + is(Object.keys(ownProperties).length, 0, "number of properties"); + is(Object.keys(safeGetterValues).length, 0, "number of safe getters"); + + await closeDebugger(state); + SimpleTest.finish(); +} + +addEventListener("load", startTest); +</script> +</body> +</html> diff --git a/devtools/shared/webconsole/test/chrome/test_page_errors.html b/devtools/shared/webconsole/test/chrome/test_page_errors.html new file mode 100644 index 0000000000..c0f6b4dfff --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/test_page_errors.html @@ -0,0 +1,224 @@ +<!DOCTYPE HTML> +<html lang="en"> +<head> + <meta charset="utf8"> + <title>Test for page errors</title> + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="common.js"></script> + <!-- Any copyright is dedicated to the Public Domain. + - http://creativecommons.org/publicdomain/zero/1.0/ --> +</head> +<body> +<p>Test for page errors</p> + +<script class="testbody" type="text/javascript"> +"use strict"; + +const { MESSAGE_CATEGORY } = require("devtools/shared/constants"); +SimpleTest.waitForExplicitFinish(); + +const previousEnabled = window.docShell.cssErrorReportingEnabled; +window.docShell.cssErrorReportingEnabled = true; + +SimpleTest.registerCleanupFunction(() => { + window.docShell.cssErrorReportingEnabled = previousEnabled; +}); + +let expectedPageErrors = []; + +const NO_UNCAUGHT_EXCEPTION = Symbol(); + +function doPageErrors() { + expectedPageErrors = { + "document.body.style.color = 'fooColor';": { + errorMessage: /fooColor/, + sourceName: /test_page_errors/, + category: MESSAGE_CATEGORY.CSS_PARSER, + timeStamp: /^\d+$/, + error: false, + warning: true, + }, + "document.doTheImpossible();": { + errorMessage: /doTheImpossible/, + errorMessageName: undefined, + sourceName: /test_page_errors/, + category: "chrome javascript", + timeStamp: /^\d+$/, + error: true, + warning: false, + }, + "(42).toString(0);": { + errorMessage: /radix/, + errorMessageName: "JSMSG_BAD_RADIX", + sourceName: /test_page_errors/, + category: "chrome javascript", + timeStamp: /^\d+$/, + error: true, + warning: false, + }, + "'use strict'; (Object.freeze({name: 'Elsa', score: 157})).score = 0;": { + errorMessage: /read.only/, + errorMessageName: "JSMSG_READ_ONLY", + sourceName: /test_page_errors/, + category: "chrome javascript", + timeStamp: /^\d+$/, + error: true, + warning: false, + }, + "([]).length = -1": { + errorMessage: /array length/, + errorMessageName: "JSMSG_BAD_ARRAY_LENGTH", + sourceName: /test_page_errors/, + category: "chrome javascript", + timeStamp: /^\d+$/, + error: true, + warning: false, + }, + "'abc'.repeat(-1);": { + errorMessage: /repeat count.*non-negative/, + errorMessageName: "JSMSG_NEGATIVE_REPETITION_COUNT", + sourceName: /test_page_errors/, + category: "chrome javascript", + timeStamp: /^\d+$/, + error: true, + warning: false, + }, + "'a'.repeat(2e28);": { + errorMessage: /repeat count.*less than infinity/, + errorMessageName: "JSMSG_RESULTING_STRING_TOO_LARGE", + sourceName: /test_page_errors/, + category: "chrome javascript", + timeStamp: /^\d+$/, + error: true, + warning: false, + }, + "77.1234.toExponential(-1);": { + errorMessage: /out of range/, + errorMessageName: "JSMSG_PRECISION_RANGE", + sourceName: /test_page_errors/, + category: "chrome javascript", + timeStamp: /^\d+$/, + error: true, + warning: false, + }, + "function a() { return; 1 + 1; }": { + errorMessage: /unreachable code/, + errorMessageName: "JSMSG_STMT_AFTER_RETURN", + sourceName: /test_page_errors/, + category: "chrome javascript", + timeStamp: /^\d+$/, + error: false, + warning: true, + }, + "let a, a;": { + errorMessage: /redeclaration of/, + errorMessageName: "JSMSG_REDECLARED_VAR", + sourceName: /test_page_errors/, + category: "chrome javascript", + timeStamp: /^\d+$/, + error: true, + warning: false, + notes: [ + { + messageBody: /Previously declared at line/, + frame: { + source: /test_page_errors/, + } + } + ] + }, + [`let error = new TypeError("abc"); + error.name = "MyError"; + error.message = "here"; + throw error`]: { + errorMessage: /MyError: here/, + errorMessageName: "", + sourceName: /test_page_errors/, + category: "chrome javascript", + timeStamp: /^\d+$/, + error: true, + warning: false, + }, + "DOMTokenList.prototype.contains.call([])": { + errorMessage: /does not implement interface/, + errorMessageName: "MSG_METHOD_THIS_DOES_NOT_IMPLEMENT_INTERFACE", + sourceName: /test_page_errors/, + category: "chrome javascript", + timeStamp: /^\d+$/, + error: true, + warning: false, + }, + [`let error2 = new TypeError("abc"); + error2.name = "MyPromiseError"; + error2.message = "here2"; + Promise.reject(error2)`]: { + errorMessage: /MyPromiseError: here2/, + errorMessageName: "", + sourceName: /test_page_errors/, + category: "chrome javascript", + timeStamp: /^\d+$/, + error: true, + warning: false, + // Promise.reject doesn't produce an uncaught exception + // even though |exception: true|. + [NO_UNCAUGHT_EXCEPTION]: true + } + }; + + let container = document.createElement("script"); + for (const stmt of Object.keys(expectedPageErrors)) { + if (expectedPageErrors[stmt].error && + !expectedPageErrors[stmt][NO_UNCAUGHT_EXCEPTION]) { + SimpleTest.expectUncaughtException(); + } + info("starting stmt: " + stmt); + container = document.createElement("script"); + document.body.appendChild(container); + container.textContent = stmt; + document.body.removeChild(container); + info("ending stmt: " + stmt); + } +} + +async function startTest() { + removeEventListener("load", startTest); + + const {state} = await attachConsole(["PageError"]); + onAttach(state); +} + +function onAttach(state) { + onPageError = onPageError.bind(null, state); + state.webConsoleFront.on("pageError", onPageError); + doPageErrors(); +} + +const pageErrors = []; + +function onPageError(state, packet) { + if (!packet.pageError.sourceName.includes("test_page_errors")) { + info("Ignoring error from unknown source: " + packet.pageError.sourceName); + return; + } + + pageErrors.push(packet.pageError); + if (pageErrors.length != Object.keys(expectedPageErrors).length) { + return; + } + + state.webConsoleFront.off("pageError", onPageError); + + Object.values(expectedPageErrors).forEach(function(message, index) { + info("checking received page error #" + index); + checkObject(pageErrors[index], Object.values(expectedPageErrors)[index]); + }); + + closeDebugger(state, function() { + SimpleTest.finish(); + }); +} + +addEventListener("load", startTest); +</script> +</body> +</html> diff --git a/devtools/shared/webconsole/test/chrome/test_throw.html b/devtools/shared/webconsole/test/chrome/test_throw.html new file mode 100644 index 0000000000..123519174a --- /dev/null +++ b/devtools/shared/webconsole/test/chrome/test_throw.html @@ -0,0 +1,92 @@ +<!DOCTYPE HTML> +<html lang="en"> +<head> + <meta charset="utf8"> + <title>Web Console throw tests</title> + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="common.js"></script> + <!-- Any copyright is dedicated to the Public Domain. + - http://creativecommons.org/publicdomain/zero/1.0/ --> +</head> +<body> +<p>Web Console throw tests</p> + +<script class="testbody" type="text/javascript"> +"use strict"; + +SimpleTest.waitForExplicitFinish(); + +async function startTest() +{ + removeEventListener("load", startTest); + const {state} = await attachConsoleToTab([]); + onAttach(state); +} + +function onAttach(aState) +{ + const tests = []; + + const falsyValues = ["-0", "null", "undefined", "Infinity", "-Infinity", "NaN"]; + falsyValues.forEach(function(value) { + tests.push(async function() { + const response = await aState.webConsoleFront.evaluateJSAsync("throw " + value + ";") + const type = response.exception.type; + is(type, value, "exception.type for throw " + value); + nextTest(); + }); + }); + + const identityTestValues = [false, 0]; + identityTestValues.forEach(function(value) { + tests.push(async function() { + const response = await aState.webConsoleFront.evaluateJSAsync("throw " + value + ";") + const exception = response.exception; + is(exception, value, "response.exception for throw " + value); + nextTest(); + }); + }); + + const longString = Array(DevToolsServer.LONG_STRING_LENGTH + 1).join("a"), + shortedString = longString.substring(0, + DevToolsServer.LONG_STRING_INITIAL_LENGTH + ); + tests.push(async function() { + const response = await aState.webConsoleFront.evaluateJSAsync("throw '" + longString + "';") + is(response.exception.initial, shortedString, + "exception.initial for throw longString" + ); + is(response.exceptionMessage.initial, shortedString, + "exceptionMessage.initial for throw longString" + ); + nextTest(); + }); + + const symbolTestValues = [ + ["Symbol.iterator", "Symbol(Symbol.iterator)"], + ["Symbol('foo')", "Symbol(foo)"], + ["Symbol()", "Symbol()"], + ]; + symbolTestValues.forEach(function([expr, message]) { + tests.push(async function() { + const response = await aState.webConsoleFront.evaluateJSAsync("throw " + expr + ";"); + is(response.exceptionMessage, message, + "response.exception for throw " + expr); + nextTest(); + }); + }); + + runTests(tests, endTest.bind(null, aState)); +} + +function endTest(aState) +{ + closeDebugger(aState, function() { + SimpleTest.finish(); + }); +} + +addEventListener("load", startTest); +</script> +</body> +</html> diff --git a/devtools/shared/webconsole/test/xpcshell/.eslintrc.js b/devtools/shared/webconsole/test/xpcshell/.eslintrc.js new file mode 100644 index 0000000000..8611c174f5 --- /dev/null +++ b/devtools/shared/webconsole/test/xpcshell/.eslintrc.js @@ -0,0 +1,6 @@ +"use strict"; + +module.exports = { + // Extend from the common devtools xpcshell eslintrc config. + extends: "../../../../.eslintrc.xpcshell.js", +}; diff --git a/devtools/shared/webconsole/test/xpcshell/test_analyze_input_string.js b/devtools/shared/webconsole/test/xpcshell/test_analyze_input_string.js new file mode 100644 index 0000000000..517e981c38 --- /dev/null +++ b/devtools/shared/webconsole/test/xpcshell/test_analyze_input_string.js @@ -0,0 +1,226 @@ +// Any copyright is dedicated to the Public Domain. +// http://creativecommons.org/publicdomain/zero/1.0/ + +"use strict"; +const { require } = ChromeUtils.import("resource://devtools/shared/Loader.jsm"); +const { + analyzeInputString, +} = require("devtools/shared/webconsole/js-property-provider"); + +add_task(() => { + const tests = [ + { + desc: "simple property access", + input: `var a = {b: 1};a.b`, + expected: { + isElementAccess: false, + isPropertyAccess: true, + expressionBeforePropertyAccess: `var a = {b: 1};a`, + lastStatement: "a.b", + mainExpression: `a`, + matchProp: `b`, + }, + }, + { + desc: "deep property access", + input: `a.b.c`, + expected: { + isElementAccess: false, + isPropertyAccess: true, + expressionBeforePropertyAccess: `a.b`, + lastStatement: "a.b.c", + mainExpression: `a.b`, + matchProp: `c`, + }, + }, + { + desc: "element access", + input: `a["b`, + expected: { + isElementAccess: true, + isPropertyAccess: true, + expressionBeforePropertyAccess: `a`, + lastStatement: `a["b`, + mainExpression: `a`, + matchProp: `"b`, + }, + }, + { + desc: "element access without quotes", + input: `a[b`, + expected: { + isElementAccess: true, + isPropertyAccess: true, + expressionBeforePropertyAccess: `a`, + lastStatement: `a[b`, + mainExpression: `a`, + matchProp: `b`, + }, + }, + { + desc: "simple optional chaining access", + input: `a?.b`, + expected: { + isElementAccess: false, + isPropertyAccess: true, + expressionBeforePropertyAccess: `a`, + lastStatement: `a?.b`, + mainExpression: `a`, + matchProp: `b`, + }, + }, + { + desc: "deep optional chaining access", + input: `a?.b?.c`, + expected: { + isElementAccess: false, + isPropertyAccess: true, + expressionBeforePropertyAccess: `a?.b`, + lastStatement: `a?.b?.c`, + mainExpression: `a?.b`, + matchProp: `c`, + }, + }, + { + desc: "optional chaining element access", + input: `a?.["b`, + expected: { + isElementAccess: true, + isPropertyAccess: true, + expressionBeforePropertyAccess: `a`, + lastStatement: `a?.["b`, + mainExpression: `a`, + matchProp: `"b`, + }, + }, + { + desc: "optional chaining element access without quotes", + input: `a?.[b`, + expected: { + isElementAccess: true, + isPropertyAccess: true, + expressionBeforePropertyAccess: `a`, + lastStatement: `a?.[b`, + mainExpression: `a`, + matchProp: `b`, + }, + }, + { + desc: "deep optional chaining element access with quotes", + input: `var a = {b: 1, c: ["."]}; a?.["b"]?.c?.["d[.`, + expected: { + isElementAccess: true, + isPropertyAccess: true, + expressionBeforePropertyAccess: `var a = {b: 1, c: ["."]}; a?.["b"]?.c`, + lastStatement: `a?.["b"]?.c?.["d[.`, + mainExpression: `a?.["b"]?.c`, + matchProp: `"d[.`, + }, + }, + { + desc: "literal arrays with newline", + input: `[1,2,3,\n4\n].`, + expected: { + isElementAccess: false, + isPropertyAccess: true, + expressionBeforePropertyAccess: `[1,2,3,\n4\n]`, + lastStatement: `[1,2,3,4].`, + mainExpression: `[1,2,3,4]`, + matchProp: ``, + }, + }, + { + desc: "number literal with newline", + input: `1\n.`, + expected: { + isElementAccess: false, + isPropertyAccess: true, + expressionBeforePropertyAccess: `1\n`, + lastStatement: `1\n.`, + mainExpression: `1`, + matchProp: ``, + }, + }, + { + desc: "string literal", + input: `"abc".`, + expected: { + isElementAccess: false, + isPropertyAccess: true, + expressionBeforePropertyAccess: `"abc"`, + lastStatement: `"abc".`, + mainExpression: `"abc"`, + matchProp: ``, + }, + }, + { + desc: "string literal containing backslash", + input: `"\\n".`, + expected: { + isElementAccess: false, + isPropertyAccess: true, + expressionBeforePropertyAccess: `"\\n"`, + lastStatement: `"\\n".`, + mainExpression: `"\\n"`, + matchProp: ``, + }, + }, + { + desc: "single quote string literal containing backslash", + input: `'\\r'.`, + expected: { + isElementAccess: false, + isPropertyAccess: true, + expressionBeforePropertyAccess: `'\\r'`, + lastStatement: `'\\r'.`, + mainExpression: `'\\r'`, + matchProp: ``, + }, + }, + { + desc: "template string literal containing backslash", + input: "`\\\\`.", + expected: { + isElementAccess: false, + isPropertyAccess: true, + expressionBeforePropertyAccess: "`\\\\`", + lastStatement: "`\\\\`.", + mainExpression: "`\\\\`", + matchProp: ``, + }, + }, + { + desc: "unterminated double quote string literal", + input: `"\n`, + expected: { + err: "unterminated string literal", + }, + }, + { + desc: "unterminated single quote string literal", + input: `'\n`, + expected: { + err: "unterminated string literal", + }, + }, + { + desc: "optional chaining operator with spaces", + input: `test ?. ["propA"] ?. [0] ?. ["propB"] ?. ['to`, + expected: { + isElementAccess: true, + isPropertyAccess: true, + expressionBeforePropertyAccess: `test ?. ["propA"] ?. [0] ?. ["propB"] `, + lastStatement: `test ?. ["propA"] ?. [0] ?. ["propB"] ?. ['to`, + mainExpression: `test ?. ["propA"] ?. [0] ?. ["propB"]`, + matchProp: `'to`, + }, + }, + ]; + + for (const { input, desc, expected } of tests) { + const result = analyzeInputString(input); + for (const [key, value] of Object.entries(expected)) { + Assert.equal(value, result[key], `${desc} | ${key} has expected value`); + } + } +}); diff --git a/devtools/shared/webconsole/test/xpcshell/test_js_property_provider.js b/devtools/shared/webconsole/test/xpcshell/test_js_property_provider.js new file mode 100644 index 0000000000..245ca425fa --- /dev/null +++ b/devtools/shared/webconsole/test/xpcshell/test_js_property_provider.js @@ -0,0 +1,712 @@ +// Any copyright is dedicated to the Public Domain. +// http://creativecommons.org/publicdomain/zero/1.0/ + +"use strict"; +const { require } = ChromeUtils.import("resource://devtools/shared/Loader.jsm"); +const { + FallibleJSPropertyProvider: JSPropertyProvider, +} = require("devtools/shared/webconsole/js-property-provider"); + +const { addDebuggerToGlobal } = ChromeUtils.import( + "resource://gre/modules/jsdebugger.jsm" +); +addDebuggerToGlobal(this); + +function run_test() { + const testArray = `var testArray = [ + {propA: "A"}, + { + propB: "B", + propC: [ + "D" + ] + }, + [ + {propE: "E"} + ] + ]`; + + const testObject = 'var testObject = {"propA": [{"propB": "B"}]}'; + const testHyphenated = 'var testHyphenated = {"prop-A": "res-A"}'; + const testLet = "let foobar = {a: ''}; const blargh = {a: 1};"; + + const testGenerators = ` + // Test with generator using a named function. + function* genFunc() { + for (let i = 0; i < 10; i++) { + yield i; + } + } + let gen1 = genFunc(); + gen1.next(); + + // Test with generator using an anonymous function. + let gen2 = (function* () { + for (let i = 0; i < 10; i++) { + yield i; + } + })();`; + + const testGetters = ` + var testGetters = { + get x() { + return Object.create(null, Object.getOwnPropertyDescriptors({ + hello: "", + world: "", + })); + }, + get y() { + return Object.create(null, Object.getOwnPropertyDescriptors({ + get y() { + return "plop"; + }, + })); + } + }; + `; + + const sandbox = Cu.Sandbox("http://example.com"); + const dbg = new Debugger(); + const dbgObject = dbg.addDebuggee(sandbox); + const dbgEnv = dbgObject.asEnvironment(); + Cu.evalInSandbox( + ` + const hello = Object.create(null, Object.getOwnPropertyDescriptors({world: 1})); + String.prototype.hello = hello; + Number.prototype.hello = hello; + Array.prototype.hello = hello; + `, + sandbox + ); + Cu.evalInSandbox(testArray, sandbox); + Cu.evalInSandbox(testObject, sandbox); + Cu.evalInSandbox(testHyphenated, sandbox); + Cu.evalInSandbox(testLet, sandbox); + Cu.evalInSandbox(testGenerators, sandbox); + Cu.evalInSandbox(testGetters, sandbox); + + info("Running tests with dbgObject"); + runChecks(dbgObject, null, sandbox); + + info("Running tests with dbgEnv"); + runChecks(null, dbgEnv, sandbox); +} + +function runChecks(dbgObject, environment, sandbox) { + const propertyProvider = (inputValue, options) => + JSPropertyProvider({ + dbgObject, + environment, + inputValue, + ...options, + }); + + info("Test that suggestions are given for 'this'"); + let results = propertyProvider("t"); + test_has_result(results, "this"); + + if (dbgObject != null) { + info("Test that suggestions are given for 'this.'"); + results = propertyProvider("this."); + test_has_result(results, "testObject"); + + info("Test that suggestions are given for '(this).'"); + results = propertyProvider("(this)."); + test_has_result(results, "testObject"); + + info("Test that suggestions are given for deep 'this' properties access"); + results = propertyProvider("(this).testObject.propA."); + test_has_result(results, "shift"); + + results = propertyProvider("(this).testObject.propA["); + test_has_result(results, `"shift"`); + + results = propertyProvider("(this)['testObject']['propA']["); + test_has_result(results, `"shift"`); + + results = propertyProvider("(this).testObject['propA']."); + test_has_result(results, "shift"); + + info("Test that no suggestions are given for 'this.this'"); + results = propertyProvider("this.this"); + test_has_no_results(results); + } + + info("Test that suggestions are given for 'globalThis'"); + results = propertyProvider("g"); + test_has_result(results, "globalThis"); + + info("Test that suggestions are given for 'globalThis.'"); + results = propertyProvider("globalThis."); + test_has_result(results, "testObject"); + + info("Test that suggestions are given for '(globalThis).'"); + results = propertyProvider("(globalThis)."); + test_has_result(results, "testObject"); + + info( + "Test that suggestions are given for deep 'globalThis' properties access" + ); + results = propertyProvider("(globalThis).testObject.propA."); + test_has_result(results, "shift"); + + results = propertyProvider("(globalThis).testObject.propA["); + test_has_result(results, `"shift"`); + + results = propertyProvider("(globalThis)['testObject']['propA']["); + test_has_result(results, `"shift"`); + + results = propertyProvider("(globalThis).testObject['propA']."); + test_has_result(results, "shift"); + + info("Testing lexical scope issues (Bug 1207868)"); + results = propertyProvider("foobar"); + test_has_result(results, "foobar"); + + results = propertyProvider("foobar."); + test_has_result(results, "a"); + + results = propertyProvider("blargh"); + test_has_result(results, "blargh"); + + results = propertyProvider("blargh."); + test_has_result(results, "a"); + + info("Test that suggestions are given for 'foo[n]' where n is an integer."); + results = propertyProvider("testArray[0]."); + test_has_result(results, "propA"); + + info("Test that suggestions are given for multidimensional arrays."); + results = propertyProvider("testArray[2][0]."); + test_has_result(results, "propE"); + + info("Test that suggestions are given for nested arrays."); + results = propertyProvider("testArray[1].propC[0]."); + test_has_result(results, "indexOf"); + + info("Test that suggestions are given for literal arrays."); + results = propertyProvider("[1,2,3]."); + test_has_result(results, "indexOf"); + + results = propertyProvider("[1,2,3].h"); + test_has_result(results, "hello"); + + results = propertyProvider("[1,2,3].hello.w"); + test_has_result(results, "world"); + + info("Test that suggestions are given for literal arrays with newlines."); + results = propertyProvider("[1,2,3,\n4\n]."); + test_has_result(results, "indexOf"); + + info("Test that suggestions are given for literal strings."); + results = propertyProvider("'foo'."); + test_has_result(results, "charAt"); + results = propertyProvider('"foo".'); + test_has_result(results, "charAt"); + results = propertyProvider("`foo`."); + test_has_result(results, "charAt"); + results = propertyProvider("`foo doc`."); + test_has_result(results, "charAt"); + results = propertyProvider('`foo " doc`.'); + test_has_result(results, "charAt"); + results = propertyProvider("`foo ' doc`."); + test_has_result(results, "charAt"); + results = propertyProvider("'[1,2,3]'."); + test_has_result(results, "charAt"); + results = propertyProvider("'foo'.h"); + test_has_result(results, "hello"); + results = propertyProvider("'foo'.hello.w"); + test_has_result(results, "world"); + results = propertyProvider(`"\\n".`); + test_has_result(results, "charAt"); + results = propertyProvider(`'\\r'.`); + test_has_result(results, "charAt"); + results = propertyProvider("`\\\\`."); + test_has_result(results, "charAt"); + + info("Test that suggestions are not given for syntax errors."); + results = propertyProvider("'foo\""); + Assert.equal(null, results); + results = propertyProvider("'foo d"); + Assert.equal(null, results); + results = propertyProvider(`"foo d`); + Assert.equal(null, results); + results = propertyProvider("`foo d"); + Assert.equal(null, results); + results = propertyProvider("[1,',2]"); + Assert.equal(null, results); + results = propertyProvider("'[1,2]."); + Assert.equal(null, results); + results = propertyProvider("'foo'.."); + Assert.equal(null, results); + + info("Test that suggestions are not given without a dot."); + results = propertyProvider("'foo'"); + test_has_no_results(results); + results = propertyProvider("`foo`"); + test_has_no_results(results); + results = propertyProvider("[1,2,3]"); + test_has_no_results(results); + results = propertyProvider("[1,2,3].\n'foo'"); + test_has_no_results(results); + + info("Test that suggestions are not given for index that's out of bounds."); + results = propertyProvider("testArray[10]."); + Assert.equal(null, results); + + info("Test that invalid element access syntax does not return anything"); + results = propertyProvider("testArray[][1]."); + Assert.equal(null, results); + + info("Test that deep element access works."); + results = propertyProvider("testObject['propA'][0]."); + test_has_result(results, "propB"); + + results = propertyProvider("testArray[1]['propC']."); + test_has_result(results, "shift"); + + results = propertyProvider("testArray[1].propC[0]["); + test_has_result(results, `"trim"`); + + results = propertyProvider("testArray[1].propC[0]."); + test_has_result(results, "trim"); + + info( + "Test that suggestions are displayed when variable is wrapped in parens" + ); + results = propertyProvider("(testObject)['propA'][0]."); + test_has_result(results, "propB"); + + results = propertyProvider("(testArray)[1]['propC']."); + test_has_result(results, "shift"); + + results = propertyProvider("(testArray)[1].propC[0]["); + test_has_result(results, `"trim"`); + + results = propertyProvider("(testArray)[1].propC[0]."); + test_has_result(results, "trim"); + + info("Test that suggestions are given if there is an hyphen in the chain."); + results = propertyProvider("testHyphenated['prop-A']."); + test_has_result(results, "trim"); + + info("Test that we have suggestions for generators."); + const gen1Result = Cu.evalInSandbox("gen1.next().value", sandbox); + results = propertyProvider("gen1."); + test_has_result(results, "next"); + info("Test that the generator next() was not executed"); + const gen1NextResult = Cu.evalInSandbox("gen1.next().value", sandbox); + Assert.equal(gen1Result + 1, gen1NextResult); + + info("Test with an anonymous generator."); + const gen2Result = Cu.evalInSandbox("gen2.next().value", sandbox); + results = propertyProvider("gen2."); + test_has_result(results, "next"); + const gen2NextResult = Cu.evalInSandbox("gen2.next().value", sandbox); + Assert.equal(gen2Result + 1, gen2NextResult); + + info( + "Test that getters are not executed if authorizedEvaluations is undefined" + ); + results = propertyProvider("testGetters.x."); + Assert.deepEqual(results, { + isUnsafeGetter: true, + getterPath: ["testGetters", "x"], + }); + + results = propertyProvider("testGetters.x["); + Assert.deepEqual(results, { + isUnsafeGetter: true, + getterPath: ["testGetters", "x"], + }); + + results = propertyProvider("testGetters.x.hell"); + Assert.deepEqual(results, { + isUnsafeGetter: true, + getterPath: ["testGetters", "x"], + }); + + results = propertyProvider("testGetters.x['hell"); + Assert.deepEqual(results, { + isUnsafeGetter: true, + getterPath: ["testGetters", "x"], + }); + + info( + "Test that getters are not executed if authorizedEvaluations does not match" + ); + results = propertyProvider("testGetters.x.", { authorizedEvaluations: [] }); + Assert.deepEqual(results, { + isUnsafeGetter: true, + getterPath: ["testGetters", "x"], + }); + + results = propertyProvider("testGetters.x.", { + authorizedEvaluations: [["testGetters"]], + }); + Assert.deepEqual(results, { + isUnsafeGetter: true, + getterPath: ["testGetters", "x"], + }); + + results = propertyProvider("testGetters.x.", { + authorizedEvaluations: [["testGtrs", "x"]], + }); + Assert.deepEqual(results, { + isUnsafeGetter: true, + getterPath: ["testGetters", "x"], + }); + + results = propertyProvider("testGetters.x.", { + authorizedEvaluations: [["x"]], + }); + Assert.deepEqual(results, { + isUnsafeGetter: true, + getterPath: ["testGetters", "x"], + }); + + info("Test that deep getter property access returns intermediate getters"); + results = propertyProvider("testGetters.y.y."); + Assert.deepEqual(results, { + isUnsafeGetter: true, + getterPath: ["testGetters", "y"], + }); + + results = propertyProvider("testGetters['y'].y."); + Assert.deepEqual(results, { + isUnsafeGetter: true, + getterPath: ["testGetters", "y"], + }); + + results = propertyProvider("testGetters['y']['y']."); + Assert.deepEqual(results, { + isUnsafeGetter: true, + getterPath: ["testGetters", "y"], + }); + + results = propertyProvider("testGetters.y['y']."); + Assert.deepEqual(results, { + isUnsafeGetter: true, + getterPath: ["testGetters", "y"], + }); + + info("Test that deep getter property access invoke intermediate getters"); + results = propertyProvider("testGetters.y.y.", { + authorizedEvaluations: [["testGetters", "y"]], + }); + Assert.deepEqual(results, { + isUnsafeGetter: true, + getterPath: ["testGetters", "y", "y"], + }); + + results = propertyProvider("testGetters['y'].y.", { + authorizedEvaluations: [["testGetters", "y"]], + }); + Assert.deepEqual(results, { + isUnsafeGetter: true, + getterPath: ["testGetters", "y", "y"], + }); + + results = propertyProvider("testGetters['y']['y'].", { + authorizedEvaluations: [["testGetters", "y"]], + }); + Assert.deepEqual(results, { + isUnsafeGetter: true, + getterPath: ["testGetters", "y", "y"], + }); + + results = propertyProvider("testGetters.y['y'].", { + authorizedEvaluations: [["testGetters", "y"]], + }); + Assert.deepEqual(results, { + isUnsafeGetter: true, + getterPath: ["testGetters", "y", "y"], + }); + + info( + "Test that getters are executed if matching an authorizedEvaluation element" + ); + results = propertyProvider("testGetters.x.", { + authorizedEvaluations: [["testGetters", "x"]], + }); + test_has_exact_results(results, ["hello", "world"]); + Assert.ok(Object.keys(results).includes("isUnsafeGetter") === false); + Assert.ok(Object.keys(results).includes("getterPath") === false); + + results = propertyProvider("testGetters.x.", { + authorizedEvaluations: [["testGetters", "x"], ["y"]], + }); + test_has_exact_results(results, ["hello", "world"]); + Assert.ok(Object.keys(results).includes("isUnsafeGetter") === false); + Assert.ok(Object.keys(results).includes("getterPath") === false); + + info("Test that executing getters filters with provided string"); + results = propertyProvider("testGetters.x.hell", { + authorizedEvaluations: [["testGetters", "x"]], + }); + test_has_exact_results(results, ["hello"]); + + results = propertyProvider("testGetters.x['hell", { + authorizedEvaluations: [["testGetters", "x"]], + }); + test_has_exact_results(results, ["'hello'"]); + + info( + "Test children getters are not executed if not included in authorizedEvaluation" + ); + results = propertyProvider("testGetters.y.y.", { + authorizedEvaluations: [["testGetters", "y", "y"]], + }); + Assert.deepEqual(results, { + isUnsafeGetter: true, + getterPath: ["testGetters", "y"], + }); + + info( + "Test children getters are executed if matching an authorizedEvaluation element" + ); + results = propertyProvider("testGetters.y.y.", { + authorizedEvaluations: [ + ["testGetters", "y"], + ["testGetters", "y", "y"], + ], + }); + test_has_result(results, "trim"); + + info("Test with number literals"); + results = propertyProvider("1."); + Assert.ok(results === null, "Does not complete on possible floating number"); + + results = propertyProvider("(1).."); + Assert.ok(results === null, "Does not complete on invalid syntax"); + + results = propertyProvider("(1.1.)."); + Assert.ok(results === null, "Does not complete on invalid syntax"); + + results = propertyProvider("1.."); + test_has_result(results, "toFixed"); + + results = propertyProvider("1 ."); + test_has_result(results, "toFixed"); + + results = propertyProvider("1\n."); + test_has_result(results, "toFixed"); + + results = propertyProvider(".1."); + test_has_result(results, "toFixed"); + + results = propertyProvider("1["); + test_has_result(results, `"toFixed"`); + + results = propertyProvider("1[toFixed"); + test_has_exact_results(results, [`"toFixed"`]); + + results = propertyProvider("1['toFixed"); + test_has_exact_results(results, ["'toFixed'"]); + + results = propertyProvider("1.1["); + test_has_result(results, `"toFixed"`); + + results = propertyProvider("(1)."); + test_has_result(results, "toFixed"); + + results = propertyProvider("(.1)."); + test_has_result(results, "toFixed"); + + results = propertyProvider("(1.1)."); + test_has_result(results, "toFixed"); + + results = propertyProvider("(1).toFixed"); + test_has_exact_results(results, ["toFixed"]); + + results = propertyProvider("(1)["); + test_has_result(results, `"toFixed"`); + + results = propertyProvider("(1.1)["); + test_has_result(results, `"toFixed"`); + + results = propertyProvider("(1)[toFixed"); + test_has_exact_results(results, [`"toFixed"`]); + + results = propertyProvider("(1)['toFixed"); + test_has_exact_results(results, ["'toFixed'"]); + + results = propertyProvider("(1).h"); + test_has_result(results, "hello"); + + results = propertyProvider("(1).hello.w"); + test_has_result(results, "world"); + + info("Test access on dot-notation invalid property name"); + results = propertyProvider("testHyphenated.prop"); + Assert.ok( + !results.matches.has("prop-A"), + "Does not return invalid property name on dot access" + ); + + results = propertyProvider("testHyphenated['prop"); + test_has_result(results, `'prop-A'`); + + results = propertyProvider(`//t`); + Assert.ok(results === null, "Does not complete in inline comment"); + + results = propertyProvider(`// t`); + Assert.ok( + results === null, + "Does not complete in inline comment after space" + ); + + results = propertyProvider(`//I'm a comment\nt`); + test_has_result(results, "testObject"); + + results = propertyProvider(`1/t`); + test_has_result(results, "testObject"); + + results = propertyProvider(`/* t`); + Assert.ok(results === null, "Does not complete in multiline comment"); + + results = propertyProvider(`/*I'm\nt`); + Assert.ok( + results === null, + "Does not complete in multiline comment after line break" + ); + + results = propertyProvider(`/*I'm a comment\n \t * /t`); + Assert.ok( + results === null, + "Does not complete in multiline comment after line break and invalid comment end" + ); + + results = propertyProvider(`/*I'm a comment\n \t */t`); + test_has_result(results, "testObject"); + + results = propertyProvider(`/*I'm a comment\n \t */\n\nt`); + test_has_result(results, "testObject"); + + info("Test local expression variables"); + results = propertyProvider("b", { expressionVars: ["a", "b", "c"] }); + test_has_result(results, "b"); + Assert.equal(results.matches.has("a"), false); + Assert.equal(results.matches.has("c"), false); + + info( + "Test that local expression variables are not included when accessing an object properties" + ); + results = propertyProvider("testObject.prop", { + expressionVars: ["propLocal"], + }); + Assert.equal(results.matches.has("propLocal"), false); + test_has_result(results, "propA"); + + results = propertyProvider("testObject['prop", { + expressionVars: ["propLocal"], + }); + test_has_result(results, "'propA'"); + Assert.equal(results.matches.has("propLocal"), false); + + info("Test that expression with optional chaining operator are completed"); + results = propertyProvider("testObject?.prop"); + test_has_result(results, "propA"); + + results = propertyProvider("testObject?.propA[0]?.propB?.to"); + test_has_result(results, "toString"); + + results = propertyProvider("testObject?.propA?.[0]?.propB?.to"); + test_has_result(results, "toString"); + + results = propertyProvider( + "testObject ?. propA[0] ?. propB ?. to" + ); + test_has_result(results, "toString"); + + results = propertyProvider("testObject?.[prop"); + test_has_result(results, '"propA"'); + + results = propertyProvider(`testObject?.["prop`); + test_has_result(results, '"propA"'); + + results = propertyProvider(`testObject?.['prop`); + test_has_result(results, `'propA'`); + + results = propertyProvider(`testObject?.["propA"]?.[0]?.["propB"]?.["to`); + test_has_result(results, `"toString"`); + + results = propertyProvider( + `testObject ?. ["propA"] ?. [0] ?. ["propB"] ?. ['to` + ); + test_has_result(results, "'toString'"); + + results = propertyProvider("[1,2,3]?."); + test_has_result(results, "indexOf"); + + results = propertyProvider("'foo'?."); + test_has_result(results, "charAt"); + + results = propertyProvider("1?."); + test_has_result(results, "toFixed"); + + // check this doesn't throw since `propC` is not defined. + results = propertyProvider("testObject?.propC?.this?.does?.not?.exist?.d"); + + // check that ternary operator isn't mistaken for optional chaining + results = propertyProvider(`true?.3.to`); + test_has_result(results, `toExponential`); + + results = propertyProvider(`true?.3?.to`); + test_has_result(results, `toExponential`); + + // Test more ternary + results = propertyProvider(`true?t`); + test_has_result(results, `testObject`); + + results = propertyProvider(`true??t`); + test_has_result(results, `testObject`); + + results = propertyProvider(`true?/* comment */t`); + test_has_result(results, `testObject`); + + results = propertyProvider(`true?<t`); + test_has_no_results(results); + + // Test autocompletion on debugger statement does not throw + results = propertyProvider(`debugger.`); + Assert.ok(results === null, "Does not complete a debugger keyword"); +} + +/** + * A helper that ensures an empty array of results were found. + * @param Object results + * The results returned by JSPropertyProvider. + */ +function test_has_no_results(results) { + Assert.notEqual(results, null); + Assert.equal(results.matches.size, 0); +} +/** + * A helper that ensures (required) results were found. + * @param Object results + * The results returned by JSPropertyProvider. + * @param String requiredSuggestion + * A suggestion that must be found from the results. + */ +function test_has_result(results, requiredSuggestion) { + Assert.notEqual(results, null); + Assert.ok(results.matches.size > 0); + Assert.ok( + results.matches.has(requiredSuggestion), + `<${requiredSuggestion}> found in ${[...results.matches.values()].join( + " - " + )}` + ); +} + +/** + * A helper that ensures results are the expected ones. + * @param Object results + * The results returned by JSPropertyProvider. + * @param Array expectedMatches + * An array of the properties that should be returned by JsPropertyProvider. + */ +function test_has_exact_results(results, expectedMatches) { + Assert.deepEqual([...results.matches], expectedMatches); +} diff --git a/devtools/shared/webconsole/test/xpcshell/test_network_helper.js b/devtools/shared/webconsole/test/xpcshell/test_network_helper.js new file mode 100644 index 0000000000..c3c85f71ea --- /dev/null +++ b/devtools/shared/webconsole/test/xpcshell/test_network_helper.js @@ -0,0 +1,91 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; +const { require } = ChromeUtils.import("resource://devtools/shared/Loader.jsm"); + +Object.defineProperty(this, "NetworkHelper", { + get: function() { + return require("devtools/shared/webconsole/network-helper"); + }, + configurable: true, + writeable: false, + enumerable: true, +}); + +function run_test() { + test_isTextMimeType(); + test_parseCookieHeader(); +} + +function test_isTextMimeType() { + Assert.equal(NetworkHelper.isTextMimeType("text/plain"), true); + Assert.equal(NetworkHelper.isTextMimeType("application/javascript"), true); + Assert.equal(NetworkHelper.isTextMimeType("application/json"), true); + Assert.equal(NetworkHelper.isTextMimeType("text/css"), true); + Assert.equal(NetworkHelper.isTextMimeType("text/html"), true); + Assert.equal(NetworkHelper.isTextMimeType("image/svg+xml"), true); + Assert.equal(NetworkHelper.isTextMimeType("application/xml"), true); + + // Test custom JSON subtype + Assert.equal( + NetworkHelper.isTextMimeType("application/vnd.tent.posts-feed.v0+json"), + true + ); + Assert.equal( + NetworkHelper.isTextMimeType("application/vnd.tent.posts-feed.v0-json"), + true + ); + // Test custom XML subtype + Assert.equal( + NetworkHelper.isTextMimeType("application/vnd.tent.posts-feed.v0+xml"), + true + ); + Assert.equal( + NetworkHelper.isTextMimeType("application/vnd.tent.posts-feed.v0-xml"), + false + ); + // Test case-insensitive + Assert.equal( + NetworkHelper.isTextMimeType("application/vnd.BIG-CORP+json"), + true + ); + // Test non-text type + Assert.equal(NetworkHelper.isTextMimeType("image/png"), false); + // Test invalid types + Assert.equal(NetworkHelper.isTextMimeType("application/foo-+json"), false); + Assert.equal(NetworkHelper.isTextMimeType("application/-foo+json"), false); + Assert.equal( + NetworkHelper.isTextMimeType("application/foo--bar+json"), + false + ); + + // Test we do not cause internal errors with unoptimized regex. Bug 961097 + Assert.equal( + NetworkHelper.isTextMimeType("application/vnd.google.safebrowsing-chunk"), + false + ); +} + +function test_parseCookieHeader() { + let result = NetworkHelper.parseSetCookieHeader("Test=1; SameSite=Strict"); + Assert.deepEqual(result, [{ name: "Test", value: "1", samesite: "Strict" }]); + + result = NetworkHelper.parseSetCookieHeader("Test=1; SameSite=strict"); + Assert.deepEqual(result, [{ name: "Test", value: "1", samesite: "Strict" }]); + + result = NetworkHelper.parseSetCookieHeader("Test=1; SameSite=STRICT"); + Assert.deepEqual(result, [{ name: "Test", value: "1", samesite: "Strict" }]); + + result = NetworkHelper.parseSetCookieHeader("Test=1; SameSite=None"); + Assert.deepEqual(result, [{ name: "Test", value: "1", samesite: "None" }]); + + result = NetworkHelper.parseSetCookieHeader("Test=1; SameSite=NONE"); + Assert.deepEqual(result, [{ name: "Test", value: "1", samesite: "None" }]); + + result = NetworkHelper.parseSetCookieHeader("Test=1; SameSite=lax"); + Assert.deepEqual(result, [{ name: "Test", value: "1", samesite: "Lax" }]); + + result = NetworkHelper.parseSetCookieHeader("Test=1; SameSite=Lax"); + Assert.deepEqual(result, [{ name: "Test", value: "1", samesite: "Lax" }]); +} diff --git a/devtools/shared/webconsole/test/xpcshell/test_security-info-certificate.js b/devtools/shared/webconsole/test/xpcshell/test_security-info-certificate.js new file mode 100644 index 0000000000..be8a5d8381 --- /dev/null +++ b/devtools/shared/webconsole/test/xpcshell/test_security-info-certificate.js @@ -0,0 +1,96 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ +"use strict"; + +// Tests that NetworkHelper.parseCertificateInfo parses certificate information +// correctly. + +const { require } = ChromeUtils.import("resource://devtools/shared/Loader.jsm"); + +Object.defineProperty(this, "NetworkHelper", { + get: function() { + return require("devtools/shared/webconsole/network-helper"); + }, + configurable: true, + writeable: false, + enumerable: true, +}); + +const DUMMY_CERT = { + commonName: "cn", + organization: "o", + organizationalUnit: "ou", + issuerCommonName: "issuerCN", + issuerOrganization: "issuerO", + issuerOrganizationUnit: "issuerOU", + sha256Fingerprint: "qwertyuiopoiuytrewq", + sha1Fingerprint: "qwertyuiop", + validity: { + notBeforeLocalDay: "yesterday", + notAfterLocalDay: "tomorrow", + }, +}; + +function run_test() { + info("Testing NetworkHelper.parseCertificateInfo."); + + const result = NetworkHelper.parseCertificateInfo(DUMMY_CERT); + + // Subject + equal( + result.subject.commonName, + DUMMY_CERT.commonName, + "Common name is correct." + ); + equal( + result.subject.organization, + DUMMY_CERT.organization, + "Organization is correct." + ); + equal( + result.subject.organizationUnit, + DUMMY_CERT.organizationUnit, + "Organizational unit is correct." + ); + + // Issuer + equal( + result.issuer.commonName, + DUMMY_CERT.issuerCommonName, + "Common name of the issuer is correct." + ); + equal( + result.issuer.organization, + DUMMY_CERT.issuerOrganization, + "Organization of the issuer is correct." + ); + equal( + result.issuer.organizationUnit, + DUMMY_CERT.issuerOrganizationUnit, + "Organizational unit of the issuer is correct." + ); + + // Validity + equal( + result.validity.start, + DUMMY_CERT.validity.notBeforeLocalDay, + "Start of the validity period is correct." + ); + equal( + result.validity.end, + DUMMY_CERT.validity.notAfterLocalDay, + "End of the validity period is correct." + ); + + // Fingerprints + equal( + result.fingerprint.sha1, + DUMMY_CERT.sha1Fingerprint, + "Certificate SHA1 fingerprint is correct." + ); + equal( + result.fingerprint.sha256, + DUMMY_CERT.sha256Fingerprint, + "Certificate SHA256 fingerprint is correct." + ); +} diff --git a/devtools/shared/webconsole/test/xpcshell/test_security-info-parser.js b/devtools/shared/webconsole/test/xpcshell/test_security-info-parser.js new file mode 100644 index 0000000000..36ffa49cb0 --- /dev/null +++ b/devtools/shared/webconsole/test/xpcshell/test_security-info-parser.js @@ -0,0 +1,67 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ +"use strict"; + +// Test that NetworkHelper.parseSecurityInfo returns correctly formatted object. + +const { require } = ChromeUtils.import("resource://devtools/shared/Loader.jsm"); + +Object.defineProperty(this, "NetworkHelper", { + get: function() { + return require("devtools/shared/webconsole/network-helper"); + }, + configurable: true, + writeable: false, + enumerable: true, +}); + +const wpl = Ci.nsIWebProgressListener; +const MockCertificate = { + commonName: "cn", + organization: "o", + organizationalUnit: "ou", + issuerCommonName: "issuerCN", + issuerOrganization: "issuerO", + issuerOrganizationUnit: "issuerOU", + sha256Fingerprint: "qwertyuiopoiuytrewq", + sha1Fingerprint: "qwertyuiop", + validity: { + notBeforeLocalDay: "yesterday", + notAfterLocalDay: "tomorrow", + }, +}; + +// This *cannot* be used as an nsITransportSecurityInfo (since that interface is +// builtinclass) but the methods being tested aren't defined by XPCOM and aren't +// calling QueryInterface, so this usage is fine. +const MockSecurityInfo = { + securityState: wpl.STATE_IS_SECURE, + errorCode: 0, + cipherName: "TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256", + // TLS_VERSION_1_2 + protocolVersion: 3, + serverCert: MockCertificate, +}; + +function run_test() { + const result = NetworkHelper.parseSecurityInfo(MockSecurityInfo, {}); + + equal(result.state, "secure", "State is correct."); + + equal( + result.cipherSuite, + MockSecurityInfo.cipherName, + "Cipher suite is correct." + ); + + equal(result.protocolVersion, "TLSv1.2", "Protocol version is correct."); + + deepEqual( + result.cert, + NetworkHelper.parseCertificateInfo(MockCertificate), + "Certificate information is correct." + ); + + equal(result.hpkp, false, "HPKP is false when URI is not available."); + equal(result.hsts, false, "HSTS is false when URI is not available."); +} diff --git a/devtools/shared/webconsole/test/xpcshell/test_security-info-protocol-version.js b/devtools/shared/webconsole/test/xpcshell/test_security-info-protocol-version.js new file mode 100644 index 0000000000..72c933676e --- /dev/null +++ b/devtools/shared/webconsole/test/xpcshell/test_security-info-protocol-version.js @@ -0,0 +1,59 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ +"use strict"; + +// Tests that NetworkHelper.formatSecurityProtocol returns correct +// protocol version strings. + +const { require } = ChromeUtils.import("resource://devtools/shared/Loader.jsm"); + +Object.defineProperty(this, "NetworkHelper", { + get: function() { + return require("devtools/shared/webconsole/network-helper"); + }, + configurable: true, + writeable: false, + enumerable: true, +}); + +const TEST_CASES = [ + { + description: "TLS_VERSION_1", + input: 1, + expected: "TLSv1", + }, + { + description: "TLS_VERSION_1.1", + input: 2, + expected: "TLSv1.1", + }, + { + description: "TLS_VERSION_1.2", + input: 3, + expected: "TLSv1.2", + }, + { + description: "TLS_VERSION_1.3", + input: 4, + expected: "TLSv1.3", + }, + { + description: "invalid version", + input: -1, + expected: "Unknown", + }, +]; + +function run_test() { + info("Testing NetworkHelper.formatSecurityProtocol."); + + for (const { description, input, expected } of TEST_CASES) { + info("Testing " + description); + + equal( + NetworkHelper.formatSecurityProtocol(input), + expected, + "Got the expected protocol string." + ); + } +} diff --git a/devtools/shared/webconsole/test/xpcshell/test_security-info-state.js b/devtools/shared/webconsole/test/xpcshell/test_security-info-state.js new file mode 100644 index 0000000000..b3a9770949 --- /dev/null +++ b/devtools/shared/webconsole/test/xpcshell/test_security-info-state.js @@ -0,0 +1,113 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ +"use strict"; + +// Tests that security info parser gives correct general security state for +// different cases. + +const { require } = ChromeUtils.import("resource://devtools/shared/Loader.jsm"); + +Object.defineProperty(this, "NetworkHelper", { + get: function() { + return require("devtools/shared/webconsole/network-helper"); + }, + configurable: true, + writeable: false, + enumerable: true, +}); + +const wpl = Ci.nsIWebProgressListener; + +// This *cannot* be used as an nsITransportSecurityInfo (since that interface is +// builtinclass) but the methods being tested aren't defined by XPCOM and aren't +// calling QueryInterface, so this usage is fine. +const MockSecurityInfo = { + securityState: wpl.STATE_IS_BROKEN, + errorCode: 0, + // nsISSLStatus.TLS_VERSION_1_2 + protocolVersion: 3, + cipherName: "TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256", +}; + +function run_test() { + test_nullSecurityInfo(); + test_insecureSecurityInfoWithNSSError(); + test_insecureSecurityInfoWithoutNSSError(); + test_brokenSecurityInfo(); + test_secureSecurityInfo(); +} + +/** + * Test that undefined security information is returns "insecure". + */ +function test_nullSecurityInfo() { + const result = NetworkHelper.parseSecurityInfo(null, {}); + equal( + result.state, + "insecure", + "state == 'insecure' when securityInfo was undefined" + ); +} + +/** + * Test that STATE_IS_INSECURE with NSSError returns "broken" + */ +function test_insecureSecurityInfoWithNSSError() { + MockSecurityInfo.securityState = wpl.STATE_IS_INSECURE; + + // Taken from security/manager/ssl/tests/unit/head_psm.js. + MockSecurityInfo.errorCode = -8180; + + const result = NetworkHelper.parseSecurityInfo(MockSecurityInfo, {}); + equal( + result.state, + "broken", + "state == 'broken' if securityState contains STATE_IS_INSECURE flag AND " + + "errorCode is NSS error." + ); + + MockSecurityInfo.errorCode = 0; +} + +/** + * Test that STATE_IS_INSECURE without NSSError returns "insecure" + */ +function test_insecureSecurityInfoWithoutNSSError() { + MockSecurityInfo.securityState = wpl.STATE_IS_INSECURE; + + const result = NetworkHelper.parseSecurityInfo(MockSecurityInfo, {}); + equal( + result.state, + "insecure", + "state == 'insecure' if securityState contains STATE_IS_INSECURE flag BUT " + + "errorCode is not NSS error." + ); +} + +/** + * Test that STATE_IS_SECURE returns "secure" + */ +function test_secureSecurityInfo() { + MockSecurityInfo.securityState = wpl.STATE_IS_SECURE; + + const result = NetworkHelper.parseSecurityInfo(MockSecurityInfo, {}); + equal( + result.state, + "secure", + "state == 'secure' if securityState contains STATE_IS_SECURE flag" + ); +} + +/** + * Test that STATE_IS_BROKEN returns "weak" + */ +function test_brokenSecurityInfo() { + MockSecurityInfo.securityState = wpl.STATE_IS_BROKEN; + + const result = NetworkHelper.parseSecurityInfo(MockSecurityInfo, {}); + equal( + result.state, + "weak", + "state == 'weak' if securityState contains STATE_IS_BROKEN flag" + ); +} diff --git a/devtools/shared/webconsole/test/xpcshell/test_security-info-static-hpkp.js b/devtools/shared/webconsole/test/xpcshell/test_security-info-static-hpkp.js new file mode 100644 index 0000000000..4cc0aa7dd4 --- /dev/null +++ b/devtools/shared/webconsole/test/xpcshell/test_security-info-static-hpkp.js @@ -0,0 +1,47 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ +"use strict"; + +// Test that NetworkHelper.parseSecurityInfo correctly detects static hpkp pins + +const { require } = ChromeUtils.import("resource://devtools/shared/Loader.jsm"); +const Services = require("Services"); + +Object.defineProperty(this, "NetworkHelper", { + get: function() { + return require("devtools/shared/webconsole/network-helper"); + }, + configurable: true, + writeable: false, + enumerable: true, +}); + +const wpl = Ci.nsIWebProgressListener; + +// This *cannot* be used as an nsITransportSecurityInfo (since that interface is +// builtinclass) but the methods being tested aren't defined by XPCOM and aren't +// calling QueryInterface, so this usage is fine. +const MockSecurityInfo = { + securityState: wpl.STATE_IS_SECURE, + errorCode: 0, + cipherName: "TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256", + // TLS_VERSION_1_2 + protocolVersion: 3, + serverCert: { + validity: {}, + }, +}; + +const MockHttpInfo = { + hostname: "include-subdomains.pinning.example.com", + private: false, +}; + +function run_test() { + Services.prefs.setIntPref("security.cert_pinning.enforcement_level", 1); + const result = NetworkHelper.parseSecurityInfo( + MockSecurityInfo, + MockHttpInfo + ); + equal(result.hpkp, true, "Static HPKP detected."); +} diff --git a/devtools/shared/webconsole/test/xpcshell/test_security-info-weakness-reasons.js b/devtools/shared/webconsole/test/xpcshell/test_security-info-weakness-reasons.js new file mode 100644 index 0000000000..b5941214a9 --- /dev/null +++ b/devtools/shared/webconsole/test/xpcshell/test_security-info-weakness-reasons.js @@ -0,0 +1,50 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ +"use strict"; + +// Tests that NetworkHelper.getReasonsForWeakness returns correct reasons for +// weak requests. + +const { require } = ChromeUtils.import("resource://devtools/shared/Loader.jsm"); + +Object.defineProperty(this, "NetworkHelper", { + get: function() { + return require("devtools/shared/webconsole/network-helper"); + }, + configurable: true, + writeable: false, + enumerable: true, +}); + +const wpl = Ci.nsIWebProgressListener; +const TEST_CASES = [ + { + description: "weak cipher", + input: wpl.STATE_IS_BROKEN | wpl.STATE_USES_WEAK_CRYPTO, + expected: ["cipher"], + }, + { + description: "only STATE_IS_BROKEN flag", + input: wpl.STATE_IS_BROKEN, + expected: [], + }, + { + description: "only STATE_IS_SECURE flag", + input: wpl.STATE_IS_SECURE, + expected: [], + }, +]; + +function run_test() { + info("Testing NetworkHelper.getReasonsForWeakness."); + + for (const { description, input, expected } of TEST_CASES) { + info("Testing " + description); + + deepEqual( + NetworkHelper.getReasonsForWeakness(input), + expected, + "Got the expected reasons for weakness." + ); + } +} diff --git a/devtools/shared/webconsole/test/xpcshell/test_throttle.js b/devtools/shared/webconsole/test/xpcshell/test_throttle.js new file mode 100644 index 0000000000..2150786b12 --- /dev/null +++ b/devtools/shared/webconsole/test/xpcshell/test_throttle.js @@ -0,0 +1,163 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/* eslint-disable mozilla/use-chromeutils-generateqi */ + +const { require } = ChromeUtils.import("resource://devtools/shared/Loader.jsm"); +const { + NetworkThrottleManager, +} = require("devtools/shared/webconsole/throttle"); +const nsIScriptableInputStream = Ci.nsIScriptableInputStream; + +function TestStreamListener() { + this.state = "initial"; +} +TestStreamListener.prototype = { + onStartRequest: function() { + this.setState("start"); + }, + + onStopRequest: function() { + this.setState("stop"); + }, + + onDataAvailable: function(request, inputStream, offset, count) { + const sin = Cc["@mozilla.org/scriptableinputstream;1"].createInstance( + nsIScriptableInputStream + ); + sin.init(inputStream); + this.data = sin.read(count); + this.setState("data"); + }, + + setState: function(state) { + this.state = state; + if (this._deferred) { + this._deferred.resolve(state); + this._deferred = null; + } + }, + + onStateChanged: function() { + if (!this._deferred) { + let resolve, reject; + const promise = new Promise(function(res, rej) { + resolve = res; + reject = rej; + }); + this._deferred = { resolve, reject, promise }; + } + return this._deferred.promise; + }, +}; + +function TestChannel() { + this.state = "initial"; + this.testListener = new TestStreamListener(); + this._throttleQueue = null; +} +TestChannel.prototype = { + QueryInterface: function() { + return this; + }, + + get throttleQueue() { + return this._throttleQueue; + }, + + set throttleQueue(q) { + this._throttleQueue = q; + this.state = "throttled"; + }, + + setNewListener: function(listener) { + this.listener = listener; + this.state = "listener"; + return this.testListener; + }, +}; + +add_task(async function() { + const throttler = new NetworkThrottleManager({ + latencyMean: 1, + latencyMax: 1, + downloadBPSMean: 500, + downloadBPSMax: 500, + uploadBPSMean: 500, + uploadBPSMax: 500, + }); + + const uploadChannel = new TestChannel(); + throttler.manageUpload(uploadChannel); + equal( + uploadChannel.state, + "throttled", + "NetworkThrottleManager set throttleQueue" + ); + + const downloadChannel = new TestChannel(); + const testListener = downloadChannel.testListener; + + const listener = throttler.manage(downloadChannel); + equal( + downloadChannel.state, + "listener", + "NetworkThrottleManager called setNewListener" + ); + + equal(testListener.state, "initial", "test listener in initial state"); + + // This method must be passed through immediately. + listener.onStartRequest(null); + equal(testListener.state, "start", "test listener started"); + + const TEST_INPUT = "hi bob"; + + const testStream = Cc["@mozilla.org/storagestream;1"].createInstance( + Ci.nsIStorageStream + ); + testStream.init(512, 512); + const out = testStream.getOutputStream(0); + out.write(TEST_INPUT, TEST_INPUT.length); + out.close(); + const testInputStream = testStream.newInputStream(0); + + const activityDistributor = Cc[ + "@mozilla.org/network/http-activity-distributor;1" + ].getService(Ci.nsIHttpActivityDistributor); + let activitySeen = false; + listener.addActivityCallback( + () => { + activitySeen = true; + }, + null, + null, + null, + activityDistributor.ACTIVITY_SUBTYPE_RESPONSE_COMPLETE, + null, + TEST_INPUT.length, + null + ); + + // onDataAvailable is required to immediately read the data. + listener.onDataAvailable(null, testInputStream, 0, 6); + equal(testInputStream.available(), 0, "no more data should be available"); + equal( + testListener.state, + "start", + "test listener should not have received data" + ); + equal(activitySeen, false, "activity not distributed yet"); + + let newState = await testListener.onStateChanged(); + equal(newState, "data", "test listener received data"); + equal(testListener.data, TEST_INPUT, "test listener received all the data"); + equal(activitySeen, true, "activity has been distributed"); + + const onChange = testListener.onStateChanged(); + listener.onStopRequest(null, null); + newState = await onChange; + equal(newState, "stop", "onStateChanged reported"); +}); diff --git a/devtools/shared/webconsole/test/xpcshell/xpcshell.ini b/devtools/shared/webconsole/test/xpcshell/xpcshell.ini new file mode 100644 index 0000000000..057e33da98 --- /dev/null +++ b/devtools/shared/webconsole/test/xpcshell/xpcshell.ini @@ -0,0 +1,17 @@ +[DEFAULT] +tags = devtools +head = +firefox-appdir = browser +skip-if = toolkit == 'android' +support-files = + +[test_analyze_input_string.js] +[test_js_property_provider.js] +[test_network_helper.js] +[test_security-info-certificate.js] +[test_security-info-parser.js] +[test_security-info-protocol-version.js] +[test_security-info-state.js] +[test_security-info-static-hpkp.js] +[test_security-info-weakness-reasons.js] +[test_throttle.js] diff --git a/devtools/shared/webconsole/throttle.js b/devtools/shared/webconsole/throttle.js new file mode 100644 index 0000000000..2f40605ee8 --- /dev/null +++ b/devtools/shared/webconsole/throttle.js @@ -0,0 +1,469 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +"use strict"; + +const { CC, Ci, Cc } = require("chrome"); + +const ArrayBufferInputStream = CC( + "@mozilla.org/io/arraybuffer-input-stream;1", + "nsIArrayBufferInputStream" +); +const BinaryInputStream = CC( + "@mozilla.org/binaryinputstream;1", + "nsIBinaryInputStream", + "setInputStream" +); + +loader.lazyServiceGetter( + this, + "gActivityDistributor", + "@mozilla.org/network/http-activity-distributor;1", + "nsIHttpActivityDistributor" +); + +const ChromeUtils = require("ChromeUtils"); +const { setTimeout } = require("resource://gre/modules/Timer.jsm"); + +/** + * Construct a new nsIStreamListener that buffers data and provides a + * method to notify another listener when data is available. This is + * used to throttle network data on a per-channel basis. + * + * After construction, @see setOriginalListener must be called on the + * new object. + * + * @param {NetworkThrottleQueue} queue the NetworkThrottleQueue to + * which status changes should be reported + */ +function NetworkThrottleListener(queue) { + this.queue = queue; + this.pendingData = []; + this.pendingException = null; + this.offset = 0; + this.responseStarted = false; + this.activities = {}; +} + +NetworkThrottleListener.prototype = { + QueryInterface: ChromeUtils.generateQI([ + "nsIStreamListener", + "nsIInterfaceRequestor", + ]), + + /** + * Set the original listener for this object. The original listener + * will receive requests from this object when the queue allows data + * through. + * + * @param {nsIStreamListener} originalListener the original listener + * for the channel, to which all requests will be sent + */ + setOriginalListener: function(originalListener) { + this.originalListener = originalListener; + }, + + /** + * @see nsIStreamListener.onStartRequest. + */ + onStartRequest: function(request) { + this.originalListener.onStartRequest(request); + this.queue.start(this); + }, + + /** + * @see nsIStreamListener.onStopRequest. + */ + onStopRequest: function(request, statusCode) { + this.pendingData.push({ request, statusCode }); + this.queue.dataAvailable(this); + }, + + /** + * @see nsIStreamListener.onDataAvailable. + */ + onDataAvailable: function(request, inputStream, offset, count) { + if (this.pendingException) { + throw this.pendingException; + } + + const bin = new BinaryInputStream(inputStream); + const bytes = new ArrayBuffer(count); + bin.readArrayBuffer(count, bytes); + + const stream = new ArrayBufferInputStream(); + stream.setData(bytes, 0, count); + + this.pendingData.push({ request, stream, count }); + this.queue.dataAvailable(this); + }, + + /** + * Allow some buffered data from this object to be forwarded to this + * object's originalListener. + * + * @param {Number} bytesPermitted The maximum number of bytes + * permitted to be sent. + * @return {Object} an object of the form {length, done}, where + * |length| is the number of bytes actually forwarded, and + * |done| is a boolean indicating whether this particular + * request has been completed. (A NetworkThrottleListener + * may be queued multiple times, so this does not mean that + * all available data has been sent.) + */ + sendSomeData: function(bytesPermitted) { + if (this.pendingData.length === 0) { + // Shouldn't happen. + return { length: 0, done: true }; + } + + const { request, stream, count, statusCode } = this.pendingData[0]; + + if (statusCode !== undefined) { + this.pendingData.shift(); + this.originalListener.onStopRequest(request, statusCode); + return { length: 0, done: true }; + } + + if (bytesPermitted > count) { + bytesPermitted = count; + } + + try { + this.originalListener.onDataAvailable( + request, + stream, + this.offset, + bytesPermitted + ); + } catch (e) { + this.pendingException = e; + } + + let done = false; + if (bytesPermitted === count) { + this.pendingData.shift(); + done = true; + } else { + this.pendingData[0].count -= bytesPermitted; + } + + this.offset += bytesPermitted; + // Maybe our state has changed enough to emit an event. + this.maybeEmitEvents(); + + return { length: bytesPermitted, done }; + }, + + /** + * Return the number of pending data requests available for this + * listener. + */ + pendingCount: function() { + return this.pendingData.length; + }, + + /** + * This is called when an http activity event is delivered. This + * object delays the event until the appropriate moment. + */ + addActivityCallback: function( + callback, + httpActivity, + channel, + activityType, + activitySubtype, + timestamp, + extraSizeData, + extraStringData + ) { + const datum = { + callback, + httpActivity, + channel, + activityType, + activitySubtype, + extraSizeData, + extraStringData, + }; + this.activities[activitySubtype] = datum; + + if ( + activitySubtype === + gActivityDistributor.ACTIVITY_SUBTYPE_RESPONSE_COMPLETE + ) { + this.totalSize = extraSizeData; + } + + this.maybeEmitEvents(); + }, + + /** + * This is called for a download throttler when the latency timeout + * has ended. + */ + responseStart: function() { + this.responseStarted = true; + this.maybeEmitEvents(); + }, + + /** + * Check our internal state and emit any http activity events as + * needed. Note that we wait until both our internal state has + * changed and we've received the real http activity event from + * platform. This approach ensures we can both pass on the correct + * data from the original event, and update the reported time to be + * consistent with the delay we're introducing. + */ + maybeEmitEvents: function() { + if (this.responseStarted) { + this.maybeEmit(gActivityDistributor.ACTIVITY_SUBTYPE_RESPONSE_START); + this.maybeEmit(gActivityDistributor.ACTIVITY_SUBTYPE_RESPONSE_HEADER); + } + + if (this.totalSize !== undefined && this.offset >= this.totalSize) { + this.maybeEmit(gActivityDistributor.ACTIVITY_SUBTYPE_RESPONSE_COMPLETE); + this.maybeEmit(gActivityDistributor.ACTIVITY_SUBTYPE_TRANSACTION_CLOSE); + } + }, + + /** + * Emit an event for |code|, if the appropriate entry in + * |activities| is defined. + */ + maybeEmit: function(code) { + if (this.activities[code] !== undefined) { + const { + callback, + httpActivity, + channel, + activityType, + activitySubtype, + extraSizeData, + extraStringData, + } = this.activities[code]; + const now = Date.now() * 1000; + callback( + httpActivity, + channel, + activityType, + activitySubtype, + now, + extraSizeData, + extraStringData + ); + this.activities[code] = undefined; + } + }, +}; + +/** + * Construct a new queue that can be used to throttle the network for + * a group of related network requests. + * + * meanBPS {Number} Mean bytes per second. + * maxBPS {Number} Maximum bytes per second. + * latencyMean {Number} Mean latency in milliseconds. + * latencyMax {Number} Maximum latency in milliseconds. + */ +function NetworkThrottleQueue(meanBPS, maxBPS, latencyMean, latencyMax) { + this.meanBPS = meanBPS; + this.maxBPS = maxBPS; + this.latencyMean = latencyMean; + this.latencyMax = latencyMax; + + this.pendingRequests = new Set(); + this.downloadQueue = []; + this.previousReads = []; + + this.pumping = false; +} + +NetworkThrottleQueue.prototype = { + /** + * A helper function that, given a mean and a maximum, returns a + * random integer between (mean - (max - mean)) and max. + */ + random: function(mean, max) { + return mean - (max - mean) + Math.floor(2 * (max - mean) * Math.random()); + }, + + /** + * A helper function that lets the indicating listener start sending + * data. This is called after the initial round trip time for the + * listener has elapsed. + */ + allowDataFrom: function(throttleListener) { + throttleListener.responseStart(); + this.pendingRequests.delete(throttleListener); + const count = throttleListener.pendingCount(); + for (let i = 0; i < count; ++i) { + this.downloadQueue.push(throttleListener); + } + this.pump(); + }, + + /** + * Notice a new listener object. This is called by the + * NetworkThrottleListener when the request has started. Initially + * a new listener object is put into a "pending" state, until the + * round-trip time has elapsed. This is used to simulate latency. + * + * @param {NetworkThrottleListener} throttleListener the new listener + */ + start: function(throttleListener) { + this.pendingRequests.add(throttleListener); + const delay = this.random(this.latencyMean, this.latencyMax); + if (delay > 0) { + setTimeout(() => this.allowDataFrom(throttleListener), delay); + } else { + this.allowDataFrom(throttleListener); + } + }, + + /** + * Note that new data is available for a given listener. Each time + * data is available, the listener will be re-queued. + * + * @param {NetworkThrottleListener} throttleListener the listener + * which has data available. + */ + dataAvailable: function(throttleListener) { + if (!this.pendingRequests.has(throttleListener)) { + this.downloadQueue.push(throttleListener); + this.pump(); + } + }, + + /** + * An internal function that permits individual listeners to send + * data. + */ + pump: function() { + // A redirect will cause two NetworkThrottleListeners to be on a + // listener chain. In this case, we might recursively call into + // this method. Avoid infinite recursion here. + if (this.pumping) { + return; + } + this.pumping = true; + + const now = Date.now(); + const oneSecondAgo = now - 1000; + + while ( + this.previousReads.length && + this.previousReads[0].when < oneSecondAgo + ) { + this.previousReads.shift(); + } + + const totalBytes = this.previousReads.reduce((sum, elt) => { + return sum + elt.numBytes; + }, 0); + + let thisSliceBytes = this.random(this.meanBPS, this.maxBPS); + if (totalBytes < thisSliceBytes) { + thisSliceBytes -= totalBytes; + let readThisTime = 0; + while (thisSliceBytes > 0 && this.downloadQueue.length) { + const { length, done } = this.downloadQueue[0].sendSomeData( + thisSliceBytes + ); + thisSliceBytes -= length; + readThisTime += length; + if (done) { + this.downloadQueue.shift(); + } + } + this.previousReads.push({ when: now, numBytes: readThisTime }); + } + + // If there is more data to download, then schedule ourselves for + // one second after the oldest previous read. + if (this.downloadQueue.length) { + const when = this.previousReads[0].when + 1000; + setTimeout(this.pump.bind(this), when - now); + } + + this.pumping = false; + }, +}; + +/** + * Construct a new object that can be used to throttle the network for + * a group of related network requests. + * + * @param {Object} An object with the following attributes: + * latencyMean {Number} Mean latency in milliseconds. + * latencyMax {Number} Maximum latency in milliseconds. + * downloadBPSMean {Number} Mean bytes per second for downloads. + * downloadBPSMax {Number} Maximum bytes per second for downloads. + * uploadBPSMean {Number} Mean bytes per second for uploads. + * uploadBPSMax {Number} Maximum bytes per second for uploads. + * + * Download throttling will not be done if downloadBPSMean and + * downloadBPSMax are <= 0. Upload throttling will not be done if + * uploadBPSMean and uploadBPSMax are <= 0. + */ +function NetworkThrottleManager({ + latencyMean, + latencyMax, + downloadBPSMean, + downloadBPSMax, + uploadBPSMean, + uploadBPSMax, +}) { + if (downloadBPSMax <= 0 && downloadBPSMean <= 0) { + this.downloadQueue = null; + } else { + this.downloadQueue = new NetworkThrottleQueue( + downloadBPSMean, + downloadBPSMax, + latencyMean, + latencyMax + ); + } + if (uploadBPSMax <= 0 && uploadBPSMean <= 0) { + this.uploadQueue = null; + } else { + this.uploadQueue = Cc[ + "@mozilla.org/network/throttlequeue;1" + ].createInstance(Ci.nsIInputChannelThrottleQueue); + this.uploadQueue.init(uploadBPSMean, uploadBPSMax); + } +} +exports.NetworkThrottleManager = NetworkThrottleManager; + +NetworkThrottleManager.prototype = { + /** + * Create a new NetworkThrottleListener for a given channel and + * install it using |setNewListener|. + * + * @param {nsITraceableChannel} channel the channel to manage + * @return {NetworkThrottleListener} the new listener, or null if + * download throttling is not being done. + */ + manage: function(channel) { + if (this.downloadQueue) { + const listener = new NetworkThrottleListener(this.downloadQueue); + const originalListener = channel.setNewListener(listener); + listener.setOriginalListener(originalListener); + return listener; + } + return null; + }, + + /** + * Throttle uploads taking place on the given channel. + * + * @param {nsITraceableChannel} channel the channel to manage + */ + manageUpload: function(channel) { + if (this.uploadQueue) { + channel = channel.QueryInterface(Ci.nsIThrottledInputChannel); + channel.throttleQueue = this.uploadQueue; + } + }, +}; |