Skip to content
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

Test optimizations and minor cleanups #6

Merged
merged 5 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NetStalkerAvalonia.Core.Services;
using NetStalkerAvalonia.Core.Services.Implementations;
using NetStalkerAvalonia.Core.Services.Implementations.BlockingRedirection;

namespace NetStalker.Tests.AutoData.Customizations
Expand All @@ -8,7 +9,7 @@ public class BlockerRedirectorCustomization : ICustomization
public void Customize(IFixture fixture)
{
fixture.Customizations.Add(new TypeRelay(typeof(IBlockerRedirector), typeof(BlockerRedirector)));
fixture.Register(() => new BlockerRedirector(fixture.Create<IRuleService>(), fixture.Create<IPcapDeviceManager>()));
fixture.Register(() => new BlockerRedirector(fixture.Create<IRuleService>(), fixture.Create<IPcapDeviceManager>(), fixture.Create<IMessageBusService>()));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NetStalkerAvalonia.Core.Services;
using NetStalkerAvalonia.Core.Services.Implementations;
using NetStalkerAvalonia.Core.Services.Implementations.DeviceScanning;

namespace NetStalker.Tests.AutoData.Customizations
Expand All @@ -8,7 +9,7 @@ public class DeviceScannerCustomization : ICustomization
public void Customize(IFixture fixture)
{
fixture.Customizations.Add(new TypeRelay(typeof(IDeviceScanner), typeof(DeviceScanner)));
fixture.Register(() => new DeviceScanner(fixture.Create<IPcapDeviceManager>(), fixture.Create<IDeviceNameResolver>(), fixture.Create<IDeviceTypeIdentifier>()));
fixture.Register(() => new DeviceScanner(fixture.Create<IPcapDeviceManager>(), fixture.Create<IDeviceNameResolver>(), fixture.Create<IDeviceTypeIdentifier>(), fixture.Create<IMessageBusService>()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using NetStalkerAvalonia.Core.Services.Implementations;
using NetStalkerAvalonia.Core.Services.Implementations.MessageBus;

namespace NetStalker.Tests.AutoData.Customizations
{
public class MessageBusCustomization : ICustomization
{
public void Customize(IFixture fixture)
{
fixture.Customizations.Add(new TypeRelay(typeof(IMessageBusService), typeof(MessageBusService)));
fixture.Register(() => new MessageBusService());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public void Customize(IFixture fixture)
var pcapDeviceManagerMock = new Mock<IPcapDeviceManager>();
var pcapLiveDeviceMock = new Mock<IPcapLiveDevice>();

pcapDeviceManagerMock.Setup(p => p.CreateDevice(It.IsAny<string>(), It.IsAny<PacketArrivalEventHandler?>(), It.IsAny<int>(), It.IsAny<string>())).Returns(pcapLiveDeviceMock.Object);
pcapDeviceManagerMock.Setup(p => p.CreateDevice(It.IsAny<string>(), It.IsAny<PacketArrivalEventHandler?>(), It.IsAny<int>(), It.IsAny<string>()))
.Returns(pcapLiveDeviceMock.Object);

return pcapDeviceManagerMock.Object;
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using AutoMapper;
using NetStalkerAvalonia.Core.Services;
using NetStalkerAvalonia.Core.Services.Implementations.RulesService;
using Splat;
using System.IO.Abstractions;

namespace NetStalker.Tests.AutoData.Customizations
Expand All @@ -10,7 +11,7 @@ public class RuleServiceCustomization : ICustomization
public void Customize(IFixture fixture)
{
fixture.Customizations.Add(new TypeRelay(typeof(IRuleService), typeof(RuleService)));
fixture.Register(() => new RuleService(fixture.Create<IMapper>(), fixture.Create<IFileSystem>()));
fixture.Register(() => new RuleService(fixture.Create<IMapper>(), fixture.Create<IFileSystem>(), fixture.Create<IStatusMessageService>()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public void Customize(IFixture fixture)
fixture.Customize(new BlockerRedirectorCustomization());
fixture.Customize(new AppLockCustomization());
fixture.Customize(new ErrorHandlerCustomization());
fixture.Customize(new MessageBusCustomization());
fixture.Customize(new StatusMessageCustomization());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using NetStalkerAvalonia.Core.Services.Implementations.ErrorHandling;
using NetStalkerAvalonia.Core.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NetStalkerAvalonia.Core.Services;
using NetStalkerAvalonia.Core.Services.Implementations;
using NetStalkerAvalonia.Core.Services.Implementations.StatusMessages;

namespace NetStalker.Tests.AutoData.Customizations
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ public class ViewModelsCustomization : ICustomization
{
public void Customize(IFixture fixture)
{
fixture.Register(() => new HomeViewModel(fixture.Create<IErrorHandler>(), fixture.Create<IStatusMessageService>(), fixture.Create<IDeviceScanner>(), fixture.Create<IBlockerRedirector>(), fixture.Create<IDeviceNameResolver>()));
fixture.Register(() => new SnifferViewModel(fixture.Create<IRouter>()));
fixture.Register(() => new OptionsViewModel(fixture.Create<IRouter>(), fixture.Create<IAppLockService>()));
fixture.Register(() => new RuleBuilderViewModel(fixture.Create<IRouter>(), fixture.Create<IRuleService>(), fixture.Create<IMapper>(), fixture.Create<IErrorHandler>()));
fixture.Register(() => new OptionsViewModel(fixture.Create<IRouter>(), fixture.Create<IAppLockService>(), fixture.Create<IStatusMessageService>()));
fixture.Register(() => new RuleBuilderViewModel(fixture.Create<IRouter>(), fixture.Create<IRuleService>(), fixture.Create<IMapper>(), fixture.Create<IErrorHandler>(), fixture.Create<IStatusMessageService>()));
fixture.Register(() => new HelpViewModel(fixture.Create<IRouter>()));
fixture.Register(() => new AboutViewModel(fixture.Create<IRouter>()));
fixture.Register(() => new AppLogViewModel());
Expand Down
74 changes: 38 additions & 36 deletions NetStalker.Tests/Avalonia/AvaloniaUiTestFramework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,42 +66,44 @@ public override void Dispose()

protected override void SetupSyncContext(int maxParallelThreads)
{
var tcs = new TaskCompletionSource<SynchronizationContext>();

new Thread(() =>
{
try
{
var builder = AvaloniaApp
.BuildAvaloniaApp()
.UseHeadless()
.AfterSetup((b) =>
{
var lifeTime = b.Instance.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime;

lifeTime.Startup += (sender, args) =>
{
HostInfo.SetHostInfo(null, null, null, null, null, IpType.Ipv4, null, NetworkClass.C);

// We wait until the framework and app are completely initialized and ready to run
tcs.SetResult(SynchronizationContext.Current);

Dispatcher.UIThread.MainLoop(CancellationToken.None);
};
})
.StartWithClassicDesktopLifetime(Array.Empty<string>());
}
catch (Exception e)
{
tcs.SetException(e);
}
})
{
IsBackground = true

}.Start();

SynchronizationContext.SetSynchronizationContext(tcs.Task.Result);
HostInfo.SetHostInfo(null, null, null, null, null, IpType.Ipv4, null, NetworkClass.C);

//var tcs = new TaskCompletionSource<SynchronizationContext>();

//new Thread(() =>
//{
// try
// {
// var builder = AvaloniaApp
// .BuildAvaloniaApp()
// .UseHeadless(new AvaloniaHeadlessPlatformOptions() { UseHeadlessDrawing = true })
// .AfterSetup((b) =>
// {
// var lifeTime = b.Instance.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime;

// lifeTime.Startup += (sender, args) =>
// {
// HostInfo.SetHostInfo(null, null, null, null, null, IpType.Ipv4, null, NetworkClass.C);

// // We wait until the framework and app are completely initialized and ready to run
// tcs.SetResult(SynchronizationContext.Current);

// Dispatcher.UIThread.MainLoop(CancellationToken.None);
// };
// })
// .StartWithClassicDesktopLifetime(Array.Empty<string>());
// }
// catch (Exception e)
// {
// tcs.SetException(e);
// }
//})
//{
// IsBackground = true

//}.Start();

//SynchronizationContext.SetSynchronizationContext(tcs.Task.Result);
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions NetStalker.Tests/NetStalker.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@
<ItemGroup>
<PackageReference Include="AutoFixture.AutoMoq" Version="4.18.0" />
<PackageReference Include="AutoFixture.Xunit2" Version="4.18.0" />
<PackageReference Include="FluentAssertions" Version="6.10.0" />
<PackageReference Include="FluentAssertions.Analyzers" Version="0.17.2">
<PackageReference Include="FluentAssertions" Version="6.11.0" />
<PackageReference Include="FluentAssertions.Analyzers" Version="0.21.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="TestableIO.System.IO.Abstractions" Version="19.2.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
<PackageReference Include="TestableIO.System.IO.Abstractions" Version="19.2.29" />
<PackageReference Include="TestableIO.System.IO.Abstractions.Analyzers" Version="2022.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="TestableIO.System.IO.Abstractions.TestingHelpers" Version="19.2.4" />
<PackageReference Include="TestableIO.System.IO.Abstractions.Wrappers" Version="19.2.4" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="TestableIO.System.IO.Abstractions.TestingHelpers" Version="19.2.29" />
<PackageReference Include="TestableIO.System.IO.Abstractions.Wrappers" Version="19.2.29" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.2.0">
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
21 changes: 11 additions & 10 deletions NetStalker.Tests/ServiceSpecs/BlockerRedirectorSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
using NetStalker.Tests.AutoData;
using NetStalkerAvalonia.Core.Models;
using NetStalkerAvalonia.Core.Services;
using NetStalkerAvalonia.Core.Services.Implementations;
using NetStalkerAvalonia.Core.Services.Implementations.BlockingRedirection;

namespace NetStalker.Tests.ServiceSpecs
{
public class BlockerRedirectorSpec
{
[Theory, AutoServiceData]
public void Can_Block_A_Device(IRuleService ruleService, IPcapDeviceManager pcapDeviceManager, Device device)
public void Can_Block_A_Device(IRuleService ruleService, IPcapDeviceManager pcapDeviceManager, IMessageBusService messageBusService, Device device)
{
var sut = new BlockerRedirector(ruleService, pcapDeviceManager);
var sut = new BlockerRedirector(ruleService, pcapDeviceManager, messageBusService);

sut.Block(device);

Expand All @@ -22,9 +23,9 @@ public void Can_Block_A_Device(IRuleService ruleService, IPcapDeviceManager pcap
}

[Theory, AutoServiceData]
public void Can_UnBlock_A_Device(IRuleService ruleService, IPcapDeviceManager pcapDeviceManager, Device device)
public void Can_UnBlock_A_Device(IRuleService ruleService, IPcapDeviceManager pcapDeviceManager, IMessageBusService messageBusService, Device device)
{
var sut = new BlockerRedirector(ruleService, pcapDeviceManager);
var sut = new BlockerRedirector(ruleService, pcapDeviceManager, messageBusService);

sut.UnBlock(device);

Expand All @@ -35,9 +36,9 @@ public void Can_UnBlock_A_Device(IRuleService ruleService, IPcapDeviceManager pc
}

[Theory, AutoServiceData]
public void Can_Redirect_A_Device(IRuleService ruleService, IPcapDeviceManager pcapDeviceManager, Device device)
public void Can_Redirect_A_Device(IRuleService ruleService, IPcapDeviceManager pcapDeviceManager, IMessageBusService messageBusService, Device device)
{
var sut = new BlockerRedirector(ruleService, pcapDeviceManager);
var sut = new BlockerRedirector(ruleService, pcapDeviceManager, messageBusService);

sut.Redirect(device);

Expand All @@ -48,9 +49,9 @@ public void Can_Redirect_A_Device(IRuleService ruleService, IPcapDeviceManager p
}

[Theory, AutoServiceData]
public void Can_UnRedirect_A_Device(IRuleService ruleService, IPcapDeviceManager pcapDeviceManager, Device device)
public void Can_UnRedirect_A_Device(IRuleService ruleService, IPcapDeviceManager pcapDeviceManager, IMessageBusService messageBusService, Device device)
{
var sut = new BlockerRedirector(ruleService, pcapDeviceManager);
var sut = new BlockerRedirector(ruleService, pcapDeviceManager, messageBusService);

sut.UnRedirect(device);

Expand All @@ -61,9 +62,9 @@ public void Can_UnRedirect_A_Device(IRuleService ruleService, IPcapDeviceManager
}

[Theory, AutoServiceData]
public void Can_Limit_A_Device(IRuleService ruleService, IPcapDeviceManager pcapDeviceManager, Device device)
public void Can_Limit_A_Device(IRuleService ruleService, IPcapDeviceManager pcapDeviceManager, IMessageBusService messageBusService, Device device)
{
var sut = new BlockerRedirector(ruleService, pcapDeviceManager);
var sut = new BlockerRedirector(ruleService, pcapDeviceManager, messageBusService);

sut.Limit(device, 15, 12);

Expand Down
9 changes: 5 additions & 4 deletions NetStalker.Tests/ServiceSpecs/DeviceScannerSpec.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
using FluentAssertions;
using NetStalker.Tests.AutoData;
using NetStalkerAvalonia.Core.Services;
using NetStalkerAvalonia.Core.Services.Implementations;
using NetStalkerAvalonia.Core.Services.Implementations.DeviceScanning;

namespace NetStalker.Tests.ServiceSpecs
{
public class DeviceScannerSpec
{
[Theory, AutoServiceData]
public void Can_Start_Scanner(IPcapDeviceManager pcapDeviceManager, IDeviceNameResolver deviceNameResolver, IDeviceTypeIdentifier deviceTypeIdentifier)
public void Can_Start_Scanner(IPcapDeviceManager pcapDeviceManager, IDeviceNameResolver deviceNameResolver, IDeviceTypeIdentifier deviceTypeIdentifier, IMessageBusService messageBusService)
{
var sut = new DeviceScanner(pcapDeviceManager, deviceNameResolver, deviceTypeIdentifier);
var sut = new DeviceScanner(pcapDeviceManager, deviceNameResolver, deviceTypeIdentifier, messageBusService);

var action = () => sut.Scan();

Expand All @@ -19,9 +20,9 @@ public void Can_Start_Scanner(IPcapDeviceManager pcapDeviceManager, IDeviceNameR
}

[Theory, AutoServiceData]
public void Can_Do_A_Scan_Refresh(IPcapDeviceManager pcapDeviceManager, IDeviceNameResolver deviceNameResolver, IDeviceTypeIdentifier deviceTypeIdentifier)
public void Can_Do_A_Scan_Refresh(IPcapDeviceManager pcapDeviceManager, IDeviceNameResolver deviceNameResolver, IDeviceTypeIdentifier deviceTypeIdentifier, IMessageBusService messageBusService)
{
var sut = new DeviceScanner(pcapDeviceManager, deviceNameResolver, deviceTypeIdentifier);
var sut = new DeviceScanner(pcapDeviceManager, deviceNameResolver, deviceTypeIdentifier, messageBusService);
sut.Scan();

var action = () => sut.Refresh();
Expand Down
6 changes: 2 additions & 4 deletions NetStalker.Tests/ViewModelTests/MainViewModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ public class MainViewModelTests
#region Routing

[Theory, AutoServiceData]
public void CanNavigateToRules(IRouter router, RuleBuilderViewModel ruleBuilderViewModel)
public void CanNavigateToRules(IRouter router, HomeViewModel homeViewModel, RuleBuilderViewModel ruleBuilderViewModel)
{
var sut = new MainViewModel(router, null, null, null, null, null, null, ruleBuilderViewModel, null, null, null);
var sut = new MainViewModel(router, homeViewModel, null, null, ruleBuilderViewModel, null, null, null);

sut.GoToRules.Execute();

sut.Router.CurrentViewModel.Subscribe(x => x.Should().Be(ruleBuilderViewModel));

sut.Dispose();
}

#endregion
Expand Down
4 changes: 2 additions & 2 deletions NetStalkerAvalonia.Core/App.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
xmlns:helpers="using:NetStalkerAvalonia.Core.Helpers"
x:Class="NetStalkerAvalonia.Core.App"
RequestedThemeVariant="Dark">

<Application.DataTemplates>
<helpers:ViewLocator/>
<helpers:ViewLocator />
</Application.DataTemplates>

<Application.Resources>
Expand All @@ -16,7 +17,6 @@
<StyleInclude Source="/Views/Components/GenericIconButton.axaml"/>
<StyleInclude Source="avares:https://Avalonia.Controls.DataGrid/Themes/Fluent.xaml"/>
<StyleInclude Source="/Views/Components/NavButton.axaml"/>
<StyleInclude Source="/Views/Components/InformationBubble.axaml"/>
</Application.Styles>

<TrayIcon.Icons>
Expand Down
8 changes: 0 additions & 8 deletions NetStalkerAvalonia.Core/NetStalkerAvalonia.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@
<ItemGroup>
<AvaloniaResource Include="Assets\**" />
</ItemGroup>
<ItemGroup>
<Content Remove="Views\TestView.axaml" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Assets\manuf.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand All @@ -56,9 +53,4 @@
<ItemGroup>
<Folder Include="Views\DataTemplates\" />
</ItemGroup>
<ItemGroup>
<AvaloniaXaml Update="Views\TestView.axaml">
<SubType>Designer</SubType>
</AvaloniaXaml>
</ItemGroup>
</Project>
6 changes: 0 additions & 6 deletions NetStalkerAvalonia.Core/ViewModels/TestViewModel.cs

This file was deleted.