summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/addrbook/test/LDAPServer.jsm
blob: c8d8edb82bb7b4839c6bf117eed1bfb64843d1cd (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
/* 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/. */

const EXPORTED_SYMBOLS = ["LDAPServer"];
const PRINT_DEBUG = false;

const { Assert } = ChromeUtils.importESModule(
  "resource://testing-common/Assert.sys.mjs"
);

/**
 * This is a partial implementation of an LDAP server as defined by RFC 4511.
 * It's not intended to serve any particular dataset, rather, tests should
 * cause the application to make requests and tell the server what to respond.
 *
 * https://docs.ldap.com/specs/rfc4511.txt
 *
 * @implements {nsIInputStreamCallback}
 * @implements {nsIServerSocketListener}
 */
var LDAPServer = {
  BindRequest: 0x60,
  UnbindRequest: 0x42,
  SearchRequest: 0x63,
  AbandonRequest: 0x50,

  serverSocket: null,

  QueryInterface: ChromeUtils.generateQI([
    "nsIInputStreamCallback",
    "nsIServerSocketListener",
  ]),

  /**
   * Start listening on an OS-selected port. The port number can be found at
   * LDAPServer.port.
   */
  open() {
    this.serverSocket = Cc[
      "@mozilla.org/network/server-socket;1"
    ].createInstance(Ci.nsIServerSocket);
    this.serverSocket.init(-1, true, 1);
    console.log(`socket open on port ${this.serverSocket.port}`);

    this.serverSocket.asyncListen(this);
  },
  /**
   * Stop listening for new connections and close any that are open.
   */
  close() {
    this.serverSocket.close();
  },
  /**
   * The port this server is listening on.
   */
  get port() {
    return this.serverSocket.port;
  },

  /**
   * Retrieves any data sent to the server since connection or the previous
   * call to read(). This should be called every time the application is
   * expected to send data.
   *
   * @returns {Promise} Resolves when data is received by the server, with the
   *                    data as a byte array.
   */
  async read(expectedOperation) {
    let data;
    if (this._data) {
      data = this._data;
      delete this._data;
    } else {
      data = await new Promise(resolve => {
        this._inputStreamReadyResolve = resolve;
      });
    }

    // Simplified parsing to get the message ID and operation code.

    let index = 4;
    // The value at [1] may be more than one byte. If it is, skip more bytes.
    if (data[1] & 0x80) {
      index += data[1] & 0x7f;
    }

    // Assumes the ID is not greater than 127.
    this._lastMessageID = data[index];

    if (expectedOperation) {
      let actualOperation = data[index + 1];

      // Unbind and abandon requests can happen at any point, when an
      // nsLDAPConnection is destroyed. This is unpredictable, and irrelevant
      // for testing. Ignore.
      if (
        actualOperation == LDAPServer.UnbindRequest ||
        actualOperation == LDAPServer.AbandonRequest
      ) {
        if (PRINT_DEBUG) {
          console.log("Ignoring unbind or abandon request");
        }
        return this.read(expectedOperation);
      }

      Assert.equal(
        actualOperation.toString(16),
        expectedOperation.toString(16),
        "LDAP Operation type"
      );
    }

    return data;
  },
  /**
   * Sends raw data to the application. Generally this shouldn't be used
   * directly but it may be useful for testing.
   *
   * @param {byte[]} data - The data to write.
   */
  write(data) {
    if (PRINT_DEBUG) {
      console.log(
        ">>> " + data.map(b => b.toString(16).padStart(2, 0)).join(" ")
      );
    }
    this._outputStream.writeByteArray(data);
  },
  /**
   * Sends a simple BindResponse to the application.
   * See section 4.2.2 of the RFC.
   */
  writeBindResponse() {
    let message = new Sequence(0x30, new IntegerValue(this._lastMessageID));
    let person = new Sequence(
      0x61,
      new EnumeratedValue(0),
      new StringValue(""),
      new StringValue("")
    );
    message.children.push(person);
    this.write(message.getBytes());
  },
  /**
   * Sends a SearchResultEntry to the application.
   * See section 4.5.2 of the RFC.
   *
   * @param {object} entry
   * @param {string} entry.dn - The LDAP DN of the person.
   * @param {string} entry.attributes - A key/value or key/array-of-values
   *   object representing the person.
   */
  writeSearchResultEntry({ dn, attributes }) {
    let message = new Sequence(0x30, new IntegerValue(this._lastMessageID));

    let person = new Sequence(0x64, new StringValue(dn));
    message.children.push(person);

    let attributeSequence = new Sequence(0x30);
    person.children.push(attributeSequence);

    for (let [key, value] of Object.entries(attributes)) {
      let seq = new Sequence(0x30, new StringValue(key), new Sequence(0x31));
      if (typeof value == "string") {
        value = [value];
      }
      for (let v of value) {
        seq.children[1].children.push(new StringValue(v));
      }
      attributeSequence.children.push(seq);
    }

    this.write(message.getBytes());
  },
  /**
   * Sends a SearchResultDone to the application.
   * See RFC 4511 section 4.5.2.
   */
  writeSearchResultDone() {
    let message = new Sequence(0x30, new IntegerValue(this._lastMessageID));
    let person = new Sequence(
      0x65,
      new EnumeratedValue(0),
      new StringValue(""),
      new StringValue("")
    );
    message.children.push(person);
    this.write(message.getBytes());
  },

  /**
   * nsIServerSocketListener.onSocketAccepted
   */
  onSocketAccepted(socket, transport) {
    let inputStream = transport
      .openInputStream(0, 8192, 1024)
      .QueryInterface(Ci.nsIAsyncInputStream);

    let outputStream = transport.openOutputStream(0, 0, 0);
    this._outputStream = Cc["@mozilla.org/binaryoutputstream;1"].createInstance(
      Ci.nsIBinaryOutputStream
    );
    this._outputStream.setOutputStream(outputStream);

    if (this._socketConnectedResolve) {
      this._socketConnectedResolve();
      delete this._socketConnectedResolve;
    }
    inputStream.asyncWait(this, 0, 0, Services.tm.mainThread);
  },
  /**
   * nsIServerSocketListener.onStopListening
   */
  onStopListening(socket, status) {
    console.log(`socket closed with status ${status.toString(16)}`);
  },

  /**
   * nsIInputStreamCallback.onInputStreamReady
   */
  onInputStreamReady(stream) {
    let available;
    try {
      available = stream.available();
    } catch (ex) {
      if (
        [Cr.NS_BASE_STREAM_CLOSED, Cr.NS_ERROR_NET_RESET].includes(ex.result)
      ) {
        return;
      }
      throw ex;
    }

    let binaryInputStream = Cc[
      "@mozilla.org/binaryinputstream;1"
    ].createInstance(Ci.nsIBinaryInputStream);
    binaryInputStream.setInputStream(stream);
    let data = binaryInputStream.readByteArray(available);
    if (PRINT_DEBUG) {
      console.log(
        "<<< " + data.map(b => b.toString(16).padStart(2, 0)).join(" ")
      );
    }

    if (this._inputStreamReadyResolve) {
      this._inputStreamReadyResolve(data);
      delete this._inputStreamReadyResolve;
    } else {
      this._data = data;
    }

    stream.asyncWait(this, 0, 0, Services.tm.mainThread);
  },
};

/**
 * Helper classes to convert primitives to LDAP byte sequences.
 */

class Sequence {
  constructor(number, ...children) {
    this.number = number;
    this.children = children;
  }
  getBytes() {
    let bytes = [];
    for (let c of this.children) {
      bytes = bytes.concat(c.getBytes());
    }
    return [this.number].concat(getLengthBytes(bytes.length), bytes);
  }
}
class IntegerValue {
  constructor(int) {
    this.int = int;
    this.number = 0x02;
  }
  getBytes() {
    let temp = this.int;
    let bytes = [];

    while (temp >= 128) {
      bytes.unshift(temp & 255);
      temp >>= 8;
    }
    bytes.unshift(temp);
    return [this.number].concat(getLengthBytes(bytes.length), bytes);
  }
}
class StringValue {
  constructor(str) {
    this.str = str;
  }
  getBytes() {
    return [0x04].concat(
      getLengthBytes(this.str.length),
      Array.from(this.str, c => c.charCodeAt(0))
    );
  }
}
class EnumeratedValue extends IntegerValue {
  constructor(int) {
    super(int);
    this.number = 0x0a;
  }
}

function getLengthBytes(int) {
  if (int < 128) {
    return [int];
  }

  let temp = int;
  let bytes = [];

  while (temp >= 128) {
    bytes.unshift(temp & 255);
    temp >>= 8;
  }
  bytes.unshift(temp);
  bytes.unshift(0x80 | bytes.length);
  return bytes;
}