summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/addrbook/test/unit/test_LDAPMessage.js
blob: ebcb746f21773ab734d7d65b3df5f64363668883 (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
/* 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/. */

/**
 * Tests for LDAPMessage.jsm.
 */

var { CommonUtils } = ChromeUtils.importESModule(
  "resource://services-common/utils.sys.mjs"
);
var { LDAPResponse, SearchRequest } = ChromeUtils.import(
  "resource:///modules/LDAPMessage.jsm"
);

/**
 * Test filter string is converted to asn1 blocks correctly.
 */
add_task(function test_SearchRequest_filter() {
  let req = new SearchRequest(
    "ou=people,dc=planetexpress,dc=com",
    Ci.nsILDAPURL.SCOPE_SUBTREE,
    "(memberof=cn=ship_crew,ou=people,dc=planetexpress,dc=com)",
    "",
    0,
    0
  );
  let filterBlock = req.protocolOp.valueBlock.value[6];
  let [filterKeyBlock, filterValueBlock] = filterBlock.valueBlock.value;
  let filterKey = new TextDecoder().decode(filterKeyBlock.valueBlock.valueHex);
  let filterValue = new TextDecoder().decode(
    filterValueBlock.valueBlock.valueHex
  );
  Assert.equal(filterKey, "memberof", "Filter key should be correct");
  Assert.equal(
    filterValue,
    "cn=ship_crew,ou=people,dc=planetexpress,dc=com",
    "Filter value should be correct"
  );
});

/**
 * Test extensibleMatch filter is encoded correctly.
 */
add_task(function test_extensibleMatchFilter() {
  // Test data is from https://ldap.com/ldapv3-wire-protocol-reference-search/.
  // filter string, BER payload, description
  let filterBER = [
    [
      "(uid:dn:caseIgnoreMatch:=jdoe)",
      "a91f810f6361736549676e6f72654d61746368820375696483046a646f658401ff",
      "<type>:dn:<rule>:=<value>",
    ],
    ["(uid:=jdoe)", "a90b820375696483046a646f65", "<type>:=<value>"],
    [
      "(:caseIgnoreMatch:=foo)",
      "a916810f6361736549676e6f72654d617463688303666f6f",
      ":<rule>:=<value>",
    ],
    // This one is not directly from ldap.com, but assembled from the above cases.
    [
      "(uid:caseIgnoreMatch:=jdoe)",
      "a91c810f6361736549676e6f72654d61746368820375696483046a646f65",
      "<type>:<rule>:=<value>",
    ],
  ];
  for (let [filter, ber, description] of filterBER) {
    let req = new SearchRequest(
      "ou=people,dc=planetexpress,dc=com",
      Ci.nsILDAPURL.SCOPE_SUBTREE,
      filter,
      "",
      0,
      0
    );
    let filterBlock = req.protocolOp.valueBlock.value[6];
    Assert.equal(
      CommonUtils.bufferToHex(new Uint8Array(filterBlock.toBER())),
      ber,
      description
    );
  }
});

/**
 * Test parsing to SearchResultReference works.
 */
add_task(function test_SearchResultReference() {
  // A BER payload representing a SearchResultReference with two urls, test data
  // is from https://ldap.com/ldapv3-wire-protocol-reference-search/.
  let hex =
    "306d020102736804326c6461703a2f2f6473312e6578616d706c652e636f6d3a3338392f64633d6578616d706c652c64633d636f6d3f3f7375623f04326c6461703a2f2f6473322e6578616d706c652e636f6d3a3338392f64633d6578616d706c652c64633d636f6d3f3f7375623f";
  let res = LDAPResponse.fromBER(CommonUtils.hexToArrayBuffer(hex).buffer);

  // Should be correctly parsed.
  Assert.equal(res.constructor.name, "SearchResultReference");
  Assert.deepEqual(res.result, [
    "ldap://ds1.example.com:389/dc=example,dc=com??sub?",
    "ldap://ds2.example.com:389/dc=example,dc=com??sub?",
  ]);
});