summaryrefslogtreecommitdiffstats
path: root/comm/chat/protocols/irc/test/test_ircMessage.js
blob: 4420856c84c0b248d30226e17b259c5faf1510ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

var { ircAccount, ircMessage } = ChromeUtils.importESModule(
  "resource:///modules/ircAccount.sys.mjs"
);

var testData = [
  // First off, let's test the messages from RFC 2812.
  "PASS secretpasswordhere",
  "NICK Wiz",
  ":WiZ!jto@tolsun.oulu.fi NICK Kilroy",
  "USER guest 0 * :Ronnie Reagan",
  "USER guest 8 * :Ronnie Reagan",
  "OPER foo bar",
  "MODE WiZ -w",
  "MODE Angel +i",
  "MODE WiZ -o",
  "SERVICE dict * *.fr 0 0 :French Dictionary",
  "QUIT :Gone to have lunch",
  ":syrk!kalt@millennium.stealth.net QUIT :Gone to have lunch",
  "SQUIT tolsun.oulu.fi :Bad Link ?",
  ":Trillian SQUIT cm22.eng.umd.edu :Server out of control",
  "JOIN #foobar",
  "JOIN &foo fubar",
  "JOIN #foo,&bar fubar",
  "JOIN #foo,#bar fubar,foobar",
  "JOIN #foo,#bar",
  "JOIN 0",
  ":WiZ!jto@tolsun.oulu.fi JOIN #Twilight_zone",
  "PART #twilight_zone",
  "PART #oz-ops,&group5",
  ":WiZ!jto@tolsun.oulu.fi PART #playzone :I lost",
  "MODE #Finnish +imI *!*@*.fi",
  "MODE #Finnish +o Kilroy",
  "MODE #Finnish +v Wiz",
  "MODE #Fins -s",
  "MODE #42 +k oulu",
  "MODE #42 -k oulu",
  "MODE #eu-opers +l 10",
  ":WiZ!jto@tolsun.oulu.fi MODE #eu-opers -l",
  "MODE &oulu +b",
  "MODE &oulu +b *!*@*",
  "MODE &oulu +b *!*@*.edu +e *!*@*.bu.edu",
  "MODE #bu +be *!*@*.edu *!*@*.bu.edu",
  "MODE #meditation e",
  "MODE #meditation I",
  "MODE !12345ircd O",
  ":WiZ!jto@tolsun.oulu.fi TOPIC #test :New topic",
  "TOPIC #test :another topic",
  "TOPIC #test :",
  "TOPIC #test",
  "NAMES #twilight_zone,#42",
  "NAMES",
  "LIST",
  "LIST #twilight_zone,#42",
  ":Angel!wings@irc.org INVITE Wiz #Dust",
  "INVITE Wiz #Twilight_Zone",
  "KICK &Melbourne Matthew",
  "KICK #Finnish John :Speaking English",
  ":WiZ!jto@tolsun.oulu.fi KICK #Finnish John",
  ":Angel!wings@irc.org PRIVMSG Wiz :Are you receiving this message ?",
  "PRIVMSG Angel :yes I'm receiving it !",
  "PRIVMSG jto@tolsun.oulu.fi :Hello !",
  "PRIVMSG kalt%millennium.stealth.net@irc.stealth.net :Are you a frog?",
  "PRIVMSG kalt%millennium.stealth.net :Do you like cheese?",
  "PRIVMSG Wiz!jto@tolsun.oulu.fi :Hello !",
  "PRIVMSG $*.fi :Server tolsun.oulu.fi rebooting.",
  "PRIVMSG #*.edu :NSFNet is undergoing work, expect interruptions",
  "VERSION tolsun.oulu.fi",
  "STATS m",
  "LINKS *.au",
  "LINKS *.edu *.bu.edu",
  "TIME tolsun.oulu.fi",
  "CONNECT tolsun.oulu.fi 6667",
  "TRACE *.oulu.fi",
  "ADMIN tolsun.oulu.fi",
  "ADMIN syrk",
  "INFO csd.bu.edu",
  "INFO Angel",
  "SQUERY irchelp :HELP privmsg",
  "SQUERY dict@irc.fr :fr2en blaireau",
  "WHO *.fi",
  "WHO jto* o",
  "WHOIS wiz",
  "WHOIS eff.org trillian",
  "WHOWAS Wiz",
  "WHOWAS Mermaid 9",
  "WHOWAS Trillian 1 *.edu",
  "PING tolsun.oulu.fi",
  "PING WiZ tolsun.oulu.fi",
  // Below fails, we don't use the (unnecessary) colon.
  // "PING :irc.funet.fi",
  "PONG csd.bu.edu tolsun.oulu.fi",
  "ERROR :Server *.fi already exists",
  "NOTICE WiZ :ERROR from csd.bu.edu -- Server *.fi already exists",
  "AWAY :Gone to lunch.  Back in 5",
  "REHASH",
  "DIE",
  "RESTART",
  "SUMMON jto",
  "SUMMON jto tolsun.oulu.fi",
  "USERS eff.org",
  ":csd.bu.edu WALLOPS :Connect '*.uiuc.edu 6667' from Joshua",
  "USERHOST Wiz Michael syrk",
  // Below fails, we don't use the (unnecessary) colon.
  // ":ircd.stealth.net 302 yournick :syrk=+syrk@millennium.stealth.net",
  "ISON phone trillian WiZ jarlek Avalon Angel Monstah syrk",

  // Now for the torture test, specially crafted messages that might be
  // "difficult" to handle.
  "PRIVMSG foo ::)", // Test sending a colon as the first character.
  "PRIVMSG foo :This is a test.", // Test sending a space.
  "PRIVMSG foo :", // Empty last parameter.
  "PRIVMSG foo :This is :a test.", // A "second" last parameter.
];

