Skip to content

Commit

Permalink
Account port number already included within server string (#92748)
Browse files Browse the repository at this point in the history
* Account port number already included within server string

* Refactor the test

* Apply feedback
  • Loading branch information
buyaa-n authored Sep 28, 2023
1 parent 8757581 commit 876f763
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ private int InternalConnectToServer()
}
temp.Append(scheme);
temp.Append(servers[i]);
temp.Append(':');
temp.Append(directoryIdentifier.PortNumber);
if (!servers[i].Contains(':'))
{
temp.Append(':');
temp.Append(directoryIdentifier.PortNumber);
}
}
if (temp.Length != 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ namespace System.DirectoryServices.Protocols.Tests
{
public partial class DirectoryServicesProtocolsTests
{
internal static bool IsLdapConfigurationExist => LdapConfiguration.Configuration != null;
internal static bool IsActiveDirectoryServer => IsLdapConfigurationExist && LdapConfiguration.Configuration.IsActiveDirectoryServer;
internal static bool LdapConfigurationExists => LdapConfiguration.Configuration != null;
internal static bool IsActiveDirectoryServer => LdapConfigurationExists && LdapConfiguration.Configuration.IsActiveDirectoryServer;

internal static bool IsServerSideSortSupported => IsLdapConfigurationExist && LdapConfiguration.Configuration.SupportsServerSideSort;
internal static bool IsServerSideSortSupported => LdapConfigurationExists && LdapConfiguration.Configuration.SupportsServerSideSort;

[ConditionalFact(nameof(IsLdapConfigurationExist))]
[ConditionalFact(nameof(LdapConfigurationExists))]
public void TestInvalidFilter()
{
using LdapConnection connection = GetConnection();
Expand All @@ -33,7 +33,7 @@ public void TestInvalidFilter()
Assert.Equal(/* LdapError.FilterError */ 0x57, ex.ErrorCode);
}

[ConditionalFact(nameof(IsLdapConfigurationExist))]
[ConditionalFact(nameof(LdapConfigurationExists))]
public void TestInvalidSearchDn()
{
using LdapConnection connection = GetConnection();
Expand All @@ -47,7 +47,7 @@ public void TestInvalidSearchDn()
Assert.Equal(ResultCode.InvalidDNSyntax, ex.Response.ResultCode);
}

[ConditionalFact(nameof(IsLdapConfigurationExist))]
[ConditionalFact(nameof(LdapConfigurationExists))]
public void TestUnavailableCriticalExtension()
{
using LdapConnection connection = GetConnection();
Expand All @@ -63,7 +63,7 @@ public void TestUnavailableCriticalExtension()
Assert.Equal(ResultCode.UnavailableCriticalExtension, ex.Response.ResultCode);
}

[ConditionalFact(nameof(IsLdapConfigurationExist))]
[ConditionalFact(nameof(LdapConfigurationExists))]
public void TestUnavailableNonCriticalExtension()
{
using LdapConnection connection = GetConnection();
Expand All @@ -74,10 +74,22 @@ public void TestUnavailableNonCriticalExtension()
_ = (SearchResponse) connection.SendRequest(searchRequest);
// Does not throw
}

[ConditionalFact(nameof(LdapConfigurationExists))]
public void TestServerWithPortNumber()
{
using LdapConnection connection = GetConnection($"{LdapConfiguration.Configuration.ServerName}:{LdapConfiguration.Configuration.Port}");

var searchRequest = new SearchRequest(LdapConfiguration.Configuration.SearchDn, "(objectClass=*)", SearchScope.Subtree);

_ = (SearchResponse)connection.SendRequest(searchRequest);
// Shall succeed
}

[InlineData(60)]
[InlineData(0)]
[InlineData(-60)]
[ConditionalTheory(nameof(IsLdapConfigurationExist))]
[ConditionalTheory(nameof(LdapConfigurationExists))]
public void TestSearchWithTimeLimit(int timeLimit)
{
using LdapConnection connection = GetConnection();
Expand All @@ -95,7 +107,7 @@ public void TestSearchWithTimeLimit(int timeLimit)
}
}

[ConditionalFact(nameof(IsLdapConfigurationExist))]
[ConditionalFact(nameof(LdapConfigurationExists))]
public void TestAddingOU()
{
using (LdapConnection connection = GetConnection())
Expand All @@ -117,7 +129,7 @@ public void TestAddingOU()
}
}

[ConditionalFact(nameof(IsLdapConfigurationExist))]
[ConditionalFact(nameof(LdapConfigurationExists))]
public void TestDeleteOU()
{
using (LdapConnection connection = GetConnection())
Expand All @@ -142,7 +154,7 @@ public void TestDeleteOU()
}
}

