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