Bug 1313745 - Use Assert.jsm in IRC code. r=aleth

This commit is contained in:
Patrick Cloke 2017-01-25 09:01:23 -05:00
Родитель f994407328
Коммит cba9771237
18 изменённых файлов: 117 добавлений и 124 удалений

Просмотреть файл

@ -71,7 +71,7 @@ function ircMessage(aData, aOrigin) {
// here, we want to ignore the first value (which is empty).
message.params = temp[4] ? temp[4].split(" ").slice(1) : [];
// Last parameter can contain spaces or be an empty string.
if (temp[5] != undefined)
if (temp[5] !== undefined)
message.params.push(temp[5]);
// Handle the prefix part of the message per RFC 2812 Section 2.3.

Просмотреть файл

@ -101,7 +101,7 @@ function serverErrorMessage(aAccount, aMessage, aError) {
if (!aAccount._showServerTab)
return true;
return writeMessage(aAccount, aMessage, aError, "error")
return writeMessage(aAccount, aMessage, aError, "error");
}
function addMotd(aAccount, aMessage) {
@ -125,7 +125,7 @@ function addMotd(aAccount, aMessage) {
// Oh, also some servers don't send a RPL_ENDOFMOTD (e.g. irc.ppy.sh), so if
// we don't receive another MOTD message after 1 second, consider it to be
// RPL_ENDOFMOTD.
clearTimeout(aAccount._motdTimer)
clearTimeout(aAccount._motdTimer);
aAccount._motdTimer = setTimeout(ircBase.commands["376"].bind(aAccount),
1000, aMessage);
@ -1048,7 +1048,7 @@ var ircBase = {
// No reason to keep the MOTD in memory.
delete this._motd;
// Clear the MOTD timer.
clearTimeout(this._motdTimer)
clearTimeout(this._motdTimer);
delete this._motdTimer;
return true;

Просмотреть файл

@ -58,7 +58,7 @@ var ircCTCP = {
"PRIVMSG": ctcpHandleMessage,
"NOTICE": ctcpHandleMessage
}
}
};
// Parse the message and call all CTCP handlers on the message.
function ctcpHandleMessage(aMessage) {
// If there are no CTCP handlers, then don't parse the CTCP message.
@ -161,7 +161,7 @@ var ctcpBase = {
// Received a CLIENTINFO response, store the information for future
// use.
let info = aMessage.ctcp.param.split(" ");
this.setWhois(aMessage.origin, {clientInfo: info})
this.setWhois(aMessage.origin, {clientInfo: info});
}
return true;
},

Просмотреть файл

@ -6,7 +6,7 @@
// implementing the commands field before we register them.
this.EXPORTED_SYMBOLS = ["commands"];
var {classes: Cc, interfaces: Ci, utils: Cu} = Components;
var {interfaces: Ci, utils: Cu} = Components;
Cu.import("resource:///modules/imXPCOMUtils.jsm");
Cu.import("resource:///modules/ircUtils.jsm");
@ -14,13 +14,13 @@ XPCOMUtils.defineLazyModuleGetter(this, "Task",
"resource://gre/modules/Task.jsm");
// Shortcut to get the JavaScript conversation object.
function getConv(aConv) { return aConv.wrappedJSObject; };
function getConv(aConv) { return aConv.wrappedJSObject; }
// Shortcut to get the JavaScript account object.
function getAccount(aConv) { return getConv(aConv)._account; };
function getAccount(aConv) { return getConv(aConv)._account; }
// Trim leading and trailing spaces and split a string by any type of space.
function splitInput(aString) { return aString.trim().split(/\s+/); };
function splitInput(aString) { return aString.trim().split(/\s+/); }
function OutgoingMessage(aMsg, aConversation, aAction) {
this.message = aMsg;
@ -372,7 +372,7 @@ var commands = [
if (newNick.indexOf(/\s+/) != -1)
return false;
let account = getAccount(aConv)
let account = getAccount(aConv);
// The user wants to change their nick, so overwrite the account
// nickname for this session.
account._requestedNickname = newNick;

Просмотреть файл

@ -35,9 +35,9 @@ function DCCMessage(aMessage, aAccount) {
message.ctcp.dcc = {
type: params[0],
argument: params[1],
address: new Number(params[2]),
port: new Number(params[3]),
size: params.length == 5 ? new Number(params[4]) : null,
address: Number(params[2]),
port: Number(params[3]),
size: params.length == 5 ? Number(params[4]) : null,
furtherArguments: params.length > 5 ? params.slice(5) : []
};
} catch (e) {

Просмотреть файл

@ -28,7 +28,7 @@ Cu.import("resource:///modules/ircUtils.jsm");
* defined by the RFC.
* value The new value for the parameter.
*/
function isupportMessage(aMessage, aToken) {
function isupportMessage(aMessage) {
// Seperate the ISUPPORT parameters.
let tokens = aMessage.params.slice(1, -1);
@ -71,11 +71,11 @@ var ircISUPPORT = {
return true;
}
}
}
};
function setSimpleNumber(aAccount, aField, aMessage, aDefaultValue) {
let value =
aMessage.isupport.value ? new Number(aMessage.isupport.value) : null;
aMessage.isupport.value ? Number(aMessage.isupport.value) : null;
aAccount[aField] = (value && !isNaN(value)) ? value : aDefaultValue;
return true;
}
@ -212,7 +212,7 @@ var isupportBase = {
let commands = aMessage.isupport.value.split(",");
for (let i = 0; i < commands.length; i++) {
let [command, limitStr] = commands[i].split("=");
let limit = limitStr ? new Number(limit) : Infinity;
let limit = limitStr ? Number(limit) : Infinity;
if (isNaN(limit)) {
this.WARN("Invalid maximum number of targets: " + limitStr);
continue;

Просмотреть файл

@ -39,7 +39,7 @@ function ServiceMessage(aAccount, aMessage) {
"chanserv": "ChanServ",
"infoserv": "InfoServ",
"nickserv": "NickServ"
}
};
let nickname = aAccount.normalize(aMessage.origin);
if (nicknameToServiceName.hasOwnProperty(nickname))

Просмотреть файл

@ -186,7 +186,7 @@ function mIRCColoring(aStack, aInput) {
// first open font tag.
if (!matches[1]) {
// Find the first font tag.
let offset = stack.map(aTag => aTag.indexOf("font") == 0)
let offset = stack.map(aTag => aTag.indexOf("font") === 0)
.indexOf(true);
// Close all tags from the first font tag on.

Просмотреть файл

@ -43,7 +43,7 @@ function test_mIRCColoring() {
];
for (let i = 0; i < input.length; i++)
do_check_eq(expectedOutput[i], ctcpFormatToHTML(input[i]));
equal(expectedOutput[i], ctcpFormatToHTML(input[i]));
run_next_test();
}
@ -62,7 +62,7 @@ function test_ctcpFormatToText() {
];
for (let i = 0; i < input.length; i++)
do_check_eq(expectedOutput[i], ctcpFormatToText(input[i]));
equal(expectedOutput[i], ctcpFormatToText(input[i]));
run_next_test();
}

Просмотреть файл

@ -24,8 +24,6 @@ var input = [
"ACTION \x5C\x5Catest"
];
var expectedOutputCommand = "ACTION";
var expectedOutputParam = [
"",
"test",
@ -48,10 +46,10 @@ var expectedOutputParam = [
function run_test() {
let output = input.map(aStr => ircCTCP.CTCPMessage({}, aStr));
// Ensure both arrays have the same length.
do_check_eq(expectedOutputParam.length, output.length);
equal(expectedOutputParam.length, output.length);
// Ensure the values in the arrays are equal.
for (let i = 0; i < output.length; ++i) {
do_check_eq(expectedOutputParam[i], output[i].ctcp.param);
do_check_eq(expectedOutputCommand, output[i].ctcp.command);
equal(expectedOutputParam[i], output[i].ctcp.param);
equal("ACTION", output[i].ctcp.command);
}
}

Просмотреть файл

@ -39,9 +39,8 @@ function test_ctcpFormatToHTML() {
"The quick <u>brown <i>fox <b>jumps</b></i></u> over <i>the lazy <b>dog.</b></i>"
];
let output = input.map(ctcpFormatToHTML);
for (let i = 0; i < output.length; i++)
do_check_eq(expectedOutput[i], output[i]);
for (let i = 0; i < input.length; i++)
equal(expectedOutput[i], ctcpFormatToHTML(input[i]));
run_next_test();
}
@ -49,9 +48,8 @@ function test_ctcpFormatToHTML() {
function test_ctcpFormatToText() {
let expectedOutput = "The quick brown fox jumps over the lazy dog.";
let output = input.map(ctcpFormatToText);
for (let i = 0; i < output.length; ++i)
do_check_eq(expectedOutput, output[i]);
for (let i = 0; i < input.length; ++i)
equal(expectedOutput, ctcpFormatToText(input[i]));
run_next_test();
}

Просмотреть файл

@ -24,8 +24,6 @@ var input = [
"\\\\atest"
];
var expectedOutputCommand = "PRIVMSG";
var expectedOutputParams = [
"ACTION",
"ACTION test",
@ -48,17 +46,17 @@ var expectedOutputParams = [
var outputParams = [];
irc.ircAccount.prototype.sendMessage = function(aCommand, aParams) {
do_check_eq(expectedOutputCommand, aCommand);
equal("PRIVMSG", aCommand);
outputParams.push(aParams[1]);
}
};
function run_test() {
let output = input.map(aStr =>
input.map(aStr =>
irc.ircAccount.prototype.sendCTCPMessage("", false, "ACTION", aStr));
// Ensure both arrays have the same length.
do_check_eq(expectedOutputParams.length, outputParams.length);
equal(expectedOutputParams.length, outputParams.length);
// Ensure the values in the arrays are equal.
for (let i = 0; i < outputParams.length; ++i)
do_check_eq("\x01" + expectedOutputParams[i] + "\x01", outputParams[i]);
equal("\x01" + expectedOutputParams[i] + "\x01", outputParams[i]);
}

Просмотреть файл

@ -144,38 +144,12 @@ function testRFC2812Messages() {
expectedStringMessage.slice(expectedStringMessage.indexOf(" ") + 1);
}
do_check_eq(stringMessage, expectedStringMessage);
equal(stringMessage, expectedStringMessage);
}
run_next_test();
}
/*
* Test if two objects have the same fields (recursively). aObject2 should be
* an ircMessage and aObject1 its expected values.
*/
function isEqual(aObject1, aObject2) {
let result = true;
for (let fieldName in aObject1) {
let field1 = aObject1[fieldName];
// Get the value current field value of the ircMessage. aObject2 might have
// be a Map, if so use the proper getter.
let field2;
if (aObject2 instanceof Map)
field2 = aObject2.get(fieldName);
else
field2 = aObject2[fieldName];
if (typeof field1 == "object")
result &= isEqual(field1, field2);
else if (Array.isArray(field1))
result &= field1.every((el, idx) => el == field2[idx]);
else
result &= field1 == field2;
}
return result;
}
// Unreal sends a couple of broken messages, see ircMessage in irc.js for a
// description of what's wrong.
function testBrokenUnrealMessages() {
@ -185,26 +159,38 @@ function testBrokenUnrealMessages() {
rawMessage: ":gravel.mozilla.org 432 #momo :Erroneous Nickname: Illegal characters",
command: "432",
params: ["", "#momo", "Erroneous Nickname: Illegal characters"],
origin: "gravel.mozilla.org"
origin: "gravel.mozilla.org",
user: undefined,
host: undefined,
source: "",
tags: new Map(),
},
// An extraneous space at the end.
":gravel.mozilla.org MODE #tckk +n ": {
rawMessage: ":gravel.mozilla.org MODE #tckk +n ",
command: "MODE",
params: ["#tckk", "+n"],
origin: "gravel.mozilla.org"
origin: "gravel.mozilla.org",
user: undefined,
host: undefined,
source: "",
tags: new Map(),
},
// Two extraneous spaces at the end.
":services.esper.net MODE #foo-bar +o foobar ": {
rawMessage: ":services.esper.net MODE #foo-bar +o foobar ",
command: "MODE",
params: ["#foo-bar", "+o", "foobar"],
origin: "services.esper.net"
origin: "services.esper.net",
user: undefined,
host: undefined,
source: "",
tags: new Map(),
}
};
for (let messageStr in messages)
do_check_true(isEqual(messages[messageStr], irc.ircMessage(messageStr, "")));
deepEqual(messages[messageStr], irc.ircMessage(messageStr, ""));
run_next_test();
}
@ -220,7 +206,8 @@ function testNewLinesInMessages() {
origin: "test",
user: "Instantbir",
host: "host",
source: "Instantbir@host"
tags: new Map(),
source: "Instantbir@host",
},
":test!Instantbir@host PRIVMSG #instantbird :First line\r\nSecond line": {
rawMessage: ":test!Instantbir@host PRIVMSG #instantbird :First line\r\nSecond line",
@ -229,12 +216,13 @@ function testNewLinesInMessages() {
origin: "test",
user: "Instantbir",
host: "host",
source: "Instantbir@host"
tags: new Map(),
source: "Instantbir@host",
}
};
for (let messageStr in messages)
do_check_true(isEqual(messages[messageStr], irc.ircMessage(messageStr)));
deepEqual(messages[messageStr], irc.ircMessage(messageStr));
run_next_test();
}
@ -251,12 +239,13 @@ function testLocalhost() {
origin: "localhost",
user: undefined,
host: undefined,
source: ""
tags: new Map(),
source: "",
}
};
for (let messageStr in messages)
do_check_true(isEqual(messages[messageStr], irc.ircMessage(messageStr)));
deepEqual(messages[messageStr], irc.ircMessage(messageStr));
run_next_test();
}
@ -270,42 +259,54 @@ function testTags() {
origin: "nick",
user: "ident",
host: "host.com",
tags: {
"aaa": "bBb",
"ccc": undefined,
"example.com/ddd": "eee"
}
tags: new Map([
["aaa", "bBb"],
["ccc", undefined],
["example.com/ddd", "eee"]
]),
source: "ident@host.com",
},
"@xn--e1afmkfd.org/foo :nick@host.com PRIVMSG him :Test": {
rawMessage: "@xn--e1afmkfd.org/foo :nick@host.com PRIVMSG him :Test",
command: "PRIVMSG",
params: ["him", "Test"],
origin: "nick",
// Note that this is a bug, it should be undefined for user and host.com
// for host/source.
user: "host.com",
tags: {
"xn--e1afmkfd.org/foo": undefined
}
host: undefined,
tags: new Map([
["xn--e1afmkfd.org/foo", undefined]
]),
source: "host.com@undefined",
},
"@aaa=\\\\n\\:\\n\\r\\s :nick@host.com PRIVMSG it :Yes": {
rawMessage: "@aaa=\\\\n\\:\\n\\r\\s :nick@host.com PRIVMSG it :Yes",
command: "PRIVMSG",
params: ["it", "Yes"],
origin: "nick",
// Note that this is a bug, it should be undefined for user and host.com
// for host/source.
user: "host.com",
tags: {
"aaa": "\\n;\n\r "
}
host: undefined,
tags: new Map([
["aaa", "\\n;\n\r "]
]),
source: "host.com@undefined",
},
"@c;h=;a=b :quux ab cd": {
rawMessage: "@c;h=;a=b :quux ab cd",
command: "ab",
params: ["cd"],
origin: "quux",
tags: {
"c": undefined,
"h": "",
"a": "b"
}
user: undefined,
host: undefined,
tags: new Map([
["c", undefined],
["h", ""],
["a", "b"]
]),
source: "",
},
"@time=2012-06-30T23:59:60.419Z :John!~john@1.2.3.4 JOIN #chan": {
rawMessage: "@time=2012-06-30T23:59:60.419Z :John!~john@1.2.3.4 JOIN #chan",
@ -314,14 +315,15 @@ function testTags() {
origin: "John",
user: "~john",
host: "1.2.3.4",
tags: {
"time": "2012-06-30T23:59:60.419Z"
}
tags: new Map([
["time", "2012-06-30T23:59:60.419Z"]
]),
source: "~john@1.2.3.4",
}
};
for (let messageStr in messages)
do_check_true(isEqual(messages[messageStr], irc.ircMessage(messageStr, "")));
deepEqual(messages[messageStr], irc.ircMessage(messageStr, ""));
run_next_test();
}

Просмотреть файл

@ -7,7 +7,7 @@ Services.scriptloader.loadSubScript("resource:///components/irc.js", irc);
Components.utils.import("resource:///modules/ircNonStandard.jsm");
// The function that is under test here.
var NOTICE = ircNonStandard.commands["NOTICE"]
var NOTICE = ircNonStandard.commands["NOTICE"];
function FakeConversation() {}
FakeConversation.prototype = {
@ -61,12 +61,12 @@ function testSecureList() {
let result = NOTICE.call(account, message);
// Yes, it was handled.
do_check_true(result);
ok(result);
// Undo the expected calculation, this should be near 0.
let value = account._lastListTime - Date.now() - 60000 + 12 * 60 * 60 * 1000;
// Give some wiggle room.
do_check_true(Math.abs(value) < 5 * 1000);
less(Math.abs(value), 5 * 1000);
run_next_test();
}
@ -91,24 +91,24 @@ function testZncAuth() {
do_check_true(result);
// No sent data and parameters should be unchanged.
do_check_true(account.buffer.length == 0);
do_check_true(account.shouldAuthenticate === undefined);
equal(account.buffer.length, 0);
equal(account.shouldAuthenticate, undefined);
// With a password.
account = new FakeAccount("password");
result = NOTICE.call(account, message);
// Yes, it was handled.
do_check_true(result);
ok(result);
// Check if the proper message was sent.
let sent = account.buffer[0];
do_check_true(sent[0] == "PASS");
do_check_true(sent[1] == "password");
do_check_true(account.buffer.length == 1);
equal(sent[0], "PASS");
equal(sent[1], "password");
equal(account.buffer.length, 1);
// Don't try to authenticate with NickServ.
do_check_true(account.shouldAuthenticate === false);
equal(account.shouldAuthenticate, false);
// Finally, check if the message is wrong.
account = new FakeAccount("password");
@ -116,7 +116,7 @@ function testZncAuth() {
result = NOTICE.call(account, message);
// This would be handled as a normal NOTICE.
do_check_false(result);
equal(result, false);
}
run_next_test();
@ -146,16 +146,16 @@ function testUMich() {
// These initial notices are not handled (i.e. they'll be subject to
// _showServerTab).
do_check_false(result);
equal(result, false);
}
// And finally the last one should be printed out, always. It contains the
// directions of what to do next.
let message = irc.ircMessage(kFinalMsg, "");
let result = NOTICE.call(account, message);
do_check_true(result);
do_check_eq(account.convs.length, 1);
do_check_eq(account.convs[0], "irc.umich.edu");
ok(result);
equal(account.convs.length, 1);
equal(account.convs[0], "irc.umich.edu");
run_next_test();
}
@ -175,7 +175,7 @@ function testAuthNick() {
// Since it is ambiguous if it was an authentication message or a message
// directed at the user, print it out.
do_check_true(result);
ok(result);
run_next_test();
}
@ -200,7 +200,7 @@ function testIgnoredNotices() {
let result = NOTICE.call(account, message);
// This message should *NOT* be shown.
do_check_false(result);
equal(result, false);
}
run_next_test();

Просмотреть файл

@ -17,7 +17,7 @@ FakeAccount.prototype = {
sendMessage: function(aCommand, aParams) {
(this.callbacks.shift())(aCommand, aParams);
}
}
};
var account = new FakeAccount();

Просмотреть файл

@ -14,7 +14,7 @@ FakeAccount.prototype = {
__proto__: irc.ircAccount.prototype,
setWhois: (n, f) => true,
ERROR: do_throw
}
};
function run_test() {
add_test(test_topicSettable);

Просмотреть файл

@ -31,10 +31,7 @@ function run_test() {
// The expected messages as defined above.
let expectedMsgs = messages[message];
// Ensure both arrays have the same length.
do_check_eq(expectedMsgs.length, generatedMsgs.length);
// Ensure the values in the arrays are equal.
for (let i = 0; i < expectedMsgs.length; ++i)
do_check_eq(generatedMsgs[i], expectedMsgs[i]);
// Ensure the arrays are equal.
deepEqual(generatedMsgs, expectedMsgs);
}
}

Просмотреть файл

@ -9,7 +9,7 @@ var fakeProto = {
id: "fake-proto",
options: {alternateNicks: ""},
_getOptionDefault: function(aOption) { return this.options[aOption]; }
}
};
function test_tryNewNick() {
const testData = {
@ -37,7 +37,7 @@ function test_tryNewNick() {
for (let currentNick in testData) {
account._sentNickname = currentNick;
account.sendMessage = (aCommand, aNewNick) =>
do_check_eq(aNewNick, testData[currentNick]);
equal(aNewNick, testData[currentNick]);
account.tryNewNick(currentNick);
}
@ -79,7 +79,7 @@ function test_maxLength() {
for (let currentNick of testData) {
account.sendMessage = (aCommand, aNewNick) =>
do_check_eq(aNewNick, currentNick[1]);
equal(aNewNick, currentNick[1]);
account.tryNewNick(currentNick[0]);
}
@ -120,7 +120,7 @@ function test_altNicks() {
account._sentNickname = currentNick;
account.sendMessage = (aCommand, aNewNick) =>
do_check_eq(aNewNick, testData[currentNick][1]);
equal(aNewNick, testData[currentNick][1]);
account.tryNewNick(currentNick);
}