[ConditionalFact(nameof(IsLdapConfigurationExist))]
[ConditionalFact(nameof(LdapConfigurationExists))]
public void TestAddAndModifyAttribute()
{
using (LdapConnection connection = GetConnection())
Expand Down Expand Up @@ -177,7 +189,7 @@ public void TestAddAndModifyAttribute()
}
}

[ConditionalFact(nameof(IsLdapConfigurationExist))]
[ConditionalFact(nameof(LdapConfigurationExists))]
public void TestNestedOUs()
{
using (LdapConnection connection = GetConnection())
Expand Down Expand Up @@ -208,7 +220,7 @@ public void TestNestedOUs()
}
}

[ConditionalFact(nameof(IsLdapConfigurationExist))]
[ConditionalFact(nameof(LdapConfigurationExists))]
public void TestAddUser()
{
using (LdapConnection connection = GetConnection())
Expand Down Expand Up @@ -260,7 +272,7 @@ public void TestAddUser()
}
}

[ConditionalFact(nameof(IsLdapConfigurationExist))]
[ConditionalFact(nameof(LdapConfigurationExists))]
public void TestAddingMultipleAttributes()
{
using (LdapConnection connection = GetConnection())
Expand Down Expand Up @@ -343,7 +355,7 @@ public void TestAddingMultipleAttributes()
}
}

[ConditionalFact(nameof(IsLdapConfigurationExist))]
[ConditionalFact(nameof(LdapConfigurationExists))]
public void TestMoveAndRenameUser()
{
using (LdapConnection connection = GetConnection())
Expand Down Expand Up @@ -402,7 +414,7 @@ public void TestMoveAndRenameUser()
}
}

[ConditionalFact(nameof(IsLdapConfigurationExist))]
[ConditionalFact(nameof(LdapConfigurationExists))]
public void TestAsyncSearch()
{
using (LdapConnection connection = GetConnection())
Expand Down Expand Up @@ -513,7 +525,7 @@ public static IEnumerable<object[]> TestCompareRequestTheory_TestData()
yield return new object[] { "http://example.com/", "http://false/"u8.ToArray(), ResultCode.CompareFalse };
}

[ConditionalTheory(nameof(IsLdapConfigurationExist))]
[ConditionalTheory(nameof(LdapConfigurationExists))]
[MemberData(nameof(TestCompareRequestTheory_TestData))]
public void TestCompareRequestTheory(object value, object assertion, ResultCode compareResult)
{
Expand Down Expand Up @@ -546,7 +558,7 @@ public void TestCompareRequestTheory(object value, object assertion, ResultCode
}
}

[ConditionalFact(nameof(IsLdapConfigurationExist))]
[ConditionalFact(nameof(LdapConfigurationExists))]
public void TestCompareRequest()
{
using (LdapConnection connection = GetConnection())
Expand Down Expand Up @@ -670,7 +682,7 @@ public void TestSortedSearch()
}
}

[ConditionalFact(nameof(IsLdapConfigurationExist))]
[ConditionalFact(nameof(LdapConfigurationExists))]
public void TestMultipleServerBind()
{
LdapDirectoryIdentifier directoryIdentifier = string.IsNullOrEmpty(LdapConfiguration.Configuration.Port) ?
Expand Down Expand Up @@ -774,13 +786,25 @@ private SearchResultEntry SearchUser(LdapConnection connection, string rootDn, s
return null;
}

private LdapConnection GetConnection(string server)
{
LdapDirectoryIdentifier directoryIdentifier = new LdapDirectoryIdentifier(server, fullyQualifiedDnsHostName: true, connectionless: false);

return GetConnection(directoryIdentifier);
}

private LdapConnection GetConnection()
{
LdapDirectoryIdentifier directoryIdentifier = string.IsNullOrEmpty(LdapConfiguration.Configuration.Port) ?
new LdapDirectoryIdentifier(LdapConfiguration.Configuration.ServerName, true, false) :
new LdapDirectoryIdentifier(LdapConfiguration.Configuration.ServerName, fullyQualifiedDnsHostName: true, connectionless: false) :
new LdapDirectoryIdentifier(LdapConfiguration.Configuration.ServerName,
int.Parse(LdapConfiguration.Configuration.Port, NumberStyles.None, CultureInfo.InvariantCulture),
true, false);
fullyQualifiedDnsHostName: true, connectionless: false);
return GetConnection(directoryIdentifier);
}

private static LdapConnection GetConnection(LdapDirectoryIdentifier directoryIdentifier)
{
NetworkCredential credential = new NetworkCredential(LdapConfiguration.Configuration.UserName, LdapConfiguration.Configuration.Password);

LdapConnection connection = new LdapConnection(directoryIdentifier, credential)
Expand Down

0 comments on commit 876f763

Please sign in to comment.