-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
247 lines (224 loc) · 6.45 KB
/
test.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
# Oden
# Copyright (C) 2010 Robin Nilsson <robinnilsson@gmail.com>
#
# This is released under GNU aGPL, see COPYRIGHT for full license.
# ------------------------------------------------------------------------------
# Execute this file to test basic functionality of ODEN
# ------------------------------------------------------------------------------
# TODO:
# Replace these tests with unittest
import config
from lib.oden import rsa_helper,aes_helper,salmon
from data import people
from data import user
from data import aspects
# Switch to database 'oden-test' while testing
config.mongo_db = "oden-test"
# Counters
success = 0
fail = 0
# Disable/Enable tests
test_rsa = True
test_aes = True
test_salmon = True
test_new_people = True
test_new_user = True
test_new_aspect = True
test_get_user_by_user = True
test_get_user_by_id = True
test_get_people_by_id = True
# Run selected tests
# ------------------------------------
if test_rsa or test_salmon:
print "Generating 4096 bit RSA key pair..."
try:
res = rsa_helper.generate_rsa(4096)
if res:
print " - Success"
success += 1
else:
print " - Fail"
fail +=1
except:
print " - Epic fail"
fail +=1
if test_rsa or test_salmon:
print "Encrypting message with public key using rsa_helper..."
try:
plain = "Testing encryption"
cipher = rsa_helper.encrypt(plain,res[0])
if cipher and cipher != plain:
print " - Success"
success += 1
else:
print " - Fail"
fail +=1
except:
print " - Epic fail"
fail +=1
if test_rsa or test_salmon:
print "Decrypting message with private key using rsa_helper..."
try:
new_plain = rsa_helper.decrypt(cipher,res[1])
if new_plain == plain:
print " - Success"
success += 1
else:
print " - Fail"
fail +=1
except:
print " - Epic fail"
fail +=1
if test_salmon:
print "Creating Salmon XML..."
try:
salmon_obj = salmon.Salmon("Robin Nilsson","robinnilsson@test.com",res[1],res[0],"Testing testing")
if salmon_obj:
print " - Success"
success += 1
else:
print " - Fail"
fail +=1
except:
print " - Epic fail"
fail +=1
if test_aes:
print "Generating 256 bit AES key..."
try:
res = aes_helper.get_random_key()
res2 = aes_helper.get_random_key()
if res and res != res2:
print " - Success"
success += 1
else:
print " - Fail"
fail +=1
except:
print " - Epic fail"
fail +=1
if test_aes:
print "Encrypting message using aes_helper..."
try:
plain = "Testing encryption"
cipher = aes_helper.encrypt(plain,res)
if cipher and cipher != plain:
print " - Success"
success += 1
else:
print " - Fail"
fail +=1
except:
print " - Epic fail"
fail +=1
if test_aes:
print "Decrypting message using aes_helper..."
try:
new_plain = aes_helper.decrypt(cipher,res)
if new_plain.strip() == plain.strip():
print " - Success"
success += 1
else:
print " - Fail"
fail +=1
except:
print " - Epic fail"
fail +=1
# MongoDB People object add
if test_new_people or test_get_people_by_id:
print "Creating a new people object..."
try:
tmp = people.People()
res = tmp.new(1337,"handle@test.com","pubkey","podurl")
_people_id = res
if res:
print " - Success"
success += 1
else:
print " - Fail ( " +tmp.debug + ")"
fail +=1
except:
print " - Epic fail"
fail +=1
# MongoDB User object add
if test_new_user or test_get_user_by_id or test_get_user_by_user:
print "Creating a new user object..."
try:
tmp = user.User()
res = tmp.new("testman","wordup","testman@testmanpod.com")
_id = tmp.object_id
_user = "testman"
if res:
print " - Success"
success += 1
else:
print " - Fail (" + tmp.debug + ")"
fail +=1
except:
print "Epic fail"
fail +=1
# MongoDB Aspect object add
if test_new_user or test_get_user_by_id or test_get_user_by_user:
print "Creating a new aspect..."
try:
tmp = aspects.Aspects()
res = tmp.new("Foo",_id) # _id is assigned in test_new_user
_aspect_id = tmp.object_id
tmp_aspect = aspects.Aspects()
tmp_aspect.get_by_id(_aspect_id)
if res and tmp_aspect.data[u'name'] == "Foo":
print " - Success"
success += 1
else:
print " - Fail (" + tmp.debug + ")"
fail +=1
except:
print "Epic fail"
fail +=1
# MongoDB Get user by id
if test_get_user_by_id:
print "Searching for user by id..."
try:
tmp = user.User().get_by_id(_id)
if tmp[u'email'] == "testman@testmanpod.com":
print " - Success"
success += 1
else:
print " - Fail"
fail +=1
except:
print "Epic fail"
fail +=1
# MongoDB Get user by username
if test_get_user_by_user:
print "Searching for user by username..."
try:
tmp = user.User().get_by_username(_user)
if tmp[u'email'] == "testman@testmanpod.com":
print " - Success"
success += 1
else:
print " - Fail"
fail +=1
except:
print "Epic fail"
fail +=1
# MongoDB Get people by id
if test_get_people_by_id:
print "Searching for people by id..."
#try:
tmp = people.People().get_by_id(_people_id)
if tmp[u'_id'] == _people_id:
print " - Success"
success += 1
else:
print " - Fail ( " + tmp.debug + ")"
fail +=1
#except:
# print "Epic fail"
# fail +=1
# DONE TESTING -- PRINT RESULTS!
print "----------------------------------------------------\nTesting done\n-----------------------------------------------------------"
if fail == 0:
print "All tests passed! (%i/%i)" % (success,success+fail)
else:
print "Ooooooops! (%i/%i) passed" % (success,success+fail)