-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_client.py
239 lines (178 loc) · 6.96 KB
/
test_client.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
import sys
import json
import time
import socket
import hashlib
#HOST, PORT = "localhost", 9999
HOST, PORT = "35.170.194.23", 9999
#HOST, PORT = "52.91.188.21", 9999
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(2)
username = '1957295fdsahjklbfdjk'
email = '[email protected]'
uid = hashlib.md5(username.encode('utf-8')).hexdigest()
def run_test(fn):
try:
fn()
return True
except Exception as e:
print(e)
return False
def test_echo():
data = {}
data['cmd'] = 'echo'
data['useless'] = 'thisisauselessstringofinfromationthatwillberemoved'
sock.sendto(bytes(json.dumps(data), "utf-8"), (HOST, PORT))
received = json.loads(str(sock.recv(1024), "utf-8"))
print("Sent: {}".format(json.dumps(data, indent=4, sort_keys=True)))
print("Received: {}".format(json.dumps(received, indent=4, sort_keys=True)))
def list_users():
data = {}
data['cmd'] = 'list_users'
print("Sent: {}".format(json.dumps(data, indent=4, sort_keys=True)))
sock.sendto(bytes(json.dumps(data), "utf-8"), (HOST, PORT))
received = json.loads(str(sock.recv(1024), "utf-8"))
print("Received: {}".format(json.dumps(received, indent=4, sort_keys=True)))
def add_user():
data = {}
data['cmd'] = 'add_user'
data['mcusername'] = username
data['email'] = email
print("Sent: {}".format(json.dumps(data, indent=4, sort_keys=True)))
sock.sendto(bytes(json.dumps(data), "utf-8"), (HOST, PORT))
received = json.loads(str(sock.recv(1024), "utf-8"))
print("Received: {}".format(json.dumps(received, indent=4, sort_keys=True)))
def remove_user():
data = {}
data['cmd'] = 'remove_user'
data['uid'] = uid
print("Sent: {}".format(json.dumps(data, indent=4, sort_keys=True)))
sock.sendto(bytes(json.dumps(data), "utf-8"), (HOST, PORT))
received = json.loads(str(sock.recv(1024), "utf-8"))
print("Received: {}".format(json.dumps(received, indent=4, sort_keys=True)))
def get_status():
data = {}
data['cmd'] = 'get_status'
data['uid'] = uid
print("Sent: {}".format(json.dumps(data, indent=4, sort_keys=True)))
sock.sendto(bytes(json.dumps(data), "utf-8"), (HOST, PORT))
received = json.loads(str(sock.recv(1024), "utf-8"))
print("Received: {}".format(json.dumps(received, indent=4, sort_keys=True)))
def get_invalid_status():
data = {}
data['cmd'] = 'get_status'
data['uid'] = uid + "__fake"
print("Testing non-existent user: ")
print("Sent: {}".format(json.dumps(data, indent=4, sort_keys=True)))
sock.sendto(bytes(json.dumps(data), "utf-8"), (HOST, PORT))
received = json.loads(str(sock.recv(1024), "utf-8"))
print("Received: {}".format(json.dumps(received, indent=4, sort_keys=True)))
def make_awesome():
data = {}
data['cmd'] = 'make_awesome'
data['uid'] = uid
print("Sent: {}".format(json.dumps(data, indent=4, sort_keys=True)))
sock.sendto(bytes(json.dumps(data), "utf-8"), (HOST, PORT))
received = json.loads(str(sock.recv(1024), "utf-8"))
print("Received: {}".format(json.dumps(received, indent=4, sort_keys=True)))
def get_minecraft_key_and_validate():
data = {}
data['cmd'] = 'get_minecraft_key'
data['uid'] = uid
print("Getting Key: ")
print("Sent: {}".format(json.dumps(data, indent=4, sort_keys=True)))
sock.sendto(bytes(json.dumps(data), "utf-8"), (HOST, PORT))
received = json.loads(str(sock.recv(1024), "utf-8"))
print("Received: {}".format(json.dumps(received, indent=4, sort_keys=True)))
if 'minecraft_key' in received:
print("Validating Key: ")
key = received['minecraft_key']
data = {}
data['cmd'] = 'validate_minecraft_key'
data['uid'] = uid
data['minecraft_key'] = key
print("Sent: {}".format(json.dumps(data, indent=4, sort_keys=True)))
sock.sendto(bytes(json.dumps(data), "utf-8"), (HOST, PORT))
received = json.loads(str(sock.recv(1024), "utf-8"))
print("Received: {}".format(json.dumps(received, indent=4, sort_keys=True)))
else:
print("Not Validating Key - no minecraft_key in response")
def get_firehose_key_and_return():
data = {}
data['cmd'] = 'get_firehose_key'
data['uid'] = uid
print("Getting Firehose Stream: ")
print("Sent: {}".format(json.dumps(data, indent=4, sort_keys=True)))
sock.sendto(bytes(json.dumps(data), "utf-8"), (HOST, PORT))
received = json.loads(str(sock.recv(1024), "utf-8"))
print("Received: {}".format(json.dumps(received, indent=4, sort_keys=True)))
if 'stream_name' in received:
print("Returning Stream: ")
name = received['stream_name']
data = {}
data['cmd'] = 'return_firehose_key'
data['uid'] = uid
data['stream_name'] = name
print("Sent: {}".format(json.dumps(data, indent=4, sort_keys=True)))
sock.sendto(bytes(json.dumps(data), "utf-8"), (HOST, PORT))
received = json.loads(str(sock.recv(1024), "utf-8"))
print("Received: {}".format(json.dumps(received, indent=4, sort_keys=True)))
else:
print("Not Returning Key - no stream_name in response")
def get_firehose_key():
data = {}
data['cmd'] = 'get_firehose_key'
data['uid'] = uid
print("### Getting Firehose Stream: ")
print("Sent: {}".format(json.dumps(data, indent=4, sort_keys=True)))
sock.sendto(bytes(json.dumps(data), "utf-8"), (HOST, PORT))
received = json.loads(str(sock.recv(1024), "utf-8"))
print("Received: {}".format(json.dumps(received, indent=4, sort_keys=True)))
def disconnect_user():
print("Disconnecting User: ")
data = {}
data['cmd'] = 'disconnect_user'
data['uid'] = uid
print("### Disconnecting User: ")
print("Sent: {}".format(json.dumps(data, indent=4, sort_keys=True)))
sock.sendto(bytes(json.dumps(data), "utf-8"), (HOST, PORT))
received = json.loads(str(sock.recv(1024), "utf-8"))
print("Received: {}".format(json.dumps(received, indent=4, sort_keys=True)))
run = True
while run:
# Ping the user server with echo command
run_test(test_echo)
print()
# Add a user
run_test(add_user)
print()
# Look at their status
run_test(get_status)
print()
# Make that user Awesome
run_test(make_awesome)
print()
# Look at their status
run_test(get_status)
print()
# Get a minecraft key for that user and validate it
run_test(get_minecraft_key_and_validate)
print()
# # Get a firehose stream from the pool and return it
run_test(get_firehose_key_and_return)
print()
# Get a firehose stream from the pool and disconnect them
run_test(get_firehose_key)
print()
run_test(disconnect_user)
print()
# Remove that user
run_test(remove_user)
print()
# Try to get a status from an invalid user
run_test(get_invalid_status)
print()
run_test(list_users)
print()
run = False
time.sleep(1)