summaryrefslogtreecommitdiffstats
path: root/python/macholib/macholib_tests/test_command_line.py
blob: a9cfd6f10a3b1f2f294954961b4109426edc86a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from macholib import macho_dump
from macholib import macho_find
from macholib import _cmdline
from macholib import util

import sys
import shutil
import os

if sys.version_info[:2] <= (2,6):
    import unittest2 as unittest
else:
    import unittest

try:
    from StringIO import StringIO
except ImportError:
    from io import StringIO

class TestCmdLine (unittest.TestCase):

    # This test is no longer valid:
    def no_test_main_is_shared(self):
        self.assertTrue(macho_dump.main is _cmdline.main)
        self.assertTrue(macho_find.main is _cmdline.main)

    def test_check_file(self):
        record = []
        def record_cb(fp, path):
            record.append((fp, path))

        self.assertEqual(_cmdline.check_file(sys.stdout, '/bin/sh', record_cb), 0)
        self.assertEqual(record, [(sys.stdout, '/bin/sh')])

        saved_stderr = sys.stderr
        saved_argv = sys.argv
        try:
            sys.stderr = StringIO()
            sys.argv = ['macho_test']

            record[:] = []
            self.assertEqual(_cmdline.check_file(sys.stdout, '/bin/no-shell', record_cb), 1)
            self.assertEqual(record, [])
            self.assertEqual(sys.stderr.getvalue(), "macho_test: /bin/no-shell: No such file or directory\n")
            self.assertEqual(record, [])

            shutil.copy('/bin/sh', 'test.exec')
            os.chmod('test.exec', 0)

            sys.stderr = StringIO()
            self.assertEqual(_cmdline.check_file(sys.stdout, 'test.exec', record_cb), 1)
            self.assertEqual(record, [])
            self.assertEqual(sys.stderr.getvalue(), "macho_test: test.exec: [Errno 13] Permission denied: 'test.exec'\n")
            self.assertEqual(record, [])


        finally:
            sys.stderr = sys.stderr
            sys.argv = saved_argv
            if os.path.exists('test.exec'):
                os.unlink('test.exec')

    def test_shared_main(self):

        saved_stderr = sys.stderr
        saved_argv = sys.argv
        try:
            sys.stderr = StringIO()

            sys.argv = ['macho_tool']

            self.assertEqual(_cmdline.main(lambda *args: None), 1)
            self.assertEqual(sys.stderr.getvalue(), 'Usage: macho_tool filename...\n')

            names = []
            def record_names(fp, name):
                self.assertEqual(fp, sys.stdout)
                names.append(name)


            sys.stderr = StringIO()
            sys.argv = ['macho_tool', '/bin/sh']
            self.assertEqual(_cmdline.main(record_names), 0)
            self.assertEqual(sys.stderr.getvalue(), '')
            self.assertEqual(names, ['/bin/sh'])

            names = []
            sys.stderr = StringIO()
            sys.argv = ['macho_tool', '/bin/sh', '/bin/ls']
            self.assertEqual(_cmdline.main(record_names), 0)
            self.assertEqual(sys.stderr.getvalue(), '')
            self.assertEqual(names, ['/bin/sh', '/bin/ls'])

            names = []
            sys.stderr = StringIO()
            sys.argv = ['macho_tool', '/bin']
            self.assertEqual(_cmdline.main(record_names), 0)
            self.assertEqual(sys.stderr.getvalue(), '')
            names.sort()
            dn = '/bin'
            real_names = [
                    os.path.join(dn, fn) for fn in os.listdir(dn)
                    if util.is_platform_file(os.path.join(dn, fn)) ]
            real_names.sort()

            self.assertEqual(names, real_names)

        finally:
            sys.stderr = saved_stderr
            sys.argv = saved_argv

    def test_macho_find(self):
        fp = StringIO()
        macho_find.print_file(fp, "file1")
        macho_find.print_file(fp, "file2")
        self.assertEqual(fp.getvalue(), "file1\nfile2\n")

    def test_macho_dump(self):
        fp = StringIO()
        macho_dump.print_file(fp, "/bin/sh")
        lines = fp.getvalue().splitlines()

        self.assertEqual(lines[0], "/bin/sh")
        self.assertTrue(len(lines) > 3)

        self.assertEqual(lines[-1], '')
        del lines[-1]

        idx = 1
        while idx < len(lines):
            self.assertTrue(lines[idx].startswith('    [MachOHeader endian'))
            idx+=1

            lc = 0
            while idx < len(lines):
                if not lines[idx].startswith('\t'):
                    break

                lc +=1
                self.assertTrue(os.path.exists(lines[idx].lstrip()))
                idx += 1

            self.assertTrue(lc > 1)


if __name__ == "__main__":
    unittest.main()