-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master' into renovate/nunit-4.x
- Loading branch information
Showing
5 changed files
with
63 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,83 @@ | ||
// Copyright 2020-2021 Ayoub Kaanich <[email protected]> | ||
// 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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# Copyright 2023 Ayoub Kaanich <[email protected]> | ||
# SPDX-License-Identifier: MIT | ||
reuse==5.0.0 | ||
reuse==5.0.2 |