2 // DirectoryServicesSearchResultTest.cs -
\r
3 // NUnit Test Cases for DirectoryServices.SearchResult
\r
6 // Boris Kirzner <borisk@mainsoft.com>
\r
9 using NUnit.Framework;
\r
11 using System.DirectoryServices;
\r
13 namespace MonoTests.System.DirectoryServices
\r
16 public class DirectoryServicesSearchResultTest
\r
20 static TestConfiguration configuration;
\r
21 static DirectoryEntry de;
\r
22 static DirectorySearcher ds;
\r
24 #endregion // Fields
\r
26 #region SetUp and TearDown
\r
29 public void TestFixtureSetUp()
\r
32 configuration = new TestConfiguration ();
\r
34 if (String.IsNullOrEmpty (configuration.ConnectionString))
\r
35 Assert.Ignore ("No configuration");
\r
39 [TestFixtureTearDown]
\r
40 public void TestFixtureTearDown()
\r
54 #region Initialize basics
\r
56 DirectoryEntry root = new DirectoryEntry( configuration.ConnectionString,
\r
57 configuration.Username,
\r
58 configuration.Password,
\r
59 configuration.AuthenticationType);
\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
67 #endregion // Initialize basics
\r
69 #region Human Resources
\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
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
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
95 #endregion // Human Resources
\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
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
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
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
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
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
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
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
180 #endregion // DevQA
\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
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
201 #endregion // Manager
\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
211 cnDanielSmith.Dispose();
\r
212 cnDanielMorgan.Dispose();
\r
214 cnUziCohen_.Dispose();
\r
215 cnManager.Dispose();
\r
216 ouPeople.Dispose();
\r
222 public void TearDown()
\r
234 using (DirectoryEntry root = new DirectoryEntry( configuration.ConnectionString,
\r
235 configuration.Username,
\r
236 configuration.Password,
\r
237 configuration.AuthenticationType)){
\r
239 foreach(DirectoryEntry child in root.Children) {
\r
240 DeleteTree_DFS(child);
\r
245 private void DeleteTree_DFS(DirectoryEntry de)
\r
247 foreach(DirectoryEntry child in de.Children) {
\r
248 DeleteTree_DFS(child);
\r
251 de.CommitChanges();
\r
254 #endregion //SetUp and TearDown
\r
259 public void SearchResult_Path()
\r
261 de = new DirectoryEntry(configuration.ConnectionString,
\r
262 configuration.Username,
\r
263 configuration.Password,
\r
264 configuration.AuthenticationType);
\r
266 ds = new DirectorySearcher(de);
\r
268 SearchResultCollection results = ds.FindAll();
\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
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
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
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
288 public void SearchResult_Properties()
\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
296 ds.PropertiesToLoad.Add("cn");
\r
297 ds.PropertiesToLoad.Add("ADsPath");
\r
298 ds.PropertiesToLoad.Add("objectClass");
\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
305 ds.Filter = "(objectClass=person)";
\r
306 SearchResult result = ds.FindOne();
\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
313 ds.PropertiesToLoad.Clear();
\r
315 ds.PropertiesToLoad.Add("cn");
\r
316 ds.PropertiesToLoad.Add("objectClass");
\r
317 ds.PropertiesToLoad.Add("missingProperty");
\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
324 ds.Filter = "(objectClass=person)";
\r
325 result = ds.FindOne();
\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
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