예제 #1
0
        public void Decompress_DirTemplate(string archiveFile, 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      srcFullPath = Path.Combine(dirPath, archiveName);
            string      destRootDir = Path.Combine(dirPath, "Decompress");

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

                string[] srcFiles  = Directory.GetFiles(srcFullPath, "*", SearchOption.AllDirectories);
                string[] destFiles = Directory.GetFiles(Path.Combine(destRootDir, archiveName), "*", SearchOption.AllDirectories);
                Assert.IsTrue(srcFiles.Length == destFiles.Length);

                for (int i = 0; i < srcFiles.Length; i++)
                {
                    using (FileStream srcStream = new FileStream(srcFiles[i], FileMode.Open, FileAccess.Read, FileShare.Read))
                        using (FileStream destStream = new FileStream(destFiles[i], 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
            {
                Directory.Delete(Path.Combine(destRootDir, archiveName), true);
            }
        }
예제 #2
0
        public void BoolLogicOper()
        {
            EngineState s = EngineTests.CreateEngineState();

            // BoolAnd
            SuccessTemplate(s, "Math,BoolAnd,%Dest%,True,True", "True");
            SuccessTemplate(s, "Math,BoolAnd,%Dest%,True,False", "False");
            SuccessTemplate(s, "Math,BoolAnd,%Dest%,False,True", "False");
            SuccessTemplate(s, "Math,BoolAnd,%Dest%,False,False", "False");
            SuccessTemplate(s, "Math,BoolAnd,%Dest%,1,1", "True");
            SuccessTemplate(s, "Math,BoolAnd,%Dest%,1,0", "False");
            SuccessTemplate(s, "Math,BoolAnd,%Dest%,0,1", "False");
            SuccessTemplate(s, "Math,BoolAnd,%Dest%,0,0", "False");

            // BoolOr
            SuccessTemplate(s, "Math,BoolOr,%Dest%,True,True", "True");
            SuccessTemplate(s, "Math,BoolOr,%Dest%,True,False", "True");
            SuccessTemplate(s, "Math,BoolOr,%Dest%,False,True", "True");
            SuccessTemplate(s, "Math,BoolOr,%Dest%,False,False", "False");
            SuccessTemplate(s, "Math,BoolOr,%Dest%,2,1", "True");
            SuccessTemplate(s, "Math,BoolOr,%Dest%,2,0", "True");
            SuccessTemplate(s, "Math,BoolOr,%Dest%,0,1", "True");
            SuccessTemplate(s, "Math,BoolOr,%Dest%,0,0", "False");

            // BoolXor
            SuccessTemplate(s, "Math,BoolXor,%Dest%,True,True", "False");
            SuccessTemplate(s, "Math,BoolXor,%Dest%,True,False", "True");
            SuccessTemplate(s, "Math,BoolXor,%Dest%,False,True", "True");
            SuccessTemplate(s, "Math,BoolXor,%Dest%,False,False", "False");
            SuccessTemplate(s, "Math,BoolXor,%Dest%,-1,-2", "False");
            SuccessTemplate(s, "Math,BoolXor,%Dest%,-234,0", "True");
            SuccessTemplate(s, "Math,BoolXor,%Dest%,0,2341345", "True");
            SuccessTemplate(s, "Math,BoolXor,%Dest%,0,0", "False");

            // Test Error
            ErrorTemplate(s, "Math,BoolAnd,Dest,4", ErrorCheck.ParserError);
            ErrorTemplate(s, "Math,BoolOr,3,4", ErrorCheck.ParserError);
            ErrorTemplate(s, "Math,BoolXor,%Dest%,4", ErrorCheck.ParserError);
            ErrorTemplate(s, "Math,BoolXor,%Dest%,B,E", ErrorCheck.RuntimeError);
        }
        public void Visible()
        {
            string srcFile    = Path.Combine(EngineTests.Project.ProjectDir, TestSuiteInterface, "ReadInterface.script");
            string scriptFile = Path.GetTempFileName();


            try
            {
                void SingleTemplate(string rawCode, string key, string compStr, ErrorCheck check = ErrorCheck.Success)
                {
                    File.Copy(srcFile, scriptFile, true);

                    EngineState s  = EngineTests.CreateEngineState();
                    Script      sc = s.Project.LoadScriptRuntime(scriptFile, new LoadScriptRuntimeOptions());
                    // ScriptSection ifaceSection = sc.GetInterfaceSection(out _);
                    ScriptSection section = sc.Sections["Process"];

                    // Enable Visible command
                    CodeParser.Options opts = CodeParser.Options.CreateOptions(Global.Setting, EngineTests.Project.Compat);
                    opts.AllowLegacyInterfaceCommand = true;
                    CodeParser parser = new CodeParser(section, opts);

                    try
                    {
                        EngineTests.Eval(s, parser, rawCode, CodeType.Visible, check);
                        if (check == ErrorCheck.Success)
                        {
                            string dest = IniReadWriter.ReadKey(scriptFile, "Interface", key);
                            Assert.IsTrue(dest.Equals(compStr, StringComparison.Ordinal));
                        }
                    }
                    finally
                    {
                        if (File.Exists(scriptFile))
                        {
                            File.Delete(scriptFile);
                        }
                    }
                }
                void OptTemplate(List <string> rawCodes, (string key, string value)[] compTuples, bool optSuccess, ErrorCheck check = ErrorCheck.Success)
예제 #4
0
        public void Reg_RegWrite()
        { // RegWrite,<HKey>,<ValueType>,<KeyPath>,<ValueName>,<ValueData | ValueDatas>,[NOWARN]
            EngineState s = EngineTests.CreateEngineState();

            string subKey = Dest_RegWrite;

            Registry.CurrentUser.DeleteSubKeyTree(subKey, false);

            RegWrite_Template(s, $@"RegWrite,HKCU,0x0,{subKey},None",
                              Registry.CurrentUser, RegistryValueKind.None, subKey, "None", null);
            RegWrite_Template(s, $@"RegWrite,HKCU,0x1,{subKey},String,SZ",
                              Registry.CurrentUser, RegistryValueKind.String, subKey, "String", "SZ");
            RegWrite_Template(s, $@"RegWrite,HKCU,0x2,{subKey},ExpandString,#$pSystemRoot#$p\System32\notepad.exe",
                              Registry.CurrentUser, RegistryValueKind.ExpandString, subKey, "ExpandString", @"%SystemRoot%\System32\notepad.exe");
            RegWrite_Template(s, $@"RegWrite,HKCU,0x7,{subKey},MultiString,1,2,3",
                              Registry.CurrentUser, RegistryValueKind.MultiString, subKey, "MultiString", new string[] { "1", "2", "3" });
            RegWrite_Template(s, $@"RegWrite,HKCU,0x3,{subKey},Binary,00,01,02",
                              Registry.CurrentUser, RegistryValueKind.Binary, subKey, "Binary", new byte[] { 00, 01, 02 });
            RegWrite_Template(s, $@"RegWrite,HKCU,0x3,{subKey},Binary,""03,04""",
                              Registry.CurrentUser, RegistryValueKind.Binary, subKey, "Binary", new byte[] { 03, 04 },
                              ErrorCheck.Overwrite);
            RegWrite_Template(s, $@"RegWrite,HKCU,0x3,{subKey},Binary,05,06,07,NOWARN",
                              Registry.CurrentUser, RegistryValueKind.Binary, subKey, "Binary", new byte[] { 05, 06, 07 });
            RegWrite_Template(s, $@"RegWrite,HKCU,0x3,{subKey},Binary,""08,09"",NOWARN",
                              Registry.CurrentUser, RegistryValueKind.Binary, subKey, "Binary", new byte[] { 08, 09 });
            RegWrite_Template(s, $@"RegWrite,HKCU,0x4,{subKey},DWORD,1234",
                              Registry.CurrentUser, RegistryValueKind.DWord, subKey, "DWORD", (uint)1234);
            RegWrite_Template(s, $@"RegWrite,HKCU,0x4,{subKey},DWORD,-1",
                              Registry.CurrentUser, RegistryValueKind.DWord, subKey, "DWORD", (uint)4294967295,
                              ErrorCheck.Overwrite);
            RegWrite_Template(s, $@"RegWrite,HKCU,0x4,{subKey},DWORD,4294967295",
                              Registry.CurrentUser, RegistryValueKind.DWord, subKey, "DWORD", (uint)4294967295,
                              ErrorCheck.Overwrite);
            RegWrite_Template(s, $@"RegWrite,HKCU,0xB,{subKey},QWORD,4294967296",
                              Registry.CurrentUser, RegistryValueKind.QWord, subKey, "QWORD", (ulong)4294967296);

            RegWrite_Template_Error(s, $@"RegWrite,HKCU,0x4,{subKey}", ErrorCheck.ParserError);

            Registry.CurrentUser.DeleteSubKeyTree(subKey, false);
        }
예제 #5
0
        public void Math_Hex()
        {
            EngineState s = EngineTests.CreateEngineState();

            // 8bit
            Math_Template(s, "Math,Hex,%Dest%,15,8", "0F");
            Math_Template(s, "Math,Hex,%Dest%,0x0F,8", "0F");
            Math_Template(s, "Math,Hex,%Dest%,-1,8", "FF");
            Math_Template(s, "Math,Hex,%Dest%,255,8", "FF");

            // 16bit
            Math_Template(s, "Math,Hex,%Dest%,15,16", "000F");
            Math_Template(s, "Math,Hex,%Dest%,0x0F,16", "000F");
            Math_Template(s, "Math,Hex,%Dest%,-1,16", "FFFF");
            Math_Template(s, "Math,Hex,%Dest%,255,16", "00FF");

            // 32bit
            Math_Template(s, "Math,Hex,%Dest%,15,32", "0000000F");
            Math_Template(s, "Math,Hex,%Dest%,0x0F,32", "0000000F");
            Math_Template(s, "Math,Hex,%Dest%,-1,32", "FFFFFFFF");
            Math_Template(s, "Math,Hex,%Dest%,255,32", "000000FF");

            // 32bit (default)
            Math_Template(s, "Math,Hex,%Dest%,15", "0000000F");
            Math_Template(s, "Math,Hex,%Dest%,0x0F", "0000000F");
            Math_Template(s, "Math,Hex,%Dest%,-1", "FFFFFFFF");
            Math_Template(s, "Math,Hex,%Dest%,255", "000000FF");

            // 64bit
            Math_Template(s, "Math,Hex,%Dest%,15,64", "000000000000000F");
            Math_Template(s, "Math,Hex,%Dest%,0x0F,64", "000000000000000F");
            Math_Template(s, "Math,Hex,%Dest%,-1,64", "FFFFFFFFFFFFFFFF");
            Math_Template(s, "Math,Hex,%Dest%,255,64", "00000000000000FF");

            // Test Error
            Math_Template_Error(s, "Math,Hex,%Dest%", ErrorCheck.ParserError);
            Math_Template_Error(s, "Math,Hex,%Dest%,256,9", ErrorCheck.ParserError);
            Math_Template_Error(s, "Math,Hex,%Dest%,256,9,12", ErrorCheck.ParserError);
            Math_Template_Error(s, "Math,Hex,%Dest%,256,8", ErrorCheck.Error);
        }
예제 #6
0
        public void IniReadSection()
        {
            EngineState s         = EngineTests.CreateEngineState();
            string      sampleStr = SampleStr();

            string tempDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

            try
            {
                Directory.CreateDirectory(tempDir);
                string tempFile  = Path.Combine(tempDir, Path.GetRandomFileName());
                string tempFile2 = Path.Combine(tempDir, Path.GetRandomFileName());

                const string sec1 = "A|1|B|2|C|3";
                const string sec2 = "X|#$pFour#$p|Y|5|Z|6";
                const string sec3 = "가|7|나|8|다|9";

                ReadTemplate(s, CodeType.IniReadSection, $@"IniReadSection,{tempFile},Sec1,%Dest%", tempFile, sampleStr, sec1);
                ReadTemplate(s, CodeType.IniReadSection, $@"IniReadSection,{tempFile},Sec2,%Dest%", tempFile, sampleStr, sec2);
                ReadTemplate(s, CodeType.IniReadSection, $@"IniReadSection,{tempFile},Sec3,%Dest%", tempFile, sampleStr, sec3);
                ReadTemplate(s, CodeType.IniReadSection, $@"IniReadSection,{tempFile},Sec1,Dest", tempFile, string.Empty, null, ErrorCheck.ParserError);

                // Optimization
                ReadOptTemplate(s, CodeType.IniReadSectionOp, new List <string>
                {
                    $@"IniReadSection,{tempFile},Sec1,%Dest0%",
                    $@"IniReadSection,{tempFile},Sec3,%Dest1%",
                }, tempFile, sampleStr, new string[] { sec1, sec3 });
                ReadOptTemplate(s, null, new List <string>
                {
                    $@"IniReadSection,{tempFile},Sec1,%Dest0%",
                    $@"IniReadSection,{tempFile2},Sec3,%Dest1%",
                }, tempFile, sampleStr, new string[] { sec1 });
            }
            finally
            {
                Directory.Delete(tempDir, true);
            }
        }
예제 #7
0
        public void TXTDelSpaces()
        {
            EngineState s = EngineTests.CreateEngineState();

            string tempDir = FileHelper.GetTempDir();

            try
            {
                string tempFile = Path.Combine(tempDir, "Sample.txt");

                StringBuilder b = new StringBuilder();
                b.AppendLine("A B C");
                b.AppendLine(" D E F");
                b.AppendLine("G H I ");
                b.AppendLine("  J K L");
                b.AppendLine("M N O  ");
                b.AppendLine("  X Y Z  ");
                b.AppendLine();
                b.AppendLine("\t가 나 다");
                string sampleStr = b.ToString();

                SingleTemplate(s, CodeType.TXTDelSpaces, $@"TXTDelSpaces,{tempFile}", tempFile, string.Empty, string.Empty);

                b = new StringBuilder();
                b.AppendLine("A B C");
                b.AppendLine("D E F");
                b.AppendLine("G H I");
                b.AppendLine("J K L");
                b.AppendLine("M N O");
                b.AppendLine("X Y Z");
                b.AppendLine();
                b.AppendLine("가 나 다");
                SingleTemplate(s, CodeType.TXTDelSpaces, $@"TXTDelSpaces,{tempFile}", tempFile, sampleStr, b.ToString());
            }
            finally
            {
                Directory.Delete(tempDir, true);
            }
        }
예제 #8
0
        public void Branch_IfExistMacro()
        {
            EngineState         s    = EngineTests.CreateEngineState();
            BranchConditionType type = BranchConditionType.ExistMacro;
            BranchCondition     cond;

            // Test if Unicode can be used in macro name
            s.Macro.MacroDict["대한"]    = CodeParser.ParseStatement("Echo,054-790-6641", EngineTests.DummySectionAddress());
            s.Macro.LocalDict["Sonic"] = CodeParser.ParseStatement("Echo,Tails", EngineTests.DummySectionAddress());
            s.Variables.SetValue(VarsType.Local, "Tails", "Sonic");

            cond = new BranchCondition(type, false, "대한");
            Assert.IsTrue(cond.Check(s, out string d));
            cond = new BranchCondition(type, false, "민국");
            Assert.IsFalse(cond.Check(s, out d));
            cond = new BranchCondition(type, false, "Sonic");
            Assert.IsTrue(cond.Check(s, out d));
            cond = new BranchCondition(type, false, "%Tails%");
            Assert.IsTrue(cond.Check(s, out d));

            cond = new BranchCondition(type, true, "대한");
            Assert.IsFalse(cond.Check(s, out d));
            cond = new BranchCondition(type, true, "민국");
            Assert.IsTrue(cond.Check(s, out d));
            cond = new BranchCondition(type, true, "Sonic");
            Assert.IsFalse(cond.Check(s, out d));
            cond = new BranchCondition(type, true, "%Tails%");
            Assert.IsFalse(cond.Check(s, out d));

            BranchCondition_Single_Template(s, @"If,ExistMacro,대한,Set,%Dest%,T", "T");
            BranchCondition_Single_Template(s, @"If,ExistMacro,민국,Set,%Dest%,T", "F");
            BranchCondition_Single_Template(s, @"If,ExistMacro,Sonic,Set,%Dest%,T", "T");
            BranchCondition_Single_Template(s, @"If,ExistMacro,%Tails%,Set,%Dest%,T", "T");
            BranchCondition_Single_Template(s, @"If,Not,ExistMacro,대한,Set,%Dest%,T", "F");
            BranchCondition_Single_Template(s, @"If,Not,ExistMacro,민국,Set,%Dest%,T", "T");
            BranchCondition_Single_Template(s, @"If,Not,ExistMacro,Sonic,Set,%Dest%,T", "F");
            BranchCondition_Single_Template(s, @"If,Not,ExistMacro,%Tails%,Set,%Dest%,T", "F");
        }
예제 #9
0
        public void Text_TXTDelEmptyLines()
        { // TXTDelEmptyLines,<FileName>
            EngineState s = EngineTests.CreateEngineState();

            string tempDir = Path.GetTempFileName();

            File.Delete(tempDir);
            Directory.CreateDirectory(tempDir);
            string tempFile = Path.Combine(tempDir, "Sample.txt");

            StringBuilder b = new StringBuilder();

            b.AppendLine("A B C");
            b.AppendLine(" D E F");
            b.AppendLine("  X Y Z");
            b.AppendLine();
            b.AppendLine("\t가 나 다");
            b.AppendLine();
            b.AppendLine("힣");
            string sampleStr = b.ToString();

            try
            {
                Text_Template(s, CodeType.TXTDelEmptyLines, $@"TXTDelEmptyLines,{tempFile}", tempFile, string.Empty, string.Empty);

                b = new StringBuilder();
                b.AppendLine("A B C");
                b.AppendLine(" D E F");
                b.AppendLine("  X Y Z");
                b.AppendLine("\t가 나 다");
                b.AppendLine("힣");
                Text_Template(s, CodeType.TXTDelEmptyLines, $@"TXTDelEmptyLines,{tempFile}", tempFile, sampleStr, b.ToString());
            }
            finally
            {
                Directory.Delete(tempDir, true);
            }
        }
예제 #10
0
        public void TXTReplace()
        {
            EngineState s = EngineTests.CreateEngineState();

            string tempDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

            try
            {
                Directory.CreateDirectory(tempDir);
                string tempFile  = Path.Combine(tempDir, "Sample.txt");
                string tempFile2 = Path.Combine(tempDir, "Sample2.txt");

                SingleTemplate(s, CodeType.TXTReplace, $@"TXTReplace,{tempFile},AB,XYZ", tempFile, "ABCD", "XYZCD");
                SingleTemplate(s, CodeType.TXTReplace, $@"TXTReplace,{tempFile},ab,XYZ", tempFile, "ABCD", "XYZCD");
                SingleTemplate(s, CodeType.TXTReplace, $@"TXTReplace,{tempFile},AB,XYZ", tempFile, "abcd", "XYZcd");
                SingleTemplate(s, CodeType.TXTReplace, $@"TXTReplace,{tempFile},ab,XYZ", tempFile, "abcd", "XYZcd");

                // Unicode, NewLine Test
                SingleTemplate(s, CodeType.TXTReplace, $@"TXTReplace,{tempFile},#$x,나다", tempFile, "가\r\n라", "가나다라");

                // Optimization
                OptTemplate(s, CodeType.TXTReplaceOp, new List <string>
                {
                    $@"TXTReplace,{tempFile},AB,PE",
                    $@"TXTReplace,{tempFile},PE,XY",
                    $@"TXTReplace,{tempFile},XYZ,TEB",
                }, tempFile, "ABC\r\nXYZ\r\nABC\r\n가나다", "XYC\r\nTEB\r\nXYC\r\n가나다");
                OptTemplate(s, null, new List <string>
                {
                    $@"TXTReplace,{tempFile},AB,PE",
                    $@"TXTReplace,{tempFile2},XYZ,TEB",
                }, tempFile, "ABC\r\nXYZ\r\nABC\r\n가나다", "PEC\r\nXYZ\r\nPEC\r\n가나다");
            }
            finally
            {
                Directory.Delete(tempDir, true);
            }
        }
예제 #11
0
        public void ErrorOff()
        {
            EngineState s = EngineTests.CreateEngineState();

            void SingleTemplate(List <string> rawCodes, ErrorCheck check = ErrorCheck.Success)
            {
                EngineTests.EvalLines(s, rawCodes, check);
            }

            void ScriptTemplate(string treePath, ErrorCheck check = ErrorCheck.Success)
            {
                EngineTests.EvalScript(treePath, check);
            }

            SingleTemplate(new List <string>
            {
                @"System,ErrorOff",
                @"Error1",
            });
            SingleTemplate(new List <string>
            {
                @"System,ErrorOff,3",
                @"Error1",
                @"Error2",
                @"Error3",
            });
            SingleTemplate(new List <string>
            {
                @"System,ErrorOff,2",
                @"Error1",
                @"Error2",
                @"Error3",
            }, ErrorCheck.RuntimeError);

            string scPath = Path.Combine(EngineTests.Project.ProjectName, "System", "ErrorOff.script");

            ScriptTemplate(scPath);
        }
예제 #12
0
        public void DirSize()
        {
            EngineState s             = EngineTests.CreateEngineState();
            string      scriptDirPath = Path.Combine("%TestBench%", "CommandFile");
            string      scriptSrcDir  = Path.Combine(scriptDirPath, SrcDirDir);

            void Template(string rawCode, string comp, ErrorCheck check = ErrorCheck.Success)
            {
                EngineTests.Eval(s, rawCode, CodeType.DirSize, check);

                if (check == ErrorCheck.Success)
                {
                    Assert.IsTrue(s.Variables["Dest"].Equals(comp, StringComparison.Ordinal));
                }
            }

            // Reuse FileSize_Template
            Template($@"DirSize,{scriptSrcDir}\ABCD,%Dest%", "9");
            Template($@"DirSize,{scriptSrcDir}\ABDE,%Dest%", "3");
            Template($@"DirSize,{scriptSrcDir},%Dest%", "13");
            Template($@"Retrieve,FolderSize,{scriptSrcDir},%Dest%", "13");
            Template($@"DirSize,{scriptSrcDir}\NotExist,%Dest%", string.Empty, ErrorCheck.RuntimeError);
        }
예제 #13
0
        public void IntegerSignedness()
        {
            EngineState s = EngineTests.CreateEngineState();

            // IntSign
            SuccessTemplate(s, "Math,ToSign,%Dest%,1", "1");           // 32
            SuccessTemplate(s, "Math,ToSign,%Dest%,4294967295", "-1"); // 32
            SuccessTemplate(s, "Math,ToSign,%Dest%,2,16", "2");        // 16
            SuccessTemplate(s, "Math,ToSign,%Dest%,65534,16", "-2");   // 16

            // IntUnsign
            SuccessTemplate(s, "Math,ToUnsign,%Dest%,1", "1");           // 32
            SuccessTemplate(s, "Math,ToUnsign,%Dest%,-1", "4294967295"); // 32
            SuccessTemplate(s, "Math,ToUnsign,%Dest%,2,16", "2");        // 16
            SuccessTemplate(s, "Math,ToUnsign,%Dest%,-2,16", "65534");   // 16

            // Test Error
            ErrorTemplate(s, "Math,ToSign,Dest,1", ErrorCheck.ParserError);
            ErrorTemplate(s, "Math,ToUnsign,%Dest%,1,2", ErrorCheck.ParserError);
            ErrorTemplate(s, "Math,ToSign,%Dest%,XYZ", ErrorCheck.Error);
            ErrorTemplate(s, "Math,ToUnsign,%Dest%,12.3", ErrorCheck.Error);
            ErrorTemplate(s, "Math,ToUnsign,%Dest%,12.0", ErrorCheck.Error);
        }
예제 #14
0
        public void Branch_IfWimExistDir()
        {
            EngineState         s = EngineTests.CreateEngineState();
            BranchCondition     cond;
            BranchConditionType type = BranchConditionType.WimExistDir;

            string srcWim = Path.Combine("%TestBench%", "CommandWim", "MultiImage.wim");

            cond = new BranchCondition(type, false, srcWim, "1", "A.txt");
            Assert.IsFalse(CommandBranch.CheckBranchCondition(s, cond, out _));
            cond = new BranchCondition(type, false, srcWim, "1", "B");
            Assert.IsTrue(CommandBranch.CheckBranchCondition(s, cond, out _));

            cond = new BranchCondition(type, true, srcWim, "1", "A.txt");
            Assert.IsTrue(CommandBranch.CheckBranchCondition(s, cond, out _));
            cond = new BranchCondition(type, true, srcWim, "1", "B");
            Assert.IsFalse(CommandBranch.CheckBranchCondition(s, cond, out _));

            BranchCondition_Single_Template(s, $"If,WimExistDir,{srcWim},1,A.txt,Set,%Dest%,T", "F");
            BranchCondition_Single_Template(s, $"If,WimExistDir,{srcWim},1,B,Set,%Dest%,T", "T");
            BranchCondition_Single_Template(s, $"If,Not,WimExistDir,{srcWim},1,A.txt,Set,%Dest%,T", "T");
            BranchCondition_Single_Template(s, $"If,Not,WimExistDir,{srcWim},1,B,Set,%Dest%,T", "F");
        }
예제 #15
0
        public void BitNot()
        {
            EngineState s = EngineTests.CreateEngineState();

            s.Variables["BitSize"] = "16";
            s.Variables["Garbage"] = "128";

            // BoolNot
            SuccessTemplate(s, "Math,BitNot,%Dest%,2,32", "4294967293");   // 32bit
            SuccessTemplate(s, "Math,BitNot,%Dest%,4294967293,32", "2");   // 32bit
            SuccessTemplate(s, "Math,BitNot,%Dest%,2,16", "65533");        // 16bit
            SuccessTemplate(s, "Math,BitNot,%Dest%,65533,16", "2");        // 16bit
            SuccessTemplate(s, "Math,BitNot,%Dest%,2,%BitSize%", "65533"); // 16bit, Var
            SuccessTemplate(s, "Math,BitNot,%Dest%,65533,%BitSize%", "2"); // 16bit, Var

            // Test Error
            ErrorTemplate(s, "Math,BitNot,%Dest%,2", ErrorCheck.ParserError);          // Had been valid prior to 0.9.6 beta6
            ErrorTemplate(s, "Math,BitNot,%Dest%,4294967293", ErrorCheck.ParserError); // Had been valid prior to 0.9.6 beta6
            ErrorTemplate(s, "Math,BitNot,Dest,12,8", ErrorCheck.ParserError);
            ErrorTemplate(s, "Math,BitNot,%Dest%,12,17", ErrorCheck.ParserError);
            ErrorTemplate(s, "Math,BitNot,%Dest%,ABC", ErrorCheck.ParserError);
            ErrorTemplate(s, "Math,BitNot,%Dest%,2,%Garbage%", ErrorCheck.RuntimeError);
        }
예제 #16
0
        public void Branch_IfExistVar()
        {
            EngineState         s    = EngineTests.CreateEngineState();
            BranchConditionType type = BranchConditionType.ExistVar;
            BranchCondition     cond;

            s.Variables.SetValue(VarsType.Fixed, "F", "ixed");
            s.Variables.SetValue(VarsType.Fixed, "G", "lobal");
            s.Variables.SetValue(VarsType.Fixed, "L", "ocal");

            cond = new BranchCondition(type, false, "%F%");
            Assert.IsTrue(cond.Check(s, out string d));
            cond = new BranchCondition(type, false, "%G%");
            Assert.IsTrue(cond.Check(s, out d));
            cond = new BranchCondition(type, false, "%L%");
            Assert.IsTrue(cond.Check(s, out d));
            cond = new BranchCondition(type, false, "%N%");
            Assert.IsFalse(cond.Check(s, out d));

            cond = new BranchCondition(type, true, "%F%");
            Assert.IsFalse(cond.Check(s, out d));
            cond = new BranchCondition(type, true, "%G%");
            Assert.IsFalse(cond.Check(s, out d));
            cond = new BranchCondition(type, true, "%L%");
            Assert.IsFalse(cond.Check(s, out d));
            cond = new BranchCondition(type, true, "%N%");
            Assert.IsTrue(cond.Check(s, out d));

            BranchCondition_Single_Template(s, @"If,ExistVar,%F%,Set,%Dest%,T", "T");
            BranchCondition_Single_Template(s, @"If,ExistVar,%G%,Set,%Dest%,T", "T");
            BranchCondition_Single_Template(s, @"If,ExistVar,%L%,Set,%Dest%,T", "T");
            BranchCondition_Single_Template(s, @"If,ExistVar,%N%,Set,%Dest%,T", "F");
            BranchCondition_Single_Template(s, @"If,Not,ExistVar,%F%,Set,%Dest%,T", "F");
            BranchCondition_Single_Template(s, @"If,Not,ExistVar,%G%,Set,%Dest%,T", "F");
            BranchCondition_Single_Template(s, @"If,Not,ExistVar,%L%,Set,%Dest%,T", "F");
            BranchCondition_Single_Template(s, @"If,Not,ExistVar,%N%,Set,%Dest%,T", "T");
        }
예제 #17
0
        public void Dec()
        {
            EngineState s = EngineTests.CreateEngineState();

            // 8bit
            SuccessTemplate(s, "Math,Dec,%Dest%,0x0F,8", "15");
            SuccessTemplate(s, "Math,Dec,%Dest%,0xFF,8", "255");
            SuccessTemplate(s, "Math,Dec,%Dest%,-1,8", "255");
            SuccessTemplate(s, "Math,Dec,%Dest%,255,8", "255");
            ErrorTemplate(s, "Math,Dec,%Dest%,0xFFFF,8", ErrorCheck.Error);

            // 16bit
            SuccessTemplate(s, "Math,Dec,%Dest%,0x000F,16", "15");
            SuccessTemplate(s, "Math,Dec,%Dest%,-1,16", "65535");
            SuccessTemplate(s, "Math,Dec,%Dest%,0xFFFF,16", "65535");
            ErrorTemplate(s, "Math,Dec,%Dest%,0x10000,16", ErrorCheck.Error);

            // 32bit
            SuccessTemplate(s, "Math,Dec,%Dest%,0x0F,32", "15");
            SuccessTemplate(s, "Math,Dec,%Dest%,-1,32", "4294967295");
            ErrorTemplate(s, "Math,Dec,%Dest%,0x100000000,32", ErrorCheck.Error);

            // 32bit (default)
            SuccessTemplate(s, "Math,Dec,%Dest%,0x0F", "15");
            SuccessTemplate(s, "Math,Dec,%Dest%,-1", "4294967295");
            ErrorTemplate(s, "Math,Dec,%Dest%,0x100000000", ErrorCheck.Error);

            // 64bit
            SuccessTemplate(s, "Math,Dec,%Dest%,0x0F,64", "15");
            SuccessTemplate(s, "Math,Dec,%Dest%,-1,64", "18446744073709551615");

            // Test Error
            ErrorTemplate(s, "Math,Dec,%Dest%", ErrorCheck.ParserError);
            ErrorTemplate(s, "Math,Dec,%Dest%,256,9", ErrorCheck.ParserError);
            ErrorTemplate(s, "Math,Dec,%Dest%,256,9,12", ErrorCheck.ParserError);
        }
예제 #18
0
        public void Decompress()
        {
            void DirTemplate(string archiveFile)
            {
                Debug.Assert(archiveFile != 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      srcFullPath = Path.Combine(dirPath, archiveName);
                string      destDir     = FileHelper.GetTempDir();

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

                    string[] srcFiles  = Directory.GetFiles(srcFullPath, "*", SearchOption.AllDirectories);
                    string[] destFiles = Directory.GetFiles(Path.Combine(destDir, archiveName), "*", SearchOption.AllDirectories);
                    Assert.IsTrue(srcFiles.Length == destFiles.Length);

                    for (int i = 0; i < srcFiles.Length; i++)
                    {
                        using (FileStream srcStream = new FileStream(srcFiles[i], FileMode.Open, FileAccess.Read, FileShare.Read))
                            using (FileStream destStream = new FileStream(destFiles[i], 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);
                    }
                }
            }

            void FileTemplate(string archiveFile, string originDir, string originFile, string password = null)
            {
                Debug.Assert(archiveFile != null);

                EngineState s        = EngineTests.CreateEngineState();
                string      dirPath  = StringEscaper.Preprocess(s, Path.Combine("%TestBench%", "CommandArchive"));
                string      srcPath  = Path.Combine(dirPath, originDir, originFile);
                string      destDir  = FileHelper.GetTempDir();
                string      destPath = Path.Combine(destDir, originFile);

                try
                {
                    string rawCode;
                    if (password == null)
                    {
                        rawCode = $"Decompress,\"%TestBench%\\CommandArchive\\{archiveFile}\",\"{destDir}\"";
                    }
                    else
                    {
                        rawCode = $"Decompress,\"%TestBench%\\CommandArchive\\{archiveFile}\",\"{destDir}\",Password={password}";
                    }

                    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.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);
                    }
                }
            }

            DirTemplate("Korea.zip");
            DirTemplate("Korea.7z");
            DirTemplate("France.zip");
            DirTemplate("France.7z");
            DirTemplate("France.rar"); // RAR5
            FileTemplate("Korean_IME_Logo.zip", "Korean_IME_Logo", "Korean_IME_Logo.jpg");
            FileTemplate("Korean_IME_Logo.7z", "Korean_IME_Logo", "Korean_IME_Logo.jpg");
            FileTemplate("Korean_IME_Logo.rar", "Korean_IME_Logo", "Korean_IME_Logo.jpg"); // RAR2.9
            // Decompress password-protected archives
            // ReSharper disable StringLiteralTypo
            FileTemplate("Password_ZipCrypto.zip", "Password", "Password.txt", "abcxyz");
            FileTemplate("Password_AES256.zip", "Password", "Password.txt", "abcxyz");
            FileTemplate("Password_AES256.7z", "Password", "Password.txt", "abcxyz");
            // ReSharper restore StringLiteralTypo
        }
예제 #19
0
        public void RegCopy()
        {
            EngineState s = EngineTests.CreateEngineState();

            string singleSrcSet = Path.Combine(RegCopyPath, "Src");
            string multiSrcSet1 = Path.Combine(RegCopyPath, "Set*");
            string multiSrcSet2 = Path.Combine(RegCopyPath, "Set?0");
            string destSet      = Path.Combine(RegCopyPath, "Dest");

            // Success
            SingleTemplate($@"RegCopy,HKCU,{singleSrcSet},HKCU,{destSet}", Registry.CurrentUser,
                           singleSrcSet, destSet);
            WildcardTemplate($@"RegCopy,HKCU,{multiSrcSet1},HKCU,{destSet},WILDCARD", Registry.CurrentUser,
                             RegCopyPath, new string[] { "Set10", "Set20", "Set31" },
                             destSet, new string[] { "Set10", "Set20", "Set31" });
            WildcardTemplate($@"RegCopy,HKCU,{multiSrcSet2},HKCU,{destSet},WILDCARD", Registry.CurrentUser,
                             RegCopyPath, new string[] { "Set10", "Set20", "Set31" },
                             destSet, new string[] { "Set10", "Set20" });

            // Error
            SingleTemplate($@"RegCopy,HKCU,{singleSrcSet},HKCU,{destSet},WILDCARD", Registry.CurrentUser,
                           singleSrcSet, destSet, ErrorCheck.RuntimeError);
            WildcardTemplate($@"RegCopy,HKCU,{multiSrcSet1},HKCU,{destSet}", Registry.CurrentUser,
                             RegCopyPath, new string[] { "Set10", "Set20", "Set31" },
                             destSet, new string[] { "Set10", "Set20", "Set31" },
                             ErrorCheck.RuntimeError);
            WildcardTemplate($@"RegCopy,HKCU,{multiSrcSet2},HKCU,{destSet}", Registry.CurrentUser,
                             RegCopyPath, new string[] { "Set10", "Set20", "Set31" },
                             destSet, new string[] { "Set10", "Set20" },
                             ErrorCheck.RuntimeError);

            #region CreateRegValues, CheckRegValues
            void CreateRegValues(RegistryKey hKey, string subKeyPath)
            {
                using (RegistryKey key = hKey.CreateSubKey(subKeyPath, true))
                {
                    Assert.IsNotNull(key);

                    key.SetValue("None", new byte[0], RegistryValueKind.None);
                    key.SetValue("Binary", new byte[] { 0x01, 0x02, 0x03 }, RegistryValueKind.Binary);
                    key.SetValue("Integer", 1225, RegistryValueKind.DWord);
                    key.SetValue("String", "English", RegistryValueKind.String);

                    // .Net Framework's RegistryKey.SetValue do not allow arbitrary type, so call Win32 API directly.
                    RegistryHelper.RegSetValue(hKey, subKeyPath, "Strange10", new byte[0], 0x100000);
                    RegistryHelper.RegSetValue(hKey, subKeyPath, "Strange20", new byte[] { 0x01, 0x02, 0x03 }, 0x200000);

                    using (RegistryKey subKey = key.CreateSubKey("SubKey", true))
                    {
                        Assert.IsNotNull(subKey);

                        subKey.SetValue("Unicode", "한국어", RegistryValueKind.ExpandString);
                        subKey.SetValue("WinDir", "%WinDir%", RegistryValueKind.ExpandString);
                    }
                }
            }

            void CheckRegValues(RegistryKey hKey, string subKeyPath)
            {
                using (RegistryKey key = hKey.OpenSubKey(subKeyPath, false))
                {
                    Assert.IsNotNull(key);

                    byte[] bin = key.GetValue("None") as byte[];
                    Assert.IsNotNull(bin);
                    Assert.AreEqual(0, bin.Length);

                    bin = key.GetValue("Binary") as byte[];
                    Assert.IsNotNull(bin);
                    Assert.IsTrue(bin.SequenceEqual(new byte[] { 0x01, 0x02, 0x03 }));

                    int dword = (int)key.GetValue("Integer");
                    Assert.AreEqual(dword, 1225);

                    string str = key.GetValue("String") as string;
                    Assert.IsNotNull(str);
                    Assert.IsTrue(str.Equals("English", StringComparison.Ordinal));

                    // .Net Framework's RegistryKey.GetValue cannot handle arbitrary type. Call Win32 API directly.
                    bin = RegistryHelper.RegGetValue(hKey, subKeyPath, "Strange10", RegistryValueKind.Unknown) as byte[];
                    Assert.IsNotNull(bin);
                    Assert.AreEqual(0, bin.Length);

                    // .Net Framework's RegistryKey.GetValue cannot handle arbitrary type. Call Win32 API directly.
                    bin = RegistryHelper.RegGetValue(hKey, subKeyPath, "Strange20", RegistryValueKind.Unknown) as byte[];
                    Assert.IsNotNull(bin);
                    Assert.IsTrue(bin.SequenceEqual(new byte[] { 0x01, 0x02, 0x03 }));

                    using (RegistryKey subKey = key.OpenSubKey("SubKey", false))
                    {
                        Assert.IsNotNull(subKey);

                        str = subKey.GetValue("Unicode") as string;
                        Assert.IsNotNull(str);
                        Assert.IsTrue(str.Equals("한국어", StringComparison.Ordinal));

                        str = subKey.GetValue("WinDir", null, RegistryValueOptions.DoNotExpandEnvironmentNames) as string;
                        Assert.IsNotNull(str);
                        Assert.IsTrue(str.Equals("%WinDir%", StringComparison.Ordinal));
                    }
                }
            }

            #endregion

            #region Template
            void SingleTemplate(string rawCode, RegistryKey hKey,
                                string srcKeyPath,
                                string destKeyPath,
                                ErrorCheck check = ErrorCheck.Success)
            { // RegCopy,<SrcKey>,<SrcKeyPath>,<DestKey>,<DestKeyPath>,[WILDCARD]
                Registry.CurrentUser.DeleteSubKeyTree(RegCopyPath, false);
                try
                {
                    CreateRegValues(hKey, srcKeyPath);

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

                    if (check == ErrorCheck.Success || check == ErrorCheck.Warning)
                    {
                        CheckRegValues(hKey, destKeyPath);
                    }
                }
                finally
                {
                    Registry.CurrentUser.DeleteSubKeyTree(RegCopyPath, false);
                }
            }

            void WildcardTemplate(string rawCode, RegistryKey hKey,
                                  string srcKeyPath, string[] srcTargets,
                                  string destKeyPath, string[] destTargets,
                                  ErrorCheck check = ErrorCheck.Success)
            { // RegCopy,<SrcKey>,<SrcKeyPath>,<DestKey>,<DestKeyPath>,[WILDCARD]
                Registry.CurrentUser.DeleteSubKeyTree(RegCopyPath, false);

                try
                {
                    foreach (string target in srcTargets)
                    {
                        string t = Path.Combine(srcKeyPath, target);
                        CreateRegValues(hKey, t);
                    }

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

                    if (check == ErrorCheck.Success || check == ErrorCheck.Warning)
                    {
                        foreach (string target in destTargets)
                        {
                            string t = Path.Combine(destKeyPath, target);
                            CheckRegValues(hKey, t);
                        }
                    }
                }
                finally
                {
                    Registry.CurrentUser.DeleteSubKeyTree(RegCopyPath, false);
                }
            }

            #endregion
        }
예제 #20
0
        public void RegImport()
        {
            EngineState s = EngineTests.CreateEngineState();

            const string subKeyStr = RegImportPath;
            string       tempFile  = Path.GetTempFileName();

            Registry.CurrentUser.DeleteSubKeyTree(subKeyStr, false);
            try
            {
                StringBuilder b = new StringBuilder();
                b.AppendLine("Windows Registry Editor Version 5.00");
                b.AppendLine();
                b.AppendLine("[HKEY_CURRENT_USER\\Software\\PEBakery\\Tests\\RegImport]");
                b.AppendLine("\"String\"=\"Str\"");
                b.AppendLine("\"ExpandString\"=hex(2):25,00,57,00,69,00,6e,00,44,00,69,00,72,00,25,00,00,00");
                b.AppendLine("\"Binary\"=hex:a0,a1,a2");
                b.AppendLine("\"DWORD\"=dword:00000003");
                b.AppendLine("\"QWORD\"=hex(b):00,00,00,00,01,00,00,00");
                b.AppendLine();
                using (StreamWriter w = new StreamWriter(tempFile, false, Encoding.Unicode))
                {
                    w.Write(b.ToString());
                }

                EngineTests.Eval(s, $@"RegImport,{tempFile}", CodeType.RegImport, ErrorCheck.Success);

                using (RegistryKey subKey = Registry.CurrentUser.CreateSubKey(subKeyStr, false))
                {
                    Assert.AreEqual(RegistryValueKind.String, subKey.GetValueKind("String"));
                    string stringValue = subKey.GetValue("String", null) as string;
                    Assert.IsNotNull(stringValue);
                    Assert.IsTrue(stringValue.Equals("Str"));

                    Assert.AreEqual(RegistryValueKind.ExpandString, subKey.GetValueKind("ExpandString"));
                    stringValue = subKey.GetValue("ExpandString", null, RegistryValueOptions.DoNotExpandEnvironmentNames) as string;
                    Assert.IsNotNull(stringValue);
                    Assert.IsTrue(stringValue.Equals("%WinDir%"));

                    Assert.AreEqual(RegistryValueKind.Binary, subKey.GetValueKind("Binary"));
                    byte[] byteValue = subKey.GetValue("Binary", null) as byte[];
                    Assert.IsNotNull(byteValue);
                    Assert.IsTrue(byteValue.SequenceEqual(new byte[] { 0xA0, 0xA1, 0xA2 }));

                    Assert.AreEqual(RegistryValueKind.DWord, subKey.GetValueKind("DWORD"));
                    Assert.IsNotNull(subKey.GetValue("DWORD", null));
                    uint dword = (uint)(int)subKey.GetValue("DWORD", null);
                    Assert.AreEqual(0x03u, dword);

                    Assert.AreEqual(RegistryValueKind.QWord, subKey.GetValueKind("QWORD"));
                    Assert.IsNotNull(subKey.GetValue("QWORD", null));
                    ulong qword = (ulong)(long)subKey.GetValue("QWORD", null);
                    Assert.AreEqual(0x100000000u, qword);
                }
            }
            finally
            {
                Registry.CurrentUser.DeleteSubKeyTree(subKeyStr, false);
                if (File.Exists(tempFile))
                {
                    File.Delete(tempFile);
                }
            }
        }
예제 #21
0
        public void RegMulti()
        {
            EngineState s = EngineTests.CreateEngineState();

            void NormalTemplate(string rawCode, string[] compStrs, ErrorCheck check = ErrorCheck.Success)
            {
                using (RegistryKey subKey = Registry.CurrentUser.CreateSubKey(RegMultiPath, 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(RegMultiPath, 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));
                        }
                    }
                }
            }

            void IndexTemplate(string rawCode, int compIdx, string compStr, ErrorCheck check = ErrorCheck.Success)
            {
                using (RegistryKey subKey = Registry.CurrentUser.CreateSubKey(RegMultiPath, 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(RegMultiPath, 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));
                        }
                    }
                }
            }

            void ErrorTemplate(string rawCode, ErrorCheck check)
            {
                EngineTests.Eval(s, rawCode, CodeType.RegWrite, check);
            }

            string subKeyStr = RegMultiPath;

            Registry.CurrentUser.DeleteSubKeyTree(subKeyStr, false);
            try
            {
                // Append
                NormalTemplate($@"RegMulti,HKCU,{subKeyStr},Key,Append,C", new string[] { "A", "B", "C" });
                NormalTemplate($@"RegMulti,HKCU,{subKeyStr},Key,Append,B", new string[] { "A", "B" }, ErrorCheck.Warning);
                // Prepend
                NormalTemplate($@"RegMulti,HKCU,{subKeyStr},Key,Prepend,C", new string[] { "C", "A", "B" });
                NormalTemplate($@"RegMulti,HKCU,{subKeyStr},Key,Prepend,A", new string[] { "A", "B" }, ErrorCheck.Warning);
                // Before
                NormalTemplate($@"RegMulti,HKCU,{subKeyStr},Key,Before,A,C", new string[] { "C", "A", "B" });
                NormalTemplate($@"RegMulti,HKCU,{subKeyStr},Key,Before,B,C", new string[] { "A", "C", "B" });
                NormalTemplate($@"RegMulti,HKCU,{subKeyStr},Key,Before,D,C", new string[] { "A", "B" }, ErrorCheck.RuntimeError);
                NormalTemplate($@"RegMulti,HKCU,{subKeyStr},Key,Before,A,B", new string[] { "A", "B" }, ErrorCheck.Warning);
                // Behind
                NormalTemplate($@"RegMulti,HKCU,{subKeyStr},Key,Behind,A,C", new string[] { "A", "C", "B" });
                NormalTemplate($@"RegMulti,HKCU,{subKeyStr},Key,Behind,B,C", new string[] { "A", "B", "C" });
                NormalTemplate($@"RegMulti,HKCU,{subKeyStr},Key,Behind,D,C", new string[] { "A", "B" }, ErrorCheck.RuntimeError);
                NormalTemplate($@"RegMulti,HKCU,{subKeyStr},Key,Behind,A,B", new string[] { "A", "B" }, ErrorCheck.Warning);
                // Place
                NormalTemplate($@"RegMulti,HKCU,{subKeyStr},Key,Place,1,C", new string[] { "C", "A", "B" });
                NormalTemplate($@"RegMulti,HKCU,{subKeyStr},Key,Place,2,C", new string[] { "A", "C", "B" });
                NormalTemplate($@"RegMulti,HKCU,{subKeyStr},Key,Place,3,C", new string[] { "A", "B", "C" });
                NormalTemplate($@"RegMulti,HKCU,{subKeyStr},Key,Place,0,C", new string[] { "C", "A", "B" }, ErrorCheck.RuntimeError);
                NormalTemplate($@"RegMulti,HKCU,{subKeyStr},Key,Place,4,C", new string[] { "C", "A", "B" }, ErrorCheck.RuntimeError);
                NormalTemplate($@"RegMulti,HKCU,{subKeyStr},Key,Place,1,B", new string[] { "A", "B" }, ErrorCheck.Warning);
                // Delete
                NormalTemplate($@"RegMulti,HKCU,{subKeyStr},Key,Delete,A", new string[] { "B" });
                NormalTemplate($@"RegMulti,HKCU,{subKeyStr},Key,Delete,B", new string[] { "A" });
                NormalTemplate($@"RegMulti,HKCU,{subKeyStr},Key,Delete,C", new string[] { "A", "B" }, ErrorCheck.RuntimeError);
                // Index
                IndexTemplate($@"RegMulti,HKCU,{subKeyStr},Key,Index,A,%Dest%", 1, "A");
                IndexTemplate($@"RegMulti,HKCU,{subKeyStr},Key,Index,B,%Dest%", 2, "B");
                IndexTemplate($@"RegMulti,HKCU,{subKeyStr},Key,Index,C,%Dest%", 0, "C");
                IndexTemplate($@"RegMulti,HKCU,{subKeyStr},Key,Index,A", 1, "A", ErrorCheck.RuntimeError);
                // Error
                ErrorTemplate($@"RegMulti,HKCU,{subKeyStr},Key,Place,1,C,E", ErrorCheck.ParserError);
            }
            finally
            {
                Registry.CurrentUser.DeleteSubKeyTree(subKeyStr, false);
            }
        }
