diff --git a/Test/RemotePcapTests.cs b/Test/RemotePcapTests.cs index ce257783..2ad9d4da 100644 --- a/Test/RemotePcapTests.cs +++ b/Test/RemotePcapTests.cs @@ -13,7 +13,6 @@ using static Test.TestHelper; using System.ComponentModel; using CategoryAttribute = NUnit.Framework.CategoryAttribute; -using System.Runtime.Versioning; namespace Test { @@ -64,10 +63,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 09a3a372..5f84b684 100644 --- a/Test/TestUser.cs +++ b/Test/TestUser.cs @@ -1,43 +1,83 @@ // Copyright 2020-2021 Ayoub Kaanich // SPDX-License-Identifier: MIT - +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"; + public const string Username = "sharppcaptestuser"; public const string Password = "password"; 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 { - user.Save(); + Delete(); + var ctx = new PrincipalContext(ContextType.Machine); + using (var user = new UserPrincipal(ctx, Username, Password, true)) + { + user.Save(); + } + return true; + } + catch (PrincipalException) + { + return false; } - return true; } - catch (PrincipalException) + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { - return false; + Bash("useradd", Username, "--groups", "sudo"); + Bash("bash", "-c", $"\"echo -e {Username}:{Password} | chpasswd\""); } + // OS not supported + return false; + } + + private static void Bash(string cmd, params string[] args) + { + var arguments = string.Join(" ", args); + var info = new ProcessStartInfo + { + FileName = cmd, + Arguments = arguments, + 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(); + if (!process.WaitForExit(10000)) + { + throw new TimeoutException($"Command '{cmd} {arguments}' timed out"); + } + Assert.That(process.ExitCode, Is.Zero); } 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); + } } } } diff --git a/codecov.yml b/codecov.yml index 4c534dac..06d21bae 100644 --- a/codecov.yml +++ b/codecov.yml @@ -5,9 +5,9 @@ codecov: # Allow collecting coverage even if some CIs fail require_ci_to_pass: false 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