Пример #1
0
 public async Task EscapingUnderstanding()
 {
     {
         string arg = new string(Array.Empty <char>());
         var    a   = await new System.Diagnostics.ProcessStartInfo("node", $"-e \"console.log({arg});\"").WaitForExitAndReadOutputAsync();
         Assert(a.StandardOutput == "\n");
         Assert(ProcessExtensions.ExecuteJS_Builder(NO_IMPORTS, "", new object[] { a.StandardOutput }).Contains(arg));
     }
     {
         string arg = new string(new char[] { '\'', '\'' });
         var    b   = await new System.Diagnostics.ProcessStartInfo("node", $"-e \"console.log({arg});\"").WaitForExitAndReadOutputAsync();
         Assert(b.StandardOutput == "\n");
         // Assert(ProcessExtensions.ExecuteJS_Builder(new string[0], "", new object[] { b.StandardOutput }).Contains(arg));
     }
     {
         string arg = new string(new char[] { '"', '"', '"', '"' });
         var    b2  = await new System.Diagnostics.ProcessStartInfo("node", $"-e \"console.log({arg});\"").WaitForExitAndReadOutputAsync();
         Assert(b2.StandardOutput == "\n");
         // Assert(ProcessExtensions.ExecuteJS_Builder(new string[0], "", new object[] { b2.StandardOutput }).Contains(arg));
     }
     {
         string arg = new string(new char[] { '"', 'a', '"' });
         var    c   = await new System.Diagnostics.ProcessStartInfo("node", $"-e \"console.log({arg});\"").WaitForExitAndReadOutputAsync();
         Assert(c.ErrorOutput.Contains("ReferenceError: a is not defined"));
     }
     {
         // literal JS:
         // console.log("a");
         string arg = new string(new char[] { '\\', '"', 'a', '\\', '"' });
         var    d   = await new System.Diagnostics.ProcessStartInfo("node", $"-e \"console.log({arg});\"").WaitForExitAndReadOutputAsync();
         Assert(d.StandardOutput == "a\n");
         string js = ProcessExtensions.ExecuteJS_Builder(NO_IMPORTS, "", new object[] { d.StandardOutput[..^ 1] });
Пример #2
0
        public async Task StringTab()
        {
            const string deserializesToTab = "\"\\t\"";

            void defaultJavascriptEncoderTabDeserialization()
            {
                Assert(JsonSerializer.Deserialize <string>(deserializesToTab) == "\t");
            }

            void unsafeJavascriptEncoderTabDeserialization()
            {
                var options = new JsonSerializerOptions {
                    Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
                };

                Assert(JsonSerializer.Deserialize <string>(deserializesToTab, options) == "\t");
            }

            // this is correct js because it's tested in preTest via temp file rather than via bash
            // this is what the JS should be such that a tab is printed:
            const string correct_js = "\nvar result = \"\\t\";\n\nconsole.log('__DEBUG__');\nconsole.log(JSON.stringify(result));\n";

            async Task preTest()
            {
                var(_, stdOut, _, _) = await ProcessExtensions.ExecuteJSViaTempFile(correct_js);

                Assert(stdOut == deserializesToTab + "\n");
            }

            defaultJavascriptEncoderTabDeserialization();
            unsafeJavascriptEncoderTabDeserialization();
            await preTest();


            // literal JS:
            // console.log("\t");
            const string input = "\t";

            // test the built JS:
            string js = ProcessExtensions.ExecuteJS_Builder(NO_IMPORTS, input);

            Assert(correct_js == js);

            // test the built JS, escaping to bash and executing:
            string jsOutput = await executeJS((object)input);

            Assert(jsOutput == deserializesToTab + "\n");             // if this fails it must be the bashEscape
        }