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 [Category ("InetAccess")]
\r
17 public class DirectoryServicesSearchResultTest
\r
21 static TestConfiguration configuration;
\r
22 static DirectoryEntry de;
\r
23 static DirectorySearcher ds;
\r
25 #endregion // Fields
\r
27 #region SetUp and TearDown
\r
30 public void TestFixtureSetUp()
\r
33 configuration = new TestConfiguration ();
\r
37 [TestFixtureTearDown]
\r
38 public void TestFixtureTearDown()
\r
52 #region Initialize basics
\r
54 DirectoryEntry root = new DirectoryEntry( configuration.ConnectionString,
\r
55 configuration.Username,
\r
56 configuration.Password,
\r
57 configuration.AuthenticationType);
\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
65 #endregion // Initialize basics
\r
67 #region Human Resources
\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
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
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
93 #endregion // Human Resources
\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
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
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
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
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
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
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
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
178 #endregion // DevQA
\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
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
199 #endregion // Manager
\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
209 cnDanielSmith.Dispose();
\r
210 cnDanielMorgan.Dispose();
\r
212 cnUziCohen_.Dispose();
\r
213 cnManager.Dispose();
\r
214 ouPeople.Dispose();
\r
220 public void TearDown()
\r
232 using (DirectoryEntry root = new DirectoryEntry( configuration.ConnectionString,
\r
233 configuration.Username,
\r
234 configuration.Password,
\r
235 configuration.AuthenticationType)){
\r
237 foreach(DirectoryEntry child in root.Children) {
\r
238 DeleteTree_DFS(child);
\r
243 private void DeleteTree_DFS(DirectoryEntry de)
\r
245 foreach(DirectoryEntry child in de.Children) {
\r
246 DeleteTree_DFS(child);
\r
249 de.CommitChanges();
\r
252 #endregion //SetUp and TearDown
\r
257 public void SearchResult_Path()
\r
259 de = new DirectoryEntry(configuration.ConnectionString,
\r
260 configuration.Username,
\r
261 configuration.Password,
\r
262 configuration.AuthenticationType);
\r
264 ds = new DirectorySearcher(de);
\r
266 SearchResultCollection results = ds.FindAll();
\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
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
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
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
286 public void SearchResult_Properties()
\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
294 ds.PropertiesToLoad.Add("cn");
\r
295 ds.PropertiesToLoad.Add("ADsPath");
\r
296 ds.PropertiesToLoad.Add("objectClass");
\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
303 ds.Filter = "(objectClass=person)";
\r
304 SearchResult result = ds.FindOne();
\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
311 ds.PropertiesToLoad.Clear();
\r
313 ds.PropertiesToLoad.Add("cn");
\r
314 ds.PropertiesToLoad.Add("objectClass");
\r
315 ds.PropertiesToLoad.Add("missingProperty");
\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
322 ds.Filter = "(objectClass=person)";
\r
323 result = ds.FindOne();
\r
325 // Properties that does not exists 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
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