|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using Microsoft.Diagnostics.DataContractReader.Contracts; |
| 7 | +using Moq; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace Microsoft.Diagnostics.DataContractReader.Tests; |
| 11 | + |
| 12 | +public class RuntimeInfoTests |
| 13 | +{ |
| 14 | + internal static Target CreateTarget( |
| 15 | + MockTarget.Architecture arch, |
| 16 | + (string Name, string Value)[] globalStrings) |
| 17 | + { |
| 18 | + MockMemorySpace.Builder builder = new MockMemorySpace.Builder(new TargetTestHelpers(arch)); |
| 19 | + TestPlaceholderTarget target = new TestPlaceholderTarget(arch, builder.GetReadContext().ReadFromTarget, [], [], globalStrings); |
| 20 | + |
| 21 | + IContractFactory<IRuntimeInfo> runtimeInfoFactory = new RuntimeInfoFactory(); |
| 22 | + |
| 23 | + ContractRegistry reg = Mock.Of<ContractRegistry>( |
| 24 | + c => c.RuntimeInfo == runtimeInfoFactory.CreateContract(target, 1)); |
| 25 | + target.SetContracts(reg); |
| 26 | + return target; |
| 27 | + } |
| 28 | + |
| 29 | + public static IEnumerable<object[]> StdArchAllTargetArchitectures() |
| 30 | + { |
| 31 | + foreach(object[] arr in new MockTarget.StdArch()) |
| 32 | + { |
| 33 | + MockTarget.Architecture arch = (MockTarget.Architecture)arr[0]; |
| 34 | + |
| 35 | + foreach(RuntimeInfoArchitecture targetArch in (RuntimeInfoArchitecture[])Enum.GetValues(typeof(RuntimeInfoArchitecture))) |
| 36 | + { |
| 37 | + // Skip Unknown architecture |
| 38 | + if (targetArch == RuntimeInfoArchitecture.Unknown) |
| 39 | + continue; |
| 40 | + |
| 41 | + yield return new object[] { arch, targetArch.ToString().ToLowerInvariant(), targetArch }; |
| 42 | + } |
| 43 | + |
| 44 | + yield return new object[] { arch, "notATargetArch", RuntimeInfoArchitecture.Unknown }; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + [Theory] |
| 49 | + [MemberData(nameof(StdArchAllTargetArchitectures))] |
| 50 | + public void GetTargetArchitectureTest( |
| 51 | + MockTarget.Architecture arch, |
| 52 | + string architecture, |
| 53 | + RuntimeInfoArchitecture expectedArchitecture) |
| 54 | + { |
| 55 | + var target = CreateTarget(arch, [(Constants.Globals.Architecture, architecture)]); |
| 56 | + |
| 57 | + // TEST |
| 58 | + |
| 59 | + var runtimeInfo = target.Contracts.RuntimeInfo; |
| 60 | + Assert.NotNull(runtimeInfo); |
| 61 | + |
| 62 | + var actualArchitecture = runtimeInfo.GetTargetArchitecture(); |
| 63 | + Assert.Equal(expectedArchitecture, actualArchitecture); |
| 64 | + } |
| 65 | + |
| 66 | + public static IEnumerable<object[]> StdArchAllTargetOS() |
| 67 | + { |
| 68 | + foreach(object[] arr in new MockTarget.StdArch()) |
| 69 | + { |
| 70 | + MockTarget.Architecture arch = (MockTarget.Architecture)arr[0]; |
| 71 | + |
| 72 | + foreach(RuntimeInfoOperatingSystem targetArch in (RuntimeInfoOperatingSystem[])Enum.GetValues(typeof(RuntimeInfoOperatingSystem))) |
| 73 | + { |
| 74 | + // Skip Unknown architecture |
| 75 | + if (targetArch == RuntimeInfoOperatingSystem.Unknown) |
| 76 | + continue; |
| 77 | + |
| 78 | + yield return new object[] { arch, targetArch.ToString().ToLowerInvariant(), targetArch }; |
| 79 | + } |
| 80 | + |
| 81 | + yield return new object[] { arch, "notAnOperatingSystem", RuntimeInfoOperatingSystem.Unknown }; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + [Theory] |
| 86 | + [MemberData(nameof(StdArchAllTargetOS))] |
| 87 | + public void GetTargetOperatingSystemTest( |
| 88 | + MockTarget.Architecture arch, |
| 89 | + string os, |
| 90 | + RuntimeInfoOperatingSystem expectedOS) |
| 91 | + { |
| 92 | + var target = CreateTarget(arch, [(Constants.Globals.OperatingSystem, os)]); |
| 93 | + |
| 94 | + // TEST |
| 95 | + |
| 96 | + var runtimeInfo = target.Contracts.RuntimeInfo; |
| 97 | + Assert.NotNull(runtimeInfo); |
| 98 | + |
| 99 | + var actualArchitecture = runtimeInfo.GetTargetOperatingSystem(); |
| 100 | + Assert.Equal(expectedOS, actualArchitecture); |
| 101 | + } |
| 102 | +} |
0 commit comments