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 public class DirectoryServicesDirectorySearcherTest
\r
22 static TestConfiguration configuration;
\r
23 static DirectoryEntry de;
\r
24 static DirectorySearcher ds;
\r
26 #endregion // Fields
\r
28 #region SetUp and TearDown
\r
31 public void TestFixtureSetUp()
\r
34 configuration = new TestConfiguration ();
\r
36 if (String.IsNullOrEmpty (configuration.ConnectionString))
\r
37 Assert.Ignore ("No configuration");
\r
41 [TestFixtureTearDown]
\r
42 public void TestFixtureTearDown()
\r
61 #region Initialize basics
\r
63 DirectoryEntry root = new DirectoryEntry( configuration.ConnectionString,
\r
64 configuration.Username,
\r
65 configuration.Password,
\r
66 configuration.AuthenticationType);
\r
68 DirectoryEntry ouPeople = root.Children.Add("ou=people","Class");
\r
69 ouPeople.Properties["objectClass"].Value = "organizationalUnit";
\r
70 ouPeople.Properties["description"].Value = "All people in organisation";
\r
71 ouPeople.Properties["ou"].Value = "people";
\r
72 ouPeople.CommitChanges();
\r
74 #endregion // Initialize basics
\r
76 #region Human Resources
\r
78 DirectoryEntry ouHumanResources = ouPeople.Children.Add("ou=Human Resources","Class");
\r
79 ouHumanResources.Properties["objectClass"].Value = "organizationalUnit";
\r
80 ouHumanResources.Properties["ou"].Value = "Human Resources";
\r
81 ouHumanResources.CommitChanges();
\r
83 DirectoryEntry cnJohnSmith = ouHumanResources.Children.Add("cn=John Smith","Class");
\r
84 cnJohnSmith.Properties["objectClass"].Value = "organizationalRole";
\r
85 cnJohnSmith.Properties["cn"].Value = "John Smith";
\r
86 cnJohnSmith.Properties["description"].Value = "Very clever person";
\r
87 cnJohnSmith.Properties["ou"].Value = "Human Resources";
\r
88 cnJohnSmith.Properties["telephoneNumber"].Value = "1 801 555 1212";
\r
89 cnJohnSmith.CommitChanges();
\r
91 DirectoryEntry cnBarakTsabari = ouHumanResources.Children.Add("cn=Barak Tsabari","Class");
\r
92 ((PropertyValueCollection)cnBarakTsabari.Properties["objectClass"]).Add("person");
\r
93 ((PropertyValueCollection)cnBarakTsabari.Properties["objectClass"]).Add("organizationalPerson");
\r
94 cnBarakTsabari.Properties["cn"].Value = "Barak Tsabari";
\r
95 cnBarakTsabari.Properties["facsimileTelephoneNumber"].Value = "+1 906 777 8853";
\r
96 ((PropertyValueCollection)cnBarakTsabari.Properties["ou"]).Add("Human Resources");
\r
97 ((PropertyValueCollection)cnBarakTsabari.Properties["ou"]).Add("People");
\r
98 cnBarakTsabari.Properties["sn"].Value = "Tsabari";
\r
99 cnBarakTsabari.Properties["telephoneNumber"].Value = "+1 906 777 8854";
\r
100 cnBarakTsabari.CommitChanges();
\r
102 #endregion // Human Resources
\r
106 DirectoryEntry ouRnD = ouPeople.Children.Add("ou=R&D","Class");
\r
107 ouRnD.Properties["objectClass"].Value = "organizationalUnit";
\r
108 ouRnD.Properties["ou"].Value = "R&D";
\r
109 ouRnD.CommitChanges();
\r
111 DirectoryEntry cnYossiCohen = ouRnD.Children.Add("cn=Yossi Cohen","Class");
\r
112 ((PropertyValueCollection)cnYossiCohen.Properties["objectClass"]).Add("person");
\r
113 ((PropertyValueCollection)cnYossiCohen.Properties["objectClass"]).Add("organizationalPerson");
\r
114 cnYossiCohen.Properties["cn"].Value = "Yossi Cohen";
\r
115 cnYossiCohen.Properties["facsimileTelephoneNumber"].Value = "+1 503 777 4498";
\r
116 ((PropertyValueCollection)cnYossiCohen.Properties["ou"]).Add("R&D");
\r
117 ((PropertyValueCollection)cnYossiCohen.Properties["ou"]).Add("People");
\r
118 cnYossiCohen.Properties["sn"].Value = "Cohen";
\r
119 cnYossiCohen.Properties["telephoneNumber"].Value = "+1 503 777 4499";
\r
120 cnYossiCohen.CommitChanges();
\r
122 DirectoryEntry cnUziCohen = ouRnD.Children.Add("cn=Uzi Cohen","Class");
\r
123 ((PropertyValueCollection)cnUziCohen.Properties["objectClass"]).Add("person");
\r
124 ((PropertyValueCollection)cnUziCohen.Properties["objectClass"]).Add("organizationalPerson");
\r
125 cnUziCohen.Properties["cn"].Value = "Uzi Cohen";
\r
126 cnUziCohen.Properties["facsimileTelephoneNumber"].Value = "+1 602 333 1234";
\r
127 ((PropertyValueCollection)cnUziCohen.Properties["ou"]).Add("R&D");
\r
128 ((PropertyValueCollection)cnUziCohen.Properties["ou"]).Add("People");
\r
129 cnUziCohen.Properties["sn"].Value = "Cohen";
\r
130 cnUziCohen.Properties["telephoneNumber"].Value = "+1 602 333 1233";
\r
131 cnUziCohen.CommitChanges();
\r
133 DirectoryEntry cnDanielCohen = ouRnD.Children.Add("cn=Daniel Cohen","Class");
\r
134 ((PropertyValueCollection)cnDanielCohen.Properties["objectClass"]).Add("person");
\r
135 ((PropertyValueCollection)cnDanielCohen.Properties["objectClass"]).Add("organizationalPerson");
\r
136 cnDanielCohen.Properties["cn"].Value = "Daniel Cohen";
\r
137 cnDanielCohen.Properties["facsimileTelephoneNumber"].Value = "+1 602 333 1235";
\r
138 ((PropertyValueCollection)cnDanielCohen.Properties["ou"]).Add("R&D");
\r
139 ((PropertyValueCollection)cnDanielCohen.Properties["ou"]).Add("People");
\r
140 cnDanielCohen.Properties["sn"].Value = "Cohen";
\r
141 cnDanielCohen.Properties["telephoneNumber"].Value = "+1 602 333 1236";
\r
142 cnDanielCohen.CommitChanges();
\r
144 DirectoryEntry cnSaraCohen = ouRnD.Children.Add("cn=Sara Cohen","Class");
\r
145 ((PropertyValueCollection)cnSaraCohen.Properties["objectClass"]).Add("person");
\r
146 ((PropertyValueCollection)cnSaraCohen.Properties["objectClass"]).Add("organizationalPerson");
\r
147 cnSaraCohen.Properties["cn"].Value = "Sara Cohen";
\r
148 cnSaraCohen.Properties["facsimileTelephoneNumber"].Value = "+1 602 333 1244";
\r
149 ((PropertyValueCollection)cnSaraCohen.Properties["ou"]).Add("R&D");
\r
150 ((PropertyValueCollection)cnSaraCohen.Properties["ou"]).Add("People");
\r
151 cnSaraCohen.Properties["sn"].Value = "Cohen";
\r
152 cnSaraCohen.Properties["telephoneNumber"].Value = "+1 602 333 1243";
\r
153 cnSaraCohen.CommitChanges();
\r
159 DirectoryEntry ouDevQA = ouPeople.Children.Add("ou=DevQA","Class");
\r
160 ouDevQA.Properties["objectClass"].Value = "organizationalUnit";
\r
161 ouDevQA.Properties["ou"].Value = "DevQA";
\r
162 ouDevQA.CommitChanges();
\r
164 DirectoryEntry cnDanielSmith = ouDevQA.Children.Add("cn=Daniel Smith","Class");
\r
165 ((PropertyValueCollection)cnDanielSmith.Properties["objectClass"]).Add("person");
\r
166 ((PropertyValueCollection)cnDanielSmith.Properties["objectClass"]).Add("organizationalPerson");
\r
167 cnDanielSmith.Properties["cn"].Value = "Daniel Smith";
\r
168 cnDanielSmith.Properties["facsimileTelephoneNumber"].Value = "+1 408 555 3372";
\r
169 cnDanielSmith.Properties["l"].Value = "Santa Clara";
\r
170 ((PropertyValueCollection)cnDanielSmith.Properties["ou"]).Add("DevQA");
\r
171 ((PropertyValueCollection)cnDanielSmith.Properties["ou"]).Add("People");
\r
172 cnDanielSmith.Properties["sn"].Value = "Smith";
\r
173 cnDanielSmith.Properties["telephoneNumber"].Value = "+1 408 555 9519";
\r
174 cnDanielSmith.CommitChanges();
\r
176 DirectoryEntry cnDanielMorgan = ouDevQA.Children.Add("cn=Daniel Morgan","Class");
\r
177 ((PropertyValueCollection)cnDanielMorgan.Properties["objectClass"]).Add("person");
\r
178 ((PropertyValueCollection)cnDanielMorgan.Properties["objectClass"]).Add("organizationalPerson");
\r
179 cnDanielMorgan.Properties["cn"].Value = "Daniel Morgan";
\r
180 cnDanielMorgan.Properties["facsimileTelephoneNumber"].Value = "+1 805 666 5645";
\r
181 ((PropertyValueCollection)cnDanielMorgan.Properties["ou"]).Add("DevQA");
\r
182 ((PropertyValueCollection)cnDanielMorgan.Properties["ou"]).Add("People");
\r
183 cnDanielMorgan.Properties["sn"].Value = "Morgan";
\r
184 cnDanielMorgan.Properties["telephoneNumber"].Value = "+1 805 666 5644";
\r
185 cnDanielMorgan.CommitChanges();
\r
187 #endregion // DevQA
\r
191 DirectoryEntry cnManager = root.Children.Add("cn=Manager","Class");
\r
192 cnManager.Properties["objectClass"].Value = "organizationalRole";
\r
193 cnManager.Properties["cn"].Value = "Manager";
\r
194 cnManager.Properties["facsimileTelephoneNumber"].Value = "+1 602 333 1238";
\r
195 cnManager.CommitChanges();
\r
197 DirectoryEntry cnUziCohen_ = cnManager.Children.Add("cn=Uzi Cohen","Class");
\r
198 ((PropertyValueCollection)cnUziCohen_.Properties["objectClass"]).Add("person");
\r
199 ((PropertyValueCollection)cnUziCohen_.Properties["objectClass"]).Add("organizationalPerson");
\r
200 cnUziCohen_.Properties["cn"].Value = "Uzi Cohen";
\r
201 cnUziCohen_.Properties["facsimileTelephoneNumber"].Value = "+1 602 333 1234";
\r
202 ((PropertyValueCollection)cnUziCohen_.Properties["ou"]).Add("R&D");
\r
203 ((PropertyValueCollection)cnUziCohen_.Properties["ou"]).Add("People");
\r
204 cnUziCohen_.Properties["sn"].Value = "Cohen";
\r
205 cnUziCohen_.Properties["telephoneNumber"].Value = "+1 602 333 1233";
\r
206 cnUziCohen_.CommitChanges();
\r
208 #endregion // Manager
\r
210 cnJohnSmith.Dispose();
\r
211 cnBarakTsabari.Dispose();
\r
212 ouHumanResources.Dispose();
\r
213 cnUziCohen.Dispose();
\r
214 cnYossiCohen.Dispose();
\r
215 cnDanielCohen.Dispose();
\r
216 cnSaraCohen.Dispose();
\r
218 cnDanielSmith.Dispose();
\r
219 cnDanielMorgan.Dispose();
\r
221 cnUziCohen_.Dispose();
\r
222 cnManager.Dispose();
\r
223 ouPeople.Dispose();
\r
229 public void TearDown()
\r
241 using (DirectoryEntry root = new DirectoryEntry( configuration.ConnectionString,
\r
242 configuration.Username,
\r
243 configuration.Password,
\r
244 configuration.AuthenticationType)){
\r
246 foreach(DirectoryEntry child in root.Children) {
\r
247 DeleteTree_DFS(child);
\r
253 private void DeleteTree_DFS(DirectoryEntry de)
\r
255 foreach(DirectoryEntry child in de.Children) {
\r
256 DeleteTree_DFS(child);
\r
259 de.CommitChanges();
\r
262 #endregion //SetUp and TearDown
\r
267 public void DirectorySearcher_DirectorySearcher()
\r
269 ds = new DirectorySearcher();
\r
271 Assert.AreEqual(ds.Filter,"(objectClass=*)");
\r
272 Assert.AreEqual(ds.PropertiesToLoad.Count,0);
\r
273 Assert.AreEqual(ds.SearchScope,SearchScope.Subtree);
\r
274 Assert.AreEqual(ds.CacheResults,true);
\r
275 Assert.AreEqual(ds.ClientTimeout,new TimeSpan(-10000000));
\r
279 public void DirectorySearcher_DirectorySearcher_De()
\r
281 de = new DirectoryEntry(configuration.ConnectionString,
\r
282 configuration.Username,
\r
283 configuration.Password,
\r
284 configuration.AuthenticationType);
\r
286 ds = new DirectorySearcher(de);
\r
288 Assert.AreEqual(ds.SearchRoot.Name,GetName (configuration.BaseDn));
\r
289 Assert.AreEqual(ds.Filter,"(objectClass=*)");
\r
290 Assert.AreEqual(ds.PropertiesToLoad.Count,0);
\r
291 Assert.AreEqual(ds.SearchScope,SearchScope.Subtree);
\r
292 Assert.AreEqual(ds.CacheResults,true);
\r
293 Assert.AreEqual(ds.ClientTimeout,new TimeSpan(-10000000));
\r
297 public void DirectorySearcher_DirectorySearcher_Str()
\r
299 ds = new DirectorySearcher("(objectClass=organizationalRole)");
\r
301 Assert.AreEqual(ds.Filter,"(objectClass=organizationalRole)");
\r
302 Assert.AreEqual(ds.PropertiesToLoad.Count,0);
\r
303 Assert.AreEqual(ds.SearchScope,SearchScope.Subtree);
\r
304 Assert.AreEqual(ds.CacheResults,true);
\r
305 Assert.AreEqual(ds.ClientTimeout,new TimeSpan(-10000000));
\r
309 public void DirectorySearcher_DirectorySearcher_DeStr()
\r
311 de = new DirectoryEntry(configuration.ConnectionString,
\r
312 configuration.Username,
\r
313 configuration.Password,
\r
314 configuration.AuthenticationType);
\r
316 ds = new DirectorySearcher(de,"(objectClass=organizationalRole)");
\r
318 Assert.AreEqual(ds.SearchRoot.Name,GetName (configuration.BaseDn));
\r
319 Assert.AreEqual(ds.Filter,"(objectClass=organizationalRole)");
\r
320 Assert.AreEqual(ds.PropertiesToLoad.Count,0);
\r
321 Assert.AreEqual(ds.SearchScope,SearchScope.Subtree);
\r
322 Assert.AreEqual(ds.CacheResults,true);
\r
323 Assert.AreEqual(ds.ClientTimeout,new TimeSpan(-10000000));
\r
327 public void DirectorySearcher_DirectorySearcher_StrStrArr()
\r
329 string[] properties = new string[] {"objectClass","ou","cn"};
\r
330 ds = new DirectorySearcher("(objectClass=organizationalRole)",properties);
\r
332 Assert.AreEqual(ds.Filter,"(objectClass=organizationalRole)");
\r
333 Assert.AreEqual(ds.PropertiesToLoad.Count,3);
\r
334 foreach(string s in properties) {
\r
335 Assert.IsTrue(ds.PropertiesToLoad.Contains(s));
\r
337 Assert.AreEqual(ds.SearchScope,SearchScope.Subtree);
\r
338 Assert.AreEqual(ds.CacheResults,true);
\r
339 Assert.AreEqual(ds.ClientTimeout,new TimeSpan(-10000000));
\r
343 public void DirectorySearcher_DirectorySearcher_DeStrStrArr()
\r
345 string[] properties = new string[] {"objectClass","ou","cn"};
\r
346 de = new DirectoryEntry(configuration.ConnectionString,
\r
347 configuration.Username,
\r
348 configuration.Password,
\r
349 configuration.AuthenticationType);
\r
351 ds = new DirectorySearcher(de,"(objectClass=organizationalRole)",properties);
\r
353 Assert.AreEqual(ds.SearchRoot.Name,GetName (configuration.BaseDn));
\r
354 Assert.AreEqual(ds.Filter,"(objectClass=organizationalRole)");
\r
355 Assert.AreEqual(ds.PropertiesToLoad.Count,3);
\r
356 foreach(string s in properties) {
\r
357 Assert.IsTrue(ds.PropertiesToLoad.Contains(s));
\r
359 Assert.AreEqual(ds.SearchScope,SearchScope.Subtree);
\r
360 Assert.AreEqual(ds.CacheResults,true);
\r
361 Assert.AreEqual(ds.ClientTimeout,new TimeSpan(-10000000));
\r
365 public void DirectorySearcher_DirectorySearcher_StrStrArrScp()
\r
367 string[] properties = new string[] {"objectClass","ou","cn"};
\r
368 ds = new DirectorySearcher("(objectClass=organizationalRole)",properties,SearchScope.Base);
\r
370 Assert.AreEqual(ds.Filter,"(objectClass=organizationalRole)");
\r
371 Assert.AreEqual(ds.PropertiesToLoad.Count,3);
\r
372 foreach(string s in properties) {
\r
373 Assert.IsTrue(ds.PropertiesToLoad.Contains(s));
\r
375 Assert.AreEqual(ds.SearchScope,SearchScope.Base);
\r
376 Assert.AreEqual(ds.CacheResults,true);
\r
377 Assert.AreEqual(ds.ClientTimeout,new TimeSpan(-10000000));
\r
381 public void DirectorySearcher_DirectorySearcher_DeStrStrArrScp()
\r
383 string[] properties = new string[] {"objectClass","ou","cn"};
\r
384 de = new DirectoryEntry(configuration.ConnectionString,
\r
385 configuration.Username,
\r
386 configuration.Password,
\r
387 configuration.AuthenticationType);
\r
389 ds = new DirectorySearcher(de,"(objectClass=organizationalRole)",properties,SearchScope.Base);
\r
391 Assert.AreEqual(ds.SearchRoot.Name,GetName (configuration.BaseDn));
\r
392 Assert.AreEqual(ds.Filter,"(objectClass=organizationalRole)");
\r
393 Assert.AreEqual(ds.PropertiesToLoad.Count,3);
\r
394 foreach(string s in properties) {
\r
395 Assert.IsTrue(ds.PropertiesToLoad.Contains(s));
\r
397 Assert.AreEqual(ds.SearchScope,SearchScope.Base);
\r
398 Assert.AreEqual(ds.CacheResults,true);
\r
399 Assert.AreEqual(ds.ClientTimeout,new TimeSpan(-10000000));
\r
403 public void DirectorySearcher_CacheResults()
\r
405 de = new DirectoryEntry(configuration.ConnectionString,
\r
406 configuration.Username,
\r
407 configuration.Password,
\r
408 configuration.AuthenticationType);
\r
410 ds = new DirectorySearcher(de,"(cn=Barak Tsabari)");
\r
411 ds.CacheResults = true;
\r
414 string newValue = "New Description";
\r
416 SearchResult result = ds.FindOne();
\r
417 SearchResult secondResult;
\r
418 using (DirectoryEntry resultDE = result.GetDirectoryEntry()){
\r
420 oldValue = (string)((PropertyValueCollection)resultDE.Properties["description"]).Value;
\r
421 ((PropertyValueCollection)resultDE.Properties["description"]).Value = newValue;
\r
422 Assert.AreEqual(((PropertyValueCollection)resultDE.Properties["description"]).Value,newValue);
\r
424 using (DirectorySearcher secondDs = new DirectorySearcher(de,"(cn=Barak Tsabari)")){
\r
425 secondResult = secondDs.FindOne();
\r
426 using (DirectoryEntry secondResultDE = secondResult.GetDirectoryEntry()){
\r
428 Assert.AreEqual(((PropertyValueCollection)secondResultDE.Properties["description"]).Value,oldValue);
\r
430 ((PropertyValueCollection)resultDE.Properties["description"]).Value = oldValue;
\r
435 ds = new DirectorySearcher(de,"(cn=Barak Tsabari)");
\r
436 ds.CacheResults = false;
\r
437 result = ds.FindOne();
\r
438 using (DirectoryEntry resultDE = result.GetDirectoryEntry()){
\r
440 ((PropertyValueCollection)resultDE.Properties["description"]).Value = newValue;
\r
441 Assert.AreEqual(((PropertyValueCollection)resultDE.Properties["description"]).Value,newValue);
\r
443 using (DirectorySearcher secondDs = new DirectorySearcher(de,"(cn=Barak Tsabari)")){
\r
444 secondResult = secondDs.FindOne();
\r
445 using (DirectoryEntry secondResultDE = secondResult.GetDirectoryEntry()){
\r
447 // LAMESPEC : according to documentation, the value retrieved should be the new one,
\r
448 // but actually it is an old one
\r
449 Assert.AreEqual(((PropertyValueCollection)secondResultDE.Properties["description"]).Value,oldValue);
\r
451 ((PropertyValueCollection)resultDE.Properties["description"]).Value = oldValue;
\r
459 public void DirectorySearcher_ClientTimeout()
\r
461 ds = new DirectorySearcher();
\r
463 Assert.AreEqual(ds.ClientTimeout,new TimeSpan(-10000000));
\r
465 ds.ClientTimeout = new TimeSpan(500000000);
\r
466 Assert.AreEqual(ds.ClientTimeout,new TimeSpan(500000000));
\r
468 ds.ClientTimeout = TimeSpan.MaxValue;
\r
469 Assert.AreEqual(ds.ClientTimeout,TimeSpan.MaxValue);
\r
471 ds.ClientTimeout = TimeSpan.MinValue;
\r
472 Assert.AreEqual(ds.ClientTimeout,TimeSpan.MinValue);
\r
476 public void DirectorySearcher_Filter1()
\r
478 de = new DirectoryEntry(configuration.ConnectionString,
\r
479 configuration.Username,
\r
480 configuration.Password,
\r
481 configuration.AuthenticationType);
\r
482 ds = new DirectorySearcher(de);
\r
484 ds.Filter = "(objectClass=person)";
\r
485 SearchResultCollection results = ds.FindAll();
\r
486 Assert.AreEqual(results.Count,8);
\r
488 ds.Filter = "(|(objectClass=person)(objectClass=organizationalUnit))";
\r
489 results = ds.FindAll();
\r
490 Assert.AreEqual(results.Count,12);
\r
492 ds.Filter = "(&(objectClass=person)(objectClass=organizationalUnit))";
\r
493 results = ds.FindAll();
\r
494 Assert.AreEqual(results.Count,0);
\r
498 [Category("NotWorking")]
\r
499 public void DirectorySearcher_Filter2()
\r
501 de = new DirectoryEntry(configuration.ConnectionString,
\r
502 configuration.Username,
\r
503 configuration.Password,
\r
504 configuration.AuthenticationType);
\r
505 ds = new DirectorySearcher(de);
\r
507 ds.Filter = "((objectClass=person))";
\r
508 SearchResultCollection results = ds.FindAll();
\r
509 Assert.AreEqual(results.Count,8);
\r
511 ds.Filter = "(|(objectClass=person)((objectClass=organizationalUnit)))";
\r
512 results = ds.FindAll();
\r
513 Assert.AreEqual(results.Count,12);
\r
515 ds.Filter = "(&((objectClass=person))(objectClass=organizationalUnit))";
\r
516 results = ds.FindAll();
\r
517 Assert.AreEqual(results.Count,0);
\r
522 public void DirectorySearcher_PageSize()
\r
524 de = new DirectoryEntry(configuration.ConnectionString,
\r
525 configuration.Username,
\r
526 configuration.Password,
\r
527 configuration.AuthenticationType);
\r
528 ds = new DirectorySearcher(de);
\r
530 Assert.AreEqual(ds.PageSize,0);
\r
532 ds.Filter = "(|(objectClass=person)(objectClass=organizationalUnit))";
\r
533 SearchResultCollection results = ds.FindAll();
\r
534 Assert.AreEqual(results.Count,12);
\r
537 Assert.AreEqual(ds.PageSize,3);
\r
539 ds.Filter = "(|(objectClass=person)(objectClass=organizationalUnit))";
\r
540 results = ds.FindAll();
\r
541 // LAMESPEC : according to documentation there should be only 3 results !!!
\r
542 Assert.AreEqual(results.Count,12);
\r
547 public void DirectorySearcher_PropertiesToLoad()
\r
549 de = new DirectoryEntry(configuration.ConnectionString,
\r
550 configuration.Username,
\r
551 configuration.Password,
\r
552 configuration.AuthenticationType);
\r
553 ds = new DirectorySearcher(de);
\r
555 Assert.AreEqual(ds.PropertiesToLoad.Count,0);
\r
557 ds.PropertiesToLoad.Add("cn");
\r
558 ds.PropertiesToLoad.Add("ADsPath");
\r
559 ds.PropertiesToLoad.Add("objectClass");
\r
561 Assert.AreEqual(ds.PropertiesToLoad.Count,3);
\r
562 Assert.IsTrue(ds.PropertiesToLoad.Contains("cn"));
\r
563 Assert.IsTrue(ds.PropertiesToLoad.Contains("ADsPath"));
\r
564 Assert.IsTrue(ds.PropertiesToLoad.Contains("objectClass"));
\r
566 ds.Filter = "(objectClass=person)";
\r
567 SearchResult result = ds.FindOne();
\r
569 Assert.AreEqual(result.Properties.Count,3);
\r
570 Assert.IsTrue(result.Properties.Contains("cn"));
\r
571 Assert.IsTrue(result.Properties.Contains("objectClass"));
\r
572 Assert.IsTrue(result.Properties.Contains("ADsPath"));
\r
575 ds.PropertiesToLoad.Clear();
\r
576 Assert.AreEqual(ds.PropertiesToLoad.Count,0);
\r
578 ds.PropertiesToLoad.Add("cn");
\r
579 ds.PropertiesToLoad.Add("objectClass");
\r
581 Assert.AreEqual(ds.PropertiesToLoad.Count,2);
\r
582 Assert.IsTrue(ds.PropertiesToLoad.Contains("cn"));
\r
583 Assert.IsTrue(ds.PropertiesToLoad.Contains("objectClass"));
\r
585 ds.Filter = "(objectClass=person)";
\r
586 result = ds.FindOne();
\r
588 Assert.AreEqual(result.Properties.Count,3);
\r
589 Assert.IsTrue(result.Properties.Contains("cn"));
\r
590 Assert.IsTrue(result.Properties.Contains("objectClass"));
\r
591 Assert.IsTrue(result.Properties.Contains("ADsPath"));
\r
594 ds.PropertiesToLoad.Clear();
\r
595 Assert.AreEqual(ds.PropertiesToLoad.Count,0);
\r
597 ds.PropertiesToLoad.Add("cn");
\r
598 ds.PropertiesToLoad.Add("dn");
\r
599 ds.PropertiesToLoad.Add("objectClass");
\r
601 Assert.AreEqual(ds.PropertiesToLoad.Count,3);
\r
602 Assert.IsTrue(ds.PropertiesToLoad.Contains("cn"));
\r
603 Assert.IsTrue(ds.PropertiesToLoad.Contains("dn"));
\r
604 Assert.IsTrue(ds.PropertiesToLoad.Contains("objectClass"));
\r
606 ds.Filter = "(objectClass=person)";
\r
607 result = ds.FindOne();
\r
609 Assert.AreEqual(result.Properties.Count,3);
\r
610 Assert.IsTrue(result.Properties.Contains("cn"));
\r
611 Assert.IsTrue(result.Properties.Contains("objectClass"));
\r
612 // FIXME : .NET framework threats "dn" as "ADsPath"
\r
613 // More on http://www.rlmueller.net/Name_Attributes.htm
\r
614 Assert.IsTrue(result.Properties.Contains("ADsPath"));
\r
618 public void DirectorySearcher_PropertyNamesOnly()
\r
620 de = new DirectoryEntry(configuration.ConnectionString,
\r
621 configuration.Username,
\r
622 configuration.Password,
\r
623 configuration.AuthenticationType);
\r
624 ds = new DirectorySearcher(de);
\r
626 Assert.AreEqual(ds.PropertyNamesOnly,false);
\r
628 // All rpoperties are loaded without values, except "ADsPath"
\r
629 ds.PropertyNamesOnly = true;
\r
631 ds.Filter = "(objectClass=person)";
\r
632 SearchResult result = ds.FindOne();
\r
634 foreach(DictionaryEntry en in result.Properties) {
\r
635 if(String.Compare((string)en.Key,"adspath",true) != 0) {
\r
636 Assert.AreEqual(((ResultPropertyValueCollection)en.Value).Count,0);
\r
639 Assert.AreEqual(((ResultPropertyValueCollection)en.Value).Count,1);
\r
644 // all properties are loaded including values
\r
645 ds.PropertyNamesOnly = false;
\r
647 ds.Filter = "(objectClass=person)";
\r
648 result = ds.FindOne();
\r
650 foreach(DictionaryEntry en in result.Properties) {
\r
651 Assert.IsTrue(((ResultPropertyValueCollection)en.Value).Count > 0);
\r
656 public void DirectorySearcher_ReferralChasing()
\r
658 de = new DirectoryEntry(configuration.ConnectionString,
\r
659 configuration.Username,
\r
660 configuration.Password,
\r
661 configuration.AuthenticationType);
\r
662 ds = new DirectorySearcher(de);
\r
664 Assert.AreEqual(ds.ReferralChasing,ReferralChasingOption.External);
\r
666 ds.ReferralChasing = ReferralChasingOption.All;
\r
667 Assert.AreEqual(ds.ReferralChasing,ReferralChasingOption.All);
\r
669 ds.ReferralChasing = ReferralChasingOption.External;
\r
670 Assert.AreEqual(ds.ReferralChasing,ReferralChasingOption.External);
\r
672 ds.ReferralChasing = ReferralChasingOption.None;
\r
673 Assert.AreEqual(ds.ReferralChasing,ReferralChasingOption.None);
\r
675 ds.ReferralChasing = ReferralChasingOption.Subordinate;
\r
676 Assert.AreEqual(ds.ReferralChasing,ReferralChasingOption.Subordinate);
\r
678 // FIXME : currently we do not have an infrastucture for good testing of this feature
\r
682 public void DirectorySearcher_SearchRoot()
\r
684 de = new DirectoryEntry(configuration.ConnectionString,
\r
685 configuration.Username,
\r
686 configuration.Password,
\r
687 configuration.AuthenticationType);
\r
688 ds = new DirectorySearcher();
\r
689 ds.SearchRoot = de;
\r
691 Assert.AreEqual(ds.SearchRoot.Name,GetName (configuration.BaseDn));
\r
693 ds.Filter = "(objectClass=person)";
\r
694 SearchResultCollection results = ds.FindAll();
\r
695 Assert.AreEqual(results.Count,8);
\r
697 de = new DirectoryEntry(configuration.ServerRoot + "ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn)),
\r
698 configuration.Username,
\r
699 configuration.Password,
\r
700 configuration.AuthenticationType);
\r
701 ds.SearchRoot = de;
\r
702 Assert.AreEqual(ds.SearchRoot.Name,"ou=people");
\r
704 results = ds.FindAll();
\r
705 Assert.AreEqual(results.Count,7);
\r
707 de = new DirectoryEntry(configuration.ServerRoot + "ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn)),
\r
708 configuration.Username,
\r
709 configuration.Password,
\r
710 configuration.AuthenticationType);
\r
711 ds.SearchRoot = de;
\r
712 Assert.AreEqual(ds.SearchRoot.Name,"ou=Human Resources");
\r
714 results = ds.FindAll();
\r
715 Assert.AreEqual(results.Count,1);
\r
719 public void DirectorySearcher_SearchScope()
\r
721 de = new DirectoryEntry(configuration.ConnectionString,
\r
722 configuration.Username,
\r
723 configuration.Password,
\r
724 configuration.AuthenticationType);
\r
725 ds = new DirectorySearcher(de);
\r
727 Assert.AreEqual(ds.SearchScope,SearchScope.Subtree);
\r
729 ds.SearchScope = SearchScope.Base;
\r
730 Assert.AreEqual(ds.SearchScope,SearchScope.Base);
\r
732 ds.Filter = "(objectClass=organizationalUnit)";
\r
733 SearchResultCollection results = ds.FindAll();
\r
734 Assert.AreEqual(results.Count,0);
\r
736 ds.SearchScope = SearchScope.OneLevel;
\r
737 Assert.AreEqual(ds.SearchScope,SearchScope.OneLevel);
\r
739 results = ds.FindAll();
\r
740 Assert.AreEqual(results.Count,1);
\r
742 ds.SearchScope = SearchScope.Subtree;
\r
743 Assert.AreEqual(ds.SearchScope,SearchScope.Subtree);
\r
745 results = ds.FindAll();
\r
746 Assert.AreEqual(results.Count,4);
\r
750 public void DirectorySearcher_ServerPageTimeLimit()
\r
752 de = new DirectoryEntry(configuration.ConnectionString,
\r
753 configuration.Username,
\r
754 configuration.Password,
\r
755 configuration.AuthenticationType);
\r
756 ds = new DirectorySearcher(de);
\r
758 Assert.AreEqual(ds.ServerPageTimeLimit,new TimeSpan(-10000000));
\r
760 // According to spec PageSize should be set to a value that is not the default of -1
\r
762 ds.ServerPageTimeLimit = new TimeSpan(500000000);
\r
763 Assert.AreEqual(ds.ServerPageTimeLimit,new TimeSpan(500000000));
\r
765 ds.ServerPageTimeLimit = TimeSpan.MaxValue;
\r
766 Assert.AreEqual(ds.ServerPageTimeLimit,TimeSpan.MaxValue);
\r
768 ds.ServerPageTimeLimit = TimeSpan.MinValue;
\r
769 Assert.AreEqual(ds.ServerPageTimeLimit,TimeSpan.MinValue);
\r
771 // FIXME : currently we do not have an infrastucture for good testing of this feature
\r
775 public void DirectorySearcher_ServerTimeLimit()
\r
777 de = new DirectoryEntry(configuration.ConnectionString,
\r
778 configuration.Username,
\r
779 configuration.Password,
\r
780 configuration.AuthenticationType);
\r
781 ds = new DirectorySearcher(de);
\r
783 Assert.AreEqual(ds.ServerTimeLimit,new TimeSpan(-10000000));
\r
785 // According to spec PageSize should be set to a value that is not the default of -1
\r
787 ds.ServerTimeLimit = new TimeSpan(500000000);
\r
788 Assert.AreEqual(ds.ServerTimeLimit,new TimeSpan(500000000));
\r
790 ds.ServerTimeLimit = TimeSpan.MaxValue;
\r
791 Assert.AreEqual(ds.ServerTimeLimit,TimeSpan.MaxValue);
\r
793 ds.ServerTimeLimit = TimeSpan.MinValue;
\r
794 Assert.AreEqual(ds.ServerTimeLimit,TimeSpan.MinValue);
\r
796 // FIXME : currently we do not have an infrastucture for good testing of this feature
\r
800 public void DirectorySearcher_SizeLimit()
\r
802 de = new DirectoryEntry(configuration.ConnectionString,
\r
803 configuration.Username,
\r
804 configuration.Password,
\r
805 configuration.AuthenticationType);
\r
806 ds = new DirectorySearcher(de);
\r
808 Assert.AreEqual(ds.SizeLimit,0);
\r
810 ds.Filter = "(|(objectClass=person)(objectClass=organizationalUnit))";
\r
811 SearchResultCollection results = ds.FindAll();
\r
812 Assert.AreEqual(results.Count,12);
\r
815 Assert.AreEqual(ds.SizeLimit,3);
\r
817 ds.Filter = "(|(objectClass=person)(objectClass=organizationalUnit))";
\r
818 results = ds.FindAll();
\r
819 Assert.AreEqual(results.Count,3);
\r
821 ds.SizeLimit = Int32.MaxValue;
\r
822 Assert.AreEqual(ds.SizeLimit,Int32.MaxValue);
\r
824 ds.Filter = "(|(objectClass=person)(objectClass=organizationalUnit))";
\r
825 results = ds.FindAll();
\r
826 Assert.AreEqual(results.Count,12);
\r
831 [ExpectedException(typeof(ArgumentException))]
\r
832 public void DirectorySearcher_SizeLimit_Neg()
\r
834 de = new DirectoryEntry(configuration.ConnectionString,
\r
835 configuration.Username,
\r
836 configuration.Password,
\r
837 configuration.AuthenticationType);
\r
838 ds = new DirectorySearcher(de);
\r
840 Assert.AreEqual(ds.SizeLimit,-1);
\r
842 SearchResultCollection results = ds.FindAll();
\r
846 public void DirectorySearcher_Sort()
\r
848 // FIXME : howto create good sorting
\r
852 public void DirectorySearcher_FindAll()
\r
854 de = new DirectoryEntry(configuration.ConnectionString,
\r
855 configuration.Username,
\r
856 configuration.Password,
\r
857 configuration.AuthenticationType);
\r
858 ds = new DirectorySearcher(de);
\r
860 ds.Filter = "(objectClass=person)";
\r
861 SearchResultCollection results = ds.FindAll();
\r
862 Assert.AreEqual(results.Count,8);
\r
864 de = new DirectoryEntry(configuration.ServerRoot + "ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn)),
\r
865 configuration.Username,
\r
866 configuration.Password,
\r
867 configuration.AuthenticationType);
\r
868 ds = new DirectorySearcher(de);
\r
870 results = ds.FindAll();
\r
871 Assert.AreEqual(results.Count,3);
\r
875 public void DirectorySearcher_FindOne()
\r
877 de = new DirectoryEntry(configuration.ConnectionString,
\r
878 configuration.Username,
\r
879 configuration.Password,
\r
880 configuration.AuthenticationType);
\r
881 ds = new DirectorySearcher(de);
\r
883 ds.Filter = "(objectClass=person)";
\r
884 SearchResult result = ds.FindOne();
\r
885 Assert.IsNotNull(result);
\r
886 Assert.AreEqual(result.GetDirectoryEntry().Name,"cn=Barak Tsabari");
\r
889 de = new DirectoryEntry(configuration.ServerRoot + "ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn)),
\r
890 configuration.Username,
\r
891 configuration.Password,
\r
892 configuration.AuthenticationType);
\r
893 ds = new DirectorySearcher(de);
\r
895 result = ds.FindOne();
\r
896 Assert.IsNotNull(result);
\r
897 Assert.AreEqual(result.GetDirectoryEntry().Name,"ou=Human Resources");
\r
899 ds.Filter = "(objectClass=Barak Tsabari)";
\r
900 result = ds.FindOne();
\r
901 Assert.IsNull(result);
\r
907 private static string GetName (string baseDn)
\r
909 if (baseDn == null || baseDn.Length == 0 || baseDn.IndexOf (',') == -1)
\r
912 int index = baseDn.IndexOf (',');
\r
914 return baseDn.Substring (0, index);
\r
917 private static string GetParentDN (string baseDn)
\r
919 if (baseDn == null || baseDn.Length == 0 || baseDn.IndexOf (',') == -1)
\r
920 return String.Empty;
\r
922 int index = baseDn.IndexOf (',');
\r
924 return baseDn.Substring (index + 1,baseDn.Length - index - 1);
\r