2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.DirectoryServices / Test / System.DirectoryServices / DirectoryServicesSearchResultTest.cs
blob487bdbcd1604ae2bb5ef9cfb1ae0c37a887b9e1a
1 //
2 // DirectoryServicesSearchResultTest.cs -
3 // NUnit Test Cases for DirectoryServices.SearchResult
4 //
5 // Author:
6 // Boris Kirzner <borisk@mainsoft.com>
7 //
9 using NUnit.Framework;
10 using System;
11 using System.DirectoryServices;
13 namespace MonoTests.System.DirectoryServices
15 [TestFixture]
16 [Category ("InetAccess")]
17 public class DirectoryServicesSearchResultTest
19 #region Fields
21 static TestConfiguration configuration;
22 static DirectoryEntry de;
23 static DirectorySearcher ds;
25 #endregion // Fields
27 #region SetUp and TearDown
29 [TestFixtureSetUp]
30 public void TestFixtureSetUp()
32 de = null;
33 configuration = new TestConfiguration ();
37 [TestFixtureTearDown]
38 public void TestFixtureTearDown()
40 if (de != null)
41 de.Dispose ();
43 de = null;
47 [SetUp]
48 public void SetUp()
50 TearDown();
52 #region Initialize basics
54 DirectoryEntry root = new DirectoryEntry( configuration.ConnectionString,
55 configuration.Username,
56 configuration.Password,
57 configuration.AuthenticationType);
59 DirectoryEntry ouPeople = root.Children.Add("ou=people","Class");
60 ouPeople.Properties["objectClass"].Value = "organizationalUnit";
61 ouPeople.Properties["description"].Value = "All people in organisation";
62 ouPeople.Properties["ou"].Value = "people";
63 ouPeople.CommitChanges();
65 #endregion // Initialize basics
67 #region Human Resources
69 DirectoryEntry ouHumanResources = ouPeople.Children.Add("ou=Human Resources","Class");
70 ouHumanResources.Properties["objectClass"].Value = "organizationalUnit";
71 ouHumanResources.Properties["ou"].Value = "Human Resources";
72 ouHumanResources.CommitChanges();
74 DirectoryEntry cnJohnSmith = ouHumanResources.Children.Add("cn=John Smith","Class");
75 cnJohnSmith.Properties["objectClass"].Value = "organizationalRole";
76 cnJohnSmith.Properties["cn"].Value = "John Smith";
77 cnJohnSmith.Properties["description"].Value = "Very clever person";
78 cnJohnSmith.Properties["ou"].Value = "Human Resources";
79 cnJohnSmith.Properties["telephoneNumber"].Value = "1 801 555 1212";
80 cnJohnSmith.CommitChanges();
82 DirectoryEntry cnBarakTsabari = ouHumanResources.Children.Add("cn=Barak Tsabari","Class");
83 ((PropertyValueCollection)cnBarakTsabari.Properties["objectClass"]).Add("person");
84 ((PropertyValueCollection)cnBarakTsabari.Properties["objectClass"]).Add("organizationalPerson");
85 cnBarakTsabari.Properties["cn"].Value = "Barak Tsabari";
86 cnBarakTsabari.Properties["facsimileTelephoneNumber"].Value = "+1 906 777 8853";
87 ((PropertyValueCollection)cnBarakTsabari.Properties["ou"]).Add("Human Resources");
88 ((PropertyValueCollection)cnBarakTsabari.Properties["ou"]).Add("People");
89 cnBarakTsabari.Properties["sn"].Value = "Tsabari";
90 cnBarakTsabari.Properties["telephoneNumber"].Value = "+1 906 777 8854";
91 cnBarakTsabari.CommitChanges();
93 #endregion // Human Resources
95 #region R&D
97 DirectoryEntry ouRnD = ouPeople.Children.Add("ou=R&D","Class");
98 ouRnD.Properties["objectClass"].Value = "organizationalUnit";
99 ouRnD.Properties["ou"].Value = "R&D";
100 ouRnD.CommitChanges();
102 DirectoryEntry cnYossiCohen = ouRnD.Children.Add("cn=Yossi Cohen","Class");
103 ((PropertyValueCollection)cnYossiCohen.Properties["objectClass"]).Add("person");
104 ((PropertyValueCollection)cnYossiCohen.Properties["objectClass"]).Add("organizationalPerson");
105 cnYossiCohen.Properties["cn"].Value = "Yossi Cohen";
106 cnYossiCohen.Properties["facsimileTelephoneNumber"].Value = "+1 503 777 4498";
107 ((PropertyValueCollection)cnYossiCohen.Properties["ou"]).Add("R&D");
108 ((PropertyValueCollection)cnYossiCohen.Properties["ou"]).Add("People");
109 cnYossiCohen.Properties["sn"].Value = "Cohen";
110 cnYossiCohen.Properties["telephoneNumber"].Value = "+1 503 777 4499";
111 cnYossiCohen.CommitChanges();
113 DirectoryEntry cnUziCohen = ouRnD.Children.Add("cn=Uzi Cohen","Class");
114 ((PropertyValueCollection)cnUziCohen.Properties["objectClass"]).Add("person");
115 ((PropertyValueCollection)cnUziCohen.Properties["objectClass"]).Add("organizationalPerson");
116 cnUziCohen.Properties["cn"].Value = "Uzi Cohen";
117 cnUziCohen.Properties["facsimileTelephoneNumber"].Value = "+1 602 333 1234";
118 ((PropertyValueCollection)cnUziCohen.Properties["ou"]).Add("R&D");
119 ((PropertyValueCollection)cnUziCohen.Properties["ou"]).Add("People");
120 cnUziCohen.Properties["sn"].Value = "Cohen";
121 cnUziCohen.Properties["telephoneNumber"].Value = "+1 602 333 1233";
122 cnUziCohen.CommitChanges();
124 DirectoryEntry cnDanielCohen = ouRnD.Children.Add("cn=Daniel Cohen","Class");
125 ((PropertyValueCollection)cnDanielCohen.Properties["objectClass"]).Add("person");
126 ((PropertyValueCollection)cnDanielCohen.Properties["objectClass"]).Add("organizationalPerson");
127 cnDanielCohen.Properties["cn"].Value = "Daniel Cohen";
128 cnDanielCohen.Properties["facsimileTelephoneNumber"].Value = "+1 602 333 1235";
129 ((PropertyValueCollection)cnDanielCohen.Properties["ou"]).Add("R&D");
130 ((PropertyValueCollection)cnDanielCohen.Properties["ou"]).Add("People");
131 cnDanielCohen.Properties["sn"].Value = "Cohen";
132 cnDanielCohen.Properties["telephoneNumber"].Value = "+1 602 333 1236";
133 cnDanielCohen.CommitChanges();
135 DirectoryEntry cnSaraCohen = ouRnD.Children.Add("cn=Sara Cohen","Class");
136 ((PropertyValueCollection)cnSaraCohen.Properties["objectClass"]).Add("person");
137 ((PropertyValueCollection)cnSaraCohen.Properties["objectClass"]).Add("organizationalPerson");
138 cnSaraCohen.Properties["cn"].Value = "Sara Cohen";
139 cnSaraCohen.Properties["facsimileTelephoneNumber"].Value = "+1 602 333 1244";
140 ((PropertyValueCollection)cnSaraCohen.Properties["ou"]).Add("R&D");
141 ((PropertyValueCollection)cnSaraCohen.Properties["ou"]).Add("People");
142 cnSaraCohen.Properties["sn"].Value = "Cohen";
143 cnSaraCohen.Properties["telephoneNumber"].Value = "+1 602 333 1243";
144 cnSaraCohen.CommitChanges();
146 #endregion // R&D
148 #region DevQA
150 DirectoryEntry ouDevQA = ouPeople.Children.Add("ou=DevQA","Class");
151 ouDevQA.Properties["objectClass"].Value = "organizationalUnit";
152 ouDevQA.Properties["ou"].Value = "DevQA";
153 ouDevQA.CommitChanges();
155 DirectoryEntry cnDanielSmith = ouDevQA.Children.Add("cn=Daniel Smith","Class");
156 ((PropertyValueCollection)cnDanielSmith.Properties["objectClass"]).Add("person");
157 ((PropertyValueCollection)cnDanielSmith.Properties["objectClass"]).Add("organizationalPerson");
158 cnDanielSmith.Properties["cn"].Value = "Daniel Smith";
159 cnDanielSmith.Properties["facsimileTelephoneNumber"].Value = "+1 408 555 3372";
160 cnDanielSmith.Properties["l"].Value = "Santa Clara";
161 ((PropertyValueCollection)cnDanielSmith.Properties["ou"]).Add("DevQA");
162 ((PropertyValueCollection)cnDanielSmith.Properties["ou"]).Add("People");
163 cnDanielSmith.Properties["sn"].Value = "Smith";
164 cnDanielSmith.Properties["telephoneNumber"].Value = "+1 408 555 9519";
165 cnDanielSmith.CommitChanges();
167 DirectoryEntry cnDanielMorgan = ouDevQA.Children.Add("cn=Daniel Morgan","Class");
168 ((PropertyValueCollection)cnDanielMorgan.Properties["objectClass"]).Add("person");
169 ((PropertyValueCollection)cnDanielMorgan.Properties["objectClass"]).Add("organizationalPerson");
170 cnDanielMorgan.Properties["cn"].Value = "Daniel Morgan";
171 cnDanielMorgan.Properties["facsimileTelephoneNumber"].Value = "+1 805 666 5645";
172 ((PropertyValueCollection)cnDanielMorgan.Properties["ou"]).Add("DevQA");
173 ((PropertyValueCollection)cnDanielMorgan.Properties["ou"]).Add("People");
174 cnDanielMorgan.Properties["sn"].Value = "Morgan";
175 cnDanielMorgan.Properties["telephoneNumber"].Value = "+1 805 666 5644";
176 cnDanielMorgan.CommitChanges();
178 #endregion // DevQA
180 #region Manager
182 DirectoryEntry cnManager = root.Children.Add("cn=Manager","Class");
183 cnManager.Properties["objectClass"].Value = "organizationalRole";
184 cnManager.Properties["cn"].Value = "Manager";
185 cnManager.Properties["facsimileTelephoneNumber"].Value = "+1 602 333 1238";
186 cnManager.CommitChanges();
188 DirectoryEntry cnUziCohen_ = cnManager.Children.Add("cn=Uzi Cohen","Class");
189 ((PropertyValueCollection)cnUziCohen_.Properties["objectClass"]).Add("person");
190 ((PropertyValueCollection)cnUziCohen_.Properties["objectClass"]).Add("organizationalPerson");
191 cnUziCohen_.Properties["cn"].Value = "Uzi Cohen";
192 cnUziCohen_.Properties["facsimileTelephoneNumber"].Value = "+1 602 333 1234";
193 ((PropertyValueCollection)cnUziCohen_.Properties["ou"]).Add("R&D");
194 ((PropertyValueCollection)cnUziCohen_.Properties["ou"]).Add("People");
195 cnUziCohen_.Properties["sn"].Value = "Cohen";
196 cnUziCohen_.Properties["telephoneNumber"].Value = "+1 602 333 1233";
197 cnUziCohen_.CommitChanges();
199 #endregion // Manager
201 cnJohnSmith.Dispose();
202 cnBarakTsabari.Dispose();
203 ouHumanResources.Dispose();
204 cnUziCohen.Dispose();
205 cnYossiCohen.Dispose();
206 cnDanielCohen.Dispose();
207 cnSaraCohen.Dispose();
208 ouRnD.Dispose();
209 cnDanielSmith.Dispose();
210 cnDanielMorgan.Dispose();
211 ouDevQA.Dispose();
212 cnUziCohen_.Dispose();
213 cnManager.Dispose();
214 ouPeople.Dispose();
215 root.Dispose();
219 [TearDown]
220 public void TearDown()
222 if (ds != null)
223 ds.Dispose ();
225 ds = null;
227 if (de != null)
228 de.Dispose ();
230 de = null;
232 using (DirectoryEntry root = new DirectoryEntry( configuration.ConnectionString,
233 configuration.Username,
234 configuration.Password,
235 configuration.AuthenticationType)){
237 foreach(DirectoryEntry child in root.Children) {
238 DeleteTree_DFS(child);
243 private void DeleteTree_DFS(DirectoryEntry de)
245 foreach(DirectoryEntry child in de.Children) {
246 DeleteTree_DFS(child);
248 de.DeleteTree();
249 de.CommitChanges();
252 #endregion //SetUp and TearDown
254 #region Tests
256 [Test]
257 public void SearchResult_Path()
259 de = new DirectoryEntry(configuration.ConnectionString,
260 configuration.Username,
261 configuration.Password,
262 configuration.AuthenticationType);
264 ds = new DirectorySearcher(de);
266 SearchResultCollection results = ds.FindAll();
268 // MS works only with "LDAP" while RFC2255 states "ldap"
269 Assert.AreEqual(results[0].Path.ToLower(),(configuration.ServerRoot + configuration.BaseDn).ToLower());
270 Assert.AreEqual(results[0].Path,results[0].GetDirectoryEntry().Path);
272 // MS works only with "LDAP" while RFC2255 states "ldap"
273 Assert.AreEqual(results[1].Path.ToLower(),(configuration.ServerRoot + "ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn))).ToLower());
274 Assert.AreEqual(results[1].Path,results[1].GetDirectoryEntry().Path);
276 // MS works only with "LDAP" while RFC2255 states "ldap"
277 Assert.AreEqual(results[2].Path.ToLower(),(configuration.ServerRoot + "ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn))).ToLower());
278 Assert.AreEqual(results[2].Path,results[2].GetDirectoryEntry().Path);
280 // MS works only with "LDAP" while RFC2255 states "ldap"
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());
282 Assert.AreEqual(results[3].Path,results[3].GetDirectoryEntry().Path);
285 [Test]
286 public void SearchResult_Properties()
288 de = new DirectoryEntry(configuration.ConnectionString,
289 configuration.Username,
290 configuration.Password,
291 configuration.AuthenticationType);
292 ds = new DirectorySearcher(de);
294 ds.PropertiesToLoad.Add("cn");
295 ds.PropertiesToLoad.Add("ADsPath");
296 ds.PropertiesToLoad.Add("objectClass");
298 Assert.AreEqual(ds.PropertiesToLoad.Count,3);
299 Assert.IsTrue(ds.PropertiesToLoad.Contains("cn"));
300 Assert.IsTrue(ds.PropertiesToLoad.Contains("ADsPath"));
301 Assert.IsTrue(ds.PropertiesToLoad.Contains("objectClass"));
303 ds.Filter = "(objectClass=person)";
304 SearchResult result = ds.FindOne();
306 Assert.AreEqual(result.Properties.Count,3);
307 Assert.IsTrue(result.Properties.Contains("cn"));
308 Assert.IsTrue(result.Properties.Contains("objectClass"));
309 Assert.IsTrue(result.Properties.Contains("ADsPath"));
311 ds.PropertiesToLoad.Clear();
313 ds.PropertiesToLoad.Add("cn");
314 ds.PropertiesToLoad.Add("objectClass");
315 ds.PropertiesToLoad.Add("missingProperty");
317 Assert.AreEqual(ds.PropertiesToLoad.Count,3);
318 Assert.IsTrue(ds.PropertiesToLoad.Contains("cn"));
319 Assert.IsTrue(ds.PropertiesToLoad.Contains("objectClass"));
320 Assert.IsTrue(ds.PropertiesToLoad.Contains("missingProperty"));
322 ds.Filter = "(objectClass=person)";
323 result = ds.FindOne();
325 // Properties that does not exists are not loaded
326 Assert.AreEqual(result.Properties.Count,3);
327 Assert.IsTrue(result.Properties.Contains("cn"));
328 Assert.IsTrue(result.Properties.Contains("objectClass"));
329 Assert.IsTrue(result.Properties.Contains("ADsPath"));
331 Assert.AreEqual(((ResultPropertyValueCollection)result.Properties["cn"])[0],"Barak Tsabari");
332 Assert.AreEqual(((ResultPropertyValueCollection)result.Properties["objectClass"])[0],"person");
333 // MS works only with "LDAP" while RFC2255 states "ldap"
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());
337 #endregion Tests