-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_validation.py
More file actions
364 lines (307 loc) · 11.6 KB
/
Copy pathtest_validation.py
File metadata and controls
364 lines (307 loc) · 11.6 KB
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
"""
Tests for validation behavior on malformed input, missing fields,
and edge cases.
These tests document and verify the expected behavior when the schema
encounters invalid or edge-case input data.
"""
import json
from pathlib import Path
import pytest
from serde import ValidationError
from l9format import (
Certificate,
DatasetSummary,
GeoLocation,
GeoPoint,
L9Event,
L9HttpEvent,
Network,
)
TESTS_DIR = Path(__file__).parent
class TestMissingRequiredFields:
"""Test behavior when required fields are missing."""
def test_geopoint_missing_lat(self) -> None:
with pytest.raises(ValidationError):
GeoPoint.from_dict({"lon": "1.0"})
def test_geopoint_missing_lon(self) -> None:
with pytest.raises(ValidationError):
GeoPoint.from_dict({"lat": "1.0"})
def test_network_missing_organization_name(self) -> None:
with pytest.raises(ValidationError):
Network.from_dict({"asn": 12345, "network": "1.0.0.0/8"})
def test_network_missing_asn(self) -> None:
with pytest.raises(ValidationError):
Network.from_dict(
{"organization_name": "Test Org", "network": "1.0.0.0/8"}
)
def test_network_missing_network(self) -> None:
with pytest.raises(ValidationError):
Network.from_dict({"organization_name": "Test Org", "asn": 12345})
def test_certificate_missing_cn(self) -> None:
with pytest.raises(ValidationError):
Certificate.from_dict(
{
"fingerprint": "abc123",
"key_algo": "RSA",
"key_size": 2048,
"issuer_name": "Test CA",
"not_before": "2024-01-01T00:00:00Z",
"not_after": "2024-12-31T23:59:59Z",
"valid": True,
}
)
def test_l9event_missing_required_fields(self) -> None:
with pytest.raises(ValidationError):
L9Event.from_dict(
{
"event_source": "test",
"ip": "127.0.0.1",
"port": "80",
"host": "example.com",
"reverse": "ptr.example.com",
"protocol": "http",
"summary": "test",
"time": "2024-01-01T00:00:00Z",
}
)
class TestExtraUnknownFields:
"""Extra fields are silently ignored (default serde behavior)."""
def test_geopoint_extra_field_ignored(self) -> None:
gp = GeoPoint.from_dict(
{"lat": "1.5", "lon": "2.5", "unknown_field": "value"}
)
assert gp.lat == 1.5
assert gp.lon == 2.5
assert not hasattr(gp, "unknown_field")
def test_network_extra_field_ignored(self) -> None:
net = Network.from_dict(
{
"organization_name": "Test Org",
"asn": 12345,
"network": "1.0.0.0/8",
"extra_field": "should be ignored",
}
)
assert net.organization_name == "Test Org"
assert net.asn == 12345
assert not hasattr(net, "extra_field")
class TestNullValues:
"""Test behavior when null values are provided."""
def test_geopoint_null_lat(self) -> None:
with pytest.raises(ValueError, match="invalid decimal"):
GeoPoint.from_dict({"lat": None, "lon": "1.0"})
def test_geopoint_null_lon(self) -> None:
with pytest.raises(ValueError, match="invalid decimal"):
GeoPoint.from_dict({"lat": "1.0", "lon": None})
def test_network_null_organization_name(self) -> None:
with pytest.raises(ValidationError):
Network.from_dict(
{
"organization_name": None,
"asn": 12345,
"network": "1.0.0.0/8",
}
)
def test_network_null_asn(self) -> None:
with pytest.raises(ValidationError):
Network.from_dict(
{
"organization_name": "Test Org",
"asn": None,
"network": "1.0.0.0/8",
}
)
def test_optional_field_allows_null(self) -> None:
geo = GeoLocation.from_dict(
{
"continent_name": None,
"region_iso_code": None,
"city_name": None,
"country_iso_code": None,
"country_name": None,
"region_name": None,
"location": None,
}
)
assert geo.continent_name is None
assert geo.location is None
class TestEmptyStrings:
"""Test behavior when empty strings are provided."""
def test_geopoint_empty_string_lat(self) -> None:
with pytest.raises(ValueError, match="invalid decimal"):
GeoPoint.from_dict({"lat": "", "lon": "1.0"})
def test_geopoint_empty_string_lon(self) -> None:
with pytest.raises(ValueError, match="invalid decimal"):
GeoPoint.from_dict({"lat": "1.0", "lon": ""})
def test_network_accepts_empty_strings(self) -> None:
net = Network.from_dict(
{"organization_name": "", "asn": 12345, "network": ""}
)
assert net.organization_name == ""
assert net.network == ""
class TestBoundaryIntegers:
"""Test behavior with boundary integer values.
The schema performs no range validation on integers.
"""
def test_network_zero_asn(self) -> None:
net = Network.from_dict(
{"organization_name": "Test", "asn": 0, "network": "1.0.0.0/8"}
)
assert net.asn == 0
def test_network_negative_asn(self) -> None:
net = Network.from_dict(
{"organization_name": "Test", "asn": -1, "network": "1.0.0.0/8"}
)
assert net.asn == -1
def test_network_large_asn(self) -> None:
net = Network.from_dict(
{
"organization_name": "Test",
"asn": 2**31 - 1,
"network": "1.0.0.0/8",
}
)
assert net.asn == 2147483647
def test_http_event_negative_status(self) -> None:
http = L9HttpEvent.from_dict(
{
"root": "/",
"url": "/test",
"status": -1,
"length": 0,
"title": "",
"favicon_hash": "",
}
)
assert http.status == -1
def test_dataset_summary_negative_values(self) -> None:
ds = DatasetSummary.from_dict(
{
"rows": -1,
"files": -1,
"size": -1,
"collections": -1,
"infected": False,
}
)
assert ds.rows == -1
assert ds.files == -1
assert ds.size == -1
class TestMalformedDatetimes:
"""Test behavior with malformed datetime strings."""
def test_certificate_invalid_datetime(self) -> None:
with pytest.raises(ValidationError):
Certificate.from_dict(
{
"cn": "example.com",
"fingerprint": "abc123",
"key_algo": "RSA",
"key_size": 2048,
"issuer_name": "Test CA",
"not_before": "invalid-datetime",
"not_after": "2024-12-31T23:59:59Z",
"valid": True,
}
)
def test_certificate_empty_datetime(self) -> None:
with pytest.raises(ValidationError):
Certificate.from_dict(
{
"cn": "example.com",
"fingerprint": "abc123",
"key_algo": "RSA",
"key_size": 2048,
"issuer_name": "Test CA",
"not_before": "",
"not_after": "2024-12-31T23:59:59Z",
"valid": True,
}
)
def test_certificate_date_only(self) -> None:
cert = Certificate.from_dict(
{
"cn": "example.com",
"fingerprint": "abc123",
"key_algo": "RSA",
"key_size": 2048,
"issuer_name": "Test CA",
"not_before": "2024-01-01",
"not_after": "2024-12-31T23:59:59Z",
"valid": True,
}
)
assert cert.not_before.year == 2024
assert cert.not_before.month == 1
assert cert.not_before.day == 1
class TestMalformedDecimals:
"""Test behavior with malformed decimal values."""
def test_geopoint_non_numeric(self) -> None:
with pytest.raises(ValueError, match="invalid decimal"):
GeoPoint.from_dict({"lat": "not-a-number", "lon": "1.0"})
def test_geopoint_infinity(self) -> None:
gp = GeoPoint.from_dict({"lat": "Infinity", "lon": "1.0"})
assert str(gp.lat) == "Infinity"
def test_geopoint_nan(self) -> None:
gp = GeoPoint.from_dict({"lat": "NaN", "lon": "1.0"})
assert str(gp.lat) == "NaN"
def test_geopoint_scientific_notation(self) -> None:
gp = GeoPoint.from_dict({"lat": "1.5e2", "lon": "2.5E-1"})
assert gp.lat == 150
assert gp.lon == 0.25
def test_geopoint_negative_values(self) -> None:
gp = GeoPoint.from_dict({"lat": "-1.5", "lon": "-2.5"})
assert gp.lat == -1.5
assert gp.lon == -2.5
def test_geopoint_round_trip_preserves_value(self) -> None:
"""Regression: normalize() used to strip trailing zeros."""
gp = GeoPoint.from_dict({"lat": "1.000000", "lon": "2.500000"})
serialized = gp.to_dict()
gp2 = GeoPoint.from_dict(serialized)
assert gp2.lat == gp.lat
assert gp2.lon == gp.lon
class TestComplexNestedValidation:
"""Test validation behavior with complex nested structures."""
def test_l9event_invalid_nested_decimal(self) -> None:
path = TESTS_DIR / "l9event.json"
with open(path) as f:
data = json.load(f)
data["geoip"]["location"] = {"lat": "invalid", "lon": "1.0"}
with pytest.raises(ValueError, match="invalid decimal"):
L9Event.from_dict(data)
def test_l9event_missing_nested_required_field(self) -> None:
path = TESTS_DIR / "l9event.json"
with open(path) as f:
data = json.load(f)
del data["network"]["asn"]
with pytest.raises(ValidationError):
L9Event.from_dict(data)
def test_certificate_with_domain_list(self) -> None:
cert = Certificate.from_dict(
{
"cn": "example.com",
"domain": ["site1.example.com", "site2.example.com"],
"fingerprint": "abc123",
"key_algo": "RSA",
"key_size": 2048,
"issuer_name": "Test CA",
"not_before": "2024-01-01T00:00:00Z",
"not_after": "2024-12-31T23:59:59Z",
"valid": True,
}
)
assert cert.domain == ["site1.example.com", "site2.example.com"]
def test_certificate_with_empty_domain_list(self) -> None:
cert = Certificate.from_dict(
{
"cn": "example.com",
"domain": [],
"fingerprint": "abc123",
"key_algo": "RSA",
"key_size": 2048,
"issuer_name": "Test CA",
"not_before": "2024-01-01T00:00:00Z",
"not_after": "2024-12-31T23:59:59Z",
"valid": True,
}
)
assert cert.domain == []