function run_test() {
  add_test(testRFC2812Messages);
  add_test(testBrokenUnrealMessages);
  add_test(testNewLinesInMessages);
  add_test(testLocalhost);
  add_test(testTags);

  run_next_test();
}

/*
 * Test round tripping parsing and then rebuilding the messages from RFC 2812.
 */
function testRFC2812Messages() {
  for (let expectedStringMessage of testData) {
    // Pass in an empty default origin in order to check this below.
    let message = ircMessage(expectedStringMessage, "");

    let stringMessage = ircAccount.prototype.buildMessage(
      message.command,
      message.params
    );

    // Let's do a little dance here...we don't rebuild the "source" of the
    // message (the server does that), so when comparing our output message, we
    // need to avoid comparing to that part.
    if (message.origin) {
      expectedStringMessage = expectedStringMessage.slice(
        expectedStringMessage.indexOf(" ") + 1
      );
    }

    equal(stringMessage, expectedStringMessage);
  }

  run_next_test();
}

// Unreal sends a couple of broken messages, see ircMessage in irc.jsm for a
// description of what's wrong.
function testBrokenUnrealMessages() {
  let messages = {
    // Two spaces after command.
    ":gravel.mozilla.org 432  #momo :Erroneous Nickname: Illegal characters": {
      rawMessage:
        ":gravel.mozilla.org 432  #momo :Erroneous Nickname: Illegal characters",
      command: "432",
      params: ["", "#momo", "Erroneous Nickname: Illegal characters"],
      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",
      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",
      user: undefined,
      host: undefined,
      source: "",
      tags: new Map(),
    },
  };

  for (let messageStr in messages) {
    deepEqual(messages[messageStr], ircMessage(messageStr, ""));
  }

  run_next_test();
}

// After unescaping we can end up with line breaks inside of IRC messages. Test
// this edge case specifically.
function testNewLinesInMessages() {
  let messages = {
    ":test!Instantbir@host PRIVMSG #instantbird :First line\nSecond line": {
      rawMessage:
        ":test!Instantbir@host PRIVMSG #instantbird :First line\nSecond line",
      command: "PRIVMSG",
      params: ["#instantbird", "First line\nSecond line"],
      origin: "test",
      user: "Instantbir",
      host: "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",
      command: "PRIVMSG",
      params: ["#instantbird", "First line\r\nSecond line"],
      origin: "test",
      user: "Instantbir",
      host: "host",
      tags: new Map(),
      source: "Instantbir@host",
    },
  };

  for (let messageStr in messages) {
    deepEqual(messages[messageStr], ircMessage(messageStr));
  }

  run_next_test();
}

// Sometimes it is a bit hard to tell whether a prefix is a nickname or a
// servername. Generally this happens when connecting to localhost or a local
// hostname and is likely seen with bouncers.
function testLocalhost() {
  let messages = {
    ":localhost 001 clokep :Welcome to the BitlBee gateway, clokep": {
      rawMessage:
        ":localhost 001 clokep :Welcome to the BitlBee gateway, clokep",
      command: "001",
      params: ["clokep", "Welcome to the BitlBee gateway, clokep"],
      origin: "localhost",
      user: undefined,
      host: undefined,
      tags: new Map(),
      source: "",
    },
  };

  for (let messageStr in messages) {
    deepEqual(messages[messageStr], ircMessage(messageStr));
  }

  run_next_test();
}

function testTags() {
  let messages = {
    "@aaa=bBb;ccc;example.com/ddd=eee :nick!ident@host.com PRIVMSG me :Hello": {
      rawMessage:
        "@aaa=bBb;ccc;example.com/ddd=eee :nick!ident@host.com PRIVMSG me :Hello",
      command: "PRIVMSG",
      params: ["me", "Hello"],
      origin: "nick",
      user: "ident",
      host: "host.com",
      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",
      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",
      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",
      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",
      command: "JOIN",
      params: ["#chan"],
      origin: "John",
      user: "~john",
      host: "1.2.3.4",
      tags: new Map([["time", "2012-06-30T23:59:60.419Z"]]),
      source: "~john@1.2.3.4",
    },
  };

  for (let messageStr in messages) {
    deepEqual(messages[messageStr], ircMessage(messageStr, ""));
  }

  run_next_test();
}