pycryptsetup-test.py 3.71 KB
Newer Older
1
#!/usr/bin/python
Milan Broz's avatar
Milan Broz committed
2 3 4
#
# Python bindings to libcryptsetup test
#
Milan Broz's avatar
Milan Broz committed
5
# Copyright (C) 2011-2017, Red Hat, Inc. All rights reserved.
Milan Broz's avatar
Milan Broz committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19
#
# This file is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This file is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this file; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20

21 22
from __future__ import print_function

23 24 25 26 27 28 29 30
import sys
import os

sys.path.insert(0, ".libs")
import pycryptsetup

IMG = "test.img"
PASSWORD = "password"
31
PASSWORD2 = "password2"
32 33
DEVICE = "pycryptsetup_test_dev"

34 35
def log(level, txt):
    if level == pycryptsetup.CRYPT_LOG_ERROR:
36
        print(txt,end="")
37 38 39
    return

def askyes(txt):
40
    print("Question:", txt)
41 42
    return 1

43 44
def print_status(c):
    r = c.status()
45
    print("status  :",end="")
46
    if r == pycryptsetup.CRYPT_ACTIVE:
47
        print("ACTIVE")
48
    elif r == pycryptsetup.CRYPT_INACTIVE:
49
        print("INACTIVE")
50
    else:
51
       print("ERROR")
52 53
    return

54
if os.geteuid() != 0:
55
	print("WARNING: You must be root to run this test, test skipped.")
56
	sys.exit(77)
57

58 59 60 61 62 63
os.system("dd if=/dev/zero of=" + IMG + " bs=1M count=32 >/dev/null 2>&1")

c = pycryptsetup.CryptSetup(
        device = IMG,
        name = DEVICE,
        yesDialog = askyes,
64
        logFunc = log)
65

66 67
#c.debugLevel(pycryptsetup.CRYPT_DEBUG_ALL);
c.debugLevel(pycryptsetup.CRYPT_DEBUG_NONE);
68
c.iterationTime(1)
69
r =  c.isLuks()
70
print("isLuks  :", r)
71
c.askyes(message = "Is there anybody out there?")
72
c.log(priority = pycryptsetup.CRYPT_LOG_ERROR, message = "Nobody there...\n")
73
c.luksFormat(cipher = "aes", cipherMode= "xts-plain64", keysize = 512, hashMode = "sha256")
74 75 76 77 78 79 80
print("isLuks  :", c.isLuks())
print("luksUUID:", c.luksUUID())
print("addKeyVK:", c.addKeyByVolumeKey(newPassphrase = PASSWORD, slot = 2))
print("addKeyP :", c.addKeyByPassphrase(passphrase = PASSWORD,
					newPassphrase = PASSWORD2, slot = 3))
print("removeP :", c.removePassphrase(passphrase = PASSWORD2))
print("addKeyP :", c.addKeyByPassphrase(PASSWORD, PASSWORD2))
Milan Broz's avatar
Milan Broz committed
81
# original api required wrong passphrase parameter here
82
# print "killSlot:", c.killSlot(passphrase = "xxx", slot = 0)
83 84 85
print("killSlot:", c.killSlot(slot = 0))
print("activate:", c.activate(name = DEVICE, passphrase = PASSWORD))
print("suspend :", c.suspend())
86
# os.system("dmsetup info -c " + DEVICE)
87
print("resume  :", c.resume(passphrase = PASSWORD))
88
print_status(c)
89
info = c.info()
90 91 92 93 94 95 96 97
print("cipher  :", info["cipher"])
print("cmode   :", info["cipher_mode"])
print("keysize :", info["keysize"])
print("dir     :", info["dir"])
print("device  :", info["device"])
print("offset  :", info["offset"])
print("name    :", info["name"])
print("uuid    :", info["uuid"])
98
# os.system("cryptsetup luksDump " + info["device"])
99
print("deact.  :", c.deactivate())
100 101 102

del c

103 104 105 106
c = pycryptsetup.CryptSetup(
        device = IMG,
        name = DEVICE,
        yesDialog = askyes,
107
        logFunc = log)
108

109
print("activate:", c.activate(name = DEVICE, passphrase = PASSWORD))
110 111 112 113

c2 = pycryptsetup.CryptSetup(
        name = DEVICE,
        yesDialog = askyes,
114
        logFunc = log)
115 116

info = c2.info()
117 118 119
print("cipher  :", info["cipher"])
print("cmode   :", info["cipher_mode"])
print("keysize :", info["keysize"])
120

121
print("deact.  :", c.deactivate())
122
r = c2.deactivate()
123
print("deact.  :", r)
124 125 126
del c
del c2

127
os.remove(IMG)