Skip to content

Commit

Permalink
Merge pull request #132 from OctopusDeploy/andrew-w/securing-message-…
Browse files Browse the repository at this point in the history
…protocol

Securing the Protocol
  • Loading branch information
andrew-at-octopus authored Aug 11, 2021
2 parents af95eb2 + b1c866b commit 4e57cbf
Show file tree
Hide file tree
Showing 29 changed files with 507 additions and 183 deletions.
20 changes: 12 additions & 8 deletions source/Halibut.Tests/ConnectionManagerFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ public class ConnectionManagerFixture
[SetUp]
public void SetUp()
{
var stream = Substitute.For<IMessageExchangeStream>();
connection = new SecureConnection(Substitute.For<IDisposable>(), Stream.Null, new MessageExchangeProtocol(stream));
connection = new SecureConnection(Substitute.For<IDisposable>(), Stream.Null,GetProtocol, Substitute.For<ILog>());
connectionFactory = Substitute.For<IConnectionFactory>();
connectionFactory.EstablishNewConnection(Arg.Any<ServiceEndPoint>(), Arg.Any<ILog>()).Returns(connection);
connectionFactory.EstablishNewConnection(GetProtocol, Arg.Any<ServiceEndPoint>(), Arg.Any<ILog>()).Returns(connection);
}

[Test]
Expand All @@ -31,8 +30,8 @@ public void DisposedConnectionsAreRemovedFromActive_WhenMultipleConnectionsAreAc
var connectionManager = new ConnectionManager();

//do it twice because this bug only triggers on multiple enumeration, having 1 in the collection doesn't trigger the bug
connectionManager.AcquireConnection(connectionFactory, serviceEndpoint, new InMemoryConnectionLog(serviceEndpoint.ToString()));
connectionManager.AcquireConnection(connectionFactory, serviceEndpoint, new InMemoryConnectionLog(serviceEndpoint.ToString()));
connectionManager.AcquireConnection(GetProtocol, connectionFactory, serviceEndpoint, new InMemoryConnectionLog(serviceEndpoint.ToString()));
connectionManager.AcquireConnection(GetProtocol, connectionFactory, serviceEndpoint, new InMemoryConnectionLog(serviceEndpoint.ToString()));

connectionManager.Disconnect(serviceEndpoint, null);
connectionManager.GetActiveConnections(serviceEndpoint).Should().BeNullOrEmpty();
Expand All @@ -44,7 +43,7 @@ public void ReleasedConnectionsAreNotActive()
var serviceEndpoint = new ServiceEndPoint("https://localhost:42", Certificates.TentacleListeningPublicThumbprint);
var connectionManager = new ConnectionManager();

var activeConnection = connectionManager.AcquireConnection(connectionFactory, serviceEndpoint, new InMemoryConnectionLog(serviceEndpoint.ToString()));
var activeConnection = connectionManager.AcquireConnection(GetProtocol, connectionFactory, serviceEndpoint, new InMemoryConnectionLog(serviceEndpoint.ToString()));
connectionManager.GetActiveConnections(serviceEndpoint).Should().OnlyContain(c => c == activeConnection);

connectionManager.ReleaseConnection(serviceEndpoint, activeConnection);
Expand All @@ -57,7 +56,7 @@ public void DisposedConnectionsAreRemovedFromActive()
var serviceEndpoint = new ServiceEndPoint("https://localhost:42", Certificates.TentacleListeningPublicThumbprint);
var connectionManager = new ConnectionManager();

var activeConnection = connectionManager.AcquireConnection(connectionFactory, serviceEndpoint, new InMemoryConnectionLog(serviceEndpoint.ToString()));
var activeConnection = connectionManager.AcquireConnection(GetProtocol, connectionFactory, serviceEndpoint, new InMemoryConnectionLog(serviceEndpoint.ToString()));
connectionManager.GetActiveConnections(serviceEndpoint).Should().OnlyContain(c => c == activeConnection);

activeConnection.Dispose();
Expand All @@ -70,11 +69,16 @@ public void DisconnectDisposesActiveConnections()
var serviceEndpoint = new ServiceEndPoint("https://localhost:42", Certificates.TentacleListeningPublicThumbprint);
var connectionManager = new ConnectionManager();

var activeConnection = connectionManager.AcquireConnection(connectionFactory, serviceEndpoint, new InMemoryConnectionLog(serviceEndpoint.ToString()));
var activeConnection = connectionManager.AcquireConnection(GetProtocol, connectionFactory, serviceEndpoint, new InMemoryConnectionLog(serviceEndpoint.ToString()));
connectionManager.GetActiveConnections(serviceEndpoint).Should().OnlyContain(c => c == activeConnection);

connectionManager.Disconnect(serviceEndpoint, new InMemoryConnectionLog(serviceEndpoint.ToString()));
connectionManager.GetActiveConnections(serviceEndpoint).Should().BeNullOrEmpty();
}

public MessageExchangeProtocol GetProtocol(Stream stream, ILog log)
{
return new MessageExchangeProtocol(new MessageExchangeStream(stream, new Type[] { }, log), log);
}
}
}
7 changes: 3 additions & 4 deletions source/Halibut.Tests/DiscoveryClientFixture.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using System;
using FluentAssertions;
using Halibut.Diagnostics;
using Halibut.ServiceModel;
using Halibut.Tests.TestServices;
using Halibut.Transport;
using NUnit.Framework;

namespace Halibut.Tests
{
public class DiscoveryClientFixture : IDisposable
public class DiscoveryClientFixture
{
ServiceEndPoint endpoint;
HalibutRuntime tentacle;
Expand All @@ -27,12 +26,12 @@ public void SetUp()
};
}

public void Dispose()
[TearDown]
public void TearDown()
{
tentacle.Dispose();
}


[Test]
public void DiscoverMethodReturnsEndpointDetails()
{
Expand Down
4 changes: 2 additions & 2 deletions source/Halibut.Tests/ProtocolFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void SetUp()
{
stream = new DumpStream();
stream.SetRemoteIdentity(new RemoteIdentity(RemoteIdentityType.Server));
protocol = new MessageExchangeProtocol(stream);
protocol = new MessageExchangeProtocol(stream, Substitute.For<ILog>());
}

[Test]
Expand Down Expand Up @@ -344,7 +344,7 @@ public void SetNumberOfReads(int reads)
numberOfReads = reads;
}

public List<object> Sent { get; set; }
public List<object> Sent { get; }

public void IdentifyAsClient()
{
Expand Down
Loading

0 comments on commit 4e57cbf

Please sign in to comment.