summaryrefslogtreecommitdiff
path: root/suite/unit.py
blob: fe2c4b8c7f0ec20c854bf7de87fdb08ed5affe0c (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/usr/bin/env python
# This is an auto generated unit test file do not edit directly
import os
import sys
import unittest
import pickle
import zipfile
import testcommon
import api_test
import difflib
from collections import Counter

global verbose
verbose = False

class TestBinaryNinjaAPI(unittest.TestCase):
    # Returns a tuple of:
    #   bool   : Two lists are equal
    #   string : The string diff
    # Args:
    #   list
    #   list   : (compare list one vs list two)
    #   string : anything additional wanted to be printed before the string diff
    #   bool   : the ordering of the items in the two lists must be the same
    def report(self, oracle, test, firstText='', strictOrdering = False):
        stringDiff = ""

        equality = False
        if not strictOrdering:
            equality = (Counter(oracle) == Counter(test))
        else:
            equality = (oracle == test)

        if equality:
            return (True, '')
        elif not strictOrdering:
            try:
                for elem in oracle:
                    test.remove(elem)
                    oracle.remove(elem)  # If it's not in the test, it won't get here!
            except ValueError:
                pass

        differ = difflib.Differ(charjunk=difflib.IS_CHARACTER_JUNK)
        skipped_lines = 0
        for delta in differ.compare(oracle, test):
            if delta[0] == ' ':
                skipped_lines += 1
                continue
            if skipped_lines > 0:
                stringDiff += "<---" + str(skipped_lines) + ' same lines--->\n'
                skipped_lines = 0
            delta = delta.replace('\n', '')
            stringDiff += delta + '\n'

        stringDiffList = stringDiff.split('\n')

        if len(stringDiffList) > 10:
            if not verbose:
                stringDiff = '\n'.join(line if len(line) <= 100 else line[:100] + "...and " + str(len(line) - 100) + " more characters" for line in stringDiffList[:10])
                stringDiff += '\n\n### And ' + str(len(stringDiffList)) + " more lines, use '-v' to show ###"
        elif not verbose:
            stringDiff = '\n'.join(line if len(line) <= 100 else line[:100] + "...and " + str(len(line) - 100) + " more characters" for line in stringDiffList)
        stringDiff = '\n\n' + firstText + stringDiff
        return (equality, stringDiff)

    @classmethod
    def setUpClass(self):
        self.builder = testcommon.TestBuilder("suite/binaries/test_corpus")
        try:
            # Python 2 does not have the encodings option
            self.oracle_test_data = pickle.load(open(os.path.join("suite", "oracle.pkl"), "rb"), encoding='charmap')
        except TypeError:
            self.oracle_test_data = pickle.load(open(os.path.join("suite", "oracle.pkl"), "r"))
        self.verifybuilder = testcommon.VerifyBuilder("suite/binaries/test_corpus")

    def run_binary_test(self, testfile):
        testname = None
        with zipfile.ZipFile(testfile, "r") as zf:
            testname = zf.namelist()[0]
            zf.extractall()

        self.assertTrue(os.path.exists(testname + ".pkl"), "Test pickle doesn't exist")
        try:
            # Python 2 does not have the encodings option
            binary_oracle = pickle.load(open(testname + ".pkl", "rb"), encoding='charmap')
        except TypeError:
            binary_oracle = pickle.load(open(testname + ".pkl", "r"))

        test_builder = testcommon.BinaryViewTestBuilder(testname, "suite/binaries/test_corpus")
        for method in test_builder.methods():
            test = getattr(test_builder, method)()
            oracle = binary_oracle[method]
            if test == oracle:
                continue

            result = getattr(test_builder, method).__doc__
            result += ":\n"
            report = self.report(oracle, test, result)
            self.assertTrue(report[0], report[1])  # Test does not agree with oracle
        os.unlink(testname)

    def test_Architecture(self):
        oracle = self.oracle_test_data['test_Architecture']
        test = self.builder.test_Architecture()
        report = self.report(oracle, test)
        self.assertTrue(report[0], report[1])  # Test does not agree with oracle

    def test_Architecture_list(self):
        oracle = self.oracle_test_data['test_Architecture_list']
        test = self.builder.test_Architecture_list()
        report = self.report(oracle, test)
        self.assertTrue(report[0], report[1])  # Test does not agree with oracle

    def test_Assemble(self):
        oracle = self.oracle_test_data['test_Assemble']
        test = self.builder.test_Assemble()
        report = self.report(oracle, test)
        self.assertTrue(report[0], report[1])  # Test does not agree with oracle

    def test_BinaryViewType_list(self):
        oracle = self.oracle_test_data['test_BinaryViewType_list']
        test = self.builder.test_BinaryViewType_list()
        report = self.report(oracle, test)
        self.assertTrue(report[0], report[1])  # Test does not agree with oracle

    def test_Enumeration(self):
        oracle = self.oracle_test_data['test_Enumeration']
        test = self.builder.test_Enumeration()
        report = self.report(oracle, test)
        self.assertTrue(report[0], report[1])  # Test does not agree with oracle

    def test_Function(self):
        oracle = self.oracle_test_data['test_Function']
        test = self.builder.test_Function()
        report = self.report(oracle, test)
        self.assertTrue(report[0], report[1])  # Test does not agree with oracle

    def test_Plugin_bin_info(self):
        oracle = self.oracle_test_data['test_Plugin_bin_info']
        test = self.builder.test_Plugin_bin_info()
        report = self.report(oracle, test)
        self.assertTrue(report[0], report[1])  # Test does not agree with oracle

    def test_Struct(self):
        oracle = self.oracle_test_data['test_Struct']
        test = self.builder.test_Struct()
        report = self.report(oracle, test)
        self.assertTrue(report[0], report[1])  # Test does not agree with oracle

    def test_Types(self):
        oracle = self.oracle_test_data['test_Types']
        test = self.builder.test_Types()
        report = self.report(oracle, test)
        self.assertTrue(report[0], report[1])  # Test does not agree with oracle

    def test_events(self):
        oracle = self.oracle_test_data['test_events']
        test = self.builder.test_events()
        report = self.report(oracle, test)
        self.assertTrue(report[0], report[1])  # Test does not agree with oracle

    def test_linear_disassembly(self):
        oracle = self.oracle_test_data['test_linear_disassembly']
        test = self.builder.test_linear_disassembly()
        report = self.report(oracle, test)
        self.assertTrue(report[0], report[1])  # Test does not agree with oracle

    def test_low_il_stack(self):
        oracle = self.oracle_test_data['test_low_il_stack']
        test = self.builder.test_low_il_stack()
        report = self.report(oracle, test)
        self.assertTrue(report[0], report[1])  # Test does not agree with oracle

    def test_med_il_stack(self):
        oracle = self.oracle_test_data['test_med_il_stack']
        test = self.builder.test_med_il_stack()
        report = self.report(oracle, test)
        self.assertTrue(report[0], report[1])  # Test does not agree with oracle

    def test_partial_register_dataflow(self):
        oracle = self.oracle_test_data['test_partial_register_dataflow']
        test = self.builder.test_partial_register_dataflow()
        report = self.report(oracle, test)
        self.assertTrue(report[0], report[1])  # Test does not agree with oracle

    def test_verify_BNDB_round_trip(self):
        self.assertTrue(self.verifybuilder.test_verify_BNDB_round_trip(), self.test_verify_BNDB_round_trip.__doc__)

    def test_binary___aliased_jumptable(self):
        self.run_binary_test('suite/binaries/test_corpus/aliased_jumptable.zip')

    def test_binary___byte_jump_table(self):
        self.run_binary_test('suite/binaries/test_corpus/byte_jump_table.zip')

    def test_binary___duff(self):
        self.run_binary_test('suite/binaries/test_corpus/duff.zip')

    def test_binary___helloworld(self):
        self.run_binary_test('suite/binaries/test_corpus/helloworld.zip')

    def test_binary___helloworld_armeb(self):
        self.run_binary_test('suite/binaries/test_corpus/helloworld_armeb.zip')

    def test_binary___integer_test(self):
        self.run_binary_test('suite/binaries/test_corpus/integer_test.zip')

    def test_binary___interprocedural_alias(self):
        self.run_binary_test('suite/binaries/test_corpus/interprocedural_alias.zip')

    def test_binary___jump_loop(self):
        self.run_binary_test('suite/binaries/test_corpus/jump_loop.zip')

    def test_binary___jumptable_aarch64(self):
        self.run_binary_test('suite/binaries/test_corpus/jumptable_aarch64.zip')

    def test_binary___jumptable_mips32(self):
        self.run_binary_test('suite/binaries/test_corpus/jumptable_mips32.zip')

    def test_binary___jumptable_multiple_indirect(self):
        self.run_binary_test('suite/binaries/test_corpus/jumptable_multiple_indirect.zip')

    def test_binary___jumptable_no_range_check(self):
        self.run_binary_test('suite/binaries/test_corpus/jumptable_no_range_check.zip')

    def test_binary___jumptable_reordered(self):
        self.run_binary_test('suite/binaries/test_corpus/jumptable_reordered.zip')

    def test_binary___jumptable_x86(self):
        self.run_binary_test('suite/binaries/test_corpus/jumptable_x86.zip')

    def test_binary___jumptable_x86_64(self):
        self.run_binary_test('suite/binaries/test_corpus/jumptable_x86_64.zip')

    def test_binary___loop_constant_propagate(self):
        self.run_binary_test('suite/binaries/test_corpus/loop_constant_propagate.zip')

    def test_binary___ls(self):
        self.run_binary_test('suite/binaries/test_corpus/ls.zip')

    def test_binary___md5(self):
        self.run_binary_test('suite/binaries/test_corpus/md5.zip')

    def test_binary___partial_register_dataflow(self):
        self.run_binary_test('suite/binaries/test_corpus/partial_register_dataflow.zip')

    def test_binary___pe_thumb(self):
        self.run_binary_test('suite/binaries/test_corpus/pe_thumb.zip')

    def test_binary___quick3dcoreplugin_dll(self):
        self.run_binary_test('suite/binaries/test_corpus/quick3dcoreplugin.dll.zip')

    def test_binary___rangecheck(self):
        self.run_binary_test('suite/binaries/test_corpus/rangecheck.zip')

    def test_binary___switch_linux_ppc_le_32(self):
        self.run_binary_test('suite/binaries/test_corpus/switch_linux_ppc_le_32.zip')

    def test_binary___x87(self):
        self.run_binary_test('suite/binaries/test_corpus/x87.zip')


if __name__ == "__main__":
    if len(sys.argv) > 1:
        if sys.argv[1] == '-v' or sys.argv[1] == '-V' or sys.argv[1] == '--verbose':
            verbose = True

    test_suite = unittest.defaultTestLoader.loadTestsFromModule(api_test)
    test_suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestBinaryNinjaAPI))
    runner = unittest.TextTestRunner(verbosity=2)
    runner.run(test_suite)