forked from joesecurity/jbxapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_jbxapi.py
259 lines (178 loc) · 8.07 KB
/
test_jbxapi.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
import os
import io
import pytest
import tempfile
import shutil
import jbxapi
# make all network calls fail
@pytest.fixture(autouse=True)
def no_requests(monkeypatch):
def will_fail(*args, **kwargs):
raise RuntimeError("Disabled")
monkeypatch.setattr("requests.sessions.Session.request", lambda: will_fail())
@pytest.fixture
def joe():
return jbxapi.JoeSandbox()
class MockedResponse(object):
class Request:
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
def __init__(self, json=None, **kwargs):
self._json = json
self.__dict__.update(kwargs)
self.requests = []
def json(self):
return self._json
def __call__(self, url, **kwargs):
self.requests.append(self.Request(url=url, **kwargs))
return self
successful_submission = {"data": {"webid": "1"}}
def test_file_submission(joe, monkeypatch):
mock = MockedResponse(ok=True, json=successful_submission)
monkeypatch.setattr("requests.sessions.Session.post", mock)
sample = io.BytesIO(b"Testdata")
response = joe.submit_sample(sample)
assert response == successful_submission["data"]
assert "sample" in mock.requests[0].files
def test_file_submission_cookbook(joe, monkeypatch):
mock = MockedResponse(ok=True, json=successful_submission)
monkeypatch.setattr("requests.sessions.Session.post", mock)
sample = io.BytesIO(b"Testdata")
cookbook = io.BytesIO(b"Testdata")
response = joe.submit_sample(sample, cookbook)
assert response == successful_submission["data"]
assert "cookbook" in mock.requests[0].files
assert "sample" in mock.requests[0].files
def test_file_submission_tuple(joe, monkeypatch):
mock = MockedResponse(ok=True, json=successful_submission)
monkeypatch.setattr("requests.sessions.Session.post", mock)
sample = io.BytesIO(b"Testdata")
response = joe.submit_sample(("Filename", sample))
assert response == successful_submission["data"]
assert mock.requests[0].files["sample"] == ("Filename", sample)
def test_strange_file_names(joe, monkeypatch):
names = {
"Sample": "Sample",
"\xc3\xb6": "xc3xb6",
"|": "x7c",
}
mock = MockedResponse(ok=True, json=successful_submission)
monkeypatch.setattr("requests.sessions.Session.post", mock)
for i, (name, expected) in enumerate(names.items()):
s = io.BytesIO(b"Testdata")
s.name = name
joe.submit_sample(s, cookbook=s)
assert mock.requests[i * 2].files["sample"] == (expected, s)
assert mock.requests[i * 2].files["cookbook"] == (expected, s)
s = io.BytesIO(b"Testdata")
joe.submit_sample((name, s), cookbook=(name, s))
assert mock.requests[i * 2 + 1].files["sample"] == (expected, s)
assert mock.requests[i * 2 + 1].files["cookbook"] == (expected, s)
def test_url_submission(joe, monkeypatch):
mock = MockedResponse(ok=True, json=successful_submission)
monkeypatch.setattr("requests.sessions.Session.post", mock)
response = joe.submit_url("https://example.net")
assert response == successful_submission["data"]
assert "url" in mock.requests[0].data
assert mock.requests[0].files is None
def test_sample_url_submission(joe, monkeypatch):
mock = MockedResponse(ok=True, json=successful_submission)
monkeypatch.setattr("requests.sessions.Session.post", mock)
response = joe.submit_sample_url("https://example.net/sample")
assert response == successful_submission["data"]
assert "sample-url" in mock.requests[0].data
assert mock.requests[0].files is None
def test_cookbook_submission(joe, monkeypatch):
mock = MockedResponse(ok=True, json=successful_submission)
monkeypatch.setattr("requests.sessions.Session.post", mock)
cookbook = io.BytesIO(b"Testdata")
response = joe.submit_cookbook(cookbook)
assert response == successful_submission["data"]
assert "cookbook" in mock.requests[0].files
assert "sample" not in mock.requests[0].files
def test_boolean_parameters(joe, monkeypatch):
tests = {
# true values
"truish": "1",
True: "1",
# false values
False: "0",
"": "0",
# no preference
None: None,
# no preference (bool)
jbxapi.UnsetBool: None,
}
for value, expected in tests.items():
mock = MockedResponse(ok=True, json=successful_submission)
monkeypatch.setattr("requests.sessions.Session.post", mock)
joe.submit_url("https://example.net", params={"internet-access": value})
assert mock.requests[0].data["internet-access"] == expected
def test_array_parameters(joe, monkeypatch):
mock = MockedResponse(ok=True, json=successful_submission)
monkeypatch.setattr("requests.sessions.Session.post", mock)
joe.submit_url("https://example.net", params={"systems": ["w7"], "tags": ["mytag"]})
assert mock.requests[0].data["systems[]"] == ["w7"]
assert "systems" not in mock.requests[0].data
assert mock.requests[0].data["tags[]"] == ["mytag"]
assert "tags" not in mock.requests[0].data
def test_array_parameters_single_value(joe, monkeypatch):
mock = MockedResponse(ok=True, json=successful_submission)
monkeypatch.setattr("requests.sessions.Session.post", mock)
joe.submit_url("https://example.net", params={"systems": "w7", "tags": "mytag"})
assert mock.requests[0].data["systems[]"] == "w7"
assert "systems" not in mock.requests[0].data
assert mock.requests[0].data["tags[]"] == "mytag"
assert "tags" not in mock.requests[0].data
# CLI tests
def test_cli_submit_file(monkeypatch):
mock = MockedResponse(ok=True, json=successful_submission)
monkeypatch.setattr("requests.sessions.Session.post", mock)
with tempfile.NamedTemporaryFile(delete=False) as temp:
temp.write(b'Some data')
try:
jbxapi.cli(["submit", temp.name])
finally:
os.remove(temp.name)
assert "sample" in mock.requests[0].files
def test_cli_submit_dir(monkeypatch):
mock = MockedResponse(ok=True, json=successful_submission)
monkeypatch.setattr("requests.sessions.Session.post", mock)
sample_dir = tempfile.mkdtemp()
with tempfile.NamedTemporaryFile(dir=sample_dir, delete=False) as temp:
temp.write(b'Some data')
with tempfile.NamedTemporaryFile(dir=sample_dir, delete=False) as temp:
temp.write(b'Some other data')
try:
jbxapi.cli(["submit", sample_dir])
finally:
shutil.rmtree(sample_dir)
assert "sample" in mock.requests[0].files
print(mock.requests[0].files)
def test_cli_submit_url(monkeypatch):
mock = MockedResponse(ok=True, json=successful_submission)
monkeypatch.setattr("requests.sessions.Session.post", mock)
jbxapi.cli(["submit", "--url", "https://example.net"])
assert mock.requests[0].data["url"] == "https://example.net"
def test_cli_submit_sample_url(monkeypatch):
mock = MockedResponse(ok=True, json=successful_submission)
monkeypatch.setattr("requests.sessions.Session.post", mock)
jbxapi.cli(["submit", "--sample-url", "https://example.net/sample"])
assert mock.requests[0].data["sample-url"] == "https://example.net/sample"
def test_cli_submit_sample_with_cookbook(monkeypatch):
mock = MockedResponse(ok=True, json=successful_submission)
monkeypatch.setattr("requests.sessions.Session.post", mock)
with tempfile.NamedTemporaryFile(delete=False) as temp1:
temp1.write(b'Some data')
with tempfile.NamedTemporaryFile(delete=False) as temp2:
temp2.write(b'Some data')
jbxapi.cli(["submit", "--cookbook", temp1.name, temp2.name])
assert "cookbook" in mock.requests[0].files
assert "sample" in mock.requests[0].files
def test_cli_common_params_position(monkeypatch):
mock = MockedResponse(ok=True, json={"data": "ok"})
monkeypatch.setattr("requests.sessions.Session.post", mock)
# command at the start
jbxapi.cli(["analysis", "list", "--apikey", "1234", "--apiurl", "http://example.net", "--accept-tac"])
# command at the end
jbxapi.cli(["--apikey", "1234", "--apiurl", "http://example.net", "--accept-tac", "analysis", "list"])