8
distro_str_dict = {"rh": "Red Hat-based",
10
"deb": "Debian-based"}
12
def process_args(ctx, opts, args):
14
if o in ("-d", "--distro="):
18
if o in ("-h", "--help"):
21
print "test-ecryptfs.py [-d|--distro=][rh|gentoo|deb]"
22
if ctx.distro is None:
23
print "No distro given; defaulting to Red Hat-based"
25
if ctx.mount_dir is None:
26
print "No mount directory given; defaulting to /secret"
27
ctx.mount_dir = '/secret'
28
ctx.orig_pwd = os.getcwd()
30
class Usage(Exception):
31
def __init__(self, msg):
34
chdir_after_tests = ["passphrase-mount-good"]
36
# base_test_descriptors are basic tests run under a single mount
38
base_test_descriptors = [
39
["Perform basic passphrase mount", # alias
40
"passphrase-mount-good", # name
41
"keyutils", # rh_base_package_name
42
None, # gentoo_base_package_name
43
None, # deb_base_package_name
44
None, # source_base_package_name
45
"keyctl show", # check_command
48
"mount -t ecryptfs -o key=passphrase:passwd=test,cipher=aes," \
49
"ecryptfs_key_bytes=16,passthrough=n,no_sig_cache [MOUNTDIR] " \
53
"umount [MOUNTDIR]; keyctl unlink " \
54
"`keyctl search @u user d395309aaad4de06` @u"], # cleanup_command
55
["List directory contents",
77
"echo 'test' > test.txt",
90
"stat test.txt | grep 'Size' | awk '{print $2;}'| egrep '^5$'",
100
None, # check_command
103
"egrep '^test$' test.txt",
107
["Connectathon basic",
108
"connectathon-basic",
113
"ls cthon04", # check_command
114
"http://www.connectathon.org/nfstests.tar.gz",
115
"cp time-tmp.sh /usr/local/bin; tar xzf nfstests.tar.gz; cd cthon04; " \
116
"cp ../tests.init .; make",
117
"cd [ORIGPWD]/cthon04; ./runtests -b -f [MOUNTDIR]/1; cd -",
120
"rm -f /usr/local/bin/time-tmp.sh"],
121
["Connectathon general",
122
"connectathon-general",
127
None, # check_command
129
"cd cthon04/general; cp ../../runtests.wrk .",
130
"cd [ORIGPWD]/cthon04; ./runtests -g -f [MOUNTDIR]/1; cd -",
136
mount_umount_test_descriptors = [
137
["Mount, write, umount, mount, read", # alias
138
"mount-umount-mount-1", # name
139
None, # rh_base_package_name
140
None, # gentoo_base_package_name
141
None, # deb_base_package_name
142
None, # source_base_package_name
143
None, # check_command
145
None, # setup_command
146
"mount -t ecryptfs -o key=passphrase:passwd=test,cipher=aes," \
147
"ecryptfs_key_bytes=16,passthrough=n,no_sig_cache [MOUNTDIR] " \
149
"echo \"test\" > [MOUNTDIR]/test1.txt " \
150
"&& umount [MOUNTDIR] && " \
151
"mount -t ecryptfs -o key=passphrase:passwd=test,cipher=aes," \
152
"ecryptfs_key_bytes=16,passthrough=n,no_sig_cache [MOUNTDIR] " \
154
"grep \"^test$\" [MOUNTDIR]/test1.txt " \
155
"&& echo \"Success\" && rm -f [MOUNTDIR]/test1.txt && " \
160
"`keyctl search @u user d395309aaad4de06` @u"], # cleanup_command
161
["Mount, write, mkdir, write, umount, mount, read", # alias
162
"mount-umount-mount-2", # name
163
None, # rh_base_package_name
164
None, # gentoo_base_package_name
165
None, # deb_base_package_name
166
None, # source_base_package_name
167
None, # check_command
169
None, # setup_command
170
"mount -t ecryptfs -o key=passphrase:passwd=test,cipher=aes," \
171
"ecryptfs_key_bytes=16,passthrough=n,no_sig_cache [MOUNTDIR] " \
173
"echo \"test\" > [MOUNTDIR]/test1.txt " \
174
"&& mkdir [MOUNTDIR]/test2 " \
175
"&& echo \"test\" > [MOUNTDIR]/test2/test3.txt " \
176
"&& echo \"test\" > [MOUNTDIR]/test4.txt " \
177
"&& umount [MOUNTDIR] && " \
178
"mount -t ecryptfs -o key=passphrase:passwd=test,cipher=aes," \
179
"ecryptfs_key_bytes=16,passthrough=n,no_sig_cache [MOUNTDIR] " \
181
"grep \"^test$\" [MOUNTDIR]/test1.txt " \
182
"&& grep \"^test$\" [MOUNTDIR]/test2/test3.txt " \
183
"&& grep \"^test$\" [MOUNTDIR]/test4.txt " \
184
"&& echo \"Success\" && rm -rf [MOUNTDIR]/test* && " \
188
None] # cleanup_command
194
rh_base_package_name = None
195
gentoo_base_package_name = None
196
deb_base_package_name = None
197
source_base_package_name = None
204
cleanup_command = None
207
def __init__(self, descriptor):
210
self.rh_base_package_name,
211
self.gentoo_base_package_name,
212
self.deb_base_package_name,
213
self.source_base_package_name,
220
self.cleanup_command) = descriptor
224
str = "%s----\n" % str
225
str = "%s * alias: [%s]\n" % (str, self.alias)
226
str = "%s * name: [%s]\n" % (str, self.name)
227
# str = "%s * status: [%s]\n" % (str, self.status)
230
def distro_install(self, ctx):
231
print "ctx.distro = [%s]; self.rh_base_package_name = [%s]" \
232
% (ctx.distro, self.rh_base_package_name)
233
if ctx.distro == "rh":
234
if self.rh_base_package_name != None:
235
print "Attempting to install distro package " \
236
"[%s]" % self.rh_base_package_name
237
rc = os.system("yum install -y %s" % \
238
self.rh_base_package_name)
240
print "Trouble installing [%s] package" \
241
% self.rh_base_package_name
242
raise test_init_exception
244
raise test_init_exception
247
if self.exec_command != None:
248
self.exec_command = self.exec_command.replace( \
249
'[MOUNTDIR]', ctx.mount_dir)
250
self.exec_command = self.exec_command.replace( \
251
'[ORIGPWD]', ctx.orig_pwd)
252
if self.post_command != None:
253
self.post_command = self.post_command.replace( \
254
'[MOUNTDIR]', ctx.mount_dir)
255
if self.cleanup_command != None:
256
self.cleanup_command = self.cleanup_command.replace( \
257
'[MOUNTDIR]', ctx.mount_dir)
258
if self.check_command != None:
260
print "Checking command: [%s]" \
262
rc = os.system(self.check_command)
264
print "Error executing [%s]; " \
265
"attempting distro install" \
268
self.distro_install(ctx)
270
cmd = "wget %s" % self.source_url
274
print "[%s]: Test check failed" % self.name
275
raise test_init_exception
276
if self.setup_command is None:
280
rc = os.system(self.setup_command)
283
print "[%s]: Test set up failed" % self.name
284
raise test_init_exception
287
print "[%s]: Test set up" % self.name
290
print "[%s]: Test set up failed" % self.name
291
raise test_init_exception
294
if self.exec_command is None:
295
raise "No exec_command"
296
rc = os.system(self.exec_command)
297
if rc == self.expected_rc:
299
print "%s: Test passed" % self.name
302
print "%s: Test failed; expected rc = [%d]; actual " \
303
"rc = [%d]" % (self.alias, self.expected_rc, rc)
305
def cleanup(self, ctx):
306
if self.cleanup_command is None:
309
rc = os.system(self.cleanup_command)
312
print "%s: Test failed to clean up" % self.name
324
str = "Test Context\n"
325
str = "%s------------\n" % str
326
str = "%s * distro: [%s]\n" % (str, self.distro)
327
str = "%s * mount_dir: [%s]\n" % (str, self.mount_dir)
328
str = "%s * orig_pwd: [%s]\n" % (str, self.orig_pwd)
331
class test_init_exception(Exception):
335
return "Test init error"
337
def install_tests(ctx, test_descriptors):
339
for td in test_descriptors:
341
print "Registering test:"
345
except test_init_exception:
346
print "Error initializing test:"
356
print "Error running test [%s]" % t.name
358
if t.name in chdir_after_tests:
359
os.chdir(ctx.mount_dir)
361
def run_test(ctx, name):
367
print "Error running test [%s]" % t.name
369
if t.name in chdir_after_tests:
370
os.chdir(ctx.mount_dir)
372
def cleanup_tests(ctx):
373
for t in reversed(ctx.tests):
374
if t.name in chdir_after_tests:
378
def general_setup(ctx):
381
raise "This test script must be run under uid 0 (root)"
382
os.mkdir(ctx.mount_dir)
383
print "This program assumes that you have inserted all the " \
384
"requisite kernel modules or that the environment is properly " \
385
"set up to auto-load the modules as needed."
387
def general_teardown(ctx):
388
os.rmdir(ctx.mount_dir)
396
opts, args = getopt.getopt(argv[1:],
398
["help", "distro=", "dir="])
399
except getopt.error, msg:
401
process_args(ctx, opts, args)
403
print >> sys.stderr, err.msg
404
print >> sys.stderr, "for help use --help"
408
print "General setup."
412
print "Fatal exception whilst attempting to perform general " \
416
print "Registering base tests."
418
install_tests(ctx, base_test_descriptors)
420
print "Fatal exception whilst attempting to install testcases"
422
print "Running tests."
426
print "Fatal exception whilst attempting to run testcases"
432
print "Fatal exception whilst attempting to clean up testcases"
435
print "Registering mount-umount-mount tests."
437
install_tests(ctx, mount_umount_test_descriptors)
439
print "Fatal exception whilst attempting to install testcases"
441
print "Running tests."
445
print "Fatal exception whilst attempting to run testcases"
451
print "Fatal exception whilst attempting to clean up testcases"
454
print "General teardown."
455
general_teardown(ctx)
456
print "All tests passed."
458
if __name__ == "__main__":