Skip to content

Commit

Permalink
decouple AppLibTests
Browse files Browse the repository at this point in the history
  • Loading branch information
trudyhood committed Dec 15, 2024
1 parent dfbd0f2 commit ce0157f
Show file tree
Hide file tree
Showing 22 changed files with 233 additions and 183 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using VpnHood.Core.Client.Device;

namespace VpnHood.Core.Client.Device.WinDivert;
namespace VpnHood.Core.Client.Device.WinDivert;

public class WinDivertDevice : IDevice
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
using PacketDotNet;
using SharpPcap;
using SharpPcap.WinDivert;
using VpnHood.Core.Client.Device;
using VpnHood.Core.Client.Device.WinDivert;
using VpnHood.Core.Common.Logging;
using VpnHood.Core.Common.Net;
using ProtocolType = PacketDotNet.ProtocolType;
Expand Down
1 change: 0 additions & 1 deletion Tests/VpnHood.AppLib.Test/MSTestSettings.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using VpnHood.Core.Common.Exceptions;
using VpnHood.Test.AccessManagers;

namespace VpnHood.Test.Providers;
namespace VpnHood.AppLib.Test.Providers;

public class TestAdProvider(TestAccessManager accessManager, AppAdType adType = AppAdType.RewardedAd) : IAppAdProvider
{
Expand Down
10 changes: 0 additions & 10 deletions Tests/VpnHood.AppLib.Test/Test1.cs

This file was deleted.

58 changes: 58 additions & 0 deletions Tests/VpnHood.AppLib.Test/TestAppHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using VpnHood.Core.Client.Device;
using VpnHood.Core.Common.Net;
using VpnHood.Core.Common.Utils;
using VpnHood.Test;
using VpnHood.Test.Device;
using VpnHood.Test.Providers;

namespace VpnHood.AppLib.Test;

public class TestAppHelper : TestHelper
{
public static Task WaitForAppState(VpnHoodApp app, AppConnectionState connectionSate, int timeout = 5000)
{
return VhTestUtil.AssertEqualsWait(connectionSate, () => app.State.ConnectionState,
"App state didn't reach the expected value.", timeout);
}

public static AppOptions CreateAppOptions()
{
var tracker = new TestTrackerProvider();
var appOptions = new AppOptions("com.vpnhood.client.test", isDebugMode: true) {
StorageFolderPath = Path.Combine(WorkingPath, "AppData_" + Guid.CreateVersion7()),
SessionTimeout = TimeSpan.FromSeconds(2),
Ga4MeasurementId = null,
Tracker = tracker,
UseInternalLocationService = false,
UseExternalLocationService = false,
AllowEndPointTracker = true,
LogVerbose = LogVerbose,
ServerQueryTimeout = TimeSpan.FromSeconds(2),
AutoDiagnose = false,
SingleLineConsoleLog = false,
CanExtendByRewardedAdThreshold = TimeSpan.Zero,
AdOptions = new AppAdOptions {
ShowAdPostDelay = TimeSpan.Zero,
LoadAdPostDelay = TimeSpan.Zero
}
};
return appOptions;
}

public static VpnHoodApp CreateClientApp(AppOptions? appOptions = default, IDevice? device = default)
{
appOptions ??= CreateAppOptions();
device ??= new TestDevice(() => new TestNullPacketCapture());

//create app
var clientApp = VpnHoodApp.Init(device, appOptions);
clientApp.Diagnoser.HttpTimeout = 2000;
clientApp.Diagnoser.NsTimeout = 2000;
clientApp.UserSettings.PacketCaptureIncludeIpRanges = TestIpAddresses.Select(x => new IpRange(x)).ToArray();
clientApp.UserSettings.Logging.LogAnonymous = false;
clientApp.TcpTimeout = TimeSpan.FromSeconds(2);
ActiveUiContext.Context = new TestAppUiContext();

return clientApp;
}
}
20 changes: 20 additions & 0 deletions Tests/VpnHood.AppLib.Test/TestAssembly.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using VpnHood.Test;

namespace VpnHood.AppLib.Test;

[TestClass]
public class TestAssembly
{
[AssemblyInitialize]
public static void AssemblyInit(TestContext _)
{
TestHelper.Init();
}

[AssemblyCleanup]
public static void AssemblyCleanup()
{
TestHelper.Cleanup();
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using VpnHood.AppLib;
using VpnHood.AppLib.Abstractions;
using VpnHood.AppLib.Test.Providers;
using VpnHood.Core.Client.Device;
using VpnHood.Core.Common.Exceptions;
using VpnHood.Core.Common.Messaging;
using VpnHood.Core.Common.Tokens;
using VpnHood.Test;
using VpnHood.Test.Device;
using VpnHood.Test.Providers;
using VpnHood.Test.Tests;

namespace VpnHood.Test.Tests;
namespace VpnHood.AppLib.Test.Tests;

[TestClass]
public class AdTest : TestBase
Expand All @@ -24,11 +25,11 @@ public async Task flexible_ad_should_not_close_session_if_load_ad_failed()
var accessToken = accessManager.AccessTokenService.Create(adRequirement: AdRequirement.Flexible);

// create client app
var appOptions = TestHelper.CreateAppOptions();
var appOptions = TestAppHelper.CreateAppOptions();
var adProvider = new TestAdProvider(accessManager);
var adProviderItem = new AppAdProviderItem { AdProvider = new TestAdProvider(accessManager, AppAdType.InterstitialAd) };
appOptions.AdProviderItems = [adProviderItem];
await using var app = TestHelper.CreateClientApp(appOptions: appOptions);
await using var app = TestAppHelper.CreateClientApp(appOptions: appOptions);
adProvider.FailLoad = true;
adProvider.FailShow = true; // should not reach this state

Expand All @@ -45,18 +46,18 @@ public async Task flexible_ad_should_close_session_if_display_ad_failed()
await using var server = await TestHelper.CreateServer(accessManager);

// create client app
var appOptions = TestHelper.CreateAppOptions();
var appOptions = TestAppHelper.CreateAppOptions();
var adProviderItem = new AppAdProviderItem { AdProvider = new TestAdProvider(accessManager, AppAdType.InterstitialAd ) };
appOptions.AdProviderItems = [adProviderItem];
await using var app = TestHelper.CreateClientApp(appOptions: appOptions);
await using var app = TestAppHelper.CreateClientApp(appOptions: appOptions);
ActiveUiContext.Context = null;
//adProviderItem.FailShow = true;

// connect
var token = accessManager.CreateToken(adRequirement: AdRequirement.Flexible);
var clientProfile = app.ClientProfileService.ImportAccessKey(token.ToAccessKey());
await Assert.ThrowsExceptionAsync<ShowAdNoUiException>(() => app.Connect(clientProfile.ClientProfileId));
await TestHelper.WaitForAppState(app, AppConnectionState.None);
await TestAppHelper.WaitForAppState(app, AppConnectionState.None);
}

[TestMethod]
Expand All @@ -67,16 +68,16 @@ public async Task flexible_ad_should_not_be_displayed_on_trial()
await using var server = await TestHelper.CreateServer(accessManager);

// create client app
var appOptions = TestHelper.CreateAppOptions();
await using var app = TestHelper.CreateClientApp(appOptions: appOptions);
var appOptions = TestAppHelper.CreateAppOptions();
await using var app = TestAppHelper.CreateClientApp(appOptions: appOptions);
ActiveUiContext.Context = null;
//adProviderItem.FailShow = true;

// connect
var token = accessManager.CreateToken(adRequirement: AdRequirement.Flexible);
var clientProfile = app.ClientProfileService.ImportAccessKey(token.ToAccessKey());
await app.Connect(clientProfile.ClientProfileId, planId: ConnectPlanId.PremiumByTrial);
await TestHelper.WaitForAppState(app, AppConnectionState.Connected);
await TestAppHelper.WaitForAppState(app, AppConnectionState.Connected);
}

[TestMethod]
Expand All @@ -87,10 +88,10 @@ public async Task Session_must_be_closed_after_few_minutes_if_ad_is_not_accepted
await using var server = await TestHelper.CreateServer(accessManager);

// create client app
var appOptions = TestHelper.CreateAppOptions();
var appOptions = TestAppHelper.CreateAppOptions();
var adProviderItem = new AppAdProviderItem { AdProvider = new TestAdProvider(accessManager) };
appOptions.AdProviderItems = [adProviderItem];
await using var app = TestHelper.CreateClientApp(appOptions: appOptions);
await using var app = TestAppHelper.CreateClientApp(appOptions: appOptions);
ActiveUiContext.Context = null;
//adProviderItem.FailShow = true;

Expand All @@ -100,7 +101,7 @@ public async Task Session_must_be_closed_after_few_minutes_if_ad_is_not_accepted
await Assert.ThrowsExceptionAsync<ShowAdNoUiException>(() =>
app.Connect(clientProfile.ClientProfileId, ConnectPlanId.PremiumByRewardedAd));

await TestHelper.WaitForAppState(app, AppConnectionState.None);
await TestAppHelper.WaitForAppState(app, AppConnectionState.None);
}

