Skip to content

Commit

Permalink
UTIndex test case updated into proper one
Browse files Browse the repository at this point in the history
  • Loading branch information
arekbulski committed Jan 30, 2021
1 parent 65d40e5 commit 0c77120
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 41 deletions.
1 change: 1 addition & 0 deletions gallery/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .pe32coff import pe32file
from .ut_index import UTIndex
43 changes: 3 additions & 40 deletions gallery/ut_index.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
import sys
sys.path = [".."] + sys.path # Use construct from repo, not pip
from construct import (
Construct,
byte2int,
int2byte,
stream_read,
stream_write,
integertypes,
IntegerError,
Struct,
)
from construct import *
from construct.lib import *


class Idx(Construct):
class UTIndex(Construct):
"""
Format for "Index" objects in Unreal Tournament 1999 packages.
Index objects are variable length signed integers with the following structure:
Expand Down Expand Up @@ -80,30 +70,3 @@ def _build(self, obj, stream, context, path):
if not more_bit:
break
return obj


if __name__ == "__main__":
c = Struct("index" / Idx())
test_data = [
[0x0f, 0x40, 0xff], # 0x0f
[0x4f, 0x40, 0xff], # (0x40 << 6) + 0x0f = 0x100f
[0x8f, 0x40, 0xff], # -0x0f
[0xcf, 0x40, 0xff], # -((0x40 << 6) + 0x0f) = 0x100f
[0x4f, 0x80, 0x40, 0xff], # (0x40 << 13) + 0x0f = 0x8000f
[0x4f, 0x80, 0x80, 0x40, 0xff], # (0x40 << 20) + 0x0f = 0x400000f
[0x4f, 0x80, 0x80, 0x80, 0x8f, 0xff] # 0x8f << 27 + 0x0f = 0x47800000f
]
expected_values = [
0x0f,
0x100f,
-0x0f,
-0x100f,
0x8000f,
0x400000f,
0x47800000f
]
for test, ev in zip(test_data, expected_values):
assert c.parse(bytes(test)).index == ev
new_bytes = c.build(dict(index=ev))
assert new_bytes == bytes(test[:len(new_bytes)])
print("All tests passed!")
28 changes: 27 additions & 1 deletion tests/gallery/test_gallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,37 @@
from construct import *
from construct.lib import *

from gallery import pe32file
from gallery import pe32file, UTIndex


def test_pe32():
commondump(pe32file, "python37-win32.exe")
commondump(pe32file, "python37-win64.exe")
commondump(pe32file, "SharpZipLib0860-dotnet20.dll")
commondump(pe32file, "sqlite3.dll")

def test_utindex():
d = UTIndex()
test_data = [
[0x0f, 0x40, 0xff], # 0x0f
[0x4f, 0x40, 0xff], # (0x40 << 6) + 0x0f = 0x100f
[0x8f, 0x40, 0xff], # -0x0f
[0xcf, 0x40, 0xff], # -((0x40 << 6) + 0x0f) = 0x100f
[0x4f, 0x80, 0x40, 0xff], # (0x40 << 13) + 0x0f = 0x8000f
[0x4f, 0x80, 0x80, 0x40, 0xff], # (0x40 << 20) + 0x0f = 0x400000f
[0x4f, 0x80, 0x80, 0x80, 0x8f, 0xff] # 0x8f << 27 + 0x0f = 0x47800000f
]
expected_values = [
0x0f,
0x100f,
-0x0f,
-0x100f,
0x8000f,
0x400000f,
0x47800000f
]
for test, ev in zip(test_data, expected_values):
assert d.parse(bytes(test)) == ev
new_bytes = d.build(ev)
assert new_bytes == bytes(test[:len(new_bytes)])
print("All tests passed!")

0 comments on commit 0c77120

Please sign in to comment.