summaryrefslogtreecommitdiffstats
path: root/comm/chat/protocols/irc/test/test_ircCAP.js
blob: a79a926efcda4a3698b04bff79fa2c7085dd0260 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

var { capMessage } = ChromeUtils.importESModule(
  "resource:///modules/ircCAP.sys.mjs"
);

var testData = [
  // A normal LS from the server.
  [
    ["*", "LS", "multi-prefix sasl userhost-in-names"],
    [
      {
        subcommand: "LS",
        parameter: "multi-prefix",
      },
      {
        subcommand: "LS",
        parameter: "sasl",
      },
      {
        subcommand: "LS",
        parameter: "userhost-in-names",
      },
    ],
  ],

  // LS with both valid and invalid vendor specific capabilities.
  [
    [
      "*",
      "LS",
      "sasl server-time znc.in/server-time-iso znc.in/playback palaverapp.com",
    ],
    [
      {
        subcommand: "LS",
        parameter: "sasl",
      },
      {
        subcommand: "LS",
        parameter: "server-time",
      },
      // Valid vendor prefixes (of the form <domain name>/<capability>).
      {
        subcommand: "LS",
        parameter: "znc.in/server-time-iso",
      },
      {
        subcommand: "LS",
        parameter: "znc.in/playback",
      },
      // Invalid vendor prefix, but we should treat it as an opaque identifier.
      {
        subcommand: "LS",
        parameter: "palaverapp.com",
      },
    ],
  ],

  // Some implementations include one less parameter.
  [
    ["LS", "sasl"],
    [
      {
        subcommand: "LS",
        parameter: "sasl",
      },
    ],
  ],

  // Modifier tests, ensure the modified is stripped from the capaibility and is
  // parsed correctly.
  [
    ["LS", "-disable =sticky ~ack"],
    [
      {
        subcommand: "LS",
        parameter: "disable",
        modifier: "-",
        disable: true,
      },
      {
        subcommand: "LS",
        parameter: "sticky",
        modifier: "=",
        sticky: true,
      },
      {
        subcommand: "LS",
        parameter: "ack",
        modifier: "~",
        ack: true,
      },
    ],
  ],

  // IRC v3.2 multi-line LS response
  [
    ["*", "LS", "*", "sasl"],
    ["*", "LS", "server-time"],
    [
      {
        subcommand: "LS",
        parameter: "sasl",
      },
      {
        subcommand: "LS",
        parameter: "server-time",
      },
    ],
  ],

  // IRC v3.2 multi-line LIST response
  [
    ["*", "LIST", "*", "sasl"],
    ["*", "LIST", "server-time"],
    [
      {
        subcommand: "LIST",
        parameter: "sasl",
      },
      {
        subcommand: "LIST",
        parameter: "server-time",
      },
    ],
  ],

  // IRC v3.2 cap value
  [
    ["*", "LS", "multi-prefix sasl=EXTERNAL sts=port=6697"],
    [
      {
        subcommand: "LS",
        parameter: "multi-prefix",
      },
      {
        subcommand: "LS",
        parameter: "sasl",
        value: "EXTERNAL",
      },
      {
        subcommand: "LS",
        parameter: "sts",
        value: "port=6697",
      },
    ],
  ],

  // cap-notify new cap
  [
    ["*", "NEW", "batch"],
    [
      {
        subcommand: "NEW",
        parameter: "batch",
      },
    ],
  ],

  // cap-notify delete cap
  [
    ["*", "DEL", "multi-prefix"],
    [
      {
        subcommand: "DEL",
        parameter: "multi-prefix",
      },
    ],
  ],
];

function run_test() {
  add_test(testCapMessages);

  run_next_test();
}

/*
 * Test round tripping parsing and then rebuilding the messages from RFC 2812.
 */
function testCapMessages() {
  for (let data of testData) {
    // Generate an ircMessage to send into capMessage.
    let i = 0;
    let message;
    let outputs;
    const account = {
      _queuedCAPs: [],
    };

    // Generate an ircMessage to send into capMessage.
    while (typeof data[i][0] == "string") {
      message = {
        params: data[i],
      };

      // Create the CAP message.
      outputs = capMessage(message, account);
      ++i;
    }

    // The original message should get a cap object added with the subcommand
    // set.
    ok(message.cap);
    equal(message.cap.subcommand, data[i][0].subcommand);

    // We only care about the "cap" part of each return message.
    outputs = outputs.map(o => o.cap);

    // Ensure the expected output is an array.
    let expectedCaps = data[i];
    if (!Array.isArray(expectedCaps)) {
      expectedCaps = [expectedCaps];
    }

    // Add defaults to the expected output.
    for (let expectedCap of expectedCaps) {
      // By default there's no modifier.
      if (!("modifier" in expectedCap)) {
        expectedCap.modifier = undefined;
      }
      for (let param of ["disable", "sticky", "ack"]) {
        if (!(param in expectedCap)) {
          expectedCap[param] = false;
        }
      }
    }

    // Ensure each item in the arrays are equal.
    deepEqual(outputs, expectedCaps);
  }

  run_next_test();
}