Skip to content

Commit 4e9464d

Browse files
authored
RPCAP test with authentication in Linux (#455)
* RPCAP test with authentication in Linux * Log on error * Change test user username * Lowercase no underscore * Fix build * Add timeout * Fix Assert * Use useradd instead of adduser * Fix CI * Fix test
1 parent aca73ac commit 4e9464d

File tree

3 files changed

+59
-24
lines changed

3 files changed

+59
-24
lines changed

Test/RemotePcapTests.cs

-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
using static Test.TestHelper;
1414
using System.ComponentModel;
1515
using CategoryAttribute = NUnit.Framework.CategoryAttribute;
16-
using System.Runtime.Versioning;
1716

1817
namespace Test
1918
{
@@ -64,10 +63,6 @@ public void NpcapDeviceListNullAuthTest()
6463
/// if the test gets too long, it would be moved to its own file
6564
/// </summary>
6665
[Test]
67-
[Platform("Win")]
68-
#if NET
69-
[SupportedOSPlatform("windows")]
70-
#endif
7166
public void PwdAuthTest()
7267
{
7368
try

Test/TestUser.cs

+57-17
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,83 @@
11
// Copyright 2020-2021 Ayoub Kaanich <[email protected]>
22
// SPDX-License-Identifier: MIT
3-
3+
using NUnit.Framework;
44
using System;
5+
using System.Diagnostics;
56
using System.DirectoryServices.AccountManagement;
6-
using System.Runtime.Versioning;
7+
using System.Runtime.InteropServices;
78

89
namespace Test
910
{
10-
#if NET
11-
[SupportedOSPlatform("windows")]
12-
#endif
1311
public static class TestUser
1412
{
15-
public const string Username = "SharpPcap.Test.User";
13+
public const string Username = "sharppcaptestuser";
1614
public const string Password = "password";
1715

1816
public static bool Create()
1917
{
20-
try
18+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
2119
{
22-
Delete();
23-
var ctx = new PrincipalContext(ContextType.Machine);
24-
using (var user = new UserPrincipal(ctx, Username, Password, true))
20+
try
2521
{
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;
2733
}
28-
return true;
2934
}
30-
catch (PrincipalException)
35+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
3136
{
32-
return false;
37+
Bash("useradd", Username, "--groups", "sudo");
38+
Bash("bash", "-c", $"\"echo -e {Username}:{Password} | chpasswd\"");
3339
}
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);
3467
}
3568

3669
public static void Delete()
3770
{
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+
}
4181
}
4282
}
4383
}

codecov.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ codecov:
55
# Allow collecting coverage even if some CIs fail
66
require_ci_to_pass: false
77
notify:
8-
# 4 for appveyor
8+
# 3 for appveyor
99
# 3 for azure pipelines
1010
# 1 for circleci
1111
# Total = 8
1212
# Tolerate one of the services being down (worst case appveyor)
13-
after_n_builds: 4
13+
after_n_builds: 3

0 commit comments

Comments
 (0)