From e9b0fb6845e623a6a41b3a7a785b8562bbe4038c Mon Sep 17 00:00:00 2001 From: Ayoub Kaanich Date: Sat, 1 Apr 2023 14:52:47 +0200 Subject: [PATCH 1/8] RPCAP test with authentication in Linux --- Test/RemotePcapTests.cs | 5 ---- Test/TestUser.cs | 56 +++++++++++++++++++++++++++++------------ 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/Test/RemotePcapTests.cs b/Test/RemotePcapTests.cs index 65554f15..77a83806 100644 --- a/Test/RemotePcapTests.cs +++ b/Test/RemotePcapTests.cs @@ -10,7 +10,6 @@ using static Test.TestHelper; using System.ComponentModel; using CategoryAttribute = NUnit.Framework.CategoryAttribute; -using System.Runtime.Versioning; namespace Test { @@ -61,10 +60,6 @@ public void NpcapDeviceListNullAuthTest() /// if the test gets too long, it would be moved to its own file /// [Test] - [Platform("Win")] -#if NET - [SupportedOSPlatform("windows")] -#endif public void PwdAuthTest() { try diff --git a/Test/TestUser.cs b/Test/TestUser.cs index 78c5c2f3..5f6efae2 100644 --- a/Test/TestUser.cs +++ b/Test/TestUser.cs @@ -1,12 +1,11 @@ -using System; +using NUnit.Framework; +using System; +using System.Diagnostics; using System.DirectoryServices.AccountManagement; -using System.Runtime.Versioning; +using System.Runtime.InteropServices; namespace Test { -#if NET - [SupportedOSPlatform("windows")] -#endif public static class TestUser { public const string Username = "SharpPcap.Test.User"; @@ -14,27 +13,52 @@ public static class TestUser public static bool Create() { - try + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { - Delete(); - var ctx = new PrincipalContext(ContextType.Machine); - using (var user = new UserPrincipal(ctx, Username, Password, true)) + try + { + Delete(); + var ctx = new PrincipalContext(ContextType.Machine); + using (var user = new UserPrincipal(ctx, Username, Password, true)) + { + user.Save(); + } + return true; + } + catch (PrincipalException) { - user.Save(); + return false; } - return true; } - catch (PrincipalException) + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { - return false; + Bash("adduser", Username); + Bash("adduser", Username, "sudo"); + Bash("bash", "-c", $"\"echo -e {Username}:{Password} | chpasswd\""); } + // OS not supported + return false; + } + + private static void Bash(string cmd, params string[] args) + { + var process = Process.Start(cmd, args); + process.WaitForExit(); + Assert.AreEqual(process.ExitCode, 0); } public static void Delete() { - var ctx = new PrincipalContext(ContextType.Machine); - var user = UserPrincipal.FindByIdentity(ctx, Username); - user?.Delete(); + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + var ctx = new PrincipalContext(ContextType.Machine); + var user = UserPrincipal.FindByIdentity(ctx, Username); + user?.Delete(); + } + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + Bash("userdel", Username); + } } } } From bd238af06b57f07de76949353182125ca94e4c4a Mon Sep 17 00:00:00 2001 From: Ayoub Kaanich Date: Sat, 1 Apr 2023 16:09:04 +0200 Subject: [PATCH 2/8] Log on error --- Test/TestUser.cs | 16 +++++++++++++++- codecov.yml | 4 ++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Test/TestUser.cs b/Test/TestUser.cs index 5f6efae2..6aefe94b 100644 --- a/Test/TestUser.cs +++ b/Test/TestUser.cs @@ -42,7 +42,21 @@ public static bool Create() private static void Bash(string cmd, params string[] args) { - var process = Process.Start(cmd, args); + var info = new ProcessStartInfo + { + FileName = cmd, + Arguments = string.Join(' ', args), + UseShellExecute = false, + RedirectStandardOutput = true, + RedirectStandardError = true + }; + var process = Process.Start(info); + + process.OutputDataReceived += (s, e) => Console.Out.WriteLine(e.Data); + process.ErrorDataReceived += (s, e) => Console.Error.WriteLine(e.Data); + + process.BeginOutputReadLine(); + process.BeginErrorReadLine(); process.WaitForExit(); Assert.AreEqual(process.ExitCode, 0); } diff --git a/codecov.yml b/codecov.yml index 763aae00..aa4c8481 100644 --- a/codecov.yml +++ b/codecov.yml @@ -2,9 +2,9 @@ codecov: # Allow collecting coverage even if some CIs fail require_ci_to_pass: no notify: - # 4 for appveyor + # 3 for appveyor # 3 for azure pipelines # 1 for circleci # Total = 8 # Tolerate one of the services being down (worst case appveyor) - after_n_builds: 4 + after_n_builds: 3 From b1b7a5118fa59fb2f1d357d4fa4e58e4a7597c03 Mon Sep 17 00:00:00 2001 From: Ayoub Kaanich Date: Sat, 1 Apr 2023 17:00:01 +0200 Subject: [PATCH 3/8] Change test user username --- Test/TestUser.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Test/TestUser.cs b/Test/TestUser.cs index 6aefe94b..07f0eac7 100644 --- a/Test/TestUser.cs +++ b/Test/TestUser.cs @@ -8,7 +8,7 @@ namespace Test { public static class TestUser { - public const string Username = "SharpPcap.Test.User"; + public const string Username = "SharpPcap_Test_User"; public const string Password = "password"; public static bool Create() From aea02ba5910c29591830e2678231f6bf752a5521 Mon Sep 17 00:00:00 2001 From: Ayoub Kaanich Date: Sat, 1 Apr 2023 17:59:19 +0200 Subject: [PATCH 4/8] Lowercase no underscore --- Test/TestUser.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Test/TestUser.cs b/Test/TestUser.cs index 07f0eac7..5a0cefd7 100644 --- a/Test/TestUser.cs +++ b/Test/TestUser.cs @@ -8,7 +8,7 @@ namespace Test { public static class TestUser { - public const string Username = "SharpPcap_Test_User"; + public const string Username = "sharppcaptestuser"; public const string Password = "password"; public static bool Create() From f1a7f0fe46958eb7d4815934d6ad0cc64219ee56 Mon Sep 17 00:00:00 2001 From: Ayoub Kaanich Date: Sun, 2 Apr 2023 12:25:22 +0200 Subject: [PATCH 5/8] Fix build --- Test/TestUser.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Test/TestUser.cs b/Test/TestUser.cs index 5a0cefd7..f24d3cac 100644 --- a/Test/TestUser.cs +++ b/Test/TestUser.cs @@ -45,7 +45,7 @@ private static void Bash(string cmd, params string[] args) var info = new ProcessStartInfo { FileName = cmd, - Arguments = string.Join(' ', args), + Arguments = string.Join(" ", args), UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true From e1961ff4f5e811bce8849c6297600f7ce9c34b93 Mon Sep 17 00:00:00 2001 From: Ayoub Kaanich Date: Wed, 3 Jan 2024 16:22:15 +0100 Subject: [PATCH 6/8] Add timeout --- Test/TestUser.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Test/TestUser.cs b/Test/TestUser.cs index f24d3cac..ded22d92 100644 --- a/Test/TestUser.cs +++ b/Test/TestUser.cs @@ -42,10 +42,11 @@ public static bool Create() private static void Bash(string cmd, params string[] args) { + var arguments = string.Join(" ", args); var info = new ProcessStartInfo { FileName = cmd, - Arguments = string.Join(" ", args), + Arguments = arguments, UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true @@ -57,7 +58,10 @@ private static void Bash(string cmd, params string[] args) process.BeginOutputReadLine(); process.BeginErrorReadLine(); - process.WaitForExit(); + if (!process.WaitForExit(10000)) + { + throw new TimeoutException($"Command '{cmd} {arguments}' timed out"); + } Assert.AreEqual(process.ExitCode, 0); } From f2364f0c336089da951a929105755a034d75af7d Mon Sep 17 00:00:00 2001 From: Ayoub Kaanich Date: Wed, 3 Jan 2024 16:28:48 +0100 Subject: [PATCH 7/8] Fix Assert --- Test/TestUser.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Test/TestUser.cs b/Test/TestUser.cs index ded22d92..c184889f 100644 --- a/Test/TestUser.cs +++ b/Test/TestUser.cs @@ -62,7 +62,7 @@ private static void Bash(string cmd, params string[] args) { throw new TimeoutException($"Command '{cmd} {arguments}' timed out"); } - Assert.AreEqual(process.ExitCode, 0); + Assert.That(process.ExitCode, Is.Zero); } public static void Delete() From 37175e71b5e9956831a474b9e29ab2521aef33f8 Mon Sep 17 00:00:00 2001 From: Ayoub Kaanich Date: Thu, 4 Jan 2024 20:30:27 +0100 Subject: [PATCH 8/8] Use useradd instead of adduser --- Test/TestUser.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Test/TestUser.cs b/Test/TestUser.cs index c184889f..ba620c35 100644 --- a/Test/TestUser.cs +++ b/Test/TestUser.cs @@ -32,8 +32,7 @@ public static bool Create() } if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { - Bash("adduser", Username); - Bash("adduser", Username, "sudo"); + Bash("useradd", Username, "--groups", "sudo"); Bash("bash", "-c", $"\"echo -e {Username}:{Password} | chpasswd\""); } // OS not supported