2 // DirectoryServicesDirectorySearcherTest.cs -
\r
3 // NUnit Test Cases for DirectoryServices.DirectorySearcher
\r
6 // Boris Kirzner <borisk@mainsoft.com>
\r
8 using NUnit.Framework;
\r
10 using System.DirectoryServices;
\r
11 using System.Collections;
\r
13 using System.Threading;
\r
15 namespace MonoTests.System.DirectoryServices
\r
18 [Category ("InetAccess")]
\r
19 public class DirectoryServicesDirectorySearcherTest
\r
23 static TestConfiguration configuration;
\r
24 static DirectoryEntry de;
\r
25 static DirectorySearcher ds;
\r
27 #endregion // Fields
\r
29 #region SetUp and TearDown
\r
32 public void TestFixtureSetUp()
\r
35 configuration = new TestConfiguration ();
\r
39 [TestFixtureTearDown]
\r
40 public void TestFixtureTearDown()
\r
59 #region Initialize basics
\r
61 DirectoryEntry root = new DirectoryEntry( configuration.ConnectionString,
\r
62 configuration.Username,
\r
63 configuration.Password,
\r
64 configuration.AuthenticationType);
\r
66 DirectoryEntry ouPeople = root.Children.Add("ou=people","Class");
\r
67 ouPeople.Properties["objectClass"].Value = "organizationalUnit";
\r
68 ouPeople.Properties["description"].Value = "All people in organisation";
\r
69 ouPeople.Properties["ou"].Value = "people";
\r
70 ouPeople.CommitChanges();
\r
72 #endregion // Initialize basics
\r
74 #region Human Resources
\r
76 DirectoryEntry ouHumanResources = ouPeople.Children.Add("ou=Human Resources","Class");
\r
77 ouHumanResources.Properties["objectClass"].Value = "organizationalUnit";
\r
78 ouHumanResources.Properties["ou"].Value = "Human Resources";
\r
79 ouHumanResources.CommitChanges();
\r
81 DirectoryEntry cnJohnSmith = ouHumanResources.Children.Add("cn=John Smith","Class");
\r
82 cnJohnSmith.Properties["objectClass"].Value = "organizationalRole";
\r
83 cnJohnSmith.Properties["cn"].Value = "John Smith";
\r
84 cnJohnSmith.Properties["description"].Value = "Very clever person";
\r
85 cnJohnSmith.Properties["ou"].Value = "Human Resources";
\r
86 cnJohnSmith.Properties["telephoneNumber"].Value = "1 801 555 1212";
\r
87 cnJohnSmith.CommitChanges();
\r
89 DirectoryEntry cnBarakTsabari = ouHumanResources.Children.Add("cn=Barak Tsabari","Class");
\r
90 ((PropertyValueCollection)cnBarakTsabari.Properties["objectClass"]).Add("person");
\r
91 ((PropertyValueCollection)cnBarakTsabari.Properties["objectClass"]).Add("organizationalPerson");
\r
92 cnBarakTsabari.Properties["cn"].Value = "Barak Tsabari";
\r
93 cnBarakTsabari.Properties["facsimileTelephoneNumber"].Value = "+1 906 777 8853";
\r
94 ((PropertyValueCollection)cnBarakTsabari.Properties["ou"]).Add("Human Resources");
\r
95 ((PropertyValueCollection)cnBarakTsabari.Properties["ou"]).Add("People");
\r
96 cnBarakTsabari.Properties["sn"].Value = "Tsabari";
\r
97 cnBarakTsabari.Properties["telephoneNumber"].Value = "+1 906 777 8854";
\r
98 cnBarakTsabari.CommitChanges();
\r
100 #endregion // Human Resources
\r
104 DirectoryEntry ouRnD = ouPeople.Children.Add("ou=R&D","Class");
\r
105 ouRnD.Properties["objectClass"].Value = "organizationalUnit";
\r
106 ouRnD.Properties["ou"].Value = "R&D";
\r
107 ouRnD.CommitChanges();
\r
109 DirectoryEntry cnYossiCohen = ouRnD.Children.Add("cn=Yossi Cohen","Class");
\r
110 ((PropertyValueCollection)cnYossiCohen.Properties["objectClass"]).Add("person");
\r
111 ((PropertyValueCollection)cnYossiCohen.Properties["objectClass"]).Add("organizationalPerson");
\r
112 cnYossiCohen.Properties["cn"].Value = "Yossi Cohen";
\r
113 cnYossiCohen.Properties["facsimileTelephoneNumber"].Value = "+1 503 777 4498";
\r
114 ((PropertyValueCollection)cnYossiCohen.Properties["ou"]).Add("R&D");
\r
115 ((PropertyValueCollection)cnYossiCohen.Properties["ou"]).Add("People");
\r
116 cnYossiCohen.Properties["sn"].Value = "Cohen";
\r
117 cnYossiCohen.Properties["telephoneNumber"].Value = "+1 503 777 4499";
\r
118 cnYossiCohen.CommitChanges();
\r
120 DirectoryEntry cnUziCohen = ouRnD.Children.Add("cn=Uzi Cohen","Class");
\r
121 ((PropertyValueCollection)cnUziCohen.Properties["objectClass"]).Add("person");
\r
122 ((PropertyValueCollection)cnUziCohen.Properties["objectClass"]).Add("organizationalPerson");
\r
123 cnUziCohen.Properties["cn"].Value = "Uzi Cohen";
\r
124 cnUziCohen.Properties["facsimileTelephoneNumber"].Value = "+1 602 333 1234";
\r
125 ((PropertyValueCollection)cnUziCohen.Properties["ou"]).Add("R&D");
\r
126 ((PropertyValueCollection)cnUziCohen.Properties["ou"]).Add("People");
\r
127 cnUziCohen.Properties["sn"].Value = "Cohen";
\r
128 cnUziCohen.Properties["telephoneNumber"].Value = "+1 602 333 1233";
\r
129 cnUziCohen.CommitChanges();
\r
131 DirectoryEntry cnDanielCohen = ouRnD.Children.Add("cn=Daniel Cohen","Class");
\r
132 ((PropertyValueCollection)cnDanielCohen.Properties["objectClass"]).Add("person");
\r
133 ((PropertyValueCollection)cnDanielCohen.Properties["objectClass"]).Add("organizationalPerson");
\r
134 cnDanielCohen.Properties["cn"].Value = "Daniel Cohen";
\r
135 cnDanielCohen.Properties["facsimileTelephoneNumber"].Value = "+1 602 333 1235";
\r
136 ((PropertyValueCollection)cnDanielCohen.Properties["ou"]).Add("R&D");
\r
137 ((PropertyValueCollection)cnDanielCohen.Properties["ou"]).Add("People");
\r
138 cnDanielCohen.Properties["sn"].Value = "Cohen";
\r
139 cnDanielCohen.Properties["telephoneNumber"].Value = "+1 602 333 1236";
\r
140 cnDanielCohen.CommitChanges();
\r
142 DirectoryEntry cnSaraCohen = ouRnD.Children.Add("cn=Sara Cohen","Class");
\r
143 ((PropertyValueCollection)cnSaraCohen.Properties["objectClass"]).Add("person");
\r
144 ((PropertyValueCollection)cnSaraCohen.Properties["objectClass"]).Add("organizationalPerson");
\r
145 cnSaraCohen.Properties["cn"].Value = "Sara Cohen";
\r
146 cnSaraCohen.Properties["facsimileTelephoneNumber"].Value = "+1 602 333 1244";
\r
147 ((PropertyValueCollection)cnSaraCohen.Properties["ou"]).Add("R&D");
\r
148 ((PropertyValueCollection)cnSaraCohen.Properties["ou"]).Add("People");
\r
149 cnSaraCohen.Properties["sn"].Value = "Cohen";
\r
150 cnSaraCohen.Properties["telephoneNumber"].Value = "+1 602 333 1243";
\r
151 cnSaraCohen.CommitChanges();
\r
157 DirectoryEntry ouDevQA = ouPeople.Children.Add("ou=DevQA","Class");
\r
158 ouDevQA.Properties["objectClass"].Value = "organizationalUnit";
\r
159 ouDevQA.Properties["ou"].Value = "DevQA";
\r
160 ouDevQA.CommitChanges();
\r
162 DirectoryEntry cnDanielSmith = ouDevQA.Children.Add("cn=Daniel Smith","Class");
\r
163 ((PropertyValueCollection)cnDanielSmith.Properties["objectClass"]).Add("person");
\r
164 ((PropertyValueCollection)cnDanielSmith.Properties["objectClass"]).Add("organizationalPerson");
\r
165 cnDanielSmith.Properties["cn"].Value = "Daniel Smith";
\r
166 cnDanielSmith.Properties["facsimileTelephoneNumber"].Value = "+1 408 555 3372";
\r
167 cnDanielSmith.Properties["l"].Value = "Santa Clara";
\r
168 ((PropertyValueCollection)cnDanielSmith.Properties["ou"]).Add("DevQA");
\r
169 ((PropertyValueCollection)cnDanielSmith.Properties["ou"]).Add("People");
\r
170 cnDanielSmith.Properties["sn"].Value = "Smith";
\r
171 cnDanielSmith.Properties["telephoneNumber"].Value = "+1 408 555 9519";
\r
172 cnDanielSmith.CommitChanges();
\r
174 DirectoryEntry cnDanielMorgan = ouDevQA.Children.Add("cn=Daniel Morgan","Class");
\r
175 ((PropertyValueCollection)cnDanielMorgan.Properties["objectClass"]).Add("person");
\r
176 ((PropertyValueCollection)cnDanielMorgan.Properties["objectClass"]).Add("organizationalPerson");
\r
177 cnDanielMorgan.Properties["cn"].Value = "Daniel Morgan";
\r
178 cnDanielMorgan.Properties["facsimileTelephoneNumber"].Value = "+1 805 666 5645";
\r
179 ((PropertyValueCollection)cnDanielMorgan.Properties["ou"]).Add("DevQA");
\r
180 ((PropertyValueCollection)cnDanielMorgan.Properties["ou"]).Add("People");
\r
181 cnDanielMorgan.Properties["sn"].Value = "Morgan";
\r
182 cnDanielMorgan.Properties["telephoneNumber"].Value = "+1 805 666 5644";
\r
183 cnDanielMorgan.CommitChanges();
\r
185 #endregion // DevQA
\r
189 DirectoryEntry cnManager = root.Children.Add("cn=Manager","Class");
\r
190 cnManager.Properties["objectClass"].Value = "organizationalRole";
\r
191 cnManager.Properties["cn"].Value = "Manager";
\r
192 cnManager.Properties["facsimileTelephoneNumber"].Value = "+1 602 333 1238";
\r
193 cnManager.CommitChanges();
\r
195 DirectoryEntry cnUziCohen_ = cnManager.Children.Add("cn=Uzi Cohen","Class");
\r
196 ((PropertyValueCollection)cnUziCohen_.Properties["objectClass"]).Add("person");
\r
197 ((PropertyValueCollection)cnUziCohen_.Properties["objectClass"]).Add("organizationalPerson");
\r
198 cnUziCohen_.Properties["cn"].Value = "Uzi Cohen";
\r
199 cnUziCohen_.Properties["facsimileTelephoneNumber"].Value = "+1 602 333 1234";
\r
200 ((PropertyValueCollection)cnUziCohen_.Properties["ou"]).Add("R&D");
\r
201 ((PropertyValueCollection)cnUziCohen_.Properties["ou"]).Add("People");
\r
202 cnUziCohen_.Properties["sn"].Value = "Cohen";
\r
203 cnUziCohen_.Properties["telephoneNumber"].Value = "+1 602 333 1233";
\r
204 cnUziCohen_.CommitChanges();
\r
206 #endregion // Manager
\r
208 cnJohnSmith.Dispose();
\r
209 cnBarakTsabari.Dispose();
\r
210 ouHumanResources.Dispose();
\r
211 cnUziCohen.Dispose();
\r
212 cnYossiCohen.Dispose();
\r
213 cnDanielCohen.Dispose();
\r
214 cnSaraCohen.Dispose();
\r
216 cnDanielSmith.Dispose();
\r
217 cnDanielMorgan.Dispose();
\r
219 cnUziCohen_.Dispose();
\r
220 cnManager.Dispose();
\r
221 ouPeople.Dispose();
\r
227 public void TearDown()
\r
239 using (DirectoryEntry root = new DirectoryEntry( configuration.ConnectionString,
\r
240 configuration.Username,
\r
241 configuration.Password,
\r
242 configuration.AuthenticationType)){
\r
244 foreach(DirectoryEntry child in root.Children) {
\r
245 DeleteTree_DFS(child);
\r
251 private void DeleteTree_DFS(DirectoryEntry de)
\r
253 foreach(DirectoryEntry child in de.Children) {
\r
254 DeleteTree_DFS(child);
\r
257 de.CommitChanges();
\r
260 #endregion //SetUp and TearDown
\r
265 public void DirectorySearcher_DirectorySearcher()
\r
267 ds = new DirectorySearcher();
\r
269 Assert.AreEqual(ds.Filter,"(objectClass=*)");
\r
270 Assert.AreEqual(ds.PropertiesToLoad.Count,0);
\r
271 Assert.AreEqual(ds.SearchScope,SearchScope.Subtree);
\r
272 Assert.AreEqual(ds.CacheResults,true);
\r
273 Assert.AreEqual(ds.ClientTimeout,new TimeSpan(-10000000));
\r
277 public void DirectorySearcher_DirectorySearcher_De()
\r
279 de = new DirectoryEntry(configuration.ConnectionString,
\r
280 configuration.Username,
\r
281 configuration.Password,
\r
282 configuration.AuthenticationType);
\r
284 ds = new DirectorySearcher(de);
\r
286 Assert.AreEqual(ds.SearchRoot.Name,GetName (configuration.BaseDn));
\r
287 Assert.AreEqual(ds.Filter,"(objectClass=*)");
\r
288 Assert.AreEqual(ds.PropertiesToLoad.Count,0);
\r
289 Assert.AreEqual(ds.SearchScope,SearchScope.Subtree);
\r
290 Assert.AreEqual(ds.CacheResults,true);
\r
291 Assert.AreEqual(ds.ClientTimeout,new TimeSpan(-10000000));
\r
295 public void DirectorySearcher_DirectorySearcher_Str()
\r
297 ds = new DirectorySearcher("(objectClass=organizationalRole)");
\r
299 Assert.AreEqual(ds.Filter,"(objectClass=organizationalRole)");
\r
300 Assert.AreEqual(ds.PropertiesToLoad.Count,0);
\r
301 Assert.AreEqual(ds.SearchScope,SearchScope.Subtree);
\r
302 Assert.AreEqual(ds.CacheResults,true);
\r
303 Assert.AreEqual(ds.ClientTimeout,new TimeSpan(-10000000));
\r
307 public void DirectorySearcher_DirectorySearcher_DeStr()
\r
309 de = new DirectoryEntry(configuration.ConnectionString,
\r
310 configuration.Username,
\r
311 configuration.Password,
\r
312 configuration.AuthenticationType);
\r
314 ds = new DirectorySearcher(de,"(objectClass=organizationalRole)");
\r
316 Assert.AreEqual(ds.SearchRoot.Name,GetName (configuration.BaseDn));
\r
317 Assert.AreEqual(ds.Filter,"(objectClass=organizationalRole)");
\r
318 Assert.AreEqual(ds.PropertiesToLoad.Count,0);
\r
319 Assert.AreEqual(ds.SearchScope,SearchScope.Subtree);
\r
320 Assert.AreEqual(ds.CacheResults,true);
\r
321 Assert.AreEqual(ds.ClientTimeout,new TimeSpan(-10000000));
\r
325 public void DirectorySearcher_DirectorySearcher_StrStrArr()
\r
327 string[] properties = new string[] {"objectClass","ou","cn"};
\r
328 ds = new DirectorySearcher("(objectClass=organizationalRole)",properties);
\r
330 Assert.AreEqual(ds.Filter,"(objectClass=organizationalRole)");
\r
331 Assert.AreEqual(ds.PropertiesToLoad.Count,3);
\r
332 foreach(string s in properties) {
\r
333 Assert.IsTrue(ds.PropertiesToLoad.Contains(s));
\r
335 Assert.AreEqual(ds.SearchScope,SearchScope.Subtree);
\r
336 Assert.AreEqual(ds.CacheResults,true);
\r
337 Assert.AreEqual(ds.ClientTimeout,new TimeSpan(-10000000));
\r
341 public void DirectorySearcher_DirectorySearcher_DeStrStrArr()
\r
343 string[] properties = new string[] {"objectClass","ou","cn"};
\r
344 de = new DirectoryEntry(configuration.ConnectionString,
\r
345 configuration.Username,
\r
346 configuration.Password,
\r
347 configuration.AuthenticationType);
\r
349 ds = new DirectorySearcher(de,"(objectClass=organizationalRole)",properties);
\r
351 Assert.AreEqual(ds.SearchRoot.Name,GetName (configuration.BaseDn));
\r
352 Assert.AreEqual(ds.Filter,"(objectClass=organizationalRole)");
\r
353 Assert.AreEqual(ds.PropertiesToLoad.Count,3);
\r
354 foreach(string s in properties) {
\r
355 Assert.IsTrue(ds.PropertiesToLoad.Contains(s));
\r
357 Assert.AreEqual(ds.SearchScope,SearchScope.Subtree);
\r
358 Assert.AreEqual(ds.CacheResults,true);
\r
359 Assert.AreEqual(ds.ClientTimeout,new TimeSpan(-10000000));
\r
363 public void DirectorySearcher_DirectorySearcher_StrStrArrScp()
\r
365 string[] properties = new string[] {"objectClass","ou","cn"};
\r
366 ds = new DirectorySearcher("(objectClass=organizationalRole)",properties,SearchScope.Base);
\r
368 Assert.AreEqual(ds.Filter,"(objectClass=organizationalRole)");
\r
369 Assert.AreEqual(ds.PropertiesToLoad.Count,3);
\r
370 foreach(string s in properties) {
\r
371 Assert.IsTrue(ds.PropertiesToLoad.Contains(s));
\r
373 Assert.AreEqual(ds.SearchScope,SearchScope.Base);
\r
374 Assert.AreEqual(ds.CacheResults,true);
\r
375 Assert.AreEqual(ds.ClientTimeout,new TimeSpan(-10000000));
\r
379 public void DirectorySearcher_DirectorySearcher_DeStrStrArrScp()
\r
381 string[] properties = new string[] {"objectClass","ou","cn"};
\r
382 de = new DirectoryEntry(configuration.ConnectionString,
\r
383 configuration.Username,
\r
384 configuration.Password,
\r
385 configuration.AuthenticationType);
\r
387 ds = new DirectorySearcher(de,"(objectClass=organizationalRole)",properties,SearchScope.Base);
\r
389 Assert.AreEqual(ds.SearchRoot.Name,GetName (configuration.BaseDn));
\r
390 Assert.AreEqual(ds.Filter,"(objectClass=organizationalRole)");
\r
391 Assert.AreEqual(ds.PropertiesToLoad.Count,3);
\r
392 foreach(string s in properties) {
\r
393 Assert.IsTrue(ds.PropertiesToLoad.Contains(s));
\r
395 Assert.AreEqual(ds.SearchScope,SearchScope.Base);
\r
396 Assert.AreEqual(ds.CacheResults,true);
\r
397 Assert.AreEqual(ds.ClientTimeout,new TimeSpan(-10000000));
\r
401 public void DirectorySearcher_CacheResults()
\r
403 de = new DirectoryEntry(configuration.ConnectionString,
\r
404 configuration.Username,
\r
405 configuration.Password,
\r
406 configuration.AuthenticationType);
\r
408 ds = new DirectorySearcher(de,"(cn=Barak Tsabari)");
\r
409 ds.CacheResults = true;
\r
412 string newValue = "New Description";
\r
414 SearchResult result = ds.FindOne();
\r
415 SearchResult secondResult;
\r
416 using (DirectoryEntry resultDE = result.GetDirectoryEntry()){
\r
418 oldValue = (string)((PropertyValueCollection)resultDE.Properties["description"]).Value;
\r
419 ((PropertyValueCollection)resultDE.Properties["description"]).Value = newValue;
\r
420 Assert.AreEqual(((PropertyValueCollection)resultDE.Properties["description"]).Value,newValue);
\r
422 using (DirectorySearcher secondDs = new DirectorySearcher(de,"(cn=Barak Tsabari)")){
\r
423 secondResult = secondDs.FindOne();
\r
424 using (DirectoryEntry secondResultDE = secondResult.GetDirectoryEntry()){
\r
426 Assert.AreEqual(((PropertyValueCollection)secondResultDE.Properties["description"]).Value,oldValue);
\r
428 ((PropertyValueCollection)resultDE.Properties["description"]).Value = oldValue;
\r
433 ds = new DirectorySearcher(de,"(cn=Barak Tsabari)");
\r
434 ds.CacheResults = false;
\r
435 result = ds.FindOne();
\r
436 using (DirectoryEntry resultDE = result.GetDirectoryEntry()){
\r
438 ((PropertyValueCollection)resultDE.Properties["description"]).Value = newValue;
\r
439 Assert.AreEqual(((PropertyValueCollection)resultDE.Properties["description"]).Value,newValue);
\r
441 using (DirectorySearcher secondDs = new DirectorySearcher(de,"(cn=Barak Tsabari)")){
\r
442 secondResult = secondDs.FindOne();
\r
443 using (DirectoryEntry secondResultDE = secondResult.GetDirectoryEntry()){
\r
445 // LAMESPEC : according to documentation, the value retrieved should be the new one,
\r
446 // but actually it is an old one
\r
447 Assert.AreEqual(((PropertyValueCollection)secondResultDE.Properties["description"]).Value,oldValue);
\r
449 ((PropertyValueCollection)resultDE.Properties["description"]).Value = oldValue;
\r
457 public void DirectorySearcher_ClientTimeout()
\r
459 ds = new DirectorySearcher();
\r
461 Assert.AreEqual(ds.ClientTimeout,new TimeSpan(-10000000));
\r
463 ds.ClientTimeout = new TimeSpan(500000000);
\r
464 Assert.AreEqual(ds.ClientTimeout,new TimeSpan(500000000));
\r
466 ds.ClientTimeout = TimeSpan.MaxValue;
\r
467 Assert.AreEqual(ds.ClientTimeout,TimeSpan.MaxValue);
\r
469 ds.ClientTimeout = TimeSpan.MinValue;
\r
470 Assert.AreEqual(ds.ClientTimeout,TimeSpan.MinValue);
\r
474 public void DirectorySearcher_Filter1()
\r
476 de = new DirectoryEntry(configuration.ConnectionString,
\r
477 configuration.Username,
\r
478 configuration.Password,
\r
479 configuration.AuthenticationType);
\r
480 ds = new DirectorySearcher(de);
\r
482 ds.Filter = "(objectClass=person)";
\r
483 SearchResultCollection results = ds.FindAll();
\r
484 Assert.AreEqual(results.Count,8);
\r
486 ds.Filter = "(|(objectClass=person)(objectClass=organizationalUnit))";
\r
487 results = ds.FindAll();
\r
488 Assert.AreEqual(results.Count,12);
\r
490 ds.Filter = "(&(objectClass=person)(objectClass=organizationalUnit))";
\r
491 results = ds.FindAll();
\r
492 Assert.AreEqual(results.Count,0);
\r
496 [Category("NotWorking")]
\r
497 public void DirectorySearcher_Filter2()
\r
499 de = new DirectoryEntry(configuration.ConnectionString,
\r
500 configuration.Username,
\r
501 configuration.Password,
\r
502 configuration.AuthenticationType);
\r
503 ds = new DirectorySearcher(de);
\r
505 ds.Filter = "((objectClass=person))";
\r
506 SearchResultCollection results = ds.FindAll();
\r
507 Assert.AreEqual(results.Count,8);
\r
509 ds.Filter = "(|(objectClass=person)((objectClass=organizationalUnit)))";
\r
510 results = ds.FindAll();
\r
511 Assert.AreEqual(results.Count,12);
\r
513 ds.Filter = "(&((objectClass=person))(objectClass=organizationalUnit))";
\r
514 results = ds.FindAll();
\r
515 Assert.AreEqual(results.Count,0);
\r
520 public void DirectorySearcher_PageSize()
\r
522 de = new DirectoryEntry(configuration.ConnectionString,
\r
523 configuration.Username,
\r
524 configuration.Password,
\r
525 configuration.AuthenticationType);
\r
526 ds = new DirectorySearcher(de);
\r
528 Assert.AreEqual(ds.PageSize,0);
\r
530 ds.Filter = "(|(objectClass=person)(objectClass=organizationalUnit))";
\r
531 SearchResultCollection results = ds.FindAll();
\r
532 Assert.AreEqual(results.Count,12);
\r
535 Assert.AreEqual(ds.PageSize,3);
\r
537 ds.Filter = "(|(objectClass=person)(objectClass=organizationalUnit))";
\r
538 results = ds.FindAll();
\r
539 // LAMESPEC : according to documentation there should be only 3 results !!!
\r
540 Assert.AreEqual(results.Count,12);
\r
545 public void DirectorySearcher_PropertiesToLoad()
\r
547 de = new DirectoryEntry(configuration.ConnectionString,
\r
548 configuration.Username,
\r
549 configuration.Password,
\r
550 configuration.AuthenticationType);
\r
551 ds = new DirectorySearcher(de);
\r
553 Assert.AreEqual(ds.PropertiesToLoad.Count,0);
\r
555 ds.PropertiesToLoad.Add("cn");
\r
556 ds.PropertiesToLoad.Add("ADsPath");
\r
557 ds.PropertiesToLoad.Add("objectClass");
\r
559 Assert.AreEqual(ds.PropertiesToLoad.Count,3);
\r
560 Assert.IsTrue(ds.PropertiesToLoad.Contains("cn"));
\r
561 Assert.IsTrue(ds.PropertiesToLoad.Contains("ADsPath"));
\r
562 Assert.IsTrue(ds.PropertiesToLoad.Contains("objectClass"));
\r
564 ds.Filter = "(objectClass=person)";
\r
565 SearchResult result = ds.FindOne();
\r
567 Assert.AreEqual(result.Properties.Count,3);
\r
568 Assert.IsTrue(result.Properties.Contains("cn"));
\r
569 Assert.IsTrue(result.Properties.Contains("objectClass"));
\r
570 Assert.IsTrue(result.Properties.Contains("ADsPath"));
\r
573 ds.PropertiesToLoad.Clear();
\r
574 Assert.AreEqual(ds.PropertiesToLoad.Count,0);
\r
576 ds.PropertiesToLoad.Add("cn");
\r
577 ds.PropertiesToLoad.Add("objectClass");
\r
579 Assert.AreEqual(ds.PropertiesToLoad.Count,2);
\r
580 Assert.IsTrue(ds.PropertiesToLoad.Contains("cn"));
\r
581 Assert.IsTrue(ds.PropertiesToLoad.Contains("objectClass"));
\r
583 ds.Filter = "(objectClass=person)";
\r
584 result = ds.FindOne();
\r
586 Assert.AreEqual(result.Properties.Count,3);
\r
587 Assert.IsTrue(result.Properties.Contains("cn"));
\r
588 Assert.IsTrue(result.Properties.Contains("objectClass"));
\r
589 Assert.IsTrue(result.Properties.Contains("ADsPath"));
\r
592 ds.PropertiesToLoad.Clear();
\r
593 Assert.AreEqual(ds.PropertiesToLoad.Count,0);
\r
595 ds.PropertiesToLoad.Add("cn");
\r
596 ds.PropertiesToLoad.Add("dn");
\r
597 ds.PropertiesToLoad.Add("objectClass");
\r
599 Assert.AreEqual(ds.PropertiesToLoad.Count,3);
\r
600 Assert.IsTrue(ds.PropertiesToLoad.Contains("cn"));
\r
601 Assert.IsTrue(ds.PropertiesToLoad.Contains("dn"));
\r
602 Assert.IsTrue(ds.PropertiesToLoad.Contains("objectClass"));
\r
604 ds.Filter = "(objectClass=person)";
\r
605 result = ds.FindOne();
\r
607 Assert.AreEqual(result.Properties.Count,3);
\r
608 Assert.IsTrue(result.Properties.Contains("cn"));
\r
609 Assert.IsTrue(result.Properties.Contains("objectClass"));
\r
610 // FIXME : .NET framework threats "dn" as "ADsPath"
\r
611 // More on http://www.rlmueller.net/Name_Attributes.htm
\r
612 Assert.IsTrue(result.Properties.Contains("ADsPath"));
\r
616 public void DirectorySearcher_PropertyNamesOnly()
\r
618 de = new DirectoryEntry(configuration.ConnectionString,
\r
619 configuration.Username,
\r
620 configuration.Password,
\r
621 configuration.AuthenticationType);
\r
622 ds = new DirectorySearcher(de);
\r
624 Assert.AreEqual(ds.PropertyNamesOnly,false);
\r
626 // All rpoperties are loaded without values, except "ADsPath"
\r
627 ds.PropertyNamesOnly = true;
\r
629 ds.Filter = "(objectClass=person)";
\r
630 SearchResult result = ds.FindOne();
\r
632 foreach(DictionaryEntry en in result.Properties) {
\r
633 if(String.Compare((string)en.Key,"adspath",true) != 0) {
\r
634 Assert.AreEqual(((ResultPropertyValueCollection)en.Value).Count,0);
\r
637 Assert.AreEqual(((ResultPropertyValueCollection)en.Value).Count,1);
\r
642 // all properties are loaded including values
\r
643 ds.PropertyNamesOnly = false;
\r
645 ds.Filter = "(objectClass=person)";
\r
646 result = ds.FindOne();
\r
648 foreach(DictionaryEntry en in result.Properties) {
\r
649 Assert.IsTrue(((ResultPropertyValueCollection)en.Value).Count > 0);
\r
654 public void DirectorySearcher_ReferralChasing()
\r
656 de = new DirectoryEntry(configuration.ConnectionString,
\r
657 configuration.Username,
\r
658 configuration.Password,
\r
659 configuration.AuthenticationType);
\r
660 ds = new DirectorySearcher(de);
\r
662 Assert.AreEqual(ds.ReferralChasing,ReferralChasingOption.External);
\r
664 ds.ReferralChasing = ReferralChasingOption.All;
\r
665 Assert.AreEqual(ds.ReferralChasing,ReferralChasingOption.All);
\r
667 ds.ReferralChasing = ReferralChasingOption.External;
\r
668 Assert.AreEqual(ds.ReferralChasing,ReferralChasingOption.External);
\r
670 ds.ReferralChasing = ReferralChasingOption.None;
\r
671 Assert.AreEqual(ds.ReferralChasing,ReferralChasingOption.None);
\r
673 ds.ReferralChasing = ReferralChasingOption.Subordinate;
\r
674 Assert.AreEqual(ds.ReferralChasing,ReferralChasingOption.Subordinate);
\r
676 // FIXME : currently we do not have an infrastucture for good testing of this feature
\r
680 public void DirectorySearcher_SearchRoot()
\r
682 de = new DirectoryEntry(configuration.ConnectionString,
\r
683 configuration.Username,
\r
684 configuration.Password,
\r
685 configuration.AuthenticationType);
\r
686 ds = new DirectorySearcher();
\r
687 ds.SearchRoot = de;
\r
689 Assert.AreEqual(ds.SearchRoot.Name,GetName (configuration.BaseDn));
\r
691 ds.Filter = "(objectClass=person)";
\r
692 SearchResultCollection results = ds.FindAll();
\r
693 Assert.AreEqual(results.Count,8);
\r
695 de = new DirectoryEntry(configuration.ServerRoot + "ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn)),
\r
696 configuration.Username,
\r
697 configuration.Password,
\r
698 configuration.AuthenticationType);
\r
699 ds.SearchRoot = de;
\r
700 Assert.AreEqual(ds.SearchRoot.Name,"ou=people");
\r
702 results = ds.FindAll();
\r
703 Assert.AreEqual(results.Count,7);
\r
705 de = new DirectoryEntry(configuration.ServerRoot + "ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn)),
\r
706 configuration.Username,
\r
707 configuration.Password,
\r
708 configuration.AuthenticationType);
\r
709 ds.SearchRoot = de;
\r
710 Assert.AreEqual(ds.SearchRoot.Name,"ou=Human Resources");
\r
712 results = ds.FindAll();
\r
713 Assert.AreEqual(results.Count,1);
\r
717 public void DirectorySearcher_SearchScope()
\r
719 de = new DirectoryEntry(configuration.ConnectionString,
\r
720 configuration.Username,
\r
721 configuration.Password,
\r
722 configuration.AuthenticationType);
\r
723 ds = new DirectorySearcher(de);
\r
725 Assert.AreEqual(ds.SearchScope,SearchScope.Subtree);
\r
727 ds.SearchScope = SearchScope.Base;
\r
728 Assert.AreEqual(ds.SearchScope,SearchScope.Base);
\r
730 ds.Filter = "(objectClass=organizationalUnit)";
\r
731 SearchResultCollection results = ds.FindAll();
\r
732 Assert.AreEqual(results.Count,0);
\r
734 ds.SearchScope = SearchScope.OneLevel;
\r
735 Assert.AreEqual(ds.SearchScope,SearchScope.OneLevel);
\r
737 results = ds.FindAll();
\r
738 Assert.AreEqual(results.Count,1);
\r
740 ds.SearchScope = SearchScope.Subtree;
\r
741 Assert.AreEqual(ds.SearchScope,SearchScope.Subtree);
\r
743 results = ds.FindAll();
\r
744 Assert.AreEqual(results.Count,4);
\r
748 public void DirectorySearcher_ServerPageTimeLimit()
\r
750 de = new DirectoryEntry(configuration.ConnectionString,
\r
751 configuration.Username,
\r
752 configuration.Password,
\r
753 configuration.AuthenticationType);
\r
754 ds = new DirectorySearcher(de);
\r
756 Assert.AreEqual(ds.ServerPageTimeLimit,new TimeSpan(-10000000));
\r
758 // According to spec PageSize should be set to a value that is not the default of -1
\r
760 ds.ServerPageTimeLimit = new TimeSpan(500000000);
\r
761 Assert.AreEqual(ds.ServerPageTimeLimit,new TimeSpan(500000000));
\r
763 ds.ServerPageTimeLimit = TimeSpan.MaxValue;
\r
764 Assert.AreEqual(ds.ServerPageTimeLimit,TimeSpan.MaxValue);
\r
766 ds.ServerPageTimeLimit = TimeSpan.MinValue;
\r
767 Assert.AreEqual(ds.ServerPageTimeLimit,TimeSpan.MinValue);
\r
769 // FIXME : currently we do not have an infrastucture for good testing of this feature
\r
773 public void DirectorySearcher_ServerTimeLimit()
\r
775 de = new DirectoryEntry(configuration.ConnectionString,
\r
776 configuration.Username,
\r
777 configuration.Password,
\r
778 configuration.AuthenticationType);
\r
779 ds = new DirectorySearcher(de);
\r
781 Assert.AreEqual(ds.ServerTimeLimit,new TimeSpan(-10000000));
\r
783 // According to spec PageSize should be set to a value that is not the default of -1
\r
785 ds.ServerTimeLimit = new TimeSpan(500000000);
\r
786 Assert.AreEqual(ds.ServerTimeLimit,new TimeSpan(500000000));
\r
788 ds.ServerTimeLimit = TimeSpan.MaxValue;
\r
789 Assert.AreEqual(ds.ServerTimeLimit,TimeSpan.MaxValue);
\r
791 ds.ServerTimeLimit = TimeSpan.MinValue;
\r
792 Assert.AreEqual(ds.ServerTimeLimit,TimeSpan.MinValue);
\r
794 // FIXME : currently we do not have an infrastucture for good testing of this feature
\r
798 public void DirectorySearcher_SizeLimit()
\r
800 de = new DirectoryEntry(configuration.ConnectionString,
\r
801 configuration.Username,
\r
802 configuration.Password,
\r
803 configuration.AuthenticationType);
\r
804 ds = new DirectorySearcher(de);
\r
806 Assert.AreEqual(ds.SizeLimit,0);
\r
808 ds.Filter = "(|(objectClass=person)(objectClass=organizationalUnit))";
\r
809 SearchResultCollection results = ds.FindAll();
\r
810 Assert.AreEqual(results.Count,12);
\r
813 Assert.AreEqual(ds.SizeLimit,3);
\r
815 ds.Filter = "(|(objectClass=person)(objectClass=organizationalUnit))";
\r
816 results = ds.FindAll();
\r
817 Assert.AreEqual(results.Count,3);
\r
819 ds.SizeLimit = Int32.MaxValue;
\r
820 Assert.AreEqual(ds.SizeLimit,Int32.MaxValue);
\r
822 ds.Filter = "(|(objectClass=person)(objectClass=organizationalUnit))";
\r
823 results = ds.FindAll();
\r
824 Assert.AreEqual(results.Count,12);
\r
829 [ExpectedException(typeof(ArgumentException))]
\r
830 public void DirectorySearcher_SizeLimit_Neg()
\r
832 de = new DirectoryEntry(configuration.ConnectionString,
\r
833 configuration.Username,
\r
834 configuration.Password,
\r
835 configuration.AuthenticationType);
\r
836 ds = new DirectorySearcher(de);
\r
838 Assert.AreEqual(ds.SizeLimit,-1);
\r
840 SearchResultCollection results = ds.FindAll();
\r
844 public void DirectorySearcher_Sort()
\r
846 // FIXME : howto create good sorting
\r
850 public void DirectorySearcher_FindAll()
\r
852 de = new DirectoryEntry(configuration.ConnectionString,
\r
853 configuration.Username,
\r
854 configuration.Password,
\r
855 configuration.AuthenticationType);
\r
856 ds = new DirectorySearcher(de);
\r
858 ds.Filter = "(objectClass=person)";
\r
859 SearchResultCollection results = ds.FindAll();
\r
860 Assert.AreEqual(results.Count,8);
\r
862 de = new DirectoryEntry(configuration.ServerRoot + "ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn)),
\r
863 configuration.Username,
\r
864 configuration.Password,
\r
865 configuration.AuthenticationType);
\r
866 ds = new DirectorySearcher(de);
\r
868 results = ds.FindAll();
\r
869 Assert.AreEqual(results.Count,3);
\r
873 public void DirectorySearcher_FindOne()
\r
875 de = new DirectoryEntry(configuration.ConnectionString,
\r
876 configuration.Username,
\r
877 configuration.Password,
\r
878 configuration.AuthenticationType);
\r
879 ds = new DirectorySearcher(de);
\r
881 ds.Filter = "(objectClass=person)";
\r
882 SearchResult result = ds.FindOne();
\r
883 Assert.IsNotNull(result);
\r
884 Assert.AreEqual(result.GetDirectoryEntry().Name,"cn=Barak Tsabari");
\r
887 de = new DirectoryEntry(configuration.ServerRoot + "ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn)),
\r
888 configuration.Username,
\r
889 configuration.Password,
\r
890 configuration.AuthenticationType);
\r
891 ds = new DirectorySearcher(de);
\r
893 result = ds.FindOne();
\r
894 Assert.IsNotNull(result);
\r
895 Assert.AreEqual(result.GetDirectoryEntry().Name,"ou=Human Resources");
\r
897 ds.Filter = "(objectClass=Barak Tsabari)";
\r
898 result = ds.FindOne();
\r
899 Assert.IsNull(result);
\r
905 private static string GetName (string baseDn)
\r
907 if (baseDn == null || baseDn.Length == 0 || baseDn.IndexOf (',') == -1)
\r
910 int index = baseDn.IndexOf (',');
\r
912 return baseDn.Substring (0, index);
\r
915 private static string GetParentDN (string baseDn)
\r
917 if (baseDn == null || baseDn.Length == 0 || baseDn.IndexOf (',') == -1)
\r
918 return String.Empty;
\r
920 int index = baseDn.IndexOf (',');
\r
922 return baseDn.Substring (index + 1,baseDn.Length - index - 1);
\r