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