-
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathtest_handshake.py
More file actions
287 lines (220 loc) · 9.58 KB
/
test_handshake.py
File metadata and controls
287 lines (220 loc) · 9.58 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
import base64
import json
import struct
import pytest
import six
from mock import ANY, Mock, call, patch
from rethinkdb.errors import ReqlAuthError, ReqlDriverError
from rethinkdb.handshake import HandshakeV1_0, LocalThreadCache
from rethinkdb.helpers import chain_to_bytes
from rethinkdb.ql2_pb2 import VersionDummy
@pytest.mark.unit
class TestLocalThreadCache(object):
def setup_method(self):
self.cache = LocalThreadCache()
self.cache_key = "test"
self.cache_value = "cache"
def test_initialization(self):
assert self.cache._cache == dict()
def test_add_to_cache(self):
self.cache.set(self.cache_key, self.cache_value)
assert self.cache._cache == {self.cache_key: self.cache_value}
def test_get_from_cache(self):
self.cache._cache = {self.cache_key: self.cache_value}
cached_value = self.cache.get(self.cache_key)
assert cached_value == self.cache_value
@pytest.mark.unit
class TestHandshake(object):
def setup_method(self):
self.encoder = json.JSONEncoder()
self.decoder = json.JSONDecoder()
self.handshake = self._get_handshake()
def _get_handshake(self):
return HandshakeV1_0(
json_encoder=self.encoder,
json_decoder=self.decoder,
host="localhost",
port=28015,
username="admin",
password="",
)
@patch("rethinkdb.handshake.HandshakeV1_0._get_pbkdf2_hmac")
@patch("rethinkdb.handshake.HandshakeV1_0._get_compare_digest")
def test_initialization(self, mock_get_compare_digest, mock_get_pbkdf2_hmac):
handshake = self._get_handshake()
assert handshake.VERSION == VersionDummy.Version.V1_0
assert handshake.PROTOCOL == VersionDummy.Protocol.JSON
assert mock_get_compare_digest.called is True
assert mock_get_pbkdf2_hmac.called is True
@patch("rethinkdb.handshake.hmac")
def test_get_builtin_compare_digest(self, mock_hmac):
mock_hmac.compare_digest = Mock
handshake = self._get_handshake()
assert handshake._compare_digest == mock_hmac.compare_digest
@patch("rethinkdb.handshake.compare_digest")
@patch("rethinkdb.handshake.hmac")
def test_get_own_compare_digest(self, mock_hmac, mock_compare_digest):
delattr(mock_hmac, "compare_digest")
handshake = self._get_handshake()
assert handshake._compare_digest == mock_compare_digest
@patch("rethinkdb.handshake.hashlib")
def test_get_builtin_get_pbkdf2_hmac(self, mock_hashlib):
mock_hashlib.pbkdf2_hmac = Mock
handshake = self._get_handshake()
assert handshake._pbkdf2_hmac == mock_hashlib.pbkdf2_hmac
@patch("rethinkdb.handshake.pbkdf2_hmac")
@patch("rethinkdb.handshake.hashlib")
def test_get_own_get_pbkdf2_hmac(self, mock_hashlib, mock_pbkdf2_hmac):
delattr(mock_hashlib, "pbkdf2_hmac")
handshake = self._get_handshake()
assert handshake._pbkdf2_hmac == mock_pbkdf2_hmac
def test_decode_json_response(self):
expected_response = {"success": True}
decoded_response = self.handshake._decode_json_response(
json.dumps(expected_response)
)
assert decoded_response == expected_response
def test_decode_json_response_utf8_encoded(self):
expected_response = {"success": True}
decoded_response = self.handshake._decode_json_response(
json.dumps(expected_response), True
)
assert decoded_response == expected_response
def test_decode_json_response_auth_error(self):
expected_response = {
"success": False,
"error_code": 15,
"error": "test error message",
}
with pytest.raises(ReqlAuthError):
decoded_response = self.handshake._decode_json_response(
json.dumps(expected_response)
)
def test_decode_json_response_driver_error(self):
expected_response = {
"success": False,
"error_code": 30,
"error": "test error message",
}
with pytest.raises(ReqlDriverError):
decoded_response = self.handshake._decode_json_response(
json.dumps(expected_response)
)
def test_next_state(self):
previous_state = self.handshake._state
self.handshake._next_state()
new_state = self.handshake._state
assert previous_state == 0
assert new_state == 1
def test_reset(self):
self.handshake._random_nonce = Mock()
self.handshake._first_client_message = Mock()
self.handshake._server_signature = Mock()
self.handshake._state = Mock()
self.handshake.reset()
assert self.handshake._random_nonce is None
assert self.handshake._first_client_message is None
assert self.handshake._server_signature is None
assert self.handshake._state == 0
@patch("rethinkdb.handshake.base64")
def test_init_connection(self, mock_base64):
self.handshake._next_state = Mock()
encoded_string = "test"
mock_base64.standard_b64encode.return_value = encoded_string
first_client_message = chain_to_bytes(
"n=", self.handshake._username, ",r=", encoded_string
)
expected_result = chain_to_bytes(
struct.pack("<L", self.handshake.VERSION),
self.handshake._json_encoder.encode(
{
"protocol_version": self.handshake._protocol_version,
"authentication_method": "SCRAM-SHA-256",
"authentication": chain_to_bytes(
"n,,", first_client_message
).decode("ascii"),
}
).encode("utf-8"),
b"\0",
)
result = self.handshake._init_connection(response=None)
assert result == expected_result
assert self.handshake._next_state.called is True
def test_init_connection_unexpected_response(self):
self.handshake._next_state = Mock()
with pytest.raises(ReqlDriverError):
result = self.handshake._init_connection(response=Mock())
assert self.handshake._next_state.called is False
def test_read_response(self):
self.handshake._next_state = Mock()
response = {
"success": True,
"min_protocol_version": 0,
"max_protocol_version": 1,
}
result = self.handshake._read_response(json.dumps(response))
assert result == ""
assert self.handshake._next_state.called is True
def test_read_response_error_received(self):
self.handshake._next_state = Mock()
with pytest.raises(ValueError):
result = self.handshake._read_response("ERROR")
assert self.handshake._next_state.called is False
def test_read_response_protocol_mismatch(self):
self.handshake._next_state = Mock()
response = {
"success": True,
"min_protocol_version": -1,
"max_protocol_version": -1,
}
with pytest.raises(ReqlDriverError):
result = self.handshake._read_response(json.dumps(response))
assert self.handshake._next_state.called is False
def test_prepare_auth_request(self):
self.handshake._next_state = Mock()
self.handshake._random_nonce = (
base64.encodebytes(b"random_nonce")
if six.PY3
else base64.b64encode(b"random_nonce")
)
self.handshake._first_client_message = chain_to_bytes(
"n=", self.handshake._username, ",r=", self.handshake._random_nonce
)
response = {
"success": True,
"authentication": "s=cmFuZG9tX25vbmNl\n,i=2,r=cmFuZG9tX25vbmNl\n",
}
if six.PY3:
expected_result = b'{"authentication": "c=biws,r=cmFuZG9tX25vbmNl\\n,p=2Tpd60LM4Tkhe7VATTPj/lh4yunl07Sm4A+m3ukC774="}\x00'
else:
expected_result = b'{"authentication": "c=biws,r=cmFuZG9tX25vbmNl\\n,p=JqVP98bzu3yye/3SLopNJvCRimBx34uKI/EY8UI41gM="}\x00'
result = self.handshake._prepare_auth_request(json.dumps(response))
assert isinstance(result, six.binary_type)
assert result == expected_result
assert self.handshake._next_state.called is True
def test_prepare_auth_request_invalid_nonce(self):
self.handshake._next_state = Mock()
self.handshake._random_nonce = (
base64.encodebytes(b"invalid") if six.PY3 else base64.b64encode(b"invalid")
)
response = {
"success": True,
"authentication": "s=fake,i=2,r=cmFuZG9tX25vbmNl\n",
}
with pytest.raises(ReqlAuthError):
result = self.handshake._prepare_auth_request(json.dumps(response))
assert self.handshake._next_state.called is False
def test_read_auth_response(self):
self.handshake._next_state = Mock()
self.handshake._server_signature = b"signature"
response = {"success": True, "authentication": "v=c2lnbmF0dXJl\n"}
result = self.handshake._read_auth_response(json.dumps(response))
assert result is None
assert self.handshake._next_state.called is True
def test_read_auth_response_invalid_server_signature(self):
self.handshake._next_state = Mock()
self.handshake._server_signature = b"invalid-signature"
response = {"success": True, "authentication": "v=c2lnbmF0dXJl\n"}
with pytest.raises(ReqlAuthError):
result = self.handshake._read_auth_response(json.dumps(response))
assert self.handshake._next_state.called is False