Revert "Imported upstream version 1.6.7"
[platform/upstream/cryptsetup.git] / tests / api-test.c
index 3ccfa73..5607f8d 100644 (file)
@@ -2,7 +2,7 @@
  * cryptsetup library API check functions
  *
  * Copyright (C) 2009-2012 Red Hat, Inc. All rights reserved.
- * Copyright (C) 2009-2012, Milan Broz
+ * Copyright (C) 2009-2013, Milan Broz
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -27,6 +27,7 @@
 #include <linux/fs.h>
 #include <errno.h>
 #include <assert.h>
+#include <signal.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
 #include <libdevmapper.h>
@@ -86,6 +87,8 @@ static int _debug   = 0;
 static int _verbose = 1;
 static int _fips_mode = 0;
 
+static int _quit = 0;
+
 static char global_log[4096];
 static int global_lines = 0;
 
@@ -520,6 +523,12 @@ static void xlog(const char *msg, const char *tst, const char *func, int line, c
                else
                        printf(" [%s,%s:%d] %s\n", msg, func, line, tst);
        }
+       if (_quit) {
+               if (_verbose)
+                       printf("Interrupted by a signal.\n");
+               _cleanup();
+               exit(-1);
+       }
 }
 
 /* crypt_device context must be "cd" to parse error properly here */
@@ -529,8 +538,8 @@ static void xlog(const char *msg, const char *tst, const char *func, int line, c
 #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); \
-                            int64_t _x = (x), _y = (y); \
+#define EQ_(x, y)      do { int64_t _x = (x), _y = (y); \
+                            xlog("(equal)  ", #x " == " #y, __FUNCTION__, __LINE__, NULL); \
                             if (_x != _y) check_equal(__LINE__, __FUNCTION__, _x, _y); \
                        } while(0)
 #define RUN_(x, y)             do { printf("%s: %s\n", #x, (y)); x(); } while (0)
@@ -1743,6 +1752,7 @@ static void TcryptTest(void)
        };
        double enc_mbr = 0, dec_mbr = 0;
        const char *tcrypt_dev = "tcrypt-images/tck_5-sha512-xts-aes";
+       const char *tcrypt_dev2 = "tcrypt-images/tc_5-sha512-xts-serpent-twofish-aes";
        size_t key_size = 64;
        char key[key_size], key_def[key_size];
        const char *key_hex =
@@ -1812,6 +1822,18 @@ static void TcryptTest(void)
 
        OK_(crypt_deactivate(cd, CDEVICE_1));
        crypt_free(cd);
+
+       OK_(crypt_init(&cd, tcrypt_dev2));
+       params.keyfiles = NULL;
+       params.keyfiles_count = 0;
+       OK_(crypt_load(cd, CRYPT_TCRYPT, &params));
+       OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, NULL, 0, CRYPT_ACTIVATE_READONLY));
+       crypt_free(cd);
+
+       // Deactivate the whole chain
+       EQ_(crypt_status(NULL, CDEVICE_1 "_1"), CRYPT_BUSY);
+       OK_(crypt_deactivate(NULL, CDEVICE_1));
+       EQ_(crypt_status(NULL, CDEVICE_1 "_1"), CRYPT_INACTIVE);
 }
 
 // Check that gcrypt is properly initialised in format
@@ -1849,8 +1871,14 @@ static void NonFIPSAlg(void)
        crypt_free(cd);
 }
 
+static void int_handler(int sig __attribute__((__unused__)))
+{
+       _quit++;
+}
+
 int main(int argc, char *argv[])
 {
+       struct sigaction sa = { .sa_handler = int_handler };
        int i;
 
        if (getuid() != 0) {
@@ -1865,6 +1893,10 @@ int main(int argc, char *argv[])
                        _debug = _verbose = 1;
        }
 
+       /* Handle interrupt properly */
+       sigaction(SIGINT, &sa, NULL);
+       sigaction(SIGTERM, &sa, NULL);
+
        _cleanup();
        if (_setup())
                goto out;