Skip to content

Commit

Permalink
Add more DirectoryServices tests - 3 (dotnet/corefx#24533)
Browse files Browse the repository at this point in the history
* 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 dotnet/corefx@2955f94
  • Loading branch information
tarekgh authored Oct 11, 2017
1 parent 2fe8886 commit 47eab49
Showing 1 changed file with 240 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -303,6 +304,245 @@ public void TestDeleteTree()
}
}

[ConditionalFact(nameof(IsLdapConfigurationExist))]
public void TestInvalidServerPath()
{
using (DirectoryEntry de = new DirectoryEntry("SomeWrongPath"))
{
Assert.Throws<COMException>(() => { foreach (var e in de.Children) {} } );
using (DirectorySearcher ds = new DirectorySearcher(de))
{
ds.Filter = $"(objectClass=*)";
Assert.Throws<COMException>(() => 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<DirectoryServicesCOMException>(() => 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<DirectoryServicesCOMException>(exception);
else
Assert.IsType<COMException>(exception);
}

private DirectoryEntry CreateOU(DirectoryEntry de, string ou, string description)
{
DirectoryEntry ouCoreDevs = de.Children.Add($"ou={ou}","Class");
Expand Down

0 comments on commit 47eab49

Please sign in to comment.