From 8878d8705f0b0fcddb3979194340ca39df897580 Mon Sep 17 00:00:00 2001 From: StevenWdV Date: Sat, 20 Nov 2021 18:09:09 +0100 Subject: Add C bindings and a C# wrapper --- .../EduVpnCommonTests/EduVpnCommonTests.csproj | 19 ++++++ wrappers/csharp/EduVpnCommonTests/VerifyTests.cs | 72 ++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 wrappers/csharp/EduVpnCommonTests/EduVpnCommonTests.csproj create mode 100644 wrappers/csharp/EduVpnCommonTests/VerifyTests.cs (limited to 'wrappers/csharp/EduVpnCommonTests') diff --git a/wrappers/csharp/EduVpnCommonTests/EduVpnCommonTests.csproj b/wrappers/csharp/EduVpnCommonTests/EduVpnCommonTests.csproj new file mode 100644 index 0000000..cf58249 --- /dev/null +++ b/wrappers/csharp/EduVpnCommonTests/EduVpnCommonTests.csproj @@ -0,0 +1,19 @@ + + + + net5.0 + false + + + + + + + + + + + + + + diff --git a/wrappers/csharp/EduVpnCommonTests/VerifyTests.cs b/wrappers/csharp/EduVpnCommonTests/VerifyTests.cs new file mode 100644 index 0000000..933f2bc --- /dev/null +++ b/wrappers/csharp/EduVpnCommonTests/VerifyTests.cs @@ -0,0 +1,72 @@ +using System; +using System.IO; +using System.Linq; +using EduVpnCommon; +using NUnit.Framework; + +namespace EduVpnCommonTests +{ + [TestFixture(TestOf = typeof(Discovery)), Parallelizable] + public class VerifyTests + { + // Relative to e.g. EduVpnCommonTests/bin/Debug/net5.0 + readonly string testDataDir_ = $"{TestContext.CurrentContext.TestDirectory}/../../../../../../test_data"; + + [OneTimeSetUp] + public void OneTimeSetUp() => + Discovery.InsecureTestingSetExtraKey(File.ReadLines($"{testDataDir_}/dummy/public.key").Last()); + + [Test] + [TestCase("dummy/server_list.json.minisig", "dummy/server_list.json", "server_list.json")] + [TestCase("dummy/organization_list.json.minisig", "dummy/organization_list.json", "organization_list.json")] + public void TestValid(string sigFile, string jsonFile, string expectedFileName) => + Discovery.Verify( + File.ReadAllBytes($"{testDataDir_}/{sigFile}"), + File.ReadAllBytes($"{testDataDir_}/{jsonFile}"), + expectedFileName, + DateTimeOffset.UnixEpoch); + + [Test] + [TestCase("dummy/random.txt", "dummy/server_list.json", "server_list.json")] + public void TestInvalidSignature(string sigFile, string jsonFile, string expectedFileName) => + Assert.Throws(Is.TypeOf() + .And.Property(nameof(VerifyException.Code)).EqualTo(VerifyErrorCode.ErrInvalidSignature), + () => Discovery.Verify( + File.ReadAllBytes($"{testDataDir_}/{sigFile}"), + File.ReadAllBytes($"{testDataDir_}/{jsonFile}"), + expectedFileName, + DateTimeOffset.UnixEpoch)); + + [Test] + [TestCase("dummy/server_list.json.wrong_key.minisig", "dummy/server_list.json", "server_list.json")] + public void TestWrongKey(string sigFile, string jsonFile, string expectedFileName) => + Assert.Throws(Is.TypeOf() + .And.Property(nameof(VerifyException.Code)).EqualTo(VerifyErrorCode.ErrInvalidSignatureUnknownKey), + () => Discovery.Verify( + File.ReadAllBytes($"{testDataDir_}/{sigFile}"), + File.ReadAllBytes($"{testDataDir_}/{jsonFile}"), + expectedFileName, + DateTimeOffset.UnixEpoch)); + + [Test] + [TestCase("dummy/server_list.json.minisig", "dummy/server_list.json", "server_list.json")] + public void TestOldSignature(string sigFile, string jsonFile, string expectedFileName) => + Assert.Throws(Is.TypeOf() + .And.Property(nameof(VerifyException.Code)).EqualTo(VerifyErrorCode.ErrTooOld), + () => Discovery.Verify( + File.ReadAllBytes($"{testDataDir_}/{sigFile}"), + File.ReadAllBytes($"{testDataDir_}/{jsonFile}"), + expectedFileName, + DateTimeOffset.MaxValue)); + + [Test] + [TestCase("dummy/other_list.json.minisig", "dummy/other_list.json", "other_list.json")] + public void TestUnknownExpectedFile(string sigFile, string jsonFile, string expectedFileName) => + Assert.Throws( + () => Discovery.Verify( + File.ReadAllBytes($"{testDataDir_}/{sigFile}"), + File.ReadAllBytes($"{testDataDir_}/{jsonFile}"), + expectedFileName, + DateTimeOffset.UnixEpoch)); + } +} \ No newline at end of file -- cgit v1.2.3