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    @skipIfReproducer # lldb::FileSP used in typemap cannot be instrumented.
108    def test_SBDebugger(self):
109        obj = lldb.SBDebugger()
110        if self.TraceOn():
111            print(obj)
112        self.assertFalse(obj)
113        # Do fuzz testing on the invalid obj, it should not crash lldb.
114        import sb_debugger
115        sb_debugger.fuzz_obj(obj)
116
117    # darwin: This test passes with swig 3.0.2, fails w/3.0.5 other tests fail
118    # with 2.0.12 http://llvm.org/pr23488
119    def test_SBError(self):
120        obj = lldb.SBError()
121        if self.TraceOn():
122            print(obj)
123        self.assertFalse(obj)
124        # Do fuzz testing on the invalid obj, it should not crash lldb.
125        import sb_error
126        sb_error.fuzz_obj(obj)
127
128    def test_SBEvent(self):
129        obj = lldb.SBEvent()
130        # This is just to test that typemap, as defined in lldb.swig, works.
131        obj2 = lldb.SBEvent(0, "abc")
132        if self.TraceOn():
133            print(obj)
134        self.assertFalse(obj)
135        # Do fuzz testing on the invalid obj, it should not crash lldb.
136        import sb_event
137        sb_event.fuzz_obj(obj)
138
139    def test_SBFileSpec(self):
140        obj = lldb.SBFileSpec()
141        # This is just to test that FileSpec(None) does not crash.
142        obj2 = lldb.SBFileSpec(None, True)
143        if self.TraceOn():
144            print(obj)
145        self.assertFalse(obj)
146        # Do fuzz testing on the invalid obj, it should not crash lldb.
147        import sb_filespec
148        sb_filespec.fuzz_obj(obj)
149
150    def test_SBFrame(self):
151        obj = lldb.SBFrame()
152        if self.TraceOn():
153            print(obj)
154        self.assertFalse(obj)
155        # Do fuzz testing on the invalid obj, it should not crash lldb.
156        import sb_frame
157        sb_frame.fuzz_obj(obj)
158
159    def test_SBFunction(self):
160        obj = lldb.SBFunction()
161        if self.TraceOn():
162            print(obj)
163        self.assertFalse(obj)
164        # Do fuzz testing on the invalid obj, it should not crash lldb.
165        import sb_function
166        sb_function.fuzz_obj(obj)
167
168    @skipIfReproducer # lldb::FileSP used in typemap cannot be instrumented.
169    def test_SBFile(self):
170        sbf = lldb.SBFile()
171        self.assertFalse(sbf.IsValid())
172        self.assertFalse(bool(sbf))
173        e, n = sbf.Write(b'foo')
174        self.assertTrue(e.Fail())
175        self.assertEqual(n, 0)
176        buffer = bytearray(100)
177        e, n = sbf.Read(buffer)
178        self.assertEqual(n, 0)
179        self.assertTrue(e.Fail())
180
181    def test_SBInstruction(self):
182        obj = lldb.SBInstruction()
183        if self.TraceOn():
184            print(obj)
185        self.assertFalse(obj)
186        # Do fuzz testing on the invalid obj, it should not crash lldb.
187        import sb_instruction
188        sb_instruction.fuzz_obj(obj)
189
190    def test_SBInstructionList(self):
191        obj = lldb.SBInstructionList()
192        if self.TraceOn():
193            print(obj)
194        self.assertFalse(obj)
195        # Do fuzz testing on the invalid obj, it should not crash lldb.
196        import sb_instructionlist
197        sb_instructionlist.fuzz_obj(obj)
198
199    def test_SBLineEntry(self):
200        obj = lldb.SBLineEntry()
201        if self.TraceOn():
202            print(obj)
203        self.assertFalse(obj)
204        # Do fuzz testing on the invalid obj, it should not crash lldb.
205        import sb_lineentry
206        sb_lineentry.fuzz_obj(obj)
207
208    def test_SBListener(self):
209        obj = lldb.SBListener()
210        if self.TraceOn():
211            print(obj)
212        self.assertFalse(obj)
213        # Do fuzz testing on the invalid obj, it should not crash lldb.
214        import sb_listener
215        sb_listener.fuzz_obj(obj)
216
217    # Py3 asserts due to a bug in SWIG.  Trying to upstream a patch to fix
218    # this in 3.0.8
219    @skipIf(py_version=['>=', (3, 0)], swig_version=['<', (3, 0, 8)])
220    def test_SBModule(self):
221        obj = lldb.SBModule()
222        if self.TraceOn():
223            print(obj)
224        self.assertFalse(obj)
225        # Do fuzz testing on the invalid obj, it should not crash lldb.
226        import sb_module
227        sb_module.fuzz_obj(obj)
228
229    def test_SBProcess(self):
230        obj = lldb.SBProcess()
231        if self.TraceOn():
232            print(obj)
233        self.assertFalse(obj)
234        # Do fuzz testing on the invalid obj, it should not crash lldb.
235        import sb_process
236        sb_process.fuzz_obj(obj)
237
238    def test_SBProcessInfo(self):
239        obj = lldb.SBProcessInfo()
240        if self.TraceOn():
241            print(obj)
242        self.assertFalse(obj)
243        # Do fuzz testing on the invalid obj, it should not crash lldb.
244        import sb_process_info
245        sb_process_info.fuzz_obj(obj)
246
247    def test_SBSection(self):
248        obj = lldb.SBSection()
249        if self.TraceOn():
250            print(obj)
251        self.assertFalse(obj)
252        # Do fuzz testing on the invalid obj, it should not crash lldb.
253        import sb_section
254        sb_section.fuzz_obj(obj)
255
256    def test_SBStream(self):
257        """SBStream object is valid after default construction."""
258        obj = lldb.SBStream()
259        if self.TraceOn():
260            print(obj)
261        self.assertTrue(obj)
262
263    def test_SBStringList(self):
264        obj = lldb.SBStringList()
265        if self.TraceOn():
266            print(obj)
267        self.assertFalse(obj)
268        # Do fuzz testing on the invalid obj, it should not crash lldb.
269        import sb_stringlist
270        sb_stringlist.fuzz_obj(obj)
271
272    def test_SBSymbol(self):
273        obj = lldb.SBSymbol()
274        if self.TraceOn():
275            print(obj)
276        self.assertFalse(obj)
277        # Do fuzz testing on the invalid obj, it should not crash lldb.
278        import sb_symbol
279        sb_symbol.fuzz_obj(obj)
280
281    def test_SBSymbolContext(self):
282        obj = lldb.SBSymbolContext()
283        if self.TraceOn():
284            print(obj)
285        self.assertFalse(obj)
286        # Do fuzz testing on the invalid obj, it should not crash lldb.
287        import sb_symbolcontext
288        sb_symbolcontext.fuzz_obj(obj)
289
290    def test_SBSymbolContextList(self):
291        """SBSymbolContextList object is valid after default construction."""
292        obj = lldb.SBSymbolContextList()
293        if self.TraceOn():
294            print(obj)
295        self.assertTrue(obj)
296
297    def test_SBTarget(self):
298        obj = lldb.SBTarget()
299        if self.TraceOn():
300            print(obj)
301        self.assertFalse(obj)
302        # Do fuzz testing on the invalid obj, it should not crash lldb.
303        import sb_target
304        sb_target.fuzz_obj(obj)
305
306    def test_SBThread(self):
307        obj = lldb.SBThread()
308        if self.TraceOn():
309            print(obj)
310        self.assertFalse(obj)
311        # Do fuzz testing on the invalid obj, it should not crash lldb.
312        import sb_thread
313        sb_thread.fuzz_obj(obj)
314
315    def test_SBType(self):
316        try:
317            obj = lldb.SBType()
318            if self.TraceOn():
319                print(obj)
320            self.assertFalse(obj)
321            # If we reach here, the test fails.
322            self.fail("lldb.SBType() should fail, not succeed!")
323        except:
324            # Exception is expected.
325            return
326
327        # Unreachable code because lldb.SBType() should fail.
328        # Do fuzz testing on the invalid obj, it should not crash lldb.
329        import sb_type
330        sb_type.fuzz_obj(obj)
331
332    def test_SBTypeList(self):
333        """SBTypeList object is valid after default construction."""
334        obj = lldb.SBTypeList()
335        if self.TraceOn():
336            print(obj)
337        self.assertTrue(obj)
338
339    def test_SBValue(self):
340        obj = lldb.SBValue()
341        if self.TraceOn():
342            print(obj)
343        self.assertFalse(obj)
344        # Do fuzz testing on the invalid obj, it should not crash lldb.
345        import sb_value
346        sb_value.fuzz_obj(obj)
347
348    def test_SBValueList(self):
349        obj = lldb.SBValueList()
350        if self.TraceOn():
351            print(obj)
352        self.assertFalse(obj)
353        # Do fuzz testing on the invalid obj, it should not crash lldb.
354        import sb_valuelist
355        sb_valuelist.fuzz_obj(obj)
356
357    def test_SBWatchpoint(self):
358        obj = lldb.SBWatchpoint()
359        if self.TraceOn():
360            print(obj)
361        self.assertFalse(obj)
362        # Do fuzz testing on the invalid obj, it should not crash lldb.
363        import sb_watchpoint
364        sb_watchpoint.fuzz_obj(obj)
365