~ecryptfs/ecryptfs/trunk

« back to all changes in this revision

Viewing changes to src/testcases/test-ecryptfs.py

  • Committer: mhalcrow@us.ibm.com
  • Date: 2007-11-06 22:56:01 UTC
  • Revision ID: git-v1:f8357de9d554b274497b5cce9db4347254b7e7eb
Initial import of eCryptfs filesystem userspace utilities (mount helper, daemon component,
etc.)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/env python
 
2
 
 
3
import os
 
4
import sys
 
5
import getopt
 
6
import re
 
7
 
 
8
distro_str_dict = {"rh": "Red Hat-based",
 
9
                   "gentoo": "Gentoo",
 
10
                   "deb": "Debian-based"}
 
11
 
 
12
def process_args(ctx, opts, args):
 
13
        for o, a in opts:
 
14
                if o in ("-d", "--distro="):
 
15
                        ctx.distro = a
 
16
                if o is "--dir":
 
17
                        ctx.mount_dir = a
 
18
                if o in ("-h", "--help"):
 
19
                        print "Usage:"
 
20
                        print
 
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"
 
24
                ctx.distro = 'rh'
 
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()
 
29
        
 
30
class Usage(Exception):
 
31
        def __init__(self, msg):
 
32
                self.msg = msg
 
33
 
 
34
chdir_after_tests = ["passphrase-mount-good"]
 
35
 
 
36
# base_test_descriptors are basic tests run under a single mount
 
37
# session
 
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
 
46
         None,                             # source_url
 
47
         None,                             # setup_command
 
48
         "mount -t ecryptfs -o key=passphrase:passwd=test,cipher=aes," \
 
49
         "ecryptfs_key_bytes=16,passthrough=n,no_sig_cache [MOUNTDIR] " \
 
50
         "[MOUNTDIR]",
 
51
         0,                                # expected_rc
 
52
         None,                             # post_command
 
53
         "umount [MOUNTDIR]; keyctl unlink " \
 
54
         "`keyctl search @u user d395309aaad4de06` @u"], # cleanup_command
 
55
        ["List directory contents",
 
56
         "listdir",
 
57
         None,
 
58
         None,
 
59
         None,
 
60
         None,
 
61
         None,                             # check_command
 
62
         None,
 
63
         None,
 
64
         "ls",
 
65
         0,
 
66
         None,
 
67
         None],
 
68
        ["Create new file",
 
69
         "createfile",
 
70
         None,
 
71
         None,
 
72
         None,
 
73
         None,
 
74
         None,                             # check_command
 
75
         None,
 
76
         None,
 
77
         "echo 'test' > test.txt",
 
78
         0,
 
79
         None,
 
80
         "rm -f test.txt"],
 
81
        ["Stat new file",
 
82
         "statfile",
 
83
         None,
 
84
         None,
 
85
         None,
 
86
         None,
 
87
         None,                             # check_command
 
88
         None,
 
89
         None,
 
90
         "stat test.txt | grep 'Size' | awk '{print $2;}'| egrep '^5$'",
 
91
         0,
 
92
         None,
 
93
         None],
 
94
        ["Read new file",
 
95
         "readfile",
 
96
         None,
 
97
         None,
 
98
         None,
 
99
         None,
 
100
         None,                             # check_command
 
101
         None,
 
102
         None,
 
103
         "egrep '^test$' test.txt",
 
104
         0,
 
105
         None,
 
106
         None],
 
107
        ["Connectathon basic",
 
108
         "connectathon-basic",
 
109
         None,
 
110
         None,
 
111
         None,
 
112
         None,
 
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 -",
 
118
         0,
 
119
         "cd [MOUNTDIR]",
 
120
         "rm -f /usr/local/bin/time-tmp.sh"],
 
121
        ["Connectathon general",
 
122
         "connectathon-general",
 
123
         None,
 
124
         None,
 
125
         None,
 
126
         None,
 
127
         None,                             # check_command
 
128
         None,
 
129
         "cd cthon04/general; cp ../../runtests.wrk .",
 
130
         "cd [ORIGPWD]/cthon04; ./runtests -g -f [MOUNTDIR]/1; cd -",
 
131
         0,
 
132
         "cd [MOUNTDIR]",
 
133
         None]
 
134
        ]
 
135
 
 
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
 
144
         None,                             # source_url
 
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] " \
 
148
         "[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] " \
 
153
         "[MOUNTDIR] &&" \
 
