Commit 2d647a18a6492f1b7661824505ae1d4dd8260745
1 parent
8f5d3eed
tests: Add some comments to testdata_reader
Showing
1 changed file
with
25 additions
and
1 deletions
tests/test_utils/testdata_reader.py
| 1 | +""" | ||
| 2 | +Helper functions to deal with zip-encrypted test files. | ||
| 3 | + | ||
| 4 | +Some test samples alerted antivirus software when installing oletools. Those | ||
| 5 | +samples were therefore "hidden" in encrypted zip-files. These functions help | ||
| 6 | +using them. | ||
| 7 | +""" | ||
| 8 | + | ||
| 1 | import os, sys, zipfile | 9 | import os, sys, zipfile |
| 2 | from os.path import dirname, abspath, normpath, join | 10 | from os.path import dirname, abspath, normpath, join |
| 3 | from . import DATA_BASE_DIR | 11 | from . import DATA_BASE_DIR |
| 4 | 12 | ||
| 13 | +# Passwort used to encrypt problematic test samples inside a zip container | ||
| 5 | ENCRYPTED_FILES_PASSWORD='infected-test' | 14 | ENCRYPTED_FILES_PASSWORD='infected-test' |
| 6 | 15 | ||
| 16 | +# import zipfile in a way compatible with all kinds of old python versions | ||
| 7 | if sys.version_info[0] <= 2: | 17 | if sys.version_info[0] <= 2: |
| 8 | # Python 2.x | 18 | # Python 2.x |
| 9 | if sys.version_info[1] <= 6: | 19 | if sys.version_info[1] <= 6: |
| @@ -18,11 +28,23 @@ else: | @@ -18,11 +28,23 @@ else: | ||
| 18 | from zipfile import is_zipfile | 28 | from zipfile import is_zipfile |
| 19 | ENCRYPTED_FILES_PASSWORD = ENCRYPTED_FILES_PASSWORD.encode() | 29 | ENCRYPTED_FILES_PASSWORD = ENCRYPTED_FILES_PASSWORD.encode() |
| 20 | 30 | ||
| 31 | + | ||
| 21 | def read(relative_path): | 32 | def read(relative_path): |
| 33 | + """ | ||
| 34 | + Return contents of unencrypted file inside test data dir. | ||
| 35 | + | ||
| 36 | + see also: `read_encrypted`. | ||
| 37 | + """ | ||
| 22 | with open(get_path_from_root(relative_path), 'rb') as file_handle: | 38 | with open(get_path_from_root(relative_path), 'rb') as file_handle: |
| 23 | return file_handle.read() | 39 | return file_handle.read() |
| 24 | 40 | ||
| 41 | + | ||
| 25 | def read_encrypted(relative_path, filename=None): | 42 | def read_encrypted(relative_path, filename=None): |
| 43 | + """ | ||
| 44 | + Return contents of encrypted file inside test data dir. | ||
| 45 | + | ||
| 46 | + see also: `read`. | ||
| 47 | + """ | ||
| 26 | z = zipfile.ZipFile(get_path_from_root(relative_path)) | 48 | z = zipfile.ZipFile(get_path_from_root(relative_path)) |
| 27 | 49 | ||
| 28 | if filename == None: | 50 | if filename == None: |
| @@ -33,5 +55,7 @@ def read_encrypted(relative_path, filename=None): | @@ -33,5 +55,7 @@ def read_encrypted(relative_path, filename=None): | ||
| 33 | z.close() | 55 | z.close() |
| 34 | return contents | 56 | return contents |
| 35 | 57 | ||
| 58 | + | ||
| 36 | def get_path_from_root(relative_path): | 59 | def get_path_from_root(relative_path): |
| 37 | - return join(DATA_BASE_DIR, relative_path) | ||
| 38 | \ No newline at end of file | 60 | \ No newline at end of file |
| 61 | + """Convert path relative to test data base dir to an absolute path.""" | ||
| 62 | + return join(DATA_BASE_DIR, relative_path) |