예제 #1
0
        public void Exit_2()
        {
            string      rawCode = $"Exit,UnitTest,NOWARN";
            EngineState s       = EngineTests.Eval(rawCode, CodeType.Exit, ErrorCheck.Success);

            Assert.IsTrue(s.PassCurrentScriptFlag);
        }
예제 #2
0
        public void WebGet_Compat(EngineState s)
        {
            // FileHelper.GetTempFile ensures very high possibility that returned temp file path is unique per call.
            string destFile = FileHelper.GetTempFile("html");

            try
            {
                File.Delete(destFile);
                s.ReturnValue = string.Empty;

                string srcFile = Path.Combine(TestSetup.WebRoot, "index.html");
                string rawCode = $"WebGet,\"{TestSetup.UrlRoot}/index.html\",\"{destFile}\"";
                EngineTests.Eval(s, rawCode, CodeType.WebGet, ErrorCheck.Success, new CompatOption {
                    DisableExtendedSectionParams = true
                });

                Assert.IsTrue(File.Exists(destFile));
                Assert.IsTrue(TestSetup.FileEqual(srcFile, destFile));
                Assert.IsTrue(s.ReturnValue.Length == 0);
            }
            finally
            {
                if (File.Exists(destFile))
                {
                    File.Delete(destFile);
                }
            }
        }
예제 #3
0
        public void WebGet_HashError(EngineState s)
        {
            string destFile = FileHelper.ReserveTempFile("html");

            try
            {
                // Try different MD5 digest
                s.ReturnValue = string.Empty;

                string rawCode = $"WebGet,\"{_sampleFileUrl}\",\"{destFile}\",MD5=00000000000000000000000000000000";
                EngineTests.Eval(s, rawCode, CodeType.WebGet, ErrorCheck.RuntimeError);

                Assert.IsFalse(File.Exists(destFile));
                Assert.IsTrue(s.ReturnValue.Equals("1", StringComparison.Ordinal));

                // Try invalid MD5 digest
                File.Delete(destFile);
                s.ReturnValue = string.Empty;

                rawCode = $"WebGet,\"{_sampleFileUrl}\",\"{destFile}\",MD5=0";
                EngineTests.Eval(s, rawCode, CodeType.WebGet, ErrorCheck.RuntimeError);

                Assert.IsFalse(File.Exists(destFile));
                Assert.IsTrue(s.ReturnValue.Length == 0);
            }
            finally
            {
                if (File.Exists(destFile))
                {
                    File.Delete(destFile);
                }
            }
        }
예제 #4
0
        public static void DelLoopCounter(EngineState s)
        {
            const string rawCode = "Set,#c,NIL";

            s.CompatOverridableLoopCounter = true;
            s.LoopStateStack.Push(new EngineLoopState(100));
            EngineTests.Eval(s, rawCode, CodeType.Set, ErrorCheck.Warning);
            EngineLoopState loop = s.LoopStateStack.Pop();

            Assert.AreEqual(100, loop.CounterIndex);
            Assert.AreEqual('\0', loop.CounterLetter);

            s.LoopStateStack.Push(new EngineLoopState('C'));
            EngineTests.Eval(s, rawCode, CodeType.Set, ErrorCheck.Warning);
            loop = s.LoopStateStack.Pop();
            Assert.AreEqual(0, loop.CounterIndex);
            Assert.AreEqual('C', loop.CounterLetter);

            s.CompatOverridableLoopCounter = false;
            s.LoopStateStack.Push(new EngineLoopState(100));
            EngineTests.Eval(s, rawCode, CodeType.Set, ErrorCheck.Warning);
            loop = s.LoopStateStack.Pop();
            Assert.AreEqual(100, loop.CounterIndex);
            Assert.AreEqual('\0', loop.CounterLetter);

            s.LoopStateStack.Push(new EngineLoopState('C'));
            EngineTests.Eval(s, rawCode, CodeType.Set, ErrorCheck.Warning);
            loop = s.LoopStateStack.Pop();
            Assert.AreEqual(0, loop.CounterIndex);
            Assert.AreEqual('C', loop.CounterLetter);
        }