154
         "grep \"^test$\" [MOUNTDIR]/test1.txt " \
 
155
         "&& echo \"Success\" && rm -f [MOUNTDIR]/test1.txt && " \
 
156
         "umount [MOUNTDIR]",
 
157
         0,                                # expected_rc
 
158
         None,                             # post_command
 
159
         "keyctl unlink " \
 
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
 
168
         None,                             # source_url
 
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] " \
 
172
         "[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] " \
 
180
         "[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* && " \
 
185
         "umount [MOUNTDIR]",
 
186
         0,                                # expected_rc
 
187
         None,                             # post_command
 
188
         None]                             # cleanup_command
 
189
        ]
 
190
 
 
191
class test:
 
192
        alias = None
 
193
        name = None
 
194
        rh_base_package_name = None
 
195
        gentoo_base_package_name = None
 
196
        deb_base_package_name = None
 
197
        source_base_package_name = None
 
198
        source_url = None
 
199
        check_command = None
 
200
        setup_command = None
 
201
        exec_command = None
 
202
        expected_rc = 0
 
203
        post_command = None
 
204
        cleanup_command = None
 
205
        status = None
 
206
 
 
207
        def __init__(self, descriptor):
 
208
                (self.alias,
 
209
                 self.name,
 
210
                 self.rh_base_package_name,
 
211
                 self.gentoo_base_package_name,
 
212
                 self.deb_base_package_name,
 
213
                 self.source_base_package_name,
 
214
                 self.check_command,
 
215
                 self.source_url,
 
216
                 self.setup_command,
 
217
                 self.exec_command,
 
218
                 self.expected_rc,
 
219
                 self.post_command,
 
220
                 self.cleanup_command) = descriptor
 
221
 
 
222
        def __str__(self):
 
223
                str =   "Test\n"
 
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)
 
228
                return str
 
229
 
 
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)
 
239
                                if rc != 0:
 
240
                                        print "Trouble installing [%s] package" \
 
241
                                              % self.rh_base_package_name
 
242
                                        raise test_init_exception
 
243
                        else:
 
244
                                raise test_init_exception
 
245
 
 
246
        def init(self, ctx):
 
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:
 
259
                        try:
 
260
                                print "Checking command: [%s]" \
 
261
                                      % self.check_command
 
262
                                rc = os.system(self.check_command)
 
263
                                if rc != 0:
 
264
                                        print "Error executing [%s]; " \
 
265
                                              "attempting distro install" \
 
266
                                              % self.check_command
 
267
                                        try:
 
268
                                                self.distro_install(ctx)
 
269
                                        except:
 
270
                                                cmd = "wget %s" % self.source_url
 
271
                                                os.system(cmd)
 
272
                        except:
 
273
                                status = "failed"
 
274
                                print "[%s]: Test check failed" % self.name
 
275
                                raise test_init_exception
 
276
                if self.setup_command is None:
 
277
                        status = "setup"
 
278
                        return
 
279
                try:
 
280
                        rc = os.system(self.setup_command)
 
281
                except:
 
282
                        status = "failed"
 
283
                        print "[%s]: Test set up failed" % self.name
 
284
                        raise test_init_exception
 
285
                if rc == 0:
 
286
                        status = "setup"
 
287
                        print "[%s]: Test set up" % self.name
 
288
                else:
 
289
                        status = "failed"
 
290
                        print "[%s]: Test set up failed" % self.name
 
291
                        raise test_init_exception
 
292
 
 
293
        def run(self, ctx):
 
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:
 
298
                        status = "passed"
 
299
                        print "%s: Test passed" % self.name
 
300
                else:
 
301
                        status = "failed"
 
302
                        print "%s: Test failed; expected rc = [%d]; actual " \
 
303
                              "rc = [%d]" % (self.alias, self.expected_rc, rc)
 
304
                return rc
 
305
        def cleanup(self, ctx):
 
306
                if self.cleanup_command is None:
 
307
                        status="clean"
 
308
                        return
 
309
                rc = os.system(self.cleanup_command)
 
310
                if rc != 0:
 
311
                        status="failed"
 
312
                        print "%s: Test failed to clean up" % self.name
 
313
                        return
 
314
                status="clean"
 
315
 
 
316
class test_context:
 
317
        distro = None
 
318
        mount_dir = None
 
319
        orig_pwd = None
 
320
        tests = []
 
321
        def __init__(self):
 
