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 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
28 #endregion // Fields
\r
30 #region SetUp and TearDown
\r
33 public void TestFixtureSetUp()
\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
45 [TestFixtureTearDown]
\r
46 public void TestFixtureTearDown()
\r
57 #region Initialize basics
\r
59 DirectoryEntry root = new DirectoryEntry( LDAPServerConnectionString,
\r
62 AuthenticationTypes.ServerBind);
\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
70 #endregion // Initialize basics
\r
72 #region Human Resources
\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
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
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
98 #endregion // Human Resources
\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
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
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
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
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
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
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
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
183 #endregion // DevQA
\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
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
203 #endregion // Manager
\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
213 cnDanielSmith.Dispose();
\r
214 cnDanielMorgan.Dispose();
\r
216 cnUziCohen_.Dispose();
\r
217 cnManager.Dispose();
\r
218 ouPeople.Dispose();
\r
224 public void TearDown()
\r
229 DirectoryEntry root = new DirectoryEntry( LDAPServerConnectionString,
\r
230 LDAPServerUsername,
\r
231 LDAPServerPassword,
\r
232 AuthenticationTypes.ServerBind);
\r
234 foreach(DirectoryEntry child in root.Children) {
\r
235 DeleteTree_DFS(child);
\r
239 private void DeleteTree_DFS(DirectoryEntry de)
\r
241 foreach(DirectoryEntry child in de.Children) {
\r
242 DeleteTree_DFS(child);
\r
245 de.CommitChanges();
\r
248 #endregion //SetUp and TearDown
\r
253 public void SearchResult_Path()
\r
255 de = new DirectoryEntry(LDAPServerConnectionString,
\r
256 LDAPServerUsername,
\r
257 LDAPServerPassword,
\r
258 AuthenticationTypes.ServerBind);
\r
260 ds = new DirectorySearcher(de);
\r
262 SearchResultCollection results = ds.FindAll();
\r
264 Assert.AreEqual(results[0].Path,LDAPServerRoot + "dc=myhosting,dc=example");
\r
265 Assert.AreEqual(results[0].Path,results[0].GetDirectoryEntry().Path);
\r
267 Assert.AreEqual(results[1].Path,LDAPServerRoot + "ou=people,dc=myhosting,dc=example");
\r
268 Assert.AreEqual(results[1].Path,results[1].GetDirectoryEntry().Path);
\r
270 Assert.AreEqual(results[2].Path,LDAPServerRoot + "ou=Human Resources,ou=people,dc=myhosting,dc=example");
\r
271 Assert.AreEqual(results[2].Path,results[2].GetDirectoryEntry().Path);
\r
273 Assert.AreEqual(results[3].Path,LDAPServerRoot + "cn=John Smith,ou=Human Resources,ou=people,dc=myhosting,dc=example");
\r
274 Assert.AreEqual(results[3].Path,results[3].GetDirectoryEntry().Path);
\r
278 public void SearchResult_Properties()
\r
280 de = new DirectoryEntry(LDAPServerConnectionString,
\r
281 LDAPServerUsername,
\r
282 LDAPServerPassword,
\r
283 AuthenticationTypes.ServerBind);
\r
285 ds = new DirectorySearcher(de);
\r
287 ds.PropertiesToLoad.Add("cn");
\r
288 ds.PropertiesToLoad.Add("ADsPath");
\r
289 ds.PropertiesToLoad.Add("objectClass");
\r
291 Assert.AreEqual(ds.PropertiesToLoad.Count,3);
\r
292 Assert.IsTrue(ds.PropertiesToLoad.Contains("cn"));
\r
293 Assert.IsTrue(ds.PropertiesToLoad.Contains("ADsPath"));
\r
294 Assert.IsTrue(ds.PropertiesToLoad.Contains("objectClass"));
\r
296 ds.Filter = "(objectClass=person)";
\r
297 SearchResult result = ds.FindOne();
\r
299 Assert.AreEqual(result.Properties.Count,3);
\r
300 Assert.IsTrue(result.Properties.Contains("cn"));
\r
301 Assert.IsTrue(result.Properties.Contains("objectClass"));
\r
302 Assert.IsTrue(result.Properties.Contains("ADsPath"));
\r
304 ds.PropertiesToLoad.Clear();
\r
306 ds.PropertiesToLoad.Add("cn");
\r
307 ds.PropertiesToLoad.Add("objectClass");
\r
308 ds.PropertiesToLoad.Add("missingProperty");
\r
310 Assert.AreEqual(ds.PropertiesToLoad.Count,3);
\r
311 Assert.IsTrue(ds.PropertiesToLoad.Contains("cn"));
\r
312 Assert.IsTrue(ds.PropertiesToLoad.Contains("objectClass"));
\r
313 Assert.IsTrue(ds.PropertiesToLoad.Contains("missingProperty"));
\r
315 ds.Filter = "(objectClass=person)";
\r
316 result = ds.FindOne();
\r
318 // Properties that does not exists are not loaded
\r
319 Assert.AreEqual(result.Properties.Count,3);
\r
320 Assert.IsTrue(result.Properties.Contains("cn"));
\r
321 Assert.IsTrue(result.Properties.Contains("objectClass"));
\r
322 Assert.IsTrue(result.Properties.Contains("ADsPath"));
\r
324 Assert.AreEqual(((ResultPropertyValueCollection)result.Properties["cn"])[0],"Barak Tsabari");
\r
325 Assert.AreEqual(((ResultPropertyValueCollection)result.Properties["objectClass"])[0],"person");
\r
326 Assert.AreEqual(((ResultPropertyValueCollection)result.Properties["AdsPath"])[0],LDAPServerRoot + "cn=Barak Tsabari,ou=Human Resources,ou=people,dc=myhosting,dc=example");
\r