]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-dcrypt: Reformatted test-stream.c.
authorStephan Bosch <stephan.bosch@dovecot.fi>
Sat, 17 Feb 2018 20:51:00 +0000 (21:51 +0100)
committerVille Savolainen <ville.savolainen@dovecot.fi>
Mon, 16 Apr 2018 08:22:46 +0000 (11:22 +0300)
src/lib-dcrypt/test-stream.c

index 34a3b7c93083ac1e85c7be48d61bf4e5f7abc3dc..ebf36e7515dda11b697c2cc3961b343b0803f03f 100644 (file)
 #include "hash-method.h"
 #include "test-common.h"
 #include "hex-binary.h"
+
 #include <fcntl.h>
 #include <sys/stat.h>
 #include <stdio.h>
 
-static const char key_v1_priv[] = "-----BEGIN PRIVATE KEY-----\n" \
-"MIGpAgEAMBAGByqGSM49AgEGBSuBBAAjBIGRMIGOAgEBBEGz2V2VMi/5s+Z+GJh7\n" \
-"4WfqZjZUpqqm+NJWojm6BbrZMY+9ZComlTGVcUZ007acFxV93oMmrfmtRUb5ynrb\n" \
-"MRFskKFGA0QAAwHrAJc8TvyPzspOoz6UH1C1YRmaUVm8tsLu2d0dYtZeOKJUl52J\n" \
-"4o8MKIg+ce4q0mTNFrhj+glKj29ppWti6JGAQA==\n" \
-"-----END PRIVATE KEY-----";
-
-static const char key_v1_pub[] = "-----BEGIN PUBLIC KEY-----\n" \
-"MFgwEAYHKoZIzj0CAQYFK4EEACMDRAADAesAlzxO/I/Oyk6jPpQfULVhGZpRWby2\n" \
-"wu7Z3R1i1l44olSXnYnijwwoiD5x7irSZM0WuGP6CUqPb2mla2LokYBA\n" \
-"-----END PUBLIC KEY-----";
-
-static const char key_v2_priv[] = "-----BEGIN PRIVATE KEY-----\n" \
-"MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgtuQJA+uboZWVwgHc\n" \
-"DciyVdrovAPwlMqshDK3s78IDDuhRANCAAQm0VEdzLB9PtD0HA8JK1zifWnj8M00\n" \
-"FQzedfp9SQsWyA8dzs5/NFR5MTe6Xbh/ndKEs1zZH3vZ4FlNrilZc0st\n" \
-"-----END PRIVATE KEY-----";
-
-static const char key_v2_pub[] = "-----BEGIN PUBLIC KEY-----\n" \
-"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEJtFRHcywfT7Q9BwPCStc4n1p4/DN\n" \
-"NBUM3nX6fUkLFsgPHc7OfzRUeTE3ul24f53ShLNc2R972eBZTa4pWXNLLQ==\n" \
-"-----END PUBLIC KEY-----";
-
-static const char test_sample_v1_hash[] = "1d7cc2cc1f1983f76241cc42389911e88590ad58cf9d54cafeb5b198d3723dd1";
-static const char test_sample_v1_short_hash[] = "b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c";
-static const char test_sample_v2_hash[] = "2e31218656dd34db65b321688bf418dee4ee785e99eb9c21e0d29b4af27a863e";
+static const char key_v1_priv[] =
+       "-----BEGIN PRIVATE KEY-----\n"
+       "MIGpAgEAMBAGByqGSM49AgEGBSuBBAAjBIGRMIGOAgEBBEGz2V2VMi/5s+Z+GJh7\n"
+       "4WfqZjZUpqqm+NJWojm6BbrZMY+9ZComlTGVcUZ007acFxV93oMmrfmtRUb5ynrb\n"
+       "MRFskKFGA0QAAwHrAJc8TvyPzspOoz6UH1C1YRmaUVm8tsLu2d0dYtZeOKJUl52J\n"
+       "4o8MKIg+ce4q0mTNFrhj+glKj29ppWti6JGAQA==\n"
+       "-----END PRIVATE KEY-----";
+
+static const char key_v1_pub[] =
+       "-----BEGIN PUBLIC KEY-----\n"
+       "MFgwEAYHKoZIzj0CAQYFK4EEACMDRAADAesAlzxO/I/Oyk6jPpQfULVhGZpRWby2\n"
+       "wu7Z3R1i1l44olSXnYnijwwoiD5x7irSZM0WuGP6CUqPb2mla2LokYBA\n"
+       "-----END PUBLIC KEY-----";
+
+static const char key_v2_priv[] =
+       "-----BEGIN PRIVATE KEY-----\n"
+       "MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgtuQJA+uboZWVwgHc\n"
+       "DciyVdrovAPwlMqshDK3s78IDDuhRANCAAQm0VEdzLB9PtD0HA8JK1zifWnj8M00\n"
+       "FQzedfp9SQsWyA8dzs5/NFR5MTe6Xbh/ndKEs1zZH3vZ4FlNrilZc0st\n"
+       "-----END PRIVATE KEY-----";
+
+static const char key_v2_pub[] =
+       "-----BEGIN PUBLIC KEY-----\n"
+       "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEJtFRHcywfT7Q9BwPCStc4n1p4/DN\n"
+       "NBUM3nX6fUkLFsgPHc7OfzRUeTE3ul24f53ShLNc2R972eBZTa4pWXNLLQ==\n"
+       "-----END PUBLIC KEY-----";
+
+static const char test_sample_v1_hash[] =
+       "1d7cc2cc1f1983f76241cc42389911e88590ad58cf9d54cafeb5b198d3723dd1";
+static const char test_sample_v1_short_hash[] =
+       "b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c";
+static const char test_sample_v2_hash[] =
+       "2e31218656dd34db65b321688bf418dee4ee785e99eb9c21e0d29b4af27a863e";
 
 static struct dcrypt_keypair test_v1_kp;
 static struct dcrypt_keypair test_v2_kp;
 