예제 #22
0
        public void RegDelete()
        {
            EngineState s = EngineTests.CreateEngineState();

            Registry.CurrentUser.DeleteSubKeyTree(RegDeletePath, false);
            try
            {
                void Template(string rawCode, RegistryKey hKey, string keyPath, string valueName, bool createDummy = true, ErrorCheck check = ErrorCheck.Success)
                { // RegDelete,<HKey>,<KeyPath>,[ValueName]
                    if (createDummy)
                    {
                        using (RegistryKey subKey = hKey.CreateSubKey(keyPath, true))
                        {
                            Assert.IsNotNull(subKey);

                            subKey.SetValue(valueName, 0, RegistryValueKind.DWord);
                        }
                    }

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

                    if (check == ErrorCheck.Success || check == ErrorCheck.Warning)
                    {
                        if (valueName == null)
                        {
                            using (RegistryKey subKey = hKey.OpenSubKey(keyPath, false))
                            {
                                Assert.IsNull(subKey);
                            }
                        }
                        else
                        {
                            using (RegistryKey subKey = hKey.OpenSubKey(keyPath, false))
                            {
                                if (createDummy)
                                {
                                    Assert.IsNotNull(subKey);

                                    object valueData = subKey.GetValue(valueName);
                                    Assert.IsNull(valueData);
                                }
                                else
                                {
                                    Assert.IsNull(subKey);
                                }
                            }
                        }
                    }
                }

                // Success
                Template($@"RegDelete,HKCU,{RegDeletePath},ValueName", Registry.CurrentUser, RegDeletePath, "ValueName");
                Template($@"RegDelete,HKCU,{RegDeletePath}", Registry.CurrentUser, RegDeletePath, null);

                // Warning
                Template($@"RegDelete,HKCU,{RegDeletePath},ValueName", Registry.CurrentUser, RegDeletePath, "ValueName", false, ErrorCheck.Warning);
                Template($@"RegDelete,HKCU,{RegDeletePath}", Registry.CurrentUser, RegDeletePath, null, false, ErrorCheck.Warning);
            }
            finally
            {
                Registry.CurrentUser.DeleteSubKeyTree(RegDeletePath, false);
            }
        }
