Merge pull request #5198 from BrzVlad/fix-binprot-stats
[mono.git] / mcs / class / System.DirectoryServices / Test / System.DirectoryServices / DirectoryServicesSearchResultTest.cs
1 //\r
2 // DirectoryServicesSearchResultTest.cs -\r
3 //      NUnit Test Cases for DirectoryServices.SearchResult\r
4 //\r
5 // Author:\r
6 //      Boris Kirzner  <borisk@mainsoft.com>\r
7 //\r
8 \r
9 using NUnit.Framework;\r
10 using System;\r
11 using System.DirectoryServices;\r
12 \r
13 namespace MonoTests.System.DirectoryServices \r
14 {\r
15         [TestFixture]\r
16         public class DirectoryServicesSearchResultTest\r
17         {\r
18                 #region Fields\r
19 \r
20                 static TestConfiguration configuration;\r
21                 static DirectoryEntry de;\r
22                 static DirectorySearcher ds;\r
23 \r
24                 #endregion // Fields\r
25 \r
26                 #region SetUp and TearDown\r
27 \r
28                 [TestFixtureSetUp]\r
29                 public void TestFixtureSetUp()\r
30                 {\r
31                         de = null;\r
32                         configuration = new TestConfiguration ();\r
33 \r
34                         if (String.IsNullOrEmpty (configuration.ConnectionString))\r
35                                 Assert.Ignore ("No configuration");\r
36                 }\r
37 \r
38 \r
39                 [TestFixtureTearDown]\r
40                 public void TestFixtureTearDown()\r
41                 {\r
42                         if (de != null)\r
43                                 de.Dispose ();\r
44 \r
45                         de = null;\r
46                 }\r
47 \r
48 \r
49                 [SetUp]\r
50                 public void SetUp()\r
51                 {\r
52                         TearDown();\r
53 \r
54                         #region Initialize basics\r
55 \r
56                         DirectoryEntry root = new DirectoryEntry(       configuration.ConnectionString,\r
57                                                                                                                 configuration.Username,\r
58                                                                                                                 configuration.Password,\r
59                                                                                                                 configuration.AuthenticationType);\r
60 \r
61                         DirectoryEntry ouPeople = root.Children.Add("ou=people","Class");\r
62                         ouPeople.Properties["objectClass"].Value = "organizationalUnit";\r
63                         ouPeople.Properties["description"].Value = "All people in organisation";\r
64                         ouPeople.Properties["ou"].Value = "people";\r
65                         ouPeople.CommitChanges();\r
66 \r
67                         #endregion // Initialize basics\r
68 \r
69                         #region Human Resources\r
70  \r
71                         DirectoryEntry ouHumanResources = ouPeople.Children.Add("ou=Human Resources","Class");\r
72                         ouHumanResources.Properties["objectClass"].Value = "organizationalUnit";\r
73                         ouHumanResources.Properties["ou"].Value = "Human Resources";\r
74                         ouHumanResources.CommitChanges();\r
75 \r
76                         DirectoryEntry cnJohnSmith = ouHumanResources.Children.Add("cn=John Smith","Class");\r
77                         cnJohnSmith.Properties["objectClass"].Value = "organizationalRole";\r
78                         cnJohnSmith.Properties["cn"].Value = "John Smith";\r
79                         cnJohnSmith.Properties["description"].Value = "Very clever person";\r
80                         cnJohnSmith.Properties["ou"].Value = "Human Resources";\r
81                         cnJohnSmith.Properties["telephoneNumber"].Value = "1 801 555 1212";\r
82                         cnJohnSmith.CommitChanges();\r
83 \r
84                         DirectoryEntry cnBarakTsabari = ouHumanResources.Children.Add("cn=Barak Tsabari","Class");\r
85                         ((PropertyValueCollection)cnBarakTsabari.Properties["objectClass"]).Add("person");\r
86                         ((PropertyValueCollection)cnBarakTsabari.Properties["objectClass"]).Add("organizationalPerson");\r
87                         cnBarakTsabari.Properties["cn"].Value = "Barak Tsabari";\r
88                         cnBarakTsabari.Properties["facsimileTelephoneNumber"].Value = "+1 906 777 8853";\r
89                         ((PropertyValueCollection)cnBarakTsabari.Properties["ou"]).Add("Human Resources");\r
90                         ((PropertyValueCollection)cnBarakTsabari.Properties["ou"]).Add("People");\r
91                         cnBarakTsabari.Properties["sn"].Value = "Tsabari";\r
92                         cnBarakTsabari.Properties["telephoneNumber"].Value = "+1 906 777 8854";\r
93                         cnBarakTsabari.CommitChanges();\r
94 \r
95                         #endregion // Human Resources\r
96 \r
97                         #region R&D\r
98 \r
99                         DirectoryEntry ouRnD = ouPeople.Children.Add("ou=R&D","Class");\r
100                         ouRnD.Properties["objectClass"].Value = "organizationalUnit";\r
101                         ouRnD.Properties["ou"].Value = "R&D";\r
102                         ouRnD.CommitChanges();\r
103 \r
104                         DirectoryEntry cnYossiCohen = ouRnD.Children.Add("cn=Yossi Cohen","Class");\r
105                         ((PropertyValueCollection)cnYossiCohen.Properties["objectClass"]).Add("person");\r
106                         ((PropertyValueCollection)cnYossiCohen.Properties["objectClass"]).Add("organizationalPerson");\r
107                         cnYossiCohen.Properties["cn"].Value = "Yossi Cohen";\r
108                         cnYossiCohen.Properties["facsimileTelephoneNumber"].Value = "+1 503 777 4498";\r
109                         ((PropertyValueCollection)cnYossiCohen.Properties["ou"]).Add("R&D");\r
110                         ((PropertyValueCollection)cnYossiCohen.Properties["ou"]).Add("People");\r
111                         cnYossiCohen.Properties["sn"].Value = "Cohen";\r
112                         cnYossiCohen.Properties["telephoneNumber"].Value = "+1 503 777 4499";\r
113                         cnYossiCohen.CommitChanges();\r
114 \r
115                         DirectoryEntry cnUziCohen = ouRnD.Children.Add("cn=Uzi Cohen","Class");\r
116                         ((PropertyValueCollection)cnUziCohen.Properties["objectClass"]).Add("person");\r
117                         ((PropertyValueCollection)cnUziCohen.Properties["objectClass"]).Add("organizationalPerson");\r
118                         cnUziCohen.Properties["cn"].Value = "Uzi Cohen";\r
119                         cnUziCohen.Properties["facsimileTelephoneNumber"].Value = "+1 602 333 1234";\r
120                         ((PropertyValueCollection)cnUziCohen.Properties["ou"]).Add("R&D");\r
121                         ((PropertyValueCollection)cnUziCohen.Properties["ou"]).Add("People");\r
122                         cnUziCohen.Properties["sn"].Value = "Cohen";\r
123                         cnUziCohen.Properties["telephoneNumber"].Value = "+1 602 333 1233";\r
124                         cnUziCohen.CommitChanges();\r
125 \r
126                         DirectoryEntry cnDanielCohen = ouRnD.Children.Add("cn=Daniel Cohen","Class");\r
127                         ((PropertyValueCollection)cnDanielCohen.Properties["objectClass"]).Add("person");\r
128                         ((PropertyValueCollection)cnDanielCohen.Properties["objectClass"]).Add("organizationalPerson");\r
129                         cnDanielCohen.Properties["cn"].Value = "Daniel Cohen";\r
130                         cnDanielCohen.Properties["facsimileTelephoneNumber"].Value = "+1 602 333 1235";\r
131                         ((PropertyValueCollection)cnDanielCohen.Properties["ou"]).Add("R&D");\r
132                         ((PropertyValueCollection)cnDanielCohen.Properties["ou"]).Add("People");\r
133                         cnDanielCohen.Properties["sn"].Value = "Cohen";\r
134                         cnDanielCohen.Properties["telephoneNumber"].Value = "+1 602 333 1236";\r
135                         cnDanielCohen.CommitChanges();\r
136 \r
137                         DirectoryEntry cnSaraCohen = ouRnD.Children.Add("cn=Sara Cohen","Class");\r
138                         ((PropertyValueCollection)cnSaraCohen.Properties["objectClass"]).Add("person");\r
139                         ((PropertyValueCollection)cnSaraCohen.Properties["objectClass"]).Add("organizationalPerson");\r
140                         cnSaraCohen.Properties["cn"].Value = "Sara Cohen";\r
141                         cnSaraCohen.Properties["facsimileTelephoneNumber"].Value = "+1 602 333 1244";\r
142                         ((PropertyValueCollection)cnSaraCohen.Properties["ou"]).Add("R&D");\r
143                         ((PropertyValueCollection)cnSaraCohen.Properties["ou"]).Add("People");\r
144                         cnSaraCohen.Properties["sn"].Value = "Cohen";\r
145                         cnSaraCohen.Properties["telephoneNumber"].Value = "+1 602 333 1243";\r
146                         cnSaraCohen.CommitChanges();\r
147 \r
148                         #endregion // R&D\r
149 \r
150                         #region DevQA\r
151 \r
152                         DirectoryEntry ouDevQA = ouPeople.Children.Add("ou=DevQA","Class");\r
153                         ouDevQA.Properties["objectClass"].Value = "organizationalUnit";\r
154                         ouDevQA.Properties["ou"].Value = "DevQA";\r
155                         ouDevQA.CommitChanges();\r
156 \r
157                         DirectoryEntry cnDanielSmith = ouDevQA.Children.Add("cn=Daniel Smith","Class");\r
158                         ((PropertyValueCollection)cnDanielSmith.Properties["objectClass"]).Add("person");\r
159                         ((PropertyValueCollection)cnDanielSmith.Properties["objectClass"]).Add("organizationalPerson");\r
160                         cnDanielSmith.Properties["cn"].Value = "Daniel Smith";\r
161                         cnDanielSmith.Properties["facsimileTelephoneNumber"].Value = "+1 408 555 3372";\r
162                         cnDanielSmith.Properties["l"].Value = "Santa Clara";\r
163                         ((PropertyValueCollection)cnDanielSmith.Properties["ou"]).Add("DevQA");\r
164                         ((PropertyValueCollection)cnDanielSmith.Properties["ou"]).Add("People");\r
165                         cnDanielSmith.Properties["sn"].Value = "Smith";\r
166                         cnDanielSmith.Properties["telephoneNumber"].Value = "+1 408 555 9519";\r
167                         cnDanielSmith.CommitChanges();\r
168 \r
169                         DirectoryEntry cnDanielMorgan = ouDevQA.Children.Add("cn=Daniel Morgan","Class");\r
170                         ((PropertyValueCollection)cnDanielMorgan.Properties["objectClass"]).Add("person");\r
171                         ((PropertyValueCollection)cnDanielMorgan.Properties["objectClass"]).Add("organizationalPerson");\r
172                         cnDanielMorgan.Properties["cn"].Value = "Daniel Morgan";\r
173                         cnDanielMorgan.Properties["facsimileTelephoneNumber"].Value = "+1 805 666 5645";\r
174                         ((PropertyValueCollection)cnDanielMorgan.Properties["ou"]).Add("DevQA");\r
175                         ((PropertyValueCollection)cnDanielMorgan.Properties["ou"]).Add("People");\r
176                         cnDanielMorgan.Properties["sn"].Value = "Morgan";\r
177                         cnDanielMorgan.Properties["telephoneNumber"].Value = "+1 805 666 5644";\r
178                         cnDanielMorgan.CommitChanges();\r
179 \r
180                         #endregion // DevQA\r
181 \r
182                         #region Manager\r
183 \r
184                         DirectoryEntry cnManager = root.Children.Add("cn=Manager","Class");\r
185                         cnManager.Properties["objectClass"].Value = "organizationalRole";\r
186                         cnManager.Properties["cn"].Value = "Manager";\r
187                         cnManager.Properties["facsimileTelephoneNumber"].Value = "+1 602 333 1238";\r
188                         cnManager.CommitChanges();\r
189 \r
190                         DirectoryEntry cnUziCohen_ = cnManager.Children.Add("cn=Uzi Cohen","Class");\r
191                         ((PropertyValueCollection)cnUziCohen_.Properties["objectClass"]).Add("person");\r
192                         ((PropertyValueCollection)cnUziCohen_.Properties["objectClass"]).Add("organizationalPerson");\r
193                         cnUziCohen_.Properties["cn"].Value = "Uzi Cohen";\r
194                         cnUziCohen_.Properties["facsimileTelephoneNumber"].Value = "+1 602 333 1234";\r
195                         ((PropertyValueCollection)cnUziCohen_.Properties["ou"]).Add("R&D");\r
196                         ((PropertyValueCollection)cnUziCohen_.Properties["ou"]).Add("People");\r
197                         cnUziCohen_.Properties["sn"].Value = "Cohen";\r
198                         cnUziCohen_.Properties["telephoneNumber"].Value = "+1 602 333 1233";\r
199                         cnUziCohen_.CommitChanges();\r
200 \r
201                         #endregion // Manager\r
202 \r
203                         cnJohnSmith.Dispose();\r
204                         cnBarakTsabari.Dispose();\r
205                         ouHumanResources.Dispose();\r
206                         cnUziCohen.Dispose();\r
207                         cnYossiCohen.Dispose();\r
208                         cnDanielCohen.Dispose();\r
209                         cnSaraCohen.Dispose();\r
210                         ouRnD.Dispose();\r
211                         cnDanielSmith.Dispose();\r
212                         cnDanielMorgan.Dispose();\r
213                         ouDevQA.Dispose();\r
214                         cnUziCohen_.Dispose();\r
215                         cnManager.Dispose();\r
216                         ouPeople.Dispose();\r
217                         root.Dispose();\r
218                 }\r
219 \r
220 \r
221                 [TearDown]\r
222                 public void TearDown()\r
223                 {\r
224                         if (ds != null)\r
225                                 ds.Dispose ();\r
226                         \r
227                         ds = null;\r
228                         \r
229                         if (de != null)\r
230                                 de.Dispose ();\r
231 \r
232                         de = null;\r
233 \r
234                         using (DirectoryEntry root = new DirectoryEntry(        configuration.ConnectionString,\r
235                                                                                                                 configuration.Username,\r
236                                                                                                                 configuration.Password,\r
237                                                                                                                 configuration.AuthenticationType)){\r
238                         \r
239                         foreach(DirectoryEntry child in root.Children) {\r
240                                 DeleteTree_DFS(child);\r
241                         }       \r
242                         }\r
243                 }\r
244 \r
245                 private void DeleteTree_DFS(DirectoryEntry de)\r
246                 {\r
247                         foreach(DirectoryEntry child in de.Children) {\r
248                                 DeleteTree_DFS(child);\r
249                         }\r
250                         de.DeleteTree();\r
251                         de.CommitChanges();\r
252                 }\r
253 \r
254                 #endregion //SetUp and TearDown\r
255 \r
256                 #region Tests\r
257 \r
258                 [Test]\r
259                 public void SearchResult_Path()\r
260                 {\r
261                         de = new DirectoryEntry(configuration.ConnectionString,\r
262                                                                         configuration.Username,\r
263                                                                         configuration.Password,\r
264                                                                         configuration.AuthenticationType);\r
265 \r
266                         ds = new DirectorySearcher(de);\r
267 \r
268                         SearchResultCollection results = ds.FindAll();\r
269 \r
270                         // MS works only with "LDAP" while RFC2255 states "ldap"\r
271                         Assert.AreEqual(results[0].Path.ToLower(),(configuration.ServerRoot + configuration.BaseDn).ToLower());\r
272                         Assert.AreEqual(results[0].Path,results[0].GetDirectoryEntry().Path);\r
273 \r
274                         // MS works only with "LDAP" while RFC2255 states "ldap"\r
275                         Assert.AreEqual(results[1].Path.ToLower(),(configuration.ServerRoot + "ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn))).ToLower());\r
276                         Assert.AreEqual(results[1].Path,results[1].GetDirectoryEntry().Path);\r
277 \r
278                         // MS works only with "LDAP" while RFC2255 states "ldap"\r
279                         Assert.AreEqual(results[2].Path.ToLower(),(configuration.ServerRoot + "ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn))).ToLower());\r
280                         Assert.AreEqual(results[2].Path,results[2].GetDirectoryEntry().Path);\r
281 \r
282                         // MS works only with "LDAP" while RFC2255 states "ldap"\r
283                         Assert.AreEqual(results[3].Path.ToLower(),(configuration.ServerRoot + "cn=John Smith,ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn))).ToLower());\r
284                         Assert.AreEqual(results[3].Path,results[3].GetDirectoryEntry().Path);\r
285                 }\r
286 \r
287                 [Test]\r
288                 public void SearchResult_Properties()\r
289                 {\r
290                         de = new DirectoryEntry(configuration.ConnectionString,\r
291                                                                         configuration.Username,\r
292                                                                         configuration.Password,\r
293                                                                         configuration.AuthenticationType);\r
294                         ds = new DirectorySearcher(de);\r
295 \r
296                         ds.PropertiesToLoad.Add("cn");\r
297                         ds.PropertiesToLoad.Add("ADsPath");\r
298                         ds.PropertiesToLoad.Add("objectClass");\r
299 \r
300                         Assert.AreEqual(ds.PropertiesToLoad.Count,3);\r
301                         Assert.IsTrue(ds.PropertiesToLoad.Contains("cn"));\r
302                         Assert.IsTrue(ds.PropertiesToLoad.Contains("ADsPath"));\r
303                         Assert.IsTrue(ds.PropertiesToLoad.Contains("objectClass"));\r
304 \r
305                         ds.Filter = "(objectClass=person)";\r
306                         SearchResult result = ds.FindOne();\r
307 \r
308                         Assert.AreEqual(result.Properties.Count,3);\r
309                         Assert.IsTrue(result.Properties.Contains("cn"));\r
310                         Assert.IsTrue(result.Properties.Contains("objectClass"));\r
311                         Assert.IsTrue(result.Properties.Contains("ADsPath"));\r
312 \r
313                         ds.PropertiesToLoad.Clear();\r
314 \r
315                         ds.PropertiesToLoad.Add("cn");\r
316                         ds.PropertiesToLoad.Add("objectClass");\r
317                         ds.PropertiesToLoad.Add("missingProperty");\r
318 \r
319                         Assert.AreEqual(ds.PropertiesToLoad.Count,3);\r
320                         Assert.IsTrue(ds.PropertiesToLoad.Contains("cn"));\r
321                         Assert.IsTrue(ds.PropertiesToLoad.Contains("objectClass"));\r
322                         Assert.IsTrue(ds.PropertiesToLoad.Contains("missingProperty"));\r
323 \r
324                         ds.Filter = "(objectClass=person)";\r
325                         result = ds.FindOne();\r
326 \r
327                         // Properties that do not exist are not loaded\r
328                         Assert.AreEqual(result.Properties.Count,3);\r
329                         Assert.IsTrue(result.Properties.Contains("cn"));\r
330                         Assert.IsTrue(result.Properties.Contains("objectClass"));\r
331                         Assert.IsTrue(result.Properties.Contains("ADsPath"));\r
332 \r
333                         Assert.AreEqual(((ResultPropertyValueCollection)result.Properties["cn"])[0],"Barak Tsabari");\r
334                         Assert.AreEqual(((ResultPropertyValueCollection)result.Properties["objectClass"])[0],"person");\r
335                         // MS works only with "LDAP" while RFC2255 states "ldap"\r
336                         Assert.AreEqual(((string)((ResultPropertyValueCollection)result.Properties["AdsPath"])[0]).ToLower(),(configuration.ServerRoot + "cn=Barak Tsabari,ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn))).ToLower());\r
337                 }\r
338 \r
339                 #endregion Tests\r
340         }\r
341 }\r