-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestServerCommunication.py
257 lines (203 loc) · 8.45 KB
/
TestServerCommunication.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
'''
Created on Nov 30, 2012
@author: morganl
'''
import unittest, ssl, socket, random, itertools
import ServerCommunication as sc
class TestServerCommunication(unittest.TestCase):
'''
Unit testing for the ServerCommunication module. Assumes there is a dataset called fakedset and
users called 'lev' (an administrator) and 'bob'
Tests methods:
xhandle_response
xsafe_recv
xsafe_send
xsimple_request
xauth
xlst
xnew_dset
xmatrix_req
xrename
xshare
xunshare
xcopy
xcopy_shared
xremove
xremove_shared
xclose
xrename_admin
xremove_admin
'''
#TODO: Test all possible error states
def setUp(self):
# Set up a socket
client_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sec_clisock = ssl.wrap_socket(client_sock)
sec_clisock.settimeout(10)
sec_clisock.connect((socket.gethostname(), 14338))
self.sock = sec_clisock
# Authenticate
sc.auth(self.sock, 'lev', 'lev')
# FIXME: Make sure lev, bob, and testdset exist
def tearDown(self):
sc.close(self.sock)
self.sock.close()
def test_new_dset_and_remove(self):
sc.new_dset(self.sock,
'/home/morganl/workspace/COVI Server/src/COVIServer/fakedset1.tar.gz',
'fakedset1')
dsets = sc.lst(self.sock)['list']
self.assertTrue(('fakedset1' in dsets), "New dataset uploaded")
sc.remove(self.sock, 'fakedset1')
dsets = sc.lst(self.sock)['list']
self.assertTrue(not ('fakedset1' in dsets), "New dataset deleted")
def test_list_and_matrix(self):
# Try and verify a list request
dsets = sc.lst(self.sock)
dkeys = dsets.keys()
self.assertTrue("list" in dkeys,
"list in lst response")
self.assertTrue("shared" in dkeys,
"shared in lst response")
self.assertTrue("requests" in dkeys,
"requests in lst response")
self.assertTrue("user's shares" in dkeys,
"user's shares in lst response")
for i in itertools.chain(dsets['list']):
# Request a random matrix for each dset
# Nothing to assert here, the method checks the data
sc.matrix_req(self.sock, i, random.randint(1,500))
def test_copy_and_remove(self):
# Choose a test element
dsets = sc.lst(self.sock)
dsets = dsets['list']
self.assertGreater(len(dsets), 0)
test_elt = dsets[0]
test_elt_copy = dsets[0]+"copied"
# Try and verify a copy operation
sc.copy(self.sock, test_elt, test_elt_copy)
dsets = sc.lst(self.sock)['list']
self.assertTrue(test_elt_copy in dsets, "Copied element exists")
# Try a verify a remove operation
sc.remove(self.sock, test_elt_copy)
dsets = sc.lst(self.sock)['list']
self.assertTrue(not (test_elt_copy in dsets), "Copied element deleted")
def test_rename(self):
# Get a list of datasets
dsets = sc.lst(self.sock)['list']
# Choose an elt and rename
test_elt = dsets[0]
test_elt_renamed = dsets[0]+'renamed'
sc.rename(self.sock, test_elt, test_elt_renamed)
# Get a new list of datasets
dsets = sc.lst(self.sock)
dsets = dsets['list']
# Make sure original is not present and new one is
self.assertTrue(
(test_elt_renamed in dsets)
and
(not (test_elt in dsets)),
"Copied element deleted")
sc.rename(self.sock, test_elt_renamed, test_elt)
# Get a new list of datasets
dsets = sc.lst(self.sock)
dsets = dsets['list']
# Make sure the second rename also worked
self.assertTrue(
(not (test_elt_renamed in dsets))
and
(not (test_elt in dsets),
"Copied element deleted"))
def test_share_and_unshare(self):
# Get a list of datasets
dsets = sc.lst(self.sock)['list']
test_elt = dsets[0]
# Share the dataset with a user
sc.share(self.sock, test_elt, 'bob', 0)
dsets = sc.lst(self.sock)
dsets = dsets["user's shares"]
self.assertTrue((["lev", "bob", test_elt, 0, 1] in dsets),
"Dataset shared successfully")
# Unshare the shared dataset
sc.unshare(self.sock, test_elt, 'bob')
dsets = sc.lst(self.sock)["user's shares"]
self.assertTrue(not (["lev", "bob", test_elt, 0, 1] in dsets),
"Dataset unshared successfully")
def test_copy_shared(self):
# Get a list of datasets
dsets = sc.lst(self.sock)['shared']
test_elt = dsets[0]
copied_elt = test_elt[2]+'copied'
# Copy a shared dataset
sc.copy_shared(self.sock, test_elt[2], copied_elt, test_elt[0])
# Assert copy succeeded
dsets = sc.lst(self.sock)['list']
self.assertIn(copied_elt, dsets, "Shared dataset not copied")
# Remove copied dataset
sc.remove(self.sock, copied_elt)
# Assert remove succeeded
dsets = sc.lst(self.sock)['list']
self.assertNotIn(copied_elt, dsets, "Shared dataset copy not removed")
def test_remove_shared(self):
# Get a list of datasets
dsets = sc.lst(self.sock)['list']
test_elt = dsets[0]
# Share test_elt with bob
sc.share(self.sock, test_elt, 'bob', 0)
# auth as bob
sc.auth(self.sock, 'bob', 'bob')
# remove shared dataset
sc.remove_shared(self.sock, test_elt, 'lev')
# verify dataset was removed
dsets = sc.lst(self.sock)["requests"]
self.assertTrue(not (["lev", "bob", test_elt, 0, 1] in dsets),
"Shared dataset removed successfully")
def test_rename_admin(self):
# Get a dataset owned by bob
sc.auth(self.sock, 'bob', 'bob')
dsets = sc.lst(self.sock)['list']
test_elt = dsets[0]
ren_elt = test_elt+'renamed'
# Rename the dataset
sc.auth(self.sock, 'lev', 'lev')
sc.rename_admin(self.sock, test_elt, ren_elt, 'bob')
# Verify the rename
sc.auth(self.sock, 'bob', 'bob')
dsets = sc.lst(self.sock)['list']
self.assertIn(ren_elt, dsets, "Other user's dataset not renamed!")
# Put things back the way they were
sc.rename(self.sock, ren_elt, test_elt)
sc.auth(self.sock, 'lev', 'lev')
def test_remove_admin(self):
# Get a dataset owned by bob
sc.auth(self.sock, 'bob', 'bob')
dsets = sc.lst(self.sock)['list']
test_elt = dsets[0]
cop_elt = test_elt+'copied'
sc.copy(self.sock, test_elt, cop_elt)
# Verify the copied element is there
dsets = sc.lst(self.sock)['list']
self.assertIn(cop_elt, dsets, "Other user's dataset not copied!")
# Rename the dataset
sc.auth(self.sock, 'lev', 'lev')
sc.remove_admin(self.sock, cop_elt, 'bob')
# Verify the rename
sc.auth(self.sock, 'bob', 'bob')
dsets = sc.lst(self.sock)['list']
self.assertNotIn(cop_elt, dsets, "Other user's dataset not removed!")
# Put things back the way they were
sc.auth(self.sock, 'lev', 'lev')
def test_clusters(self):
clusters = sc.cluster(self.sock, "fakedset")
# Try to parse the first entry, not catching any exceptions
self.assertRegexpMatches(clusters, r'[0-9\n]+', "Invalid cluster data")
def test_shared_clusters(self):
# Get a list of shared datasets
dsets = sc.lst(self.sock)['shared']
test_elt = dsets[0]
clusters = sc.shared_cluster(self.sock, test_elt[2], test_elt[0])
self.assertRegexpMatches(clusters, r'[0-9\n]+', "Invalid cluster data")
if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testName']
unittest.main()
#TestServerCommunication()