1"""
2Test quoting of arguments to lldb commands.
3"""
4
5import lldb
6from lldbsuite.test.decorators import *
7from lldbsuite.test.lldbtest import *
8from lldbsuite.test import lldbutil
9
10
11class SettingsCommandTestCase(TestBase):
12
13    mydir = TestBase.compute_mydir(__file__)
14    output_file_name = "output.txt"
15
16    @classmethod
17    def classCleanup(cls):
18        """Cleanup the test byproducts."""
19        cls.RemoveTempFile(SettingsCommandTestCase.output_file_name)
20
21    @skipIfReproducer  # Reproducers don't know about output.txt
22    @no_debug_info_test
23    def test(self):
24        self.build()
25        exe = self.getBuildArtifact("a.out")
26        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
27
28        # No quotes.
29        self.expect_args("a b c", "a\0b\0c\0")
30        # Single quotes.
31        self.expect_args("'a b c'", "a b c\0")
32        # Double quotes.
33        self.expect_args('"a b c"', "a b c\0")
34        # Single quote escape.
35        self.expect_args("'a b\\' c", "a b\\\0c\0")
36        # Double quote escape.
37        self.expect_args('"a b\\" c"', 'a b" c\0')
38        self.expect_args('"a b\\\\" c', 'a b\\\0c\0')
39        # Single quote in double quotes.
40        self.expect_args('"a\'b"', "a'b\0")
41        # Double quotes in single quote.
42        self.expect_args("'a\"b'", 'a"b\0')
43        # Combined quotes.
44        self.expect_args('"a b"c\'d e\'', 'a bcd e\0')
45        # Bare single/double quotes.
46        self.expect_args("a\\'b", "a'b\0")
47        self.expect_args('a\\"b', 'a"b\0')
48
49    def expect_args(self, args_in, args_out):
50        """Test argument parsing. Run the program with args_in. The program dumps its arguments
51        to stdout. Compare the stdout with args_out."""
52
53        filename = SettingsCommandTestCase.output_file_name
54
55        if lldb.remote_platform:
56            outfile = lldb.remote_platform.GetWorkingDirectory() + filename
57        else:
58            outfile = self.getBuildArtifact(filename)
59
60        self.runCmd("process launch -- %s %s" % (outfile, args_in))
61
62        if lldb.remote_platform:
63            src_file_spec = lldb.SBFileSpec(outfile, False)
64            dst_file_spec = lldb.SBFileSpec(outfile, True)
65            lldb.remote_platform.Get(src_file_spec, dst_file_spec)
66
67        with open(outfile, 'r') as f:
68            output = f.read()
69
70        self.RemoveTempFile(outfile)
71
72        self.assertEqual(output, args_out)
73