[TestMethod]
Expand All @@ -115,10 +116,10 @@ public async Task RewardedAd_expiration_must_be_increased_by_plan_id(bool accept
accessManager.RejectAllAds = !acceptAd;

// create client app
var appOptions = TestHelper.CreateAppOptions();
var appOptions = TestAppHelper.CreateAppOptions();
var adProviderItem = new AppAdProviderItem { AdProvider = new TestAdProvider(accessManager) };
appOptions.AdProviderItems = [adProviderItem];
await using var app = TestHelper.CreateClientApp(appOptions: appOptions);
await using var app = TestAppHelper.CreateClientApp(appOptions: appOptions);

// create access token
var token = accessManager.CreateToken();
Expand Down Expand Up @@ -147,11 +148,11 @@ public async Task RewardedAd_expiration_must_be_increased_by_user(bool acceptAd)
accessManager.RejectAllAds = !acceptAd;

// create client app
var appOptions = TestHelper.CreateAppOptions();
var appOptions = TestAppHelper.CreateAppOptions();
var adProviderItem = new AppAdProviderItem { AdProvider = new TestAdProvider(accessManager) };
appOptions.AdProviderItems = [adProviderItem];
var device = new TestDevice(() => new NullPacketCapture { CanDetectInProcessPacket = true });
await using var app = TestHelper.CreateClientApp(appOptions: appOptions, device: device);
await using var app = TestAppHelper.CreateClientApp(appOptions: appOptions, device: device);

// create access token
var token = accessManager.CreateToken();
Expand All @@ -173,7 +174,7 @@ public async Task RewardedAd_expiration_must_be_increased_by_user(bool acceptAd)
var ex = await Assert.ThrowsExceptionAsync<SessionException>(() => app.ExtendByRewardedAd(CancellationToken.None));
Assert.AreEqual(SessionErrorCode.RewardedAdRejected, ex.SessionResponse.ErrorCode);
await Task.Delay(500);
await TestHelper.WaitForAppState(app, AppConnectionState.Connected);
await TestAppHelper.WaitForAppState(app, AppConnectionState.Connected);
}
}