-static
-void test_static_v1_input(void)
+static void test_static_v1_input(void)
 {
        ssize_t siz;
        const struct hash_method *hash = hash_method_lookup("sha256");
@@ -60,7 +67,9 @@ void test_static_v1_input(void)
 
        test_begin("test_static_v1_input");
 
-       struct istream *is_1 = i_stream_create_file(DCRYPT_SRC_DIR"/sample-v1.asc", IO_BLOCK_SIZE);
+       struct istream *is_1 =
+               i_stream_create_file(DCRYPT_SRC_DIR"/sample-v1.asc",
+                                    IO_BLOCK_SIZE);
        struct istream *is_2 = i_stream_create_base64_decoder(is_1);
        i_stream_unref(&is_1);
        struct istream *is_3 = i_stream_create_decrypt(is_2, test_v1_kp.priv);
@@ -79,13 +88,13 @@ void test_static_v1_input(void)
 
        hash->result(hash_ctx, hash_dgst);
 
-       test_assert(strcmp(test_sample_v1_hash, binary_to_hex(hash_dgst, sizeof(hash_dgst))) == 0);
+       test_assert(strcmp(test_sample_v1_hash,
+                          binary_to_hex(hash_dgst, sizeof(hash_dgst))) == 0);
 
        test_end();
 }
 