예제 #5
0
        public void WebGet_Http(EngineState s)
        {
            // FileHelper.GetTempFile ensures very high possibility that returned temp file path is unique per call.
            string destFile = FileHelper.ReserveTempFile("html");

            try
            {
                s.ReturnValue = string.Empty;

                // Try downloading index.html from GitHub.
                string srcFile = Path.Combine(TestSetup.WebRoot, "index.html");
                string rawCode = $"WebGet,\"{TestSetup.UrlRoot}/index.html\",\"{destFile}\"";
                EngineTests.Eval(s, rawCode, CodeType.WebGet, ErrorCheck.Success);

                Assert.IsTrue(File.Exists(destFile));
                Assert.IsTrue(TestSetup.FileEqual(srcFile, destFile));
                Assert.IsTrue(s.ReturnValue.Equals("200", StringComparison.Ordinal));
            }
            finally
            {
                if (File.Exists(destFile))
                {
                    File.Delete(destFile);
                }
            }
        }
예제 #6
0
        private void PathMove_DirTemplate(EngineState s, string rawCode, string srcFileName, string destFileName, ErrorCheck check = ErrorCheck.Success)
        {
            string dirPath = StringEscaper.Preprocess(s, Path.Combine("%TestBench%", "CommandFile"));

            string srcDir  = Path.Combine(dirPath, SrcDir_Dir);
            string destDir = Path.Combine(dirPath, DestDir_PathMove);

            string srcFullPath  = Path.Combine(destDir, srcFileName);
            string destFullPath = Path.Combine(destDir, destFileName);

            if (Directory.Exists(destDir))
            {
                Directory.Delete(destDir, true);
            }
            FileHelper.DirectoryCopy(srcDir, destDir, true, true);
            try
            {
                EngineTests.Eval(s, rawCode, CodeType.PathMove, check);

                if (check == ErrorCheck.Success)
                {
                    Assert.IsFalse(Directory.Exists(srcFullPath));
                    Assert.IsTrue(Directory.Exists(destFullPath));
                }
            }
            finally
            {
                Directory.Delete(destDir, true);
            }
        }
예제 #7
0
        public void IntDiv()
        {
            EngineState s = EngineTests.CreateEngineState();

            void IntDivTemplate(string rawCode, string quotientCheck, string remainderCheck)
            {
                EngineTests.Eval(s, rawCode, CodeType.Math, ErrorCheck.Success);

                Assert.IsTrue(s.Variables["DestQ"].Equals(quotientCheck, StringComparison.Ordinal));
                Assert.IsTrue(s.Variables["DestR"].Equals(remainderCheck, StringComparison.Ordinal));
            }

            // IntDiv
            IntDivTemplate("Math,IntDiv,%DestQ%,%DestR%,0x3,2", "1", "1");
            IntDivTemplate("Math,IntDiv,%DestQ%,%DestR%,10,3", "3", "1");
            IntDivTemplate("Math,IntDiv,%DestQ%,%DestR%,10,-3", "-3", "1");
            IntDivTemplate("Math,IntDiv,%DestQ%,%DestR%,-10,3", "-3", "-1");
            IntDivTemplate("Math,IntDiv,%DestQ%,%DestR%,-10,-3", "3", "-1");

            // Test Error
            ErrorTemplate(s, "Math,IntDiv,DestQ,%DestR%,3,2", ErrorCheck.ParserError);
            ErrorTemplate(s, "Math,IntDiv,%DestQ%,DestR,3,2", ErrorCheck.ParserError);
            ErrorTemplate(s, "Math,IntDiv,%DestQ%,%DestR%,3,2,1", ErrorCheck.ParserError);
            ErrorTemplate(s, "Math,IntDiv,%DestQ%,%DestR%,3", ErrorCheck.ParserError);
            ErrorTemplate(s, "Math,IntDiv,%DestQ%,%DestR%,3,F", ErrorCheck.RuntimeError);
            ErrorTemplate(s, "Math,IntDiv,%DestQ%,%DestR%,A,F", ErrorCheck.RuntimeError);
            ErrorTemplate(s, "Math,IntDiv,%DestQ%,%DestR%,B,C", ErrorCheck.RuntimeError);
        }
