xref: /llvm-project/lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py (revision 1370a1cb5b97ecfc4fd2cb550159db9c9ebd3a68)
1"""
2Test some SBStructuredData API.
3"""
4
5
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12import json
13
14class TestStructuredDataAPI(TestBase):
15    NO_DEBUG_INFO_TESTCASE = True
16
17    def test(self):
18        self.structured_data_api_test()
19
20    def structured_data_api_test(self):
21        error = lldb.SBError()
22        s = lldb.SBStream()
23
24        dict_str = json.dumps(
25            {
26                "key_dict": {
27                    "key_string": "STRING",
28                    "key_uint": 0xFFFFFFFF00000000,
29                    "key_sint": -42,
30                    "key_float": 2.99,
31                    "key_bool": True,
32                    "key_array": ["23", "arr"],
33                }
34            }
35        )
36        s.Print(dict_str)
37        example = lldb.SBStructuredData()
38
39        # Check SetFromJSON API for dictionaries, integers, floating point
40        # values, strings and arrays
41        error = example.SetFromJSON(s)
42        if not error.Success():
43            self.fail("FAILED:   " + error.GetCString())
44
45        # Tests for invalid data type
46        self.invalid_struct_test(example)
47
48        # Test that GetDescription works:
49        s.Clear()
50        error = example.GetDescription(s)
51        self.assertSuccess(error, "GetDescription works")
52        if not "key_float" in s.GetData():
53            self.fail("FAILED: could not find key_float in description output")
54
55        dict_struct = lldb.SBStructuredData()
56        dict_struct = example.GetValueForKey("key_dict")
57
58        # Tests for dictionary data type
59        self.dictionary_struct_test(example)
60
61        # Tests for string data type
62        self.string_struct_test(dict_struct)
63
64        # Tests for integer data type
65        self.uint_struct_test(dict_struct)
66
67        # Tests for integer data type
68        self.sint_struct_test(dict_struct)
69
70        # Tests for floating point data type
71        self.double_struct_test(dict_struct)
72
73        # Tests for boolean data type
74        self.bool_struct_test(dict_struct)
75
76        # Tests for array data type
77        self.array_struct_test(dict_struct)
78
79    def invalid_struct_test(self, example):
80        invalid_struct = lldb.SBStructuredData()
81        invalid_struct = example.GetValueForKey("invalid_key")
82        if invalid_struct.IsValid():
83            self.fail("An invalid object should have been returned")
84
85        # Check Type API
86        if not invalid_struct.GetType() == lldb.eStructuredDataTypeInvalid:
87            self.fail("Wrong type returned: " + str(invalid_struct.GetType()))
88
89    def dictionary_struct_test(self, example):
90        # Check API returning a valid SBStructuredData of 'dictionary' type
91        dict_struct = lldb.SBStructuredData()
92        dict_struct = example.GetValueForKey("key_dict")
93        if not dict_struct.IsValid():
94            self.fail("A valid object should have been returned")
95
96        # Check Type API
97        if not dict_struct.GetType() == lldb.eStructuredDataTypeDictionary:
98            self.fail("Wrong type returned: " + str(dict_struct.GetType()))
99
100        # Check Size API for 'dictionary' type
101        if not dict_struct.GetSize() == 6:
102            self.fail("Wrong no of elements returned: " +
103                      str(dict_struct.GetSize()))
104
105    def string_struct_test(self, dict_struct):
106        string_struct = lldb.SBStructuredData()
107        string_struct = dict_struct.GetValueForKey("key_string")
108        if not string_struct.IsValid():
109            self.fail("A valid object should have been returned")
110
111        # Check Type API
112        if not string_struct.GetType() == lldb.eStructuredDataTypeString:
113            self.fail("Wrong type returned: " + str(string_struct.GetType()))
114
115        # Check API returning 'string' value
116        output = string_struct.GetStringValue(25)
117        if not "STRING" in output:
118            self.fail("wrong output: " + output)
119
120        # Calling wrong API on a SBStructuredData
121        # (e.g. getting an integer from a string type structure)
122        output = string_struct.GetIntegerValue()
123        if output:
124            self.fail(
125                "Valid integer value " +
126                str(output) +
127                " returned for a string object")
128
129    def uint_struct_test(self, dict_struct):
130        # Check a valid SBStructuredData containing an unsigned integer.
131        # We intentionally make this larger than what an int64_t can hold but
132        # still small enough to fit a uint64_t
133        uint_struct = lldb.SBStructuredData()
134        uint_struct = dict_struct.GetValueForKey("key_uint")
135        if not uint_struct.IsValid():
136            self.fail("A valid object should have been returned")
137
138        # Check Type API
139        if not uint_struct.GetType() == lldb.eStructuredDataTypeInteger:
140            self.fail("Wrong type returned: " + str(uint_struct.GetType()))
141
142        # Check API returning unsigned integer value
143        output = uint_struct.GetUnsignedIntegerValue()
144        if not output == 0xffffffff00000000:
145            self.fail("wrong output: " + str(output))
146
147        # Calling wrong API on a SBStructuredData
148        # (e.g. getting a string value from an integer type structure)
149        output = uint_struct.GetStringValue(25)
150        if output:
151            self.fail("Valid string " + output + " returned for an integer object")
152
153    def sint_struct_test(self, dict_struct):
154        # Check a valid SBStructuredData containing an signed integer.
155        # We intentionally make this smaller than what an uint64_t can hold but
156        # still small enough to fit a int64_t
157        sint_struct = lldb.SBStructuredData()
158        sint_struct = dict_struct.GetValueForKey("key_sint")
159        if not sint_struct.IsValid():
160            self.fail("A valid object should have been returned")
161
162        # Check Type API
163        if not sint_struct.GetType() == lldb.eStructuredDataTypeSignedInteger:
164            self.fail("Wrong type returned: " + str(sint_struct.GetType()))
165
166        # Check API returning signed integer value
167        output = sint_struct.GetSignedIntegerValue()
168        if not output == -42:
169            self.fail("wrong output: " + str(output))
170
171        # Calling wrong API on a SBStructuredData
172        # (e.g. getting a string value from an integer type structure)
173        output = sint_struct.GetStringValue(69)
174        if output:
175            self.fail(
176                "Valid string " +
177                output +
178                " returned for an integer object")
179
180    def double_struct_test(self, dict_struct):
181        floating_point_struct = lldb.SBStructuredData()
182        floating_point_struct = dict_struct.GetValueForKey("key_float")
183        if not floating_point_struct.IsValid():
184            self.fail("A valid object should have been returned")
185
186        # Check Type API
187        if not floating_point_struct.GetType() == lldb.eStructuredDataTypeFloat:
188            self.fail("Wrong type returned: " +
189                      str(floating_point_struct.GetType()))
190
191        # Check API returning 'double' value
192        output = floating_point_struct.GetFloatValue()
193        if not output == 2.99:
194            self.fail("wrong output: " + str(output))
195
196    def bool_struct_test(self, dict_struct):
197        bool_struct = lldb.SBStructuredData()
198        bool_struct = dict_struct.GetValueForKey("key_bool")
199        if not bool_struct.IsValid():
200            self.fail("A valid object should have been returned")
201
202        # Check Type API
203        if not bool_struct.GetType() == lldb.eStructuredDataTypeBoolean:
204            self.fail("Wrong type returned: " + str(bool_struct.GetType()))
205
206        # Check API returning 'bool' value
207        output = bool_struct.GetBooleanValue()
208        if not output:
209            self.fail("wrong output: " + str(output))
210
211    def array_struct_test(self, dict_struct):
212        # Check API returning a valid SBStructuredData of 'array' type
213        array_struct = lldb.SBStructuredData()
214        array_struct = dict_struct.GetValueForKey("key_array")
215        if not array_struct.IsValid():
216            self.fail("A valid object should have been returned")
217
218        # Check Type API
219        if not array_struct.GetType() == lldb.eStructuredDataTypeArray:
220            self.fail("Wrong type returned: " + str(array_struct.GetType()))
221
222        # Check Size API for 'array' type
223        if not array_struct.GetSize() == 2:
224            self.fail("Wrong no of elements returned: " +
225                      str(array_struct.GetSize()))
226
227        # Check API returning a valid SBStructuredData for different 'array'
228        # indices
229        string_struct = array_struct.GetItemAtIndex(0)
230        if not string_struct.IsValid():
231            self.fail("A valid object should have been returned")
232        if not string_struct.GetType() == lldb.eStructuredDataTypeString:
233            self.fail("Wrong type returned: " + str(string_struct.GetType()))
234        output = string_struct.GetStringValue(5)
235        if not output == "23":
236            self.fail("wrong output: " + str(output))
237
238        string_struct = array_struct.GetItemAtIndex(1)
239        if not string_struct.IsValid():
240            self.fail("A valid object should have been returned")
241        if not string_struct.GetType() == lldb.eStructuredDataTypeString:
242            self.fail("Wrong type returned: " + str(string_struct.GetType()))
243        output = string_struct.GetStringValue(5)
244        if not output == "arr":
245            self.fail("wrong output: " + str(output))
246