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    @no_debug_info_test
22    def test(self):
23        self.build()
24        exe = self.getBuildArtifact("a.out")
25        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
26
27        # No quotes.
28        self.expect_args("a b c", "a\0b\0c\0")
29        # Single quotes.
30        self.expect_args("'a b c'", "a b c\0")
31        # Double quotes.
32        self.expect_args('"a b c"', "a b c\0")
33        # Single quote escape.
34        self.expect_args("'a b\\' c", "a b\\\0c\0")
35        # Double quote escape.
36        self.expect_args('"a b\\" c"', 'a b" c\0')
37        self.expect_args('"a b\\\\" c', 'a b\\\0c\0')
38        # Single quote in double quotes.
39        self.expect_args('"a\'b"', "a'b\0")
40        # Double quotes in single quote.
41        self.expect_args("'a\"b'", 'a"b\0')
42        # Combined quotes.
43        self.expect_args('"a b"c\'d e\'', 'a bcd e\0')
44        # Bare single/double quotes.
45        self.expect_args("a\\'b", "a'b\0")
46        self.expect_args('a\\"b', 'a"b\0')
47
48    def expect_args(self, args_in, args_out):
49        """Test argument parsing. Run the program with args_in. The program dumps its arguments
50        to stdout. Compare the stdout with args_out."""
51
52        filename = SettingsCommandTestCase.output_file_name
53
54        if lldb.remote_platform:
55            outfile = lldb.remote_platform.GetWorkingDirectory() + filename
56        else:
57            outfile = self.getBuildArtifact(filename)
58
59        self.runCmd("process launch -- %s %s" % (outfile, args_in))
60
61        if lldb.remote_platform:
62            src_file_spec = lldb.SBFileSpec(outfile, False)
63            dst_file_spec = lldb.SBFileSpec(outfile, True)
64            lldb.remote_platform.Get(src_file_spec, dst_file_spec)
65
66        with open(outfile, 'r') as f:
67            output = f.read()
68
69        self.RemoveTempFile(outfile)
70
71        self.assertEqual(output, args_out)
72