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