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