Include 'sys/sysmacros.h' for GCC-9
[platform/upstream/cryptsetup.git] / python / pycryptsetup-test.py
1 #!/usr/bin/python
2 #
3 # Python bindings to libcryptsetup test
4 #
5 # Copyright (C) 2011-2014, 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 from __future__ import print_function
22
23 import sys
24 import os
25
26 sys.path.insert(0, ".libs")
27 import pycryptsetup
28
29 IMG = "test.img"
30 PASSWORD = "password"
31 PASSWORD2 = "password2"
32 DEVICE = "pycryptsetup_test_dev"
33
34 def log(level, txt):
35     if level == pycryptsetup.CRYPT_LOG_ERROR:
36         print(txt,end="")
37     return
38
39 def askyes(txt):
40     print("Question:", txt)
41     return 1
42
43 def askpassword(txt):
44     return PASSWORD
45
46 def print_status(c):
47     r = c.status()
48     print("status  :",end="")
49     if r == pycryptsetup.CRYPT_ACTIVE:
50         print("ACTIVE")
51     elif r == pycryptsetup.CRYPT_INACTIVE:
52         print("INACTIVE")
53     else:
54        print("ERROR")
55     return
56
57 if os.geteuid() != 0:
58         print("WARNING: You must be root to run this test, test skipped.")
59         sys.exit(0)
60
61 os.system("dd if=/dev/zero of=" + IMG + " bs=1M count=32 >/dev/null 2>&1")
62
63 c = pycryptsetup.CryptSetup(
64         device = IMG,
65         name = DEVICE,
66         yesDialog = askyes,
67         logFunc = log,
68         passwordDialog = askpassword)
69
70 #c.debugLevel(pycryptsetup.CRYPT_DEBUG_ALL);
71 c.debugLevel(pycryptsetup.CRYPT_DEBUG_NONE);
72 c.iterationTime(1)
73 r =  c.isLuks()
74 print("isLuks  :", r)
75 c.askyes(message = "Is there anybody out there?")
76 c.log(priority = pycryptsetup.CRYPT_LOG_ERROR, message = "Nobody there...\n")
77 c.luksFormat(cipher = "aes", cipherMode= "xts-plain64", keysize = 512)
78 print("isLuks  :", c.isLuks())
79 print("luksUUID:", c.luksUUID())
80 print("addKeyVK:", c.addKeyByVolumeKey(newPassphrase = PASSWORD, slot = 2))
81 print("addKeyP :", c.addKeyByPassphrase(passphrase = PASSWORD,
82                                         newPassphrase = PASSWORD2, slot = 3))
83 print("removeP :", c.removePassphrase(passphrase = PASSWORD2))
84 print("addKeyP :", c.addKeyByPassphrase(PASSWORD, PASSWORD2))
85 # original api required wrong passphrase parameter here
86 # print "killSlot:", c.killSlot(passphrase = "xxx", slot = 0)
87 print("killSlot:", c.killSlot(slot = 0))
88 print("activate:", c.activate(name = DEVICE, passphrase = PASSWORD))
89 print("suspend :", c.suspend())
90 # os.system("dmsetup info -c " + DEVICE)
91 print("resume  :", c.resume(passphrase = PASSWORD))
92 print_status(c)
93 info = c.info()
94 print("cipher  :", info["cipher"])
95 print("cmode   :", info["cipher_mode"])
96 print("keysize :", info["keysize"])
97 print("dir     :", info["dir"])
98 print("device  :", info["device"])
99 print("offset  :", info["offset"])
100 print("name    :", info["name"])
101 print("uuid    :", info["uuid"])
102 # os.system("cryptsetup luksDump " + info["device"])
103 print("deact.  :", c.deactivate())
104
105 del c
106
107 c = pycryptsetup.CryptSetup(
108         device = IMG,
109         name = DEVICE,
110         yesDialog = askyes,
111         logFunc = log,
112         passwordDialog = askpassword)
113
114 print("activate:", c.activate(name = DEVICE, passphrase = PASSWORD))
115
116 c2 = pycryptsetup.CryptSetup(
117         name = DEVICE,
118         yesDialog = askyes,
119         logFunc = log,
120         passwordDialog = askpassword)
121
122 info = c2.info()
123 print("cipher  :", info["cipher"])
124 print("cmode   :", info["cipher_mode"])
125 print("keysize :", info["keysize"])
126
127 print("deact.  :", c.deactivate())
128 r = c2.deactivate()
129 print("deact.  :", r)
130 del c
131 del c2
132
133 os.remove(IMG)