Remove some gcc extra warnings (signed/unsigned problems etc).
[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 if os.geteuid() != 0:
56         print "WARNING: You must be root to run this test, test skipped."
57         sys.exit(0)
58
59 os.system("dd if=/dev/zero of=" + IMG + " bs=1M count=32 >/dev/null 2>&1")
60
61 c = pycryptsetup.CryptSetup(
62         device = IMG,
63         name = DEVICE,
64         yesDialog = askyes,
65         logFunc = log,
66         passwordDialog = askpassword)
67
68 #c.debugLevel(pycryptsetup.CRYPT_DEBUG_ALL);
69 c.debugLevel(pycryptsetup.CRYPT_DEBUG_NONE);
70 c.iterationTime(1)
71 r =  c.isLuks()
72 print "isLuks  :", r
73 c.askyes(message = "Is there anybody out there?")
74 c.log(priority = pycryptsetup.CRYPT_LOG_ERROR, message = "Nobody there...\n")
75 c.luksFormat(cipher = "aes", cipherMode= "xts-plain64", keysize = 512)
76 print "isLuks  :", c.isLuks()
77 print "luksUUID:", c.luksUUID()
78 print "addKeyVK:", c.addKeyByVolumeKey(newPassphrase = PASSWORD, slot = 2)
79 print "addKeyP :", c.addKeyByPassphrase(passphrase = PASSWORD,
80                                         newPassphrase = PASSWORD2, slot = 3)
81 print "removeP :", c.removePassphrase(passphrase = PASSWORD2)
82 print "addKeyP :", c.addKeyByPassphrase(PASSWORD, PASSWORD2)
83 # original api required wrong passphrase parameter here
84 # print "killSlot:", c.killSlot(passphrase = "xxx", slot = 0)
85 print "killSlot:", c.killSlot(slot = 0)
86 print "activate:", c.activate(name = DEVICE, passphrase = PASSWORD)
87 print "suspend :", c.suspend()
88 # os.system("dmsetup info -c " + DEVICE)
89 print "resume  :", c.resume(passphrase = PASSWORD)
90 print_status(c)
91 info = c.info()
92 print "cipher  :", info["cipher"]
93 print "cmode   :", info["cipher_mode"]
94 print "keysize :", info["keysize"]
95 print "dir     :", info["dir"]
96 print "device  :", info["device"]
97 print "offset  :", info["offset"]
98 print "name    :", info["name"]
99 print "uuid    :", info["uuid"]
100 # os.system("cryptsetup luksDump " + info["device"])
101 print "deact.  :", c.deactivate()
102
103 del c
104
105 c = pycryptsetup.CryptSetup(
106         device = IMG,
107         name = DEVICE,
108         yesDialog = askyes,
109         logFunc = log,
110         passwordDialog = askpassword)
111
112 print "activate:", c.activate(name = DEVICE, passphrase = PASSWORD)
113
114 c2 = pycryptsetup.CryptSetup(
115         name = DEVICE,
116         yesDialog = askyes,
117         logFunc = log,
118         passwordDialog = askpassword)
119
120 info = c2.info()
121 print "cipher  :", info["cipher"]
122 print "cmode   :", info["cipher_mode"]
123 print "keysize :", info["keysize"]
124
125 print "deact.  :", c.deactivate()
126 r = c2.deactivate()
127 print "deact.  :", r
128 del c
129 del c2
130
131 os.remove(IMG)