Expand All @@ -187,11 +188,11 @@ public async Task RewardedAd_should_toggle_by_canDetectInProcessPacket(bool canD
accessManager.CanExtendPremiumByAd = true;

// create client app
var appOptions = TestHelper.CreateAppOptions();
var appOptions = TestAppHelper.CreateAppOptions();
var adProviderItem = new AppAdProviderItem { AdProvider = new TestAdProvider(accessManager) };
appOptions.AdProviderItems = [adProviderItem];
var device = new TestDevice(() => new NullPacketCapture { CanDetectInProcessPacket = canDetectInProcessPacket });
await using var app = TestHelper.CreateClientApp(device: device, appOptions: appOptions);
await using var app = TestAppHelper.CreateClientApp(device: device, appOptions: appOptions);

// create token
var token = accessManager.CreateToken();
Expand All @@ -217,11 +218,11 @@ public async Task RewardedAd_should_toggle_by_access_manager(bool enable)
accessManager.CanExtendPremiumByAd = enable;

// create client app
var appOptions = TestHelper.CreateAppOptions();
var appOptions = TestAppHelper.CreateAppOptions();
var adProviderItem = new AppAdProviderItem { AdProvider = new TestAdProvider(accessManager) };
appOptions.AdProviderItems = [adProviderItem];
var device = new TestDevice(() => new NullPacketCapture { CanDetectInProcessPacket = true });
await using var app = TestHelper.CreateClientApp(device: device, appOptions: appOptions);
await using var app = TestAppHelper.CreateClientApp(device: device, appOptions: appOptions);

// create token
var token = accessManager.CreateToken();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System.Text.Json;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using VpnHood.AppLib;
using VpnHood.Core.Common.Utils;
using VpnHood.Test;
using VpnHood.Test.Tests;

namespace VpnHood.Test.Tests;
namespace VpnHood.AppLib.Test.Tests;

[TestClass]
public class CheckNewVersionTest : TestBase
Expand Down Expand Up @@ -33,9 +34,9 @@ private static void SetNewRelease(Version version, DateTime releaseDate, TimeSpa
[TestMethod]
public async Task Remote_is_unknown_if_remote_is_unreachable()
{
var appOptions = TestHelper.CreateAppOptions();
var appOptions = TestAppHelper.CreateAppOptions();
appOptions.UpdateInfoUrl = new Uri("https://localhost:39999");
await using var app = TestHelper.CreateClientApp(appOptions: appOptions);
await using var app = TestAppHelper.CreateClientApp(appOptions: appOptions);

await Task.Delay(1000);
await VhTestUtil.AssertEqualsWait(VersionStatus.Unknown, () => app.State.VersionStatus);
Expand All @@ -47,10 +48,10 @@ public async Task Current_is_latest()
{
SetNewRelease(CurrentAppVersion, DateTime.UtcNow, TimeSpan.Zero);

var appOptions = TestHelper.CreateAppOptions();
var appOptions = TestAppHelper.CreateAppOptions();
appOptions.UpdateInfoUrl = TestHelper.WebServer.FileHttpUrl1;
appOptions.VersionCheckInterval = TimeSpan.FromMilliseconds(500);
await using var app = TestHelper.CreateClientApp(appOptions: appOptions);
await using var app = TestAppHelper.CreateClientApp(appOptions: appOptions);

await VhTestUtil.AssertEqualsWait(VersionStatus.Latest, () => app.State.VersionStatus);
}
Expand All @@ -62,10 +63,10 @@ public async Task Current_is_deprecated()
DateTime.UtcNow,
TimeSpan.Zero, CurrentAppVersion);

var appOptions = TestHelper.CreateAppOptions();
var appOptions = TestAppHelper.CreateAppOptions();
appOptions.UpdateInfoUrl = TestHelper.WebServer.FileHttpUrl1;
appOptions.VersionCheckInterval = TimeSpan.FromMilliseconds(500);
await using var app = TestHelper.CreateClientApp(appOptions: appOptions);
await using var app = TestAppHelper.CreateClientApp(appOptions: appOptions);

await VhTestUtil.AssertEqualsWait(VersionStatus.Deprecated, () => app.State.VersionStatus);
}
Expand All @@ -76,10 +77,10 @@ public async Task Current_is_old_by_job()
SetNewRelease(CurrentAppVersion, DateTime.UtcNow, TimeSpan.Zero);

// create client
var appOptions = TestHelper.CreateAppOptions();
var appOptions = TestAppHelper.CreateAppOptions();
appOptions.UpdateInfoUrl = TestHelper.WebServer.FileHttpUrl1;
appOptions.VersionCheckInterval = TimeSpan.FromMilliseconds(500);
await using var app = TestHelper.CreateClientApp(appOptions: appOptions);
await using var app = TestAppHelper.CreateClientApp(appOptions: appOptions);

// version should be latest
await VhTestUtil.AssertEqualsWait(VersionStatus.Latest, () => app.State.VersionStatus);
Expand All @@ -96,10 +97,10 @@ public async Task Current_is_old_but_wait_for_notification_delay()
SetNewRelease(new Version(CurrentAppVersion.Major, CurrentAppVersion.Minor, CurrentAppVersion.Build + 1),
DateTime.UtcNow, TimeSpan.FromSeconds(2));

var appOptions = TestHelper.CreateAppOptions();
var appOptions = TestAppHelper.CreateAppOptions();
appOptions.UpdateInfoUrl = TestHelper.WebServer.FileHttpUrl1;
appOptions.VersionCheckInterval = TimeSpan.FromMilliseconds(500);
await using var app = TestHelper.CreateClientApp(appOptions: appOptions);
await using var app = TestAppHelper.CreateClientApp(appOptions: appOptions);

await VhTestUtil.AssertEqualsWait(VersionStatus.Latest, () => app.State.VersionStatus);
await VhTestUtil.AssertEqualsWait(VersionStatus.Old, () => app.State.VersionStatus);
Expand All @@ -111,10 +112,10 @@ public async Task Current_is_old_before_connect_to_vpn()
SetNewRelease(new Version(CurrentAppVersion.Major, CurrentAppVersion.Minor, CurrentAppVersion.Build + 1),
DateTime.UtcNow, TimeSpan.Zero);

var appOptions = TestHelper.CreateAppOptions();
var appOptions = TestAppHelper.CreateAppOptions();
appOptions.UpdateInfoUrl = TestHelper.WebServer.FileHttpUrl1;
appOptions.VersionCheckInterval = TimeSpan.FromMilliseconds(500);
await using var app = TestHelper.CreateClientApp(appOptions: appOptions);
await using var app = TestAppHelper.CreateClientApp(appOptions: appOptions);

await VhTestUtil.AssertEqualsWait(VersionStatus.Old, () => app.State.VersionStatus);
}
Expand All @@ -130,9 +131,9 @@ public async Task Current_is_old_after_connect_to_vpn()
TestHelper.WebServer.FileContent1 = Guid.NewGuid().ToString();

// create client app
var appOptions = TestHelper.CreateAppOptions();
var appOptions = TestAppHelper.CreateAppOptions();
appOptions.UpdateInfoUrl = TestHelper.WebServer.FileHttpUrl1;
await using var app = TestHelper.CreateClientApp(appOptions: appOptions);
await using var app = TestAppHelper.CreateClientApp(appOptions: appOptions);
var clientProfile = app.ClientProfileService.ImportAccessKey(token.ToAccessKey());

await Task.Delay(1000);
Expand All @@ -142,7 +143,7 @@ public async Task Current_is_old_after_connect_to_vpn()
SetNewRelease(new Version(CurrentAppVersion.Major, CurrentAppVersion.Minor, CurrentAppVersion.Build + 1),
DateTime.UtcNow, TimeSpan.Zero);
await app.Connect(clientProfile.ClientProfileId);
await TestHelper.WaitForAppState(app, AppConnectionState.Connected);
await TestAppHelper.WaitForAppState(app, AppConnectionState.Connected);
await VhTestUtil.AssertEqualsWait(VersionStatus.Old, () => app.State.VersionStatus);
}
}
Loading

0 comments on commit ce0157f

Please sign in to comment.