-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathtest_daemon.py
301 lines (264 loc) · 14.1 KB
/
test_daemon.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
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
import aiohttp
import asyncio
import json
import logging
import pytest
from chia.daemon.server import WebSocketServer
from chia.server.outbound_message import NodeType
from chia.types.peer_info import PeerInfo
from tests.block_tools import BlockTools
from chia.util.ints import uint16
from chia.util.keyring_wrapper import DEFAULT_PASSPHRASE_IF_NO_MASTER_PASSPHRASE
from chia.util.ws_message import create_payload
from tests.core.node_height import node_height_at_least
from tests.time_out_assert import time_out_assert_custom_interval, time_out_assert
class TestDaemon:
@pytest.mark.asyncio
async def test_daemon_simulation(self, self_hostname, daemon_simulation, bt, get_b_tools, get_b_tools_1):
node1, node2, _, _, _, _, _, _, _, _, server1, daemon1 = daemon_simulation
node2_port = node2.full_node.config["port"]
await server1.start_client(PeerInfo(self_hostname, uint16(node2_port)))
async def num_connections():
count = len(node2.server.connection_by_type[NodeType.FULL_NODE].items())
return count
await time_out_assert_custom_interval(60, 1, num_connections, 1)
await time_out_assert(1500, node_height_at_least, True, node2, 1)
session = aiohttp.ClientSession()
log = logging.getLogger()
log.warning(f"Connecting to daemon on port {daemon1.daemon_port}")
ws = await session.ws_connect(
f"wss://127.0.0.1:{daemon1.daemon_port}",
autoclose=True,
autoping=True,
heartbeat=60,
ssl_context=get_b_tools.get_daemon_ssl_context(),
max_msg_size=100 * 1024 * 1024,
)
service_name = "test_service_name"
data = {"service": service_name}
payload = create_payload("register_service", data, service_name, "daemon")
await ws.send_str(payload)
message_queue = asyncio.Queue()
async def reader(ws, queue):
while True:
msg = await ws.receive()
if msg.type == aiohttp.WSMsgType.TEXT:
message = msg.data.strip()
message = json.loads(message)
await queue.put(message)
elif msg.type == aiohttp.WSMsgType.PING:
await ws.pong()
elif msg.type == aiohttp.WSMsgType.PONG:
continue
else:
if msg.type == aiohttp.WSMsgType.CLOSE:
await ws.close()
elif msg.type == aiohttp.WSMsgType.ERROR:
await ws.close()
elif msg.type == aiohttp.WSMsgType.CLOSED:
pass
break
read_handler = asyncio.create_task(reader(ws, message_queue))
data = {}
payload = create_payload("get_blockchain_state", data, service_name, "chia_full_node")
await ws.send_str(payload)
await asyncio.sleep(5)
blockchain_state_found = False
while not message_queue.empty():
message = await message_queue.get()
if message["command"] == "get_blockchain_state":
blockchain_state_found = True
await ws.close()
read_handler.cancel()
assert blockchain_state_found
# Suppress warning: "The explicit passing of coroutine objects to asyncio.wait() is deprecated since Python 3.8..."
# Can be removed when we upgrade to a newer version of websockets (9.1 works)
@pytest.mark.filterwarnings("ignore::DeprecationWarning:websockets.*")
@pytest.mark.asyncio
async def test_validate_keyring_passphrase_rpc(self, get_daemon_with_temp_keyring):
local_b_tools: BlockTools = get_daemon_with_temp_keyring[0]
keychain = local_b_tools.local_keychain
# When: the keychain has a master passphrase set
keychain.set_master_passphrase(
current_passphrase=DEFAULT_PASSPHRASE_IF_NO_MASTER_PASSPHRASE, new_passphrase="the correct passphrase"
)
async def check_success_case(response: aiohttp.http_websocket.WSMessage):
# Expect: JSON response
assert response.type == aiohttp.WSMsgType.TEXT
message = json.loads(response.data.strip())
# Expect: daemon handled the request
assert message["ack"] is True
# Expect: success flag is set to True
assert message["data"]["success"] is True
async def check_bad_passphrase_case(response: aiohttp.http_websocket.WSMessage):
# Expect: JSON response
assert response.type == aiohttp.WSMsgType.TEXT
message = json.loads(response.data.strip())
# Expect: daemon handled the request
assert message["ack"] is True
# Expect: success flag is set to False
assert message["data"]["success"] is False
async def check_missing_passphrase_case(response: aiohttp.http_websocket.WSMessage):
# Expect: JSON response
assert response.type == aiohttp.WSMsgType.TEXT
message = json.loads(response.data.strip())
# Expect: daemon handled the request
assert message["ack"] is True
# Expect: success flag is set to False
assert message["data"]["success"] is False
# Expect: error string is set
assert message["data"]["error"] == "missing key"
async def check_empty_passphrase_case(response: aiohttp.http_websocket.WSMessage):
# Expect: JSON response
assert response.type == aiohttp.WSMsgType.TEXT
message = json.loads(response.data.strip())
# Expect: daemon handled the request
assert message["ack"] is True
# Expect: success flag is set to False
assert message["data"]["success"] is False
async with aiohttp.ClientSession() as session:
async with session.ws_connect(
f"wss://127.0.0.1:{local_b_tools._config['daemon_port']}",
autoclose=True,
autoping=True,
heartbeat=60,
ssl=local_b_tools.get_daemon_ssl_context(),
max_msg_size=52428800,
) as ws:
# When: using the correct passphrase
await ws.send_str(
create_payload("validate_keyring_passphrase", {"key": "the correct passphrase"}, "test", "daemon")
)
# Expect: validation succeeds
await check_success_case(await ws.receive())
# When: using the wrong passphrase
await ws.send_str(
create_payload("validate_keyring_passphrase", {"key": "the wrong passphrase"}, "test", "daemon")
)
# Expect: validation failure
await check_bad_passphrase_case(await ws.receive())
# When: not including the passphrase in the payload
await ws.send_str(create_payload("validate_keyring_passphrase", {}, "test", "daemon"))
# Expect: validation failure
await check_missing_passphrase_case(await ws.receive())
# When: including an empty passphrase in the payload
await ws.send_str(create_payload("validate_keyring_passphrase", {"key": ""}, "test", "daemon"))
# Expect: validation failure
await check_empty_passphrase_case(await ws.receive())
# Suppress warning: "The explicit passing of coroutine objects to asyncio.wait() is deprecated since Python 3.8..."
# Can be removed when we upgrade to a newer version of websockets (9.1 works)
@pytest.mark.filterwarnings("ignore::DeprecationWarning:websockets.*")
@pytest.mark.asyncio
async def test_add_private_key(self, get_daemon_with_temp_keyring):
local_b_tools: BlockTools = get_daemon_with_temp_keyring[0]
daemon: WebSocketServer = get_daemon_with_temp_keyring[1]
keychain = daemon.keychain_server._default_keychain # Keys will be added here
test_mnemonic = (
"grief lock ketchup video day owner torch young work "
"another venue evidence spread season bright private "
"tomato remind jaguar original blur embody project can"
)
test_fingerprint = 2877570395
mnemonic_with_typo = f"{test_mnemonic}xyz" # intentional typo: can -> canxyz
mnemonic_with_missing_word = " ".join(test_mnemonic.split(" ")[:-1]) # missing last word
async def check_success_case(response: aiohttp.http_websocket.WSMessage):
nonlocal keychain
# Expect: JSON response
assert response.type == aiohttp.WSMsgType.TEXT
message = json.loads(response.data.strip())
# Expect: daemon handled the request
assert message["ack"] is True
# Expect: success flag is set to True
assert message["data"]["success"] is True
# Expect: the keychain has the new key
assert keychain.get_private_key_by_fingerprint(test_fingerprint) is not None
async def check_missing_param_case(response: aiohttp.http_websocket.WSMessage):
# Expect: JSON response
assert response.type == aiohttp.WSMsgType.TEXT
message = json.loads(response.data.strip())
# Expect: daemon handled the request
assert message["ack"] is True
# Expect: success flag is set to False
assert message["data"]["success"] is False
# Expect: error field is set to "malformed request"
assert message["data"]["error"] == "malformed request"
# Expect: error_details message is set to "missing mnemonic and/or passphrase"
assert message["data"]["error_details"]["message"] == "missing mnemonic and/or passphrase"
async def check_mnemonic_with_typo_case(response: aiohttp.http_websocket.WSMessage):
# Expect: JSON response
assert response.type == aiohttp.WSMsgType.TEXT
message = json.loads(response.data.strip())
# Expect: daemon handled the request
assert message["ack"] is True
# Expect: success flag is set to False
assert message["data"]["success"] is False
# Expect: error field is set to "'canxyz' is not in the mnemonic dictionary; may be misspelled"
assert message["data"]["error"] == "'canxyz' is not in the mnemonic dictionary; may be misspelled"
async def check_invalid_mnemonic_length_case(response: aiohttp.http_websocket.WSMessage):
# Expect: JSON response
assert response.type == aiohttp.WSMsgType.TEXT
message = json.loads(response.data.strip())
# Expect: daemon handled the request
assert message["ack"] is True
# Expect: success flag is set to False
assert message["data"]["success"] is False
# Expect: error field is set to "Invalid mnemonic length"
assert message["data"]["error"] == "Invalid mnemonic length"
async def check_invalid_mnemonic_case(response: aiohttp.http_websocket.WSMessage):
# Expect: JSON response
assert response.type == aiohttp.WSMsgType.TEXT
message = json.loads(response.data.strip())
# Expect: daemon handled the request
assert message["ack"] is True
# Expect: success flag is set to False
assert message["data"]["success"] is False
# Expect: error field is set to "Invalid order of mnemonic words"
assert message["data"]["error"] == "Invalid order of mnemonic words"
async with aiohttp.ClientSession() as session:
async with session.ws_connect(
f"wss://127.0.0.1:{local_b_tools._config['daemon_port']}",
autoclose=True,
autoping=True,
heartbeat=60,
ssl=local_b_tools.get_daemon_ssl_context(),
max_msg_size=52428800,
) as ws:
# Expect the key hasn't been added yet
assert keychain.get_private_key_by_fingerprint(test_fingerprint) is None
await ws.send_str(
create_payload("add_private_key", {"mnemonic": test_mnemonic, "passphrase": ""}, "test", "daemon")
)
# Expect: key was added successfully
await check_success_case(await ws.receive())
# When: missing mnemonic
await ws.send_str(create_payload("add_private_key", {"passphrase": ""}, "test", "daemon"))
# Expect: Failure due to missing mnemonic
await check_missing_param_case(await ws.receive())
# When: missing passphrase
await ws.send_str(create_payload("add_private_key", {"mnemonic": test_mnemonic}, "test", "daemon"))
# Expect: Failure due to missing passphrase
await check_missing_param_case(await ws.receive())
# When: using a mmnemonic with an incorrect word (typo)
await ws.send_str(
create_payload(
"add_private_key", {"mnemonic": mnemonic_with_typo, "passphrase": ""}, "test", "daemon"
)
)
# Expect: Failure due to misspelled mnemonic
await check_mnemonic_with_typo_case(await ws.receive())
# When: using a mnemonic with an incorrect word count
await ws.send_str(
create_payload(
"add_private_key", {"mnemonic": mnemonic_with_missing_word, "passphrase": ""}, "test", "daemon"
)
)
# Expect: Failure due to invalid mnemonic
await check_invalid_mnemonic_length_case(await ws.receive())
# When: using an incorrect mnemnonic
await ws.send_str(
create_payload(
"add_private_key", {"mnemonic": " ".join(["abandon"] * 24), "passphrase": ""}, "test", "daemon"
)
)
# Expect: Failure due to checksum error
await check_invalid_mnemonic_case(await ws.receive())