Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RPCAP test with authentication in Linux #455

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading