~apport-hackers/apport/trunk

2732 by Martin Pitt
* fileutils.py, get_all_system_reports(): Filter out "guest..." users, they might have a system UID. (LP: #1250679)
1
import unittest, tempfile, os, shutil, time, sys, pwd
2161 by Martin Pitt
Move all test suites out of the code modules into test/test_<module>.py. This avoids having to load it every time the program runs, and also allows running the tests against the installed version of Apport.
2
3
import problem_report
4
import apport.fileutils
5
import apport.packaging
6
2302 by Martin Pitt
problem_report.py: Fix for Python 3
7
from io import BytesIO
2161 by Martin Pitt
Move all test suites out of the code modules into test/test_<module>.py. This avoids having to load it every time the program runs, and also allows running the tests against the installed version of Apport.
8
3036 by Martin Pitt
* Tests: Move to unittest's builtin "mock" module.
9
from unittest.mock import patch
2732 by Martin Pitt
* fileutils.py, get_all_system_reports(): Filter out "guest..." users, they might have a system UID. (LP: #1250679)
10
2277 by Martin Pitt
* Fix the whole code to be PEP-8 compatible, and enforce this in test/run by running the "pep8" tool.
11
2161 by Martin Pitt
Move all test suites out of the code modules into test/test_<module>.py. This avoids having to load it every time the program runs, and also allows running the tests against the installed version of Apport.
12
class T(unittest.TestCase):
13
    def setUp(self):
14
        self.orig_report_dir = apport.fileutils.report_dir
15
        apport.fileutils.report_dir = tempfile.mkdtemp()
16
        self.orig_config_file = apport.fileutils._config_file
17
18
    def tearDown(self):
19
        shutil.rmtree(apport.fileutils.report_dir)
20
        apport.fileutils.report_dir = self.orig_report_dir
21
        self.orig_report_dir = None
22
        apport.fileutils._config_file = self.orig_config_file
23
2277 by Martin Pitt
* Fix the whole code to be PEP-8 compatible, and enforce this in test/run by running the "pep8" tool.
24
    def _create_reports(self, create_inaccessible=False):
2161 by Martin Pitt
Move all test suites out of the code modules into test/test_<module>.py. This avoids having to load it every time the program runs, and also allows running the tests against the installed version of Apport.
25
        '''Create some test reports'''
26
27
        r1 = os.path.join(apport.fileutils.report_dir, 'rep1.crash')
28
        r2 = os.path.join(apport.fileutils.report_dir, 'rep2.crash')
29
30
        with open(r1, 'w') as fd:
31
            fd.write('report 1')
32
        with open(r2, 'w') as fd:
33
            fd.write('report 2')
34
        os.chmod(r1, 0o600)
35
        os.chmod(r2, 0o600)
36
        if create_inaccessible:
37
            ri = os.path.join(apport.fileutils.report_dir, 'inaccessible.crash')
38
            with open(ri, 'w') as fd:
39
                fd.write('inaccessible')
40
            os.chmod(ri, 0)
41
            return [r1, r2, ri]
42
        else:
43
            return [r1, r2]
44
45
    def test_find_package_desktopfile(self):
46
        '''find_package_desktopfile()'''
47
48
        # package without any .desktop file
49
        nodesktop = 'bash'
50
        assert len([f for f in apport.packaging.get_files(nodesktop)
2407 by Martin Pitt
* Fix PEP-8 violations picked up by latest pep8 checker.
51
                    if f.endswith('.desktop')]) == 0
2161 by Martin Pitt
Move all test suites out of the code modules into test/test_<module>.py. This avoids having to load it every time the program runs, and also allows running the tests against the installed version of Apport.
52
2542 by Martin Pitt
* fileutils.py: Ignore .desktop files with NoDisplay=true. (LP: #1048524)
53
        # find a package with one, a package with multiple .desktop files, and
54
        # a package with a NoDisplay .desktop file
2161 by Martin Pitt
Move all test suites out of the code modules into test/test_<module>.py. This avoids having to load it every time the program runs, and also allows running the tests against the installed version of Apport.
55
        onedesktop = None
56
        multidesktop = None
2542 by Martin Pitt
* fileutils.py: Ignore .desktop files with NoDisplay=true. (LP: #1048524)
57
        nodisplay = None
3038 by Martin Pitt
* Fix fileutils.test_find_package_desktopfile test for symlinks and other unowned files in /usr/share/applications/.
58
        found_some = False
2161 by Martin Pitt
Move all test suites out of the code modules into test/test_<module>.py. This avoids having to load it every time the program runs, and also allows running the tests against the installed version of Apport.
59
        for d in os.listdir('/usr/share/applications/'):
60
            if not d.endswith('.desktop'):
61
                continue
2542 by Martin Pitt
* fileutils.py: Ignore .desktop files with NoDisplay=true. (LP: #1048524)
62
            path = os.path.join('/usr/share/applications/', d)
63
            pkg = apport.packaging.get_file_package(path)
3038 by Martin Pitt
* Fix fileutils.test_find_package_desktopfile test for symlinks and other unowned files in /usr/share/applications/.
64
            if pkg is None:
65
                continue
66
            found_some = True
2161 by Martin Pitt
Move all test suites out of the code modules into test/test_<module>.py. This avoids having to load it every time the program runs, and also allows running the tests against the installed version of Apport.
67
            num = len([f for f in apport.packaging.get_files(pkg)
2407 by Martin Pitt
* Fix PEP-8 violations picked up by latest pep8 checker.
68
                       if f.endswith('.desktop')])
2625 by Martin Pitt
* Run tests under LC_CTYPE=C and unset LANG as well, to discover assumptions about UTF-8 locales. Fix the two failing tests.
69
            with open(path, 'rb') as f:
70
                if b'NoDisplay=true' in f.read():
2544 by Martin Pitt
* Fix test_find_package_desktopfile test to not consider packages with only one "NoDisplay=true" .desktop file for the "has one desktop file" test.
71
                    if not nodisplay and num == 1:
2542 by Martin Pitt
* fileutils.py: Ignore .desktop files with NoDisplay=true. (LP: #1048524)
72
                        nodisplay = pkg
2544 by Martin Pitt
* Fix test_find_package_desktopfile test to not consider packages with only one "NoDisplay=true" .desktop file for the "has one desktop file" test.
73
                    continue
2161 by Martin Pitt
Move all test suites out of the code modules into test/test_<module>.py. This avoids having to load it every time the program runs, and also allows running the tests against the installed version of Apport.
74
            if not onedesktop and num == 1:
75
                onedesktop = pkg
76
            elif not multidesktop and num > 1:
77
                multidesktop = pkg
78
2542 by Martin Pitt
* fileutils.py: Ignore .desktop files with NoDisplay=true. (LP: #1048524)
79
            if onedesktop and multidesktop and nodisplay:
2161 by Martin Pitt
Move all test suites out of the code modules into test/test_<module>.py. This avoids having to load it every time the program runs, and also allows running the tests against the installed version of Apport.
80
                break
81
3038 by Martin Pitt
* Fix fileutils.test_find_package_desktopfile test for symlinks and other unowned files in /usr/share/applications/.
82
        self.assertTrue(found_some)
83
2161 by Martin Pitt
Move all test suites out of the code modules into test/test_<module>.py. This avoids having to load it every time the program runs, and also allows running the tests against the installed version of Apport.
84
        if nodesktop:
2542 by Martin Pitt
* fileutils.py: Ignore .desktop files with NoDisplay=true. (LP: #1048524)
85
            self.assertEqual(apport.fileutils.find_package_desktopfile(nodesktop),
86
                             None, 'no-desktop package %s' % nodesktop)
2161 by Martin Pitt
Move all test suites out of the code modules into test/test_<module>.py. This avoids having to load it every time the program runs, and also allows running the tests against the installed version of Apport.
87
        if multidesktop:
2542 by Martin Pitt
* fileutils.py: Ignore .desktop files with NoDisplay=true. (LP: #1048524)
88
            self.assertEqual(apport.fileutils.find_package_desktopfile(multidesktop),
89
                             None, 'multi-desktop package %s' % multidesktop)
2161 by Martin Pitt
Move all test suites out of the code modules into test/test_<module>.py. This avoids having to load it every time the program runs, and also allows running the tests against the installed version of Apport.
90
        if onedesktop:
91
            d = apport.fileutils.find_package_desktopfile(onedesktop)
92
            self.assertNotEqual(d, None, 'one-desktop package %s' % onedesktop)
93
            self.assertTrue(os.path.exists(d))
94
            self.assertTrue(d.endswith('.desktop'))
2542 by Martin Pitt
* fileutils.py: Ignore .desktop files with NoDisplay=true. (LP: #1048524)
95
        if nodisplay:
96
            self.assertEqual(apport.fileutils.find_package_desktopfile(nodisplay), None,
97
                             'NoDisplay package %s' % nodisplay)
2161 by Martin Pitt
Move all test suites out of the code modules into test/test_<module>.py. This avoids having to load it every time the program runs, and also allows running the tests against the installed version of Apport.
98
99
    def test_likely_packaged(self):
100
        '''likely_packaged()'''
101
102
        self.assertEqual(apport.fileutils.likely_packaged('/bin/bash'), True)
103
        self.assertEqual(apport.fileutils.likely_packaged('/usr/bin/foo'), True)
104
        self.assertEqual(apport.fileutils.likely_packaged('/usr/local/bin/foo'), False)
105
        self.assertEqual(apport.fileutils.likely_packaged('/home/test/bin/foo'), False)
106
        self.assertEqual(apport.fileutils.likely_packaged('/tmp/foo'), False)
107
        # ignore crashes in /var/lib (LP#122859, LP#414368)
108
        self.assertEqual(apport.fileutils.likely_packaged('/var/lib/foo'), False)
109
110
    def test_find_file_package(self):
111
        '''find_file_package()'''
112
113
        self.assertEqual(apport.fileutils.find_file_package('/bin/bash'), 'bash')
114
        self.assertEqual(apport.fileutils.find_file_package('/bin/cat'), 'coreutils')
115
        self.assertEqual(apport.fileutils.find_file_package('/nonexisting'), None)
116
117
    def test_seen(self):
118
        '''get_new_reports() and seen_report()'''
119
120
        self.assertEqual(apport.fileutils.get_new_reports(), [])
121
        if os.getuid() == 0:
122
            tr = self._create_reports(True)
123
        else:
2811 by Martin Pitt
* Adjust code to match latest pep8 checker.
124
            tr = [r for r in self._create_reports(True) if 'inaccessible' not in r]
2161 by Martin Pitt
Move all test suites out of the code modules into test/test_<module>.py. This avoids having to load it every time the program runs, and also allows running the tests against the installed version of Apport.
125
        self.assertEqual(set(apport.fileutils.get_new_reports()), set(tr))
126
127
        # now mark them as seen and check again
128
        nr = set(tr)
129
        for r in tr:
130
            self.assertEqual(apport.fileutils.seen_report(r), False)
131
            nr.remove(r)
132
            apport.fileutils.mark_report_seen(r)
133
            self.assertEqual(apport.fileutils.seen_report(r), True)
134
            self.assertEqual(set(apport.fileutils.get_new_reports()), nr)
135
2393.1.9 by Evan Dandrea
Add tests for mark_report_upload and mark_hanging_process.
136
    def test_mark_hanging_process(self):
137
        '''mark_hanging_process()'''
2393.1.15 by Evan Dandrea
Use the ExecutablePath and uid as part of the .hanging path, to avoid potential race conditions.
138
        pr = problem_report.ProblemReport()
139
        pr['ExecutablePath'] = '/bin/bash'
140
        apport.fileutils.mark_hanging_process(pr, '1')
141
        uid = str(os.getuid())
142
        base = '_bin_bash.%s.1.hanging' % uid
143
        expected = os.path.join(apport.fileutils.report_dir, base)
2393.1.9 by Evan Dandrea
Add tests for mark_report_upload and mark_hanging_process.
144
        self.assertTrue(os.path.exists(expected))
145
146
    def test_mark_report_upload(self):
147
        '''mark_report_upload()'''
148
        report = os.path.join(apport.fileutils.report_dir, 'report.crash')
149
        apport.fileutils.mark_report_upload(report)
150
        expected = os.path.join(apport.fileutils.report_dir, 'report.upload')
151
        self.assertTrue(os.path.exists(expected))
152
2538.2.1 by Brian Murray
if there is a 2nd occurrence of a reported crash remove existing .upload and .uploaded files
153
    def test_mark_2nd_report_upload(self):
2540 by Martin Pitt
* fileutils.py, mark_report_upload(): Refresh the .upload stamps if a previous version of the report was already uploaded, but another instance of the problem happened since then. Thanks Brian Murray. (LP: #1084296)
154
        '''mark_report_upload() for a previously uploaded report'''
2538.2.1 by Brian Murray
if there is a 2nd occurrence of a reported crash remove existing .upload and .uploaded files
155
        upload = os.path.join(apport.fileutils.report_dir, 'report.upload')
156
        with open(upload, 'w'):
157
            pass
158
        uploaded = os.path.join(apport.fileutils.report_dir, 'report.uploaded')
159
        with open(uploaded, 'w'):
160
            pass
161
        time.sleep(1)
162
        report = os.path.join(apport.fileutils.report_dir, 'report.crash')
163
        with open(report, 'w'):
164
            pass
165
        time.sleep(1)
166
        apport.fileutils.mark_report_upload(report)
167
        upload_st = os.stat(upload)
168
        report_st = os.stat(report)
169
        self.assertTrue(upload_st.st_mtime > report_st.st_mtime)
170
2161 by Martin Pitt
Move all test suites out of the code modules into test/test_<module>.py. This avoids having to load it every time the program runs, and also allows running the tests against the installed version of Apport.
171
    def test_get_all_reports(self):
172
        '''get_all_reports()'''
173
174
        self.assertEqual(apport.fileutils.get_all_reports(), [])
175
        if os.getuid() == 0:
176
            tr = self._create_reports(True)
177
        else:
2811 by Martin Pitt
* Adjust code to match latest pep8 checker.
178
            tr = [r for r in self._create_reports(True) if 'inaccessible' not in r]
2161 by Martin Pitt
Move all test suites out of the code modules into test/test_<module>.py. This avoids having to load it every time the program runs, and also allows running the tests against the installed version of Apport.
179
        self.assertEqual(set(apport.fileutils.get_all_reports()), set(tr))
180
181
        # now mark them as seen and check again
182
        for r in tr:
183
            apport.fileutils.mark_report_seen(r)
184
185
        self.assertEqual(set(apport.fileutils.get_all_reports()), set(tr))
186
187
    def test_get_system_reports(self):
188
        '''get_all_system_reports() and get_new_system_reports()'''
189
190
        self.assertEqual(apport.fileutils.get_all_reports(), [])
191
        self.assertEqual(apport.fileutils.get_all_system_reports(), [])
192
        if os.getuid() == 0:
193
            tr = self._create_reports(True)
194
            self.assertEqual(set(apport.fileutils.get_all_system_reports()), set(tr))
195
            self.assertEqual(set(apport.fileutils.get_new_system_reports()), set(tr))
196
197
            # now mark them as seen and check again
198
            for r in tr:
199
                apport.fileutils.mark_report_seen(r)
200
201
            self.assertEqual(set(apport.fileutils.get_all_system_reports()), set(tr))
202
            self.assertEqual(set(apport.fileutils.get_new_system_reports()), set([]))
203
        else:
2811 by Martin Pitt
* Adjust code to match latest pep8 checker.
204
            tr = [r for r in self._create_reports(True) if 'inaccessible' not in r]
2161 by Martin Pitt
Move all test suites out of the code modules into test/test_<module>.py. This avoids having to load it every time the program runs, and also allows running the tests against the installed version of Apport.
205
            self.assertEqual(set(apport.fileutils.get_all_system_reports()), set([]))
206
            self.assertEqual(set(apport.fileutils.get_new_system_reports()), set([]))
207
2732 by Martin Pitt
* fileutils.py, get_all_system_reports(): Filter out "guest..." users, they might have a system UID. (LP: #1250679)
208
    @patch.object(os, 'stat')
209
    @patch.object(pwd, 'getpwuid')
210
    def test_get_system_reports_guest(self, *args):
211
        '''get_all_system_reports() filters out reports from guest user'''
212
213
        self._create_reports()
214
215
        os.stat.return_value.st_size = 1000
216
        os.stat.return_value.st_uid = 123
217
        pwd.getpwuid.return_value.pw_name = 'guest_tmp987'
218
        self.assertEqual(apport.fileutils.get_all_system_reports(), [])
219
2694 by Martin Pitt
* fileutils.py, get_{new,all}_reports(): Don't consider reports which are readable, but not writable. (LP: #1098844)
220
    def test_unwritable_report(self):
221
        '''get_all_reports() and get_new_reports() for unwritable report'''
222
223
        self.assertEqual(apport.fileutils.get_all_reports(), [])
224
        self.assertEqual(apport.fileutils.get_all_system_reports(), [])
225
226
        r = os.path.join(apport.fileutils.report_dir, 'unwritable.crash')
227
        with open(r, 'w') as fd:
228
            fd.write('unwritable')
229
        os.chmod(r, 0o444)
230
231
        if os.getuid() == 0:
232
            self.assertEqual(set(apport.fileutils.get_new_reports()), set([r]))
233
            self.assertEqual(set(apport.fileutils.get_all_reports()), set([r]))
234
        else:
235
            self.assertEqual(set(apport.fileutils.get_new_reports()), set())
236
            self.assertEqual(set(apport.fileutils.get_all_reports()), set())
237
2161 by Martin Pitt
Move all test suites out of the code modules into test/test_<module>.py. This avoids having to load it every time the program runs, and also allows running the tests against the installed version of Apport.
238
    def test_delete_report(self):
239
        '''delete_report()'''
240
241
        tr = self._create_reports()
242
243
        while tr:
244
            self.assertEqual(set(apport.fileutils.get_all_reports()), set(tr))
245
            apport.fileutils.delete_report(tr.pop())
246
247
    def test_get_recent_crashes(self):
248
        '''get_recent_crashes()'''
249
250
        # incomplete fields
2302 by Martin Pitt
problem_report.py: Fix for Python 3
251
        r = BytesIO(b'''ProblemType: Crash''')
2161 by Martin Pitt
Move all test suites out of the code modules into test/test_<module>.py. This avoids having to load it every time the program runs, and also allows running the tests against the installed version of Apport.
252
        self.assertEqual(apport.fileutils.get_recent_crashes(r), 0)
253
2302 by Martin Pitt
problem_report.py: Fix for Python 3
254
        r = BytesIO(b'''ProblemType: Crash
2161 by Martin Pitt
Move all test suites out of the code modules into test/test_<module>.py. This avoids having to load it every time the program runs, and also allows running the tests against the installed version of Apport.
255
Date: Wed Aug 01 00:00:01 1990''')
256
        self.assertEqual(apport.fileutils.get_recent_crashes(r), 0)
257
258
        # ancient report
2302 by Martin Pitt
problem_report.py: Fix for Python 3
259
        r = BytesIO(b'''ProblemType: Crash
2161 by Martin Pitt
Move all test suites out of the code modules into test/test_<module>.py. This avoids having to load it every time the program runs, and also allows running the tests against the installed version of Apport.
260
Date: Wed Aug 01 00:00:01 1990
261
CrashCounter: 3''')
262
        self.assertEqual(apport.fileutils.get_recent_crashes(r), 0)
263
264
        # old report (one day + one hour ago)
2302 by Martin Pitt
problem_report.py: Fix for Python 3
265
        date = time.ctime(time.mktime(time.localtime()) - 25 * 3600)
266
        r = BytesIO(b'''ProblemType: Crash
267
Date: ''' + date.encode() + b'''
268
CrashCounter: 3''')
2161 by Martin Pitt
Move all test suites out of the code modules into test/test_<module>.py. This avoids having to load it every time the program runs, and also allows running the tests against the installed version of Apport.
269
        self.assertEqual(apport.fileutils.get_recent_crashes(r), 0)
270
271
        # current report (one hour ago)
2302 by Martin Pitt
problem_report.py: Fix for Python 3
272
        date = time.ctime(time.mktime(time.localtime()) - 3600)
273
        r = BytesIO(b'''ProblemType: Crash
274
Date: ''' + date.encode() + b'''
275
CrashCounter: 3''')
2161 by Martin Pitt
Move all test suites out of the code modules into test/test_<module>.py. This avoids having to load it every time the program runs, and also allows running the tests against the installed version of Apport.
276
        self.assertEqual(apport.fileutils.get_recent_crashes(r), 3)
277
3008 by Martin Pitt
SECURITY FIX: Fix all writers of report files to open the report file exclusively
278
    def test_make_report_file(self):
279
        '''make_report_file()'''
2161 by Martin Pitt
Move all test suites out of the code modules into test/test_<module>.py. This avoids having to load it every time the program runs, and also allows running the tests against the installed version of Apport.
280
281
        pr = problem_report.ProblemReport()
3008 by Martin Pitt
SECURITY FIX: Fix all writers of report files to open the report file exclusively
282
        self.assertRaises(ValueError, apport.fileutils.make_report_file, pr)
2161 by Martin Pitt
Move all test suites out of the code modules into test/test_<module>.py. This avoids having to load it every time the program runs, and also allows running the tests against the installed version of Apport.
283
284
        pr['Package'] = 'bash 1'
3008 by Martin Pitt
SECURITY FIX: Fix all writers of report files to open the report file exclusively
285
        with apport.fileutils.make_report_file(pr) as f:
286
            if sys.version >= '3':
287
                path = f.name
288
            else:
289
                path = os.path.join(apport.fileutils.report_dir, os.listdir(apport.fileutils.report_dir)[0])
290
            self.assertTrue(path.startswith('%s/bash' % apport.fileutils.report_dir), path)
291
            os.unlink(path)
292
2277 by Martin Pitt
* Fix the whole code to be PEP-8 compatible, and enforce this in test/run by running the "pep8" tool.
293
        pr['ExecutablePath'] = '/bin/bash'
3008 by Martin Pitt
SECURITY FIX: Fix all writers of report files to open the report file exclusively
294
        with apport.fileutils.make_report_file(pr) as f:
295
            if sys.version >= '3':
296
                path = f.name
297
            else:
298
                path = os.path.join(apport.fileutils.report_dir, os.listdir(apport.fileutils.report_dir)[0])
299
            self.assertTrue(path.startswith('%s/_bin_bash' % apport.fileutils.report_dir), path)
300
301
        # file exists already, should fail now
302
        self.assertRaises(OSError, apport.fileutils.make_report_file, pr)
303
304
        # should still fail if it's a dangling symlink
305
        os.unlink(path)
306
        os.symlink(os.path.join(apport.fileutils.report_dir, 'pwned'), path)
307
        self.assertRaises(OSError, apport.fileutils.make_report_file, pr)
2161 by Martin Pitt
Move all test suites out of the code modules into test/test_<module>.py. This avoids having to load it every time the program runs, and also allows running the tests against the installed version of Apport.
308
309
    def test_check_files_md5(self):
310
        '''check_files_md5()'''
311
312
        f1 = os.path.join(apport.fileutils.report_dir, 'test 1.txt')
313
        f2 = os.path.join(apport.fileutils.report_dir, 'test:2.txt')
314
        sumfile = os.path.join(apport.fileutils.report_dir, 'sums.txt')
315
        with open(f1, 'w') as fd:
316
            fd.write('Some stuff')
317
        with open(f2, 'w') as fd:
318
            fd.write('More stuff')
319
        # use one relative and one absolute path in checksums file
320
        with open(sumfile, 'w') as fd:
321
            fd.write('''2e41290da2fa3f68bd3313174467e3b5  %s
322
f6423dfbc4faf022e58b4d3f5ff71a70  %s
323
''' % (f1[1:], f2))
324
        self.assertEqual(apport.fileutils.check_files_md5(sumfile), [], 'correct md5sums')
325
326
        with open(f1, 'w') as fd:
327
            fd.write('Some stuff!')
328
        self.assertEqual(apport.fileutils.check_files_md5(sumfile), [f1[1:]], 'file 1 wrong')
329
        with open(f2, 'w') as fd:
330
            fd.write('More stuff!')
331
        self.assertEqual(apport.fileutils.check_files_md5(sumfile), [f1[1:], f2], 'files 1 and 2 wrong')
332
        with open(f1, 'w') as fd:
333
            fd.write('Some stuff')
334
        self.assertEqual(apport.fileutils.check_files_md5(sumfile), [f2], 'file 2 wrong')
335
336
    def test_get_config(self):
337
        '''get_config()'''
338
339
        # nonexisting
340
        apport.fileutils._config_file = '/nonexisting'
341
        self.assertEqual(apport.fileutils.get_config('main', 'foo'), None)
342
        self.assertEqual(apport.fileutils.get_config('main', 'foo', 'moo'), 'moo')
2277 by Martin Pitt
* Fix the whole code to be PEP-8 compatible, and enforce this in test/run by running the "pep8" tool.
343
        apport.fileutils.get_config.config = None  # trash cache
2161 by Martin Pitt
Move all test suites out of the code modules into test/test_<module>.py. This avoids having to load it every time the program runs, and also allows running the tests against the installed version of Apport.
344
345
        # empty
346
        f = tempfile.NamedTemporaryFile()
347
        apport.fileutils._config_file = f.name
348
        self.assertEqual(apport.fileutils.get_config('main', 'foo'), None)
349
        self.assertEqual(apport.fileutils.get_config('main', 'foo', 'moo'), 'moo')
2277 by Martin Pitt
* Fix the whole code to be PEP-8 compatible, and enforce this in test/run by running the "pep8" tool.
350
        apport.fileutils.get_config.config = None  # trash cache
2161 by Martin Pitt
Move all test suites out of the code modules into test/test_<module>.py. This avoids having to load it every time the program runs, and also allows running the tests against the installed version of Apport.
351
352
        # nonempty
353
        f.write(b'[main]\none=1\ntwo = TWO\nb1 = 1\nb2=False\n[spethial]\none= 99\n')
354
        f.flush()
355
        self.assertEqual(apport.fileutils.get_config('main', 'foo'), None)
356
        self.assertEqual(apport.fileutils.get_config('main', 'foo', 'moo'), 'moo')
357
        self.assertEqual(apport.fileutils.get_config('main', 'one'), '1')
358
        self.assertEqual(apport.fileutils.get_config('main', 'one', default='moo'), '1')
359
        self.assertEqual(apport.fileutils.get_config('main', 'two'), 'TWO')
360
        self.assertEqual(apport.fileutils.get_config('main', 'b1', bool=True), True)
361
        self.assertEqual(apport.fileutils.get_config('main', 'b2', bool=True), False)
362
        self.assertEqual(apport.fileutils.get_config('main', 'b3', bool=True), None)
363
        self.assertEqual(apport.fileutils.get_config('main', 'b3', default=False, bool=True), False)
364
        self.assertEqual(apport.fileutils.get_config('spethial', 'one'), '99')
365
        self.assertEqual(apport.fileutils.get_config('spethial', 'two'), None)
366
        self.assertEqual(apport.fileutils.get_config('spethial', 'one', 'moo'), '99')
367
        self.assertEqual(apport.fileutils.get_config('spethial', 'nope', 'moo'), 'moo')
2277 by Martin Pitt
* Fix the whole code to be PEP-8 compatible, and enforce this in test/run by running the "pep8" tool.
368
        apport.fileutils.get_config.config = None  # trash cache
2161 by Martin Pitt
Move all test suites out of the code modules into test/test_<module>.py. This avoids having to load it every time the program runs, and also allows running the tests against the installed version of Apport.
369
370
        f.close()
371
2587 by Martin Pitt
* Move shared_libraries() and links_with_shared_library() from hookutils into fileutils, so that we can use it from apport-valgrind. Thanks to Kyle Nitzsche for the initial patch.
372
    def test_shared_libraries(self):
373
        '''shared_libraries()'''
374
375
        libs = apport.fileutils.shared_libraries(sys.executable)
376
        self.assertGreater(len(libs), 3)
377
        self.assertTrue('libc.so.6' in libs, libs)
2593 by Martin Pitt
* fileutils.py, shared_libraries(): Return a "name → path" dict instead of just a set of names. Thanks Kyle Nitzsche.
378
        self.assertTrue('libc.so.6' in libs['libc.so.6'], libs['libc.so.6'])
379
        self.assertTrue(os.path.exists(libs['libc.so.6']))
2589 by Martin Pitt
* fileutils.shared_libraries(): Filter out virtual "linux-vdso" from result. Thanks Kyle Nitzsche.
380
        for l in libs:
381
            self.assertFalse('vdso' in l, libs)
2593 by Martin Pitt
* fileutils.py, shared_libraries(): Return a "name → path" dict instead of just a set of names. Thanks Kyle Nitzsche.
382
            self.assertTrue(os.path.exists(libs[l]))
2587 by Martin Pitt
* Move shared_libraries() and links_with_shared_library() from hookutils into fileutils, so that we can use it from apport-valgrind. Thanks to Kyle Nitzsche for the initial patch.
383
2593 by Martin Pitt
* fileutils.py, shared_libraries(): Return a "name → path" dict instead of just a set of names. Thanks Kyle Nitzsche.
384
        self.assertEqual(apport.fileutils.shared_libraries('/non/existing'), {})
385
        self.assertEqual(apport.fileutils.shared_libraries('/etc'), {})
386
        self.assertEqual(apport.fileutils.shared_libraries('/etc/passwd'), {})
2587 by Martin Pitt
* Move shared_libraries() and links_with_shared_library() from hookutils into fileutils, so that we can use it from apport-valgrind. Thanks to Kyle Nitzsche for the initial patch.
387
388
    def test_links_with_shared_library(self):
389
        '''links_with_shared_library()'''
390
391
        self.assertTrue(apport.fileutils.links_with_shared_library(sys.executable, 'libc'))
392
        self.assertTrue(apport.fileutils.links_with_shared_library(sys.executable, 'libc.so.6'))
393
        self.assertFalse(apport.fileutils.links_with_shared_library(sys.executable, 'libc.so.7'))
394
        self.assertFalse(apport.fileutils.links_with_shared_library(sys.executable, 'libd'))
395
        self.assertFalse(apport.fileutils.links_with_shared_library('/non/existing', 'libc'))
396
        self.assertFalse(apport.fileutils.links_with_shared_library('/etc', 'libc'))
397
        self.assertFalse(apport.fileutils.links_with_shared_library('/etc/passwd', 'libc'))
398
3110 by Martin Pitt
Fix PEP-8 errors with latest pycodestyle
399
2161 by Martin Pitt
Move all test suites out of the code modules into test/test_<module>.py. This avoids having to load it every time the program runs, and also allows running the tests against the installed version of Apport.
400
if __name__ == '__main__':
401
    unittest.main()