summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/addrbook/test/unit/test_ldap1.js
blob: e323d713863b0e7b3dba5ff875f89a5bdad53678 (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
 * Test suite for basic LDAP address book functions
 */

var kLDAPUriPrefix = "moz-abldapdirectory://";
var kLDAPTestSpec = "ldap://invalidhost//dc=intranet??sub?(objectclass=*)";

function run_test() {
  // If nsIAbLDAPDirectory doesn't exist in our build options, someone has
  // specified --disable-ldap
  if (!("nsIAbLDAPDirectory" in Ci)) {
    return;
  }

  let abCountBeforeStart = MailServices.ab.directories.length;

  // Test - Create an LDAP directory
  let abUri = MailServices.ab.newAddressBook(
    "test",
    kLDAPTestSpec,
    Ci.nsIAbManager.LDAP_DIRECTORY_TYPE
  );

  let abCountAfterCreate = MailServices.ab.directories.length;
  Assert.equal(abCountAfterCreate, abCountBeforeStart + 1);

  // Test - Check we have the directory.
  let abDir = MailServices.ab
    .getDirectory(kLDAPUriPrefix + abUri)
    .QueryInterface(Ci.nsIAbLDAPDirectory);

  // Test - Check various fields
  Assert.equal(abDir.dirName, "test");
  Assert.equal(abDir.lDAPURL.spec, kLDAPTestSpec);
  Assert.ok(abDir.readOnly);

  // Test - Write a UTF-8 Auth DN and check it
  abDir.authDn = "test\u00D0";

  Assert.equal(abDir.authDn, "test\u00D0");

  // Test - searchDuringLocalAutocomplete

  // Set up an account and identity in the account manager
  let identity = MailServices.accounts.createIdentity();

  const localAcTests = [
    // Online checks
    {
      useDir: false,
      dirSer: "",
      idOver: false,
      idSer: "",
      idKey: "",
      offline: false,
      result: false,
    },
    {
      useDir: true,
      dirSer: abDir.dirPrefId,
      idOver: false,
      idSer: "",
      idKey: "",
      offline: false,
      result: false,
    },
    // Offline checks with and without global prefs set, no identity key
    {
      useDir: false,
      dirSer: "",
      idOver: false,
      idSer: "",
      idKey: "",
      offline: true,
      result: false,
    },
    {
      useDir: true,
      dirSer: "",
      idOver: false,
      idSer: "",
      idKey: "",
      offline: true,
      result: false,
    },
    {
      useDir: true,
      dirSer: abDir.dirPrefId,
      idOver: false,
      idSer: "",
      idKey: "",
      offline: true,
      result: true,
    },
    // Offline checks with and without global prefs set, with identity key
    {
      useDir: false,
      dirSer: "",
      idOver: false,
      idSer: "",
      idKey: identity.key,
      offline: true,
      result: false,
    },
    {
      useDir: true,
      dirSer: "",
      idOver: false,
      idSer: "",
      idKey: identity.key,
      offline: true,
      result: false,
    },
    {
      useDir: true,
      dirSer: abDir.dirPrefId,
      idOver: false,
      idSer: "",
      idKey: identity.key,
      offline: true,
      result: true,
    },
    // Offline checks, no global prefs, identity ones only
    {
      useDir: false,
      dirSer: "",
      idOver: true,
      idSer: "",
      idKey: identity.key,
      offline: true,
      result: false,
    },
    {
      useDir: false,
      dirSer: "",
      idOver: true,
      idSer: kPABData.dirPrefID,
      idKey: identity.key,
      offline: true,
      result: false,
    },
    {
      useDir: false,
      dirSer: "",
      idOver: true,
      idSer: abDir.dirPrefId,
      idKey: identity.key,
      offline: true,
      result: true,
    },
    {
      useDir: false,
      dirSer: "",
      idOver: false,
      idSer: abDir.dirPrefId,
      idKey: identity.key,
      offline: true,
      result: false,
    },
    // Offline checks, global prefs and identity ones
    {
      useDir: true,
      dirSer: kPABData.dirPrefID,
      idOver: true,
      idSer: abDir.dirPrefId,
      idKey: identity.key,
      offline: true,
      result: true,
    },
    {
      useDir: true,
      dirSer: abDir.dirPrefId,
      idOver: true,
      idSer: kPABData.dirPrefID,
      idKey: identity.key,
      offline: true,
      result: false,
    },
  ];

  function checkAc(element, index, array) {
    dump("Testing index " + index + "\n");
    Services.prefs.setBoolPref(
      "ldap_2.autoComplete.useDirectory",
      element.useDir
    );
    Services.prefs.setCharPref(
      "ldap_2.autoComplete.directoryServer",
      element.dirSer
    );
    identity.overrideGlobalPref = element.idOver;
    identity.directoryServer = element.idSer;
    Services.io.offline = element.offline;

    Assert.equal(abDir.useForAutocomplete(element.idKey), element.result);
  }

  localAcTests.forEach(checkAc);

  MailServices.ab.deleteAddressBook(abDir.URI);

  let abCountAfterDelete = MailServices.ab.directories.length;
  Assert.equal(abCountAfterDelete, abCountBeforeStart);
}