-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create unit tests for TestConnection
- Loading branch information
1 parent
cb1cd1f
commit 76d7289
Showing
2 changed files
with
96 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System; | ||
using System.DirectoryServices.Protocols; | ||
using CommonLibTest.Facades; | ||
using SharpHoundCommonLib; | ||
using SharpHoundCommonLib.Enums; | ||
using Xunit; | ||
|
||
namespace ConnectionTest | ||
{ | ||
public class ConnectionTests | ||
{ | ||
[Fact] | ||
public void TestConnectionNullResponse() | ||
{ | ||
var connection = MockLdapConnection.Get(ResponseBehavior.NullResponse); | ||
var testResponse = connection.TestConnection(); | ||
|
||
Assert.False(testResponse.Success); | ||
Assert.Null(testResponse.Exception); | ||
} | ||
|
||
// This happens when a Kerberos misconfiguration occurs | ||
[Fact] | ||
public void TestConnectionEmptyResponse() | ||
{ | ||
var connection = MockLdapConnection.Get(ResponseBehavior.EmptyResponse); | ||
var testResponse = connection.TestConnection(); | ||
|
||
Assert.False(testResponse.Success); | ||
Assert.IsType<LdapException>(testResponse.Exception); | ||
Assert.Equal((int)LdapErrorCodes.KerberosAuthType, testResponse.Exception.ErrorCode); | ||
Assert.Throws<ObjectDisposedException>(() => connection.Bind()); | ||
} | ||
|
||
[Fact] | ||
public void TestConnectionThrowsLdapException() | ||
{ | ||
var connection = MockLdapConnection.Get(ResponseBehavior.ThrowsLdapException); | ||
var testResponse = connection.TestConnection(); | ||
|
||
Assert.False(testResponse.Success); | ||
Assert.IsType<LdapException>(testResponse.Exception); | ||
Assert.Throws<ObjectDisposedException>(() => connection.Bind()); | ||
} | ||
} | ||
} |
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,50 @@ | ||
using System; | ||
using System.DirectoryServices.Protocols; | ||
using Moq; | ||
|
||
namespace CommonLibTest.Facades | ||
{ | ||
public static class MockLdapConnection | ||
{ | ||
public static LdapConnection Get(ResponseBehavior responseBehavior) | ||
=> responseBehavior switch | ||
{ | ||
ResponseBehavior.NullResponse => ReturnsNullResponse(), | ||
ResponseBehavior.EmptyResponse => ReturnsEmptyResponse(), | ||
ResponseBehavior.ThrowsLdapException => ThrowsLdapException(), | ||
_ => throw new ArgumentOutOfRangeException(nameof(responseBehavior)) | ||
}; | ||
|
||
private static LdapConnection ReturnsNullResponse() | ||
{ | ||
var mock = new Mock<LdapConnection>(); | ||
mock.Setup(x => x.SendRequest(It.IsAny<SearchRequest>())) | ||
.Returns<SearchResponse>(null); | ||
return mock.Object; | ||
} | ||
|
||
private static LdapConnection ReturnsEmptyResponse() | ||
{ | ||
var mock = new Mock<LdapConnection>(); | ||
var emptyResponseMock = Mock.Of<SearchResponse>(m => m.Entries == Mock.Of<SearchResultEntryCollection>(m => m.Count == 0)); | ||
mock.Setup(x => x.SendRequest(It.IsAny<SearchRequest>())) | ||
.Returns(emptyResponseMock); | ||
return mock.Object; | ||
} | ||
|
||
private static LdapConnection ThrowsLdapException() | ||
{ | ||
var mock = new Mock<LdapConnection>(); | ||
mock.Setup(x => x.SendRequest(It.IsAny<SearchRequest>())) | ||
.Throws<LdapException>(); | ||
return mock.Object; | ||
} | ||
} | ||
|
||
public enum ResponseBehavior | ||
{ | ||
NullResponse, | ||
EmptyResponse, | ||
ThrowsLdapException, | ||
} | ||
} |