예제 #23
0
        public void ShellExecute()
        {
            EngineState s       = EngineTests.CreateEngineState();
            string      pbBatch = Path.Combine("%TestBench%", "CommandSystem", "TestBatch.cmd");
            string      destDir = Path.GetRandomFileName();

            try
            {
                File.Delete(destDir);
                Directory.CreateDirectory(destDir);
                string srcBatch  = StringEscaper.Preprocess(s, pbBatch);
                string destBatch = Path.Combine(destDir, "TestBatch.cmd");

                void BaseTemplate(string rawCode, string exitKey, string compStr, bool enableCompat = false, ErrorCheck check = ErrorCheck.Success)
                {
                    s.Variables.DeleteKey(VarsType.Local, exitKey);
                    s.ReturnValue = string.Empty;

                    if (!exitKey.Equals("ExitCode", StringComparison.OrdinalIgnoreCase))
                    {
                        s.Variables[exitKey] = string.Empty;
                    }

                    if (enableCompat)
                    {
                        EngineTests.Eval(s, rawCode, CodeType.ShellExecute, check, new CompatOption {
                            DisableExtendedSectionParams = true
                        });
                    }
                    else
                    {
                        EngineTests.Eval(s, rawCode, CodeType.ShellExecute, check);
                    }

                    if (check == ErrorCheck.Success || check == ErrorCheck.Warning)
                    {
                        string dest = s.Variables[exitKey];
                        Assert.IsTrue(dest.Equals(compStr, StringComparison.Ordinal));

                        if (!enableCompat)
                        {
                            Assert.IsTrue(s.ReturnValue.Equals(compStr, StringComparison.Ordinal));
                        }
                    }

                    s.Variables.DeleteKey(VarsType.Local, "ExitCode");
                    if (!exitKey.Equals("ExitCode", StringComparison.OrdinalIgnoreCase))
                    {
                        s.Variables.DeleteKey(VarsType.Local, exitKey);
                    }
                }

                void DeleteTemplate(string rawCode, string exitKey, string compStr, ErrorCheck check = ErrorCheck.Success)
                {
                    File.Copy(srcBatch, destBatch, true);

                    s.Variables.DeleteKey(VarsType.Local, exitKey);
                    s.ReturnValue = string.Empty;

                    if (!exitKey.Equals("ExitCode", StringComparison.OrdinalIgnoreCase))
                    {
                        s.Variables[exitKey] = string.Empty;
                    }

                    EngineTests.Eval(s, rawCode, CodeType.ShellExecuteDelete, check);
                    if (check == ErrorCheck.Success || check == ErrorCheck.Warning)
                    {
                        string dest = s.Variables[exitKey];
                        Assert.IsTrue(dest.Equals(compStr, StringComparison.Ordinal));
                        Assert.IsFalse(File.Exists(destBatch));
                    }

                    s.Variables.DeleteKey(VarsType.Local, "ExitCode");
                    if (!exitKey.Equals("ExitCode", StringComparison.OrdinalIgnoreCase))
                    {
                        s.Variables.DeleteKey(VarsType.Local, exitKey);
                    }
                }

                BaseTemplate($@"ShellExecute,Open,{pbBatch},78", "ExitCode", "78");
                BaseTemplate($@"ShellExecute,Open,{pbBatch},78", "ExitCode", "78", true);
                BaseTemplate($@"ShellExecute,Open,{pbBatch},3,,%Dest%", "Dest", "3");
                BaseTemplate($@"ShellExecute,Open,{pbBatch},3,,%Dest%", "ExitCode", "3", false, ErrorCheck.Warning);
                DeleteTemplate($@"ShellExecuteDelete,Open,{destBatch},78", "ExitCode", "78");
                DeleteTemplate($@"ShellExecuteDelete,Open,{destBatch},3,,%Dest%", "Dest", "3");
                DeleteTemplate($@"ShellExecuteDelete,Open,{destBatch},3,,%Dest%", "ExitCode", "3", ErrorCheck.Warning);
            }
            finally
            {
                if (Directory.Exists(destDir))
                {
                    Directory.Delete(destDir, true);
                }
            }
        }