-static
-void test_static_v1_input_short(void)
+static void test_static_v1_input_short(void)
 {
        ssize_t siz;
        const struct hash_method *hash = hash_method_lookup("sha256");
@@ -95,7 +104,9 @@ void test_static_v1_input_short(void)
 
        test_begin("test_static_v1_input_short");
 
-       struct istream *is_1 = i_stream_create_file(DCRYPT_SRC_DIR"/sample-v1_short.asc", IO_BLOCK_SIZE);
+       struct istream *is_1 =
+               i_stream_create_file(DCRYPT_SRC_DIR"/sample-v1_short.asc",
+                                    IO_BLOCK_SIZE);
        struct istream *is_2 = i_stream_create_base64_decoder(is_1);
        i_stream_unref(&is_1);
        struct istream *is_3 = i_stream_create_decrypt(is_2, test_v1_kp.priv);
@@ -114,13 +125,13 @@ void test_static_v1_input_short(void)
 
        hash->result(hash_ctx, hash_dgst);
 
-       test_assert(strcmp(test_sample_v1_short_hash, binary_to_hex(hash_dgst, sizeof(hash_dgst))) == 0);
+       test_assert(strcmp(test_sample_v1_short_hash,
+                          binary_to_hex(hash_dgst, sizeof(hash_dgst))) == 0);
 
        test_end();
 }
 
-static
-void test_static_v2_input(void)
+static void test_static_v2_input(void)
 {
        test_begin("test_static_v2_input");
 
@@ -130,7 +141,9 @@ void test_static_v2_input(void)
        unsigned char hash_dgst[hash->digest_size];
        hash->init(hash_ctx);
 
-       struct istream *is_1 = i_stream_create_file(DCRYPT_SRC_DIR"/sample-v2.asc", IO_BLOCK_SIZE);
+       struct istream *is_1 =
+               i_stream_create_file(DCRYPT_SRC_DIR"/sample-v2.asc",
+                                    IO_BLOCK_SIZE);
        struct istream *is_2 = i_stream_create_base64_decoder(is_1);
        i_stream_unref(&is_1);
        struct istream *is_3 = i_stream_create_decrypt(is_2, test_v2_kp.priv);
@@ -149,16 +162,20 @@ void test_static_v2_input(void)
 
        hash->result(hash_ctx, hash_dgst);
 
-       test_assert(strcmp(test_sample_v2_hash, binary_to_hex(hash_dgst, sizeof(hash_dgst))) == 0);
+       test_assert(strcmp(test_sample_v2_hash,
+                   binary_to_hex(hash_dgst, sizeof(hash_dgst))) == 0);
 
        test_end();
 
 /** this code is left here to show how the sample file is created
-       struct istream *is = i_stream_create_file("../lib-fts/udhr_fra.txt", 8192);
+       struct istream *is =
+               i_stream_create_file("../lib-fts/udhr_fra.txt", 8192);
        struct istream *is_2 = i_stream_create_hash(is, hash, hash_ctx);
        int fd = open("sample-v2.bin", O_CREAT|O_TRUNC|O_WRONLY, S_IRWXU);
        struct ostream *os = o_stream_create_fd_file(fd, 0, TRUE);
-       struct ostream *os_2 = o_stream_create_encrypt(os, "aes-256-gcm-sha256", test_v2_kp.pub, IO_STREAM_ENC_INTEGRITY_AEAD);
+       struct ostream *os_2 = o_stream_create_encrypt(os,
+               "aes-256-gcm-sha256", test_v2_kp.pub,
+               IO_STREAM_ENC_INTEGRITY_AEAD);
        const unsigned char *ptr;
        size_t siz;
 
@@ -177,8 +194,7 @@ void test_static_v2_input(void)
 */
 }
 