예제 #8
0
        public void WebGet_SHA512(EngineState s)
        {
            string tempSrc  = CommandHashTests.SampleText();
            string tempDest = Path.GetTempFileName();

            File.Delete(tempDest);

            try
            {
                Uri    fileUri = new Uri(tempSrc);
                string rawCode = $"WebGet,\"{fileUri.AbsoluteUri}\",\"{tempDest}\",SHA512,f5829cb5e052ab5ef6820630fd992acabb798512d21b5c5295fb81b88b74f3812863c0804e730f26e166b51d77eb5f1de200fd75913278522da78fbb269600cc";
                EngineTests.Eval(s, rawCode, CodeType.WebGet, ErrorCheck.Success);

                Assert.IsTrue(File.Exists(tempDest));
            }
            finally
            {
                if (File.Exists(tempSrc))
                {
                    File.Delete(tempSrc);
                }
                if (File.Exists(tempDest))
                {
                    File.Delete(tempDest);
                }
            }
        }
예제 #9
0
        public void WebGet_HashError(EngineState s)
        {
            string tempSrc  = CommandHashTests.SampleText();
            string tempDest = Path.GetTempFileName();

            File.Delete(tempDest);

            try
            {
                Uri    fileUri = new Uri(tempSrc);
                string rawCode = $"WebGet,\"{fileUri.AbsoluteUri}\",\"{tempDest}\",MD5,00000000000000000000000000000000";
                EngineTests.Eval(s, rawCode, CodeType.WebGet, ErrorCheck.Error);
            }
            finally
            {
                if (File.Exists(tempSrc))
                {
                    File.Delete(tempSrc);
                }
                if (File.Exists(tempDest))
                {
                    File.Delete(tempDest);
                }
            }
        }
예제 #10
0
        public void WebGet_SHA256(EngineState s)
        {
            string tempSrc  = CommandHashTests.SampleText();
            string tempDest = Path.GetTempFileName();

            File.Delete(tempDest);

            try
            {
                Uri    fileUri = new Uri(tempSrc);
                string rawCode = $"WebGet,\"{fileUri.AbsoluteUri}\",\"{tempDest}\",SHA256,3596bc5a263736c9d5b9a06e85a66ed2a866b457a44e5ed8548e504ca5599772";
                EngineTests.Eval(s, rawCode, CodeType.WebGet, ErrorCheck.Success);

                Assert.IsTrue(File.Exists(tempDest));
            }
            finally
            {
                if (File.Exists(tempSrc))
                {
                    File.Delete(tempSrc);
                }
                if (File.Exists(tempDest))
                {
                    File.Delete(tempDest);
                }
            }
        }
예제 #11
0
        public void WebGet_SHA384(EngineState s)
        {
            string tempSrc  = CommandHashTests.SampleText();
            string tempDest = Path.GetTempFileName();

            File.Delete(tempDest);

            try
            {
                Uri    fileUri = new Uri(tempSrc);
                string rawCode = $"WebGet,\"{fileUri.AbsoluteUri}\",\"{tempDest}\",SHA384,e068a3ac0b4ab4b37306dc354af6b8a4c89ef3fbbf1db969ec6d6a4281f1ab1f472fcd7bc2f16c0cf41c1991056846a6";
                EngineTests.Eval(s, rawCode, CodeType.WebGet, ErrorCheck.Success);

                Assert.IsTrue(File.Exists(tempDest));
            }
            finally
            {
                if (File.Exists(tempSrc))
                {
                    File.Delete(tempSrc);
                }
                if (File.Exists(tempDest))
                {
                    File.Delete(tempDest);
                }
            }
        }
