|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +namespace VirtualClient.Monitors |
| 5 | +{ |
| 6 | + using System; |
| 7 | + using System.Collections; |
| 8 | + using System.Collections.Generic; |
| 9 | + using System.Runtime.InteropServices; |
| 10 | + using System.Threading; |
| 11 | + using System.Threading.Tasks; |
| 12 | + using NUnit.Framework; |
| 13 | + using VirtualClient.Common.Telemetry; |
| 14 | + using VirtualClient.Dependencies; |
| 15 | + |
| 16 | + [TestFixture] |
| 17 | + [Category("Unit")] |
| 18 | + internal class ExecuteCommandMonitorTests : MockFixture |
| 19 | + { |
| 20 | + public void SetupDefaults(PlatformID platform, Architecture architecture = Architecture.X64) |
| 21 | + { |
| 22 | + this.Setup(platform, architecture); |
| 23 | + this.Parameters[nameof(ExecuteCommandMonitor.Command)] = "anycommand"; |
| 24 | + this.Parameters[nameof(ExecuteCommandMonitor.MonitorEventType)] = "any_event_type"; |
| 25 | + this.Parameters[nameof(ExecuteCommandMonitor.MonitorEventSource)] = "any_event_source"; |
| 26 | + } |
| 27 | + |
| 28 | + [Test] |
| 29 | + [TestCase("anycommand", "anycommand", null)] |
| 30 | + [TestCase("anycommand ", "anycommand", null)] |
| 31 | + [TestCase("anycommand --argument=value", "anycommand", "--argument=value")] |
| 32 | + [TestCase("/home/user/anycommand", "/home/user/anycommand", null)] |
| 33 | + [TestCase("/home/user/anycommand --argument=value --argument2 value2", "/home/user/anycommand", "--argument=value --argument2 value2")] |
| 34 | + [TestCase("\"/home/user/dir with space/anycommand\" --argument=value --argument2 value2", "\"/home/user/dir with space/anycommand\"", "--argument=value --argument2 value2")] |
| 35 | + [TestCase("sudo anycommand", "sudo", "anycommand")] |
| 36 | + [TestCase("sudo /home/user/anycommand", "sudo", "/home/user/anycommand")] |
| 37 | + [TestCase("sudo /home/user/anycommand --argument=value --argument2 value2", "sudo", "/home/user/anycommand --argument=value --argument2 value2")] |
| 38 | + [TestCase("sudo \"/home/user/dir with space/anycommand\" --argument=value --argument2 value2", "sudo", "\"/home/user/dir with space/anycommand\" --argument=value --argument2 value2")] |
| 39 | + public async Task ExecuteCommandMonitorExecutesTheExpectedCommandOnUnixSystems(string fullCommand, string expectedCommand, string expectedCommandArguments) |
| 40 | + { |
| 41 | + this.SetupDefaults(PlatformID.Unix); |
| 42 | + |
| 43 | + using (TestExecuteCommandMonitor command = new TestExecuteCommandMonitor(this)) |
| 44 | + { |
| 45 | + command.Parameters[nameof(ExecuteCommandMonitor.Command)] = fullCommand; |
| 46 | + |
| 47 | + this.ProcessManager.OnProcessCreated = (process) => |
| 48 | + { |
| 49 | + Assert.AreEqual($"{expectedCommand} {expectedCommandArguments}".Trim(), process.FullCommand()); |
| 50 | + }; |
| 51 | + |
| 52 | + await command.ExecuteCommandAsync(EventContext.None, CancellationToken.None); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + [Test] |
| 57 | + [TestCase("anycommand.exe", "anycommand.exe", null)] |
| 58 | + [TestCase("anycommand.exe ", "anycommand.exe", null)] |
| 59 | + [TestCase("anycommand.exe --argument=value --argument2 value2", "anycommand.exe", "--argument=value --argument2 value2")] |
| 60 | + [TestCase("C:\\Users\\User\\anycommand.exe", "C:\\Users\\User\\anycommand.exe", null)] |
| 61 | + [TestCase("C:\\Users\\User\\anycommand.exe --argument=value --argument2 value2", "C:\\Users\\User\\anycommand.exe", "--argument=value --argument2 value2")] |
| 62 | + [TestCase("\"C:\\Users\\User\\Dir With Space\\anycommand.exe\" --argument=value --argument2 value2", "\"C:\\Users\\User\\Dir With Space\\anycommand.exe\"", "--argument=value --argument2 value2")] |
| 63 | + public async Task ExecuteCommandMonitorExecutesTheExpectedCommandOnWindowsSystems(string fullCommand, string expectedCommand, string expectedCommandArguments) |
| 64 | + { |
| 65 | + this.SetupDefaults(PlatformID.Win32NT); |
| 66 | + |
| 67 | + using (TestExecuteCommandMonitor command = new TestExecuteCommandMonitor(this)) |
| 68 | + { |
| 69 | + command.Parameters[nameof(ExecuteCommandMonitor.Command)] = fullCommand; |
| 70 | + |
| 71 | + this.ProcessManager.OnProcessCreated = (process) => |
| 72 | + { |
| 73 | + Assert.AreEqual($"{expectedCommand} {expectedCommandArguments}".Trim(), process.FullCommand()); |
| 74 | + }; |
| 75 | + |
| 76 | + await command.ExecuteCommandAsync(EventContext.None, CancellationToken.None); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private class TestExecuteCommandMonitor : ExecuteCommandMonitor |
| 81 | + { |
| 82 | + public TestExecuteCommandMonitor(MockFixture mockFixture) |
| 83 | + : base(mockFixture?.Dependencies, mockFixture?.Parameters) |
| 84 | + { |
| 85 | + } |
| 86 | + |
| 87 | + public new Task InitializeAsync(EventContext telemetryContext, CancellationToken cancellationToken) |
| 88 | + { |
| 89 | + return base.InitializeAsync(telemetryContext, cancellationToken); |
| 90 | + } |
| 91 | + |
| 92 | + public new Task ExecuteCommandAsync(EventContext telemetryContext, CancellationToken cancellationToken) |
| 93 | + { |
| 94 | + return base.ExecuteCommandAsync(telemetryContext, cancellationToken); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments