-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_stringencrypt.py
More file actions
143 lines (109 loc) · 4.26 KB
/
test_stringencrypt.py
File metadata and controls
143 lines (109 loc) · 4.26 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
"""Unit tests for StringEncrypt WebAPI client (HTTP mocked)."""
import zlib
import base64
from urllib.parse import parse_qs
import responses
from stringencrypt import StringEncrypt
API = StringEncrypt.API_URL
@responses.activate
def test_encrypt_success_plain_source():
body = {"error": 0, "source": "print('ok')\n"}
responses.add(responses.POST, API, json=body, status=200)
se = StringEncrypt("", session=requests_session())
se.enableCompression = False
r = se.encrypt_string("hi", "lbl")
assert r is not False
assert r["error"] == 0
assert r["error_string"] == "ERROR_SUCCESS"
assert r["source"] == "print('ok')\n"
@responses.activate
def test_encrypt_success_compressed_source_roundtrip():
plain = "decrypted source"
compressed_field = base64.b64encode(
zlib.compress(plain.encode("utf-8"), 9)
).decode("ascii")
body = {"error": 0, "source": compressed_field}
responses.add(responses.POST, API, json=body, status=200)
se = StringEncrypt("", session=requests_session())
se.enableCompression = True
r = se.encrypt_string("hello", "lab")
assert r is not False
assert r["source"] == plain
@responses.activate
def test_http_error_returns_false():
responses.add(responses.POST, API, status=500)
se = StringEncrypt("", session=requests_session())
assert se.encrypt_string("a", "b") is False
@responses.activate
def test_invalid_json_returns_false():
responses.add(responses.POST, API, body="not json", status=200)
se = StringEncrypt("", session=requests_session())
assert se.encrypt_string("a", "b") is False
@responses.activate
def test_unknown_error_code_maps_to_unknown_string():
body = {"error": 99999, "source": ""}
responses.add(responses.POST, API, json=body, status=200)
se = StringEncrypt("", session=requests_session())
se.enableCompression = False
r = se.encrypt_string("x", "y")
assert r is not False
assert r["error_string"] == "ERROR_UNKNOWN_99999"
@responses.activate
def test_info_command():
body = {"engine_version": "v1.5.0", "supported_languages": [{"cpp": "C / C++"}]}
responses.add(responses.POST, API, json=body, status=200)
se = StringEncrypt("CODE", session=requests_session())
r = se.info()
assert r is not False
assert r["engine_version"] == "v1.5.0"
assert "error_string" not in r
@responses.activate
def test_encrypt_posts_include_flags():
responses.add(responses.POST, API, json={"error": 0, "source": "x"}, status=200)
se = StringEncrypt("", session=requests_session())
se.enableCompression = False
se.includeTags = True
se.includeExample = True
se.returnTemplate = True
se.includeDebugComments = True
se.highlight = "js"
se.encryptionTemplate = '{"commands":[]}'
se.encrypt_string("s", "l")
assert len(responses.calls) == 1
sent = responses.calls[0].request.body
if isinstance(sent, bytes):
sent = sent.decode("utf-8")
q = parse_qs(sent, keep_blank_values=True)
assert q.get("include_tags") == ["1"]
assert q.get("include_example") == ["1"]
assert q.get("return_template") == ["1"]
assert q.get("include_debug_comments") == ["1"]
assert q.get("highlight") == ["js"]
assert "template" in q and q["template"]
def test_encrypt_file_empty(tmp_path):
p = tmp_path / "empty.bin"
p.write_bytes(b"")
se = StringEncrypt("")
assert se.encrypt_file_contents(p, "lbl") is False
def test_encrypt_file_reads_bytes(tmp_path, monkeypatch):
p = tmp_path / "one.bin"
p.write_bytes(b"\x00\xff")
captured = {}
def fake_post(self, params):
captured["bytes"] = params.get("bytes")
return {"error": 0, "source": ""}
monkeypatch.setattr(StringEncrypt, "post_request", fake_post)
se = StringEncrypt("")
se.enableCompression = False
se.encrypt_file_contents(p, "x")
assert captured["bytes"] == b"\x00\xff"
def requests_session():
import requests
return requests.Session()
@responses.activate
def test_corrupt_compressed_source_returns_false():
body = {"error": 0, "source": "not-valid-base64!!!"}
responses.add(responses.POST, API, json=body, status=200)
se = StringEncrypt("", session=requests_session())
se.enableCompression = True
assert se.encrypt_string("a", "b") is False