예제 #12
0
        public void WebGet_SHA1(EngineState s)
        {
            string tempSrc  = CommandHashTests.SampleText();
            string tempDest = Path.GetTempFileName();

            File.Delete(tempDest);

            try
            {
                Uri    fileUri = new Uri(tempSrc);
                string rawCode = $"WebGet,\"{fileUri.AbsoluteUri}\",\"{tempDest}\",SHA1,0aaac8883f1c8dd48dbf974299a9422f1ab437ee";
                EngineTests.Eval(s, rawCode, CodeType.WebGet, ErrorCheck.Success);

                Assert.IsTrue(File.Exists(tempDest));
            }
            finally
            {
                if (File.Exists(tempSrc))
                {
                    File.Delete(tempSrc);
                }
                if (File.Exists(tempDest))
                {
                    File.Delete(tempDest);
                }
            }
        }
예제 #13
0
        public void WebGet_2(EngineState s)
        {
            string tempSrc  = Path.GetTempFileName();
            string tempDest = Path.GetTempFileName();

            try
            {
                File.Delete(tempSrc);
                File.Delete(tempDest);

                Uri    fileUri = new Uri(tempSrc);
                string rawCode = $"WebGet,\"{fileUri.AbsoluteUri}\",\"{tempDest}\"";
                EngineTests.Eval(s, rawCode, CodeType.WebGet, ErrorCheck.Error);

                Assert.IsFalse(File.Exists(tempDest));
                Assert.IsTrue(s.Variables["StatusCode"].Equals("0", StringComparison.Ordinal));
            }
            finally
            {
                if (File.Exists(tempSrc))
                {
                    File.Delete(tempSrc);
                }
                if (File.Exists(tempDest))
                {
                    File.Delete(tempDest);
                }
            }
        }
예제 #14
0
        public void Halt_1()
        {
            string      rawCode = $"Halt,UnitTest";
            EngineState s       = EngineTests.Eval(rawCode, CodeType.Halt, ErrorCheck.Warning);

            Assert.IsTrue(s.CmdHaltFlag);
        }
예제 #15
0
        private void FileCreateBlank_Template(EngineState s, string rawCode, string fileName, Encoding encoding, bool createDummy, ErrorCheck check = ErrorCheck.Success)
        {
            string dirPath = StringEscaper.Preprocess(s, Path.Combine("%TestBench%", "CommandFile"));

            string destDir = Path.Combine(dirPath, DestDir_FileCreateBlank);

            string destFullPath = Path.Combine(destDir, fileName);

            if (Directory.Exists(destDir))
            {
                Directory.Delete(destDir, true);
            }
            Directory.CreateDirectory(destDir);
            try
            {
                if (createDummy)
                {
                    File.Create(destFullPath).Close();
                }

                EngineTests.Eval(s, rawCode, CodeType.FileCreateBlank, check);

                if (check == ErrorCheck.Success)
                {
                    Assert.IsTrue(File.Exists(destFullPath));
                    Assert.IsTrue(FileHelper.DetectTextEncoding(destFullPath) == encoding);
                }
            }
            finally
            {
                Directory.Delete(destDir, true);
            }
        }
예제 #16
0
        public void WebGetIfNotExist_Local()
        {
            string tempSrc  = CommandHashTests.SampleText();
            string tempDest = Path.GetTempFileName();

            try
            {
                Uri         fileUri = new Uri(tempSrc);
                string      rawCode = $"WebGetIfNotExist,\"{fileUri.AbsoluteUri}\",\"{tempDest}\"";
                EngineState s       = EngineTests.Eval(rawCode, CodeType.WebGetIfNotExist, ErrorCheck.Warning);

                // WebGet should have been ignored
                FileInfo info = new FileInfo(tempDest);
                Assert.IsTrue(info.Length == 0);
            }
            finally
            {
                if (File.Exists(tempSrc))
                {
                    File.Delete(tempSrc);
                }
                if (File.Exists(tempDest))
                {
                    File.Delete(tempDest);
                }
            }
        }
