Merge pull request #5198 from BrzVlad/fix-binprot-stats
[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         public class DirectoryServicesDirectorySearcherTest\r
19         {\r
20                 #region Fields\r
21 \r
22                 static TestConfiguration configuration;\r
23                 static DirectoryEntry de;\r
24                 static DirectorySearcher ds;\r
25 \r
26                 #endregion // Fields\r
27 \r
28                 #region SetUp and TearDown\r
29 \r
30                 [TestFixtureSetUp]\r
31                 public void TestFixtureSetUp()\r
32                 {\r
33                         de = null;                      \r
34                         configuration = new TestConfiguration ();\r
35 \r
36                         if (String.IsNullOrEmpty (configuration.ConnectionString))\r
37                                 Assert.Ignore ("No configuration");\r
38                 }\r
39 \r
40 \r
41                 [TestFixtureTearDown]\r
42                 public void TestFixtureTearDown()\r
43                 {\r
44                         if (ds != null)\r
45                                 ds.Dispose ();\r
46                         \r
47                         ds = null;\r
48                         \r
49                         if (de != null)\r
50                                 de.Dispose ();\r
51 \r
52                         de = null;\r
53                 }\r
54 \r
55 \r
56                 [SetUp]\r
57                 public void SetUp()\r
58                 {\r
59                         TearDown();\r
60 \r
61                         #region Initialize basics\r
62 \r
63                         DirectoryEntry root = new DirectoryEntry(       configuration.ConnectionString,\r
64                                                                                                                 configuration.Username,\r
65                                                                                                                 configuration.Password,\r
66                                                                                                                 configuration.AuthenticationType);\r
67 \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
73 \r
74                         #endregion // Initialize basics\r
75 \r
76                         #region Human Resources\r
77  \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
82 \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
90 \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
101 \r
102                         #endregion // Human Resources\r
103 \r
104                         #region R&D\r
105 \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
110 \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
121 \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
132 \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
143 \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
154 \r
155                         #endregion // R&D\r
156 \r
157                         #region DevQA\r
158 \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
163 \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
175 \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
186 \r
187                         #endregion // DevQA\r
188 \r
189                         #region Manager\r
190 \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
196 \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
207 \r
208                         #endregion // Manager\r
209 \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
217                         ouRnD.Dispose();\r
218                         cnDanielSmith.Dispose();\r
219                         cnDanielMorgan.Dispose();\r
220                         ouDevQA.Dispose();\r
221                         cnUziCohen_.Dispose();\r
222                         cnManager.Dispose();\r
223                         ouPeople.Dispose();\r
224                         root.Dispose();\r
225                 }\r
226 \r
227 \r
228                 [TearDown]\r
229                 public void TearDown()\r
230                 {\r
231                         if (ds != null)\r
232                                 ds.Dispose ();\r
233                         \r
234                         ds = null;\r
235                         \r
236                         if (de != null)\r
237                                 de.Dispose ();\r
238 \r
239                         de = null;\r
240 \r
241                         using (DirectoryEntry root = new DirectoryEntry(        configuration.ConnectionString,\r
242                                                                                                         configuration.Username,\r
243                                                                                                         configuration.Password,\r
244                                                                                                         configuration.AuthenticationType)){\r
245                         \r
246                         foreach(DirectoryEntry child in root.Children) {\r
247                                 DeleteTree_DFS(child);\r
248                         }\r
249                         }\r
250                 }\r
251 \r
252 \r
253                 private void DeleteTree_DFS(DirectoryEntry de)\r
254                 {\r
255                         foreach(DirectoryEntry child in de.Children) {\r
256                                 DeleteTree_DFS(child);\r
257                         }\r
258                         de.DeleteTree();\r
259                         de.CommitChanges();\r
260                 }\r
261 \r
262                 #endregion //SetUp and TearDown\r
263 \r
264                 #region Tests\r
265 \r
266                 [Test]\r
267                 public void DirectorySearcher_DirectorySearcher()\r
268                 {\r
269                         ds = new DirectorySearcher();\r
270 \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
276                 }\r
277 \r
278                 [Test]\r
279                 public void DirectorySearcher_DirectorySearcher_De()\r
280                 {\r
281                         de = new DirectoryEntry(configuration.ConnectionString,\r
282                                                                         configuration.Username,\r
283                                                                         configuration.Password,\r
284                                                                         configuration.AuthenticationType);\r
285 \r
286                         ds = new DirectorySearcher(de);\r
287 \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
294                 }\r
295 \r
296                 [Test]\r
297                 public void DirectorySearcher_DirectorySearcher_Str()\r
298                 {\r
299                         ds = new DirectorySearcher("(objectClass=organizationalRole)");\r
300 \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
306                 }\r
307 \r
308                 [Test]\r
309                 public void DirectorySearcher_DirectorySearcher_DeStr()\r
310                 {\r
311                         de = new DirectoryEntry(configuration.ConnectionString,\r
312                                                                         configuration.Username,\r
313                                                                         configuration.Password,\r
314                                                                         configuration.AuthenticationType);\r
315 \r
316                         ds = new DirectorySearcher(de,"(objectClass=organizationalRole)");\r
317 \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
324                 }\r
325 \r
326                 [Test]\r
327                 public void DirectorySearcher_DirectorySearcher_StrStrArr()\r
328                 {\r
329                         string[] properties = new string[] {"objectClass","ou","cn"};\r
330                         ds = new DirectorySearcher("(objectClass=organizationalRole)",properties);\r
331 \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
336                         }\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
340                 }\r
341 \r
342                 [Test]\r
343                 public void DirectorySearcher_DirectorySearcher_DeStrStrArr()\r
344                 {\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
350 \r
351                         ds = new DirectorySearcher(de,"(objectClass=organizationalRole)",properties);\r
352 \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
358                         }\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
362                 }\r
363 \r
364                 [Test]\r
365                 public void DirectorySearcher_DirectorySearcher_StrStrArrScp()\r
366                 {\r
367                         string[] properties = new string[] {"objectClass","ou","cn"};\r
368                         ds = new DirectorySearcher("(objectClass=organizationalRole)",properties,SearchScope.Base);\r
369 \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
374                         }\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
378                 }\r
379 \r
380                 [Test]\r
381                 public void DirectorySearcher_DirectorySearcher_DeStrStrArrScp()\r
382                 {\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
388 \r
389                         ds = new DirectorySearcher(de,"(objectClass=organizationalRole)",properties,SearchScope.Base);\r
390 \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
396                         }\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
400                 }\r
401 \r
402                 [Test]\r
403                 public void DirectorySearcher_CacheResults()\r
404                 {\r
405                         de = new DirectoryEntry(configuration.ConnectionString,\r
406                                                                         configuration.Username,\r
407                                                                         configuration.Password,\r
408                                                                         configuration.AuthenticationType);\r
409 \r
410                         ds = new DirectorySearcher(de,"(cn=Barak Tsabari)");\r
411                         ds.CacheResults = true;\r
412 \r
413                         string oldValue;\r
414                         string newValue = "New Description";\r
415 \r
416                         SearchResult result = ds.FindOne();\r
417                         SearchResult secondResult;\r
418                         using (DirectoryEntry resultDE = result.GetDirectoryEntry()){\r
419 \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
423 \r
424                         using (DirectorySearcher secondDs = new DirectorySearcher(de,"(cn=Barak Tsabari)")){\r
425                         secondResult = secondDs.FindOne();\r
426                         using (DirectoryEntry secondResultDE = secondResult.GetDirectoryEntry()){\r
427 \r
428                         Assert.AreEqual(((PropertyValueCollection)secondResultDE.Properties["description"]).Value,oldValue);\r
429 \r
430                         ((PropertyValueCollection)resultDE.Properties["description"]).Value = oldValue;\r
431                         }\r
432                         }\r
433                         }\r
434                         \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
439 \r
440                         ((PropertyValueCollection)resultDE.Properties["description"]).Value = newValue;\r
441                         Assert.AreEqual(((PropertyValueCollection)resultDE.Properties["description"]).Value,newValue);\r
442 \r
443                         using (DirectorySearcher secondDs = new DirectorySearcher(de,"(cn=Barak Tsabari)")){\r
444                         secondResult = secondDs.FindOne();\r
445                         using (DirectoryEntry secondResultDE = secondResult.GetDirectoryEntry()){\r
446 \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
450 \r
451                         ((PropertyValueCollection)resultDE.Properties["description"]).Value = oldValue; \r
452                         }\r
453                         }\r
454                         }\r
455                 }\r
456 \r
457         \r
458                 [Test]\r
459                 public void DirectorySearcher_ClientTimeout()\r
460                 {\r
461                         ds = new DirectorySearcher();\r
462 \r
463                         Assert.AreEqual(ds.ClientTimeout,new TimeSpan(-10000000));\r
464 \r
465                         ds.ClientTimeout = new TimeSpan(500000000);\r
466                         Assert.AreEqual(ds.ClientTimeout,new TimeSpan(500000000));\r
467 \r
468                         ds.ClientTimeout = TimeSpan.MaxValue;\r
469                         Assert.AreEqual(ds.ClientTimeout,TimeSpan.MaxValue);\r
470 \r
471                         ds.ClientTimeout = TimeSpan.MinValue;\r
472                         Assert.AreEqual(ds.ClientTimeout,TimeSpan.MinValue);\r
473                 }\r
474 \r
475                 [Test]\r
476                 public void DirectorySearcher_Filter1()\r
477                 {\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
483                         \r
484                         ds.Filter = "(objectClass=person)";\r
485                         SearchResultCollection results = ds.FindAll();\r
486                         Assert.AreEqual(results.Count,8);\r
487 \r
488                         ds.Filter = "(|(objectClass=person)(objectClass=organizationalUnit))";\r
489                         results = ds.FindAll();\r
490                         Assert.AreEqual(results.Count,12);\r
491 \r
492                         ds.Filter = "(&(objectClass=person)(objectClass=organizationalUnit))";\r
493                         results = ds.FindAll();\r
494                         Assert.AreEqual(results.Count,0);\r
495                 }\r
496 \r
497                 [Test]\r
498                 [Category("NotWorking")]\r
499                 public void DirectorySearcher_Filter2()\r
500                 {\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
506                         \r
507                         ds.Filter = "((objectClass=person))";\r
508                         SearchResultCollection results = ds.FindAll();\r
509                         Assert.AreEqual(results.Count,8);\r
510 \r
511                         ds.Filter = "(|(objectClass=person)((objectClass=organizationalUnit)))";\r
512                         results = ds.FindAll();\r
513                         Assert.AreEqual(results.Count,12);\r
514 \r
515                         ds.Filter = "(&((objectClass=person))(objectClass=organizationalUnit))";\r
516                         results = ds.FindAll();\r
517                         Assert.AreEqual(results.Count,0);\r
518                 }\r
519 \r
520 \r
521                 [Test]\r
522                 public void DirectorySearcher_PageSize()\r
523                 {\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
529 \r
530                         Assert.AreEqual(ds.PageSize,0);\r
531                         \r
532                         ds.Filter = "(|(objectClass=person)(objectClass=organizationalUnit))";\r
533                         SearchResultCollection results = ds.FindAll();\r
534                         Assert.AreEqual(results.Count,12);\r
535 \r
536                         ds.PageSize = 3;\r
537                         Assert.AreEqual(ds.PageSize,3);\r
538 \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
543 \r
544                 }\r
545 \r
546                 [Test]\r
547                 public void DirectorySearcher_PropertiesToLoad()\r
548                 {\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
554 \r
555                         Assert.AreEqual(ds.PropertiesToLoad.Count,0);\r
556 \r
557                         ds.PropertiesToLoad.Add("cn");\r
558                         ds.PropertiesToLoad.Add("ADsPath");\r
559                         ds.PropertiesToLoad.Add("objectClass");\r
560 \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
565 \r
566                         ds.Filter = "(objectClass=person)";\r
567                         SearchResult result = ds.FindOne();\r
568 \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
573 \r
574                 \r
575                         ds.PropertiesToLoad.Clear();\r
576                         Assert.AreEqual(ds.PropertiesToLoad.Count,0);\r
577 \r
578                         ds.PropertiesToLoad.Add("cn");\r
579                         ds.PropertiesToLoad.Add("objectClass");\r
580 \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
584 \r
585                         ds.Filter = "(objectClass=person)";\r
586                         result = ds.FindOne();\r
587 \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
592 \r
593                                                 \r
594                         ds.PropertiesToLoad.Clear();\r
595                         Assert.AreEqual(ds.PropertiesToLoad.Count,0);\r
596 \r
597                         ds.PropertiesToLoad.Add("cn");\r
598                         ds.PropertiesToLoad.Add("dn");\r
599                         ds.PropertiesToLoad.Add("objectClass");\r
600 \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
605 \r
606                         ds.Filter = "(objectClass=person)";\r
607                         result = ds.FindOne();\r
608 \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
615                 }\r
616 \r
617                 [Test]\r
618                 public void DirectorySearcher_PropertyNamesOnly()\r
619                 {\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
625 \r
626                         Assert.AreEqual(ds.PropertyNamesOnly,false);\r
627 \r
628                         // All rpoperties are loaded without values, except "ADsPath"\r
629                         ds.PropertyNamesOnly = true;\r
630 \r
631                         ds.Filter = "(objectClass=person)";\r
632                         SearchResult result = ds.FindOne();\r
633 \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
637                                 }\r
638                                 else {\r
639                                         Assert.AreEqual(((ResultPropertyValueCollection)en.Value).Count,1);\r
640                                 }                               \r
641                         }\r
642 \r
643 \r
644                         // all properties are loaded including values\r
645                         ds.PropertyNamesOnly = false;\r
646 \r
647                         ds.Filter = "(objectClass=person)";\r
648                         result = ds.FindOne();\r
649 \r
650                         foreach(DictionaryEntry en in result.Properties) {\r
651                                 Assert.IsTrue(((ResultPropertyValueCollection)en.Value).Count > 0);\r
652                         }\r
653                 }\r
654 \r
655                 [Test]\r
656                 public void DirectorySearcher_ReferralChasing()\r
657                 {\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
663 \r
664                         Assert.AreEqual(ds.ReferralChasing,ReferralChasingOption.External);\r
665 \r
666                         ds.ReferralChasing = ReferralChasingOption.All;\r
667                         Assert.AreEqual(ds.ReferralChasing,ReferralChasingOption.All);\r
668 \r
669                         ds.ReferralChasing = ReferralChasingOption.External;\r
670                         Assert.AreEqual(ds.ReferralChasing,ReferralChasingOption.External);\r
671 \r
672                         ds.ReferralChasing = ReferralChasingOption.None;\r
673                         Assert.AreEqual(ds.ReferralChasing,ReferralChasingOption.None);\r
674 \r
675                         ds.ReferralChasing = ReferralChasingOption.Subordinate;\r
676                         Assert.AreEqual(ds.ReferralChasing,ReferralChasingOption.Subordinate);\r
677 \r
678                         // FIXME : currently we do not have an infrastucture for good testing of this feature\r
679                 }\r
680 \r
681                 [Test]\r
682                 public void DirectorySearcher_SearchRoot()\r
683                 {\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
690 \r
691                         Assert.AreEqual(ds.SearchRoot.Name,GetName (configuration.BaseDn));\r
692 \r
693                         ds.Filter = "(objectClass=person)";\r
694                         SearchResultCollection results = ds.FindAll();\r
695                         Assert.AreEqual(results.Count,8);\r
696 \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
703 \r
704                         results = ds.FindAll();\r
705                         Assert.AreEqual(results.Count,7);\r
706 \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
713 \r
714                         results = ds.FindAll();\r
715                         Assert.AreEqual(results.Count,1);\r
716                 }\r
717 \r
718                 [Test]\r
719                 public void DirectorySearcher_SearchScope()\r
720                 {\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
726                         \r
727                         Assert.AreEqual(ds.SearchScope,SearchScope.Subtree);\r
728 \r
729                         ds.SearchScope = SearchScope.Base;\r
730                         Assert.AreEqual(ds.SearchScope,SearchScope.Base);\r
731 \r
732                         ds.Filter = "(objectClass=organizationalUnit)";\r
733                         SearchResultCollection results = ds.FindAll();\r
734                         Assert.AreEqual(results.Count,0);\r
735 \r
736                         ds.SearchScope = SearchScope.OneLevel;\r
737                         Assert.AreEqual(ds.SearchScope,SearchScope.OneLevel);\r
738 \r
739                         results = ds.FindAll();\r
740                         Assert.AreEqual(results.Count,1);\r
741 \r
742                         ds.SearchScope = SearchScope.Subtree;\r
743                         Assert.AreEqual(ds.SearchScope,SearchScope.Subtree);\r
744 \r
745                         results = ds.FindAll();\r
746                         Assert.AreEqual(results.Count,4);\r
747                 }\r
748 \r
749                 [Test]\r
750                 public void DirectorySearcher_ServerPageTimeLimit()\r
751                 {\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
757 \r
758                         Assert.AreEqual(ds.ServerPageTimeLimit,new TimeSpan(-10000000));\r
759 \r
760                         // According to spec PageSize should be set to a value that is not the default of -1\r
761                         ds.PageSize = 5;\r
762                         ds.ServerPageTimeLimit = new TimeSpan(500000000);\r
763                         Assert.AreEqual(ds.ServerPageTimeLimit,new TimeSpan(500000000));\r
764 \r
765                         ds.ServerPageTimeLimit = TimeSpan.MaxValue;\r
766                         Assert.AreEqual(ds.ServerPageTimeLimit,TimeSpan.MaxValue);\r
767 \r
768                         ds.ServerPageTimeLimit = TimeSpan.MinValue;\r
769                         Assert.AreEqual(ds.ServerPageTimeLimit,TimeSpan.MinValue);\r
770 \r
771                         // FIXME : currently we do not have an infrastucture for good testing of this feature\r
772                 }\r
773 \r
774                 [Test]\r
775                 public void DirectorySearcher_ServerTimeLimit()\r
776                 {\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
782 \r
783                         Assert.AreEqual(ds.ServerTimeLimit,new TimeSpan(-10000000));\r
784 \r
785                         // According to spec PageSize should be set to a value that is not the default of -1\r
786                         ds.PageSize = 5;\r
787                         ds.ServerTimeLimit = new TimeSpan(500000000);\r
788                         Assert.AreEqual(ds.ServerTimeLimit,new TimeSpan(500000000));\r
789 \r
790                         ds.ServerTimeLimit = TimeSpan.MaxValue;\r
791                         Assert.AreEqual(ds.ServerTimeLimit,TimeSpan.MaxValue);\r
792 \r
793                         ds.ServerTimeLimit = TimeSpan.MinValue;\r
794                         Assert.AreEqual(ds.ServerTimeLimit,TimeSpan.MinValue);\r
795 \r
796                         // FIXME : currently we do not have an infrastucture for good testing of this feature\r
797                 }\r
798 \r
799                 [Test]\r
800                 public void DirectorySearcher_SizeLimit()\r
801                 {\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
807 \r
808                         Assert.AreEqual(ds.SizeLimit,0);\r
809                         \r
810                         ds.Filter = "(|(objectClass=person)(objectClass=organizationalUnit))";\r
811                         SearchResultCollection results = ds.FindAll();\r
812                         Assert.AreEqual(results.Count,12);\r
813 \r
814                         ds.SizeLimit = 3;\r
815                         Assert.AreEqual(ds.SizeLimit,3);\r
816 \r
817                         ds.Filter = "(|(objectClass=person)(objectClass=organizationalUnit))";\r
818                         results = ds.FindAll();\r
819                         Assert.AreEqual(results.Count,3);\r
820 \r
821                         ds.SizeLimit = Int32.MaxValue;\r
822                         Assert.AreEqual(ds.SizeLimit,Int32.MaxValue);\r
823 \r
824                         ds.Filter = "(|(objectClass=person)(objectClass=organizationalUnit))";\r
825                         results = ds.FindAll();\r
826                         Assert.AreEqual(results.Count,12);\r
827 \r
828                 }\r
829 \r
830                 [Test]\r
831                 [ExpectedException(typeof(ArgumentException))]\r
832                 public void DirectorySearcher_SizeLimit_Neg()\r
833                 {\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
839                         ds.SizeLimit = -1;\r
840                         Assert.AreEqual(ds.SizeLimit,-1);\r
841 \r
842                         SearchResultCollection results = ds.FindAll();\r
843                 }\r
844 \r
845                 [Test]\r
846                 public void DirectorySearcher_Sort()\r
847                 {\r
848                         // FIXME : howto create good sorting\r
849                 }\r
850 \r
851                 [Test]\r
852                 public void DirectorySearcher_FindAll()\r
853                 {\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
859 \r
860                         ds.Filter = "(objectClass=person)";\r
861                         SearchResultCollection results = ds.FindAll();\r
862                         Assert.AreEqual(results.Count,8);\r
863 \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
869 \r
870                         results = ds.FindAll();\r
871                         Assert.AreEqual(results.Count,3);\r
872                 }\r
873 \r
874                 [Test]\r
875                 public void DirectorySearcher_FindOne()\r
876                 {\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
882 \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
887                         \r
888 \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
894 \r
895                         result = ds.FindOne();\r
896                         Assert.IsNotNull(result);\r
897                         Assert.AreEqual(result.GetDirectoryEntry().Name,"ou=Human Resources");\r
898 \r
899                         ds.Filter = "(objectClass=Barak Tsabari)";\r
900                         result = ds.FindOne();\r
901                         Assert.IsNull(result);\r
902                 }\r
903 \r
904 \r
905                 #endregion Tests\r
906 \r
907                 private static string GetName (string baseDn)\r
908                 {\r
909                         if (baseDn == null || baseDn.Length == 0 || baseDn.IndexOf (',') == -1)\r
910                                 return baseDn;\r
911 \r
912                         int index = baseDn.IndexOf (',');\r
913 \r
914                         return baseDn.Substring (0, index);\r
915                 }\r
916 \r
917                 private static string GetParentDN (string baseDn)\r
918                 {\r
919                         if (baseDn == null || baseDn.Length == 0 || baseDn.IndexOf (',') == -1)\r
920                                 return String.Empty;\r
921 \r
922                         int index = baseDn.IndexOf (',');\r
923 \r
924                         return baseDn.Substring (index + 1,baseDn.Length - index - 1);\r
925                 }\r
926         }\r
927 }\r
928 \r