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