/// <nodoc/>
        public FactIfSupportedAttribute(
            bool requiresAdmin                       = false,
            bool requiresJournalScan                 = false,
            bool requiresSymlinkPermission           = false,
            bool requiresWindowsBasedOperatingSystem = false,
            bool requiresUnixBasedOperatingSystem    = false,
            bool requiresHeliumDriversAvailable      = false,
            bool requiresHeliumDriversNotAvailable   = false)
        {
            RequiresAdmin             = requiresAdmin;
            RequiresJournalScan       = requiresJournalScan;
            RequiresSymlinkPermission = requiresSymlinkPermission;

            if (Skip != null)
            {
                // If skip is specified, do nothing because the test will be skipped anyway.
                return;
            }

            if (requiresAdmin)
            {
                if (!s_isElevated.HasValue)
                {
                    s_isElevated = CurrentProcess.IsElevated;
                }

                if (!s_isElevated.Value)
                {
                    Skip = "Test must be run elevated!";
                    return;
                }
            }

            if (requiresSymlinkPermission)
            {
                if (!s_canCreateSymlink.HasValue)
                {
                    string tempFile    = Path.GetTempFileName();
                    string symlinkPath = Path.GetTempFileName();
                    FileUtilities.DeleteFile(symlinkPath);

                    // For reliable tests, we ensure that the symlink is created.
                    s_canCreateSymlink = FileUtilities.TryCreateSymbolicLink(symlinkPath, tempFile, true).Succeeded&& FileUtilities.FileExistsNoFollow(symlinkPath);
                    FileUtilities.DeleteFile(symlinkPath);
                    FileUtilities.DeleteFile(tempFile);
                }

                if (!s_canCreateSymlink.Value)
                {
                    Skip = "Test must be run with symbolic link creation privileged";
                    return;
                }
            }

            if (requiresJournalScan)
            {
                if (!s_canScanJournal.HasValue)
                {
                    var loggingContext = new LoggingContext("Dummy", "Dummy");
                    var map            = JournalUtils.TryCreateMapOfAllLocalVolumes(loggingContext);
                    var accessor       = JournalUtils.TryGetJournalAccessorForTest(map);
                    s_canScanJournal = accessor.Succeeded;
                    if (!accessor.Succeeded)
                    {
                        s_journalAccessorFailure = accessor.Failure.Describe();
                    }
                }

                if (!s_canScanJournal.Value)
                {
                    Skip = $"Test requires access to the in process change journal scan but getting the journal access failed. {s_journalAccessorFailure ?? string.Empty}{Environment.NewLine}Either run elevated or install RS2.";
                    return;
                }
            }

            if (requiresWindowsBasedOperatingSystem)
            {
                if (OperatingSystemHelper.IsUnixOS)
                {
                    Skip = "Test must be run on the CLR on Windows based operating systems!";
                    return;
                }
            }

            if (requiresUnixBasedOperatingSystem)
            {
                if (!OperatingSystemHelper.IsUnixOS)
                {
                    Skip = "Test must be run on the CoreCLR on Unix based operating systems!";
                    return;
                }
            }

            if (requiresHeliumDriversAvailable)
            {
                if (!s_isHeliumFiltersAvailable.HasValue)
                {
                    s_isHeliumFiltersAvailable = ProcessUtilities.IsWciAndBindFiltersAvailable();
                }

                if (!s_isHeliumFiltersAvailable.Value)
                {
                    Skip = "Test must be run elevated on a machine with WCI and Bind filters available.";
                    return;
                }
            }

            if (requiresHeliumDriversNotAvailable)
            {
                if (!s_isHeliumFiltersAvailable.HasValue)
                {
                    s_isHeliumFiltersAvailable = ProcessUtilities.IsWciAndBindFiltersAvailable();
                }
                if (s_isHeliumFiltersAvailable.Value)
                {
                    Skip = "Test must be run on a machine where WCI and Bind filters are NOT available.";
                    return;
                }
            }
        }
