1"""
2Test quoting of arguments to lldb commands
3"""
4
5
6
7
8import lldb
9from lldbsuite.test.decorators import *
10from lldbsuite.test.lldbtest import *
11from lldbsuite.test import lldbutil
12
13
14class SettingsCommandTestCase(TestBase):
15
16    mydir = TestBase.compute_mydir(__file__)
17
18    @classmethod
19    def classCleanup(cls):
20        """Cleanup the test byproducts."""
21        cls.RemoveTempFile("stdout.txt")
22
23    @no_debug_info_test
24    def test_no_quote(self):
25        self.do_test_args("a b c", "a\0b\0c\0")
26
27    @no_debug_info_test
28    def test_single_quote(self):
29        self.do_test_args("'a b c'", "a b c\0")
30
31    @no_debug_info_test
32    def test_double_quote(self):
33        self.do_test_args('"a b c"', "a b c\0")
34
35    @no_debug_info_test
36    def test_single_quote_escape(self):
37        self.do_test_args("'a b\\' c", "a b\\\0c\0")
38
39    @no_debug_info_test
40    def test_double_quote_escape(self):
41        self.do_test_args('"a b\\" c"', 'a b" c\0')
42
43    @no_debug_info_test
44    def test_double_quote_escape2(self):
45        self.do_test_args('"a b\\\\" c', 'a b\\\0c\0')
46
47    @no_debug_info_test
48    def test_single_in_double(self):
49        self.do_test_args('"a\'b"', "a'b\0")
50
51    @no_debug_info_test
52    def test_double_in_single(self):
53        self.do_test_args("'a\"b'", 'a"b\0')
54
55    @no_debug_info_test
56    def test_combined(self):
57        self.do_test_args('"a b"c\'d e\'', 'a bcd e\0')
58
59    @no_debug_info_test
60    def test_bare_single(self):
61        self.do_test_args("a\\'b", "a'b\0")
62
63    @no_debug_info_test
64    def test_bare_double(self):
65        self.do_test_args('a\\"b', 'a"b\0')
66
67    def do_test_args(self, args_in, args_out):
68        """Test argument parsing. Run the program with args_in. The program dumps its arguments
69        to stdout. Compare the stdout with args_out."""
70        self.buildDefault()
71
72        exe = self.getBuildArtifact("a.out")
73        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
74
75        local_outfile = self.getBuildArtifact("output.txt")
76        if lldb.remote_platform:
77            remote_outfile = lldb.remote_platform.GetWorkingDirectory() + "/output.txt"
78        else:
79            remote_outfile = local_outfile
80
81        self.runCmd("process launch -- %s %s" %(remote_outfile, args_in))
82
83        if lldb.remote_platform:
84            src_file_spec = lldb.SBFileSpec(remote_outfile, False)
85            dst_file_spec = lldb.SBFileSpec(local_outfile, True)
86            lldb.remote_platform.Get(src_file_spec, dst_file_spec)
87
88        with open(local_outfile, 'r') as f:
89            output = f.read()
90
91        self.RemoveTempFile(local_outfile)
92
93        self.assertEqual(output, args_out)
94