test-stream.c revision a62dad9ec88bb112079dd95be456d258c6c86369
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi#include "lib.h"
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi#include "buffer.h"
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi#include "str.h"
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi#include "dcrypt.h"
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi#include "ostream.h"
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi#include "ostream-encrypt.h"
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi#include "istream.h"
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi#include "istream-decrypt.h"
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi#include "istream-hash.h"
de1c645a79d42c37dffb7f52e9c643b6251fdcd0Aki Tuomi#include "istream-base64.h"
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi#include "iostream-temp.h"
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi#include "randgen.h"
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi#include "hash-method.h"
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi#include "test-common.h"
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi#include "hex-binary.h"
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi#include <fcntl.h>
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi#include <sys/stat.h>
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi#include <stdio.h>
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomistatic const char key_v1_priv[] = "-----BEGIN PRIVATE KEY-----\n" \
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi"MIGpAgEAMBAGByqGSM49AgEGBSuBBAAjBIGRMIGOAgEBBEGz2V2VMi/5s+Z+GJh7\n" \
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi"4WfqZjZUpqqm+NJWojm6BbrZMY+9ZComlTGVcUZ007acFxV93oMmrfmtRUb5ynrb\n" \
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi"MRFskKFGA0QAAwHrAJc8TvyPzspOoz6UH1C1YRmaUVm8tsLu2d0dYtZeOKJUl52J\n" \
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi"4o8MKIg+ce4q0mTNFrhj+glKj29ppWti6JGAQA==\n" \
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi"-----END PRIVATE KEY-----";
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomistatic const char key_v1_pub[] = "-----BEGIN PUBLIC KEY-----\n" \
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi"MFgwEAYHKoZIzj0CAQYFK4EEACMDRAADAesAlzxO/I/Oyk6jPpQfULVhGZpRWby2\n" \
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi"wu7Z3R1i1l44olSXnYnijwwoiD5x7irSZM0WuGP6CUqPb2mla2LokYBA\n" \
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi"-----END PUBLIC KEY-----";
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomistatic const char key_v2_priv[] = "-----BEGIN PRIVATE KEY-----\n" \
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi"MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgtuQJA+uboZWVwgHc\n" \
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi"DciyVdrovAPwlMqshDK3s78IDDuhRANCAAQm0VEdzLB9PtD0HA8JK1zifWnj8M00\n" \
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi"FQzedfp9SQsWyA8dzs5/NFR5MTe6Xbh/ndKEs1zZH3vZ4FlNrilZc0st\n" \
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi"-----END PRIVATE KEY-----";
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomistatic const char key_v2_pub[] = "-----BEGIN PUBLIC KEY-----\n" \
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEJtFRHcywfT7Q9BwPCStc4n1p4/DN\n" \
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi"NBUM3nX6fUkLFsgPHc7OfzRUeTE3ul24f53ShLNc2R972eBZTa4pWXNLLQ==\n" \
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi"-----END PUBLIC KEY-----";
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomistatic const char test_sample_v1_hash[] = "1d7cc2cc1f1983f76241cc42389911e88590ad58cf9d54cafeb5b198d3723dd1";
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomistatic const char test_sample_v2_hash[] = "2e31218656dd34db65b321688bf418dee4ee785e99eb9c21e0d29b4af27a863e";
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomistatic struct dcrypt_keypair test_v1_kp;
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomistatic struct dcrypt_keypair test_v2_kp;
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomistatic
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomivoid test_static_v1_input(void)
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi{
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi ssize_t siz;
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi const struct hash_method *hash = hash_method_lookup("sha256");
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi unsigned char hash_ctx[hash->context_size];
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi unsigned char hash_dgst[hash->digest_size];
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi hash->init(hash_ctx);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi test_begin("test_static_v1_input");
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
b0c472c7a93dfc869e2124ca738d62f1d6794e43Timo Sirainen struct istream *is_1 = i_stream_create_file(DCRYPT_SRC_DIR"/sample-v1.asc", IO_BLOCK_SIZE);
de1c645a79d42c37dffb7f52e9c643b6251fdcd0Aki Tuomi struct istream *is_2 = i_stream_create_base64_decoder(is_1);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi i_stream_unref(&is_1);
de1c645a79d42c37dffb7f52e9c643b6251fdcd0Aki Tuomi struct istream *is_3 = i_stream_create_decrypt(is_2, test_v1_kp.priv);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi i_stream_unref(&is_2);
de1c645a79d42c37dffb7f52e9c643b6251fdcd0Aki Tuomi struct istream *is_4 = i_stream_create_hash(is_3, hash, hash_ctx);
de1c645a79d42c37dffb7f52e9c643b6251fdcd0Aki Tuomi i_stream_unref(&is_3);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
de1c645a79d42c37dffb7f52e9c643b6251fdcd0Aki Tuomi while((siz = i_stream_read(is_4))>0) { i_stream_skip(is_4, siz); }
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
de1c645a79d42c37dffb7f52e9c643b6251fdcd0Aki Tuomi if (is_4->stream_errno != 0)
de1c645a79d42c37dffb7f52e9c643b6251fdcd0Aki Tuomi i_debug("error: %s", i_stream_get_error(is_4));
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
de1c645a79d42c37dffb7f52e9c643b6251fdcd0Aki Tuomi test_assert(is_4->stream_errno == 0);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
de1c645a79d42c37dffb7f52e9c643b6251fdcd0Aki Tuomi i_stream_unref(&is_4);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi hash->result(hash_ctx, hash_dgst);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi test_assert(strcmp(test_sample_v1_hash, binary_to_hex(hash_dgst, sizeof(hash_dgst))) == 0);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi test_end();
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi}
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomistatic
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomivoid test_static_v2_input(void)
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi{
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi test_begin("test_static_v2_input");
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi ssize_t amt;
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi const struct hash_method *hash = hash_method_lookup("sha256");
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi unsigned char hash_ctx[hash->context_size];
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi unsigned char hash_dgst[hash->digest_size];
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi hash->init(hash_ctx);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
b0c472c7a93dfc869e2124ca738d62f1d6794e43Timo Sirainen struct istream *is_1 = i_stream_create_file(DCRYPT_SRC_DIR"/sample-v2.asc", IO_BLOCK_SIZE);
de1c645a79d42c37dffb7f52e9c643b6251fdcd0Aki Tuomi struct istream *is_2 = i_stream_create_base64_decoder(is_1);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi i_stream_unref(&is_1);
de1c645a79d42c37dffb7f52e9c643b6251fdcd0Aki Tuomi struct istream *is_3 = i_stream_create_decrypt(is_2, test_v2_kp.priv);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi i_stream_unref(&is_2);
de1c645a79d42c37dffb7f52e9c643b6251fdcd0Aki Tuomi struct istream *is_4 = i_stream_create_hash(is_3, hash, hash_ctx);
de1c645a79d42c37dffb7f52e9c643b6251fdcd0Aki Tuomi i_stream_unref(&is_3);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
de1c645a79d42c37dffb7f52e9c643b6251fdcd0Aki Tuomi while((amt = i_stream_read(is_4))>0) { i_stream_skip(is_4, amt); }
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
de1c645a79d42c37dffb7f52e9c643b6251fdcd0Aki Tuomi if (is_4->stream_errno != 0)
de1c645a79d42c37dffb7f52e9c643b6251fdcd0Aki Tuomi i_debug("error: %s", i_stream_get_error(is_4));
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
de1c645a79d42c37dffb7f52e9c643b6251fdcd0Aki Tuomi test_assert(is_4->stream_errno == 0);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
de1c645a79d42c37dffb7f52e9c643b6251fdcd0Aki Tuomi i_stream_unref(&is_4);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi hash->result(hash_ctx, hash_dgst);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi test_assert(strcmp(test_sample_v2_hash, binary_to_hex(hash_dgst, sizeof(hash_dgst))) == 0);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi test_end();
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi/** this code is left here to show how the sample file is created
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi struct istream *is = i_stream_create_file("../lib-fts/udhr_fra.txt", 8192);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi struct istream *is_2 = i_stream_create_hash(is, hash, hash_ctx);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi int fd = open("sample-v2.bin", O_CREAT|O_TRUNC|O_WRONLY, S_IRWXU);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi struct ostream *os = o_stream_create_fd_file(fd, 0, TRUE);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi struct ostream *os_2 = o_stream_create_encrypt(os, "aes-256-gcm-sha256", test_v2_kp.pub, IO_STREAM_ENC_INTEGRITY_AEAD);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi const unsigned char *ptr;
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi size_t siz;
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi while(i_stream_read_data(is_2, &ptr, &siz, 0)>0) {
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi o_stream_nsend(os_2, ptr, siz);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi i_stream_skip(is_2, siz);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi }
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi i_assert(o_stream_nfinish(os_2)==0);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi o_stream_close(os_2);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi i_stream_close(is_2);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi hash->result(hash_ctx, hash_dgst);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi printf("%s\n", binary_to_hex(hash_dgst, sizeof(hash_dgst)));
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi*/
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi}
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomistatic
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomivoid test_write_read_v1(void)
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi{
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi test_begin("test_write_read_v1");
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi unsigned char payload[IO_BLOCK_SIZE];
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi const unsigned char *ptr;
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi size_t pos = 0, siz;
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi random_fill_weak(payload, IO_BLOCK_SIZE);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi struct ostream *os = iostream_temp_create("/tmp", 0);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi struct ostream *os_2 = o_stream_create_encrypt(os, "<unused>", test_v2_kp.pub, IO_STREAM_ENC_VERSION_1);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi o_stream_nsend(os_2, payload, sizeof(payload));
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi if (os_2->stream_errno != 0)
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi i_debug("error: %s", o_stream_get_error(os_2));
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi test_assert(os_2->stream_errno == 0);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi test_assert(o_stream_nfinish(os_2) == 0);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi test_assert(os_2->stream_errno == 0);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi o_stream_unref(&os_2);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi struct istream *is = iostream_temp_finish(&os, IO_BLOCK_SIZE);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi struct istream *is_2 = i_stream_create_decrypt(is, test_v2_kp.priv);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi i_stream_unref(&is);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi while(i_stream_read_data(is_2, &ptr, &siz, 0)>0) {
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi test_assert_idx(pos + siz <= sizeof(payload), pos);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi if (pos + siz > sizeof(payload)) break;
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi test_assert_idx(memcmp(ptr, payload + pos, siz) == 0, pos);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi i_stream_skip(is_2, siz);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi }
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi test_assert(is_2->stream_errno == 0);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi i_stream_unref(&is_2);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi test_end();
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi}
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomistatic
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomivoid test_write_read_v2(void)
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi{
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi test_begin("test_write_read_v2");
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi unsigned char payload[IO_BLOCK_SIZE];
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi const unsigned char *ptr;
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi size_t pos = 0, siz;
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi random_fill_weak(payload, IO_BLOCK_SIZE);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi struct ostream *os = iostream_temp_create("/tmp", 0);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi struct ostream *os_2 = o_stream_create_encrypt(os, "aes-256-gcm-sha256", test_v1_kp.pub, IO_STREAM_ENC_INTEGRITY_AEAD);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi o_stream_nsend(os_2, payload, IO_BLOCK_SIZE);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi test_assert(o_stream_nfinish(os_2) == 0);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi if (os_2->stream_errno != 0)
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi i_debug("error: %s", o_stream_get_error(os_2));
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi o_stream_unref(&os_2);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi struct istream *is = iostream_temp_finish(&os, IO_BLOCK_SIZE);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi struct istream *is_2 = i_stream_create_decrypt(is, test_v1_kp.priv);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi i_stream_unref(&is);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi while(i_stream_read_data(is_2, &ptr, &siz, 0)>0) {
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi test_assert_idx(pos + siz <= sizeof(payload), pos);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi if (pos + siz > sizeof(payload)) break;
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi test_assert_idx(memcmp(ptr, payload + pos, siz) == 0, pos);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi i_stream_skip(is_2, siz);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi }
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi test_assert(is_2->stream_errno == 0);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi if (is_2->stream_errno != 0)
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi i_debug("error: %s", i_stream_get_error(is_2));
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi i_stream_unref(&is_2);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi test_end();
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi}
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomiint main(void) {
a62dad9ec88bb112079dd95be456d258c6c86369Timo Sirainen dcrypt_initialize("openssl", NULL, NULL);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi random_init();
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi dcrypt_key_load_private(&test_v1_kp.priv, DCRYPT_FORMAT_PEM, key_v1_priv, NULL, NULL, NULL);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi dcrypt_key_load_public(&test_v1_kp.pub, DCRYPT_FORMAT_PEM, key_v1_pub, NULL);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi dcrypt_key_load_private(&test_v2_kp.priv, DCRYPT_FORMAT_PEM, key_v2_priv, NULL, NULL, NULL);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi dcrypt_key_load_public(&test_v2_kp.pub, DCRYPT_FORMAT_PEM, key_v2_pub, NULL);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi static void (*test_functions[])(void) = {
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi test_static_v1_input,
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi test_static_v2_input,
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi test_write_read_v1,
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi test_write_read_v2,
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi NULL
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi };
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi return test_run(test_functions);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi}