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
|
#!/usr/bin/python3
import shutil
import subprocess
import time
from pathlib import Path
import pexpect
# Set up the environment
mntpath = Path("/tmp/mnt")
basepath = Path("/tmp/base")
testfilepath = mntpath / "foo"
testtext = "bar"
password="password"
def umount(path):
try:
subprocess.run(
"fusermount -u {}".format(str(path)).split(),
capture_output=True,
)
time.sleep(1)
except FileNotFoundError:
pass
def cleanup():
umount(mntpath)
for path in [mntpath, basepath]:
if path.exists():
shutil.rmtree(str(path))
path.mkdir()
print("Initial cleanup")
cleanup()
print("Create an encrypted volume")
session = pexpect.spawn(
"cryfs --allow-replaced-filesystem {0} {1}".format(
str(basepath), str(mntpath)
)
)
session.expect("Your choice")
session.sendline("y")
session.expect("Password:", timeout=600)
session.sendline(password)
session.expect("Confirm Password:", timeout=600)
session.sendline(password)
session.expect(pexpect.EOF, timeout=600)
print("Test the volume")
assert not testfilepath.exists()
testfilepath.write_text(testtext)
assert testfilepath.exists()
assert testfilepath.read_text() == testtext
print("Unmount and test")
umount(mntpath)
time.sleep(1)
assert not testfilepath.exists()
print("Remount and test")
session = pexpect.spawn("cryfs {0} {1}".format(str(basepath), str(mntpath)))
session.expect("Password:")
session.sendline(password)
session.expect(pexpect.EOF)
assert testfilepath.exists()
assert testfilepath.read_text() == testtext
print("Final cleanup")
cleanup()
mntpath.rmdir()
basepath.rmdir()
|