-static
-void test_write_read_v1(void)
+static void test_write_read_v1(void)
 {
        test_begin("test_write_read_v1");
        unsigned char payload[IO_BLOCK_SIZE];
@@ -188,7 +204,8 @@ void test_write_read_v1(void)
 
        buffer_t *buf = buffer_create_dynamic(default_pool, sizeof(payload));
        struct ostream *os = o_stream_create_buffer(buf);
-       struct ostream *os_2 = o_stream_create_encrypt(os, "<unused>", test_v2_kp.pub, IO_STREAM_ENC_VERSION_1);
+       struct ostream *os_2 = o_stream_create_encrypt(os,
+               "<unused>", test_v2_kp.pub, IO_STREAM_ENC_VERSION_1);
        o_stream_nsend(os_2, payload, sizeof(payload));
 
        if (os_2->stream_errno != 0)
@@ -214,8 +231,10 @@ void test_write_read_v1(void)
                        test_istream_set_size(is, ++offset);
 
                test_assert_idx(pos + siz <= sizeof(payload), pos);
-               if (pos + siz > sizeof(payload)) break;
-               test_assert_idx(siz == 0 || memcmp(ptr, payload + pos, siz) == 0, pos);
+               if (pos + siz > sizeof(payload))
+                       break;
+               test_assert_idx(siz == 0 ||
+                               memcmp(ptr, payload + pos, siz) == 0, pos);
                i_stream_skip(is_2, siz); pos += siz;
        }
 
@@ -228,8 +247,7 @@ void test_write_read_v1(void)
        test_end();
 }
 
-static
-void test_write_read_v1_short(void)
+static void test_write_read_v1_short(void)
 {
        test_begin("test_write_read_v1_short");
        unsigned char payload[1];
@@ -239,7 +257,8 @@ void test_write_read_v1_short(void)
 
        buffer_t *buf = buffer_create_dynamic(default_pool, 64);
        struct ostream *os = o_stream_create_buffer(buf);
-       struct ostream *os_2 = o_stream_create_encrypt(os, "<unused>", test_v2_kp.pub, IO_STREAM_ENC_VERSION_1);
+       struct ostream *os_2 = o_stream_create_encrypt(os,
+               "<unused>", test_v2_kp.pub, IO_STREAM_ENC_VERSION_1);
        o_stream_nsend(os_2, payload, sizeof(payload));
 
        if (os_2->stream_errno != 0)
@@ -265,8 +284,10 @@ void test_write_read_v1_short(void)
                        test_istream_set_size(is, ++offset);
 
                test_assert_idx(pos + siz <= sizeof(payload), pos);
-               if (siz > sizeof(payload) || pos + siz > sizeof(payload)) break;
-               test_assert_idx(siz == 0 || memcmp(ptr, payload + pos, siz) == 0, pos);
+               if (siz > sizeof(payload) || pos + siz > sizeof(payload))
+                       break;
+               test_assert_idx(siz == 0 ||
+                               memcmp(ptr, payload + pos, siz) == 0, pos);
                i_stream_skip(is_2, siz); pos += siz;
        }
 
@@ -279,15 +300,15 @@ void test_write_read_v1_short(void)
        test_end();
 }
 
-static
-void test_write_read_v1_empty(void)
+static void test_write_read_v1_empty(void)
 {
        const unsigned char *ptr;
        size_t siz;
        test_begin("test_write_read_v1_empty");
        buffer_t *buf = buffer_create_dynamic(default_pool, 64);
        struct ostream *os = o_stream_create_buffer(buf);
-       struct ostream *os_2 = o_stream_create_encrypt(os, "<unused>", test_v1_kp.pub, IO_STREAM_ENC_VERSION_1);
+       struct ostream *os_2 = o_stream_create_encrypt(os,
+               "<unused>", test_v1_kp.pub, IO_STREAM_ENC_VERSION_1);
        test_assert(o_stream_finish(os_2) > 0);
        if (os_2->stream_errno != 0)
                i_debug("error: %s", o_stream_get_error(os_2));
@@ -321,8 +342,7 @@ void test_write_read_v1_empty(void)
        test_end();
 }
 
