-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_hex0tobin.py
executable file
·196 lines (163 loc) · 6.73 KB
/
test_hex0tobin.py
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
# Copyright (C) 2019 Mark Jenkins <[email protected]>
# This file is part of knightpies
#
# knightpies is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# knightpies is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with knightpies. If not, see <http://www.gnu.org/licenses/>.
from unittest import TestCase, skipIf
from io import BytesIO
from os.path import exists
from stage0dir import get_stage0_file
from hex0tobin import write_binary_filefd_from_hex0_filefd
from knightvm_minimal import load_hex_program
from .hexcommon import (
Hex256SumMatch, HexCommon, Encoding_rom_256_Common,
make_get_sha256sum_of_file_after_encode,
TestHexKnightExecuteCommon,
CommonStage1HexEncode,
)
from .stage0 import (
STAGE_0_MONITOR_HEX_FILEPATH, STAGE_0_MONITOR_RELATIVE_PATH,
STAGE_0_HEX0_ASSEMBLER_RELATIVE_PATH, STAGE_0_HEX0_ASSEMBLER_FILEPATH,
STAGE_0_HEX1_ASSEMBLER_RELATIVE_PATH,
)
from .util import make_optimize_and_register_size_variations
get_sha256sum_of_file_after_hex0_encode = \
make_get_sha256sum_of_file_after_encode(
write_binary_filefd_from_hex0_filefd)
ADDITIONAL_SHA256SUMS = {
filename: get_sha256sum_of_file_after_hex0_encode(
get_stage0_file(filename) )
for filename in (
'stage1/more.hex0',
'stage1/dehex.hex0',
) # end tuple fed to for filename in
}
class Hex0Common(HexCommon):
encoding_rom_filename = STAGE_0_MONITOR_HEX_FILEPATH
rom_encode_func = staticmethod(write_binary_filefd_from_hex0_filefd)
class Test_monitor_256Sum(Hex0Common, Encoding_rom_256_Common):
sha256sumfilename = 'roms/stage0_monitor'
class Test_hex_assembler0_ROM_256Sum(Hex0Common, Encoding_rom_256_Common):
encoding_rom_filename = STAGE_0_HEX0_ASSEMBLER_FILEPATH
sha256sumfilename = 'roms/stage1_assembler-0'
class Hex0EncodeSpecificFile(TestCase):
def compute_sha256_digest(self):
return get_sha256sum_of_file_after_hex0_encode(
self.sha256_compare_filename)
class Test_dehex_256Sum(
Hex256SumMatch, Hex0EncodeSpecificFile):
sha256sumfilename = 'roms/DEHEX'
sha256_compare_filename = get_stage0_file('stage1/dehex.hex0')
class TestHex0KnightExecuteCommon(Hex0Common, TestHexKnightExecuteCommon):
# necessary disambiguation because both Hex0Common and
# TestHexKnightExecuteCommon subclass HexCommon
def setUp(self):
# this does:
# HexCommon.setUp(self)
# self.setup_stack_and_tmp_files()
TestHexKnightExecuteCommon.setUp(self)
# necessary disambiguation because both Hex0Common and
# TestHexKnightExecuteCommon subclass HexCommon
def tearDown(self):
# this does
# HexCommon.tearDown(self)
# self.remove_tmp_files()
TestHexKnightExecuteCommon.tearDown(self)
def load_encoding_rom(self, vm):
load_hex_program(vm, self.encoding_rom_filename )
def get_tape1_file_path(self, input_file_fd):
return self.tape_01_temp_file_path
def get_stdin_for_vm(self, input_file_fd):
return input_file_fd
def get_output_file_path(self):
return self.tape_01_temp_file_path
def execute_test_hex0_load_against_computed_SHA256SUM(self, filename):
self.execute_test_hex0_load_against_computed_SHA256SUM_dict(
filename, ADDITIONAL_SHA256SUMS)
def execute_test_hex0_load_against_computed_SHA256SUM_dict(
self, filename, sha256sumdict):
self.execute_test_hex_load(
get_stage0_file(filename),
sha256sumdict[filename]
)
class TestStage0Monitorexecute(TestHex0KnightExecuteCommon):
def test_stage0_monitor_encodes_self(self):
self.execute_test_hex_load_published_sha256(
STAGE_0_MONITOR_RELATIVE_PATH,
"roms/stage0_monitor")
def test_encode_stage1_assembler_0(self):
self.execute_test_hex_load_published_sha256(
STAGE_0_HEX0_ASSEMBLER_RELATIVE_PATH,
"roms/stage1_assembler-0")
def test_encode_stage1_assembler_1(self):
self.execute_test_hex_load_published_sha256(
STAGE_0_HEX1_ASSEMBLER_RELATIVE_PATH,
"roms/stage1_assembler-1")
def test_encode_stage1_more(self):
self.execute_test_hex0_load_against_computed_SHA256SUM(
'stage1/more.hex0')
def test_encode_stage1_dehex(self):
self.execute_test_hex0_load_against_computed_SHA256SUM(
'stage1/dehex.hex0')
(TestHex0ToBinOptimize,
TestHex0ToBin64,
TestHex0ToBin64Optimize,
TestHex0ToBin16,
TestHex0ToBin16Optimize) = \
make_optimize_and_register_size_variations(TestStage0Monitorexecute)
TestHex0ToBin64.stack_size_multiplier = 2
assert( TestHex0ToBin64Optimize.stack_size_multiplier == 2 )
class TestStage1Hex0Encode(CommonStage1HexEncode, TestHex0KnightExecuteCommon):
def test_encode_stage1_hex0_encodes_self(self):
self.execute_test_hex_load_published_sha256(
STAGE_0_HEX0_ASSEMBLER_RELATIVE_PATH,
"roms/stage1_assembler-0",
)
def test_encode_stage0_monitor_with_stage1_hex0(self):
self.execute_test_hex_load_published_sha256(
STAGE_0_MONITOR_HEX_FILEPATH,
"roms/stage0_monitor",
)
def test_encode_stage1_hex1_with_stage1_hex0(self):
self.execute_test_hex_load_published_sha256(
STAGE_0_HEX1_ASSEMBLER_RELATIVE_PATH,
"roms/stage1_assembler-1",
)
def test_encode_stage1_more(self):
self.execute_test_hex_load(
get_stage0_file('stage1/more.hex0'),
ADDITIONAL_SHA256SUMS['stage1/more.hex0']
)
def test_encode_stage1_dehex(self):
filename = 'stage1/dehex.hex0'
self.execute_test_hex_load(
get_stage0_file(filename),
ADDITIONAL_SHA256SUMS[filename]
)
(TestStage1Hex0ToBin32Optimize,
TestStage1Hex0ToBin64,
TestStage1Hex0ToBin64Optimize,
TestStage1Hex0ToBin16,
TestStage1Hex0ToBin16Optimize,
) = make_optimize_and_register_size_variations(TestStage1Hex0Encode)
TestStage1Hex0ToBin64.stack_size_multiplier = 2
assert( TestStage1Hex0ToBin64Optimize.stack_size_multiplier == 2 )
if __name__ == '__main__':
# to invoke, run
# $ python3 -m knighttests.test_hex0tobin
# or
# $ ./runtestmodule.py knighttests/test_hex0tobin.py
#
# direct invocation like ./test_hex0tobin.py will not work
from unittest import main
main()