Port pycryptsetup-test.py to Python3.

This commit is contained in:
Robert Kuska
2014-06-17 08:52:10 +02:00
committed by Milan Broz
parent 98ba2f2333
commit 7a2e6990ca

View File

@@ -2,7 +2,7 @@
# #
# Python bindings to libcryptsetup test # Python bindings to libcryptsetup test
# #
# Copyright (C) 2011, Red Hat, Inc. All rights reserved. # Copyright (C) 2011-2014, Red Hat, Inc. All rights reserved.
# #
# This file is free software; you can redistribute it and/or # This file is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public # modify it under the terms of the GNU Lesser General Public
@@ -18,6 +18,8 @@
# License along with this file; if not, write to the Free Software # License along with this file; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
from __future__ import print_function
import sys import sys
import os import os
@@ -31,11 +33,11 @@ DEVICE = "pycryptsetup_test_dev"
def log(level, txt): def log(level, txt):
if level == pycryptsetup.CRYPT_LOG_ERROR: if level == pycryptsetup.CRYPT_LOG_ERROR:
print txt, print(txt,end="")
return return
def askyes(txt): def askyes(txt):
print "Question:", txt print("Question:", txt)
return 1 return 1
def askpassword(txt): def askpassword(txt):
@@ -43,17 +45,17 @@ def askpassword(txt):
def print_status(c): def print_status(c):
r = c.status() r = c.status()
print "status :", print("status :",end="")
if r == pycryptsetup.CRYPT_ACTIVE: if r == pycryptsetup.CRYPT_ACTIVE:
print "ACTIVE" print("ACTIVE")
elif r == pycryptsetup.CRYPT_INACTIVE: elif r == pycryptsetup.CRYPT_INACTIVE:
print "INACTIVE" print("INACTIVE")
else: else:
print "ERROR" print("ERROR")
return return
if os.geteuid() != 0: if os.geteuid() != 0:
print "WARNING: You must be root to run this test, test skipped." print("WARNING: You must be root to run this test, test skipped.")
sys.exit(0) sys.exit(0)
os.system("dd if=/dev/zero of=" + IMG + " bs=1M count=32 >/dev/null 2>&1") os.system("dd if=/dev/zero of=" + IMG + " bs=1M count=32 >/dev/null 2>&1")
@@ -69,36 +71,36 @@ c = pycryptsetup.CryptSetup(
c.debugLevel(pycryptsetup.CRYPT_DEBUG_NONE); c.debugLevel(pycryptsetup.CRYPT_DEBUG_NONE);
c.iterationTime(1) c.iterationTime(1)
r = c.isLuks() r = c.isLuks()
print "isLuks :", r print("isLuks :", r)
c.askyes(message = "Is there anybody out there?") c.askyes(message = "Is there anybody out there?")
c.log(priority = pycryptsetup.CRYPT_LOG_ERROR, message = "Nobody there...\n") c.log(priority = pycryptsetup.CRYPT_LOG_ERROR, message = "Nobody there...\n")
c.luksFormat(cipher = "aes", cipherMode= "xts-plain64", keysize = 512) c.luksFormat(cipher = "aes", cipherMode= "xts-plain64", keysize = 512)
print "isLuks :", c.isLuks() print("isLuks :", c.isLuks())
print "luksUUID:", c.luksUUID() print("luksUUID:", c.luksUUID())
print "addKeyVK:", c.addKeyByVolumeKey(newPassphrase = PASSWORD, slot = 2) print("addKeyVK:", c.addKeyByVolumeKey(newPassphrase = PASSWORD, slot = 2))
print "addKeyP :", c.addKeyByPassphrase(passphrase = PASSWORD, print("addKeyP :", c.addKeyByPassphrase(passphrase = PASSWORD,
newPassphrase = PASSWORD2, slot = 3) newPassphrase = PASSWORD2, slot = 3))
print "removeP :", c.removePassphrase(passphrase = PASSWORD2) print("removeP :", c.removePassphrase(passphrase = PASSWORD2))
print "addKeyP :", c.addKeyByPassphrase(PASSWORD, PASSWORD2) print("addKeyP :", c.addKeyByPassphrase(PASSWORD, PASSWORD2))
# original api required wrong passphrase parameter here # original api required wrong passphrase parameter here
# print "killSlot:", c.killSlot(passphrase = "xxx", slot = 0) # print "killSlot:", c.killSlot(passphrase = "xxx", slot = 0)
print "killSlot:", c.killSlot(slot = 0) print("killSlot:", c.killSlot(slot = 0))
print "activate:", c.activate(name = DEVICE, passphrase = PASSWORD) print("activate:", c.activate(name = DEVICE, passphrase = PASSWORD))
print "suspend :", c.suspend() print("suspend :", c.suspend())
# os.system("dmsetup info -c " + DEVICE) # os.system("dmsetup info -c " + DEVICE)
print "resume :", c.resume(passphrase = PASSWORD) print("resume :", c.resume(passphrase = PASSWORD))
print_status(c) print_status(c)
info = c.info() info = c.info()
print "cipher :", info["cipher"] print("cipher :", info["cipher"])
print "cmode :", info["cipher_mode"] print("cmode :", info["cipher_mode"])
print "keysize :", info["keysize"] print("keysize :", info["keysize"])
print "dir :", info["dir"] print("dir :", info["dir"])
print "device :", info["device"] print("device :", info["device"])
print "offset :", info["offset"] print("offset :", info["offset"])
print "name :", info["name"] print("name :", info["name"])
print "uuid :", info["uuid"] print("uuid :", info["uuid"])
# os.system("cryptsetup luksDump " + info["device"]) # os.system("cryptsetup luksDump " + info["device"])
print "deact. :", c.deactivate() print("deact. :", c.deactivate())
del c del c
@@ -109,7 +111,7 @@ c = pycryptsetup.CryptSetup(
logFunc = log, logFunc = log,
passwordDialog = askpassword) passwordDialog = askpassword)
print "activate:", c.activate(name = DEVICE, passphrase = PASSWORD) print("activate:", c.activate(name = DEVICE, passphrase = PASSWORD))
c2 = pycryptsetup.CryptSetup( c2 = pycryptsetup.CryptSetup(
name = DEVICE, name = DEVICE,
@@ -118,13 +120,13 @@ c2 = pycryptsetup.CryptSetup(
passwordDialog = askpassword) passwordDialog = askpassword)
info = c2.info() info = c2.info()
print "cipher :", info["cipher"] print("cipher :", info["cipher"])
print "cmode :", info["cipher_mode"] print("cmode :", info["cipher_mode"])
print "keysize :", info["keysize"] print("keysize :", info["keysize"])
print "deact. :", c.deactivate() print("deact. :", c.deactivate())
r = c2.deactivate() r = c2.deactivate()
print "deact. :", r print("deact. :", r)
del c del c
del c2 del c2