Skip to content

Commit

Permalink
Create unit tests for TestConnection
Browse files Browse the repository at this point in the history
  • Loading branch information
definitelynotagoblin committed May 17, 2024
1 parent cb1cd1f commit 76d7289
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
46 changes: 46 additions & 0 deletions test/unit/ConnectionTest.cs
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());
}
}
}
50 changes: 50 additions & 0 deletions test/unit/Facades/MockLdapConnection.cs
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,
}
}

0 comments on commit 76d7289

Please sign in to comment.