xref: /llvm-project/lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1"""
2Test some SBStructuredData API.
3"""
4
5
6import lldb
7from lldbsuite.test.decorators import *
8from lldbsuite.test.lldbtest import *
9from lldbsuite.test import lldbutil
10
11import json
12
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: " + str(dict_struct.GetSize()))
103
104    def string_struct_test(self, dict_struct):
105        string_struct = lldb.SBStructuredData()
106        string_struct = dict_struct.GetValueForKey("key_string")
107        if not string_struct.IsValid():
108            self.fail("A valid object should have been returned")
109
110        # Check Type API
111        if not string_struct.GetType() == lldb.eStructuredDataTypeString:
112            self.fail("Wrong type returned: " + str(string_struct.GetType()))
113
114        # Check API returning 'string' value
115        output = string_struct.GetStringValue(25)
116        if not "STRING" in output:
117            self.fail("wrong output: " + output)
118
119        # Calling wrong API on a SBStructuredData
120        # (e.g. getting an integer from a string type structure)
121        output = string_struct.GetIntegerValue()
122        if output:
123            self.fail(
124                "Valid integer value " + str(output) + " returned for a string object"
125            )
126
127    def uint_struct_test(self, dict_struct):
128        # Check a valid SBStructuredData containing an unsigned integer.
129        # We intentionally make this larger than what an int64_t can hold but
130        # still small enough to fit a uint64_t
131        uint_struct = lldb.SBStructuredData()
132        uint_struct = dict_struct.GetValueForKey("key_uint")
133        if not uint_struct.IsValid():
134            self.fail("A valid object should have been returned")
135
136        # Check Type API
137        if not uint_struct.GetType() == lldb.eStructuredDataTypeInteger:
138            self.fail("Wrong type returned: " + str(uint_struct.GetType()))
139
140        # Check API returning unsigned integer value
141        output = uint_struct.GetUnsignedIntegerValue()
142        if not output == 0xFFFFFFFF00000000:
143            self.fail("wrong output: " + str(output))
144
145        # Calling wrong API on a SBStructuredData
146        # (e.g. getting a string value from an integer type structure)
147        output = uint_struct.GetStringValue(25)
148        if output:
149            self.fail("Valid string " + output + " returned for an integer object")
150
151    def sint_struct_test(self, dict_struct):
152        # Check a valid SBStructuredData containing an signed integer.
153        # We intentionally make this smaller than what an uint64_t can hold but
154        # still small enough to fit a int64_t
155        sint_struct = lldb.SBStructuredData()
156        sint_struct = dict_struct.GetValueForKey("key_sint")
157        if not sint_struct.IsValid():
158            self.fail("A valid object should have been returned")
159
160        # Check Type API
161        if not sint_struct.GetType() == lldb.eStructuredDataTypeSignedInteger:
162            self.fail("Wrong type returned: " + str(sint_struct.GetType()))
163
164        # Check API returning signed integer value
165        output = sint_struct.GetSignedIntegerValue()
166        if not output == -42:
167            self.fail("wrong output: " + str(output))
168
169        # Calling wrong API on a SBStructuredData
170        # (e.g. getting a string value from an integer type structure)
171        output = sint_struct.GetStringValue(69)
172        if output:
173            self.fail("Valid string " + output + " returned for an integer object")
174
175    def double_struct_test(self, dict_struct):
176        floating_point_struct = lldb.SBStructuredData()
177        floating_point_struct = dict_struct.GetValueForKey("key_float")
178        if not floating_point_struct.IsValid():
179            self.fail("A valid object should have been returned")
180
181        # Check Type API
182        if not floating_point_struct.GetType() == lldb.eStructuredDataTypeFloat:
183            self.fail("Wrong type returned: " + str(floating_point_struct.GetType()))
184
185        # Check API returning 'double' value
186        output = floating_point_struct.GetFloatValue()
187        if not output == 2.99:
188            self.fail("wrong output: " + str(output))
189
190    def bool_struct_test(self, dict_struct):
191        bool_struct = lldb.SBStructuredData()
192        bool_struct = dict_struct.GetValueForKey("key_bool")
193        if not bool_struct.IsValid():
194            self.fail("A valid object should have been returned")
195
196        # Check Type API
197        if not bool_struct.GetType() == lldb.eStructuredDataTypeBoolean:
198            self.fail("Wrong type returned: " + str(bool_struct.GetType()))
199
200        # Check API returning 'bool' value
201        output = bool_struct.GetBooleanValue()
202        if not output:
203            self.fail("wrong output: " + str(output))
204
205    def array_struct_test(self, dict_struct):
206        # Check API returning a valid SBStructuredData of 'array' type
207        array_struct = lldb.SBStructuredData()
208        array_struct = dict_struct.GetValueForKey("key_array")
209        if not array_struct.IsValid():
210            self.fail("A valid object should have been returned")
211
212        # Check Type API
213        if not array_struct.GetType() == lldb.eStructuredDataTypeArray:
214            self.fail("Wrong type returned: " + str(array_struct.GetType()))
215
216        # Check Size API for 'array' type
217        if not array_struct.GetSize() == 2:
218            self.fail("Wrong no of elements returned: " + str(array_struct.GetSize()))
219
220        # Check API returning a valid SBStructuredData for different 'array'
221        # indices
222        string_struct = array_struct.GetItemAtIndex(0)
223        if not string_struct.IsValid():
224            self.fail("A valid object should have been returned")
225        if not string_struct.GetType() == lldb.eStructuredDataTypeString:
226            self.fail("Wrong type returned: " + str(string_struct.GetType()))
227        output = string_struct.GetStringValue(5)
228        if not output == "23":
229            self.fail("wrong output: " + str(output))
230
231        string_struct = array_struct.GetItemAtIndex(1)
232        if not string_struct.IsValid():
233            self.fail("A valid object should have been returned")
234        if not string_struct.GetType() == lldb.eStructuredDataTypeString:
235            self.fail("Wrong type returned: " + str(string_struct.GetType()))
236        output = string_struct.GetStringValue(5)
237        if not output == "arr":
238            self.fail("wrong output: " + str(output))
239