-static
-void test_write_read_v2(void)
+static void test_write_read_v2(void)
 {
        test_begin("test_write_read_v2");
        unsigned char payload[IO_BLOCK_SIZE*10];
@@ -332,7 +352,9 @@ void test_write_read_v2(void)
 
        buffer_t *buf = buffer_create_dynamic(default_pool, sizeof(payload));
        struct ostream *os = o_stream_create_buffer(buf);
-       struct ostream *os_2 = o_stream_create_encrypt(os, "aes-256-gcm-sha256", test_v1_kp.pub, IO_STREAM_ENC_INTEGRITY_AEAD);
+       struct ostream *os_2 = o_stream_create_encrypt(os,
+               "aes-256-gcm-sha256", test_v1_kp.pub,
+               IO_STREAM_ENC_INTEGRITY_AEAD);
        o_stream_nsend(os_2, payload, sizeof(payload));
        test_assert(o_stream_finish(os_2) > 0);
        if (os_2->stream_errno != 0)
@@ -359,7 +381,8 @@ void test_write_read_v2(void)
 
                test_assert_idx(pos + siz <= sizeof(payload), pos);
                if (pos + siz > sizeof(payload)) break;
-               test_assert_idx(siz == 0 || memcmp(ptr, payload + pos, siz) == 0, pos);
+               test_assert_idx(siz == 0 ||
+                               memcmp(ptr, payload + pos, siz) == 0, pos);
                i_stream_skip(is_2, siz); pos += siz;
        }
 
@@ -374,8 +397,7 @@ void test_write_read_v2(void)
        test_end();
 }
 
-static
-void test_write_read_v2_short(void)
+static void test_write_read_v2_short(void)
 {
        test_begin("test_write_read_v2_short");
        unsigned char payload[1];
@@ -385,7 +407,9 @@ void test_write_read_v2_short(void)
 
        buffer_t *buf = buffer_create_dynamic(default_pool, 64);
        struct ostream *os = o_stream_create_buffer(buf);
-       struct ostream *os_2 = o_stream_create_encrypt(os, "aes-256-gcm-sha256", test_v1_kp.pub, IO_STREAM_ENC_INTEGRITY_AEAD);
+       struct ostream *os_2 = o_stream_create_encrypt(os,
+               "aes-256-gcm-sha256", test_v1_kp.pub,
+               IO_STREAM_ENC_INTEGRITY_AEAD);
        o_stream_nsend(os_2, payload, sizeof(payload));
        test_assert(o_stream_finish(os_2) > 0);
        if (os_2->stream_errno != 0)
@@ -406,8 +430,10 @@ void test_write_read_v2_short(void)
                test_istream_set_size(is, ++offset);
 
                test_assert_idx(pos + siz <= sizeof(payload), pos);
-               if (siz > sizeof(payload) || pos + siz > sizeof(payload)) break;
-               test_assert_idx(siz == 0 || memcmp(ptr, payload + pos, siz) == 0, pos);
+               if (siz > sizeof(payload) || pos + siz > sizeof(payload))
+                       break;
+               test_assert_idx(siz == 0 ||
+                               memcmp(ptr, payload + pos, siz) == 0, pos);
                i_stream_skip(is_2, siz); pos += siz;
        }
 
@@ -422,15 +448,16 @@ void test_write_read_v2_short(void)
        test_end();
 }
 
-static
-void test_write_read_v2_empty(void)
+static void test_write_read_v2_empty(void)
 {
        const unsigned char *ptr;
        size_t siz;
        test_begin("test_write_read_v2_empty");
        buffer_t *buf = buffer_create_dynamic(default_pool, 64);
        struct ostream *os = o_stream_create_buffer(buf);
-       struct ostream *os_2 = o_stream_create_encrypt(os, "aes-256-gcm-sha256", test_v1_kp.pub, IO_STREAM_ENC_INTEGRITY_AEAD);
+       struct ostream *os_2 = o_stream_create_encrypt(os,
+               "aes-256-gcm-sha256", test_v1_kp.pub,
+               IO_STREAM_ENC_INTEGRITY_AEAD);
        test_assert(o_stream_finish(os_2) > 0);
        if (os_2->stream_errno != 0)
                i_debug("error: %s", o_stream_get_error(os_2));
@@ -463,10 +490,11 @@ void test_write_read_v2_empty(void)
        test_end();
 }
 
