Newer
Older
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
from scapy.layers.inet import TCP
from scapy.layers.http import HTTP, HTTPRequest, HTTPResponse
from pcap_anonymize.layers.http import (
HttpFields,
get_http_layer,
anonymize_http
)
### TEST CONSTANTS ###
ENCODING = "utf-8"
http_request = HTTPRequest(
Method="GET",
Path="/index.html"
)
http_response = HTTPResponse(
Status_Code="200",
Reason_Phrase="OK"
)
### TEST FUNCTIONS ###
def test_get_http_layer_request() -> None:
"""
Test the function `get_http_layer`,
with an HTTP Request packet.
"""
packet = TCP(dport=80) / http_request
http = get_http_layer(packet)
assert http == http_request
assert http.getfieldval(HttpFields.METHOD.value).decode(ENCODING) == "GET"
assert http.getfieldval(HttpFields.PATH.value).decode(ENCODING) == "/index.html"
def test_get_http_layer_request_indirect() -> None:
"""
Test the function `get_http_layer`,
with an HTTP Request packet
which is not directly accessible by scapy.
"""
packet = TCP(dport=8800) / http_request
http = get_http_layer(packet)
assert isinstance(http, HTTPRequest)
assert http == http_request
assert http.getfieldval(HttpFields.METHOD.value).decode(ENCODING) == "GET"
assert http.getfieldval(HttpFields.PATH.value).decode(ENCODING) == "/index.html"
def test_get_http_layer_response() -> None:
"""
Test the function `get_http_layer`,
with an HTTP Response packet.
"""
packet = TCP(dport=80) / http_response
http = get_http_layer(packet)
assert http == http_response
assert http.getfieldval("Status_Code").decode(ENCODING) == "200"
assert http.getfieldval("Reason_Phrase").decode(ENCODING) == "OK"
def test_get_http_layer_response_indirect() -> None:
"""
Test the function `get_http_layer`,
with an HTTP Response packet
which is not directly accessible by scapy.
"""
packet = TCP(dport=8800) / http_response
http = get_http_layer(packet)
assert isinstance(http, HTTPResponse)
assert http == http_response
assert http.getfieldval("Status_Code").decode(ENCODING) == "200"
assert http.getfieldval("Reason_Phrase").decode(ENCODING) == "OK"
def test_anonymize_http_request() -> None:
"""
Test the function `anonymize_http`,
with an HTTP Request packet.
"""
packet = TCP(dport=80) / http_request
http = get_http_layer(packet)
anonymize_http(http)
assert http.getfieldval(HttpFields.METHOD.value).decode(ENCODING) == "GET"
assert http.getfieldval(HttpFields.PATH.value).decode(ENCODING) == "/index.html"
# Ensure other fields have been deleted
for field in http.fields:
assert field == HttpFields.METHOD.value or field == HttpFields.PATH.value
def test_anonymize_http_request_indirect() -> None:
"""
Test the function `anonymize_http`,
with an HTTP Request packet.
"""
packet = TCP(dport=8800) / http_request
http = get_http_layer(packet)
anonymize_http(http)
assert http.getfieldval(HttpFields.METHOD.value).decode(ENCODING) == "GET"
assert http.getfieldval(HttpFields.PATH.value).decode(ENCODING) == "/index.html"
# Ensure other fields have been deleted
for field in http.fields:
assert field == HttpFields.METHOD.value or field == HttpFields.PATH.value
def test_anonymize_http_response() -> None:
"""
Test the function `anonymize_http`,
with an HTTP Response packet.
"""
packet = TCP(dport=80) / http_response
http = get_http_layer(packet)
anonymize_http(http)
for field in http.fields:
assert field == HttpFields.METHOD.value or field == HttpFields.PATH.value
def test_anonymize_http_response_indirect() -> None:
"""
Test the function `anonymize_http`,
with an HTTP Response packet.
"""
packet = TCP(dport=8800) / http_response
http = get_http_layer(packet)
anonymize_http(http)
for field in http.fields:
assert field == HttpFields.METHOD.value or field == HttpFields.PATH.value