diff --git a/src/common.tests/Logging/MacAddressHash/MacInformationProviderTests.cs b/src/common.tests/Logging/MacAddressHash/MacInformationProviderTests.cs new file mode 100644 index 00000000..0059b7d7 --- /dev/null +++ b/src/common.tests/Logging/MacAddressHash/MacInformationProviderTests.cs @@ -0,0 +1,61 @@ +using FakeItEasy; +using Microsoft.BridgeToKubernetes.Common.IO; +using Microsoft.BridgeToKubernetes.Common.Logging.MacAddressHash; +using Microsoft.BridgeToKubernetes.Common.PersistentProperyBag; +using Microsoft.BridgeToKubernetes.TestHelpers; +using System; +using Xunit; + +namespace Microsoft.BridgeToKubernetes.Common.Tests.Logging.MacAddressHash +{ + public class MacInformationProviderTests : TestsBase + { + [Fact] + public void GetMacAddressHashOnWindows() + { + const string output = "Physical Address Transport Name\r\n=================== ==========================================================\r\nDC-41-A9-AA-1A-14 Media disconnected\r\nDC-42-A9-AA-1E-18 Media disconnected\r\nCA-48-3A-C0-A6-63 \\Device\\Tcpip_{DCA2D11A-367A-4582-A3C5-077619A50152}"; + + var fakeClientConfig = A.Fake(); + _autoFake.Provide(fakeClientConfig); + + var fakePlatform = A.Fake(); + A.CallTo(() => fakePlatform.IsWindows).Returns(true); + A.CallTo(() => fakePlatform.ExecuteAndReturnOutput(A.Ignored, A.Ignored, A.Ignored, A>.Ignored, A>.Ignored, null, null)).Returns((0, output)); + _autoFake.Provide(fakePlatform); + + var macInformationProvider = _autoFake.Resolve(); + + const string expectedResult = "f52b35d47f8b2bf2eb37182c7dd6197d1879b90cb43f80f3eeda7a4b77eb1fd9"; + string result = macInformationProvider.GetMacAddressHash(); + + Assert.Equal(expectedResult, result); + + A.CallTo(() => fakeClientConfig.SetProperty("mac.address", expectedResult)).MustHaveHappenedOnceExactly(); + A.CallTo(() => fakeClientConfig.Persist()).MustHaveHappenedOnceExactly(); + } + + [Fact] + public void GetMacAddressHashOnLinux() + { + const string output = " ether dc:41:a9:aa:1a:14 txqueuelen 1000 (Ethernet)\r\n RX packets 0 bytes 0 (0.0 B)\r\n RX errors 0 dropped 0 overruns 0 frame 0\r\n TX packets 0 bytes 0 (0.0 B)\r\n TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0\r\n\r\ndummy0: flags=130 mtu 1500\r\n ether 6a:c7:29:b2:dc:9e txqueuelen 1000 (Ethernet)\r\n RX packets 0 bytes 0 (0.0 B)\r\n RX errors 0 dropped 0 overruns 0 frame 0\r\n TX packets 0 bytes 0 (0.0 B)\r\n TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0\r\n\r\neth0: flags=4163 mtu 1280\r\n inet 0.0.0.0 netmask 0.0.0.0 broadcast 0.0.0.0\r\n inet6 fe80::215:5dff:feb6:b81 prefixlen 64 scopeid 0x20\r\n ether 00:15:5d:b6:0b:81 txqueuelen 1000 (Ethernet)\r\n RX packets 640092 bytes 605988996 (605.9 MB)\r\n RX errors 0 dropped 0 overruns 0 frame 0\r\n TX packets 77211 bytes 5949128 (5.9 MB)\r\n TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0\r\n\r\nlo: flags=73 mtu 65536\r\n inet 127.0.0.1 netmask 255.0.0.0\r\n inet6 ::1 prefixlen 128 scopeid 0x10\r\n loop txqueuelen 1000 (Local Loopback)\r\n RX packets 58608 bytes 361924804 (361.9 MB)\r\n RX errors 0 dropped 0 overruns 0 frame 0\r\n TX packets 58608 bytes 361924804 (361.9 MB)\r\n TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0\r\n\r\nsit0: flags=128 mtu 1480\r\n sit txqueuelen 1000 (IPv6-in-IPv4)\r\n RX packets 0 bytes 0 (0.0 B)\r\n RX errors 0 dropped 0 overruns 0 frame 0\r\n TX packets 0 bytes 0 (0.0 B)\r\n TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0\r\n\r\ntunl0: flags=128 mtu 1480\r\n tunnel txqueuelen 1000 (IPIP Tunnel)\r\n RX packets 0 bytes 0 (0.0 B)\r\n RX errors 0 dropped 0 overruns 0 frame 0\r\n TX packets 0 bytes 0 (0.0 B)\r\n TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0"; + + var fakeClientConfig = A.Fake(); + _autoFake.Provide(fakeClientConfig); + + var fakePlatform = A.Fake(); + A.CallTo(() => fakePlatform.IsLinux).Returns(true); + A.CallTo(() => fakePlatform.ExecuteAndReturnOutput(A.Ignored, A.Ignored, A.Ignored, A>.Ignored, A>.Ignored, null, null)).Returns((0, output)); + _autoFake.Provide(fakePlatform); + + var macInformationProvider = _autoFake.Resolve(); + + const string expectedResult = "e6e736e74149404e33e7f35171e3b178798c389a195b2fd81be1e9c0e6e13409"; + string result = macInformationProvider.GetMacAddressHash(); + + Assert.Equal(expectedResult, result); + + A.CallTo(() => fakeClientConfig.SetProperty("mac.address", expectedResult)).MustHaveHappenedOnceExactly(); + A.CallTo(() => fakeClientConfig.Persist()).MustHaveHappenedOnceExactly(); + } + } +} diff --git a/src/common/Logging/MacAddressHash/FipsCompliantSha.cs b/src/common/Logging/MacAddressHash/FipsCompliantSha.cs index 5e2a2fc4..a4b1fc41 100644 --- a/src/common/Logging/MacAddressHash/FipsCompliantSha.cs +++ b/src/common/Logging/MacAddressHash/FipsCompliantSha.cs @@ -11,6 +11,6 @@ namespace Microsoft.BridgeToKubernetes.Common.Logging.MacAddressHash internal class FipsCompliantSha { // FIPS compliant SHA256 hash algorithm. - public static readonly HashAlgorithm Sha256 = HashAlgorithm.Create(typeof(SHA256CryptoServiceProvider).AssemblyQualifiedName); + public static readonly SHA256 Sha256 = SHA256.Create(); } } \ No newline at end of file diff --git a/src/common/Logging/MacAddressHash/MACInformationProvider.cs b/src/common/Logging/MacAddressHash/MACInformationProvider.cs index 43b5f966..fe32402c 100644 --- a/src/common/Logging/MacAddressHash/MACInformationProvider.cs +++ b/src/common/Logging/MacAddressHash/MACInformationProvider.cs @@ -4,7 +4,6 @@ // -------------------------------------------------------------------------------------------- using System; -using System.Diagnostics; using System.Text; using System.Text.RegularExpressions; using System.Threading; @@ -46,9 +45,9 @@ internal static class Mono /// /// The client config, it contains a cached value for the hashed mac - /// - /// - /// + /// + /// + /// public MacInformationProvider( IClientConfig clientConfig, VSCodeStorageReader vsCodeStorageReader, @@ -69,7 +68,7 @@ public MacInformationProvider( /// Check if there is a persisted value otherwise calculates and persist a new one /// /// The hash of the mac address - private string GetMacAddressHash() + public string GetMacAddressHash() { string persistedValue = null; string result = null; @@ -157,34 +156,19 @@ private void PersistMacAddressHash(string hashedMacAddress) private static bool ValidateMacAddressHash(string macAddressHash) => !string.IsNullOrEmpty(macAddressHash) && Regex.IsMatch(macAddressHash, PersistRegex); - private static string RunCommandAndGetOutput(string commandName, string commandArgs = null) + private string RunCommandAndGetOutput(string commandName, string commandArgs = null) { - var processOutput = new StringBuilder(); - var process = new Process(); try { - process.EnableRaisingEvents = true; - process.StartInfo.UseShellExecute = false; - process.StartInfo.CreateNoWindow = true; - process.StartInfo.RedirectStandardOutput = true; - - process.StartInfo.RedirectStandardError = true; - process.StartInfo.FileName = commandName; - process.StartInfo.Arguments = commandArgs ?? string.Empty; - process.OutputDataReceived += (object sender, DataReceivedEventArgs e) => - { - processOutput.AppendLine(e.Data); - }; - process.Start(); - process.BeginOutputReadLine(); - process.BeginErrorReadLine(); - process.WaitForExit(); - process.Close(); - return processOutput.ToString(); + (var exitCode, var output) = this._platform.ExecuteAndReturnOutput(commandName, + commandArgs, + timeout: TimeSpan.FromSeconds(30), + stdOutCallback: null, + stdErrCallback: null); + return output; } catch (Exception) { - process.Close(); return ""; } }