예제 #17
0
        private void DirDelete_Template(EngineState s, string rawCode, string dirName, ErrorCheck check = ErrorCheck.Success, bool copyDir = true)
        {
            string dirPath = StringEscaper.Preprocess(s, Path.Combine("%TestBench%", "CommandFile"));
            string srcDir  = Path.Combine(dirPath, SrcDir_Dir);
            string destDir = Path.Combine(dirPath, DestDir_DirDelete);

            string srcFullPath  = Path.Combine(srcDir, dirName);
            string destFullPath = Path.Combine(destDir, dirName);

            if (Directory.Exists(destDir))
            {
                Directory.Delete(destDir, true);
            }
            try
            {
                if (copyDir)
                {
                    FileHelper.DirectoryCopy(srcFullPath, destFullPath, true, true);
                }

                EngineTests.Eval(s, rawCode, CodeType.DirDelete, check);

                if (check == ErrorCheck.Success)
                {
                    Assert.IsFalse(Directory.Exists(destFullPath));
                }
            }
            finally
            {
                if (Directory.Exists(destDir))
                {
                    Directory.Delete(destDir, true);
                }
            }
        }
예제 #18
0
        public void WebGet_MD5(EngineState s)
        {
            string tempSrc  = CommandHashTests.SampleText();
            string tempDest = Path.GetTempFileName();

            File.Delete(tempDest);

            try
            {
                Uri    fileUri = new Uri(tempSrc);
                string rawCode = $"WebGet,\"{fileUri.AbsoluteUri}\",\"{tempDest}\",MD5,1179cf94187d2d2f94010a8d39099543";
                EngineTests.Eval(s, rawCode, CodeType.WebGet, ErrorCheck.Success);

                Assert.IsTrue(File.Exists(tempSrc));
            }
            finally
            {
                if (File.Exists(tempSrc))
                {
                    File.Delete(tempSrc);
                }
                if (File.Exists(tempDest))
                {
                    File.Delete(tempDest);
                }
            }
        }
예제 #19
0
        public void Rand()
        {
            EngineState s = EngineTests.CreateEngineState();

            void Template(string rawCode, int min, int max)
            {
                // Try many times to check proper bound of Math.Rand
                for (int i = 0; i < 256; i++)
                {
                    EngineTests.Eval(s, rawCode, CodeType.Math, ErrorCheck.Success);
                    string dest = s.Variables["Dest"];
                    Assert.IsTrue(int.TryParse(dest, NumberStyles.Integer, CultureInfo.InvariantCulture, out int destInt));
                    Assert.IsTrue(min <= destInt);
                    Assert.IsTrue(destInt < max);
                }
            }

            Template("Math,Rand,%Dest%", 0, 65536);
            Template("Math,Rand,%Dest%,0,16", 0, 16);
            Template("Math,Rand,%Dest%,16,64", 16, 64);
            Template("Math,Rand,%Dest%,32768,65536", 32768, 65536);

            // Test Error
            ErrorTemplate(s, "Math,Rand", ErrorCheck.ParserError);
            ErrorTemplate(s, "Math,Rand,%Dest%,0,1,2", ErrorCheck.ParserError);
            ErrorTemplate(s, $"Math,Rand,%Dest%,0,{(long)int.MaxValue + 16}", ErrorCheck.RuntimeError);
        }
