Add some constants to Python binding.
[platform/upstream/cryptsetup.git] / python / pycryptsetup-test.py
1 #!/usr/bin/python
2 #
3 # Python bindings to libcryptsetup test
4 #
5 # Copyright (C) 2011, Red Hat, Inc. All rights reserved.
6 #
7 # This file is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
11 #
12 # This file is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this file; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 import sys
22 import os
23
24 sys.path.insert(0, ".libs")
25 import pycryptsetup
26
27 IMG = "test.img"
28 PASSWORD = "password"
29 PASSWORD2 = "password2"
30 DEVICE = "pycryptsetup_test_dev"
31
32 def log(level, txt):
33     if level == pycryptsetup.CRYPT_LOG_ERROR:
34         print txt,
35     return
36
37 def askyes(txt):
38     print "Question:", txt
39     return 1
40
41 def askpassword(txt):
42     return PASSWORD
43
44 def print_status(c):
45     r = c.status()
46     print "status  :",
47     if r == pycryptsetup.CRYPT_ACTIVE:
48         print "ACTIVE"
49     elif r == pycryptsetup.CRYPT_INACTIVE:
50         print "INACTIVE"
51     else:
52        print "ERROR"
53     return
54
55 os.system("dd if=/dev/zero of=" + IMG + " bs=1M count=32 >/dev/null 2>&1")
56
57 c = pycryptsetup.CryptSetup(
58         device = IMG,
59         name = DEVICE,
60         yesDialog = askyes,
61         logFunc = log,
62         passwordDialog = askpassword)
63
64 #c.debugLevel(pycryptsetup.CRYPT_DEBUG_ALL);
65 c.debugLevel(pycryptsetup.CRYPT_DEBUG_NONE);
66 c.iterationTime(1)
67 r =  c.isLuks()
68 print "isLuks  :", r
69 c.askyes(message = "Is there anybody out there?")
70 c.log(priority = pycryptsetup.CRYPT_LOG_ERROR, message = "Nobody there...\n")
71 c.luksFormat(cipher = "aes", cipherMode= "xts-plain64", keysize = 512)
72 print "isLuks  :", c.isLuks()
73 print "luksUUID:", c.luksUUID()
74 print "addKeyVK:", c.addKeyByVolumeKey(newPassphrase = PASSWORD, slot = 2)
75 print "addKeyP :", c.addKeyByPassphrase(passphrase = PASSWORD,
76                                         newPassphrase = PASSWORD2, slot = 3)
77 print "removeP :", c.removePassphrase(passphrase = PASSWORD2)
78 print "addKeyP :", c.addKeyByPassphrase(PASSWORD, PASSWORD2)
79 # original api required wrong passphrase parameter here
80 # print "killSlot:", c.killSlot(passphrase = "xxx", slot = 0)
81 print "killSlot:", c.killSlot(slot = 0)
82 print "activate:", c.activate(name = DEVICE, passphrase = PASSWORD)
83 print "suspend :", c.suspend()
84 # os.system("dmsetup info -c " + DEVICE)
85 print "resume  :", c.resume(passphrase = PASSWORD)
86 print_status(c)
87 info = c.info()
88 print "cipher  :", info["cipher"]
89 print "cmode   :", info["cipher_mode"]
90 print "keysize :", info["keysize"]
91 print "dir     :", info["dir"]
92 print "device  :", info["device"]
93 print "offset  :", info["offset"]
94 print "name    :", info["name"]
95 print "uuid    :", info["uuid"]
96 # os.system("cryptsetup luksDump " + info["device"])
97 print "deact.  :", c.deactivate()
98
99 del c
100
101 c = pycryptsetup.CryptSetup(
102         device = IMG,
103         name = DEVICE,
104         yesDialog = askyes,
105         logFunc = log,
106         passwordDialog = askpassword)
107
108 print "activate:", c.activate(name = DEVICE, passphrase = PASSWORD)
109
110 c2 = pycryptsetup.CryptSetup(
111         name = DEVICE,
112         yesDialog = askyes,
113         logFunc = log,
114         passwordDialog = askpassword)
115
116 info = c2.info()
117 print "cipher  :", info["cipher"]
118 print "cmode   :", info["cipher_mode"]
119 print "keysize :", info["keysize"]
120
121 print "deact.  :", c.deactivate()
122 r = c2.deactivate()
123 print "deact.  :", r
124 del c
125 del c2
126
127 os.remove(IMG)