Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix break in DirectoryServices SearchResultCollection #113775

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,17 @@ private ArrayList InnerList
{
if (_innerList == null)
{
_innerList = new ArrayList();
var eagerList = new ArrayList();
var enumerator = new ResultsEnumerator(
this,
_rootEntry.GetUsername(),
_rootEntry.GetPassword(),
_rootEntry.AuthenticationType);

while (enumerator.MoveNext())
_innerList.Add(enumerator.Current);
eagerList.Add(enumerator.Current);

_innerList = eagerList;
}

return _innerList;
Expand Down Expand Up @@ -188,12 +190,10 @@ protected virtual void Dispose(bool disposing)

public IEnumerator GetEnumerator()
{
// Two ResultsEnumerators can't exist at the same time over the
// same object. Need to get a new handle, which means re-querying.
return new ResultsEnumerator(this,
_rootEntry.GetUsername(),
_rootEntry.GetPassword(),
_rootEntry.AuthenticationType);
if (_innerList != null)
return new AlreadyReadResultsEnumerator(_innerList);
else
return new ResultsEnumerator(this, _rootEntry.GetUsername(), _rootEntry.GetPassword(), _rootEntry.AuthenticationType);
}

public bool Contains(SearchResult result) => InnerList.Contains(result);
Expand All @@ -214,6 +214,49 @@ void ICollection.CopyTo(Array array, int index)
InnerList.CopyTo(array, index);
}

/// <devdoc>
/// Supports a simple type-specific wrapper for the underlying cached list
/// </devdoc>
private sealed class AlreadyReadResultsEnumerator : IEnumerator
{
private readonly IEnumerator _innerEnumerator;

internal AlreadyReadResultsEnumerator(ArrayList innerList)
{
_innerEnumerator = innerList.GetEnumerator();
}

/// <devdoc>
/// Gets the current element in the collection.
/// </devdoc>
public SearchResult Current
{
get
{
return (SearchResult)(_innerEnumerator.Current);
}
}

/// <devdoc>
/// Advances the enumerator to the next element of the collection
/// and returns a Boolean value indicating whether a valid element is available.
/// </devdoc>
public bool MoveNext()
{
return _innerEnumerator.MoveNext();
}

/// <devdoc>
/// Resets the enumerator back to its initial position before the first element in the collection.
/// </devdoc>
public void Reset()
{
_innerEnumerator.Reset();
}

object IEnumerator.Current => Current;
}

/// <devdoc>
/// Supports a simple ForEach-style iteration over a collection.
/// </devdoc>
Expand Down Expand Up @@ -310,6 +353,7 @@ public bool MoveNext()
return false;

_currentResult = null;

if (!_initialized)
{
int hr = _results.SearchObject.GetFirstRow(_results.Handle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<Compile Include="System\DirectoryServices\ActiveDirectory\DomainControllerTests.cs" />
<Compile Include="System\DirectoryServices\DirectoryEntryTests.cs" />
<Compile Include="System\DirectoryServices\DirectoryServicesPermissionTests.cs" />
<Compile Include="System\DirectoryServices\DirectorySearcherTests.cs" />
<Compile Include="System\DirectoryServices\DirectoryServicesTests.cs" />
<Compile Include="System\DirectoryServices\DirectorySynchronizationTests.cs" />
<Compile Include="System\DirectoryServices\DirectoryVirtualListViewContextTests.cs" />
Expand All @@ -20,8 +21,7 @@
<Compile Include="System\DirectoryServices\ActiveDirectory\ForestTests.cs" />
<Compile Include="System\DirectoryServices\ActiveDirectory\ActiveDirectoryTests.cs" />
<Compile Include="System\DirectoryServices\ActiveDirectory\TrustHelperTests.cs" />
<Compile Include="$(CommonTestPath)System\DirectoryServices\LdapConfiguration.cs"
Link="Common\DirectoryServices\LdapConfiguration.cs" />
<Compile Include="$(CommonTestPath)System\DirectoryServices\LdapConfiguration.cs" Link="Common\DirectoryServices\LdapConfiguration.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="$(CommonTestPath)System\DirectoryServices\LDAP.Configuration.xml">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Collections;
using Xunit;
using Xunit.Sdk;
using System.Reflection;

namespace System.DirectoryServices.Tests
{
public partial class DirectorySearcherTests
{
internal static bool IsLdapConfigurationExist => LdapConfiguration.Configuration != null;
internal static bool IsActiveDirectoryServer => IsLdapConfigurationExist && LdapConfiguration.Configuration.IsActiveDirectoryServer;

private const int ADS_SYSTEMFLAG_CR_NTDS_NC = 0x1;
private const int ADS_SYSTEMFLAG_CR_NTDS_DOMAIN = 0x2;

[ConditionalFact(nameof(IsLdapConfigurationExist))]
public void DirectorySearch_IteratesCorrectly_SimpleEnumeration()
{
var e = GetDomains();
Assert.NotNull(e);

foreach (var result in e)
{
Assert.NotNull(result);
}
}

[ConditionalFact(nameof(IsLdapConfigurationExist))]
public void DirectorySearch_IteratesCorrectly_AfterCount()
{
var e = GetDomains();
Assert.NotNull(e);
Assert.NotEqual(0, e.Count);

foreach (var result in e)
{
Assert.NotNull(result);
}
}


[ConditionalFact(nameof(IsLdapConfigurationExist))]
public void DirectorySearch_IteratesCorrectly_MixedCount()
{
var e = GetDomains();
Assert.NotNull(e);
Assert.NotEqual(0, e.Count);

foreach (var result in e)
{
Assert.NotEqual(0, e.Count);
Assert.NotNull(result);
}
}

private static SearchResultCollection GetDomains()
{
using var entry = new DirectoryEntry("LDAP://rootDSE");
var namingContext = entry.Properties["configurationNamingContext"][0]!.ToString();
using var searchRoot = new DirectoryEntry($"LDAP://CN=Partitions,{namingContext}");
using var ds = new DirectorySearcher(searchRoot)
{
PageSize = 1000,
CacheResults = false
};
ds.SearchScope = SearchScope.OneLevel;
ds.PropertiesToLoad.Add("distinguishedName");
ds.PropertiesToLoad.Add("nETBIOSName");
ds.PropertiesToLoad.Add("nCName");
ds.PropertiesToLoad.Add("dnsRoot");
ds.PropertiesToLoad.Add("trustParent");
ds.PropertiesToLoad.Add("objectSid");
ds.Filter = string.Format("(&(objectCategory=crossRef)(systemFlags={0}))", ADS_SYSTEMFLAG_CR_NTDS_DOMAIN | ADS_SYSTEMFLAG_CR_NTDS_NC);

return ds.FindAll();
}
}
}
Loading