예제 #20
0
        public void Decompress_FileTemplate(string archiveFile, string compFile, string encodingStr = null)
        {
            string archiveType = Path.GetExtension(archiveFile).Substring(1);
            string archiveName = archiveFile.Substring(0, archiveFile.Length - (archiveType.Length + 1));

            EngineState s        = EngineTests.CreateEngineState();
            string      dirPath  = StringEscaper.Preprocess(s, Path.Combine("%TestBench%", "CommandArchive"));
            string      srcPath  = Path.Combine(dirPath, archiveName, compFile);
            string      destPath = Path.Combine(dirPath, "Decompress", compFile);

            try
            {
                string rawCode = $"Decompress,\"%TestBench%\\CommandArchive\\{archiveFile}\",\"%TestBench%\\CommandArchive\\Decompress\"";
                if (encodingStr != null)
                {
                    rawCode += "," + encodingStr;
                }
                EngineTests.Eval(s, rawCode, CodeType.Decompress, ErrorCheck.Success);

                using (FileStream srcStream = new FileStream(srcPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                    using (FileStream destStream = new FileStream(destPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        byte[] srcDigest  = HashHelper.CalcHash(HashType.SHA256, srcStream);
                        byte[] destDigest = HashHelper.CalcHash(HashType.SHA256, destStream);
                        Assert.IsTrue(srcDigest.SequenceEqual(destDigest));
                    }

                Console.WriteLine($"{archiveFile} Success");
            }
            finally
            {
                File.Delete(destPath);
            }
        }
예제 #21
0
        public void CopyOrExpand_3(EngineState s)
        {
            string destDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
            string rawCode = $"CopyOrExpand,\"%TestBench%\\CommandArchive\\ex5.jpg\",\"{destDir}\"";

            EngineTests.Eval(s, rawCode, CodeType.CopyOrExpand, ErrorCheck.Error);
        }
예제 #22
0
        public void Expand_FileTemplate(EngineState s, string archiveFile, string compFile, int rev, string rawCode, ErrorCheck check, bool testPreserve = false, bool checkIfPreserve = true)
        {
            string dirPath  = StringEscaper.Preprocess(s, Path.Combine("%TestBench%", "CommandArchive"));
            string srcPath  = Path.Combine(dirPath, "Cab", compFile);
            string destPath = Path.Combine(dirPath, $"Expand_{rev}", compFile);

            try
            {
                if (testPreserve) // Check preserve
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(destPath));
                    File.Create(destPath).Close();
                }

                EngineTests.Eval(s, rawCode, CodeType.Expand, check);

                if ((!testPreserve && File.Exists(destPath)) || (testPreserve && checkIfPreserve))
                {
                    using (FileStream srcStream = new FileStream(srcPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                        using (FileStream destStream = new FileStream(destPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                        {
                            byte[] srcDigest  = HashHelper.CalcHash(HashType.SHA256, srcStream);
                            byte[] destDigest = HashHelper.CalcHash(HashType.SHA256, destStream);
                            Assert.IsTrue(srcDigest.SequenceEqual(destDigest));
                        }
                }

                Console.WriteLine($"{archiveFile}, {rev} Success");
            }
            finally
            {
                Directory.Delete(Path.GetDirectoryName(destPath), true);
            }
        }
예제 #23
0
        public void AddVariables()
        {
            // Add variables
            {
                EngineState s      = EngineTests.CreateEngineState();
                string      scPath = Path.Combine(EngineTests.BaseDir, Project.Names.Projects, "TestSuite", "Control", "General.script");

                void VariableTemplate(string rawCode, VarsType varsType)
                {
                    s.ReturnValue = string.Empty;
                    EngineTests.Eval(s, rawCode, CodeType.AddVariables, ErrorCheck.Success);

                    Assert.IsTrue(s.Variables.GetValue(varsType, "A").Equals("1", StringComparison.Ordinal));
                    Assert.IsTrue(s.Variables.GetValue(varsType, "B").Equals("2", StringComparison.Ordinal));
                    Assert.IsTrue(s.Variables.GetValue(varsType, "C").Equals("3", StringComparison.Ordinal));

                    Dictionary <string, CodeCommand> macroDict = s.Macro.GetMacroDict(varsType == VarsType.Global ? MacroType.Global : MacroType.Local);

                    Assert.IsTrue(macroDict.ContainsKey("InlineMacro"));
                    EngineTests.Eval(s, "InlineMacro", CodeType.Macro, ErrorCheck.Success);
                    Assert.IsTrue(s.ReturnValue.Equals("T", StringComparison.Ordinal));
                }

                VariableTemplate($"AddVariables,{scPath},TestVars", VarsType.Local);
                VariableTemplate($"AddVariables,{scPath},TestVars,GLOBAL", VarsType.Global);
            }

            // Add macro
            {
예제 #24
0
        private static void ReadTemplate(
            EngineState s, CodeType type,
            string rawCode, string testFile, string sampleStr, string compStr,
            ErrorCheck check = ErrorCheck.Success)
        {
            if (File.Exists(testFile))
            {
                File.Delete(testFile);
            }
            try
            {
                File.Create(testFile).Close();

                EncodingHelper.WriteTextBom(testFile, Encoding.UTF8);
                using (StreamWriter w = new StreamWriter(testFile, true, Encoding.UTF8))
                {
                    w.Write(sampleStr);
                }

                s.Variables.Delete(VarsType.Local, "Dest");
                EngineTests.Eval(s, rawCode, type, check);
                if (check == ErrorCheck.Success || check == ErrorCheck.Warning)
                {
                    Assert.IsTrue(s.Variables.ContainsKey("Dest"));
                    Assert.IsTrue(s.Variables["Dest"].Equals(compStr, StringComparison.Ordinal));
                }
            }
            finally
            {
                if (File.Exists(testFile))
                {
                    File.Delete(testFile);
                }
            }
        }
예제 #25
0
        public void WebGet_NonExistDomain(EngineState s)
        {
            // FileHelper.GetTempFile ensures very high possibility that returned temp file path is unique per call.
            string destFile = FileHelper.ReserveTempFile("html");

            try
            {
                s.ReturnValue = string.Empty;

                // Test without NOERR
                string testUrl = GenerateNeverExistUrl();
                string rawCode = $"WebGet,\"{testUrl}/Sample.txt\",\"{destFile}\"";
                EngineTests.Eval(s, rawCode, CodeType.WebGet, ErrorCheck.RuntimeError);

                Assert.IsFalse(File.Exists(destFile));
                Assert.IsTrue(s.ReturnValue.Equals("0", StringComparison.Ordinal));

                // Test with NOERR
                File.Delete(destFile);
                s.ReturnValue = string.Empty;

                rawCode = $"WebGet,\"{testUrl}/Sample.txt\",\"{destFile}\",NOERR";
                EngineTests.Eval(s, rawCode, CodeType.WebGet, ErrorCheck.Warning);

                Assert.IsFalse(File.Exists(destFile));
                Assert.IsTrue(s.ReturnValue.Equals("0", StringComparison.Ordinal));
            }
            finally
            {
                if (File.Exists(destFile))
                {
                    File.Delete(destFile);
                }
            }
        }
예제 #26
0
        private void RegMulti_Template(EngineState s, string rawCode, string[] compStrs, ErrorCheck check = ErrorCheck.Success)
        {
            using (RegistryKey subKey = Registry.CurrentUser.CreateSubKey(Dest_RegMulti, true))
            {
                subKey.SetValue("Key", new string[] { "A", "B" }, RegistryValueKind.MultiString);
            }

            EngineTests.Eval(s, rawCode, CodeType.RegMulti, check);

            if (check == ErrorCheck.Success || check == ErrorCheck.Warning)
            {
                using (RegistryKey subKey = Registry.CurrentUser.OpenSubKey(Dest_RegMulti, false))
                {
                    Assert.IsNotNull(subKey);

                    RegistryValueKind kind = subKey.GetValueKind("Key");
                    Assert.IsTrue(kind == RegistryValueKind.MultiString);

                    object valueData = subKey.GetValue("Key", null, RegistryValueOptions.DoNotExpandEnvironmentNames);
                    Assert.IsNotNull(valueData);

                    string[] destStrs = (string[])valueData;
                    Assert.IsTrue(destStrs.Length == compStrs.Length);
                    for (int i = 0; i < destStrs.Length; i++)
                    {
                        Assert.IsTrue(destStrs[i].Equals(compStrs[i], StringComparison.Ordinal));
                    }
                }
            }
        }
예제 #27
0
        public void WebGet_HashSuccess(EngineState s)
        {
            foreach (HashHelper.HashType hashType in SampleDigestDict.Keys)
            {
                string destFile = FileHelper.ReserveTempFile("html");
                try
                {
                    s.ReturnValue = string.Empty;

                    string rawCode = $"WebGet,\"{_sampleFileUrl}\",\"{destFile}\",{hashType}={SampleDigestDict[hashType]}";
                    EngineTests.Eval(s, rawCode, CodeType.WebGet, ErrorCheck.Success);

                    Assert.IsTrue(File.Exists(destFile));
                    Assert.IsTrue(TestSetup.FileEqual(_sampleSrcFile, destFile));
                    Assert.IsTrue(s.ReturnValue.Equals("200", StringComparison.Ordinal));
                }
                finally
                {
                    if (File.Exists(destFile))
                    {
                        File.Delete(destFile);
                    }
                }
            }
        }
예제 #28
0
        private void RegMulti_IndexTemplate(EngineState s, string rawCode, int compIdx, string compStr, ErrorCheck check = ErrorCheck.Success)
        {
            using (RegistryKey subKey = Registry.CurrentUser.CreateSubKey(Dest_RegMulti, true))
            {
                subKey.SetValue("Key", new string[] { "A", "B" }, RegistryValueKind.MultiString);
            }

            EngineTests.Eval(s, rawCode, CodeType.RegMulti, check);

            if (check == ErrorCheck.Success || check == ErrorCheck.Warning)
            {
                using (RegistryKey subKey = Registry.CurrentUser.OpenSubKey(Dest_RegMulti, false))
                {
                    Assert.IsNotNull(subKey);

                    RegistryValueKind kind = subKey.GetValueKind("Key");
                    Assert.IsTrue(kind == RegistryValueKind.MultiString);

                    object valueData = subKey.GetValue("Key", null, RegistryValueOptions.DoNotExpandEnvironmentNames);
                    Assert.IsNotNull(valueData);

                    string[] destStrs = (string[])valueData;
                    Assert.IsTrue(0 <= compIdx && compIdx <= destStrs.Length);
                    if (1 <= compIdx && compIdx <= destStrs.Length)
                    {
                        Assert.IsTrue(destStrs[compIdx - 1].Equals(compStr, StringComparison.Ordinal));
                        Assert.IsTrue(s.Variables["Dest"].Equals(compIdx.ToString(), StringComparison.Ordinal));
                    }
                }
            }
        }
예제 #29
0
        public void CopyOrExpand_2(EngineState s)
        {
            string dirPath  = StringEscaper.Preprocess(s, Path.Combine("%TestBench%", "CommandArchive"));
            string srcPath  = Path.Combine(dirPath, "Cab", "ex3.jpg");
            string destDir  = FileHelper.GetTempDir();
            string destFile = Path.Combine(destDir, "change.jpg");

            try
            {
                string rawCode = $"CopyOrExpand,\"%TestBench%\\CommandArchive\\ex3.jpg\",\"{destDir}\\change.jpg\"";
                EngineTests.Eval(s, rawCode, CodeType.CopyOrExpand, ErrorCheck.Success);

                Assert.IsTrue(Directory.Exists(destDir));
                Assert.IsTrue(File.Exists(destFile));

                using (FileStream srcStream = new FileStream(srcPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                    using (FileStream destStream = new FileStream(destFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        byte[] srcDigest  = HashHelper.GetHash(HashHelper.HashType.SHA256, srcStream);
                        byte[] destDigest = HashHelper.GetHash(HashHelper.HashType.SHA256, destStream);
                        Assert.IsTrue(srcDigest.SequenceEqual(destDigest));
                    }
            }
            finally
            {
                if (Directory.Exists(destDir))
                {
                    Directory.Delete(destDir, true);
                }
            }
        }
예제 #30
0
        public void Exit_1()
        {
            string      rawCode = $"Exit,UnitTest";
            EngineState s       = EngineTests.Eval(rawCode, CodeType.Exit, ErrorCheck.Warning);

            Assert.IsTrue(s.PassCurrentScriptFlag);
        }