-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// 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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did all of these new tests fail prior? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There were never any tests against this class as far as I could find, so no :) The two tests come from from @stephentoub's minimal reproduction in this comment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, that might have been unclear... the first test would have always succeeded (and I put this test in to ensure I didn't break anything). The second test would have failed without the changes in this PR. |
||
{ | ||
bool seen = false; | ||
SearchResultCollection e = GetDomains(); | ||
Assert.NotNull(e); | ||
|
||
foreach (var result in e) | ||
{ | ||
Assert.NotNull(result); | ||
seen = true; | ||
} | ||
|
||
Assert.True(seen); | ||
} | ||
|
||
[ConditionalFact(nameof(IsLdapConfigurationExist))] | ||
public void DirectorySearch_IteratesCorrectly_AfterCount() | ||
{ | ||
bool seen = false; | ||
SearchResultCollection e = GetDomains(); | ||
Assert.NotNull(e); | ||
Assert.NotEqual(0, e.Count); | ||
|
||
foreach (var result in e) | ||
{ | ||
Assert.NotNull(result); | ||
seen = true; | ||
} | ||
|
||
Assert.True(seen); | ||
} | ||
|
||
private static SearchResultCollection GetDomains() | ||
{ | ||
using DirectoryEntry entry = new DirectoryEntry("LDAP://rootDSE"); | ||
string namingContext = entry.Properties["configurationNamingContext"][0]!.ToString(); | ||
using DirectoryEntry searchRoot = new DirectoryEntry($"LDAP://CN=Partitions,{namingContext}"); | ||
using DirectorySearcher 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(); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The change to build the inner list in it's entirety and then set the member is to ensure that if an exception in thrown while populating this inner list we do NOT memoize the incomplete list because then we would (if enumerated again) not throw again and would instead silently only yield the entries we previously gathered. Of course anyone attempting _re_enumeration of a list that yielded an error the first time is probably wrong, but why add to the problem?