PageRenderTime 61ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/System.DirectoryServices/Test/System.DirectoryServices/DirectoryServicesDirectoryEntryTest.cs

https://github.com/ztfuqingvip/mono
C# | 1900 lines | 1373 code | 401 blank | 126 comment | 70 complexity | 515d56f6b0cab2ffd3007124e396e5cb MD5 | raw file
Possible License(s): GPL-2.0, Unlicense, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0
  1. //
  2. // DirectoryServicesDirectoryEntryTest.cs -
  3. // NUnit Test Cases for DirectoryServices.DirectoryEntry
  4. //
  5. // Author:
  6. // Boris Kirzner <borisk@mainsoft.com>
  7. //
  8. using NUnit.Framework;
  9. using System;
  10. using System.DirectoryServices;
  11. namespace MonoTests.System.DirectoryServices
  12. {
  13. [TestFixture]
  14. [Category ("InetAccess")]
  15. public class DirectoryServicesDirectoryEntryTest
  16. {
  17. #region Fields
  18. static TestConfiguration configuration;
  19. static DirectoryEntry de;
  20. #endregion // Fields
  21. #region SetUp and TearDown
  22. [TestFixtureSetUp]
  23. public void TestFixtureSetUp()
  24. {
  25. de = null;
  26. configuration = new TestConfiguration ();
  27. }
  28. [TestFixtureTearDown]
  29. public void TestFixtureTearDown()
  30. {
  31. if (de != null)
  32. de.Dispose ();
  33. de = null;
  34. }
  35. [SetUp]
  36. public void SetUp()
  37. {
  38. TearDown();
  39. #region Initialize basics
  40. DirectoryEntry root = new DirectoryEntry( configuration.ConnectionString,
  41. configuration.Username,
  42. configuration.Password,
  43. configuration.AuthenticationType);
  44. DirectoryEntry ouPeople = root.Children.Add("ou=people","Class");
  45. ouPeople.Properties["objectClass"].Value = "organizationalUnit";
  46. ouPeople.Properties["description"].Value = "All people in organisation";
  47. ouPeople.Properties["ou"].Value = "people";
  48. ouPeople.CommitChanges();
  49. #endregion // Initialize basics
  50. #region Human Resources
  51. DirectoryEntry ouHumanResources = ouPeople.Children.Add("ou=Human Resources","Class");
  52. ouHumanResources.Properties["objectClass"].Value = "organizationalUnit";
  53. ouHumanResources.Properties["ou"].Value = "Human Resources";
  54. ouHumanResources.CommitChanges();
  55. DirectoryEntry cnJohnSmith = ouHumanResources.Children.Add("cn=John Smith","Class");
  56. cnJohnSmith.Properties["objectClass"].Value = "organizationalRole";
  57. cnJohnSmith.Properties["cn"].Value = "John Smith";
  58. cnJohnSmith.Properties["description"].Value = "Very clever person";
  59. cnJohnSmith.Properties["ou"].Value = "Human Resources";
  60. cnJohnSmith.Properties["telephoneNumber"].Value = "1 801 555 1212";
  61. cnJohnSmith.CommitChanges();
  62. DirectoryEntry cnBarakTsabari = ouHumanResources.Children.Add("cn=Barak Tsabari","Class");
  63. ((PropertyValueCollection)cnBarakTsabari.Properties["objectClass"]).Add("person");
  64. ((PropertyValueCollection)cnBarakTsabari.Properties["objectClass"]).Add("organizationalPerson");
  65. cnBarakTsabari.Properties["cn"].Value = "Barak Tsabari";
  66. cnBarakTsabari.Properties["facsimileTelephoneNumber"].Value = "+1 906 777 8853";
  67. ((PropertyValueCollection)cnBarakTsabari.Properties["ou"]).Add("Human Resources");
  68. ((PropertyValueCollection)cnBarakTsabari.Properties["ou"]).Add("People");
  69. cnBarakTsabari.Properties["sn"].Value = "Tsabari";
  70. cnBarakTsabari.Properties["telephoneNumber"].Value = "+1 906 777 8854";
  71. cnBarakTsabari.CommitChanges();
  72. #endregion // Human Resources
  73. #region R&D
  74. DirectoryEntry ouRnD = ouPeople.Children.Add("ou=R&D","Class");
  75. ouRnD.Properties["objectClass"].Value = "organizationalUnit";
  76. ouRnD.Properties["ou"].Value = "R&D";
  77. ouRnD.CommitChanges();
  78. DirectoryEntry cnYossiCohen = ouRnD.Children.Add("cn=Yossi Cohen","Class");
  79. ((PropertyValueCollection)cnYossiCohen.Properties["objectClass"]).Add("person");
  80. ((PropertyValueCollection)cnYossiCohen.Properties["objectClass"]).Add("organizationalPerson");
  81. cnYossiCohen.Properties["cn"].Value = "Yossi Cohen";
  82. cnYossiCohen.Properties["facsimileTelephoneNumber"].Value = "+1 503 777 4498";
  83. ((PropertyValueCollection)cnYossiCohen.Properties["ou"]).Add("R&D");
  84. ((PropertyValueCollection)cnYossiCohen.Properties["ou"]).Add("People");
  85. cnYossiCohen.Properties["sn"].Value = "Cohen";
  86. cnYossiCohen.Properties["telephoneNumber"].Value = "+1 503 777 4499";
  87. cnYossiCohen.CommitChanges();
  88. DirectoryEntry cnUziCohen = ouRnD.Children.Add("cn=Uzi Cohen","Class");
  89. ((PropertyValueCollection)cnUziCohen.Properties["objectClass"]).Add("person");
  90. ((PropertyValueCollection)cnUziCohen.Properties["objectClass"]).Add("organizationalPerson");
  91. cnUziCohen.Properties["cn"].Value = "Uzi Cohen";
  92. cnUziCohen.Properties["facsimileTelephoneNumber"].Value = "+1 602 333 1234";
  93. ((PropertyValueCollection)cnUziCohen.Properties["ou"]).Add("R&D");
  94. ((PropertyValueCollection)cnUziCohen.Properties["ou"]).Add("People");
  95. cnUziCohen.Properties["sn"].Value = "Cohen";
  96. cnUziCohen.Properties["telephoneNumber"].Value = "+1 602 333 1233";
  97. cnUziCohen.CommitChanges();
  98. DirectoryEntry cnDanielCohen = ouRnD.Children.Add("cn=Daniel Cohen","Class");
  99. ((PropertyValueCollection)cnDanielCohen.Properties["objectClass"]).Add("person");
  100. ((PropertyValueCollection)cnDanielCohen.Properties["objectClass"]).Add("organizationalPerson");
  101. cnDanielCohen.Properties["cn"].Value = "Daniel Cohen";
  102. cnDanielCohen.Properties["facsimileTelephoneNumber"].Value = "+1 602 333 1235";
  103. ((PropertyValueCollection)cnDanielCohen.Properties["ou"]).Add("R&D");
  104. ((PropertyValueCollection)cnDanielCohen.Properties["ou"]).Add("People");
  105. cnDanielCohen.Properties["sn"].Value = "Cohen";
  106. cnDanielCohen.Properties["telephoneNumber"].Value = "+1 602 333 1236";
  107. cnDanielCohen.CommitChanges();
  108. DirectoryEntry cnSaraCohen = ouRnD.Children.Add("cn=Sara Cohen","Class");
  109. ((PropertyValueCollection)cnSaraCohen.Properties["objectClass"]).Add("person");
  110. ((PropertyValueCollection)cnSaraCohen.Properties["objectClass"]).Add("organizationalPerson");
  111. cnSaraCohen.Properties["cn"].Value = "Sara Cohen";
  112. cnSaraCohen.Properties["facsimileTelephoneNumber"].Value = "+1 602 333 1244";
  113. ((PropertyValueCollection)cnSaraCohen.Properties["ou"]).Add("R&D");
  114. ((PropertyValueCollection)cnSaraCohen.Properties["ou"]).Add("People");
  115. cnSaraCohen.Properties["sn"].Value = "Cohen";
  116. cnSaraCohen.Properties["telephoneNumber"].Value = "+1 602 333 1243";
  117. cnSaraCohen.CommitChanges();
  118. #endregion // R&D
  119. #region DevQA
  120. DirectoryEntry ouDevQA = ouPeople.Children.Add("ou=DevQA","Class");
  121. ouDevQA.Properties["objectClass"].Value = "organizationalUnit";
  122. ouDevQA.Properties["ou"].Value = "DevQA";
  123. ouDevQA.CommitChanges();
  124. DirectoryEntry cnDanielSmith = ouDevQA.Children.Add("cn=Daniel Smith","Class");
  125. ((PropertyValueCollection)cnDanielSmith.Properties["objectClass"]).Add("person");
  126. ((PropertyValueCollection)cnDanielSmith.Properties["objectClass"]).Add("organizationalPerson");
  127. cnDanielSmith.Properties["cn"].Value = "Daniel Smith";
  128. cnDanielSmith.Properties["facsimileTelephoneNumber"].Value = "+1 408 555 3372";
  129. cnDanielSmith.Properties["l"].Value = "Santa Clara";
  130. ((PropertyValueCollection)cnDanielSmith.Properties["ou"]).Add("DevQA");
  131. ((PropertyValueCollection)cnDanielSmith.Properties["ou"]).Add("People");
  132. cnDanielSmith.Properties["sn"].Value = "Smith";
  133. cnDanielSmith.Properties["telephoneNumber"].Value = "+1 408 555 9519";
  134. cnDanielSmith.CommitChanges();
  135. DirectoryEntry cnDanielMorgan = ouDevQA.Children.Add("cn=Daniel Morgan","Class");
  136. ((PropertyValueCollection)cnDanielMorgan.Properties["objectClass"]).Add("person");
  137. ((PropertyValueCollection)cnDanielMorgan.Properties["objectClass"]).Add("organizationalPerson");
  138. cnDanielMorgan.Properties["cn"].Value = "Daniel Morgan";
  139. cnDanielMorgan.Properties["facsimileTelephoneNumber"].Value = "+1 805 666 5645";
  140. ((PropertyValueCollection)cnDanielMorgan.Properties["ou"]).Add("DevQA");
  141. ((PropertyValueCollection)cnDanielMorgan.Properties["ou"]).Add("People");
  142. cnDanielMorgan.Properties["sn"].Value = "Morgan";
  143. cnDanielMorgan.Properties["telephoneNumber"].Value = "+1 805 666 5644";
  144. cnDanielMorgan.CommitChanges();
  145. #endregion // DevQA
  146. #region Manager
  147. DirectoryEntry cnManager = root.Children.Add("cn=Manager","Class");
  148. cnManager.Properties["objectClass"].Value = "organizationalRole";
  149. cnManager.Properties["cn"].Value = "Manager";
  150. cnManager.Properties["facsimileTelephoneNumber"].Value = "+1 602 333 1238";
  151. cnManager.CommitChanges();
  152. DirectoryEntry cnUziCohen_ = cnManager.Children.Add("cn=Uzi Cohen","Class");
  153. ((PropertyValueCollection)cnUziCohen_.Properties["objectClass"]).Add("person");
  154. ((PropertyValueCollection)cnUziCohen_.Properties["objectClass"]).Add("organizationalPerson");
  155. cnUziCohen_.Properties["cn"].Value = "Uzi Cohen";
  156. cnUziCohen_.Properties["facsimileTelephoneNumber"].Value = "+1 602 333 1234";
  157. ((PropertyValueCollection)cnUziCohen_.Properties["ou"]).Add("R&D");
  158. ((PropertyValueCollection)cnUziCohen_.Properties["ou"]).Add("People");
  159. cnUziCohen_.Properties["sn"].Value = "Cohen";
  160. cnUziCohen_.Properties["telephoneNumber"].Value = "+1 602 333 1233";
  161. cnUziCohen_.CommitChanges();
  162. #endregion // Manager
  163. cnJohnSmith.Dispose();
  164. cnBarakTsabari.Dispose();
  165. ouHumanResources.Dispose();
  166. cnUziCohen.Dispose();
  167. cnYossiCohen.Dispose();
  168. cnDanielCohen.Dispose();
  169. cnSaraCohen.Dispose();
  170. ouRnD.Dispose();
  171. cnDanielSmith.Dispose();
  172. cnDanielMorgan.Dispose();
  173. ouDevQA.Dispose();
  174. cnUziCohen_.Dispose();
  175. cnManager.Dispose();
  176. ouPeople.Dispose();
  177. root.Dispose();
  178. }
  179. [TearDown]
  180. public void TearDown()
  181. {
  182. if (de != null)
  183. de.Dispose ();
  184. de = null;
  185. using (DirectoryEntry root = new DirectoryEntry( configuration.ConnectionString,
  186. configuration.Username,
  187. configuration.Password,
  188. configuration.AuthenticationType)) {
  189. foreach(DirectoryEntry child in root.Children) {
  190. DeleteTree_DFS(child);
  191. }
  192. }
  193. }
  194. private void DeleteTree_DFS(DirectoryEntry de)
  195. {
  196. foreach(DirectoryEntry child in de.Children) {
  197. DeleteTree_DFS(child);
  198. }
  199. de.DeleteTree();
  200. de.CommitChanges();
  201. }
  202. #endregion //SetUp and TearDown
  203. #region Tests
  204. [Test]
  205. public void DirectoryEntry_DirectoryEntry()
  206. {
  207. de = new DirectoryEntry();
  208. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.None);
  209. #if !NET_2_0
  210. Assert.AreEqual (de.Password, null);
  211. #endif
  212. Assert.AreEqual(de.Path,String.Empty);
  213. Assert.AreEqual(de.UsePropertyCache,true);
  214. Assert.AreEqual(de.Username,null);
  215. }
  216. [Test]
  217. public void DirectoryEntry_DirectoryEntry_Str()
  218. {
  219. using (DirectoryEntry de = new DirectoryEntry(configuration.ConnectionString)) {
  220. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.None);
  221. Assert.AreEqual(de.Name,GetName (configuration.BaseDn));
  222. #if !NET_2_0
  223. Assert.AreEqual(de.Password,null);
  224. #endif
  225. Assert.AreEqual(de.Path,configuration.ConnectionString);
  226. Assert.AreEqual(de.SchemaClassName,"organization");
  227. Assert.AreEqual(de.UsePropertyCache,true);
  228. Assert.AreEqual(de.Username,null);
  229. }
  230. }
  231. [Test]
  232. public void DirectoryEntry_DirectoryEntry_StrStrStrAuth()
  233. {
  234. if ((configuration.AuthenticationType != AuthenticationTypes.ServerBind) &&
  235. (configuration.AuthenticationType != AuthenticationTypes.None) &&
  236. (configuration.AuthenticationType != AuthenticationTypes.Anonymous))
  237. return;
  238. #region AuthenticationTypes.Anonymous
  239. using (DirectoryEntry de = new DirectoryEntry( configuration.ConnectionString,
  240. configuration.Username,
  241. configuration.Password,
  242. AuthenticationTypes.Anonymous)){
  243. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.Anonymous);
  244. //Assert.AreEqual(de.Guid,new Guid("0b045012-1d97-4f94-9d47-87cbf6dada46"));
  245. Assert.AreEqual(de.Name,GetName (configuration.BaseDn));
  246. //Assert.AreEqual(de.NativeGuid,null);
  247. #if !NET_2_0
  248. Assert.AreEqual(de.Password,configuration.Password);
  249. #endif
  250. Assert.AreEqual(de.Path,configuration.ConnectionString);
  251. Assert.AreEqual(de.SchemaClassName,"organization");
  252. Assert.AreEqual(de.UsePropertyCache,true);
  253. Assert.AreEqual(de.Username,configuration.Username);
  254. }
  255. #endregion //AuthenticationTypes.Anonymous
  256. #region AuthenticationTypes.Delegation
  257. using (DirectoryEntry de = new DirectoryEntry(configuration.ConnectionString,
  258. configuration.Username,
  259. configuration.Password,
  260. AuthenticationTypes.Delegation)){
  261. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.Delegation);
  262. //Assert.AreEqual(de.Guid,new Guid("0b045012-1d97-4f94-9d47-87cbf6dada46"));
  263. Assert.AreEqual(de.Name,GetName (configuration.BaseDn));
  264. //Assert.AreEqual(de.NativeGuid,null);
  265. #if !NET_2_0
  266. Assert.AreEqual(de.Password,configuration.Password);
  267. #endif
  268. Assert.AreEqual(de.Path,configuration.ConnectionString);
  269. Assert.AreEqual(de.SchemaClassName,"organization");
  270. Assert.AreEqual(de.UsePropertyCache,true);
  271. Assert.AreEqual(de.Username,configuration.Username);
  272. }
  273. #endregion //AuthenticationTypes.Delegation
  274. #region AuthenticationTypes.Encryption
  275. // de = new DirectoryEntry( configuration.ConnectionString,
  276. // configuration.Username,
  277. // configuration.Password,
  278. // AuthenticationTypes.Encryption);
  279. //
  280. // Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.Encryption);
  281. // //Assert.AreEqual(de.Guid,new Guid("0b045012-1d97-4f94-9d47-87cbf6dada46"));
  282. // Assert.AreEqual(de.Name,"dc=myhosting");
  283. // //Assert.AreEqual(de.NativeGuid,null);
  284. // Assert.AreEqual(de.Password,configuration.Password);
  285. // Assert.AreEqual(de.Path,configuration.ConnectionString);
  286. // Assert.AreEqual(de.SchemaClassName,"organization");
  287. // Assert.AreEqual(de.UsePropertyCache,true);
  288. // Assert.AreEqual(de.Username,configuration.Username);
  289. #endregion //AuthenticationTypes.Encryption
  290. #region AuthenticationTypes.FastBind
  291. using (DirectoryEntry de = new DirectoryEntry(configuration.ConnectionString,
  292. configuration.Username,
  293. configuration.Password,
  294. AuthenticationTypes.FastBind)){
  295. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.FastBind);
  296. //Assert.AreEqual(de.Guid,new Guid("0b045012-1d97-4f94-9d47-87cbf6dada46"));
  297. Assert.AreEqual(de.Name,GetName (configuration.BaseDn));
  298. //Assert.AreEqual(de.NativeGuid,null);
  299. #if !NET_2_0
  300. Assert.AreEqual(de.Password,configuration.Password);
  301. #endif
  302. Assert.AreEqual(de.Path,configuration.ConnectionString);
  303. Assert.AreEqual(de.SchemaClassName,"organization");
  304. Assert.AreEqual(de.UsePropertyCache,true);
  305. Assert.AreEqual(de.Username,configuration.Username);
  306. }
  307. #endregion //AuthenticationTypes.FastBind
  308. #region AuthenticationTypes.None
  309. using (DirectoryEntry de = new DirectoryEntry(configuration.ConnectionString,
  310. configuration.Username,
  311. configuration.Password,
  312. AuthenticationTypes.None)){
  313. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.None);
  314. //Assert.AreEqual(de.Guid,new Guid("0b045012-1d97-4f94-9d47-87cbf6dada46"));
  315. Assert.AreEqual(de.Name,GetName (configuration.BaseDn));
  316. //Assert.AreEqual(de.NativeGuid,null);
  317. #if !NET_2_0
  318. Assert.AreEqual(de.Password,configuration.Password);
  319. #endif
  320. Assert.AreEqual(de.Path,configuration.ConnectionString);
  321. Assert.AreEqual(de.SchemaClassName,"organization");
  322. Assert.AreEqual(de.UsePropertyCache,true);
  323. Assert.AreEqual(de.Username,configuration.Username);
  324. }
  325. #endregion //AuthenticationTypes.None
  326. #region AuthenticationTypes.ReadonlyServer
  327. using (DirectoryEntry de = new DirectoryEntry(configuration.ConnectionString,
  328. configuration.Username,
  329. configuration.Password,
  330. AuthenticationTypes.ReadonlyServer)){
  331. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.ReadonlyServer);
  332. //Assert.AreEqual(de.Guid,new Guid("0b045012-1d97-4f94-9d47-87cbf6dada46"));
  333. Assert.AreEqual(de.Name,GetName (configuration.BaseDn));
  334. //Assert.AreEqual(de.NativeGuid,null);
  335. #if !NET_2_0
  336. Assert.AreEqual(de.Password,configuration.Password);
  337. #endif
  338. Assert.AreEqual(de.Path,configuration.ConnectionString);
  339. Assert.AreEqual(de.SchemaClassName,"organization");
  340. Assert.AreEqual(de.UsePropertyCache,true);
  341. Assert.AreEqual(de.Username,configuration.Username);
  342. }
  343. #endregion //AuthenticationTypes.ReadonlyServer
  344. #region AuthenticationTypes.Sealing
  345. using (DirectoryEntry de = new DirectoryEntry(configuration.ConnectionString,
  346. configuration.Username,
  347. configuration.Password,
  348. AuthenticationTypes.Sealing)){
  349. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.Sealing);
  350. //Assert.AreEqual(de.Guid,new Guid("0b045012-1d97-4f94-9d47-87cbf6dada46"));
  351. Assert.AreEqual(de.Name,GetName (configuration.BaseDn));
  352. //Assert.AreEqual(de.NativeGuid,null);
  353. #if !NET_2_0
  354. Assert.AreEqual(de.Password,configuration.Password);
  355. #endif
  356. Assert.AreEqual(de.Path,configuration.ConnectionString);
  357. Assert.AreEqual(de.SchemaClassName,"organization");
  358. Assert.AreEqual(de.UsePropertyCache,true);
  359. Assert.AreEqual(de.Username,configuration.Username);
  360. }
  361. #endregion //AuthenticationTypes.Sealing
  362. #region AuthenticationTypes.Secure
  363. // de = new DirectoryEntry(configuration.ConnectionString,
  364. // configuration.Username,
  365. // configuration.Password,
  366. // AuthenticationTypes.Secure);
  367. //
  368. // Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.Secure);
  369. // //Assert.AreEqual(de.Guid,new Guid("0b045012-1d97-4f94-9d47-87cbf6dada46"));
  370. // Assert.AreEqual(de.Name,"dc=myhosting");
  371. // //Assert.AreEqual(de.NativeGuid,null);
  372. // Assert.AreEqual(de.Password,configuration.Password);
  373. // Assert.AreEqual(de.Path,configuration.ConnectionString);
  374. // Assert.AreEqual(de.SchemaClassName,"organization");
  375. // Assert.AreEqual(de.UsePropertyCache,true);
  376. // Assert.AreEqual(de.Username,configuration.Username);
  377. #endregion //AuthenticationTypes.Secure
  378. #region AuthenticationTypes.SecureSocketsLayer
  379. // de = new DirectoryEntry(configuration.ConnectionString,
  380. // configuration.Username,
  381. // configuration.Password,
  382. // AuthenticationTypes.SecureSocketsLayer);
  383. //
  384. // Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.SecureSocketsLayer);
  385. // //Assert.AreEqual(de.Guid,new Guid("0b045012-1d97-4f94-9d47-87cbf6dada46"));
  386. // Assert.AreEqual(de.Name,"dc=myhosting");
  387. // //Assert.AreEqual(de.NativeGuid,null);
  388. // Assert.AreEqual(de.Password,configuration.Password);
  389. // Assert.AreEqual(de.Path,configuration.ConnectionString);
  390. // Assert.AreEqual(de.SchemaClassName,"organization");
  391. // Assert.AreEqual(de.UsePropertyCache,true);
  392. // Assert.AreEqual(de.Username,configuration.Username);
  393. #endregion //AuthenticationTypes.SecureSocketsLayer
  394. #region AuthenticationTypes.ServerBind
  395. using (DirectoryEntry de = new DirectoryEntry(configuration.ConnectionString,
  396. configuration.Username,
  397. configuration.Password,
  398. AuthenticationTypes.ServerBind)){
  399. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.ServerBind);
  400. //Assert.AreEqual(de.Guid,new Guid("0b045012-1d97-4f94-9d47-87cbf6dada46"));
  401. Assert.AreEqual(de.Name,GetName (configuration.BaseDn));
  402. //Assert.AreEqual(de.NativeGuid,null);
  403. #if !NET_2_0
  404. Assert.AreEqual(de.Password,configuration.Password);
  405. #endif
  406. Assert.AreEqual(de.Path,configuration.ConnectionString);
  407. Assert.AreEqual(de.SchemaClassName,"organization");
  408. Assert.AreEqual(de.UsePropertyCache,true);
  409. Assert.AreEqual(de.Username,configuration.Username);
  410. }
  411. #endregion //AuthenticationTypes.ServerBind
  412. #region AuthenticationTypes.Signing
  413. using (DirectoryEntry de = new DirectoryEntry(configuration.ConnectionString,
  414. configuration.Username,
  415. configuration.Password,
  416. AuthenticationTypes.Signing)){
  417. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.Signing);
  418. //Assert.AreEqual(de.Guid,new Guid("0b045012-1d97-4f94-9d47-87cbf6dada46"));
  419. Assert.AreEqual(de.Name,GetName (configuration.BaseDn));
  420. //Assert.AreEqual(de.NativeGuid,null);
  421. #if !NET_2_0
  422. Assert.AreEqual(de.Password,configuration.Password);
  423. #endif
  424. Assert.AreEqual(de.Path,configuration.ConnectionString);
  425. Assert.AreEqual(de.SchemaClassName,"organization");
  426. Assert.AreEqual(de.UsePropertyCache,true);
  427. Assert.AreEqual(de.Username,configuration.Username);
  428. }
  429. #endregion //AuthenticationTypes.Signing
  430. }
  431. [Test]
  432. public void DirectoryEntry_Dispose()
  433. {
  434. using (DirectoryEntry root = new DirectoryEntry( configuration.ConnectionString,
  435. configuration.Username,
  436. configuration.Password,
  437. configuration.AuthenticationType)){
  438. DirectoryEntry ouPeople = root.Children.Add("ou=printers","Class");
  439. ouPeople.Properties["objectClass"].Value = "organizationalUnit";
  440. ouPeople.Properties["description"].Value = "All printers in organisation";
  441. ouPeople.Properties["ou"].Value = "printers";
  442. ouPeople.CommitChanges();
  443. ouPeople.Rename("ou=anotherPrinters");
  444. ouPeople.CommitChanges();
  445. Assert.IsTrue(DirectoryEntry.Exists(configuration.ServerRoot + "ou=anotherPrinters" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn))));
  446. }
  447. }
  448. [Test]
  449. public void DirectoryEntry_AuthenticationType()
  450. {
  451. de = new DirectoryEntry();
  452. de.AuthenticationType = AuthenticationTypes.Anonymous;
  453. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.Anonymous);
  454. de.AuthenticationType = AuthenticationTypes.Delegation;
  455. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.Delegation);
  456. de.AuthenticationType = AuthenticationTypes.Encryption;
  457. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.Encryption);
  458. de.AuthenticationType = AuthenticationTypes.FastBind;
  459. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.FastBind);
  460. de.AuthenticationType = AuthenticationTypes.None;
  461. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.None);
  462. de.AuthenticationType = AuthenticationTypes.ReadonlyServer;
  463. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.ReadonlyServer);
  464. de.AuthenticationType = AuthenticationTypes.Sealing;
  465. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.Sealing);
  466. de.AuthenticationType = AuthenticationTypes.Secure;
  467. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.Secure);
  468. de.AuthenticationType = AuthenticationTypes.SecureSocketsLayer;
  469. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.SecureSocketsLayer);
  470. de.AuthenticationType = AuthenticationTypes.ServerBind;
  471. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.ServerBind);
  472. de.AuthenticationType = AuthenticationTypes.Signing;
  473. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.Signing);
  474. de = new DirectoryEntry(configuration.ConnectionString);
  475. de.AuthenticationType = AuthenticationTypes.Anonymous;
  476. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.Anonymous);
  477. de.AuthenticationType = AuthenticationTypes.Delegation;
  478. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.Delegation);
  479. de.AuthenticationType = AuthenticationTypes.Encryption;
  480. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.Encryption);
  481. de.AuthenticationType = AuthenticationTypes.FastBind;
  482. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.FastBind);
  483. de.AuthenticationType = AuthenticationTypes.None;
  484. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.None);
  485. de.AuthenticationType = AuthenticationTypes.ReadonlyServer;
  486. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.ReadonlyServer);
  487. de.AuthenticationType = AuthenticationTypes.Sealing;
  488. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.Sealing);
  489. de.AuthenticationType = AuthenticationTypes.Secure;
  490. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.Secure);
  491. de.AuthenticationType = AuthenticationTypes.SecureSocketsLayer;
  492. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.SecureSocketsLayer);
  493. de.AuthenticationType = AuthenticationTypes.ServerBind;
  494. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.ServerBind);
  495. de.AuthenticationType = AuthenticationTypes.Signing;
  496. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.Signing);
  497. de = new DirectoryEntry(configuration.ConnectionString,
  498. configuration.Username,
  499. configuration.Password,
  500. AuthenticationTypes.None);
  501. de.AuthenticationType = AuthenticationTypes.Anonymous;
  502. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.Anonymous);
  503. de.AuthenticationType = AuthenticationTypes.Delegation;
  504. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.Delegation);
  505. de.AuthenticationType = AuthenticationTypes.Encryption;
  506. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.Encryption);
  507. de.AuthenticationType = AuthenticationTypes.FastBind;
  508. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.FastBind);
  509. de.AuthenticationType = AuthenticationTypes.None;
  510. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.None);
  511. de.AuthenticationType = AuthenticationTypes.ReadonlyServer;
  512. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.ReadonlyServer);
  513. de.AuthenticationType = AuthenticationTypes.Sealing;
  514. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.Sealing);
  515. de.AuthenticationType = AuthenticationTypes.Secure;
  516. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.Secure);
  517. de.AuthenticationType = AuthenticationTypes.SecureSocketsLayer;
  518. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.SecureSocketsLayer);
  519. de.AuthenticationType = AuthenticationTypes.ServerBind;
  520. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.ServerBind);
  521. de.AuthenticationType = AuthenticationTypes.Signing;
  522. Assert.AreEqual(de.AuthenticationType,AuthenticationTypes.Signing);
  523. }
  524. [Test]
  525. public void DirectoryEntry_UsePropertyCache()
  526. {
  527. string barakTsabariDN = configuration.ServerRoot + "cn=Barak Tsabari,ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn));
  528. de = new DirectoryEntry(barakTsabariDN,
  529. configuration.Username,
  530. configuration.Password,
  531. configuration.AuthenticationType);
  532. // UsePropertyCache = true
  533. de.UsePropertyCache = true;
  534. Assert.AreEqual(de.UsePropertyCache,true);
  535. #region Check Properties
  536. // Properties changes are cached
  537. string oldTelephoneNumber = (string)de.Properties["telephoneNumber"].Value;
  538. string newTelephoneNumber = "+972-3-6572345";
  539. de.Properties["telephoneNumber"].Value = newTelephoneNumber;
  540. using (DirectoryEntry barakTsabariDE = new DirectoryEntry( barakTsabariDN,
  541. configuration.Username,
  542. configuration.Password,
  543. configuration.AuthenticationType)){
  544. Assert.AreEqual(barakTsabariDE.Properties["telephoneNumber"].Value,oldTelephoneNumber);
  545. de.CommitChanges();
  546. }
  547. using (DirectoryEntry barakTsabariDE = new DirectoryEntry(barakTsabariDN,
  548. configuration.Username,
  549. configuration.Password,
  550. configuration.AuthenticationType)){
  551. Assert.AreEqual(barakTsabariDE.Properties["telephoneNumber"].Value,newTelephoneNumber);
  552. // restore object state
  553. de.Properties["telephoneNumber"].Value = oldTelephoneNumber;
  554. de.CommitChanges();
  555. }
  556. #endregion // Check Properties
  557. #region Check DeleteTree
  558. // DeleteTree is not cached
  559. de.DeleteTree();
  560. try {
  561. using (DirectoryEntry barakTsabariDE = new DirectoryEntry(barakTsabariDN,
  562. configuration.Username,
  563. configuration.Password,
  564. configuration.AuthenticationType)){
  565. barakTsabariDE.Properties["telephoneNumber"].Value = newTelephoneNumber;
  566. barakTsabariDE.CommitChanges();
  567. Assert.Fail("Object " + barakTsabariDN + " was not deleted from server.");
  568. }
  569. }
  570. catch(AssertionException ae) {
  571. throw ae;
  572. }
  573. catch (Exception e) {
  574. // do nothing
  575. }
  576. // restore object state
  577. using (DirectoryEntry ouHumanResources = new DirectoryEntry( configuration.ServerRoot + "ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn)),
  578. configuration.Username,
  579. configuration.Password,
  580. configuration.AuthenticationType)){
  581. using (DirectoryEntry cnBarakTsabari = ouHumanResources.Children.Add("cn=Barak Tsabari","Class")){
  582. ((PropertyValueCollection)cnBarakTsabari.Properties["objectClass"]).Add("person");
  583. ((PropertyValueCollection)cnBarakTsabari.Properties["objectClass"]).Add("organizationalPerson");
  584. cnBarakTsabari.Properties["cn"].Value = "Barak Tsabari";
  585. cnBarakTsabari.Properties["facsimileTelephoneNumber"].Value = "+1 906 777 8853";
  586. ((PropertyValueCollection)cnBarakTsabari.Properties["ou"]).Add("Human Resources");
  587. ((PropertyValueCollection)cnBarakTsabari.Properties["ou"]).Add("People");
  588. cnBarakTsabari.Properties["sn"].Value = "Tsabari";
  589. cnBarakTsabari.Properties["telephoneNumber"].Value = "+1 906 777 8854";
  590. cnBarakTsabari.CommitChanges();
  591. }
  592. }
  593. #endregion // Check DeleteTree
  594. #region Check MoveTo
  595. // Move to is not cached
  596. de = new DirectoryEntry(barakTsabariDN,
  597. configuration.Username,
  598. configuration.Password,
  599. configuration.AuthenticationType);
  600. using (DirectoryEntry ouRnD = new DirectoryEntry( configuration.ServerRoot + "ou=R&D,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn)),
  601. configuration.Username,
  602. configuration.Password,
  603. configuration.AuthenticationType)){
  604. de.MoveTo(ouRnD);
  605. try {
  606. using (DirectoryEntry barakTsabariDE = new DirectoryEntry(barakTsabariDN,
  607. configuration.Username,
  608. configuration.Password,
  609. configuration.AuthenticationType)){
  610. barakTsabariDE.Properties["telephoneNumber"].Value = newTelephoneNumber;
  611. barakTsabariDE.CommitChanges();
  612. Assert.Fail("Object " + barakTsabariDN + " was not moved from old location on the server.");
  613. }
  614. }
  615. catch(AssertionException ae) {
  616. throw ae;
  617. }
  618. catch (Exception e) {
  619. // do nothing
  620. }
  621. }
  622. using (DirectoryEntry barakTsabariDE = new DirectoryEntry(configuration.ServerRoot + "cn=Barak Tsabari,ou=R&D,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn)),
  623. configuration.Username,
  624. configuration.Password,
  625. configuration.AuthenticationType)){
  626. Assert.AreEqual(barakTsabariDE.Properties["telephoneNumber"].Value,oldTelephoneNumber);
  627. }
  628. // restore object state
  629. using (DirectoryEntry ouHumanResources = new DirectoryEntry( configuration.ServerRoot + "ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn)),
  630. configuration.Username,
  631. configuration.Password,
  632. configuration.AuthenticationType)){
  633. using (DirectoryEntry barakTsabariDE = new DirectoryEntry(configuration.ServerRoot + "cn=Barak Tsabari,ou=R&D,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn)),
  634. configuration.Username,
  635. configuration.Password,
  636. configuration.AuthenticationType)){
  637. barakTsabariDE.MoveTo(ouHumanResources);
  638. barakTsabariDE.CommitChanges();
  639. }
  640. }
  641. #endregion // Check MoveTo
  642. #region Check Rename
  643. // Rename not chached
  644. de = new DirectoryEntry(barakTsabariDN,
  645. configuration.Username,
  646. configuration.Password,
  647. configuration.AuthenticationType);
  648. de.Rename("cn=MyUser");
  649. try {
  650. using (DirectoryEntry barakTsabariDE = new DirectoryEntry(barakTsabariDN,
  651. configuration.Username,
  652. configuration.Password,
  653. configuration.AuthenticationType)){
  654. barakTsabariDE.Properties["telephoneNumber"].Value = newTelephoneNumber;
  655. barakTsabariDE.CommitChanges();
  656. Assert.Fail("Object " + barakTsabariDN + " was not renamed on the server.");
  657. }
  658. }
  659. catch(AssertionException ae) {
  660. throw ae;
  661. }
  662. catch (Exception e) {
  663. // do nothing
  664. }
  665. using (DirectoryEntry barakTsabariDE = new DirectoryEntry(configuration.ServerRoot + "cn=MyUser,ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn)),
  666. configuration.Username,
  667. configuration.Password,
  668. configuration.AuthenticationType)){
  669. Assert.AreEqual(barakTsabariDE.Properties["telephoneNumber"].Value,oldTelephoneNumber);
  670. }
  671. // restore object state
  672. using (DirectoryEntry barakTsabariDE = new DirectoryEntry(configuration.ServerRoot + "cn=MyUser,ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn)),
  673. configuration.Username,
  674. configuration.Password,
  675. configuration.AuthenticationType)){
  676. barakTsabariDE.Rename("cn=Barak Tsabari");
  677. barakTsabariDE.CommitChanges();
  678. }
  679. #endregion // Check Rename
  680. // UsePropertyCache = false
  681. de = new DirectoryEntry(barakTsabariDN,
  682. configuration.Username,
  683. configuration.Password,
  684. configuration.AuthenticationType);
  685. de.UsePropertyCache = false;
  686. Assert.AreEqual(de.UsePropertyCache,false);
  687. #region Check Properties
  688. // Properties changes not cached
  689. de.Properties["telephoneNumber"].Value = newTelephoneNumber;
  690. using (DirectoryEntry barakTsabariDE = new DirectoryEntry(barakTsabariDN,
  691. configuration.Username,
  692. configuration.Password,
  693. configuration.AuthenticationType)){
  694. }
  695. //Assert.AreEqual(barakTsabariDE.Properties["telephoneNumber"].Value,newTelephoneNumber);
  696. #endregion // Check Properties
  697. #region Check DeleteTree
  698. // DeleteTree is not cached
  699. de.DeleteTree();
  700. try {
  701. using (DirectoryEntry barakTsabariDE = new DirectoryEntry(barakTsabariDN,
  702. configuration.Username,
  703. configuration.Password,
  704. configuration.AuthenticationType)){
  705. barakTsabariDE.Properties["telephoneNumber"].Value = newTelephoneNumber;
  706. barakTsabariDE.CommitChanges();
  707. Assert.Fail("Object " + barakTsabariDN + " was not deleted from server.");
  708. }
  709. }
  710. catch(AssertionException ae) {
  711. throw ae;
  712. }
  713. catch (Exception e) {
  714. // do nothing
  715. }
  716. // restore object state
  717. using (DirectoryEntry ouHumanResources = new DirectoryEntry( configuration.ServerRoot + "ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn)),
  718. configuration.Username,
  719. configuration.Password,
  720. configuration.AuthenticationType)){
  721. using (DirectoryEntry cnBarakTsabari = ouHumanResources.Children.Add("cn=Barak Tsabari","Class")){
  722. ((PropertyValueCollection)cnBarakTsabari.Properties["objectClass"]).Add("person");
  723. ((PropertyValueCollection)cnBarakTsabari.Properties["objectClass"]).Add("organizationalPerson");
  724. cnBarakTsabari.Properties["cn"].Value = "Barak Tsabari";
  725. cnBarakTsabari.Properties["facsimileTelephoneNumber"].Value = "+1 906 777 8853";
  726. ((PropertyValueCollection)cnBarakTsabari.Properties["ou"]).Add("Human Resources");
  727. ((PropertyValueCollection)cnBarakTsabari.Properties["ou"]).Add("People");
  728. cnBarakTsabari.Properties["sn"].Value = "Tsabari";
  729. cnBarakTsabari.Properties["telephoneNumber"].Value = "+1 906 777 8854";
  730. cnBarakTsabari.CommitChanges();
  731. }
  732. }
  733. #endregion // Check DeleteTree
  734. #region Check MoveTo
  735. // Move to is not cached
  736. de = new DirectoryEntry(barakTsabariDN,
  737. configuration.Username,
  738. configuration.Password,
  739. configuration.AuthenticationType);
  740. using (DirectoryEntry ouRnD = new DirectoryEntry( configuration.ServerRoot + "ou=R&D,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn)),
  741. configuration.Username,
  742. configuration.Password,
  743. configuration.AuthenticationType)){
  744. de.MoveTo(ouRnD);
  745. }
  746. try {
  747. using (DirectoryEntry barakTsabariDE = new DirectoryEntry(barakTsabariDN,
  748. configuration.Username,
  749. configuration.Password,
  750. configuration.AuthenticationType)){
  751. barakTsabariDE.Properties["telephoneNumber"].Value = newTelephoneNumber;
  752. barakTsabariDE.CommitChanges();
  753. Assert.Fail("Object " + barakTsabariDN + " was not moved from old location on the server.");
  754. }
  755. }
  756. catch(AssertionException ae) {
  757. throw ae;
  758. }
  759. catch (Exception e) {
  760. // do nothing
  761. }
  762. using (DirectoryEntry barakTsabariDE = new DirectoryEntry(configuration.ServerRoot + "cn=Barak Tsabari,ou=R&D,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn)),
  763. configuration.Username,
  764. configuration.Password,
  765. configuration.AuthenticationType)){
  766. Assert.AreEqual(barakTsabariDE.Properties["telephoneNumber"].Value,oldTelephoneNumber);
  767. }
  768. // restore object state
  769. using (DirectoryEntry ouHumanResources = new DirectoryEntry( configuration.ServerRoot + "ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn)),
  770. configuration.Username,
  771. configuration.Password,
  772. configuration.AuthenticationType)){
  773. using (DirectoryEntry barakTsabariDE = new DirectoryEntry(configuration.ServerRoot + "cn=Barak Tsabari,ou=R&D,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn)),
  774. configuration.Username,
  775. configuration.Password,
  776. configuration.AuthenticationType)){
  777. barakTsabariDE.MoveTo(ouHumanResources);
  778. barakTsabariDE.CommitChanges();
  779. }
  780. }
  781. #endregion // Check MoveTo
  782. #region Check Rename
  783. // Rename not chached
  784. de = new DirectoryEntry(barakTsabariDN,
  785. configuration.Username,
  786. configuration.Password,
  787. configuration.AuthenticationType);
  788. de.Rename("cn=MyUser");
  789. try {
  790. using (DirectoryEntry barakTsabariDE = new DirectoryEntry(barakTsabariDN,
  791. configuration.Username,
  792. configuration.Password,
  793. configuration.AuthenticationType)){
  794. barakTsabariDE.Properties["telephoneNumber"].Value = newTelephoneNumber;
  795. barakTsabariDE.CommitChanges();
  796. Assert.Fail("Object " + barakTsabariDN + " was not renamed on the server.");
  797. }
  798. }
  799. catch(AssertionException ae) {
  800. throw ae;
  801. }
  802. catch (Exception e) {
  803. // do nothing
  804. }
  805. using (DirectoryEntry barakTsabariDE = new DirectoryEntry(configuration.ServerRoot + "cn=MyUser,ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn)),
  806. configuration.Username,
  807. configuration.Password,
  808. configuration.AuthenticationType)){
  809. Assert.AreEqual(barakTsabariDE.Properties["telephoneNumber"].Value,oldTelephoneNumber);
  810. }
  811. // restore object state
  812. using (DirectoryEntry barakTsabariDE = new DirectoryEntry(configuration.ServerRoot + "cn=MyUser,ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn)),
  813. configuration.Username,
  814. configuration.Password,
  815. configuration.AuthenticationType)){
  816. barakTsabariDE.Rename("cn=Barak Tsabari");
  817. barakTsabariDE.CommitChanges();
  818. }
  819. #endregion // Check Rename
  820. }
  821. [Test]
  822. [Category("NotWorking")]
  823. public void DirectoryEntry_Children()
  824. {
  825. de = new DirectoryEntry();
  826. DirectoryEntries children = de.Children;
  827. //Assert.AreEqual(children.SchemaFilter.Count,0);
  828. de = new DirectoryEntry(configuration.ConnectionString);
  829. children = de.Children;
  830. //Assert.AreEqual(children.SchemaFilter.Count,0);
  831. int childrenCount = 0;
  832. foreach(DirectoryEntry childDe in children) {
  833. childrenCount++;
  834. }
  835. Assert.AreEqual(childrenCount,2);
  836. Assert.AreEqual(children.Find("ou=people").Name,"ou=people");
  837. Assert.AreEqual(children.Find("cn=Manager").Name,"cn=Manager");
  838. de = new DirectoryEntry(configuration.ConnectionString,
  839. configuration.Username,
  840. configuration.Password,
  841. configuration.AuthenticationType);
  842. children = de.Children;
  843. //Assert.AreEqual(children.SchemaFilter.Count,0);
  844. childrenCount = 0;
  845. foreach(DirectoryEntry childDe in children) {
  846. childrenCount++;
  847. }
  848. Assert.AreEqual(childrenCount,2);
  849. Assert.AreEqual(children.Find("ou=people").Name,"ou=people");
  850. Assert.AreEqual(children.Find("cn=Manager").Name,"cn=Manager");
  851. de = new DirectoryEntry(configuration.ServerRoot + "ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn)) ,
  852. configuration.Username,
  853. configuration.Password,
  854. configuration.AuthenticationType);
  855. children = de.Children;
  856. Assert.AreEqual(children.Find("cn=Barak Tsabari").Name,"cn=Barak Tsabari");
  857. Assert.AreEqual(children.Find("cn=John Smith").Name,"cn=John Smith");
  858. }
  859. [Test]
  860. public void DirectoryEntry_Name()
  861. {
  862. de = new DirectoryEntry(configuration.ConnectionString);
  863. de = new DirectoryEntry(configuration.ConnectionString,
  864. configuration.Username,
  865. configuration.Password,
  866. configuration.AuthenticationType);
  867. de = new DirectoryEntry(configuration.ServerRoot + "ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn)) ,
  868. configuration.Username,
  869. configuration.Password,
  870. configuration.AuthenticationType);
  871. Assert.AreEqual(de.Name,"ou=Human Resources");
  872. de = new DirectoryEntry(configuration.ServerRoot + "cn=Barak Tsabari,ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn)) ,
  873. configuration.Username,
  874. configuration.Password,
  875. configuration.AuthenticationType);
  876. Assert.AreEqual(de.Name,"cn=Barak Tsabari");
  877. }
  878. [Test]
  879. public void DirectoryEntry_Parent()
  880. {
  881. de = new DirectoryEntry(configuration.ConnectionString);
  882. // MS works only with "LDAP" while RFC2255 states "ldap"
  883. Assert.AreEqual(de.Parent.Path.ToLower(),(configuration.ServerRoot + GetParentDN (configuration.BaseDn)).ToLower());
  884. de = new DirectoryEntry(configuration.ConnectionString,
  885. configuration.Username,
  886. configuration.Password,
  887. configuration.AuthenticationType);
  888. // MS works only with "LDAP" while RFC2255 states "ldap"
  889. Assert.AreEqual(de.Parent.Path.ToLower(),(configuration.ServerRoot + GetParentDN (configuration.BaseDn)).ToLower());
  890. de = new DirectoryEntry(configuration.ServerRoot + "cn=Barak Tsabari,ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn)) ,
  891. configuration.Username,
  892. configuration.Password,
  893. configuration.AuthenticationType);
  894. // MS works only with "LDAP" while RFC2255 states "ldap"
  895. Assert.AreEqual(de.Parent.Path.ToLower(),(configuration.ServerRoot + "ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn))).ToLower());
  896. }
  897. #if !NET_2_0
  898. [Test]
  899. public void DirectoryEntry_Password()
  900. {
  901. string wrongPassword = "some wrong password";
  902. de = new DirectoryEntry();
  903. Assert.AreEqual(de.Password,null);
  904. de.Password = configuration.Password;
  905. Assert.AreEqual(de.Password,configuration.Password);
  906. de.Password = "";
  907. Assert.AreEqual(de.Password,String.Empty);
  908. de.Password = wrongPassword;
  909. Assert.AreEqual(de.Password,wrongPassword);
  910. de = new DirectoryEntry(configuration.ConnectionString);
  911. de.Password = configuration.Password;
  912. Assert.AreEqual(de.Password,configuration.Password);
  913. de.Password = "";
  914. Assert.AreEqual(de.Password,String.Empty);
  915. de.Password = wrongPassword;
  916. Assert.AreEqual(de.Password,wrongPassword);
  917. de = new DirectoryEntry(configuration.ConnectionString,
  918. configuration.Username,
  919. configuration.Password,
  920. configuration.AuthenticationType);
  921. de.Password = configuration.Password;
  922. Assert.AreEqual(de.Password,configuration.Password);
  923. de.Password = "";
  924. Assert.AreEqual(de.Password,String.Empty);
  925. de.Password = wrongPassword;
  926. Assert.AreEqual(de.Password,wrongPassword);
  927. }
  928. #endif
  929. [Test]
  930. public void DirectoryEntry_Path()
  931. {
  932. string wrongPath = "something that is not LDAP path";
  933. de = new DirectoryEntry();
  934. Assert.AreEqual(de.Path,String.Empty);
  935. de.Path = configuration.ConnectionString;
  936. Assert.AreEqual(de.Path,configuration.ConnectionString);
  937. de.Path = "";
  938. Assert.AreEqual(de.Path,String.Empty);
  939. de.Path = wrongPath;
  940. Assert.AreEqual(de.Path,wrongPath);
  941. de = new DirectoryEntry(configuration.ConnectionString);
  942. de.Path = configuration.ConnectionString;
  943. Assert.AreEqual(de.Path,configuration.ConnectionString);
  944. de.Path = "";
  945. Assert.AreEqual(de.Path,String.Empty);
  946. de.Path = wrongPath;
  947. Assert.AreEqual(de.Path,wrongPath);
  948. de = new DirectoryEntry(configuration.ConnectionString,
  949. configuration.Username,
  950. configuration.Password,
  951. configuration.AuthenticationType);
  952. de.Path = configuration.ConnectionString;
  953. Assert.AreEqual(de.Path,configuration.ConnectionString);
  954. de.Path = "";
  955. Assert.AreEqual(de.Path,String.Empty);
  956. de.Path = wrongPath;
  957. Assert.AreEqual(de.Path,wrongPath);
  958. de = new DirectoryEntry("ldap://myhost:389/ou=people",null,null,AuthenticationTypes.None);
  959. Assert.AreEqual(de.Path,"ldap://myhost:389/ou=people");
  960. de.Path = null;
  961. Assert.AreEqual(de.Path,String.Empty);
  962. }
  963. [Test]
  964. public void DirectoryEntry_Properties1()
  965. {
  966. de = new DirectoryEntry(configuration.ConnectionString);
  967. Assert.AreEqual(de.Properties.Count,3);
  968. Assert.AreEqual(((PropertyValueCollection)de.Properties["dc"]).Value,"example");
  969. Assert.AreEqual(((PropertyValueCollection)de.Properties["description"]).Value,null);
  970. de = new DirectoryEntry(configuration.ConnectionString,
  971. configuration.Username,
  972. configuration.Password,
  973. configuration.AuthenticationType);
  974. Assert.AreEqual(de.Properties.Count,3);
  975. Assert.AreEqual(((PropertyValueCollection)de.Properties["dc"]).Value,"example");
  976. Assert.AreEqual(((PropertyValueCollection)de.Properties["description"]).Value,null);
  977. // ensure that properties are not accessible after removing an entry from the server
  978. string barakTsabariDN = configuration.ServerRoot + "cn=Barak Tsabari,ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn));
  979. de = new DirectoryEntry(barakTsabariDN,
  980. configuration.Username,
  981. configuration.Password,
  982. configuration.AuthenticationType);
  983. de.DeleteTree();
  984. try {
  985. int i = de.Properties.Count;
  986. Assert.Fail("Properties should not be accessible after deleting an entry from the server");
  987. }
  988. catch(AssertionException ae) {
  989. throw ae;
  990. }
  991. catch(Exception e) {
  992. // supress exception
  993. }
  994. try {
  995. string s = (string)((PropertyValueCollection)de.Properties["dc"]).Value;
  996. Assert.Fail("Properties should not be accessible after deleting an entry from the server");
  997. }
  998. catch(AssertionException ae) {
  999. throw ae;
  1000. }
  1001. catch(Exception e) {
  1002. // supress exception
  1003. }
  1004. }
  1005. [Test]
  1006. public void DirectoryEntry_Properties2()
  1007. {
  1008. // delete entry, create a new one (the same) and access properties of the old object
  1009. string barakTsabariDN = configuration.ServerRoot + "cn=Barak Tsabari,ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn));
  1010. de = new DirectoryEntry(barakTsabariDN,
  1011. configuration.Username,
  1012. configuration.Password,
  1013. configuration.AuthenticationType);
  1014. // cause to properties loading
  1015. Assert.AreEqual(de.Properties.Count,6);
  1016. Assert.AreEqual(((PropertyValueCollection)de.Properties["sn"]).Value,"Tsabari");
  1017. // delete entry
  1018. de.DeleteTree();
  1019. // the local property chache is still accessible
  1020. Assert.AreEqual(de.Properties.Count,6);
  1021. Assert.AreEqual(((PropertyValueCollection)de.Properties["sn"]).Value,"Tsabari");
  1022. de.CommitChanges();
  1023. // the local property chache is still accessible
  1024. ((PropertyValueCollection)de.Properties["sn"]).Value = "Barbari";
  1025. // create the entry back again
  1026. using (DirectoryEntry ouHumanResources = new DirectoryEntry( configuration.ServerRoot + "ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn)),
  1027. configuration.Username,
  1028. configuration.Password,
  1029. configuration.AuthenticationType)){
  1030. using (DirectoryEntry cnBarakTsabari = ouHumanResources.Children.Add("cn=Barak Tsabari","Class")){
  1031. ((PropertyValueCollection)cnBarakTsabari.Properties["objectClass"]).Add("person");
  1032. ((PropertyValueCollection)cnBarakTsabari.Properties["objectClass"]).Add("organizationalPerson");
  1033. cnBarakTsabari.Properties["cn"].Value = "Barak Tsabari";
  1034. cnBarakTsabari.Properties["facsimileTelephoneNumber"].Value = "+1 906 777 8853";
  1035. ((PropertyValueCollection)cnBarakTsabari.Properties["ou"]).Add("Human Resources");
  1036. ((PropertyValueCollection)cnBarakTsabari.Properties["ou"]).Add("People");
  1037. cnBarakTsabari.Properties["sn"].Value = "Tsabari";
  1038. cnBarakTsabari.Properties["telephoneNumber"].Value = "+1 906 777 8854";
  1039. cnBarakTsabari.CommitChanges();
  1040. }
  1041. }
  1042. // the local property chache is still accessible
  1043. Assert.AreEqual(((PropertyValueCollection)de.Properties["sn"]).Value,"Barbari");
  1044. // Refresh from server
  1045. de.RefreshCache();
  1046. // ensure the properties of an entry are still accessible through the old object
  1047. Assert.AreEqual(((PropertyValueCollection)de.Properties["sn"]).Value,"Tsabari");
  1048. }
  1049. [Test]
  1050. [Category("NotWorking")]
  1051. public void DirectoryEntry_SchemaClassName()
  1052. {
  1053. de = new DirectoryEntry();
  1054. Assert.AreEqual(de.SchemaClassName,"domainDNS");
  1055. de = new DirectoryEntry(configuration.ConnectionString);
  1056. Assert.AreEqual(de.SchemaClassName,"organization");
  1057. de = new DirectoryEntry(configuration.ConnectionString,
  1058. configuration.Username,
  1059. configuration.Password,
  1060. configuration.AuthenticationType);
  1061. Assert.AreEqual(de.SchemaClassName,"organization");
  1062. DirectoryEntry de2 = de.Children.Add("ou=My Child","Class");
  1063. Assert.AreEqual(de2.SchemaClassName,"Class");
  1064. Assert.AreEqual(((PropertyValueCollection)de2.Properties["structuralObjectClass"]).Value,null);
  1065. }
  1066. [Test]
  1067. [Category("NotWorking")]
  1068. public void DirectoryEntry_SchemaEntry()
  1069. {
  1070. de = new DirectoryEntry();
  1071. DirectoryEntry schemaEntry = de.SchemaEntry;
  1072. // MS works only with "LDAP" while RFC2255 states "ldap"
  1073. Assert.AreEqual(schemaEntry.Path.ToLower(),"LDAP://schema/domainDNS".ToLower());
  1074. Assert.AreEqual(schemaEntry.Name,"domainDNS");
  1075. Assert.AreEqual(schemaEntry.Username,null);
  1076. #if !NET_2_0
  1077. Assert.AreEqual(schemaEntry.Password,null);
  1078. #endif
  1079. Assert.AreEqual(schemaEntry.UsePropertyCache,true);
  1080. Assert.AreEqual(schemaEntry.SchemaClassName,"Class");
  1081. Assert.AreEqual(schemaEntry.AuthenticationType,AuthenticationTypes.None);
  1082. de = new DirectoryEntry(configuration.ConnectionString);
  1083. schemaEntry = de.SchemaEntry;
  1084. Assert.AreEqual(schemaEntry.Path,configuration.ServerRoot + "schema/organization");
  1085. Assert.AreEqual(schemaEntry.Name,"organization");
  1086. Assert.AreEqual(schemaEntry.Username,null);
  1087. #if !NET_2_0
  1088. Assert.AreEqual(schemaEntry.Password,null);
  1089. #endif
  1090. Assert.AreEqual(schemaEntry.UsePropertyCache,true);
  1091. Assert.AreEqual(schemaEntry.SchemaClassName,"Class");
  1092. Assert.AreEqual(schemaEntry.AuthenticationType,AuthenticationTypes.None);
  1093. de = new DirectoryEntry(configuration.ConnectionString,
  1094. configuration.Username,
  1095. configuration.Password,
  1096. configuration.AuthenticationType);
  1097. schemaEntry = de.SchemaEntry;
  1098. Assert.AreEqual(schemaEntry.Path,configuration.ServerRoot + "schema/organization");
  1099. Assert.AreEqual(schemaEntry.Name,"organization");
  1100. Assert.AreEqual(schemaEntry.Username,configuration.Username);
  1101. #if !NET_2_0
  1102. Assert.AreEqual(schemaEntry.Password,configuration.Password);
  1103. #endif
  1104. Assert.AreEqual(schemaEntry.UsePropertyCache,true);
  1105. Assert.AreEqual(schemaEntry.SchemaClassName,"Class");
  1106. Assert.AreEqual(schemaEntry.AuthenticationType,configuration.AuthenticationType);
  1107. }
  1108. [Test]
  1109. public void DirectoryEntry_Username()
  1110. {
  1111. string wrongUsername = "some wrong username";
  1112. de = new DirectoryEntry();
  1113. Assert.AreEqual(de.Username,null);
  1114. de.Username = configuration.Username;
  1115. Assert.AreEqual(de.Username,configuration.Username);
  1116. de.Username = "";
  1117. Assert.AreEqual(de.Username,String.Empty);
  1118. de.Username = wrongUsername;
  1119. Assert.AreEqual(de.Username,wrongUsername);
  1120. de = new DirectoryEntry(configuration.ConnectionString);
  1121. de.Username = configuration.Username;
  1122. Assert.AreEqual(de.Username,configuration.Username);
  1123. de.Username = "";
  1124. Assert.AreEqual(de.Username,String.Empty);
  1125. de.Username = wrongUsername;
  1126. Assert.AreEqual(de.Username,wrongUsername);
  1127. de = new DirectoryEntry(configuration.ConnectionString,
  1128. configuration.Username,
  1129. configuration.Password,
  1130. configuration.AuthenticationType);
  1131. de.Username = configuration.Username;
  1132. Assert.AreEqual(de.Username,configuration.Username);
  1133. de.Username = "";
  1134. Assert.AreEqual(de.Username,String.Empty);
  1135. de.Username = wrongUsername;
  1136. Assert.AreEqual(de.Username,wrongUsername);
  1137. }
  1138. [Test]
  1139. public void DirectoryEntry_Close()
  1140. {
  1141. de = new DirectoryEntry();
  1142. de.Close();
  1143. de = new DirectoryEntry(configuration.ConnectionString);
  1144. de.Close();
  1145. de = new DirectoryEntry(configuration.ConnectionString,
  1146. configuration.Username,
  1147. configuration.Password,
  1148. configuration.AuthenticationType);
  1149. de.Close();
  1150. }
  1151. [Test]
  1152. public void DirectoryEntry_CommitChanges1()
  1153. {
  1154. string humanResourcesDN = configuration.ServerRoot + "ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn));
  1155. using (DirectoryEntry ouHumanResources = new DirectoryEntry( humanResourcesDN,
  1156. configuration.Username,
  1157. configuration.Password,
  1158. configuration.AuthenticationType)){
  1159. // new entry
  1160. string newEmployeeDN = configuration.ServerRoot + "cn=New Employee,ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn));
  1161. de = ouHumanResources.Children.Add("cn=New Employee","Class");
  1162. Assert.IsFalse(DirectoryEntry.Exists(newEmployeeDN));
  1163. de.Properties["objectClass"].Value = "organizationalRole";
  1164. de.Properties["cn"].Value = "New Employee";
  1165. Assert.IsFalse(DirectoryEntry.Exists(newEmployeeDN));
  1166. de.CommitChanges();
  1167. Assert.IsTrue(DirectoryEntry.Exists(newEmployeeDN));
  1168. }
  1169. // existing entry
  1170. string barakTsabariDN = configuration.ServerRoot + "cn=Barak Tsabari,ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn));
  1171. de = new DirectoryEntry(barakTsabariDN,
  1172. configuration.Username,
  1173. configuration.Password,
  1174. configuration.AuthenticationType);
  1175. string oldTelephone = (string)((PropertyValueCollection)de.Properties["telephoneNumber"]).Value;
  1176. string newTelephone = "+972 3 6078596";
  1177. // UsePropertyCache - true
  1178. de.UsePropertyCache = true;
  1179. ((PropertyValueCollection)de.Properties["telephoneNumber"]).Value = newTelephone;
  1180. Assert.AreEqual(((PropertyValueCollection)de.Properties["telephoneNumber"]).Value,newTelephone);
  1181. using (DirectoryEntry cnBarakTsabari = new DirectoryEntry( barakTsabariDN,
  1182. configuration.Username,
  1183. configuration.Password,
  1184. configuration.AuthenticationType)){
  1185. //check that on server there is still an old value
  1186. Assert.AreEqual(((PropertyValueCollection)cnBarakTsabari.Properties["telephoneNumber"]).Value,oldTelephone);
  1187. de.CommitChanges();
  1188. }
  1189. using (DirectoryEntry cnBarakTsabari = new DirectoryEntry(barakTsabariDN,
  1190. configuration.Username,
  1191. configuration.Password,
  1192. configuration.AuthenticationType)){
  1193. // check that new value is updated on the server
  1194. Assert.AreEqual(((PropertyValueCollection)cnBarakTsabari.Properties["telephoneNumber"]).Value,newTelephone);
  1195. }
  1196. // UsePropertyCache - false
  1197. de = new DirectoryEntry(barakTsabariDN,
  1198. configuration.Username,
  1199. configuration.Password,
  1200. configuration.AuthenticationType);
  1201. de.UsePropertyCache = false;
  1202. Assert.AreEqual(((PropertyValueCollection)de.Properties["telephoneNumber"]).Value,newTelephone);
  1203. ((PropertyValueCollection)de.Properties["telephoneNumber"]).Value = oldTelephone;
  1204. Assert.AreEqual(((PropertyValueCollection)de.Properties["telephoneNumber"]).Value,oldTelephone);
  1205. using (DirectoryEntry cnBarakTsabari = new DirectoryEntry(barakTsabariDN,
  1206. configuration.Username,
  1207. configuration.Password,
  1208. configuration.AuthenticationType)){
  1209. // check that new value is updated on the server
  1210. //Assert.AreEqual(((PropertyValueCollection)cnBarakTsabari.Properties["telephoneNumber"]).Value,oldTelephone);
  1211. }
  1212. de.CommitChanges(); // this should do nothing
  1213. }
  1214. [Test]
  1215. public void DirectoryEntry_CommitChanges2()
  1216. {
  1217. string barakTsabariDN = configuration.ServerRoot + "cn=Barak Tsabari,ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn));
  1218. using (DirectoryEntry barakTsabariDE1 = new DirectoryEntry(barakTsabariDN,
  1219. configuration.Username,
  1220. configuration.Password,
  1221. configuration.AuthenticationType)){
  1222. barakTsabariDE1.UsePropertyCache = true;
  1223. using (DirectoryEntry barakTsabariDE2 = new DirectoryEntry(barakTsabariDN,
  1224. configuration.Username,
  1225. configuration.Password,
  1226. configuration.AuthenticationType)){
  1227. barakTsabariDE2.UsePropertyCache = true;
  1228. string oldTelephone = (string)((PropertyValueCollection)barakTsabariDE1.Properties["telephoneNumber"]).Value;
  1229. string newTelephone = "+972 3 6078596";
  1230. string oldFacsimilieTelephoneNumber = (string)((PropertyValueCollection)barakTsabariDE1.Properties["facsimileTelephoneNumber"]).Value;
  1231. string newFacsimilieTelephoneNumber1 = "+972-3-9872365";
  1232. string newFacsimilieTelephoneNumber2 = "+972-3-9999999";
  1233. barakTsabariDE1.Properties["telephoneNumber"].Value = newTelephone;
  1234. barakTsabariDE1.Properties["facsimileTelephoneNumber"].Value = newFacsimilieTelephoneNumber1;
  1235. barakTsabariDE2.Properties["facsimileTelephoneNumber"].Value = newFacsimilieTelephoneNumber2;
  1236. // only the changed properties of each object are set
  1237. barakTsabariDE1.CommitChanges();
  1238. de = new DirectoryEntry(barakTsabariDN,
  1239. configuration.Username,
  1240. configuration.Password,
  1241. configuration.AuthenticationType);
  1242. Assert.AreEqual(de.Properties["telephoneNumber"].Value,newTelephone);
  1243. Assert.AreEqual(de.Properties["facsimileTelephoneNumber"].Value,newFacsimilieTelephoneNumber1);
  1244. barakTsabariDE2.CommitChanges();
  1245. de = new DirectoryEntry(barakTsabariDN,
  1246. configuration.Username,
  1247. configuration.Password,
  1248. configuration.AuthenticationType);
  1249. Assert.AreEqual(de.Properties["telephoneNumber"].Value,newTelephone);
  1250. Assert.AreEqual(de.Properties["facsimileTelephoneNumber"].Value,newFacsimilieTelephoneNumber2);
  1251. }
  1252. }
  1253. }
  1254. [Test]
  1255. [ExpectedException(typeof(NotImplementedException))]
  1256. public void DirectoryEntry_CopyTo()
  1257. {
  1258. string barakTsabariDN = configuration.ServerRoot + "cn=Barak Tsabari,ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn));
  1259. de = new DirectoryEntry(configuration.ConnectionString,
  1260. configuration.Username,
  1261. configuration.Password,
  1262. configuration.AuthenticationType);
  1263. using (DirectoryEntry cnBarakTsabari = new DirectoryEntry( barakTsabariDN,
  1264. configuration.Username,
  1265. configuration.Password,
  1266. configuration.AuthenticationType)){
  1267. cnBarakTsabari.CopyTo(de);
  1268. }
  1269. }
  1270. [Test]
  1271. public void DirectoryEntry_DeleteTree()
  1272. {
  1273. string barakTsabariDN = configuration.ServerRoot + "cn=Barak Tsabari,ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn));
  1274. Assert.IsTrue(DirectoryEntry.Exists(barakTsabariDN));
  1275. de = new DirectoryEntry(barakTsabariDN,
  1276. configuration.Username,
  1277. configuration.Password,
  1278. configuration.AuthenticationType);
  1279. // no properties changed
  1280. de.DeleteTree();
  1281. de.CommitChanges();
  1282. Assert.IsFalse(DirectoryEntry.Exists(barakTsabariDN));
  1283. string johnSmithDN = configuration.ServerRoot + "cn=John Smith,ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn));
  1284. Assert.IsTrue(DirectoryEntry.Exists(johnSmithDN));
  1285. de = new DirectoryEntry(johnSmithDN,
  1286. configuration.Username,
  1287. configuration.Password,
  1288. configuration.AuthenticationType);
  1289. de.Properties["telephoneNumber"].Value = "+972 3 9999999";
  1290. // some properties changed
  1291. de.DeleteTree();
  1292. try {
  1293. de.CommitChanges();
  1294. Assert.Fail("Object " + johnSmithDN + " was not deleted from server");
  1295. }
  1296. catch(AssertionException ae) {
  1297. throw ae;
  1298. }
  1299. catch(Exception e) {
  1300. // do nothing
  1301. }
  1302. }
  1303. [Test]
  1304. public void DirectoryEntry_DeleteTree2()
  1305. {
  1306. string johnSmithDN = configuration.ServerRoot + "cn=John Smith,ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn));
  1307. Assert.IsTrue(DirectoryEntry.Exists(johnSmithDN));
  1308. // two objects refer to the same entry
  1309. de = new DirectoryEntry(johnSmithDN,
  1310. configuration.Username,
  1311. configuration.Password,
  1312. configuration.AuthenticationType);
  1313. using (DirectoryEntry johnSmithDE = new DirectoryEntry(johnSmithDN,
  1314. configuration.Username,
  1315. configuration.Password,
  1316. configuration.AuthenticationType)){
  1317. johnSmithDE.Properties["telephoneNumber"].Value = "+972 3 9999999";
  1318. // check that the second entry is not accessible after the first is deleted
  1319. de.DeleteTree();
  1320. de.CommitChanges();
  1321. try {
  1322. johnSmithDE.CommitChanges();
  1323. Assert.Fail("Object " + johnSmithDN + " should not be accessible");
  1324. }
  1325. catch(AssertionException ae) {
  1326. throw ae;
  1327. }
  1328. catch(Exception e) {
  1329. // do nothing
  1330. }
  1331. }
  1332. }
  1333. [Test]
  1334. public void DirectoryEntry_Exists()
  1335. {
  1336. string barakTsabariDN = configuration.ServerRoot + "cn=Barak Tsabari,ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn));
  1337. string johnSmithDN = configuration.ServerRoot + "cn=John Smith,ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn));
  1338. string humanResourcesOU = configuration.ServerRoot + "ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn));
  1339. Assert.IsTrue(DirectoryEntry.Exists(barakTsabariDN));
  1340. Assert.IsTrue(DirectoryEntry.Exists(johnSmithDN));
  1341. Assert.IsTrue(DirectoryEntry.Exists(humanResourcesOU));
  1342. Assert.IsFalse(DirectoryEntry.Exists(barakTsabariDN + ",dc=mono"));
  1343. }
  1344. [Test]
  1345. public void DirectoryEntry_MoveTo_De()
  1346. {
  1347. string barakTsabariHumanResourcesDN = configuration.ServerRoot + "cn=Barak Tsabari,ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn));
  1348. string barakTsabariDevQaDN = configuration.ServerRoot + "cn=Barak Tsabari,ou=DevQA,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn));
  1349. using (DirectoryEntry barakTsabariDE = new DirectoryEntry( barakTsabariHumanResourcesDN,
  1350. configuration.Username,
  1351. configuration.Password,
  1352. configuration.AuthenticationType)){
  1353. string devQaOU = configuration.ServerRoot + "ou=DevQA,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn));
  1354. using (DirectoryEntry devQaDE = new DirectoryEntry(devQaOU,
  1355. configuration.Username,
  1356. configuration.Password,
  1357. configuration.AuthenticationType)){
  1358. barakTsabariDE.MoveTo(devQaDE);
  1359. barakTsabariDE.CommitChanges();
  1360. Assert.IsTrue(DirectoryEntry.Exists(barakTsabariDevQaDN));
  1361. string humanRwsourcesOU = configuration.ServerRoot + "ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn));
  1362. using (DirectoryEntry humanResourcesDE = new DirectoryEntry( humanRwsourcesOU,
  1363. configuration.Username,
  1364. configuration.Password,
  1365. configuration.AuthenticationType)){
  1366. barakTsabariDE.MoveTo(humanResourcesDE);
  1367. barakTsabariDE.CommitChanges();
  1368. Assert.IsTrue(DirectoryEntry.Exists(barakTsabariHumanResourcesDN));
  1369. }
  1370. }
  1371. }
  1372. }
  1373. [Test]
  1374. public void DirectoryEntry_MoveTo_DeStr()
  1375. {
  1376. string barakTsabariHumanResourcesDN = configuration.ServerRoot + "cn=Barak Tsabari,ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn));
  1377. string barakTsabariDevQaDN = configuration.ServerRoot + "cn=My Name,ou=DevQA,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn));
  1378. using (DirectoryEntry barakTsabariDE = new DirectoryEntry( barakTsabariHumanResourcesDN,
  1379. configuration.Username,
  1380. configuration.Password,
  1381. configuration.AuthenticationType)){
  1382. string devQaOU = configuration.ServerRoot + "ou=DevQA,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn));
  1383. using (DirectoryEntry devQaDE = new DirectoryEntry(devQaOU,
  1384. configuration.Username,
  1385. configuration.Password,
  1386. configuration.AuthenticationType)){
  1387. barakTsabariDE.MoveTo(devQaDE,"cn=My Name");
  1388. barakTsabariDE.CommitChanges();
  1389. Assert.IsTrue(DirectoryEntry.Exists(barakTsabariDevQaDN));
  1390. string humanRwsourcesOU = configuration.ServerRoot + "ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn));
  1391. using (DirectoryEntry humanResourcesDE = new DirectoryEntry( humanRwsourcesOU,
  1392. configuration.Username,
  1393. configuration.Password,
  1394. configuration.AuthenticationType)){
  1395. barakTsabariDE.MoveTo(humanResourcesDE,"cn=Barak Tsabari");
  1396. barakTsabariDE.CommitChanges();
  1397. Assert.IsTrue(DirectoryEntry.Exists(barakTsabariHumanResourcesDN));
  1398. }
  1399. }
  1400. }
  1401. }
  1402. [Test]
  1403. public void DirectoryEntry_RefreshCache()
  1404. {
  1405. de = new DirectoryEntry(configuration.ConnectionString);
  1406. de.UsePropertyCache = true;
  1407. string newValue = "Just a company";
  1408. string oldValue = (string)((PropertyValueCollection)de.Properties["description"]).Value;
  1409. ((PropertyValueCollection)de.Properties["description"]).Value = newValue;
  1410. Assert.AreEqual(((PropertyValueCollection)de.Properties["description"]).Value,newValue);
  1411. de.RefreshCache();
  1412. Assert.AreEqual(((PropertyValueCollection)de.Properties["description"]).Value,oldValue);
  1413. // call RefeshCache on new entry prior to submitting it to the server shoud fail
  1414. string newEmployeeDN = configuration.ServerRoot + "cn=New Employee,ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn));
  1415. string humanResourcesDN = configuration.ServerRoot + "ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn));
  1416. using (DirectoryEntry humanResourcesDE = new DirectoryEntry( humanResourcesDN,
  1417. configuration.Username,
  1418. configuration.Password,
  1419. configuration.AuthenticationType)){
  1420. using (DirectoryEntry newEmployeeDE = humanResourcesDE.Children.Add("cn=New Employee","Class")){
  1421. Assert.AreEqual(newEmployeeDE.Properties["cn"].Value,null);
  1422. ((PropertyValueCollection)newEmployeeDE.Properties["objectClass"]).Add("person");
  1423. ((PropertyValueCollection)newEmployeeDE.Properties["objectClass"]).Add("organizationalPerson");
  1424. newEmployeeDE.Properties["cn"].Value = "New Employee";
  1425. newEmployeeDE.Properties["sn"].Value = "Employee";
  1426. newEmployeeDE.Properties["ou"].Value = "Human Resources";
  1427. Assert.AreEqual(newEmployeeDE.Properties["cn"].Value,"New Employee");
  1428. try {
  1429. newEmployeeDE.RefreshCache();
  1430. Assert.Fail("Call to RefreshCache did not fail");
  1431. }
  1432. catch(AssertionException ae) {
  1433. throw ae;
  1434. }
  1435. catch (Exception e) {
  1436. // supress exception
  1437. }
  1438. Assert.AreEqual(newEmployeeDE.Properties["cn"].Value,"New Employee");
  1439. newEmployeeDE.CommitChanges();
  1440. // now it should work without any problem
  1441. newEmployeeDE.RefreshCache();
  1442. Assert.AreEqual(newEmployeeDE.Properties["cn"].Value,"New Employee");
  1443. }
  1444. }
  1445. }
  1446. [Test]
  1447. public void DirectoryEntry_RefreshCache_StrArr()
  1448. {
  1449. de = new DirectoryEntry(configuration.ServerRoot + "cn=Uzi Cohen,cn=Manager" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn)));
  1450. de.UsePropertyCache = true;
  1451. string[] newValues = new string [] { "Just a manager", "Levi" };
  1452. string[] oldValues = new string [2];
  1453. oldValues [0] = (string)((PropertyValueCollection)de.Properties["description"]).Value;
  1454. oldValues [1] = (string)((PropertyValueCollection)de.Properties["sn"]).Value;
  1455. ((PropertyValueCollection)de.Properties["description"]).Value = newValues [0];
  1456. ((PropertyValueCollection)de.Properties["sn"]).Value = newValues [1];
  1457. Assert.AreEqual(((PropertyValueCollection)de.Properties["description"]).Value,newValues [0]);
  1458. Assert.AreEqual(((PropertyValueCollection)de.Properties["sn"]).Value,newValues [1]);
  1459. de.RefreshCache(new string[] {"cn"});
  1460. Assert.AreEqual(((PropertyValueCollection)de.Properties["description"]).Value,newValues [0]);
  1461. Assert.AreEqual(((PropertyValueCollection)de.Properties["sn"]).Value,newValues [1]);
  1462. de.RefreshCache(new string[] {"description","sn"});
  1463. Assert.AreEqual(((PropertyValueCollection)de.Properties["description"]).Value,newValues [0]);
  1464. Assert.AreEqual(((PropertyValueCollection)de.Properties["sn"]).Value,oldValues [1]);
  1465. }
  1466. [Test]
  1467. public void DirectoryEntry_Rename()
  1468. {
  1469. string barakTsabariOldDN = configuration.ServerRoot + "cn=Barak Tsabari,ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn));
  1470. string barakTsabariNewDN = configuration.ServerRoot + "cn=My Name,ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn));
  1471. using (DirectoryEntry barakTsabariDE = new DirectoryEntry( barakTsabariOldDN,
  1472. configuration.Username,
  1473. configuration.Password,
  1474. configuration.AuthenticationType)){
  1475. barakTsabariDE.Rename("cn=My Name");
  1476. barakTsabariDE.CommitChanges();
  1477. Assert.IsTrue(DirectoryEntry.Exists(barakTsabariNewDN));
  1478. barakTsabariDE.Rename("cn=Barak Tsabari");
  1479. barakTsabariDE.CommitChanges();
  1480. Assert.IsTrue(DirectoryEntry.Exists(barakTsabariOldDN));
  1481. }
  1482. }
  1483. #endregion Tests
  1484. private static string GetName (string baseDn)
  1485. {
  1486. if (baseDn == null || baseDn.Length == 0 || baseDn.IndexOf (',') == -1)
  1487. return baseDn;
  1488. int index = baseDn.IndexOf (',');
  1489. return baseDn.Substring (0, index);
  1490. }
  1491. private static string GetParentDN (string baseDn)
  1492. {
  1493. if (baseDn == null || baseDn.Length == 0 || baseDn.IndexOf (',') == -1)
  1494. return String.Empty;
  1495. int index = baseDn.IndexOf (',');
  1496. return baseDn.Substring (index + 1,baseDn.Length - index - 1);
  1497. }
  1498. }
  1499. }