From 47eab497c402a715c209803202364451e483e68b Mon Sep 17 00:00:00 2001 From: Tarek Mahmoud Sayed Date: Tue, 10 Oct 2017 17:42:11 -0700 Subject: [PATCH] Add more DirectoryServices tests - 3 (dotnet/corefx#24533) * Add more DirectoryServices tests - 3 This is adding some search and negative tests * Split the negative test method to smaller methods * Add using different attribute types test Commit migrated from https://github.com/dotnet/corefx/commit/2955f94d8f6bd2df65d8bce941012f5b9338a870 --- .../DirectoryServicesTests.cs | 240 ++++++++++++++++++ 1 file changed, 240 insertions(+) diff --git a/src/libraries/System.DirectoryServices/tests/System/DirectoryServices/DirectoryServicesTests.cs b/src/libraries/System.DirectoryServices/tests/System/DirectoryServices/DirectoryServicesTests.cs index d2ac4cf95177b..6aa250c852e53 100644 --- a/src/libraries/System.DirectoryServices/tests/System/DirectoryServices/DirectoryServicesTests.cs +++ b/src/libraries/System.DirectoryServices/tests/System/DirectoryServices/DirectoryServicesTests.cs @@ -12,6 +12,7 @@ namespace System.DirectoryServices.Tests public class DirectoryServicesTests { internal static bool IsLdapConfigurationExist => LdapConfiguration.Configuration != null; + internal static bool IsActiveDirectoryServer => IsLdapConfigurationExist && LdapConfiguration.Configuration.IsActiveDirectoryServer; [ConditionalFact(nameof(IsLdapConfigurationExist))] public void TestOU() // adding and removing organization unit @@ -303,6 +304,245 @@ public void TestDeleteTree() } } + [ConditionalFact(nameof(IsLdapConfigurationExist))] + public void TestInvalidServerPath() + { + using (DirectoryEntry de = new DirectoryEntry("SomeWrongPath")) + { + Assert.Throws(() => { foreach (var e in de.Children) {} } ); + using (DirectorySearcher ds = new DirectorySearcher(de)) + { + ds.Filter = $"(objectClass=*)"; + Assert.Throws(() => ds.FindAll() ); + } + } + } + + [ConditionalFact(nameof(IsLdapConfigurationExist))] + public void TestMissingUserAndPasswordInfo() + { + using (DirectoryEntry de = new DirectoryEntry(LdapConfiguration.Configuration.LdapPath)) + { + CheckSpecificException(() => { foreach (var e in de.Children) {} } ); + using (DirectorySearcher ds = new DirectorySearcher(de)) + { + ds.Filter = $"(objectClass=*)"; + CheckSpecificException(() => ds.FindAll()); + } + } + } + + [ConditionalFact(nameof(IsLdapConfigurationExist))] + public void TestInvalidUserAndPassword() + { + using (DirectoryEntry de = new DirectoryEntry(LdapConfiguration.Configuration.LdapPath, "wrongUser", "wrongPassword")) + { + CheckSpecificException(() => { foreach (var e in de.Children) {} }); + using (DirectorySearcher ds = new DirectorySearcher(de)) + { + ds.Filter = $"(objectClass=*)"; + CheckSpecificException(() => ds.FindAll() ); + } + } + + using (DirectoryEntry de = new DirectoryEntry( + LdapConfiguration.Configuration.LdapPath, + LdapConfiguration.Configuration.UserName, + "wrongPassword" + )) + { + CheckSpecificException(() => { foreach (var e in de.Children) {} } ); + using (DirectorySearcher ds = new DirectorySearcher(de)) + { + ds.Filter = $"(objectClass=*)"; + CheckSpecificException(() => ds.FindAll() ); + } + } + } + + [ConditionalFact(nameof(IsLdapConfigurationExist))] + public void TestInvalidSearchFilter() + { + using (DirectoryEntry de = new DirectoryEntry( + LdapConfiguration.Configuration.LdapPath, + LdapConfiguration.Configuration.UserName, + "wrongPassword" + )) + { + using (DirectorySearcher ds = new DirectorySearcher(de)) + { + ds.Filter = $"(objectClass=*))"; // invalid search filter + CheckSpecificException(() => ds.FindOne() ); + } + } + } + + [ConditionalFact(nameof(IsLdapConfigurationExist))] + public void TestUnAllowedProperty() + { + using (DirectoryEntry de = CreateRootEntry()) + { + DeleteOU(de, "NegativeRoot"); + + try + { + using (DirectoryEntry rootOU = CreateOU(de, "NegativeRoot", "Negative Test Root OU")) + { + DirectoryEntry entry = rootOU.Children.Add($"cn=MyNamedObject","Class"); + entry.Properties["objectClass"].Value = "namedObject"; + entry.Properties["cn"].Value = "MyNamedObject"; + entry.Properties["description"].Value = "description"; // description is not allowed in the schema + Assert.Throws(() => entry.CommitChanges()); + } + } + finally + { + DeleteOU(de, "NegativeRoot"); + } + } + } + + [ConditionalFact(nameof(IsLdapConfigurationExist))] + public void TestSearch() + { + using (DirectoryEntry de = CreateRootEntry()) + { + DeleteOU(de, "SearchRoot"); + + try + { + using (DirectoryEntry rootOU = CreateOU(de, "SearchRoot", "Root OU")) + using (DirectoryEntry childOU = CreateOU(rootOU, "Search.Child1", "Root Child 1 OU")) + using (DirectoryEntry anotherChildOU = CreateOU(rootOU, "Search.Child2", "Root Child 2 OU")) + using (DirectoryEntry grandChildOU = CreateOU(childOU, "Search.GrandChild", "Grand Child OU")) + using (DirectoryEntry user1 = CreateOrganizationalRole(grandChildOU, "user.search.grandChild.1", "Grand Child User", "1 111 111 1111")) + using (DirectoryEntry user2 = CreateOrganizationalRole(grandChildOU, "user.search.grandChild.2", "Grand Child User", "1 222 222 2222")) + { + user1.Properties["postalCode"].Value = 98052; + user1.Properties["postalAddress"].Value = "12345 SE 1st Street, City1, State1"; + user1.CommitChanges(); + + user2.Properties["postalCode"].Value = 98088; + user2.Properties["postalAddress"].Value = "67890 SE 2nd Street, City2, State2"; + user2.CommitChanges(); + + using (DirectorySearcher ds = new DirectorySearcher(rootOU)) + { + ds.ClientTimeout = new TimeSpan(0, 2, 0); + ds.Filter = "(objectClass=organizationalUnit)"; + Assert.Equal(4, ds.FindAll().Count); + + ds.Filter = "(objectClass=organizationalRole)"; + Assert.Equal(2, ds.FindAll().Count); + + ds.Filter = "(ou=SearchRoot)"; + Assert.Equal(1, ds.FindAll().Count); + + ds.Filter = "(ou=Search.Child1)"; + Assert.Equal(1, ds.FindAll().Count); + + ds.Filter = "(ou=Search.Child2)"; + Assert.Equal(1, ds.FindAll().Count); + + ds.Filter = "(ou=Search.GrandChild)"; + Assert.Equal(1, ds.FindAll().Count); + + ds.Filter = "(description=Grand Child OU)"; + Assert.Equal(1, ds.FindAll().Count); + + ds.Filter = "(description=*)"; + Assert.Equal(6, ds.FindAll().Count); + + ds.Filter = "(&(description=*)(objectClass=organizationalUnit))"; + Assert.Equal(4, ds.FindAll().Count); + + ds.Filter = "(&(description=*)(objectClass=organizationalRole))"; + Assert.Equal(2, ds.FindAll().Count); + + ds.Filter = "(&(description=No Description)(objectClass=organizationalRole))"; + Assert.Equal(0, ds.FindAll().Count); + + ds.Filter = "(postalCode=*)"; + Assert.Equal(2, ds.FindAll().Count); + + ds.Filter = "(postalCode=98052)"; + Assert.Equal(1, ds.FindAll().Count); + SearchResult sr = ds.FindOne(); + Assert.Equal("98052", sr.Properties["postalCode"][0]); + + ds.Filter = "(postalCode=98088)"; + Assert.Equal(1, ds.FindAll().Count); + sr = ds.FindOne(); + Assert.Equal("98088", sr.Properties["postalCode"][0]); + } + } + } + finally + { + DeleteOU(de, "SearchRoot"); + } + } + } + + [ConditionalFact(nameof(IsLdapConfigurationExist))] + public void TestAttributesWithDifferentTypes() + { + // Windows server looks not supporting extensibleObject. + // It throw exception with the message "The specified directory service attribute or value does not exist." + if (IsActiveDirectoryServer) + return; + + using (DirectoryEntry de = CreateRootEntry()) + { + DeleteOU(de, "AttributesRoot"); + + try + { + using (DirectoryEntry rootOU = CreateOU(de, "AttributesRoot", "Attributes Root OU")) + { + DirectoryEntry cnEntry = rootOU.Children.Add($"cn=CustomUser","Class"); + cnEntry.Properties["objectClass"].Value = new string [] { "extensibleObject", "organizationalRole" }; + cnEntry.Properties["cn"].Value = "CustomUser"; + cnEntry.Properties["description"].Value = "Some Custom User"; + cnEntry.Properties["telephoneNumber"].Value = "1 111 111 11111"; + cnEntry.Properties["changeNumber"].Value = 101; + cnEntry.Properties["deleteOldRDN"].Value = true; + cnEntry.CommitChanges(); + + using (DirectorySearcher ds = new DirectorySearcher(de)) + { + ds.ClientTimeout = new TimeSpan(0, 2, 0); + ds.Filter = $"(cn=CustomUser)"; + SearchResult sr = ds.FindOne(); + if (sr == null) + { + throw new DirectoryServicesCOMException($"Couldn't find CustomUser in the entry {de.Name}"); + } + Assert.Equal(true, sr.Properties["deleteOldRDN"][0]); + Assert.Equal(101, sr.Properties["changeNumber"][0]); + Assert.Equal("Some Custom User", sr.Properties["description"][0]); + Assert.Equal("1 111 111 11111", sr.Properties["telephoneNumber"][0]); + } + } + } + finally + { + DeleteOU(de, "AttributesRoot"); + } + } + } + + private void CheckSpecificException(Action blockToExecute) + { + Exception exception = Record.Exception(blockToExecute); + Assert.NotNull(exception); + + if (IsActiveDirectoryServer) + Assert.IsType(exception); + else + Assert.IsType(exception); + } + private DirectoryEntry CreateOU(DirectoryEntry de, string ou, string description) { DirectoryEntry ouCoreDevs = de.Children.Add($"ou={ou}","Class");