From b30c1c7f431f088dcb72527ac5f85f708f1e247f Mon Sep 17 00:00:00 2001 From: Milan Broz Date: Fri, 28 Aug 2009 15:22:37 +0000 Subject: [PATCH] Add separate tests directory, move LUKS tests, Add old library API tests. git-svn-id: https://cryptsetup.googlecode.com/svn/trunk@87 36d66b0a-2a48-0410-832c-cd162a569da5 --- Makefile.am | 1 + configure.in | 1 + luks/Makefile.am | 155 ------- tests/Makefile.am | 170 +++++++ tests/apitest.c | 488 +++++++++++++++++++++ .../compatimage.bz2 => tests/compatimage.img.bz2 | Bin {luks/testing => tests}/fileDiffer.py | 0 7 files changed, 660 insertions(+), 155 deletions(-) create mode 100644 tests/Makefile.am create mode 100644 tests/apitest.c rename luks/testing/compatimage.bz2 => tests/compatimage.img.bz2 (100%) rename {luks/testing => tests}/fileDiffer.py (100%) diff --git a/Makefile.am b/Makefile.am index b6c761e..270855b 100644 --- a/Makefile.am +++ b/Makefile.am @@ -3,6 +3,7 @@ SUBDIRS = \ lib \ src \ man \ + tests \ po ACLOCAL_AMFLAGS = -I m4 diff --git a/configure.in b/configure.in index c208ebc..4a2815e 100644 --- a/configure.in +++ b/configure.in @@ -132,4 +132,5 @@ src/Makefile po/Makefile.in luks/Makefile man/Makefile +tests/Makefile ]) diff --git a/luks/Makefile.am b/luks/Makefile.am index a2fcc05..19dce32 100644 --- a/luks/Makefile.am +++ b/luks/Makefile.am @@ -21,158 +21,3 @@ INCLUDES = -D_GNU_SOURCE \ -D_FILE_OFFSET_BITS=64 \ -I$(top_srcdir)/lib -EXTRA_DIST = testing/fileDiffer.py testing/compatimage.bz2 - -ORIG_IMG = /tmp/luks-test-orig -IMG = /tmp/luks-test -IMG1 = /tmp/luks-test1 - - -LUKS_HEADER = S0-5 S6-7 S8-39 S40-71 S72-103 S104-107 S108-111 R112-131 R132-163 S164-167 S168-207 A0-591 - -KEY_SLOT0 = S208-211 S212-215 R216-247 S248-251 S251-255 -KEY_MATERIAL0 = R4096-68096 -KEY_MATERIAL0_EXT = R4096-68096 - -KEY_SLOT1 = S256-259 S260-263 R264-295 S296-299 S300-303 -KEY_MATERIAL1 = R69632-133632 -KEY_MATERIAL1_EXT = S69632-133632 - -LOOPDEV = /dev/loop/5 - -test: - (cd ..; make clean; make CFLAGS=-Werror) - @if [ `id -u` != 0 ]; then \ - echo Not root; \ - fi - @if [ ! -e /tmp/key1 ]; then \ - dd if=/dev/urandom of=/tmp/key1 count=1 bs=32; \ - fi - @bzip2 -cd testing/compatimage.bz2 > $(IMG) - @-/sbin/losetup -d $(LOOPDEV) - @/sbin/losetup $(LOOPDEV) $(IMG) - - @echo Case: open - compat image - acceptance check -# Image must not change - @cp $(IMG) $(ORIG_IMG) - echo "compatkey" | ../src/cryptsetup -v luksOpen $(LOOPDEV) dummy - @sync - testing/fileDiffer.py $(IMG) $(ORIG_IMG) - @-ls -l /dev/mapper/dummy > /dev/null - @../src/cryptsetup remove dummy - @echo "success" - - - @echo Case: open - compat image - denial check -# Image must not change - @cp $(IMG) $(ORIG_IMG) - echo "wrongkey" | ../src/cryptsetup -v luksOpen $(LOOPDEV) dummy || true - @sync - testing/fileDiffer.py $(IMG) $(ORIG_IMG) - @echo "success" - - @echo Case: format -# All headers items and first key material section must change - @cp $(IMG) $(ORIG_IMG) - echo "key0" | ../src/cryptsetup -v -i 1000 -c aes-cbc-essiv:sha256 luksFormat $(LOOPDEV) - @sync - testing/fileDiffer.py $(IMG) $(ORIG_IMG) $(LUKS_HEADER) $(KEY_SLOT0) $(KEY_MATERIAL0) - - @echo Case: format using hash sha512 - @cp $(IMG) $(ORIG_IMG) - echo "key0" | ../src/cryptsetup -v -i 1000 -h sha512 -c aes-cbc-essiv:sha256 luksFormat $(LOOPDEV) - @sync - testing/fileDiffer.py $(IMG) $(ORIG_IMG) $(LUKS_HEADER) $(KEY_SLOT0) $(KEY_MATERIAL0) - - @echo Case: open -# Image must not change - @cp $(IMG) $(ORIG_IMG) - echo "key0" | ../src/cryptsetup -v luksOpen $(LOOPDEV) dummy - @sync - testing/fileDiffer.py $(IMG) $(ORIG_IMG) - @-ls -l /dev/mapper/dummy > /dev/null - @../src/cryptsetup remove dummy - @echo "success" - - @echo Case: add key -# Key Slot 1 and key material section 1 must change, the rest must not. - @cp $(IMG) $(ORIG_IMG) - echo -e "key0\nkey1" | ../src/cryptsetup -v luksAddKey $(LOOPDEV) - @sync - testing/fileDiffer.py $(IMG) $(ORIG_IMG) $(KEY_SLOT1) $(KEY_MATERIAL1) - echo "key1" | ../src/cryptsetup -v luksOpen $(LOOPDEV) dummy - @-ls -l /dev/mapper/dummy > /dev/null - @../src/cryptsetup -v remove dummy - @echo "success" - -# Unsuccessful Key Delete - nothing may change - @echo Case: unsuccessful delete - @cp $(IMG) $(ORIG_IMG) - echo "invalid" | ../src/cryptsetup -v luksDelKey $(LOOPDEV) 1 || true - @sync - testing/fileDiffer.py $(IMG) $(ORIG_IMG) - @echo "success" - -# Delete Key Test -# Key Slot 1 and key material section 1 must change, the rest must not - @echo Case: successful delete - @cp $(IMG) $(ORIG_IMG) - ../src/cryptsetup -v -q luksDelKey $(LOOPDEV) 1 - @sync - testing/fileDiffer.py $(IMG) $(ORIG_IMG) $(KEY_SLOT1) $(KEY_MATERIAL1_EXT) - echo "key1" | ../src/cryptsetup -v luksOpen $(LOOPDEV) dummy 2>/dev/null || true - echo "key0" | ../src/cryptsetup -v luksOpen $(LOOPDEV) dummy 2>/dev/null - @../src/cryptsetup -v remove dummy - @echo "success" - -# Non-Exclusive Open test - @echo Case: Non-Exclusive Open - echo "key0" | ../src/cryptsetup -v --readonly luksOpen $(LOOPDEV) dummy1 2>/dev/null -# must fail - echo "key0" | ../src/cryptsetup -v --readonly luksOpen $(LOOPDEV) dummy2 2>/dev/null || true - echo "key0" | ../src/cryptsetup -v --non-exclusive --readonly luksOpen $(LOOPDEV) dummy2 2>/dev/null - @../src/cryptsetup -v remove dummy1 - @../src/cryptsetup -v remove dummy2 - - -# Key Slot 1 and key material section 1 must change, the rest must not - @echo Case: add key test for key files - @cp $(IMG) $(ORIG_IMG) - echo "key0" | ../src/cryptsetup -v luksAddKey $(LOOPDEV) /tmp/key1 - @sync - testing/fileDiffer.py $(IMG) $(ORIG_IMG) $(KEY_SLOT1) $(KEY_MATERIAL1) - ../src/cryptsetup -d /tmp/key1 -v luksOpen $(LOOPDEV) dummy - @-ls -l /dev/mapper/dummy > /dev/null - @../src/cryptsetup -v remove dummy - @echo "success" - - @echo Case: delete key test with /tmp/key1 as remaining key -# Key Slot 1 and key material section 1 must change, the rest must not - @cp $(IMG) $(ORIG_IMG) - ../src/cryptsetup -v -d /tmp/key1 luksDelKey $(LOOPDEV) 0 - @sync - testing/fileDiffer.py $(IMG) $(ORIG_IMG) $(KEY_SLOT0) $(KEY_MATERIAL0_EXT) - echo "key0" | ../src/cryptsetup -v luksOpen $(LOOPDEV) dummy 2>/dev/null || true - ../src/cryptsetup -v luksOpen -d /tmp/key1 $(LOOPDEV) dummy 2>/dev/null - @../src/cryptsetup -v remove dummy - @echo "success" - -# Delete last slot - @echo Case: delete last key - @cp $(IMG) $(ORIG_IMG) - @echo "key0" | ../src/cryptsetup -v luksFormat $(LOOPDEV) - echo "key0" | ../src/cryptsetup -v luksKillSlot $(LOOPDEV) 0 - @sync - echo "key0" | ../src/cryptsetup -v luksOpen $(LOOPDEV) dummy 2>/dev/null || true - @echo "success" - -# Format test for ESSIV, and some other parameters. - @echo Case: parameter variation test - @dd if=/dev/zero of=$(IMG) count=20000 - @cp $(IMG) $(ORIG_IMG) - @../src/cryptsetup -q -v -i 1000 -c aes-cbc-essiv:sha256 luksFormat $(LOOPDEV) /tmp/key1 - @sync - testing/fileDiffer.py $(IMG) $(ORIG_IMG) $(LUKS_HEADER) $(KEY_SLOT0) $(KEY_MATERIAL0) - @../src/cryptsetup -d /tmp/key1 -v luksOpen $(LOOPDEV) dummy - @-ls -l /dev/mapper/dummy > /dev/null && echo "success" - @../src/cryptsetup -v remove dummy diff --git a/tests/Makefile.am b/tests/Makefile.am new file mode 100644 index 0000000..de4671a --- /dev/null +++ b/tests/Makefile.am @@ -0,0 +1,170 @@ +TESTS = apitest + +EXTRA_DIST = apitest fileDiffer.py compatimage.img.bz2 test + +apitest_SOURCES = apitest.c +apitest_LDADD = ../lib/libcryptsetup.la +apitest_LDFLAGS = -static +apitest_CFLAGS = -g -O0 + +check_PROGRAMS = apitest compatimage.img test + +compatimage.img: + @bzip2 -k -d compatimage.img.bz2 + +# LUKS tests +ORIG_IMG = /tmp/luks-test-orig +IMG = /tmp/luks-test +IMG1 = /tmp/luks-test1 + + +LUKS_HEADER = S0-5 S6-7 S8-39 S40-71 S72-103 S104-107 S108-111 R112-131 R132-163 S164-167 S168-207 A0-591 + +KEY_SLOT0 = S208-211 S212-215 R216-247 S248-251 S251-255 +KEY_MATERIAL0 = R4096-68096 +KEY_MATERIAL0_EXT = R4096-68096 + +KEY_SLOT1 = S256-259 S260-263 R264-295 S296-299 S300-303 +KEY_MATERIAL1 = R69632-133632 +KEY_MATERIAL1_EXT = S69632-133632 + +LOOPDEV = /dev/loop/5 + +test: +# (cd ..; make clean; make CFLAGS=-Werror) + @if [ `id -u` != 0 ]; then \ + echo Not root; \ + fi + @if [ ! -e /tmp/key1 ]; then \ + dd if=/dev/urandom of=/tmp/key1 count=1 bs=32; \ + fi + @bzip2 -cd compatimage.img.bz2 > $(IMG) + @-/sbin/losetup -d $(LOOPDEV) + @/sbin/losetup $(LOOPDEV) $(IMG) + + @echo Case: open - compat image - acceptance check +# Image must not change + @cp $(IMG) $(ORIG_IMG) + echo "compatkey" | ../src/cryptsetup -v luksOpen $(LOOPDEV) dummy + @sync + ./fileDiffer.py $(IMG) $(ORIG_IMG) + @-ls -l /dev/mapper/dummy > /dev/null + @../src/cryptsetup remove dummy + @echo "success" + + + @echo Case: open - compat image - denial check +# Image must not change + @cp $(IMG) $(ORIG_IMG) + echo "wrongkey" | ../src/cryptsetup -v luksOpen $(LOOPDEV) dummy || true + @sync + ./fileDiffer.py $(IMG) $(ORIG_IMG) + @echo "success" + + @echo Case: format +# All headers items and first key material section must change + @cp $(IMG) $(ORIG_IMG) + echo "key0" | ../src/cryptsetup -v -i 1000 -c aes-cbc-essiv:sha256 luksFormat $(LOOPDEV) + @sync + ./fileDiffer.py $(IMG) $(ORIG_IMG) $(LUKS_HEADER) $(KEY_SLOT0) $(KEY_MATERIAL0) + + @echo Case: format using hash sha512 + @cp $(IMG) $(ORIG_IMG) + echo "key0" | ../src/cryptsetup -v -i 1000 -h sha512 -c aes-cbc-essiv:sha256 luksFormat $(LOOPDEV) + @sync + ./fileDiffer.py $(IMG) $(ORIG_IMG) $(LUKS_HEADER) $(KEY_SLOT0) $(KEY_MATERIAL0) + + @echo Case: open +# Image must not change + @cp $(IMG) $(ORIG_IMG) + echo "key0" | ../src/cryptsetup -v luksOpen $(LOOPDEV) dummy + @sync + ./fileDiffer.py $(IMG) $(ORIG_IMG) + @-ls -l /dev/mapper/dummy > /dev/null + @../src/cryptsetup remove dummy + @echo "success" + + @echo Case: add key +# Key Slot 1 and key material section 1 must change, the rest must not. + @cp $(IMG) $(ORIG_IMG) + echo -e "key0\nkey1" | ../src/cryptsetup -v luksAddKey $(LOOPDEV) + @sync + ./fileDiffer.py $(IMG) $(ORIG_IMG) $(KEY_SLOT1) $(KEY_MATERIAL1) + echo "key1" | ../src/cryptsetup -v luksOpen $(LOOPDEV) dummy + @-ls -l /dev/mapper/dummy > /dev/null + @../src/cryptsetup -v remove dummy + @echo "success" + +# Unsuccessful Key Delete - nothing may change + @echo Case: unsuccessful delete + @cp $(IMG) $(ORIG_IMG) + echo "invalid" | ../src/cryptsetup -v luksDelKey $(LOOPDEV) 1 || true + @sync + ./fileDiffer.py $(IMG) $(ORIG_IMG) + @echo "success" + +# Delete Key Test +# Key Slot 1 and key material section 1 must change, the rest must not + @echo Case: successful delete + @cp $(IMG) $(ORIG_IMG) + ../src/cryptsetup -v -q luksDelKey $(LOOPDEV) 1 + @sync + ./fileDiffer.py $(IMG) $(ORIG_IMG) $(KEY_SLOT1) $(KEY_MATERIAL1_EXT) + echo "key1" | ../src/cryptsetup -v luksOpen $(LOOPDEV) dummy 2>/dev/null || true + echo "key0" | ../src/cryptsetup -v luksOpen $(LOOPDEV) dummy 2>/dev/null + @../src/cryptsetup -v remove dummy + @echo "success" + +# Non-Exclusive Open test + @echo Case: Non-Exclusive Open + echo "key0" | ../src/cryptsetup -v --readonly luksOpen $(LOOPDEV) dummy1 2>/dev/null +# must fail + echo "key0" | ../src/cryptsetup -v --readonly luksOpen $(LOOPDEV) dummy2 2>/dev/null || true + echo "key0" | ../src/cryptsetup -v --non-exclusive --readonly luksOpen $(LOOPDEV) dummy2 2>/dev/null + @../src/cryptsetup -v remove dummy1 + @../src/cryptsetup -v remove dummy2 + + +# Key Slot 1 and key material section 1 must change, the rest must not + @echo Case: add key test for key files + @cp $(IMG) $(ORIG_IMG) + echo "key0" | ../src/cryptsetup -v luksAddKey $(LOOPDEV) /tmp/key1 + @sync + ./fileDiffer.py $(IMG) $(ORIG_IMG) $(KEY_SLOT1) $(KEY_MATERIAL1) + ../src/cryptsetup -d /tmp/key1 -v luksOpen $(LOOPDEV) dummy + @-ls -l /dev/mapper/dummy > /dev/null + @../src/cryptsetup -v remove dummy + @echo "success" + + @echo Case: delete key test with /tmp/key1 as remaining key +# Key Slot 1 and key material section 1 must change, the rest must not + @cp $(IMG) $(ORIG_IMG) + ../src/cryptsetup -v -d /tmp/key1 luksDelKey $(LOOPDEV) 0 + @sync + ./fileDiffer.py $(IMG) $(ORIG_IMG) $(KEY_SLOT0) $(KEY_MATERIAL0_EXT) + echo "key0" | ../src/cryptsetup -v luksOpen $(LOOPDEV) dummy 2>/dev/null || true + ../src/cryptsetup -v luksOpen -d /tmp/key1 $(LOOPDEV) dummy 2>/dev/null + @../src/cryptsetup -v remove dummy + @echo "success" + +# Delete last slot + @echo Case: delete last key + @cp $(IMG) $(ORIG_IMG) + @echo "key0" | ../src/cryptsetup -v luksFormat $(LOOPDEV) + echo "key0" | ../src/cryptsetup -v luksKillSlot $(LOOPDEV) 0 + @sync + echo "key0" | ../src/cryptsetup -v luksOpen $(LOOPDEV) dummy 2>/dev/null || true + @echo "success" + +# Format test for ESSIV, and some other parameters. + @echo Case: parameter variation test + @dd if=/dev/zero of=$(IMG) count=20000 + @cp $(IMG) $(ORIG_IMG) + @../src/cryptsetup -q -v -i 1000 -c aes-cbc-essiv:sha256 luksFormat $(LOOPDEV) /tmp/key1 + @sync + ./fileDiffer.py $(IMG) $(ORIG_IMG) $(LUKS_HEADER) $(KEY_SLOT0) $(KEY_MATERIAL0) + @../src/cryptsetup -d /tmp/key1 -v luksOpen $(LOOPDEV) dummy + @-ls -l /dev/mapper/dummy > /dev/null && echo "success" + @../src/cryptsetup -v remove dummy + + @-/sbin/losetup -d $(LOOPDEV) diff --git a/tests/apitest.c b/tests/apitest.c new file mode 100644 index 0000000..12e8f30 --- /dev/null +++ b/tests/apitest.c @@ -0,0 +1,488 @@ +/* + * cryptsetup library API check functions + * + * Copyright (C) 2009 Red Hat, Inc. All rights reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "libcryptsetup.h" + +#define DMDIR "/dev/mapper/" + +#define DEVICE_1 "/dev/loop5" +#define DEVICE_1_UUID "28632274-8c8a-493f-835b-da802e1c576b" +#define DEVICE_2 "/dev/loop6" +#define DEVICE_EMPTY_name "crypt_zero" +#define DEVICE_EMPTY DMDIR DEVICE_EMPTY_name +#define DEVICE_ERROR_name "crypt_error" +#define DEVICE_ERROR DMDIR DEVICE_ERROR_name + +#define CDEVICE_1 "ctest1" +#define CDEVICE_2 "ctest2" +#define CDEVICE_WRONG "O_o" + +#define IMAGE1 "compatimage.img" +#define IMAGE_EMPTY "empty.img" + +#define KEYFILE1 "key1.file" +#define KEY1 "compatkey" + +#define KEYFILE2 "key2.file" +#define KEY2 "0123456789abcdef" + +static int _debug = 0; +static int _verbose = 1; + +static char global_log[4096]; + +// Helpers +static int _prepare_keyfile(const char *name, const char *passphrase) +{ + int fd, r; + + fd = open(name, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR); + if (fd != -1) { + r = write(fd, passphrase, strlen(passphrase)); + close(fd); + } else + r = 0; + + return r == strlen(passphrase) ? 0 : 1; +} + +static void _remove_keyfiles(void) +{ + remove(KEYFILE1); + remove(KEYFILE2); +} + +static int yesDialog(char *msg) +{ + return 1; +} + +static void cmdLineLog(int class, char *msg) +{ + strncat(global_log, msg, sizeof(global_log)); +} + +static void reset_log() +{ + memset(global_log, 0, sizeof(global_log)); +} + +static struct interface_callbacks cmd_icb = { + .yesDialog = yesDialog, + .log = cmdLineLog, +}; + +static void _cleanup(void) +{ + struct stat st; + + //system("udevadm settle"); + + if (!stat(DMDIR CDEVICE_1, &st)) + system("dmsetup remove " CDEVICE_1); + + if (!stat(DMDIR CDEVICE_2, &st)) + system("dmsetup remove " CDEVICE_2); + + if (!stat(DEVICE_EMPTY, &st)) + system("dmsetup remove " DEVICE_EMPTY_name); + + if (!stat(DEVICE_ERROR, &st)) + system("dmsetup remove " DEVICE_ERROR_name); + + if (!strncmp("/dev/loop", DEVICE_1, 9)) + system("losetup -d " DEVICE_1); + + if (!strncmp("/dev/loop", DEVICE_2, 9)) + system("losetup -d " DEVICE_2); + + system("rm -f " IMAGE_EMPTY); + _remove_keyfiles(); +} + +static void _setup(void) +{ + system("dmsetup create " DEVICE_EMPTY_name " --table \"0 10000 zero\""); + system("dmsetup create " DEVICE_ERROR_name " --table \"0 10000 error\""); + if (!strncmp("/dev/loop", DEVICE_1, 9)) + system("losetup " DEVICE_1 " " IMAGE1); + if (!strncmp("/dev/loop", DEVICE_2, 9)) { + system("dd if=/dev/zero of=" IMAGE_EMPTY " bs=1M count=4"); + system("losetup " DEVICE_2 " " IMAGE_EMPTY); + } + +} + +void check_ok(int status, int line, const char *func) +{ + char buf[256]; + + if (status) { + crypt_get_error(buf, sizeof(buf)); + printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, buf); + _cleanup(); + exit(-1); + } +} + +void check_ko(int status, int line, const char *func) +{ + char buf[256]; + + memset(buf, 0, sizeof(buf)); + crypt_get_error(buf, sizeof(buf)); + if (status >= 0) { + printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, buf); + _cleanup(); + exit(-1); + } else if (_verbose) + printf(" => errno %d, errmsg: %s\n", status, buf); +} + +void check_equal(int line, const char *func) +{ + printf("FAIL line %d [%s]: expected equal values differs.\n", line, func); + _cleanup(); + exit(-1); +} + +void xlog(const char *msg, const char *tst, const char *func, int line, const char *txt) +{ + if (_verbose) { + if (txt) + printf(" [%s,%s:%d] %s [%s]\n", msg, func, line, tst, txt); + else + printf(" [%s,%s:%d] %s\n", msg, func, line, tst); + } +} +#define OK_(x) do { xlog("(success)", #x, __FUNCTION__, __LINE__, NULL); \ + check_ok((x), __LINE__, __FUNCTION__); \ + } while(0) +#define FAIL_(x, y) do { xlog("(fail) ", #x, __FUNCTION__, __LINE__, y); \ + check_ko((x), __LINE__, __FUNCTION__); \ + } while(0) +#define EQ_(x, y) do { xlog("(equal) ", #x " == " #y, __FUNCTION__, __LINE__, NULL); \ + if ((x) != (y)) check_equal(__LINE__, __FUNCTION__); \ + } while(0) + +#define RUN_(x, y) do { printf("%s: %s\n", #x, (y)); x(); } while (0) + +// OLD API TESTS +static void LuksUUID(void) +{ + struct crypt_options co = { .icb = &cmd_icb }; + + co.device = DEVICE_EMPTY; + EQ_(crypt_luksUUID(&co), -EINVAL); + + co.device = DEVICE_ERROR; + EQ_(crypt_luksUUID(&co), -EINVAL); + + reset_log(); + co.device = DEVICE_1; + OK_(crypt_luksUUID(&co)); + EQ_(strlen(global_log), 37); /* UUID + "\n" */ + EQ_(strncmp(global_log, DEVICE_1_UUID, strlen(DEVICE_1_UUID)), 0); + +} + +static void IsLuks(void) +{ + struct crypt_options co = { .icb = &cmd_icb }; + + co.device = DEVICE_EMPTY; + EQ_(crypt_isLuks(&co), -EINVAL); + + co.device = DEVICE_ERROR; + EQ_(crypt_isLuks(&co), -EINVAL); + + co.device = DEVICE_1; + OK_(crypt_isLuks(&co)); +} + +static void LuksOpen(void) +{ + struct crypt_options co = { + .name = CDEVICE_1, + //.passphrase = "blabla", + .icb = &cmd_icb, + }; + + OK_(_prepare_keyfile(KEYFILE1, KEY1)); + co.key_file = KEYFILE1; + + co.device = DEVICE_EMPTY; + EQ_(crypt_luksOpen(&co), -EINVAL); + + co.device = DEVICE_ERROR; + EQ_(crypt_luksOpen(&co), -EINVAL); + + co.device = DEVICE_1; + OK_(crypt_luksOpen(&co)); + FAIL_(crypt_luksOpen(&co), "already open"); + + _remove_keyfiles(); +} + +static void query_device(void) +{ + struct crypt_options co = {. icb = &cmd_icb }; + + co.name = CDEVICE_WRONG; + EQ_(crypt_query_device(&co), 0); + + co.name = CDEVICE_1; + EQ_(crypt_query_device(&co), 1); + + OK_(strncmp(crypt_get_dir(), DMDIR, 11)); + OK_(strcmp(co.cipher, "aes-cbc-essiv:sha256")); + EQ_(co.key_size, 16); + EQ_(co.offset, 1032); + EQ_(co.flags & CRYPT_FLAG_READONLY, 0); + EQ_(co.skip, 0); + crypt_put_options(&co); +} + +static void remove_device(void) +{ + int fd; + struct crypt_options co = {. icb = &cmd_icb }; + + co.name = CDEVICE_WRONG; + EQ_(crypt_remove_device(&co), -ENODEV); + + fd = open(DMDIR CDEVICE_1, O_RDONLY); + co.name = CDEVICE_1; + FAIL_(crypt_remove_device(&co), "device busy"); + close(fd); + + OK_(crypt_remove_device(&co)); +} + +static void LuksFormat(void) +{ + struct crypt_options co = { + .device = DEVICE_2, + .key_size = 256 / 8, + .key_slot = -1, + .cipher = "aes-cbc-essiv:sha256", + .hash = "sha1", + .flags = 0, + .iteration_time = 10, + .align_payload = 0, + .icb = &cmd_icb, + }; + + OK_(_prepare_keyfile(KEYFILE1, KEY1)); + + co.new_key_file = KEYFILE1; + co.device = DEVICE_ERROR; + FAIL_(crypt_luksFormat(&co), "error device"); + + co.device = DEVICE_2; + OK_(crypt_luksFormat(&co)); + + co.new_key_file = NULL; + co.key_file = KEYFILE1; + co.name = CDEVICE_2; + OK_(crypt_luksOpen(&co)); + OK_(crypt_remove_device(&co)); + _remove_keyfiles(); +} + +static void LuksKeyGame(void) +{ + int i; + struct crypt_options co = { + .device = DEVICE_2, + .key_size = 256 / 8, + .key_slot = -1, + .cipher = "aes-cbc-essiv:sha256", + .hash = "sha1", + .flags = 0, + .iteration_time = 10, + .align_payload = 0, + .icb = &cmd_icb, + }; + + OK_(_prepare_keyfile(KEYFILE1, KEY1)); + OK_(_prepare_keyfile(KEYFILE2, KEY2)); + + co.new_key_file = KEYFILE1; + co.device = DEVICE_2; + co.key_slot = 8; + FAIL_(crypt_luksFormat(&co), "wrong slot #"); + + co.key_slot = 7; // last slot + OK_(crypt_luksFormat(&co)); + + co.new_key_file = KEYFILE1; + co.key_file = KEYFILE1; + co.key_slot = 8; + FAIL_(crypt_luksAddKey(&co), "wrong slot #"); + co.key_slot = 7; + FAIL_(crypt_luksAddKey(&co), "slot already used"); + + co.key_slot = 6; + OK_(crypt_luksAddKey(&co)); + + co.key_file = KEYFILE2 "blah"; + co.key_slot = 5; + FAIL_(crypt_luksAddKey(&co), "keyfile not found"); + + co.new_key_file = KEYFILE2; // key to add + co.key_file = KEYFILE1; + co.key_slot = -1; + for (i = 0; i < 6; i++) + OK_(crypt_luksAddKey(&co)); //FIXME: EQ_(i)? + + FAIL_(crypt_luksAddKey(&co), "all slots full"); + + // REMOVE KEY + co.new_key_file = KEYFILE1; // key to remove + co.key_file = NULL; + co.key_slot = 8; // should be ignored + // only 2 slots should use KEYFILE1 + OK_(crypt_luksRemoveKey(&co)); + OK_(crypt_luksRemoveKey(&co)); + FAIL_(crypt_luksRemoveKey(&co), "no slot with this passphrase"); + + co.new_key_file = KEYFILE2 "blah"; + co.key_file = NULL; + FAIL_(crypt_luksRemoveKey(&co), "keyfile not found"); + + // KILL SLOT + co.new_key_file = NULL; + co.key_file = NULL; + co.key_slot = 8; + FAIL_(crypt_luksKillSlot(&co), "wrong slot #"); + co.key_slot = 7; + FAIL_(crypt_luksKillSlot(&co), "slot already wiped"); + + co.key_slot = 5; + OK_(crypt_luksKillSlot(&co)); + + _remove_keyfiles(); +} + +size_t _get_device_size(const char *device) +{ + unsigned long size = 0; + int fd; + + fd = open(device, O_RDONLY); + if (fd == -1) + return 0; + (void)ioctl(fd, BLKGETSIZE, &size); + close(fd); + + return size; +} + +void DeviceResizeGame(void) +{ + size_t orig_size; + struct crypt_options co = { + .name = CDEVICE_2, + .device = DEVICE_2, + .key_size = 128 / 8, + .cipher = "aes-cbc-plain", + .hash = "sha1", + .offset = 333, + .skip = 0, + .icb = &cmd_icb, + }; + + orig_size = _get_device_size(DEVICE_2); + + OK_(_prepare_keyfile(KEYFILE2, KEY2)); + + co.key_file = KEYFILE2; + co.size = 1000; + OK_(crypt_create_device(&co)); + EQ_(_get_device_size(DMDIR CDEVICE_2), 1000); + + co.size = 2000; + OK_(crypt_resize_device(&co)); + EQ_(_get_device_size(DMDIR CDEVICE_2), 2000); + + co.size = 0; + OK_(crypt_resize_device(&co)); + EQ_(_get_device_size(DMDIR CDEVICE_2), (orig_size - 333)); + + co.size = 0; + co.offset = 444; + co.skip = 555; + co.cipher = "aes-cbc-benbi"; + OK_(crypt_update_device(&co)); + EQ_(_get_device_size(DMDIR CDEVICE_2), (orig_size - 444)); + + memset(&co, 0, sizeof(co)); + co.icb = &cmd_icb, + co.name = CDEVICE_2; + EQ_(crypt_query_device(&co), 1); + EQ_(strcmp(co.cipher, "aes-cbc-benbi"), 0); + EQ_(co.key_size, 128 / 8); + EQ_(co.offset, 444); + EQ_(co.skip, 555); + OK_(crypt_remove_device(&co)); + + crypt_put_options(&co); + + _remove_keyfiles(); +} + +int main (int argc, char *argv[]) +{ + int i; + + for (i = 1; i < argc; i++) { + if (!strcmp("-v", argv[i]) || !strcmp("--verbose", argv[i])) + _verbose = 1; + else if (!strcmp("--debug", argv[i])) + _debug = _verbose = 1; + } + + _cleanup(); + _setup(); + +#ifdef CRYPT_DEBUG_ALL + crypt_set_debug_level(_debug ? CRYPT_DEBUG_ALL : CRYPT_DEBUG_NONE); +#endif + + RUN_(LuksUUID, "luksUUID API call"); + RUN_(IsLuks, "isLuks API call"); + RUN_(LuksOpen, "luksOpen API call"); + RUN_(query_device, "crypt_query_device API call"); + RUN_(remove_device, "crypt_remove_device API call"); + RUN_(LuksFormat, "luksFormat API call"); + RUN_(LuksKeyGame, "luksAddKey, RemoveKey, KillSlot API calls"); + RUN_(DeviceResizeGame, "regular crypto, resize calls"); + + _cleanup(); + return 0; +} diff --git a/luks/testing/compatimage.bz2 b/tests/compatimage.img.bz2 similarity index 100% rename from luks/testing/compatimage.bz2 rename to tests/compatimage.img.bz2 diff --git a/luks/testing/fileDiffer.py b/tests/fileDiffer.py similarity index 100% rename from luks/testing/fileDiffer.py rename to tests/fileDiffer.py -- 2.7.4