1
0

Tests now fail correctly
Some checks failed
Test Python / Test (push) Failing after 22s

This commit is contained in:
Jon Michael Aanes 2024-08-27 21:03:20 +02:00
parent 032fac541d
commit b503933115
Signed by: Jmaa
SSH Key Fingerprint: SHA256:Ab0GfHGCblESJx7JRE4fj4bFy/KRpeLhi41y4pF3sNA

View File

@ -1,73 +1,20 @@
import os
import sys
import traceback
import logging
import pytest
sys.path.insert(1, os.path.join(sys.path[0], '..'))
from infernal_interpreter.Junk import JunkComparisonException
from infernal_interpreter.Emulator import Emulator, CodeParseException
import infernal
logger = logging.getLogger(__name__)
################################################################################
tests = []
code_tests = []
def add_test(name, result, register, code):
tests.append((name, result, register, code))
def printf(str, *args):
print(str.format(*args))
def execute_tests():
print('Executing tests!')
total_tests = 0
failed_tests = 0
error_tests = 0
for name, result, register, code in tests:
total_tests += 1
line_nr = None
try:
emu = infernal.Emulator(code)
emu.setStack('junk...', 'calling eip')
emu.setRegs(rip=0, rbp='old bp')
except infernal.CodeParseException as e:
error_tests += 1
printf(
'Encountered error when parsing {}, at line {}: {}',
name,
e.line_nr,
e.str,
)
try:
for line_nr in emu:
pass
if isinstance(result, BaseException):
printf('Error should have happened in {}, but did not', name)
failed_tests += 1
output = emu.getVal(register)
if output != result:
failed_tests += 1
printf(
'Failed in {}. {} was {}, should be {}',
name,
register,
output,
result,
)
except BaseException as e:
print(e)
if not isinstance(result, BaseException) or not isinstance(e, result):
error_tests += 1
printf(
'Encountered error in {}, at operation {}: {}',
name,
emu.getVal('%rip'),
e,
)
traceback.print_exc()
printf('Tests done! {}/{}.', total_tests - failed_tests - error_tests, total_tests)
printf('{} failed, and {} encountered a python error', failed_tests, error_tests)
code_tests.append((name, result, register, code))
################################################################################
@ -158,7 +105,7 @@ for jump_instruct, a, comp, b, result in branch_tests:
add_test(
'invalid comparison 1',
infernal.JunkComparisonException,
JunkComparisonException,
'None',
"""
start: movq $100, %rsp # Set stack pointer to a random position.
@ -172,7 +119,7 @@ start: movq $100, %rsp # Set stack pointer to a random position.
add_test(
'invalid addition 1',
infernal.JunkComparisonException,
JunkComparisonException,
'None',
"""
start: movq $100, %rsp # Set stack pointer to a random position.
@ -187,7 +134,7 @@ start: movq $100, %rsp # Set stack pointer to a random position.
add_test(
'invalid addition 2',
infernal.JunkComparisonException,
JunkComparisonException,
'None',
"""
start: movq $100, %rsp # Set stack pointer to a random position.
@ -202,7 +149,7 @@ start: movq $100, %rsp # Set stack pointer to a random position.
add_test(
'invalid subtraction 1',
infernal.JunkComparisonException,
JunkComparisonException,
'None',
"""
start: movq $100, %rsp # Set stack pointer to a random position.
@ -217,7 +164,7 @@ start: movq $100, %rsp # Set stack pointer to a random position.
add_test(
'invalid subtraction 2',
infernal.JunkComparisonException,
JunkComparisonException,
'None',
"""
start: movq $100, %rsp # Set stack pointer to a random position.
@ -232,5 +179,44 @@ start: movq $100, %rsp # Set stack pointer to a random position.
################################################################################
if __name__ == '__main__':
execute_tests()
@pytest.mark.parametrize("name,result,register,code", code_tests)
def test_execution(name, result, register, code):
line_nr = None
try:
emu = Emulator(code)
emu.setStack('junk...', 'calling eip')
emu.setRegs(rip=0, rbp='old bp')
except CodeParseException as e:
logger.exception(
'Encountered error when parsing %s, at line %s: %s',
name,
e.line_nr,
e.str,
)
raise
emu = emu
try:
for line_nr in emu:
pass
if isinstance(result, BaseException):
logger.error('Error should have happened in %s, but did not', name)
output = emu.getVal(register)
if output != result:
logger.error(
'Failed in %s. %s was %s, should be %s',
name,
register,
output,
result,
)
except BaseException as e:
logger.exception('Failed')
if not isinstance(result, BaseException) or not isinstance(e, result):
logger.exception(
'Encountered error in %s, at operation %s',
name,
emu.getVal('%rip'),
)
traceback.print_exc()
raise