[sgen] Fix function signature
[mono.git] / mcs / class / System.DirectoryServices / Test / System.DirectoryServices / DirectoryServicesDirectorySearcherTest.cs
1 //\r
2 // DirectoryServicesDirectorySearcherTest.cs -\r
3 //      NUnit Test Cases for DirectoryServices.DirectorySearcher\r
4 //\r
5 // Author:\r
6 //      Boris Kirzner  <borisk@mainsoft.com>\r
7 //\r
8 using NUnit.Framework;\r
9 using System;\r
10 using System.DirectoryServices;\r
11 using System.Collections;\r
12 \r
13 using System.Threading;\r
14 \r
15 namespace MonoTests.System.DirectoryServices \r
16 {\r
17         [TestFixture]\r
18         [Category ("InetAccess")]\r
19         public class DirectoryServicesDirectorySearcherTest\r
20         {\r
21                 #region Fields\r
22 \r
23                 static TestConfiguration configuration;\r
24                 static DirectoryEntry de;\r
25                 static DirectorySearcher ds;\r
26 \r
27                 #endregion // Fields\r
28 \r
29                 #region SetUp and TearDown\r
30 \r
31                 [TestFixtureSetUp]\r
32                 public void TestFixtureSetUp()\r
33                 {\r
34                         de = null;                      \r
35                         configuration = new TestConfiguration ();\r
36                 }\r
37 \r
38 \r
39                 [TestFixtureTearDown]\r
40                 public void TestFixtureTearDown()\r
41                 {\r
42                         if (ds != null)\r
43                                 ds.Dispose ();\r
44                         \r
45                         ds = null;\r
46                         \r
47                         if (de != null)\r
48                                 de.Dispose ();\r
49 \r
50                         de = null;\r
51                 }\r
52 \r
53 \r
54                 [SetUp]\r
55                 public void SetUp()\r
56                 {\r
57                         TearDown();\r
58 \r
59                         #region Initialize basics\r
60 \r
61                         DirectoryEntry root = new DirectoryEntry(       configuration.ConnectionString,\r
62                                                                                                                 configuration.Username,\r
63                                                                                                                 configuration.Password,\r
64                                                                                                                 configuration.AuthenticationType);\r
65 \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
71 \r
72                         #endregion // Initialize basics\r
73 \r
74                         #region Human Resources\r
75  \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
80 \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
88 \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
99 \r
100                         #endregion // Human Resources\r
101 \r
102                         #region R&D\r
103 \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
108 \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
119 \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
130 \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
141 \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
152 \r
153                         #endregion // R&D\r
154 \r
155                         #region DevQA\r
156 \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
161 \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
173 \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
184 \r
185                         #endregion // DevQA\r
186 \r
187                         #region Manager\r
188 \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
194 \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
205 \r
206                         #endregion // Manager\r
207 \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
215                         ouRnD.Dispose();\r
216                         cnDanielSmith.Dispose();\r
217                         cnDanielMorgan.Dispose();\r
218                         ouDevQA.Dispose();\r
219                         cnUziCohen_.Dispose();\r
220                         cnManager.Dispose();\r
221                         ouPeople.Dispose();\r
222                         root.Dispose();\r
223                 }\r
224 \r
225 \r
226                 [TearDown]\r
227                 public void TearDown()\r
228                 {\r
229                         if (ds != null)\r
230                                 ds.Dispose ();\r
231                         \r
232                         ds = null;\r
233                         \r
234                         if (de != null)\r
235                                 de.Dispose ();\r
236 \r
237                         de = null;\r
238 \r
239                         using (DirectoryEntry root = new DirectoryEntry(        configuration.ConnectionString,\r
240                                                                                                         configuration.Username,\r
241                                                                                                         configuration.Password,\r
242                                                                                                         configuration.AuthenticationType)){\r
243                         \r
244                         foreach(DirectoryEntry child in root.Children) {\r
245                                 DeleteTree_DFS(child);\r
246                         }\r
247                         }\r
248                 }\r
249 \r
250 \r
251                 private void DeleteTree_DFS(DirectoryEntry de)\r
252                 {\r
253                         foreach(DirectoryEntry child in de.Children) {\r
254                                 DeleteTree_DFS(child);\r
255                         }\r
256                         de.DeleteTree();\r
257                         de.CommitChanges();\r
258                 }\r
259 \r
260                 #endregion //SetUp and TearDown\r
261 \r
262                 #region Tests\r
263 \r
264                 [Test]\r
265                 public void DirectorySearcher_DirectorySearcher()\r
266                 {\r
267                         ds = new DirectorySearcher();\r
268 \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
274                 }\r
275 \r
276                 [Test]\r
277                 public void DirectorySearcher_DirectorySearcher_De()\r
278                 {\r
279                         de = new DirectoryEntry(configuration.ConnectionString,\r
280                                                                         configuration.Username,\r
281                                                                         configuration.Password,\r
282                                                                         configuration.AuthenticationType);\r
283 \r
284                         ds = new DirectorySearcher(de);\r
285 \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
292                 }\r
293 \r
294                 [Test]\r
295                 public void DirectorySearcher_DirectorySearcher_Str()\r
296                 {\r
297                         ds = new DirectorySearcher("(objectClass=organizationalRole)");\r
298 \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
304                 }\r
305 \r
306                 [Test]\r
307                 public void DirectorySearcher_DirectorySearcher_DeStr()\r
308                 {\r
309                         de = new DirectoryEntry(configuration.ConnectionString,\r
310                                                                         configuration.Username,\r
311                                                                         configuration.Password,\r
312                                                                         configuration.AuthenticationType);\r
313 \r
314                         ds = new DirectorySearcher(de,"(objectClass=organizationalRole)");\r
315 \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
322                 }\r
323 \r
324                 [Test]\r
325                 public void DirectorySearcher_DirectorySearcher_StrStrArr()\r
326                 {\r
327                         string[] properties = new string[] {"objectClass","ou","cn"};\r
328                         ds = new DirectorySearcher("(objectClass=organizationalRole)",properties);\r
329 \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
334                         }\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
338                 }\r
339 \r
340                 [Test]\r
341                 public void DirectorySearcher_DirectorySearcher_DeStrStrArr()\r
342                 {\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
348 \r
349                         ds = new DirectorySearcher(de,"(objectClass=organizationalRole)",properties);\r
350 \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
356                         }\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
360                 }\r
361 \r
362                 [Test]\r
363                 public void DirectorySearcher_DirectorySearcher_StrStrArrScp()\r
364                 {\r
365                         string[] properties = new string[] {"objectClass","ou","cn"};\r
366                         ds = new DirectorySearcher("(objectClass=organizationalRole)",properties,SearchScope.Base);\r
367 \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
372                         }\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
376                 }\r
377 \r
378                 [Test]\r
379                 public void DirectorySearcher_DirectorySearcher_DeStrStrArrScp()\r
380                 {\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
386 \r
387                         ds = new DirectorySearcher(de,"(objectClass=organizationalRole)",properties,SearchScope.Base);\r
388 \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
394                         }\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
398                 }\r
399 \r
400                 [Test]\r
401                 public void DirectorySearcher_CacheResults()\r
402                 {\r
403                         de = new DirectoryEntry(configuration.ConnectionString,\r
404                                                                         configuration.Username,\r
405                                                                         configuration.Password,\r
406                                                                         configuration.AuthenticationType);\r
407 \r
408                         ds = new DirectorySearcher(de,"(cn=Barak Tsabari)");\r
409                         ds.CacheResults = true;\r
410 \r
411                         string oldValue;\r
412                         string newValue = "New Description";\r
413 \r
414                         SearchResult result = ds.FindOne();\r
415                         SearchResult secondResult;\r
416                         using (DirectoryEntry resultDE = result.GetDirectoryEntry()){\r
417 \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
421 \r
422                         using (DirectorySearcher secondDs = new DirectorySearcher(de,"(cn=Barak Tsabari)")){\r
423                         secondResult = secondDs.FindOne();\r
424                         using (DirectoryEntry secondResultDE = secondResult.GetDirectoryEntry()){\r
425 \r
426                         Assert.AreEqual(((PropertyValueCollection)secondResultDE.Properties["description"]).Value,oldValue);\r
427 \r
428                         ((PropertyValueCollection)resultDE.Properties["description"]).Value = oldValue;\r
429                         }\r
430                         }\r
431                         }\r
432                         \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
437 \r
438                         ((PropertyValueCollection)resultDE.Properties["description"]).Value = newValue;\r
439                         Assert.AreEqual(((PropertyValueCollection)resultDE.Properties["description"]).Value,newValue);\r
440 \r
441                         using (DirectorySearcher secondDs = new DirectorySearcher(de,"(cn=Barak Tsabari)")){\r
442                         secondResult = secondDs.FindOne();\r
443                         using (DirectoryEntry secondResultDE = secondResult.GetDirectoryEntry()){\r
444 \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
448 \r
449                         ((PropertyValueCollection)resultDE.Properties["description"]).Value = oldValue; \r
450                         }\r
451                         }\r
452                         }\r
453                 }\r
454 \r
455         \r
456                 [Test]\r
457                 public void DirectorySearcher_ClientTimeout()\r
458                 {\r
459                         ds = new DirectorySearcher();\r
460 \r
461                         Assert.AreEqual(ds.ClientTimeout,new TimeSpan(-10000000));\r
462 \r
463                         ds.ClientTimeout = new TimeSpan(500000000);\r
464                         Assert.AreEqual(ds.ClientTimeout,new TimeSpan(500000000));\r
465 \r
466                         ds.ClientTimeout = TimeSpan.MaxValue;\r
467                         Assert.AreEqual(ds.ClientTimeout,TimeSpan.MaxValue);\r
468 \r
469                         ds.ClientTimeout = TimeSpan.MinValue;\r
470                         Assert.AreEqual(ds.ClientTimeout,TimeSpan.MinValue);\r
471                 }\r
472 \r
473                 [Test]\r
474                 public void DirectorySearcher_Filter1()\r
475                 {\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
481                         \r
482                         ds.Filter = "(objectClass=person)";\r
483                         SearchResultCollection results = ds.FindAll();\r
484                         Assert.AreEqual(results.Count,8);\r
485 \r
486                         ds.Filter = "(|(objectClass=person)(objectClass=organizationalUnit))";\r
487                         results = ds.FindAll();\r
488                         Assert.AreEqual(results.Count,12);\r
489 \r
490                         ds.Filter = "(&(objectClass=person)(objectClass=organizationalUnit))";\r
491                         results = ds.FindAll();\r
492                         Assert.AreEqual(results.Count,0);\r
493                 }\r
494 \r
495                 [Test]\r
496                 [Category("NotWorking")]\r
497                 public void DirectorySearcher_Filter2()\r
498                 {\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
504                         \r
505                         ds.Filter = "((objectClass=person))";\r
506                         SearchResultCollection results = ds.FindAll();\r
507                         Assert.AreEqual(results.Count,8);\r
508 \r
509                         ds.Filter = "(|(objectClass=person)((objectClass=organizationalUnit)))";\r
510                         results = ds.FindAll();\r
511                         Assert.AreEqual(results.Count,12);\r
512 \r
513                         ds.Filter = "(&((objectClass=person))(objectClass=organizationalUnit))";\r
514                         results = ds.FindAll();\r
515                         Assert.AreEqual(results.Count,0);\r
516                 }\r
517 \r
518 \r
519                 [Test]\r
520                 public void DirectorySearcher_PageSize()\r
521                 {\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
527 \r
528                         Assert.AreEqual(ds.PageSize,0);\r
529                         \r
530                         ds.Filter = "(|(objectClass=person)(objectClass=organizationalUnit))";\r
531                         SearchResultCollection results = ds.FindAll();\r
532                         Assert.AreEqual(results.Count,12);\r
533 \r
534                         ds.PageSize = 3;\r
535                         Assert.AreEqual(ds.PageSize,3);\r
536 \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
541 \r
542                 }\r
543 \r
544                 [Test]\r
545                 public void DirectorySearcher_PropertiesToLoad()\r
546                 {\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
552 \r
553                         Assert.AreEqual(ds.PropertiesToLoad.Count,0);\r
554 \r
555                         ds.PropertiesToLoad.Add("cn");\r
556                         ds.PropertiesToLoad.Add("ADsPath");\r
557                         ds.PropertiesToLoad.Add("objectClass");\r
558 \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
563 \r
564                         ds.Filter = "(objectClass=person)";\r
565                         SearchResult result = ds.FindOne();\r
566 \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
571 \r
572                 \r
573                         ds.PropertiesToLoad.Clear();\r
574                         Assert.AreEqual(ds.PropertiesToLoad.Count,0);\r
575 \r
576                         ds.PropertiesToLoad.Add("cn");\r
577                         ds.PropertiesToLoad.Add("objectClass");\r
578 \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
582 \r
583                         ds.Filter = "(objectClass=person)";\r
584                         result = ds.FindOne();\r
585 \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
590 \r
591                                                 \r
592                         ds.PropertiesToLoad.Clear();\r
593                         Assert.AreEqual(ds.PropertiesToLoad.Count,0);\r
594 \r
595                         ds.PropertiesToLoad.Add("cn");\r
596                         ds.PropertiesToLoad.Add("dn");\r
597                         ds.PropertiesToLoad.Add("objectClass");\r
598 \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
603 \r
604                         ds.Filter = "(objectClass=person)";\r
605                         result = ds.FindOne();\r
606 \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
613                 }\r
614 \r
615                 [Test]\r
616                 public void DirectorySearcher_PropertyNamesOnly()\r
617                 {\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
623 \r
624                         Assert.AreEqual(ds.PropertyNamesOnly,false);\r
625 \r
626                         // All rpoperties are loaded without values, except "ADsPath"\r
627                         ds.PropertyNamesOnly = true;\r
628 \r
629                         ds.Filter = "(objectClass=person)";\r
630                         SearchResult result = ds.FindOne();\r
631 \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
635                                 }\r
636                                 else {\r
637                                         Assert.AreEqual(((ResultPropertyValueCollection)en.Value).Count,1);\r
638                                 }                               \r
639                         }\r
640 \r
641 \r
642                         // all properties are loaded including values\r
643                         ds.PropertyNamesOnly = false;\r
644 \r
645                         ds.Filter = "(objectClass=person)";\r
646                         result = ds.FindOne();\r
647 \r
648                         foreach(DictionaryEntry en in result.Properties) {\r
649                                 Assert.IsTrue(((ResultPropertyValueCollection)en.Value).Count > 0);\r
650                         }\r
651                 }\r
652 \r
653                 [Test]\r
654                 public void DirectorySearcher_ReferralChasing()\r
655                 {\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
661 \r
662                         Assert.AreEqual(ds.ReferralChasing,ReferralChasingOption.External);\r
663 \r
664                         ds.ReferralChasing = ReferralChasingOption.All;\r
665                         Assert.AreEqual(ds.ReferralChasing,ReferralChasingOption.All);\r
666 \r
667                         ds.ReferralChasing = ReferralChasingOption.External;\r
668                         Assert.AreEqual(ds.ReferralChasing,ReferralChasingOption.External);\r
669 \r
670                         ds.ReferralChasing = ReferralChasingOption.None;\r
671                         Assert.AreEqual(ds.ReferralChasing,ReferralChasingOption.None);\r
672 \r
673                         ds.ReferralChasing = ReferralChasingOption.Subordinate;\r
674                         Assert.AreEqual(ds.ReferralChasing,ReferralChasingOption.Subordinate);\r
675 \r
676                         // FIXME : currently we do not have an infrastucture for good testing of this feature\r
677                 }\r
678 \r
679                 [Test]\r
680                 public void DirectorySearcher_SearchRoot()\r
681                 {\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
688 \r
689                         Assert.AreEqual(ds.SearchRoot.Name,GetName (configuration.BaseDn));\r
690 \r
691                         ds.Filter = "(objectClass=person)";\r
692                         SearchResultCollection results = ds.FindAll();\r
693                         Assert.AreEqual(results.Count,8);\r
694 \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
701 \r
702                         results = ds.FindAll();\r
703                         Assert.AreEqual(results.Count,7);\r
704 \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
711 \r
712                         results = ds.FindAll();\r
713                         Assert.AreEqual(results.Count,1);\r
714                 }\r
715 \r
716                 [Test]\r
717                 public void DirectorySearcher_SearchScope()\r
718                 {\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
724                         \r
725                         Assert.AreEqual(ds.SearchScope,SearchScope.Subtree);\r
726 \r
727                         ds.SearchScope = SearchScope.Base;\r
728                         Assert.AreEqual(ds.SearchScope,SearchScope.Base);\r
729 \r
730                         ds.Filter = "(objectClass=organizationalUnit)";\r
731                         SearchResultCollection results = ds.FindAll();\r
732                         Assert.AreEqual(results.Count,0);\r
733 \r
734                         ds.SearchScope = SearchScope.OneLevel;\r
735                         Assert.AreEqual(ds.SearchScope,SearchScope.OneLevel);\r
736 \r
737                         results = ds.FindAll();\r
738                         Assert.AreEqual(results.Count,1);\r
739 \r
740                         ds.SearchScope = SearchScope.Subtree;\r
741                         Assert.AreEqual(ds.SearchScope,SearchScope.Subtree);\r
742 \r
743                         results = ds.FindAll();\r
744                         Assert.AreEqual(results.Count,4);\r
745                 }\r
746 \r
747                 [Test]\r
748                 public void DirectorySearcher_ServerPageTimeLimit()\r
749                 {\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
755 \r
756                         Assert.AreEqual(ds.ServerPageTimeLimit,new TimeSpan(-10000000));\r
757 \r
758                         // According to spec PageSize should be set to a value that is not the default of -1\r
759                         ds.PageSize = 5;\r
760                         ds.ServerPageTimeLimit = new TimeSpan(500000000);\r
761                         Assert.AreEqual(ds.ServerPageTimeLimit,new TimeSpan(500000000));\r
762 \r
763                         ds.ServerPageTimeLimit = TimeSpan.MaxValue;\r
764                         Assert.AreEqual(ds.ServerPageTimeLimit,TimeSpan.MaxValue);\r
765 \r
766                         ds.ServerPageTimeLimit = TimeSpan.MinValue;\r
767                         Assert.AreEqual(ds.ServerPageTimeLimit,TimeSpan.MinValue);\r
768 \r
769                         // FIXME : currently we do not have an infrastucture for good testing of this feature\r
770                 }\r
771 \r
772                 [Test]\r
773                 public void DirectorySearcher_ServerTimeLimit()\r
774                 {\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
780 \r
781                         Assert.AreEqual(ds.ServerTimeLimit,new TimeSpan(-10000000));\r
782 \r
783                         // According to spec PageSize should be set to a value that is not the default of -1\r
784                         ds.PageSize = 5;\r
785                         ds.ServerTimeLimit = new TimeSpan(500000000);\r
786                         Assert.AreEqual(ds.ServerTimeLimit,new TimeSpan(500000000));\r
787 \r
788                         ds.ServerTimeLimit = TimeSpan.MaxValue;\r
789                         Assert.AreEqual(ds.ServerTimeLimit,TimeSpan.MaxValue);\r
790 \r
791                         ds.ServerTimeLimit = TimeSpan.MinValue;\r
792                         Assert.AreEqual(ds.ServerTimeLimit,TimeSpan.MinValue);\r
793 \r
794                         // FIXME : currently we do not have an infrastucture for good testing of this feature\r
795                 }\r
796 \r
797                 [Test]\r
798                 public void DirectorySearcher_SizeLimit()\r
799                 {\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
805 \r
806                         Assert.AreEqual(ds.SizeLimit,0);\r
807                         \r
808                         ds.Filter = "(|(objectClass=person)(objectClass=organizationalUnit))";\r
809                         SearchResultCollection results = ds.FindAll();\r
810                         Assert.AreEqual(results.Count,12);\r
811 \r
812                         ds.SizeLimit = 3;\r
813                         Assert.AreEqual(ds.SizeLimit,3);\r
814 \r
815                         ds.Filter = "(|(objectClass=person)(objectClass=organizationalUnit))";\r
816                         results = ds.FindAll();\r
817                         Assert.AreEqual(results.Count,3);\r
818 \r
819                         ds.SizeLimit = Int32.MaxValue;\r
820                         Assert.AreEqual(ds.SizeLimit,Int32.MaxValue);\r
821 \r
822                         ds.Filter = "(|(objectClass=person)(objectClass=organizationalUnit))";\r
823                         results = ds.FindAll();\r
824                         Assert.AreEqual(results.Count,12);\r
825 \r
826                 }\r
827 \r
828                 [Test]\r
829                 [ExpectedException(typeof(ArgumentException))]\r
830                 public void DirectorySearcher_SizeLimit_Neg()\r
831                 {\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
837                         ds.SizeLimit = -1;\r
838                         Assert.AreEqual(ds.SizeLimit,-1);\r
839 \r
840                         SearchResultCollection results = ds.FindAll();\r
841                 }\r
842 \r
843                 [Test]\r
844                 public void DirectorySearcher_Sort()\r
845                 {\r
846                         // FIXME : howto create good sorting\r
847                 }\r
848 \r
849                 [Test]\r
850                 public void DirectorySearcher_FindAll()\r
851                 {\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
857 \r
858                         ds.Filter = "(objectClass=person)";\r
859                         SearchResultCollection results = ds.FindAll();\r
860                         Assert.AreEqual(results.Count,8);\r
861 \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
867 \r
868                         results = ds.FindAll();\r
869                         Assert.AreEqual(results.Count,3);\r
870                 }\r
871 \r
872                 [Test]\r
873                 public void DirectorySearcher_FindOne()\r
874                 {\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
880 \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
885                         \r
886 \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
892 \r
893                         result = ds.FindOne();\r
894                         Assert.IsNotNull(result);\r
895                         Assert.AreEqual(result.GetDirectoryEntry().Name,"ou=Human Resources");\r
896 \r
897                         ds.Filter = "(objectClass=Barak Tsabari)";\r
898                         result = ds.FindOne();\r
899                         Assert.IsNull(result);\r
900                 }\r
901 \r
902 \r
903                 #endregion Tests\r
904 \r
905                 private static string GetName (string baseDn)\r
906                 {\r
907                         if (baseDn == null || baseDn.Length == 0 || baseDn.IndexOf (',') == -1)\r
908                                 return baseDn;\r
909 \r
910                         int index = baseDn.IndexOf (',');\r
911 \r
912                         return baseDn.Substring (0, index);\r
913                 }\r
914 \r
915                 private static string GetParentDN (string baseDn)\r
916                 {\r
917                         if (baseDn == null || baseDn.Length == 0 || baseDn.IndexOf (',') == -1)\r
918                                 return String.Empty;\r
919 \r
920                         int index = baseDn.IndexOf (',');\r
921 \r
922                         return baseDn.Substring (index + 1,baseDn.Length - index - 1);\r
923                 }\r
924         }\r
925 }\r
926 \r