Skip to content

Commit

Permalink
RPCAP test with authentication in Linux (#455)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kayoub5 authored Mar 2, 2025
1 parent aca73ac commit 4e9464d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 24 deletions.
5 changes: 0 additions & 5 deletions Test/RemotePcapTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using static Test.TestHelper;
using System.ComponentModel;
using CategoryAttribute = NUnit.Framework.CategoryAttribute;
using System.Runtime.Versioning;

namespace Test
{
Expand Down Expand Up @@ -64,10 +63,6 @@ public void NpcapDeviceListNullAuthTest()
/// if the test gets too long, it would be moved to its own file
/// </summary>
[Test]
[Platform("Win")]
#if NET
[SupportedOSPlatform("windows")]
#endif
public void PwdAuthTest()
{
try
Expand Down
74 changes: 57 additions & 17 deletions Test/TestUser.cs
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);
}
}
}
}
4 changes: 2 additions & 2 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4e9464d

Please sign in to comment.