예제 #24
0
        public void FileCopy()
        {
            EngineState s = EngineTests.CreateEngineState();

            string pbDirPath = Path.Combine("%TestBench%", "CommandFile");
            string pbSrcDir  = Path.Combine(pbDirPath, SrcDirFile);
            string destDir   = FileHelper.GetTempDir();

            void SingleTemplate(string rawCode, string srcFileName, string destFileName,
                                ErrorCheck check = ErrorCheck.Success, bool preserve = false, bool ignoreCompare = false)
            {
                if (destFileName == null)
                {
                    destFileName = srcFileName;
                }

                string dirPath = StringEscaper.Preprocess(s, Path.Combine("%TestBench%", "CommandFile"));
                string srcDir  = Path.Combine(dirPath, SrcDirFile);

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

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

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

                    if (check == ErrorCheck.Success && !ignoreCompare)
                    {
                        Assert.IsTrue(File.Exists(destFullPath));

                        using (FileStream srcStream = new FileStream(srcFullPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                            using (FileStream destStream = new FileStream(destFullPath, 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);
                    }
                }
            }

            void MultiTemplate(string rawCode, string srcFileWildCard, bool recursive, ErrorCheck check = ErrorCheck.Success)
            {
                string dirPath = StringEscaper.Preprocess(s, Path.Combine("%TestBench%", "CommandFile"));
                string srcDir  = Path.Combine(dirPath, SrcDirFile);

                if (Directory.Exists(destDir))
                {
                    Directory.Delete(destDir, true);
                }
                Directory.CreateDirectory(destDir);
                try
                {
                    EngineTests.Eval(s, rawCode, CodeType.FileCopy, check);

                    if (check == ErrorCheck.Success)
                    {
                        string[] srcFiles;
                        string[] destFiles;
                        if (recursive)
                        {
                            srcFiles  = Directory.GetFiles(srcDir, srcFileWildCard, SearchOption.AllDirectories);
                            destFiles = Directory.GetFiles(destDir, srcFileWildCard, SearchOption.AllDirectories);
                        }
                        else
                        {
                            srcFiles  = Directory.GetFiles(srcDir, srcFileWildCard);
                            destFiles = Directory.GetFiles(destDir, srcFileWildCard);
                        }

                        Assert.IsTrue(srcFiles.Length == destFiles.Length);

                        for (int i = 0; i < srcFiles.Length; i++)
                        {
                            using (FileStream srcStream = new FileStream(srcFiles[i], FileMode.Open, FileAccess.Read, FileShare.Read))
                                using (FileStream destStream = new FileStream(destFiles[i], 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);
                    }
                }
            }

            SingleTemplate($@"FileCopy,{pbSrcDir}\A.txt,{destDir}", "A.txt", null);
            SingleTemplate($@"FileCopy,{pbSrcDir}\A.txt,{destDir}\B.txt", "A.txt", "B.txt");
            SingleTemplate($@"FileCopy,{pbSrcDir}\Z\Y.ini,{destDir}", Path.Combine("Z", "Y.ini"), "Y.ini");
            MultiTemplate($@"FileCopy,{pbSrcDir}\*.txt,{destDir}", "*.txt", true);
            MultiTemplate($@"FileCopy,{pbSrcDir}\*.ini,{destDir},NOREC", "*.ini", false);
            // Check https://github.com/pebakery/pebakery/issues/150
            MultiTemplate($@"FileCopy,{pbSrcDir}\\*.txt,{destDir}", "*.txt", true);
            MultiTemplate($@"FileCopy,{pbSrcDir}\\*.ini,{destDir},NOREC", "*.ini", false);

            SingleTemplate($@"FileCopy,{pbSrcDir}\P.txt,{destDir}", "P.txt", null, ErrorCheck.RuntimeError);
            SingleTemplate($@"FileCopy,{pbSrcDir}\C.txt,{destDir}", "C.txt", null, ErrorCheck.Overwrite, true);
            SingleTemplate($@"FileCopy,{pbSrcDir}\C.txt,{destDir},NOWARN", "C.txt", null, ErrorCheck.Success, true);
            SingleTemplate($@"FileCopy,{pbSrcDir}\C.txt,{destDir},PRESERVE", "C.txt", null, ErrorCheck.Overwrite, true);
            SingleTemplate($@"FileCopy,{pbSrcDir}\C.txt,{destDir},PRESERVE,NOWARN", "C.txt", null, ErrorCheck.Success, true, true);

            SingleTemplate($@"FileCopy,{pbSrcDir}\A.txt,{destDir}\NonExistDir\", "A.txt", null, ErrorCheck.Warning, false, false);
        }
예제 #25
0
        public void Expand()
        {
            EngineState s       = EngineTests.CreateEngineState();
            string      destDir = FileHelper.GetTempDir();

            void FileTemplate(string compFile, 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(destDir, compFile);

                Debug.Assert(destPath != null);

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

                    EngineTests.Eval(s, rawCode, CodeType.Expand, check);
                    if (check == ErrorCheck.Success || check == ErrorCheck.Warning)
                    {
                        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.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);
                    }
                }
            }

            void DirTemplate(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");

                try
                {
                    if (testPreserve) // Check preserve
                    {
                        File.Create(destDir).Close();
                    }

                    EngineTests.Eval(s, rawCode, CodeType.Expand, check);
                    if (check == ErrorCheck.Success || check == ErrorCheck.Warning)
                    {
                        string[] srcFiles  = Directory.GetFiles(srcPath, "*", SearchOption.AllDirectories);
                        string[] destFiles = Directory.GetFiles(destDir, "*", SearchOption.AllDirectories);
                        Assert.IsTrue(srcFiles.Length == destFiles.Length);

                        if (!testPreserve && Directory.Exists(destDir) || testPreserve && checkIfPreserve)
                        {
                            for (int i = 0; i < srcFiles.Length; i++)
                            {
                                using (FileStream srcStream = new FileStream(srcFiles[i], FileMode.Open, FileAccess.Read, FileShare.Read))
                                    using (FileStream destStream = new FileStream(destFiles[i], 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);
                    }
                }
            }

            string testCode = $"Expand,\"%TestBench%\\CommandArchive\\ex1.cab\",\"{destDir}\"";

            FileTemplate("ex1.jpg", testCode, ErrorCheck.Success);

            testCode = $"Expand,\"%TestBench%\\CommandArchive\\ex2.cab\",\"{destDir}\",ex2.jpg";
            FileTemplate("ex2.jpg", testCode, ErrorCheck.Success);

            testCode = $"Expand,\"%TestBench%\\CommandArchive\\ex2.cab\",\"{destDir}\",ex1.jpg";
            FileTemplate("ex1.jpg", testCode, ErrorCheck.RuntimeError);

            testCode = $"Expand,\"%TestBench%\\CommandArchive\\ex3.jp_\",\"{destDir}\",ex3.jpg,PRESERVE";
            FileTemplate("ex3.jpg", testCode, ErrorCheck.Success, false, true);

            testCode = $"Expand,\"%TestBench%\\CommandArchive\\ex3.jp_\",\"{destDir}\",ex3.jpg,PRESERVE";
            FileTemplate("ex3.jpg", testCode, ErrorCheck.Overwrite, true, false);

            testCode = $"Expand,\"%TestBench%\\CommandArchive\\ex3.jp_\",\"{destDir}\",ex3.jpg,PRESERVE,NOWARN";
            FileTemplate("ex3.jpg", testCode, ErrorCheck.Success, true, false);

            testCode = $"Expand,\"%TestBench%\\CommandArchive\\ex4.cab\",\"{destDir}\"";
            DirTemplate(testCode, ErrorCheck.Success);

            testCode = $"Expand,\"%TestBench%\\CommandArchive\\ex4.cab\",\"{destDir}\",ex3.jpg";
            FileTemplate("ex3.jpg", testCode, ErrorCheck.Success);

            testCode = $"Expand,\"%TestBench%\\CommandArchive\\ex4.cab\",\"{destDir}\",ex2.jpg,NOWARN";
            FileTemplate("ex2.jpg", testCode, ErrorCheck.Success, true);

            testCode = $"Expand,\"%TestBench%\\CommandArchive\\ex4.cab\",\"{destDir}\",ex2.jpg,NOWARN";
            FileTemplate("ex2.jpg", testCode, ErrorCheck.Success, false);
        }
예제 #26
0
        public void TXTDelLine()
        {
            EngineState s = EngineTests.CreateEngineState();

            string tempDir   = FileHelper.GetTempDir();
            string sampleStr = GenerateSampleText(true, true, true, true);

            try
            {
                string tempFile  = Path.Combine(tempDir, "TXTReplace.txt");
                string tempFile2 = Path.Combine(tempDir, "TXTReplace2.txt");

                // Test empty string
                // Strange, but WB082 works like this
                SingleTemplate(s, CodeType.TXTDelLine, $@"TXTDelLine,{tempFile},Z", tempFile, string.Empty, string.Empty);

                // Test normal text
                SingleTemplate(s, CodeType.TXTDelLine, $@"TXTDelLine,{tempFile},AB", tempFile, sampleStr, GenerateSampleText(false, true, true, true));
                SingleTemplate(s, CodeType.TXTDelLine, $@"TXTDelLine,{tempFile},ab", tempFile, sampleStr, GenerateSampleText(true, true, true, true));
                SingleTemplate(s, CodeType.TXTDelLine, $@"TXTDelLine,{tempFile},D", tempFile, sampleStr, GenerateSampleText(true, false, true, true));
                SingleTemplate(s, CodeType.TXTDelLine, $@"TXTDelLine,{tempFile},d", tempFile, sampleStr, GenerateSampleText(true, true, true, true));
                SingleTemplate(s, CodeType.TXTDelLine, $@"TXTDelLine,{tempFile},Z", tempFile, sampleStr, GenerateSampleText(true, true, true, true));
                SingleTemplate(s, CodeType.TXTDelLine, $@"TXTDelLine,{tempFile},z", tempFile, sampleStr, GenerateSampleText(true, true, true, true));

                // Test unicode text
                SingleTemplate(s, CodeType.TXTDelLine, $@"TXTDelLine,{tempFile},가", tempFile, sampleStr, GenerateSampleText(true, true, true, false));

                // Optimization
                OptTemplate(s, CodeType.TXTDelLineOp, new List <string>
                {
                    $@"TXTDelLine,{tempFile},AB",
                    $@"TXTDelLine,{tempFile},XY",
                }, tempFile, sampleStr, GenerateSampleText(false, true, false, true));
                OptTemplate(s, null, new List <string>
                {
                    $@"TXTDelLine,{tempFile},AB",
                    $@"TXTDelLine,{tempFile2},XY",
                }, tempFile, sampleStr, GenerateSampleText(false, true, true, true));
            }
            finally
            {
                Directory.Delete(tempDir, true);
            }

            string GenerateSampleText(bool t1, bool t2, bool t3, bool t4)
            {
                StringBuilder b = new StringBuilder();

                if (t1)
                {
                    b.AppendLine("ABC");
                }
                if (t2)
                {
                    b.AppendLine("DEF");
                }
                if (t3)
                {
                    b.AppendLine("XYZ");
                }
                if (t4)
                {
                    b.AppendLine("가나다");
                }
                return(b.ToString());
            }
        }
예제 #27
0
        public void Branch_IfEqual()
        {
            EngineState         s = EngineTests.CreateEngineState();
            BranchCondition     cond;
            BranchConditionType type = BranchConditionType.Equal;

            // Equal
            cond = new BranchCondition(type, false, "A", "A");
            Assert.IsTrue(cond.Check(s, out string d));
            cond = new BranchCondition(type, false, "A", "B");
            Assert.IsFalse(cond.Check(s, out d));
            cond = new BranchCondition(type, false, "a", "A");
            Assert.IsTrue(cond.Check(s, out d));

            cond = new BranchCondition(type, true, "A", "A");
            Assert.IsFalse(cond.Check(s, out d));
            cond = new BranchCondition(type, true, "A", "B");
            Assert.IsTrue(cond.Check(s, out d));

            cond = new BranchCondition(type, false, "11.1", "11.1.0");
            Assert.IsFalse(cond.Check(s, out d));
            cond = new BranchCondition(type, true, "11.1", "11.1.0");
            Assert.IsTrue(cond.Check(s, out d));
            cond = new BranchCondition(type, false, "10.9", "11.1");
            Assert.IsFalse(cond.Check(s, out d));
            cond = new BranchCondition(type, false, "12", "12");
            Assert.IsTrue(cond.Check(s, out d));
            cond = new BranchCondition(type, false, "11.1.2.9", "11.1.2.3");
            Assert.IsFalse(cond.Check(s, out d));

            cond = new BranchCondition(type, false, "5", "5.0");
            Assert.IsTrue(cond.Check(s, out d));
            cond = new BranchCondition(type, false, "5", "5.1.2600");
            Assert.IsFalse(cond.Check(s, out d));

            // WB082 does not recognize hex integer representation
            // PEBakery support hex integer representation
            cond = new BranchCondition(type, false, "11", "0xC");
            Assert.IsFalse(cond.Check(s, out d));
            cond = new BranchCondition(type, false, "12", "0xC");
            Assert.IsTrue(cond.Check(s, out d));
            cond = new BranchCondition(type, false, "13", "0xC");
            Assert.IsFalse(cond.Check(s, out d));

            BranchCondition_Comparison_Template(s, "A", "If,%Src%,Equal,A,Set,%Dest%,T", "T");
            BranchCondition_Comparison_Template(s, "A", "If,%Src%,Equal,a,Set,%Dest%,T", "T");
            BranchCondition_Comparison_Template(s, "10", "If,%Src%,Equal,09,Set,%Dest%,T", "F");
            BranchCondition_Comparison_Template(s, "10", "If,%Src%,Equal,10,Set,%Dest%,T", "T");
            BranchCondition_Comparison_Template(s, "10", "If,%Src%,Equal,11,Set,%Dest%,T", "F");

            BranchCondition_Comparison_Template(s, "A", "If,%Src%,==,A,Set,%Dest%,T", "T");
            BranchCondition_Comparison_Template(s, "A", "If,%Src%,==,a,Set,%Dest%,T", "T");
            BranchCondition_Comparison_Template(s, "10", "If,%Src%,==,09,Set,%Dest%,T", "F");
            BranchCondition_Comparison_Template(s, "10", "If,%Src%,==,10,Set,%Dest%,T", "T");
            BranchCondition_Comparison_Template(s, "10", "If,%Src%,==,11,Set,%Dest%,T", "F");

            BranchCondition_Comparison_Template(s, "A", "If,Not,%Src%,Equal,A,Set,%Dest%,T", "F");
            BranchCondition_Comparison_Template(s, "A", "If,%Src%,NotEqual,a,Set,%Dest%,T", "F");
            BranchCondition_Comparison_Template(s, "10", "If,%Src%,NotEqual,09,Set,%Dest%,T", "T");
            BranchCondition_Comparison_Template(s, "10", "If,Not,%Src%,Equal,10,Set,%Dest%,T", "F");
            BranchCondition_Comparison_Template(s, "10", "If,Not,%Src%,Equal,11,Set,%Dest%,T", "T");

            BranchCondition_Comparison_Template(s, "A", "If,%Src%,!=,A,Set,%Dest%,T", "F");
            BranchCondition_Comparison_Template(s, "A", "If,%Src%,!=,a,Set,%Dest%,T", "F");
            BranchCondition_Comparison_Template(s, "10", "If,%Src%,!=,09,Set,%Dest%,T", "T");
            BranchCondition_Comparison_Template(s, "10", "If,%Src%,!=,10,Set,%Dest%,T", "F");
            BranchCondition_Comparison_Template(s, "10", "If,%Src%,!=,11,Set,%Dest%,T", "T");
        }
예제 #28
0
        public void PathMove()
        {
            EngineState s       = EngineTests.CreateEngineState();
            string      destDir = FileHelper.GetTempDir();

            void FileTemplate(string rawCode, string srcFileName, string destFileName, ErrorCheck check = ErrorCheck.Success)
            {
                string dirPath      = StringEscaper.Preprocess(s, Path.Combine("%TestBench%", "CommandFile"));
                string srcDir       = Path.Combine(dirPath, SrcDirFile);
                string srcFullPath  = Path.Combine(destDir, srcFileName);
                string destFullPath = Path.Combine(destDir, destFileName);

                if (Directory.Exists(destDir))
                {
                    Directory.Delete(destDir, true);
                }
                FileHelper.DirCopy(srcDir, destDir, new DirCopyOptions
                {
                    CopySubDirs = true,
                    Overwrite   = true,
                });
                try
                {
                    EngineTests.Eval(s, rawCode, CodeType.PathMove, check);

                    if (check == ErrorCheck.Success)
                    {
                        Assert.IsFalse(File.Exists(srcFullPath));
                        Assert.IsTrue(File.Exists(destFullPath));
                    }
                }
                finally
                {
                    if (Directory.Exists(destDir))
                    {
                        Directory.Delete(destDir, true);
                    }
                }
            }

            void DirTemplate(string rawCode, string srcFileName, string destFileName, ErrorCheck check = ErrorCheck.Success)
            {
                string dirPath      = StringEscaper.Preprocess(s, Path.Combine("%TestBench%", "CommandFile"));
                string srcDir       = Path.Combine(dirPath, SrcDirDir);
                string srcFullPath  = Path.Combine(destDir, srcFileName);
                string destFullPath = Path.Combine(destDir, destFileName);

                if (Directory.Exists(destDir))
                {
                    Directory.Delete(destDir, true);
                }
                FileHelper.DirCopy(srcDir, destDir, new DirCopyOptions
                {
                    CopySubDirs = true,
                    Overwrite   = true,
                });
                try
                {
                    EngineTests.Eval(s, rawCode, CodeType.PathMove, check);

                    if (check == ErrorCheck.Success)
                    {
                        Assert.IsFalse(Directory.Exists(srcFullPath));
                        Assert.IsTrue(Directory.Exists(destFullPath));
                    }
                }
                finally
                {
                    if (Directory.Exists(destDir))
                    {
                        Directory.Delete(destDir, true);
                    }
                }
            }

            FileTemplate($@"PathMove,{destDir}\A.txt,{destDir}\R.txt", "A.txt", "R.txt");
            DirTemplate($@"PathMove,{destDir}\ABCD,{destDir}\XYZ", "ABCD", "XYZ");
        }
예제 #29
0
        public void Compress()
        {
            void DirTemplate(string arcType, string srcDirName, ArchiveFile.CompressLevel?level)
            { // Compress,<ArchiveType>,<SrcPath>,<DestArchive>,[CompressLevel]
                EngineState s           = EngineTests.CreateEngineState();
                string      srcDir      = StringEscaper.Preprocess(s, Path.Combine("%TestBench%", "CommandArchive"));
                string      srcFullPath = Path.Combine(srcDir, srcDirName);
                string      destDir     = FileHelper.GetTempDir();
                string      destArchive = Path.Combine(destDir, $"{srcDirName}.{arcType}");
                string      decompDir   = FileHelper.GetTempDir();

                try
                {
                    Directory.CreateDirectory(destDir);
                    Directory.CreateDirectory(decompDir);

                    string rawCode = $@"Compress,{arcType},""%TestBench%\CommandArchive\{srcDirName}"",""{destArchive}""";
                    switch (level)
                    {
                    case ArchiveFile.CompressLevel.Best:
                        rawCode += ",BEST";
                        break;

                    case ArchiveFile.CompressLevel.Fastest:
                        rawCode += ",FASTEST";
                        break;

                    case ArchiveFile.CompressLevel.Normal:
                        rawCode += ",NORMAL";
                        break;

                    case ArchiveFile.CompressLevel.Store:
                        rawCode += ",STORE";
                        break;
                    }
                    EngineTests.Eval(s, rawCode, CodeType.Compress, ErrorCheck.Success);
                    TestSetup.ExtractWith7Z(srcDir, destArchive, decompDir);

                    string[] srcFiles  = Directory.GetFiles(srcFullPath, "*", SearchOption.AllDirectories);
                    string[] destFiles = Directory.GetFiles(decompDir, "*", SearchOption.AllDirectories);
                    Assert.IsTrue(srcFiles.Length == destFiles.Length);

                    for (int i = 0; i < srcFiles.Length; i++)
                    {
                        using (FileStream srcStream = new FileStream(srcFiles[i], FileMode.Open, FileAccess.Read, FileShare.Read))
                            using (FileStream destStream = new FileStream(destFiles[i], 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);
                    }
                    if (Directory.Exists(decompDir))
                    {
                        Directory.Delete(decompDir, true);
                    }
                }
            }

            void FileTemplate(string arcType, string srcFilePath, ArchiveFile.CompressLevel level)
            { // Compress,<ArchiveType>,<SrcPath>,<DestArchive>,[CompressLevel]
                EngineState s           = EngineTests.CreateEngineState();
                string      srcDir      = StringEscaper.Preprocess(s, Path.Combine("%TestBench%", "CommandArchive"));
                string      srcFullPath = Path.Combine(srcDir, srcFilePath);
                string      srcFileName = Path.GetFileName(srcFilePath);
                string      destDir     = FileHelper.GetTempDir();
                string      destArchive = Path.Combine(destDir, $"{srcFileName}.{arcType}");
                string      decompDir   = FileHelper.GetTempDir();

                try
                {
                    Directory.CreateDirectory(destDir);
                    Directory.CreateDirectory(decompDir);

                    string rawCode = $@"Compress,{arcType},""%TestBench%\CommandArchive\{srcFilePath}"",""{destArchive}""";
                    switch (level)
                    {
                    case ArchiveFile.CompressLevel.Best:
                        rawCode += ",BEST";
                        break;

                    case ArchiveFile.CompressLevel.Fastest:
                        rawCode += ",FASTEST";
                        break;

                    case ArchiveFile.CompressLevel.Normal:
                        rawCode += ",NORMAL";
                        break;

                    case ArchiveFile.CompressLevel.Store:
                        rawCode += ",STORE";
                        break;
                    }
                    EngineTests.Eval(s, rawCode, CodeType.Compress, ErrorCheck.Success);
                    TestSetup.ExtractWith7Z(srcDir, destArchive, decompDir);

                    using (FileStream srcStream = new FileStream(srcFullPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                        using (FileStream destStream = new FileStream(Path.Combine(decompDir, srcFileName), 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
                {
                    Directory.Delete(destDir, true);
                    Directory.Delete(decompDir, true);
                }
            }

            void AppendTemplate(string arcType, string srcFilePath, string appendFilePath, ArchiveFile.CompressLevel?level)
            { // Compress,<ArchiveType>,<SrcPath>,<DestArchive>,[CompressLevel]
                EngineState s              = EngineTests.CreateEngineState();
                string      srcDir         = StringEscaper.Preprocess(s, Path.Combine("%TestBench%", "CommandArchive"));
                string      srcFileName    = Path.GetFileName(srcFilePath);
                string      appendFullPath = Path.Combine(srcDir, appendFilePath);
                string      appendFileName = Path.GetFileName(appendFilePath);
                string      destDir        = FileHelper.GetTempDir();
                string      destArchive    = Path.Combine(destDir, $"{srcFileName}.{arcType}");
                string      decompDir      = FileHelper.GetTempDir();

                try
                {
                    // Create sample archive file
                    string rawCode = $@"Compress,{arcType},""%TestBench%\CommandArchive\{srcFilePath}"",""{destArchive}""";
                    EngineTests.Eval(s, rawCode, CodeType.Compress, ErrorCheck.Success);

                    // Append file to sample archive file
                    rawCode = $@"Compress,{arcType},""%TestBench%\CommandArchive\{appendFilePath}"",""{destArchive}""";
                    switch (level)
                    {
                    case ArchiveFile.CompressLevel.Best:
                        rawCode += ",BEST";
                        break;

                    case ArchiveFile.CompressLevel.Fastest:
                        rawCode += ",FASTEST";
                        break;

                    case ArchiveFile.CompressLevel.Normal:
                        rawCode += ",NORMAL";
                        break;

                    case ArchiveFile.CompressLevel.Store:
                        rawCode += ",STORE";
                        break;
                    }
                    EngineTests.Eval(s, rawCode, CodeType.Compress, ErrorCheck.Success);
                    TestSetup.ExtractWith7Z(srcDir, destArchive, decompDir);

                    using (FileStream srcStream = new FileStream(appendFullPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                        using (FileStream destStream = new FileStream(Path.Combine(decompDir, appendFileName), 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
                {
                    Directory.Delete(destDir, true);
                    Directory.Delete(decompDir, true);
                }
            }

            void WildcardTemplate(string arcType, string wildcard, ArchiveFile.CompressLevel level)
            { // Compress,<ArchiveType>,<SrcPath>,<DestArchive>,[CompressLevel]
                EngineState s           = EngineTests.CreateEngineState();
                string      srcDir      = StringEscaper.Preprocess(s, Path.Combine("%TestBench%", "CommandFile", "SrcDir"));
                string      destDir     = FileHelper.GetTempDir();
                string      destArchive = Path.Combine(destDir, $"Wildcard.{arcType}");
                string      decompDir   = FileHelper.GetTempDir();

                try
                {
                    Directory.CreateDirectory(destDir);
                    Directory.CreateDirectory(decompDir);

                    string rawCode = $@"Compress,{arcType},""%TestBench%\CommandFile\SrcDir\{wildcard}"",""{destArchive}""";
                    switch (level)
                    {
                    case ArchiveFile.CompressLevel.Best:
                        rawCode += ",BEST";
                        break;

                    case ArchiveFile.CompressLevel.Fastest:
                        rawCode += ",FASTEST";
                        break;

                    case ArchiveFile.CompressLevel.Normal:
                        rawCode += ",NORMAL";
                        break;

                    case ArchiveFile.CompressLevel.Store:
                        rawCode += ",STORE";
                        break;
                    }
                    EngineTests.Eval(s, rawCode, CodeType.Compress, ErrorCheck.Success);
                    string exeDir = StringEscaper.Preprocess(s, Path.Combine("%TestBench%", "CommandArchive"));
                    TestSetup.ExtractWith7Z(exeDir, destArchive, decompDir);

                    string[] srcFiles  = Directory.GetFiles(srcDir, wildcard, SearchOption.AllDirectories);
                    string[] destFiles = Directory.GetFiles(decompDir, wildcard, SearchOption.AllDirectories);
                    Assert.IsTrue(srcFiles.Length == destFiles.Length);

                    for (int i = 0; i < srcFiles.Length; i++)
                    {
                        using (FileStream srcStream = new FileStream(srcFiles[i], FileMode.Open, FileAccess.Read, FileShare.Read))
                            using (FileStream destStream = new FileStream(destFiles[i], 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);
                    }
                    if (Directory.Exists(decompDir))
                    {
                        Directory.Delete(decompDir, true);
                    }
                }
            }

            // Create archives
            DirTemplate("Zip", "France", ArchiveFile.CompressLevel.Store);
            DirTemplate("7z", "Korea", ArchiveFile.CompressLevel.Normal);
            DirTemplate("Zip", "Korea", ArchiveFile.CompressLevel.Best);
            FileTemplate("Zip", Path.Combine("Korean_IME_Logo", "Korean_IME_Logo.jpg"), ArchiveFile.CompressLevel.Normal);
            FileTemplate("7z", Path.Combine("Korean_IME_Logo", "Korean_IME_Logo.jpg"), ArchiveFile.CompressLevel.Best);

            // Append to archives
            AppendTemplate("Zip", Path.Combine("Korea", "대한민국.png"), Path.Combine("Korea", "대한민국.txt"), ArchiveFile.CompressLevel.Best);
            AppendTemplate("7z", Path.Combine("Korea", "대한민국.png"), Path.Combine("Korea", "대한민국.txt"), ArchiveFile.CompressLevel.Store);

            // Wildcard tests
            WildcardTemplate("Zip", "*.txt", ArchiveFile.CompressLevel.Normal);
        }
예제 #30
0
        public void TXTAddLine()
        {
            EngineState s = EngineTests.CreateEngineState();

            string tempDir = FileHelper.GetTempDir();

            try
            {
                string tempFile  = Path.Combine(tempDir, "Sample.txt");
                string tempFile2 = Path.Combine(tempDir, "Sample2.txt");

                // Test empty string
                SingleTemplate(s, CodeType.TXTAddLine, $@"TXTAddLine,{tempFile},C,Append", tempFile, string.Empty, "C\r\n");
                SingleTemplate(s, CodeType.TXTAddLine, $@"TXTAddLine,{tempFile},C,Prepend", tempFile, string.Empty, "C\r\n");

                // Test normal text
                SingleTemplate(s, CodeType.TXTAddLine, $@"TXTAddLine,{tempFile},C,Append", tempFile, "A\r\nB", "A\r\nB\r\nC\r\n");
                SingleTemplate(s, CodeType.TXTAddLine, $@"TXTAddLine,{tempFile},C,Prepend", tempFile, "A\r\nB", "C\r\nA\r\nB\r\n");

                // Test unicode text
                SingleTemplate(s, CodeType.TXTAddLine, $@"TXTAddLine,{tempFile},나,Append", tempFile, "가\r\n", "가\r\n나\r\n");

                // Test error
                SingleTemplate(s, CodeType.TXTAddLine, $@"TXTAddLine,{tempFile},C,ErrorMode", tempFile, string.Empty, string.Empty, ErrorCheck.ParserError);

                // Optimization
                OptTemplate(s, CodeType.TXTAddLineOp, new List <string>
                {
                    $@"TXTAddLine,{tempFile},X,Append",
                    $@"TXTAddLine,{tempFile},Y,Append",
                    $@"TXTAddLine,{tempFile},Z,Append",
                }, tempFile, string.Empty, "X\r\nY\r\nZ\r\n");
                OptTemplate(s, CodeType.TXTAddLineOp, new List <string>
                {
                    $@"TXTAddLine,{tempFile},X,Append",
                    $@"TXTAddLine,{tempFile},Y,Append",
                    $@"TXTAddLine,{tempFile},Z,Append",
                }, tempFile, "A\r\nB", "A\r\nB\r\nX\r\nY\r\nZ\r\n");
                OptTemplate(s, CodeType.TXTAddLineOp, new List <string>
                {
                    $@"TXTAddLine,{tempFile},X,Prepend",
                    $@"TXTAddLine,{tempFile},Y,Prepend",
                    $@"TXTAddLine,{tempFile},Z,Prepend",
                }, tempFile, "A\r\nB", "Z\r\nY\r\nX\r\nA\r\nB");
                OptTemplate(s, null, new List <string>
                {
                    $@"TXTAddLine,{tempFile},X,Append",
                    $@"TXTAddLine,{tempFile},Y,Append",
                    $@"TXTAddLine,{tempFile2},Z,Append",
                }, tempFile, "A\r\nB", "A\r\nB\r\nX\r\nY\r\n");
                OptTemplate(s, null, new List <string>
                {
                    $@"TXTAddLine,{tempFile},X,Prepend",
                    $@"TXTAddLine,{tempFile},Y,Prepend",
                    $@"TXTAddLine,{tempFile},Y,Append",
                    $@"TXTAddLine,{tempFile},Z,Prepend",
                }, tempFile, "A\r\nB", "Z\r\nY\r\nX\r\nA\r\nB\r\nY\r\n");
            }
            finally
            {
                Directory.Delete(tempDir, true);
            }
        }