-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtest_regression.py
690 lines (550 loc) · 19.8 KB
/
test_regression.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
import codecs
import json
import os
import pathlib
import subprocess
import sys
from datetime import datetime, timezone
from io import BytesIO
from unittest.mock import MagicMock, mock_open, patch
import msgpack
import pytest
from flow.record import (
RECORD_VERSION,
GroupedRecord,
Record,
RecordDescriptor,
RecordPacker,
RecordReader,
RecordWriter,
base,
fieldtypes,
whitelist,
)
from flow.record.base import _generate_record_class, fieldtype, is_valid_field_name
from flow.record.packer import RECORD_PACK_EXT_TYPE, RECORD_PACK_TYPE_RECORD
from flow.record.selector import CompiledSelector, Selector
from flow.record.tools import rdump
from flow.record.utils import is_stdout
def test_datetime_serialization():
packer = RecordPacker()
now = datetime.now(timezone.utc)
for tz in ["UTC", "Europe/Amsterdam"]:
os.environ["TZ"] = tz
descriptor = RecordDescriptor(
"test/datetime",
[
("datetime", "datetime"),
],
)
record = descriptor.recordType(datetime=now)
data = packer.pack(record)
r = packer.unpack(data)
assert r.datetime == now
def test_long_int_serialization():
packer = RecordPacker()
long_types = RecordDescriptor(
"test/long_types",
[
("varint", "long_type"),
("varint", "int_type"),
("varint", "long_type_neg"),
("varint", "int_type_neg"),
("varint", "max_int_as_long"),
],
)
l = 1239812398217398127398217389217389217398271398217321 # noqa: E741
i = 888888
lneg = -3239812398217398127398217389217389217398271398217321
ineg = -988888
max_int_as_long = sys.maxsize
record = long_types(l, i, lneg, ineg, max_int_as_long)
data = packer.pack(record)
r = packer.unpack(data)
assert r.long_type == l
assert r.int_type == i
assert r.long_type_neg == lneg
assert r.int_type_neg == ineg
assert r.max_int_as_long == max_int_as_long
def test_unicode_serialization():
packer = RecordPacker()
descriptor = RecordDescriptor(
"test/unicode",
[
("string", "text"),
],
)
puny_domains = [b"xn--s7y.co", b"xn--80ak6aa92e.com", b"xn--pple-43d.com"]
for p in puny_domains:
domain = codecs.decode(p, "idna")
record = descriptor.recordType(text=domain)
d = packer.pack(record)
record2 = packer.unpack(d)
assert record.text == record2.text
assert record.text == domain
def test_pack_long_int_serialization():
packer = RecordPacker()
# test if 'long int' that fit in the 'int' type would be packed as int internally
max_neg_int = -0x8000000000000000
d = packer.pack([1234, 123456, max_neg_int, sys.maxsize])
assert (
d
== b"\x94\xcd\x04\xd2\xce\x00\x01\xe2@\xd3\x80\x00\x00\x00\x00\x00\x00\x00\xcf\x7f\xff\xff\xff\xff\xff\xff\xff"
) # noqa: E501
def test_non_existing_field():
# RecordDescriptor that is used to test locally in the Broker client
TestRecord = RecordDescriptor(
"test/record",
[
("string", "text"),
],
)
x = TestRecord(text="Fox-IT, For a More Secure Society")
# r.content does not exist in the RecordDescriptor
assert Selector('lower("Fox-IT") in lower(r.content)').match(x) is False
assert Selector('"Fox-IT" in r.content').match(x) is False
# because the field does not exist, it will still evaluate to False even for negative matches
assert Selector('"Fox-IT" not in r.content').match(x) is False
assert Selector('"Fox-IT" in r.content').match(x) is False
assert Selector('"Fox-IT" != r.content').match(x) is False
assert Selector('"Fox-IT" == r.content').match(x) is False
assert Selector('r.content == "Fox-IT, For a More Secure Society"').match(x) is False
assert Selector('r.content != "Fox-IT, For a More Secure Society"').match(x) is False
assert Selector('r.content in "Fox-IT, For a More Secure Society!"').match(x) is False
assert Selector('r.content not in "Fox-IT, For a More Secure Society!"').match(x) is False
# r.text exist in the RecordDescriptor
assert Selector('"fox-it" in lower(r.text)').match(x)
assert Selector('r.text in "Fox-IT, For a More Secure Society!!"').match(x)
assert Selector('r.text == "Fox-IT, For a More Secure Society"').match(x)
assert Selector('r.text != "Fox-IT"').match(x)
assert Selector('lower("SECURE") in lower(r.text)').match(x)
assert Selector('"f0x-1t" not in lower(r.text)').match(x)
assert Selector('lower("NOT SECURE") not in lower(r.text)').match(x)
def test_set_field_type():
TestRecord = RecordDescriptor(
"test/record",
[
("uint32", "value"),
],
)
r = TestRecord(1)
assert isinstance(r.value, fieldtypes.uint32)
r.value = 2
assert isinstance(r.value, fieldtypes.uint32)
with pytest.raises(ValueError):
r.value = "lalala"
r.value = 2
r = TestRecord()
assert r.value is None
r.value = 1234
assert r.value == 1234
with pytest.raises(TypeError):
r.value = [1, 2, 3, 4, 5]
def test_packer_unpacker_none_values():
"""Tests packing and unpacking of Empty records (default values of None)."""
packer = RecordPacker()
# construct field types from all available fieldtypes
field_tuples = []
for typename in whitelist.WHITELIST:
fieldname = "field_{}".format(typename.replace(".", "_").lower())
field_tuples.append((typename, fieldname))
# create a TestRecord descriptor containing all the fieldtypes
TestRecord = RecordDescriptor("test/empty_record", field_tuples)
# initialize an Empty record and serialize/deserialize
record = TestRecord()
data = packer.pack(record)
r = packer.unpack(data)
assert isinstance(r, Record)
def test_fieldname_regression():
TestRecord = RecordDescriptor(
"test/uri_typed",
[
("string", "fieldname"),
],
)
rec = TestRecord("omg regression")
assert rec in Selector("r.fieldname == 'omg regression'")
with pytest.raises(AttributeError):
assert rec not in Selector("fieldname == 'omg regression'")
def test_version_field_regression():
packer = RecordPacker()
TestRecord = RecordDescriptor(
"test/record",
[
("uint32", "value"),
],
)
r = TestRecord(1)
assert r.__slots__[-1] == "_version"
r._version = 256
data = packer.pack(r)
with pytest.warns(RuntimeWarning) as record:
packer.unpack(data)
assert len(record) == 1
assert record[0].message.args[0].startswith("Got old style record with no version information")
r._version = RECORD_VERSION + 1 if RECORD_VERSION < 255 else RECORD_VERSION - 1
data = packer.pack(r)
with pytest.warns(RuntimeWarning) as record:
packer.unpack(data)
assert len(record) == 1
assert record[0].message.args[0].startswith("Got other version record")
def test_reserved_field_count_regression():
del base.RESERVED_FIELDS["_version"]
base.RESERVED_FIELDS["_extra"] = "varint"
base.RESERVED_FIELDS["_version"] = "varint"
RecordDescriptor.get_required_fields.cache_clear()
_generate_record_class.cache_clear()
TestRecordExtra = RecordDescriptor(
"test/record",
[
("uint32", "value"),
],
)
del base.RESERVED_FIELDS["_extra"]
RecordDescriptor.get_required_fields.cache_clear()
_generate_record_class.cache_clear()
TestRecordBase = RecordDescriptor(
"test/record",
[
("uint32", "value"),
],
)
packer = RecordPacker()
r = TestRecordExtra(1, _extra=1337)
assert r.value == 1
assert r._extra == 1337
data = packer.pack(r)
packer.register(TestRecordBase)
unpacked = packer.unpack(data)
with pytest.raises(AttributeError):
unpacked._extra
assert unpacked.value == 1
assert unpacked._version == 1
def test_no_version_field_regression():
# Emulate old style record
packer = RecordPacker()
TestRecord = RecordDescriptor(
"test/record",
[
("uint32", "value"),
],
)
packer.register(TestRecord)
r = TestRecord(1)
packed = r._pack()
mod = (packed[0], packed[1][:-1]) # Strip version field
rdata = packer.pack((RECORD_PACK_TYPE_RECORD, mod))
data = packer.pack(msgpack.ExtType(RECORD_PACK_EXT_TYPE, rdata))
with pytest.warns(RuntimeWarning) as record:
unpacked = packer.unpack(data)
assert len(record) == 1
assert record[0].message.args[0].startswith("Got old style record with no version information")
assert unpacked.value == 1
assert unpacked._version == 1 # Version field implicitly added
def test_mixed_case_name():
assert is_valid_field_name("Test")
assert is_valid_field_name("test")
assert is_valid_field_name("TEST")
assert not is_valid_field_name("test[]")
assert not is_valid_field_name("_test")
TestRecord = RecordDescriptor(
"Test/Record",
[
("uint32", "Value"),
],
)
r = TestRecord(1)
assert r.Value == 1
def test_multi_grouped_record_serialization(tmp_path):
TestRecord = RecordDescriptor(
"Test/Record",
[
("net.ipv4.Address", "ip"),
],
)
GeoRecord = RecordDescriptor(
"geoip/country",
[
("string", "country"),
("string", "city"),
],
)
ASNRecord = RecordDescriptor(
"geoip/asn",
[
("string", "asn"),
("string", "isp"),
],
)
with pytest.deprecated_call():
test_rec = TestRecord("1.3.3.7")
geo_rec = GeoRecord(country="Netherlands", city="Delft")
grouped_rec = GroupedRecord("grouped/geoip", [test_rec, geo_rec])
asn_rec = ASNRecord(asn="1337", isp="Cyberspace")
record = GroupedRecord("grouped/geo/asn", [grouped_rec, asn_rec])
assert record.ip == "1.3.3.7"
assert record.country == "Netherlands"
assert record.city == "Delft"
assert record.asn == "1337"
assert record.isp == "Cyberspace"
writer = RecordWriter(tmp_path / "out.record")
writer.write(record)
writer.close()
with pytest.deprecated_call():
reader = RecordReader(tmp_path / "out.record")
records = list(reader)
assert len(records) == 1
record = records[0]
assert record.ip == "1.3.3.7"
assert record.country == "Netherlands"
assert record.city == "Delft"
assert record.asn == "1337"
assert record.isp == "Cyberspace"
@pytest.mark.parametrize("PSelector", [Selector, CompiledSelector])
def test_ast_unicode_literals(PSelector):
TestRecord = RecordDescriptor("Test/Record", [])
assert TestRecord() in PSelector("get_type('string literal') == get_type(u'hello')")
assert TestRecord() in PSelector("get_type('not bytes') != get_type(b'hello')")
def test_grouped_replace():
TestRecord = RecordDescriptor(
"test/adapter",
[
("uint32", "number"),
],
)
OtherRecord = RecordDescriptor(
"test/other",
[
("string", "other"),
],
)
# Constructing grouped record normally
record = TestRecord(number=1)
other_record = OtherRecord("foobar")
grouped_record = GroupedRecord("grouped/original", [record, other_record])
assert grouped_record._source is None
assert grouped_record.number == 1
assert grouped_record.other == "foobar"
# Constructing grouped record normally (using a replaced record)
replaced_record = record._replace(_source="newsource")
grouped_record = GroupedRecord("grouped/replaced", [replaced_record, other_record])
assert grouped_record._source == "newsource"
assert grouped_record.number == 1
assert grouped_record.other == "foobar"
# Test GroupedRecord replace
replaced_grouped_record = grouped_record._replace(number=100)
assert replaced_grouped_record.number == 100
assert replaced_grouped_record.other == "foobar"
# Test with multiple replacements
replaced_grouped_record = grouped_record._replace(number=200, other="a string", _source="testcase")
assert replaced_grouped_record.number == 200
assert replaced_grouped_record.other == "a string"
assert replaced_grouped_record._source == "testcase"
# Replacement with non existing field should raise a ValueError
with pytest.raises(ValueError) as excinfo:
grouped_record._replace(number=100, other="changed", non_existing_field="oops")
excinfo.match(".*Got unexpected field names:.*non_existing_field.*")
def test_bytes_line_adapter(capsys):
TestRecord = RecordDescriptor(
"test/bytes_hex",
[
("bytes", "data"),
],
)
with RecordWriter("line://") as writer:
writer.write(TestRecord(b"hello world"))
captured = capsys.readouterr()
assert "data = b'hello world'" in captured.out
def test_is_stdout(tmp_path, capsysbinary):
assert is_stdout(sys.stdout)
assert is_stdout(sys.stdout.buffer)
assert not is_stdout(sys.stderr)
assert not is_stdout(sys.stderr.buffer)
with open(tmp_path / "test", "w") as f:
assert not is_stdout(f)
with RecordWriter() as writer:
assert is_stdout(writer.fp)
out, err = capsysbinary.readouterr()
assert out.startswith(b"\x00\x00\x00\x0f\xc4\rRECORDSTREAM\n")
with RecordWriter(tmp_path / "output.records") as writer:
assert not is_stdout(writer.fp)
with RecordWriter("csvfile://") as writer:
assert is_stdout(writer.fp)
with RecordWriter("line://") as writer:
assert is_stdout(writer.fp)
def test_rdump_fieldtype_path_json(tmp_path):
TestRecord = RecordDescriptor(
"test/record",
[
("path", "path"),
],
)
# write the test records so rdump can read it
record_path = tmp_path / "test.records"
with RecordWriter(record_path) as writer:
writer.write(
TestRecord(
path=fieldtypes.path.from_windows(r"c:\windows\system32"),
)
)
writer.write(
TestRecord(
path=fieldtypes.path.from_posix("/root/.bash_history"),
)
)
# rdump --jsonlines
args = [
"rdump",
str(record_path),
"--jsonlines",
]
process = subprocess.Popen(args, stdout=subprocess.PIPE)
stdout, stderr = process.communicate()
assert process.returncode == 0
assert stderr is None
# strip _generated, _source, _classification from dictionary
jsonlines = []
for line in stdout.splitlines():
jsondict = {k: v for k, v in json.loads(line).items() if not k.startswith("_")}
jsonlines.append(jsondict)
assert jsonlines == [
{"path": "c:\\windows\\system32"},
{"path": "/root/.bash_history"},
]
@pytest.mark.parametrize(
"path_initializer",
[
pathlib.PureWindowsPath,
fieldtypes.windows_path,
fieldtypes.path.from_windows,
],
)
def test_windows_path_regression(path_initializer):
TestRecord = RecordDescriptor(
"test/record",
[
("path", "path"),
],
)
r = TestRecord(path=path_initializer("/c:/Windows/System32/drivers/null.sys"))
assert str(r.path) == "\\c:\\Windows\\System32\\drivers\\null.sys"
assert repr(r.path) == "'\\c:\\Windows\\System32\\drivers\\null.sys'"
@pytest.mark.parametrize(
"record_count,count,expected_count",
[
(10, 10, 10),
(0, 10, 0),
(1, 10, 1),
(5, 0, 5), # --count 0 should be ignored
(5, 1, 1),
(5, 10, 5),
],
)
def test_rdump_count_list(tmp_path, capsysbinary, record_count, count, expected_count):
TestRecord = RecordDescriptor(
"test/record",
[
("varint", "count"),
],
)
# write the test records so rdump can read it
record_path = tmp_path / "test.records"
with RecordWriter(record_path) as writer:
for i in range(record_count):
writer.write(TestRecord(count=i))
# rdump --count <count>
rdump.main([str(record_path), "--count", str(count)])
captured = capsysbinary.readouterr()
assert captured.err == b""
assert len(captured.out.splitlines()) == expected_count
# rdump --list --count <count>
rdump.main([str(record_path), "--list", "--count", str(count)])
captured = capsysbinary.readouterr()
assert captured.err == b""
assert f"Processed {expected_count} records".encode() in captured.out
def test_record_adapter_windows_path(tmp_path):
TestRecord = RecordDescriptor(
"test/record",
[
("string", "text"),
],
)
path_records = tmp_path / "test.records"
with RecordWriter(path_records) as writer:
writer.write(TestRecord("foo"))
writer.write(TestRecord("bar"))
test_read_buf = BytesIO(path_records.read_bytes())
mock_reader = MagicMock(wraps=test_read_buf, spec=BytesIO)
with patch("io.open", MagicMock(return_value=mock_reader)) as m:
m.return_value.closed = False
adapter = RecordReader(r"c:\users\user\test.records")
assert type(adapter).__name__ == "StreamReader"
m.assert_called_once_with(r"c:\users\user\test.records", "rb")
assert [r.text for r in adapter] == ["foo", "bar"]
with patch("io.open", mock_open()) as m:
adapter = RecordWriter(r"c:\users\user\test.records")
assert type(adapter).__name__ == "StreamWriter"
m.assert_called_once_with(r"c:\users\user\test.records", "wb")
def test_datetime_as_fieldname():
TestRecord = RecordDescriptor(
"test/record",
[
("string", "datetime"),
],
)
TestRecord()
def test_string_surrogateescape_serialization(tmp_path):
TestRecord = RecordDescriptor(
"test/record",
[
("string", "str_value"),
],
)
str_value = b"hello \xa7 world".decode(errors="surrogateescape")
record = TestRecord(str_value=str_value)
assert str_value == "hello \udca7 world"
assert record.str_value == str_value
with RecordWriter(tmp_path / "test.records") as writer:
writer.write(record)
with RecordReader(tmp_path / "test.records") as reader:
record = next(iter(reader))
assert str(record.str_value) == str_value
assert record.str_value == str_value
assert record.str_value.encode(errors="surrogateescape") == b"hello \xa7 world"
def test_fieldtype_typedlist_net_ipaddress():
assert fieldtype("net.ipaddress[]")
assert fieldtype("net.ipaddress[]").__type__ == fieldtypes.net.ipaddress
assert issubclass(fieldtype("net.ipaddress[]"), list)
assert issubclass(fieldtype("net.ipaddress[]"), fieldtypes.FieldType)
def test_record_reader_default_stdin(tmp_path):
"""RecordWriter should default to stdin if no path is given"""
TestRecord = RecordDescriptor(
"test/record",
[
("string", "text"),
],
)
# write some records
records_path = tmp_path / "test.records"
with RecordWriter(records_path) as writer:
writer.write(TestRecord("foo"))
# Test stdin
with patch("sys.stdin", BytesIO(records_path.read_bytes())):
with RecordReader() as reader:
for record in reader:
assert record.text == "foo"
def test_record_writer_default_stdout(capsysbinary):
"""RecordWriter should default to stdout if no path is given"""
TestRecord = RecordDescriptor(
"test/record",
[
("string", "text"),
],
)
# write a record to stdout
with RecordWriter() as writer:
writer.write(TestRecord("foo"))
stdout = capsysbinary.readouterr().out
assert stdout.startswith(b"\x00\x00\x00\x0f\xc4\rRECORDSTREAM\n")
if __name__ == "__main__":
__import__("standalone_test").main(globals())