xref: /llvm-project/lldb/test/API/python_api/default-constructor/TestDefaultConstructorForAPIObjects.py (revision 4cc8f2a017c76af25234afc7c380550e9c93135c)
1"""
2Test lldb Python API object's default constructor and make sure it is invalid
3after initial construction.
4
5There are also some cases of boundary condition testings sprinkled throughout
6the tests where None is passed to SB API which expects (const char *) in the
7C++ API counterpart.  Passing None should not crash lldb!
8
9There are three exceptions to the above general rules, though; API objects
10SBCommandReturnObject, SBStream, and SBSymbolContextList, are all valid objects
11after default construction.
12"""
13
14from __future__ import print_function
15
16
17import lldb
18from lldbsuite.test.decorators import *
19from lldbsuite.test.lldbtest import *
20from lldbsuite.test import lldbutil
21
22
23class APIDefaultConstructorTestCase(TestBase):
24    NO_DEBUG_INFO_TESTCASE = True
25
26    def test_SBAddress(self):
27        obj = lldb.SBAddress()
28        if self.TraceOn():
29            print(obj)
30        self.assertFalse(obj)
31        # Do fuzz testing on the invalid obj, it should not crash lldb.
32        import sb_address
33        sb_address.fuzz_obj(obj)
34
35    def test_SBBlock(self):
36        obj = lldb.SBBlock()
37        if self.TraceOn():
38            print(obj)
39        self.assertFalse(obj)
40        # Do fuzz testing on the invalid obj, it should not crash lldb.
41        import sb_block
42        sb_block.fuzz_obj(obj)
43
44    def test_SBBreakpoint(self):
45        obj = lldb.SBBreakpoint()
46        if self.TraceOn():
47            print(obj)
48        self.assertFalse(obj)
49        # Do fuzz testing on the invalid obj, it should not crash lldb.
50        import sb_breakpoint
51        sb_breakpoint.fuzz_obj(obj)
52
53    def test_SBBreakpointLocation(self):
54        obj = lldb.SBBreakpointLocation()
55        if self.TraceOn():
56            print(obj)
57        self.assertFalse(obj)
58        # Do fuzz testing on the invalid obj, it should not crash lldb.
59        import sb_breakpointlocation
60        sb_breakpointlocation.fuzz_obj(obj)
61
62    def test_SBBreakpointName(self):
63        obj = lldb.SBBreakpointName()
64        if self.TraceOn():
65            print(obj)
66        self.assertFalse(obj)
67        # Do fuzz testing on the invalid obj, it should not crash lldb.
68        import sb_breakpointname
69        sb_breakpointname.fuzz_obj(obj)
70
71    def test_SBBroadcaster(self):
72        obj = lldb.SBBroadcaster()
73        if self.TraceOn():
74            print(obj)
75        self.assertFalse(obj)
76        # Do fuzz testing on the invalid obj, it should not crash lldb.
77        import sb_broadcaster
78        sb_broadcaster.fuzz_obj(obj)
79
80    def test_SBCommandReturnObject(self):
81        """SBCommandReturnObject object is valid after default construction."""
82        obj = lldb.SBCommandReturnObject()
83        if self.TraceOn():
84            print(obj)
85        self.assertTrue(obj)
86
87    def test_SBCommunication(self):
88        obj = lldb.SBCommunication()
89        if self.TraceOn():
90            print(obj)
91        self.assertFalse(obj)
92        # Do fuzz testing on the invalid obj, it should not crash lldb.
93        import sb_communication
94        sb_communication.fuzz_obj(obj)
95
96    def test_SBCompileUnit(self):
97        obj = lldb.SBCompileUnit()
98        if self.TraceOn():
99            print(obj)
100        self.assertFalse(obj)
101        # Do fuzz testing on the invalid obj, it should not crash lldb.
102        import sb_compileunit
103        sb_compileunit.fuzz_obj(obj)
104
105    def test_SBDebugger(self):
106        obj = lldb.SBDebugger()
107        if self.TraceOn():
108            print(obj)
109        self.assertFalse(obj)
110        # Do fuzz testing on the invalid obj, it should not crash lldb.
111        import sb_debugger
112        sb_debugger.fuzz_obj(obj)
113
114    # darwin: This test passes with swig 3.0.2, fails w/3.0.5 other tests fail
115    # with 2.0.12 http://llvm.org/pr23488
116    def test_SBError(self):
117        obj = lldb.SBError()
118        if self.TraceOn():
119            print(obj)
120        self.assertFalse(obj)
121        # Do fuzz testing on the invalid obj, it should not crash lldb.
122        import sb_error
123        sb_error.fuzz_obj(obj)
124
125    def test_SBEvent(self):
126        obj = lldb.SBEvent()
127        # This is just to test that typemap, as defined in lldb.swig, works.
128        obj2 = lldb.SBEvent(0, "abc")
129        if self.TraceOn():
130            print(obj)
131        self.assertFalse(obj)
132        # Do fuzz testing on the invalid obj, it should not crash lldb.
133        import sb_event
134        sb_event.fuzz_obj(obj)
135
136    def test_SBFileSpec(self):
137        obj = lldb.SBFileSpec()
138        # This is just to test that FileSpec(None) does not crash.
139        obj2 = lldb.SBFileSpec(None, True)
140        if self.TraceOn():
141            print(obj)
142        self.assertFalse(obj)
143        # Do fuzz testing on the invalid obj, it should not crash lldb.
144        import sb_filespec
145        sb_filespec.fuzz_obj(obj)
146
147    def test_SBFrame(self):
148        obj = lldb.SBFrame()
149        if self.TraceOn():
150            print(obj)
151        self.assertFalse(obj)
152        # Do fuzz testing on the invalid obj, it should not crash lldb.
153        import sb_frame
154        sb_frame.fuzz_obj(obj)
155
156    def test_SBFunction(self):
157        obj = lldb.SBFunction()
158        if self.TraceOn():
159            print(obj)
160        self.assertFalse(obj)
161        # Do fuzz testing on the invalid obj, it should not crash lldb.
162        import sb_function
163        sb_function.fuzz_obj(obj)
164
165    def test_SBFile(self):
166        sbf = lldb.SBFile()
167        self.assertFalse(sbf.IsValid())
168        self.assertFalse(bool(sbf))
169        e, n = sbf.Write(b'foo')
170        self.assertTrue(e.Fail())
171        self.assertEqual(n, 0)
172        buffer = bytearray(100)
173        e, n = sbf.Read(buffer)
174        self.assertEqual(n, 0)
175        self.assertTrue(e.Fail())
176
177    def test_SBInstruction(self):
178        obj = lldb.SBInstruction()
179        if self.TraceOn():
180            print(obj)
181        self.assertFalse(obj)
182        # Do fuzz testing on the invalid obj, it should not crash lldb.
183        import sb_instruction
184        sb_instruction.fuzz_obj(obj)
185
186    def test_SBInstructionList(self):
187        obj = lldb.SBInstructionList()
188        if self.TraceOn():
189            print(obj)
190        self.assertFalse(obj)
191        # Do fuzz testing on the invalid obj, it should not crash lldb.
192        import sb_instructionlist
193        sb_instructionlist.fuzz_obj(obj)
194
195    def test_SBLineEntry(self):
196        obj = lldb.SBLineEntry()
197        if self.TraceOn():
198            print(obj)
199        self.assertFalse(obj)
200        # Do fuzz testing on the invalid obj, it should not crash lldb.
201        import sb_lineentry
202        sb_lineentry.fuzz_obj(obj)
203
204    def test_SBListener(self):
205        obj = lldb.SBListener()
206        if self.TraceOn():
207            print(obj)
208        self.assertFalse(obj)
209        # Do fuzz testing on the invalid obj, it should not crash lldb.
210        import sb_listener
211        sb_listener.fuzz_obj(obj)
212
213    # Py3 asserts due to a bug in SWIG.  Trying to upstream a patch to fix
214    # this in 3.0.8
215    @skipIf(py_version=['>=', (3, 0)], swig_version=['<', (3, 0, 8)])
216    def test_SBModule(self):
217        obj = lldb.SBModule()
218        if self.TraceOn():
219            print(obj)
220        self.assertFalse(obj)
221        # Do fuzz testing on the invalid obj, it should not crash lldb.
222        import sb_module
223        sb_module.fuzz_obj(obj)
224
225    def test_SBProcess(self):
226        obj = lldb.SBProcess()
227        if self.TraceOn():
228            print(obj)
229        self.assertFalse(obj)
230        # Do fuzz testing on the invalid obj, it should not crash lldb.
231        import sb_process
232        sb_process.fuzz_obj(obj)
233
234    def test_SBProcessInfo(self):
235        obj = lldb.SBProcessInfo()
236        if self.TraceOn():
237            print(obj)
238        self.assertFalse(obj)
239        # Do fuzz testing on the invalid obj, it should not crash lldb.
240        import sb_process_info
241        sb_process_info.fuzz_obj(obj)
242
243    def test_SBSection(self):
244        obj = lldb.SBSection()
245        if self.TraceOn():
246            print(obj)
247        self.assertFalse(obj)
248        # Do fuzz testing on the invalid obj, it should not crash lldb.
249        import sb_section
250        sb_section.fuzz_obj(obj)
251
252    def test_SBStream(self):
253        """SBStream object is valid after default construction."""
254        obj = lldb.SBStream()
255        if self.TraceOn():
256            print(obj)
257        self.assertTrue(obj)
258
259    def test_SBStringList(self):
260        obj = lldb.SBStringList()
261        if self.TraceOn():
262            print(obj)
263        self.assertFalse(obj)
264        # Do fuzz testing on the invalid obj, it should not crash lldb.
265        import sb_stringlist
266        sb_stringlist.fuzz_obj(obj)
267
268    def test_SBSymbol(self):
269        obj = lldb.SBSymbol()
270        if self.TraceOn():
271            print(obj)
272        self.assertFalse(obj)
273        # Do fuzz testing on the invalid obj, it should not crash lldb.
274        import sb_symbol
275        sb_symbol.fuzz_obj(obj)
276
277    def test_SBSymbolContext(self):
278        obj = lldb.SBSymbolContext()
279        if self.TraceOn():
280            print(obj)
281        self.assertFalse(obj)
282        # Do fuzz testing on the invalid obj, it should not crash lldb.
283        import sb_symbolcontext
284        sb_symbolcontext.fuzz_obj(obj)
285
286    def test_SBSymbolContextList(self):
287        """SBSymbolContextList object is valid after default construction."""
288        obj = lldb.SBSymbolContextList()
289        if self.TraceOn():
290            print(obj)
291        self.assertTrue(obj)
292
293    def test_SBTarget(self):
294        obj = lldb.SBTarget()
295        if self.TraceOn():
296            print(obj)
297        self.assertFalse(obj)
298        # Do fuzz testing on the invalid obj, it should not crash lldb.
299        import sb_target
300        sb_target.fuzz_obj(obj)
301
302    def test_SBThread(self):
303        obj = lldb.SBThread()
304        if self.TraceOn():
305            print(obj)
306        self.assertFalse(obj)
307        # Do fuzz testing on the invalid obj, it should not crash lldb.
308        import sb_thread
309        sb_thread.fuzz_obj(obj)
310
311    def test_SBType(self):
312        try:
313            obj = lldb.SBType()
314            if self.TraceOn():
315                print(obj)
316            self.assertFalse(obj)
317            # If we reach here, the test fails.
318            self.fail("lldb.SBType() should fail, not succeed!")
319        except:
320            # Exception is expected.
321            return
322
323        # Unreachable code because lldb.SBType() should fail.
324        # Do fuzz testing on the invalid obj, it should not crash lldb.
325        import sb_type
326        sb_type.fuzz_obj(obj)
327
328    def test_SBTypeList(self):
329        """SBTypeList object is valid after default construction."""
330        obj = lldb.SBTypeList()
331        if self.TraceOn():
332            print(obj)
333        self.assertTrue(obj)
334
335    def test_SBValue(self):
336        obj = lldb.SBValue()
337        if self.TraceOn():
338            print(obj)
339        self.assertFalse(obj)
340        # Do fuzz testing on the invalid obj, it should not crash lldb.
341        import sb_value
342        sb_value.fuzz_obj(obj)
343
344    def test_SBValueList(self):
345        obj = lldb.SBValueList()
346        if self.TraceOn():
347            print(obj)
348        self.assertFalse(obj)
349        # Do fuzz testing on the invalid obj, it should not crash lldb.
350        import sb_valuelist
351        sb_valuelist.fuzz_obj(obj)
352
353    def test_SBWatchpoint(self):
354        obj = lldb.SBWatchpoint()
355        if self.TraceOn():
356            print(obj)
357        self.assertFalse(obj)
358        # Do fuzz testing on the invalid obj, it should not crash lldb.
359        import sb_watchpoint
360        sb_watchpoint.fuzz_obj(obj)
361