aboutsummaryrefslogtreecommitdiff
path: root/src/test/burn/WixTestTools/RuntimeFactAttribute.cs
blob: 2644a1cc0649a961eda691c49122ff1695b23312 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

namespace WixTestTools
{
    using System;
    using System.DirectoryServices.ActiveDirectory;
    using System.Security.Principal;
    using WixInternal.TestSupport.XunitExtensions;
    using System.Runtime.InteropServices;

    public class RuntimeFactAttribute : SkippableFactAttribute
    {
        private bool domainRequired;

        const string RequiredEnvironmentVariableName = "RuntimeTestsEnabled";
        const string RequiredDomainEnvironmentVariableName = "RuntimeDomainTestsEnabled";

        public static bool RuntimeTestsEnabled { get; }
        public static bool RuntimeDomainTestsEnabled { get; }
        public static bool RunningAsAdministrator { get; }
        public static bool RunningOnWindowsServer { get; }
        public static bool RunningInDomain { get; }

        [DllImport("shlwapi.dll", SetLastError = true, EntryPoint = "#437")]
        private static extern bool IsOS(int os);
        private static bool IsWindowsServer()
        {
            const int OS_ANYSERVER = 29;
            return IsOS(OS_ANYSERVER);
        }

        static RuntimeFactAttribute()
        {
            using var identity = WindowsIdentity.GetCurrent();
            var principal = new WindowsPrincipal(identity);
            RunningAsAdministrator = principal.IsInRole(WindowsBuiltInRole.Administrator);

            var testsEnabledString = Environment.GetEnvironmentVariable(RequiredEnvironmentVariableName);
            RuntimeTestsEnabled = Boolean.TryParse(testsEnabledString, out var testsEnabled) && testsEnabled;

            RunningInDomain = false;
            try
            {
                RunningInDomain = !String.IsNullOrEmpty(System.DirectoryServices.ActiveDirectory.Domain.GetComputerDomain().Name);
            }
            catch (ActiveDirectoryObjectNotFoundException) { }

            var domainTestsEnabledString = Environment.GetEnvironmentVariable(RequiredDomainEnvironmentVariableName);
            RuntimeDomainTestsEnabled = Boolean.TryParse(domainTestsEnabledString, out var domainTestsEnabled) && domainTestsEnabled;

            RunningOnWindowsServer = IsWindowsServer();
        }

        public bool DomainRequired
        {
            get
            {
                return this.domainRequired;
            }
            set
            {
                this.domainRequired = value;
                if (this.domainRequired && String.IsNullOrEmpty(this.Skip) && (!RunningInDomain || !RuntimeDomainTestsEnabled))
                {
                    this.Skip = $"These tests require the test host to be running as a domain member ({(RunningInDomain ? "passed" : "failed")}). These tests affect both MACHINE AND DOMAIN state. To accept the consequences, set the {RequiredDomainEnvironmentVariableName} environment variable to true ({(RuntimeDomainTestsEnabled ? "passed" : "failed")}).";
                }
            }
        }

        private bool _RequireWindowsServer;
        public bool RequireWindowsServer
        {
            get
            {
                return _RequireWindowsServer;
            }
            set
            {
                _RequireWindowsServer = value;
                if (_RequireWindowsServer && !RunningOnWindowsServer)
                {
                    this.Skip = $"These tests are only run on Windows Server";
                }
            }
        }

        public RuntimeFactAttribute()
        {
            if (!RuntimeTestsEnabled || !RunningAsAdministrator)
            {
                this.Skip = $"These tests must run elevated ({(RunningAsAdministrator ? "passed" : "failed")}). These tests affect machine state. To accept the consequences, set the {RequiredEnvironmentVariableName} environment variable to true ({(RuntimeTestsEnabled ? "passed" : "failed")}).";
            }
        }
    }
}