322
                None
 
323
        def __str__(self):
 
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)
 
329
                return str
 
330
 
 
331
class test_init_exception(Exception):
 
332
        def __init__(self):
 
333
                None
 
334
        def __str__(self):
 
335
                return "Test init error"
 
336
 
 
337
def install_tests(ctx, test_descriptors):
 
338
        ctx.tests = []
 
339
        for td in test_descriptors:
 
340
                t = test(td)
 
341
                print "Registering test:"
 
342
                print t
 
343
                try:
 
344
                        t.init(ctx)
 
345
                except test_init_exception:
 
346
                        print "Error initializing test:"
 
347
                        print t
 
348
                else:
 
349
                        ctx.tests.append(t)
 
350
 
 
351
def run_tests(ctx):
 
352
        for t in ctx.tests:
 
353
                try:
 
354
                        t.run(ctx)
 
355
                except:
 
356
                        print "Error running test [%s]" % t.name
 
357
                        raise
 
358
                if t.name in chdir_after_tests:
 
359
                        os.chdir(ctx.mount_dir)
 
360
 
 
361
def run_test(ctx, name):
 
362
        for t in ctx.tests:
 
363
                if t.name == name:
 
364
                        try:
 
365
                                t.run(ctx)
 
366
                        except:
 
367
                                print "Error running test [%s]" % t.name
 
368
                                raise
 
369
                        if t.name in chdir_after_tests:
 
370
                                os.chdir(ctx.mount_dir)
 
371
 
 
372
def cleanup_tests(ctx):
 
373
        for t in reversed(ctx.tests):
 
374
                if t.name in chdir_after_tests:
 
375
                        os.chdir("/")
 
376
                t.cleanup(ctx)
 
377
 
 
378
def general_setup(ctx):
 
379
        uid = os.getuid()
 
380
        if uid != 0:
 
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."
 
386
 
 
387
def general_teardown(ctx):
 
388
        os.rmdir(ctx.mount_dir)
 
389
 
 
390
def main(argv=None):
 
391
        ctx = test_context()
 
392
        if argv is None:
 
393
                argv = sys.argv
 
394
        try:
 
395
                try:
 
396
                        opts, args = getopt.getopt(argv[1:],
 
397
                                                   "hd:",
 
398
                                                   ["help", "distro=", "dir="])
 
399
                except getopt.error, msg:
 
400
                        raise Usage(msg)
 
401
                process_args(ctx, opts, args)
 
402
        except Usage, err:
 
403
                print >> sys.stderr, err.msg
 
404
                print >> sys.stderr, "for help use --help"
 
405
                return 2
 
406
        print
 
407
        print ctx
 
408
        print "General setup."
 
409
        try:
 
410
                general_setup(ctx)
 
411
        except:
 
412
                print "Fatal exception whilst attempting to perform general " \
 
413
                      "setup"
 
414
                raise
 
415
        
 
416
        print "Registering base tests."
 
417
        try:
 
418
                install_tests(ctx, base_test_descriptors)
 
419
        except:
 
420
                print "Fatal exception whilst attempting to install testcases"
 
421
                raise
 
422
        print "Running tests."
 
423
        try:
 
424
                run_tests(ctx)
 
425
        except:
 
426
                print "Fatal exception whilst attempting to run testcases"
 
427
                raise
 
428
        print "Cleaning up."
 
429
        try:
 
430
                cleanup_tests(ctx)
 
431
        except:
 
432
                print "Fatal exception whilst attempting to clean up testcases"
 
433
                raise
 
434
 
 
435
        print "Registering mount-umount-mount tests."
 
436
        try:
 
437
                install_tests(ctx, mount_umount_test_descriptors)
 
438
        except:
 
439
                print "Fatal exception whilst attempting to install testcases"
 
440
                raise
 
441
        print "Running tests."
 
442
        try:
 
443
                run_tests(ctx)
 
444
        except:
 
445
                print "Fatal exception whilst attempting to run testcases"
 
446
                raise
 
447
        print "Cleaning up."
 
448
        try:
 
449
                cleanup_tests(ctx)
 
450
        except:
 
451
                print "Fatal exception whilst attempting to clean up testcases"
 
452
                raise
 
453
 
 
454
        print "General teardown."
 
455
        general_teardown(ctx)
 
456
        print "All tests passed."
 
457
 
 
458
if __name__ == "__main__":
 
459
        sys.exit(main())