-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtest_xlsx_adapter.py
55 lines (47 loc) · 1.92 KB
/
test_xlsx_adapter.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
import re
import sys
from datetime import datetime, timedelta, timezone
from typing import Iterator
from unittest.mock import MagicMock
import pytest
from flow.record import fieldtypes
@pytest.fixture
def mock_openpyxl_package(monkeypatch: pytest.MonkeyPatch) -> Iterator[MagicMock]:
with monkeypatch.context() as m:
mock_openpyxl = MagicMock()
mock_cell = MagicMock()
mock_cell.ILLEGAL_CHARACTERS_RE = re.compile(r"[\000-\010]|[\013-\014]|[\016-\037]")
m.setitem(sys.modules, "openpyxl", mock_openpyxl)
m.setitem(sys.modules, "openpyxl.cell.cell", mock_cell)
yield mock_openpyxl
def test_sanitize_field_values(mock_openpyxl_package):
from flow.record.adapter.xlsx import sanitize_fieldvalues
assert list(
sanitize_fieldvalues(
[
7,
datetime(1920, 11, 11, 13, 37, 0, tzinfo=timezone(timedelta(hours=2))),
"James",
b"Bond",
b"\x00\x07",
fieldtypes.net.ipaddress("13.37.13.37"),
["Shaken", "Not", "Stirred"],
fieldtypes.posix_path("/home/user"),
fieldtypes.posix_command("/bin/bash -c 'echo hello world'"),
fieldtypes.windows_path("C:\\Users\\user\\Desktop"),
fieldtypes.windows_command("C:\\Some.exe /?"),
]
)
) == [
7,
datetime(1920, 11, 11, 11, 37, 0), # UTC normalization
"James",
'b"Bond"', # When possible, encode bytes in a printable way
"base64:AAc=", # If not, base64 encode
"13.37.13.37", # Stringify an ip address
"['Shaken', 'Not', 'Stirred']", # Stringify a list
"/home/user", # Stringify a posix path
"/bin/bash -c 'echo hello world'", # Stringify a posix command
"C:\\Users\\user\\Desktop", # Stringify a windows path
"C:\\Some.exe /?", # Stringify a windows command
]