public void LoadsInputFromAGivenFile(FileWrapper wrapper)
        {
            string inputPath = null;
            try
            {
                // -- Setup
                var expected = new[] {"multiply 9", "apply 5"};

                inputPath = Path.GetTempFileName();
                File.WriteAllLines(inputPath, expected);

                // -- Arrange

                // -- Act
                var actual = wrapper.ReadAllLines(inputPath);

                // -- Assert
                Assert.Equal(expected.Length, actual.Length);
                Assert.Equal(expected[0], actual[0]);
                Assert.Equal(expected[1], actual[1]);
            }
            finally
            {
                // -- Teardown
                if (File.Exists(inputPath))
                    File.Delete(inputPath);
            }
        }
예제 #2
0
        public void LoadsInputFromAGivenFile(FileWrapper wrapper)
        {
            string inputPath = null;

            try
            {
                // -- Setup
                var expected = new[] { "multiply 9", "apply 5" };

                inputPath = Path.GetTempFileName();
                File.WriteAllLines(inputPath, expected);

                // -- Arrange

                // -- Act
                var actual = wrapper.ReadAllLines(inputPath);

                // -- Assert
                Assert.Equal(expected.Length, actual.Length);
                Assert.Equal(expected[0], actual[0]);
                Assert.Equal(expected[1], actual[1]);
            }
            finally
            {
                // -- Teardown
                if (File.Exists(inputPath))
                {
                    File.Delete(inputPath);
                }
            }
        }
예제 #3
0
        public void FailsFastOnNullInput(FileWrapper wrapper)
        {
            // -- Arrange

            // -- Act

            // -- Assert
            Assert.ThrowsAny <ArgumentNullException>(() => wrapper.ReadAllLines(null));
        }
예제 #4
0
        public void FailsFastOnNullInput(FileWrapper wrapper)
        {
            // -- Arrange

            // -- Act

            // -- Assert
            Assert.ThrowsAny<ArgumentNullException>(() => wrapper.ReadAllLines(null));
        }
예제 #5
0
        private static Win32Program InternetShortcutProgram(string path)
        {
            try
            {
                string[] lines    = FileWrapper.ReadAllLines(path);
                string   iconPath = string.Empty;
                string   urlPath  = string.Empty;
                bool     validApp = false;

                Regex internetShortcutURLPrefixes = new Regex(@"^steam:\/\/(rungameid|run)\/|^com\.epicgames\.launcher:\/\/apps\/");

                const string urlPrefix      = "URL=";
                const string iconFilePrefix = "IconFile=";

                foreach (string line in lines)
                {
                    // Using OrdinalIgnoreCase since this is used internally
                    if (line.StartsWith(urlPrefix, StringComparison.OrdinalIgnoreCase))
                    {
                        urlPath = line.Substring(urlPrefix.Length);

                        try
                        {
                            Uri uri = new Uri(urlPath);
                        }
                        catch (UriFormatException e)
                        {
                            // To catch the exception if the uri cannot be parsed.
                            // Link to watson crash: https://watsonportal.microsoft.com/Failure?FailureSearchText=5f871ea7-e886-911f-1b31-131f63f6655b
                            ProgramLogger.Exception($"url could not be parsed", e, MethodBase.GetCurrentMethod().DeclaringType, urlPath);
                            return(new Win32Program()
                            {
                                Valid = false, Enabled = false
                            });
                        }

                        // To filter out only those steam shortcuts which have 'run' or 'rungameid' as the hostname
                        if (internetShortcutURLPrefixes.Match(urlPath).Success)
                        {
                            validApp = true;
                        }
                    }

                    // Using OrdinalIgnoreCase since this is used internally
                    if (line.StartsWith(iconFilePrefix, StringComparison.OrdinalIgnoreCase))
                    {
                        iconPath = line.Substring(iconFilePrefix.Length);
                    }
                }

                if (!validApp)
                {
                    return(new Win32Program()
                    {
                        Valid = false, Enabled = false
                    });
                }

                try
                {
                    var p = new Win32Program
                    {
                        Name             = Path.GetFileNameWithoutExtension(path),
                        ExecutableName   = Path.GetFileName(path),
                        IcoPath          = iconPath,
                        FullPath         = urlPath,
                        UniqueIdentifier = path,
                        ParentDirectory  = Directory.GetParent(path).FullName,
                        Valid            = true,
                        Enabled          = true,
                        AppType          = ApplicationType.InternetShortcutApplication,
                    };
                    return(p);
                }
                catch (Exception e) when(e is SecurityException || e is UnauthorizedAccessException)
                {
                    ProgramLogger.Exception($"|Permission denied when trying to load the program from {path}", e, MethodBase.GetCurrentMethod().DeclaringType, path);

                    return(new Win32Program()
                    {
                        Valid = false, Enabled = false
                    });
                }
            }
            catch (Exception e)
            {
                ProgramLogger.Exception($"|An unexpected error occurred in the calling method InternetShortcutProgram at {path}", e, MethodBase.GetCurrentMethod().DeclaringType, path);

                return(new Win32Program()
                {
                    Valid = false, Enabled = false
                });
            }
        }
예제 #6
0
        // This function filters Internet Shortcut programs
        private static Win32Program InternetShortcutProgram(string path)
        {
            string[] lines    = FileWrapper.ReadAllLines(path);
            string   appName  = string.Empty;
            string   iconPath = string.Empty;
            string   urlPath  = string.Empty;
            string   scheme   = string.Empty;
            bool     validApp = false;

            Regex InternetShortcutURLPrefixes = new Regex(@"^steam:\/\/(rungameid|run)\/|^com\.epicgames\.launcher:\/\/apps\/");

            const string urlPrefix      = "URL=";
            const string iconFilePrefix = "IconFile=";

            foreach (string line in lines)
            {
                if (line.StartsWith(urlPrefix, StringComparison.OrdinalIgnoreCase))
                {
                    urlPath = line.Substring(urlPrefix.Length);
                    Uri uri = new Uri(urlPath);

                    // To filter out only those steam shortcuts which have 'run' or 'rungameid' as the hostname
                    if (InternetShortcutURLPrefixes.Match(urlPath).Success)
                    {
                        validApp = true;
                    }
                }

                if (line.StartsWith(iconFilePrefix, StringComparison.OrdinalIgnoreCase))
                {
                    iconPath = line.Substring(iconFilePrefix.Length);
                }
            }

            if (!validApp)
            {
                return(new Win32Program()
                {
                    Valid = false, Enabled = false
                });
            }

            try
            {
                var p = new Win32Program
                {
                    Name             = Path.GetFileNameWithoutExtension(path),
                    ExecutableName   = Path.GetFileName(path),
                    IcoPath          = iconPath,
                    FullPath         = urlPath,
                    UniqueIdentifier = path,
                    ParentDirectory  = Directory.GetParent(path).FullName,
                    Valid            = true,
                    Enabled          = true,
                    AppType          = (uint)ApplicationTypes.INTERNET_SHORTCUT_APPLICATION,
                };
                return(p);
            }
            catch (Exception e) when(e is SecurityException || e is UnauthorizedAccessException)
            {
                ProgramLogger.LogException(
                    $"|Win32|InternetShortcutProgram|{path}" +
                    $"|Permission denied when trying to load the program from {path}", e);

                return(new Win32Program()
                {
                    Valid = false, Enabled = false
                });
            }
        }