-static int no_op_cb(const char *digest ATTR_UNUSED,
-               struct dcrypt_private_key **priv_key_r ATTR_UNUSED,
-               const char **error_r ATTR_UNUSED,
-               void *context ATTR_UNUSED)
+static int
+no_op_cb(const char *digest ATTR_UNUSED,
+        struct dcrypt_private_key **priv_key_r ATTR_UNUSED,
+        const char **error_r ATTR_UNUSED,
+        void *context ATTR_UNUSED)
 {
        return 0;
 }
@@ -503,8 +531,11 @@ static void test_read_large_header(void)
 {
        test_begin("test_read_large_header");
 
-       struct istream *is = test_istream_create_data(IOSTREAM_CRYPT_MAGIC, sizeof(IOSTREAM_CRYPT_MAGIC));
-       struct istream *ds = i_stream_create_decrypt_callback(is, no_op_cb, NULL);
+       struct istream *is =
+               test_istream_create_data(IOSTREAM_CRYPT_MAGIC,
+                                        sizeof(IOSTREAM_CRYPT_MAGIC));
+       struct istream *ds =
+               i_stream_create_decrypt_callback(is, no_op_cb, NULL);
        test_istream_set_allow_eof(is, FALSE);
        test_istream_set_max_buffer_size(is, sizeof(IOSTREAM_CRYPT_MAGIC));
 
@@ -521,7 +552,8 @@ static void test_read_increment(void)
 
        ssize_t amt, total, i;
 
-       struct istream *is_1 = i_stream_create_file(DCRYPT_SRC_DIR"/sample-v2.asc", IO_BLOCK_SIZE);
+       struct istream *is_1 = i_stream_create_file(
+               DCRYPT_SRC_DIR"/sample-v2.asc", IO_BLOCK_SIZE);
        struct istream *is_2 = i_stream_create_base64_decoder(is_1);
        i_stream_unref(&is_1);
        struct istream *is_3 = i_stream_create_decrypt(is_2, test_v2_kp.priv);
@@ -544,28 +576,32 @@ static void test_read_increment(void)
         test_end();
 }
 
-static
-void test_free_keys() {
+static void test_free_keys()
+{
        dcrypt_key_unref_private(&test_v1_kp.priv);
        dcrypt_key_unref_public(&test_v1_kp.pub);
        dcrypt_key_unref_private(&test_v2_kp.priv);
        dcrypt_key_unref_public(&test_v2_kp.pub);
 }
 
-int main(void) {
+int main(void)
+{
        struct dcrypt_settings set = {
                .module_dir = ".libs"
        };
        const char *error;
 
        if (!dcrypt_initialize(NULL, &set, &error)) {
-               i_error("No functional dcrypt backend found - skipping tests: %s", error);
+               i_error("No functional dcrypt backend found - "
+                       "skipping tests: %s", error);
                return 0;
        }
 
-       test_assert(dcrypt_key_load_private(&test_v1_kp.priv, key_v1_priv, NULL, NULL, NULL));
+       test_assert(dcrypt_key_load_private(&test_v1_kp.priv, key_v1_priv,
+                                           NULL, NULL, NULL));
        test_assert(dcrypt_key_load_public(&test_v1_kp.pub, key_v1_pub, NULL));
-       test_assert(dcrypt_key_load_private(&test_v2_kp.priv, key_v2_priv, NULL, NULL, NULL));
+       test_assert(dcrypt_key_load_private(&test_v2_kp.priv, key_v2_priv,
+                                           NULL, NULL, NULL));
        test_assert(dcrypt_key_load_public(&test_v2_kp.pub, key_v2_pub, NULL));
 
        static void (*const test_functions[])(void) = {