Пример #2
0
        /// <nodoc/>
        public FactIfSupportedAttribute(
            bool requiresAdmin                       = false,
            bool requiresJournalScan                 = false,
            bool requiresSymlinkPermission           = false,
            bool requiresWindowsBasedOperatingSystem = false,
            bool requiresUnixBasedOperatingSystem    = false,
            bool requiresHeliumDriversAvailable      = false,
            bool requiresHeliumDriversNotAvailable   = false,
            bool requiresMacOperatingSystem          = false,
            bool requiresWindowsOrMacOperatingSystem = false,
            TestRequirements additionalRequirements  = TestRequirements.None)
        {
            var requirements = additionalRequirements;

            AddRequirement(ref requirements, requiresAdmin, TestRequirements.Admin);
            AddRequirement(ref requirements, requiresJournalScan, TestRequirements.JournalScan);
            AddRequirement(ref requirements, requiresSymlinkPermission, TestRequirements.SymlinkPermission);
            AddRequirement(ref requirements, requiresWindowsBasedOperatingSystem, TestRequirements.WindowsOs);
            AddRequirement(ref requirements, requiresUnixBasedOperatingSystem, TestRequirements.UnixBasedOs);
            AddRequirement(ref requirements, requiresHeliumDriversAvailable, TestRequirements.HeliumDriversAvailable);
            AddRequirement(ref requirements, requiresHeliumDriversNotAvailable, TestRequirements.HeliumDriversNotAvailable);
            AddRequirement(ref requirements, requiresMacOperatingSystem, TestRequirements.MacOs);
            AddRequirement(ref requirements, requiresWindowsOrMacOperatingSystem, TestRequirements.WindowsOrMacOs);

            Requirements = requirements;

            if (Skip != null)
            {
                // If skip is specified, do nothing because the test will be skipped anyway.
                return;
            }

            CheckRequirement(
                TestRequirements.Admin,
                () =>
            {
                return(!CurrentProcess.IsElevated ? "Test must be run elevated!" : null);
            });

            CheckRequirement(
                TestRequirements.SymlinkPermission,
                () =>
            {
                string tempFile    = FileUtilities.GetTempFileName();
                string symlinkPath = FileUtilities.GetTempFileName();
                FileUtilities.DeleteFile(symlinkPath);

                // For reliable tests, we ensure that the symlink is created.
                var canCreateSymlink = FileUtilities.TryCreateSymbolicLink(symlinkPath, tempFile, true).Succeeded&& FileUtilities.FileExistsNoFollow(symlinkPath);
                FileUtilities.DeleteFile(symlinkPath);
                FileUtilities.DeleteFile(tempFile);

                return(!canCreateSymlink ? "Test must be run with symbolic link creation privileged" : null);
            });

            CheckRequirement(
                TestRequirements.JournalScan,
                () =>
            {
                if (OperatingSystemHelper.IsUnixOS)
                {
                    return($"Test requires a journaled Windows file system, can't be executed on non-windows systems.");
                }

                var loggingContext = new LoggingContext("Dummy", "Dummy");
                var map            = JournalUtils.TryCreateMapOfAllLocalVolumes(loggingContext);
                var accessor       = JournalUtils.TryGetJournalAccessorForTest(map);
                if (!accessor.Succeeded)
                {
                    return(accessor.Failure.Describe());
                }

                return(null);
            });

            CheckRequirement(
                TestRequirements.WindowsOs,
                () =>
            {
                if (OperatingSystemHelper.IsUnixOS)
                {
                    return("Test must be run on the CLR on Windows based operating systems!");
                }

                return(null);
            });

            CheckRequirement(
                TestRequirements.UnixBasedOs,
                () =>
            {
                if (!OperatingSystemHelper.IsUnixOS)
                {
                    return("Test must be run on the CoreCLR on Unix based operating systems!");
                }

                return(null);
            });

            CheckRequirement(
                TestRequirements.MacOs,
                () =>
            {
                if (!OperatingSystemHelper.IsMacOS)
                {
                    return("Test must be run on macOS");
                }

                return(null);
            });

            CheckRequirement(
                TestRequirements.HeliumDriversAvailable,
                () =>
            {
                if (!ProcessUtilities.IsWciAndBindFiltersAvailable())
                {
                    return("Test must be run elevated on a machine with WCI and Bind filters available.");
                }

                return(null);
            });

            CheckRequirement(
                TestRequirements.HeliumDriversNotAvailable,
                () =>
            {
                if (ProcessUtilities.IsWciAndBindFiltersAvailable())
                {
                    return("Test must be run on a machine where WCI and Bind filters are NOT available.");
                }

                return(null);
            });

            CheckRequirement(
                TestRequirements.WindowsProjFs,
                () =>
            {
                if (OperatingSystemHelper.IsUnixOS)
                {
                    return("WindowsProjFs requires running on Windows operating system");
                }

                bool foundGvfsService = System.Diagnostics.Process.GetProcessesByName("GVFS.Service").Length != 0;
                if (!foundGvfsService)
                {
                    return("Could not find GVFS.Service. Is Windows Projected FileSystem enabled?");
                }

                return(null);
            });

            CheckRequirement(
                TestRequirements.WindowsOrMacOs,
                () =>
            {
                if (OperatingSystemHelper.IsLinuxOS)
                {
                    return("Test must be run on Windows or macOS");
                }

                return(null);
            });
        }