|
1 | 1 | // Copyright 2020-2021 Ayoub Kaanich <[email protected]>
|
2 | 2 | // SPDX-License-Identifier: MIT
|
3 |
| - |
| 3 | +using NUnit.Framework; |
4 | 4 | using System;
|
| 5 | +using System.Diagnostics; |
5 | 6 | using System.DirectoryServices.AccountManagement;
|
6 |
| -using System.Runtime.Versioning; |
| 7 | +using System.Runtime.InteropServices; |
7 | 8 |
|
8 | 9 | namespace Test
|
9 | 10 | {
|
10 |
| -#if NET |
11 |
| - [SupportedOSPlatform("windows")] |
12 |
| -#endif |
13 | 11 | public static class TestUser
|
14 | 12 | {
|
15 |
| - public const string Username = "SharpPcap.Test.User"; |
| 13 | + public const string Username = "sharppcaptestuser"; |
16 | 14 | public const string Password = "password";
|
17 | 15 |
|
18 | 16 | public static bool Create()
|
19 | 17 | {
|
20 |
| - try |
| 18 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
21 | 19 | {
|
22 |
| - Delete(); |
23 |
| - var ctx = new PrincipalContext(ContextType.Machine); |
24 |
| - using (var user = new UserPrincipal(ctx, Username, Password, true)) |
| 20 | + try |
25 | 21 | {
|
26 |
| - user.Save(); |
| 22 | + Delete(); |
| 23 | + var ctx = new PrincipalContext(ContextType.Machine); |
| 24 | + using (var user = new UserPrincipal(ctx, Username, Password, true)) |
| 25 | + { |
| 26 | + user.Save(); |
| 27 | + } |
| 28 | + return true; |
| 29 | + } |
| 30 | + catch (PrincipalException) |
| 31 | + { |
| 32 | + return false; |
27 | 33 | }
|
28 |
| - return true; |
29 | 34 | }
|
30 |
| - catch (PrincipalException) |
| 35 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) |
31 | 36 | {
|
32 |
| - return false; |
| 37 | + Bash("useradd", Username, "--groups", "sudo"); |
| 38 | + Bash("bash", "-c", $"\"echo -e {Username}:{Password} | chpasswd\""); |
33 | 39 | }
|
| 40 | + // OS not supported |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + private static void Bash(string cmd, params string[] args) |
| 45 | + { |
| 46 | + var arguments = string.Join(" ", args); |
| 47 | + var info = new ProcessStartInfo |
| 48 | + { |
| 49 | + FileName = cmd, |
| 50 | + Arguments = arguments, |
| 51 | + UseShellExecute = false, |
| 52 | + RedirectStandardOutput = true, |
| 53 | + RedirectStandardError = true |
| 54 | + }; |
| 55 | + var process = Process.Start(info); |
| 56 | + |
| 57 | + process.OutputDataReceived += (s, e) => Console.Out.WriteLine(e.Data); |
| 58 | + process.ErrorDataReceived += (s, e) => Console.Error.WriteLine(e.Data); |
| 59 | + |
| 60 | + process.BeginOutputReadLine(); |
| 61 | + process.BeginErrorReadLine(); |
| 62 | + if (!process.WaitForExit(10000)) |
| 63 | + { |
| 64 | + throw new TimeoutException($"Command '{cmd} {arguments}' timed out"); |
| 65 | + } |
| 66 | + Assert.That(process.ExitCode, Is.Zero); |
34 | 67 | }
|
35 | 68 |
|
36 | 69 | public static void Delete()
|
37 | 70 | {
|
38 |
| - var ctx = new PrincipalContext(ContextType.Machine); |
39 |
| - var user = UserPrincipal.FindByIdentity(ctx, Username); |
40 |
| - user?.Delete(); |
| 71 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| 72 | + { |
| 73 | + var ctx = new PrincipalContext(ContextType.Machine); |
| 74 | + var user = UserPrincipal.FindByIdentity(ctx, Username); |
| 75 | + user?.Delete(); |
| 76 | + } |
| 77 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) |
| 78 | + { |
| 79 | + Bash("userdel", Username); |
| 80 | + } |
41 | 81 | }
|
42 | 82 | }
|
43 | 83 | }
|
0 commit comments