-
Notifications
You must be signed in to change notification settings - Fork 47
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
WIP: utils query tests, especially around failure cases #146
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
using Moq; | ||
using System.DirectoryServices.Protocols; | ||
using SharpHoundCommonLib; | ||
|
||
public class LdapUtilsQueryTest | ||
{ | ||
// [Fact] | ||
// public async Task RangedRetrieval_SuccessfulRetrieval_ReturnsExpectedResults() | ||
// { | ||
// // Arrange | ||
// var distinguishedName = "CN=TestUser,DC=example,DC=com"; | ||
// var attributeName = "member"; | ||
// var domain = "example.com"; | ||
|
||
// var connectionWrapper = new Mock<LdapConnectionWrapper>(); | ||
// var connection = new Mock<LdapConnection>(); | ||
// connectionWrapper.SetupGet(x => x.Connection).Returns(connection.Object); | ||
|
||
// var mockConnectionPool = new Mock<ILdapConnectionProvider>(); | ||
// mockConnectionPool.Setup(x => x.GetLdapConnection(domain, false)) | ||
// .ReturnsAsync((true, connectionWrapper.Object, null)); | ||
|
||
// var utils = new LdapUtils(mockConnectionPool.Object); | ||
|
||
// var searchResponse = new Mock<SearchResponse>(); | ||
// var entry = new SearchResultEntry | ||
// { | ||
// Attributes = | ||
// { | ||
// new DirectoryAttribute("member;range=0-*", "CN=Member1,DC=example,DC=com", "CN=Member2,DC=example,DC=com") | ||
// } | ||
// }; | ||
// searchResponse.Entries.Add(entry); | ||
|
||
// connection.Setup(x => x.SendRequest(It.IsAny<SearchRequest>())) | ||
// .Returns(searchResponse); | ||
|
||
// // Act | ||
// var results = new List<Result<string>>(); | ||
// await foreach (var result in utils.RangedRetrieval(distinguishedName, attributeName)) | ||
// { | ||
// results.Add(result); | ||
// } | ||
|
||
// // Assert | ||
// Assert.Equal(2, results.Count); | ||
// Assert.True(results[0].IsSuccess); | ||
// Assert.Equal("CN=Member1,DC=example,DC=com", results[0].Value); | ||
// Assert.True(results[1].IsSuccess); | ||
// Assert.Equal("CN=Member2,DC=example,DC=com", results[1].Value); | ||
// } | ||
|
||
[Fact] | ||
public async Task RangedRetrieval_ConnectionFailure_ReturnsFailResult() | ||
{ | ||
// Arrange | ||
var distinguishedName = "CN=TestUser,DC=example,DC=com"; | ||
var attributeName = "member"; | ||
|
||
var utils = new LdapUtils(); | ||
|
||
// Act | ||
var results = new List<Result<string>>(); | ||
await foreach (var result in utils.RangedRetrieval(distinguishedName, attributeName)) | ||
{ | ||
results.Add(result); | ||
} | ||
|
||
// Assert | ||
Assert.Single(results); | ||
Assert.False(results[0].IsSuccess); | ||
Assert.Equal("All attempted connections failed", results[0].Error); | ||
} | ||
|
||
// [Fact] | ||
// public async Task RangedRetrieval_ServerDown_RetriesAndRecovers() | ||
// { | ||
// // Arrange | ||
// var distinguishedName = "CN=TestUser,DC=example,DC=com"; | ||
// var attributeName = "member"; | ||
// var domain = "example.com"; | ||
|
||
// var connectionWrapper = new Mock<LdapConnectionWrapper>(); | ||
// var connection = new Mock<LdapConnection>(); | ||
|
||
// // TODO : setup | ||
|
||
// // Act | ||
// var results = new List<Result<string>>(); | ||
// await foreach (var result in _utils.RangedRetrieval(distinguishedName, attributeName)) | ||
// { | ||
// results.Add(result); | ||
// } | ||
|
||
// // TODO Assert | ||
// } | ||
|
||
[Fact] | ||
public async Task RangedRetrieval_CancellationRequested_StopsRetrieval() | ||
{ | ||
// Arrange | ||
var distinguishedName = "CN=TestUser,DC=example,DC=com"; | ||
var attributeName = "member"; | ||
var domain = "example.com"; | ||
|
||
var connectionWrapper = new Mock<LdapConnectionWrapper>(null, null, false, string.Empty); | ||
var connection = new Mock<LdapConnection>(); | ||
var mockConnectionPool = new Mock<ILdapConnectionProvider>(); | ||
|
||
mockConnectionPool.Setup(x => x.GetLdapConnection(domain, false)) | ||
.ReturnsAsync((true, connectionWrapper.Object, null)); | ||
|
||
var utils = new LdapUtils(mockConnectionPool.Object); | ||
|
||
var cts = new CancellationTokenSource(); | ||
cts.Cancel(); | ||
|
||
// Act | ||
var results = new List<Result<string>>(); | ||
await foreach (var result in utils.RangedRetrieval(distinguishedName, attributeName, cts.Token)) | ||
{ | ||
results.Add(result); | ||
} | ||
|
||
// Assert | ||
Assert.False(results[0].IsSuccess); | ||
} | ||
|
||
[Fact] | ||
public async Task Query_ConnectionFailure_ReturnsEmptyResult() | ||
{ | ||
// Arrange | ||
var queryParams = new LdapQueryParameters(); | ||
var utils = new LdapUtils(); | ||
|
||
// Act | ||
var results = new List<LdapResult<IDirectoryObject>>(); | ||
await foreach (var result in utils.Query(queryParams)) | ||
{ | ||
results.Add(result); | ||
} | ||
|
||
// Assert | ||
Assert.Empty(results); | ||
} | ||
|
||
[Fact] | ||
public async Task Query_CancellationRequested_StopsRetrieval() | ||
{ | ||
// Arrange | ||
var queryParams = new LdapQueryParameters(); | ||
var domain = "example.com"; | ||
|
||
var connectionWrapper = new Mock<LdapConnectionWrapper>(null, null, false, string.Empty); | ||
var connection = new Mock<LdapConnection>(); | ||
var mockConnectionPool = new Mock<ILdapConnectionProvider>(); | ||
|
||
mockConnectionPool.Setup(x => x.GetLdapConnection(domain, false)) | ||
.ReturnsAsync((true, connectionWrapper.Object, null)); | ||
|
||
var utils = new LdapUtils(mockConnectionPool.Object); | ||
|
||
var cts = new CancellationTokenSource(); | ||
cts.Cancel(); | ||
|
||
// Act | ||
var results = new List<LdapResult<IDirectoryObject>>(); | ||
await foreach (var result in utils.Query(queryParams, cts.Token)) | ||
{ | ||
results.Add(result); | ||
} | ||
|
||
// Assert | ||
Assert.Empty(results); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Todo: This doesn't really test anything just yet