From 0e086b3b6c125ff7ccdc22259664118cea15d5ce Mon Sep 17 00:00:00 2001 From: Peter Kurfer Date: Fri, 5 Mar 2021 16:55:25 +0100 Subject: [PATCH] Cleanup - Fix compile errors - MOve consumer key split into Subscription to allow better testing - move models to extra file - adopt API changes --- .gitlab-ci.yml | 4 +- src/INetMock.Client/Audit/Details.cs | 15 +++--- src/INetMock.Client/Audit/Event.cs | 6 +-- .../PCAP/Client/PcapApiClient.cs | 21 +++----- src/INetMock.Client/PCAP/IPcapApiClient.cs | 16 ------ src/INetMock.Client/PCAP/Models.cs | 53 +++++++++++++++++++ .../PCAP/SubscriptionTests.cs | 17 ++++++ 7 files changed, 89 insertions(+), 43 deletions(-) create mode 100644 src/INetMock.Client/PCAP/Models.cs create mode 100644 tests/INetMock.Client.Test/PCAP/SubscriptionTests.cs diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a5f3335..ad01cf8 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -7,8 +7,8 @@ stages: variables: DOCKER_TLS_CERTDIR: "/certs" DOCKER_CERT_PATH: "/certs/client" - DOCKER_TLS_VERIFY: 0 - DOCKER_HOST: 'tcp://docker:2375' + DOCKER_TLS_VERIFY: 1 + DOCKER_HOST: 'tcp://docker:2376' test: stage: test diff --git a/src/INetMock.Client/Audit/Details.cs b/src/INetMock.Client/Audit/Details.cs index 8a87dc1..ccfba88 100644 --- a/src/INetMock.Client/Audit/Details.cs +++ b/src/INetMock.Client/Audit/Details.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Net.Http; using System.Net.Http.Headers; +using Google.Protobuf.Collections; using Google.Protobuf.WellKnownTypes; namespace INetMock.Client.Audit @@ -60,11 +61,11 @@ namespace INetMock.Client.Audit Headers = new INetMockHttpHeaders(detailsEntity.Headers); } - public HttpMethod Method { get; init; } - public string Host { get; init; } - public string Uri { get; init; } - public string Proto { get; init; } - public HttpHeaders Headers { get; init; } + public HttpMethod Method { get; init; } = HttpMethod.Get; + public string Host { get; init; } = string.Empty; + public string Uri { get; init; } = string.Empty; + public string Proto { get; init; } = string.Empty; + public HttpHeaders Headers { get; init; } = new INetMockHttpHeaders(new MapField()); } public record DnsDetails : EventDetails @@ -73,9 +74,9 @@ namespace INetMock.Client.Audit { } - public DNSOpCode OpCode { get; init; } + public DNSOpCode OpCode { get; init; } = DNSOpCode.Query; - public IReadOnlyList Questions { get; init; } + public IReadOnlyList Questions { get; init; } = Array.Empty(); public DnsDetails(Any? any) { diff --git a/src/INetMock.Client/Audit/Event.cs b/src/INetMock.Client/Audit/Event.cs index e4d33f7..c1b93ff 100644 --- a/src/INetMock.Client/Audit/Event.cs +++ b/src/INetMock.Client/Audit/Event.cs @@ -37,11 +37,11 @@ namespace INetMock.Client.Audit public DateTimeOffset Timestamp { get; init; } public TransportProtocol Transport { get; init; } public AppProtocol Application { get; init; } - public IPAddress SourceIp { get; init; } - public IPAddress DestinationIp { get; init; } + public IPAddress SourceIp { get; init; } = IPAddress.Any; + public IPAddress DestinationIp { get; init; } = IPAddress.Any; public ushort SourcePort { get; init; } public ushort DestinationPort { get; init; } - public TLSDetailsEntity TlsDetails { get; init; } + public TLSDetailsEntity TlsDetails { get; init; } = new(); public T? Details { get; init; } public bool CanConvert() where TTarget : EventDetails, new() diff --git a/src/INetMock.Client/PCAP/Client/PcapApiClient.cs b/src/INetMock.Client/PCAP/Client/PcapApiClient.cs index d06acdc..4e4a00d 100644 --- a/src/INetMock.Client/PCAP/Client/PcapApiClient.cs +++ b/src/INetMock.Client/PCAP/Client/PcapApiClient.cs @@ -48,7 +48,9 @@ namespace INetMock.Client.PCAP.Client public async Task> ListActiveRecordingsAsync(CancellationToken token = default) { var recordings = await _pcapServiceClient.ListActiveRecordingsAsync(new(), Metadata.Empty, null, token); - return recordings.Subscriptions.Select(SplitKeyIntoSubscription).ToList(); + return recordings.Subscriptions + .Select(consumerKey => new Subscription(consumerKey)) + .ToList(); } public async Task StartPcapFileRecordingAsync(RecordingRequest request, @@ -71,10 +73,10 @@ namespace INetMock.Client.PCAP.Client return result.ResolvedPath; } - public async Task StopPcapFileRecord(string consumerKey, CancellationToken token = default) + public async Task StopPcapFileRecording(string consumerKey, CancellationToken token = default) { - var clientRequest = new StopPCAPFileRecordRequest {ConsumerKey = consumerKey}; - var result = await _pcapServiceClient.StopPCAPFileRecordAsync( + var clientRequest = new StopPCAPFileRecordingRequest {ConsumerKey = consumerKey}; + var result = await _pcapServiceClient.StopPCAPFileRecordingAsync( clientRequest, Metadata.Empty, null, @@ -82,16 +84,5 @@ namespace INetMock.Client.PCAP.Client ); return result.Removed; } - - private static Subscription SplitKeyIntoSubscription(string key) - { - var splitIndex = key.IndexOf(':'); - if (splitIndex < 0) - { - throw new ArgumentOutOfRangeException(); - } - - return new Subscription(key.Substring(splitIndex+1), key.Substring(0, splitIndex), key); - } } } diff --git a/src/INetMock.Client/PCAP/IPcapApiClient.cs b/src/INetMock.Client/PCAP/IPcapApiClient.cs index 43d24b5..c5d5b25 100644 --- a/src/INetMock.Client/PCAP/IPcapApiClient.cs +++ b/src/INetMock.Client/PCAP/IPcapApiClient.cs @@ -1,25 +1,9 @@ -using System; using System.Collections.Generic; -using System.Net; using System.Threading; using System.Threading.Tasks; namespace INetMock.Client.PCAP { - public record RecordingDevice(string Name, IReadOnlyList Addresses); - - public record Subscription(string ConsumerName, string Device, string ConsumerKey); - public record RecordingRequest(string Device, string TargetPath, bool Promiscuous = false) - { - public RecordingRequest(string device, string targetPath, bool promiscuous, TimeSpan readTimeout) : this(device, - targetPath, promiscuous) - { - ReadTimeout = readTimeout; - } - - public TimeSpan ReadTimeout { get; } = TimeSpan.FromSeconds(30); - } - public interface IPcapApiClient { Task> ListAvailableDevicesAsync(CancellationToken token = default); diff --git a/src/INetMock.Client/PCAP/Models.cs b/src/INetMock.Client/PCAP/Models.cs new file mode 100644 index 0000000..2642158 --- /dev/null +++ b/src/INetMock.Client/PCAP/Models.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Net; + +namespace INetMock.Client.PCAP +{ + public record RecordingDevice(string Name, IReadOnlyList Addresses); + + public record Subscription + { + public Subscription(string consumerKey) + { + ConsumerKey = consumerKey; + (ConsumerName, Device) = SplitConsumerKey(consumerKey); + } + + public Subscription(string consumerKey, string consumerName, string device) + { + ConsumerKey = consumerKey; + ConsumerName = consumerName; + Device = device; + } + + public string ConsumerKey { get; init; } + public string ConsumerName { get; init; } + public string Device { get; init; } + + private static (string name, string key) SplitConsumerKey(string consumerKey) + { + var splitIndex = consumerKey.IndexOf(':'); + if (splitIndex < 0) + { + throw new ArgumentOutOfRangeException( + nameof(consumerKey), + "The given consumer key could not be split into components" + ); + } + + return (consumerKey[(splitIndex + 1)..], consumerKey[..splitIndex]); + } + } + + public record RecordingRequest(string Device, string TargetPath, bool Promiscuous = false) + { + public RecordingRequest(string device, string targetPath, bool promiscuous, TimeSpan readTimeout) : this(device, + targetPath, promiscuous) + { + ReadTimeout = readTimeout; + } + + public TimeSpan ReadTimeout { get; } = TimeSpan.FromSeconds(30); + } +} diff --git a/tests/INetMock.Client.Test/PCAP/SubscriptionTests.cs b/tests/INetMock.Client.Test/PCAP/SubscriptionTests.cs new file mode 100644 index 0000000..7a4e776 --- /dev/null +++ b/tests/INetMock.Client.Test/PCAP/SubscriptionTests.cs @@ -0,0 +1,17 @@ +using INetMock.Client.PCAP; +using Xunit; + +namespace INetMock.Client.Test.PCAP +{ + public class SubscriptionTests + { + [Theory] + [InlineData("lo:test.pcap", "test.pcap", "lo")] + public void Constructor_ConsumerKey_SplitIntoComponents(string key, string expectedName, string expectedDevice) + { + var sub = new Subscription(key); + + Assert.Equal(sub, new Subscription(key, expectedName, expectedDevice)); + } + } +}