test-stream.c revision b671974c5aafc99ff3dc9c6718aff4260aee4ba3
8c294c1cd4d721818a59684cf7f2b36123f79163Stephen Gallagher#include "lib.h"
8c294c1cd4d721818a59684cf7f2b36123f79163Stephen Gallagher#include "buffer.h"
8c294c1cd4d721818a59684cf7f2b36123f79163Stephen Gallagher#include "str.h"
8c294c1cd4d721818a59684cf7f2b36123f79163Stephen Gallagher#include "dcrypt.h"
8c294c1cd4d721818a59684cf7f2b36123f79163Stephen Gallagher#include "ostream.h"
c252d148fa8ab50aaaa8bbae7beb4d208025171dNikolai Kondrashov#include "ostream-encrypt.h"
9542512d7be40f2000298c86d3d2b728f4f0f65aStephen Gallagher#include "istream.h"
9542512d7be40f2000298c86d3d2b728f4f0f65aStephen Gallagher#include "istream-decrypt.h"
9542512d7be40f2000298c86d3d2b728f4f0f65aStephen Gallagher#include "istream-hash.h"
c6e39e15178675d0779e0ae855245774a09b4eb5Nikolai Kondrashov#include "istream-base64.h"
c6e39e15178675d0779e0ae855245774a09b4eb5Nikolai Kondrashov#include "randgen.h"
c6e39e15178675d0779e0ae855245774a09b4eb5Nikolai Kondrashov#include "hash-method.h"
c6e39e15178675d0779e0ae855245774a09b4eb5Nikolai Kondrashov#include "test-common.h"
c6e39e15178675d0779e0ae855245774a09b4eb5Nikolai Kondrashov#include "hex-binary.h"
fd5a4eacd56700ffb08a73121aeacdc806cb0132Sumit Bose#include <fcntl.h>
8b1f525acd20f36c836e827de3c251088961c5d9Stephen Gallagher#include <sys/stat.h>
8b1f525acd20f36c836e827de3c251088961c5d9Stephen Gallagher#include <stdio.h>
8b1f525acd20f36c836e827de3c251088961c5d9Stephen Gallagher
8b1f525acd20f36c836e827de3c251088961c5d9Stephen Gallagherstatic const char key_v1_priv[] = "-----BEGIN PRIVATE KEY-----\n" \
8b1f525acd20f36c836e827de3c251088961c5d9Stephen Gallagher"MIGpAgEAMBAGByqGSM49AgEGBSuBBAAjBIGRMIGOAgEBBEGz2V2VMi/5s+Z+GJh7\n" \
84ae5edab16ad6be5e3be956cb6fa031c1428eb5Stephen Gallagher"4WfqZjZUpqqm+NJWojm6BbrZMY+9ZComlTGVcUZ007acFxV93oMmrfmtRUb5ynrb\n" \
428db8a58c0c149d5efccc6d788f70916c1d34d7Jakub Hrozek"MRFskKFGA0QAAwHrAJc8TvyPzspOoz6UH1C1YRmaUVm8tsLu2d0dYtZeOKJUl52J\n" \
428db8a58c0c149d5efccc6d788f70916c1d34d7Jakub Hrozek"4o8MKIg+ce4q0mTNFrhj+glKj29ppWti6JGAQA==\n" \
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher"-----END PRIVATE KEY-----";
df4cc3a83c5d6700b6a09ff96cb4a6b1949b1aa9Stephen Gallagher
df4cc3a83c5d6700b6a09ff96cb4a6b1949b1aa9Stephen Gallagherstatic const char key_v1_pub[] = "-----BEGIN PUBLIC KEY-----\n" \
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher"MFgwEAYHKoZIzj0CAQYFK4EEACMDRAADAesAlzxO/I/Oyk6jPpQfULVhGZpRWby2\n" \
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher"wu7Z3R1i1l44olSXnYnijwwoiD5x7irSZM0WuGP6CUqPb2mla2LokYBA\n" \
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher"-----END PUBLIC KEY-----";
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagherstatic const char key_v2_priv[] = "-----BEGIN PRIVATE KEY-----\n" \
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher"MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgtuQJA+uboZWVwgHc\n" \
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher"DciyVdrovAPwlMqshDK3s78IDDuhRANCAAQm0VEdzLB9PtD0HA8JK1zifWnj8M00\n" \
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher"FQzedfp9SQsWyA8dzs5/NFR5MTe6Xbh/ndKEs1zZH3vZ4FlNrilZc0st\n" \
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher"-----END PRIVATE KEY-----";
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagherstatic const char key_v2_pub[] = "-----BEGIN PUBLIC KEY-----\n" \
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEJtFRHcywfT7Q9BwPCStc4n1p4/DN\n" \
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher"NBUM3nX6fUkLFsgPHc7OfzRUeTE3ul24f53ShLNc2R972eBZTa4pWXNLLQ==\n" \
8a5e793a0576250da80371e53aa3e7eba15cdb63Sumit Bose"-----END PUBLIC KEY-----";
8a5e793a0576250da80371e53aa3e7eba15cdb63Sumit Bose
8a5e793a0576250da80371e53aa3e7eba15cdb63Sumit Bosestatic const char test_sample_v1_hash[] = "1d7cc2cc1f1983f76241cc42389911e88590ad58cf9d54cafeb5b198d3723dd1";
90fd1bbd6035cdab46faa3a695a2fb2be6508b17Sumit Bosestatic const char test_sample_v1_short_hash[] = "b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c";
90fd1bbd6035cdab46faa3a695a2fb2be6508b17Sumit Bosestatic const char test_sample_v2_hash[] = "2e31218656dd34db65b321688bf418dee4ee785e99eb9c21e0d29b4af27a863e";
90fd1bbd6035cdab46faa3a695a2fb2be6508b17Sumit Bose
af4ffe1001adcc0a96897e426d26444f07af9aa1Benjamin Franzkestatic struct dcrypt_keypair test_v1_kp;
af4ffe1001adcc0a96897e426d26444f07af9aa1Benjamin Franzkestatic struct dcrypt_keypair test_v2_kp;
af4ffe1001adcc0a96897e426d26444f07af9aa1Benjamin Franzke
f3c85d900c4663854cc7bbae7d9f77867ed1f69bSumit Bosestatic
f3c85d900c4663854cc7bbae7d9f77867ed1f69bSumit Bosevoid test_static_v1_input(void)
f3c85d900c4663854cc7bbae7d9f77867ed1f69bSumit Bose{
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher ssize_t siz;
2a552e43581c74f51205c7141ec9f6e9542509f8Stephen Gallagher const struct hash_method *hash = hash_method_lookup("sha256");
2a552e43581c74f51205c7141ec9f6e9542509f8Stephen Gallagher unsigned char hash_ctx[hash->context_size];
8214510f125879c3b1d247f2ce981ee20b5375d1Jakub Hrozek unsigned char hash_dgst[hash->digest_size];
1a59af8245f183f22d87d067a90197d8e2ea958dJakub Hrozek hash->init(hash_ctx);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_begin("test_static_v1_input");
d921c1eba437662437847279f251a0a5d8f70127Maxim
2cbdd12983eb85eddb90f64cfafb24eae5b448f4Jakub Hrozek struct istream *is_1 = i_stream_create_file(DCRYPT_SRC_DIR"/sample-v1.asc", IO_BLOCK_SIZE);
b9c8ce2bdd4045782c243605a1b999098bedcffcNoam Meltzer struct istream *is_2 = i_stream_create_base64_decoder(is_1);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher i_stream_unref(&is_1);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct istream *is_3 = i_stream_create_decrypt(is_2, test_v1_kp.priv);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher i_stream_unref(&is_2);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct istream *is_4 = i_stream_create_hash(is_3, hash, hash_ctx);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher i_stream_unref(&is_3);
eb2e21b764d03544d8161e9956d7f70b07b75f77Simo Sorce
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher while((siz = i_stream_read(is_4))>0) { i_stream_skip(is_4, siz); }
2a5790216f57e9bdfb2930d52860bb5300366536Jakub Hrozek
5377441d7a846461c2d9a7a870cea711360a529aNikolai Kondrashov if (is_4->stream_errno != 0)
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher i_debug("error: %s", i_stream_get_error(is_4));
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley test_assert(is_4->stream_errno == 0);
4dd615c01357b8715711aad6820ba9595d3ad377Stephen Gallagher
4b6a0d0b3d42e5fdb457f47d9adfa5e66b160256Stephen Gallagher i_stream_unref(&is_4);
e124844907ed6973915e4d56f5442ecd07535a12Jakub Hrozek
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher hash->result(hash_ctx, hash_dgst);
b32159300fea63222d8dd9200ed634087704ea74Stephen Gallagher
b32159300fea63222d8dd9200ed634087704ea74Stephen Gallagher test_assert(strcmp(test_sample_v1_hash, binary_to_hex(hash_dgst, sizeof(hash_dgst))) == 0);
87d3b47abba6a40fcf809c85a2b138bc1013d9c5Jakub Hrozek
bc13c352ba9c2877f1e9bc62e55ad60fc000a55dJakub Hrozek test_end();
bc13c352ba9c2877f1e9bc62e55ad60fc000a55dJakub Hrozek}
bc13c352ba9c2877f1e9bc62e55ad60fc000a55dJakub Hrozek
bc13c352ba9c2877f1e9bc62e55ad60fc000a55dJakub Hrozekstatic
bc13c352ba9c2877f1e9bc62e55ad60fc000a55dJakub Hrozekvoid test_static_v1_input_short(void)
bc13c352ba9c2877f1e9bc62e55ad60fc000a55dJakub Hrozek{
bc13c352ba9c2877f1e9bc62e55ad60fc000a55dJakub Hrozek ssize_t siz;
bc13c352ba9c2877f1e9bc62e55ad60fc000a55dJakub Hrozek const struct hash_method *hash = hash_method_lookup("sha256");
87d3b47abba6a40fcf809c85a2b138bc1013d9c5Jakub Hrozek unsigned char hash_ctx[hash->context_size];
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher unsigned char hash_dgst[hash->digest_size];
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher hash->init(hash_ctx);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_begin("test_static_v1_input_short");
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct istream *is_1 = i_stream_create_file(DCRYPT_SRC_DIR"/sample-v1_short.asc", IO_BLOCK_SIZE);
054b5d4bb98973698f74d66b14ccd14394b53f10Lukas Slebodnik struct istream *is_2 = i_stream_create_base64_decoder(is_1);
054b5d4bb98973698f74d66b14ccd14394b53f10Lukas Slebodnik i_stream_unref(&is_1);
a3d176d116ceccd6a7547c128fab5df5cdd2c2b6Michal Zidek struct istream *is_3 = i_stream_create_decrypt(is_2, test_v1_kp.priv);
a3d176d116ceccd6a7547c128fab5df5cdd2c2b6Michal Zidek i_stream_unref(&is_2);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct istream *is_4 = i_stream_create_hash(is_3, hash, hash_ctx);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher i_stream_unref(&is_3);
4f6931e854c698dcb1c09f99eb330ce2fb97e7c6Lukas Slebodnik
4dd615c01357b8715711aad6820ba9595d3ad377Stephen Gallagher while((siz = i_stream_read(is_4))>0) { i_stream_skip(is_4, siz); }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher if (is_4->stream_errno != 0)
558998ce664055a75595371118f818084d8f2b23Jan Cholasta i_debug("error: %s", i_stream_get_error(is_4));
558998ce664055a75595371118f818084d8f2b23Jan Cholasta
9a3e40dc49c1e38bf58e45be5adff37615f3910bJan Cholasta test_assert(is_4->stream_errno == 0);
9a3e40dc49c1e38bf58e45be5adff37615f3910bJan Cholasta
558998ce664055a75595371118f818084d8f2b23Jan Cholasta i_stream_unref(&is_4);
558998ce664055a75595371118f818084d8f2b23Jan Cholasta
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher hash->result(hash_ctx, hash_dgst);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_assert(strcmp(test_sample_v1_short_hash, binary_to_hex(hash_dgst, sizeof(hash_dgst))) == 0);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_end();
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher}
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagherstatic
c737e1444fb186e349e59bfa9dac4995b720b4b1Jan Zelenyvoid test_static_v2_input(void)
f1828234a850dd28465425248a83a993f262918fPavel Březina{
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay test_begin("test_static_v2_input");
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ssize_t amt;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher const struct hash_method *hash = hash_method_lookup("sha256");
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher unsigned char hash_ctx[hash->context_size];
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher unsigned char hash_dgst[hash->digest_size];
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher hash->init(hash_ctx);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct istream *is_1 = i_stream_create_file(DCRYPT_SRC_DIR"/sample-v2.asc", IO_BLOCK_SIZE);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct istream *is_2 = i_stream_create_base64_decoder(is_1);
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek i_stream_unref(&is_1);
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek struct istream *is_3 = i_stream_create_decrypt(is_2, test_v2_kp.priv);
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek i_stream_unref(&is_2);
2827b0d03f7b6bafa504d22a5d7ca39cbda048b3Pavel Březina struct istream *is_4 = i_stream_create_hash(is_3, hash, hash_ctx);
2827b0d03f7b6bafa504d22a5d7ca39cbda048b3Pavel Březina i_stream_unref(&is_3);
2827b0d03f7b6bafa504d22a5d7ca39cbda048b3Pavel Březina
9e80079370ff3b943832adc3c5ef430e64be0a0cJakub Hrozek while((amt = i_stream_read(is_4))>0) { i_stream_skip(is_4, amt); }
9e80079370ff3b943832adc3c5ef430e64be0a0cJakub Hrozek
9e80079370ff3b943832adc3c5ef430e64be0a0cJakub Hrozek if (is_4->stream_errno != 0)
e7311aec8d691e5427317442387af1bc8fff3742Jan Cholasta i_debug("error: %s", i_stream_get_error(is_4));
e7311aec8d691e5427317442387af1bc8fff3742Jan Cholasta
e7311aec8d691e5427317442387af1bc8fff3742Jan Cholasta test_assert(is_4->stream_errno == 0);
cb4d5b588e704114b7090678752d33512baa718eJakub Hrozek
cb4d5b588e704114b7090678752d33512baa718eJakub Hrozek i_stream_unref(&is_4);
cb4d5b588e704114b7090678752d33512baa718eJakub Hrozek
19d3aba12c70528708be9440aca66038a291f29eYassir Elley hash->result(hash_ctx, hash_dgst);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley test_assert(strcmp(test_sample_v2_hash, binary_to_hex(hash_dgst, sizeof(hash_dgst))) == 0);
f3a25949de81f80c136bb073e4a8f504b080c20cJakub Hrozek
f3a25949de81f80c136bb073e4a8f504b080c20cJakub Hrozek test_end();
f3a25949de81f80c136bb073e4a8f504b080c20cJakub Hrozek
cb4d5b588e704114b7090678752d33512baa718eJakub Hrozek/** this code is left here to show how the sample file is created
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct istream *is = i_stream_create_file("../lib-fts/udhr_fra.txt", 8192);
b9e5bd09a5ff7009537a18914dbebcf10498f592Sumit Bose struct istream *is_2 = i_stream_create_hash(is, hash, hash_ctx);
b9e5bd09a5ff7009537a18914dbebcf10498f592Sumit Bose int fd = open("sample-v2.bin", O_CREAT|O_TRUNC|O_WRONLY, S_IRWXU);
b9e5bd09a5ff7009537a18914dbebcf10498f592Sumit Bose struct ostream *os = o_stream_create_fd_file(fd, 0, TRUE);
b9e5bd09a5ff7009537a18914dbebcf10498f592Sumit Bose struct ostream *os_2 = o_stream_create_encrypt(os, "aes-256-gcm-sha256", test_v2_kp.pub, IO_STREAM_ENC_INTEGRITY_AEAD);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher const unsigned char *ptr;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher size_t siz;
36ccdecd053a9ad88dce86b8c84770dc2aa11d21Simo Sorce
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher while(i_stream_read_data(is_2, &ptr, &siz, 0)>0) {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher o_stream_nsend(os_2, ptr, siz);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher i_stream_skip(is_2, siz);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher i_assert(o_stream_nfinish(os_2)==0);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher o_stream_close(os_2);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher i_stream_close(is_2);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher hash->result(hash_ctx, hash_dgst);
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher printf("%s\n", binary_to_hex(hash_dgst, sizeof(hash_dgst)));
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher*/
69aaef8719c5cf33ed1c4090fa313ba281bf8a02Jakub Hrozek}
4dd615c01357b8715711aad6820ba9595d3ad377Stephen Gallagher
fe60346714a73ac3987f786731389320633dd245Pavel Březinastatic
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bosevoid test_write_read_v1(void)
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose{
b9d8c6172e48a2633ebe196b2e88bebdf9523c20Stef Walter test_begin("test_write_read_v1");
4bd20c075f0f187db0181dc53d00ab6cd47fdb4dJakub Hrozek unsigned char payload[IO_BLOCK_SIZE];
e5e8252ec48bfdd4e7529debc705c8e090264b9aSumit Bose const unsigned char *ptr;
71e7918be3ca5d38794a16a17f6b4f19a24d51fcPavel Březina size_t pos = 0, siz;
8359bf07a2e6c0181251ce8d5d9160dc57546c55Stephen Gallagher random_fill_weak(payload, IO_BLOCK_SIZE);
71e7918be3ca5d38794a16a17f6b4f19a24d51fcPavel Březina
71e7918be3ca5d38794a16a17f6b4f19a24d51fcPavel Březina buffer_t *buf = buffer_create_dynamic(default_pool, sizeof(payload));
bbaba8b3ef9bc101863b8687f234f4ee956caacdPavel Březina struct ostream *os = o_stream_create_buffer(buf);
80314a6f3ea8d81abe73d501d5b953a256cb2167Pavel Březina struct ostream *os_2 = o_stream_create_encrypt(os, "<unused>", test_v2_kp.pub, IO_STREAM_ENC_VERSION_1);
80314a6f3ea8d81abe73d501d5b953a256cb2167Pavel Březina o_stream_nsend(os_2, payload, sizeof(payload));
bbaba8b3ef9bc101863b8687f234f4ee956caacdPavel Březina
bbaba8b3ef9bc101863b8687f234f4ee956caacdPavel Březina if (os_2->stream_errno != 0)
80314a6f3ea8d81abe73d501d5b953a256cb2167Pavel Březina i_debug("error: %s", o_stream_get_error(os_2));
4bd20c075f0f187db0181dc53d00ab6cd47fdb4dJakub Hrozek
4bd20c075f0f187db0181dc53d00ab6cd47fdb4dJakub Hrozek test_assert(os_2->stream_errno == 0);
4bd20c075f0f187db0181dc53d00ab6cd47fdb4dJakub Hrozek test_assert(o_stream_nfinish(os_2) == 0);
4bd20c075f0f187db0181dc53d00ab6cd47fdb4dJakub Hrozek test_assert(os_2->stream_errno == 0);
4bd20c075f0f187db0181dc53d00ab6cd47fdb4dJakub Hrozek
4bd20c075f0f187db0181dc53d00ab6cd47fdb4dJakub Hrozek o_stream_unref(&os_2);
4bd20c075f0f187db0181dc53d00ab6cd47fdb4dJakub Hrozek
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct istream *is = test_istream_create_data(buf->data, buf->used);
150b76e13b7c4f3ccf1d709bf517ca2af6b2c9a2Jakub Hrozek struct istream *is_2 = i_stream_create_decrypt(is, test_v2_kp.priv);
150b76e13b7c4f3ccf1d709bf517ca2af6b2c9a2Jakub Hrozek
a65a64aee968bd2ac18156ced15a1e2509a8acbaAbhishek Singh size_t offset = 0;
ae6c1596225c65bec2a2dabff9eee4e3e0691181Abhishek Singh while(i_stream_read_data(is_2, &ptr, &siz, 0)>=0) {
2a9af1f71887f02935e2fb6ad5023afba5b6d43eSumit Bose if (offset == buf->used)
d00ffd2cb4e2f17c75b466178bb645b5c9317909Pallavi Jha test_istream_set_allow_eof(is, TRUE);
461da2984c747708e8badd27fa55ef879f40e712Pallavi Jha test_istream_set_size(is, offset);
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek
777374243e15c53e7b0a7345e190c1018920be18Jakub Hrozek test_assert_idx(pos + siz <= sizeof(payload), pos);
d064fef06dcbcb5f6c1be03e286b1a3433d6dfd7Sumit Bose if (pos + siz > sizeof(payload)) break;
e046ae03d0f55b1c8b0ec2fa6139bf86a3449adfPavel Březina test_assert_idx(memcmp(ptr, payload + pos, siz) == 0, pos);
939246537b0b9a4af6862c513d3919501ad57d92Sumit Bose i_stream_skip(is_2, siz);
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose }
1ce58f139699dd26b8888f4131c996263b6a80a5Jakub Hrozek
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek test_assert(is_2->stream_errno == 0);
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek i_stream_unref(&is);
0161a3c5637a0c0092bf54c436bb3d6508d7df26Jakub Hrozek i_stream_unref(&is_2);
f43c6a9ae2aea13b7a83fd932139f9352efbfcadPavel Březina buffer_free(&buf);
e592d5f157be869151983bd1b46d6f4f7a29daafJakub Hrozek
e592d5f157be869151983bd1b46d6f4f7a29daafJakub Hrozek test_end();
a524965fbe0551f1b3a68f1e5c7a5689a652998fSumit Bose}
e592d5f157be869151983bd1b46d6f4f7a29daafJakub Hrozek
f92ace4a52602e8c38a34f2392bec3deeac2ddddJakub Hrozekstatic
f92ace4a52602e8c38a34f2392bec3deeac2ddddJakub Hrozekvoid test_write_read_v1_short(void)
f92ace4a52602e8c38a34f2392bec3deeac2ddddJakub Hrozek{
f92ace4a52602e8c38a34f2392bec3deeac2ddddJakub Hrozek test_begin("test_write_read_v1_short");
f92ace4a52602e8c38a34f2392bec3deeac2ddddJakub Hrozek unsigned char payload[1];
a2e417f38c57ed87c956ddcecf4dafca93842b65Lukas Slebodnik const unsigned char *ptr;
99f8be128274eba264ea1434a7eb2800bced5902Lukas Slebodnik size_t pos = 0, siz;
99f8be128274eba264ea1434a7eb2800bced5902Lukas Slebodnik random_fill_weak(payload, 1);
99f8be128274eba264ea1434a7eb2800bced5902Lukas Slebodnik
a2e417f38c57ed87c956ddcecf4dafca93842b65Lukas Slebodnik buffer_t *buf = buffer_create_dynamic(default_pool, 64);
a2e417f38c57ed87c956ddcecf4dafca93842b65Lukas Slebodnik struct ostream *os = o_stream_create_buffer(buf);
f92ace4a52602e8c38a34f2392bec3deeac2ddddJakub Hrozek struct ostream *os_2 = o_stream_create_encrypt(os, "<unused>", test_v2_kp.pub, IO_STREAM_ENC_VERSION_1);
150b76e13b7c4f3ccf1d709bf517ca2af6b2c9a2Jakub Hrozek o_stream_nsend(os_2, payload, sizeof(payload));
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher if (os_2->stream_errno != 0)
f232789430a080384188d5da89b19d874cf17513Jakub Hrozek i_debug("error: %s", o_stream_get_error(os_2));
150b76e13b7c4f3ccf1d709bf517ca2af6b2c9a2Jakub Hrozek
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_assert(os_2->stream_errno == 0);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_assert(o_stream_nfinish(os_2) == 0);
bf5a808fa92007c325c3996e79694badfab201d4Stephen Gallagher test_assert(os_2->stream_errno == 0);
bf5a808fa92007c325c3996e79694badfab201d4Stephen Gallagher
bf5a808fa92007c325c3996e79694badfab201d4Stephen Gallagher o_stream_unref(&os_2);
fa551077410019fb34460dc730950e93b62b2963Jakub Hrozek
fa551077410019fb34460dc730950e93b62b2963Jakub Hrozek struct istream *is = test_istream_create_data(buf->data, buf->used);
fa551077410019fb34460dc730950e93b62b2963Jakub Hrozek struct istream *is_2 = i_stream_create_decrypt(is, test_v2_kp.priv);
bf5a808fa92007c325c3996e79694badfab201d4Stephen Gallagher
bf5a808fa92007c325c3996e79694badfab201d4Stephen Gallagher size_t offset = 0;
03a071399ab5fb58d4bb4fa38928413a21ae4d61Nikolai Kondrashov while(i_stream_read_data(is_2, &ptr, &siz, 0)>=0) {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher if (offset == buf->used)
bf5a808fa92007c325c3996e79694badfab201d4Stephen Gallagher test_istream_set_allow_eof(is, TRUE);
150b76e13b7c4f3ccf1d709bf517ca2af6b2c9a2Jakub Hrozek test_istream_set_size(is, offset);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_assert_idx(pos + siz <= sizeof(payload), pos);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher if (pos + siz > sizeof(payload)) break;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_assert_idx(memcmp(ptr, payload + pos, siz) == 0, pos);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher i_stream_skip(is_2, siz);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_assert(is_2->stream_errno == 0);
a2e417f38c57ed87c956ddcecf4dafca93842b65Lukas Slebodnik
a2e417f38c57ed87c956ddcecf4dafca93842b65Lukas Slebodnik i_stream_unref(&is);
a2e417f38c57ed87c956ddcecf4dafca93842b65Lukas Slebodnik i_stream_unref(&is_2);
a2e417f38c57ed87c956ddcecf4dafca93842b65Lukas Slebodnik buffer_free(&buf);
a2e417f38c57ed87c956ddcecf4dafca93842b65Lukas Slebodnik
a2e417f38c57ed87c956ddcecf4dafca93842b65Lukas Slebodnik test_end();
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher}
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagherstatic
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallaghervoid test_write_read_v1_empty(void)
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher{
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher const unsigned char *ptr;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher size_t siz;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_begin("test_write_read_v1_empty");
8a5e793a0576250da80371e53aa3e7eba15cdb63Sumit Bose buffer_t *buf = buffer_create_dynamic(default_pool, 64);
8a5e793a0576250da80371e53aa3e7eba15cdb63Sumit Bose struct ostream *os = o_stream_create_buffer(buf);
8a5e793a0576250da80371e53aa3e7eba15cdb63Sumit Bose struct ostream *os_2 = o_stream_create_encrypt(os, "<unused>", test_v1_kp.pub, IO_STREAM_ENC_VERSION_1);
8a5e793a0576250da80371e53aa3e7eba15cdb63Sumit Bose test_assert(o_stream_nfinish(os_2) == 0);
8a5e793a0576250da80371e53aa3e7eba15cdb63Sumit Bose if (os_2->stream_errno != 0)
90fd1bbd6035cdab46faa3a695a2fb2be6508b17Sumit Bose i_debug("error: %s", o_stream_get_error(os_2));
90fd1bbd6035cdab46faa3a695a2fb2be6508b17Sumit Bose
90fd1bbd6035cdab46faa3a695a2fb2be6508b17Sumit Bose o_stream_unref(&os_2);
90fd1bbd6035cdab46faa3a695a2fb2be6508b17Sumit Bose /* this should've been enough */
90fd1bbd6035cdab46faa3a695a2fb2be6508b17Sumit Bose
af4ffe1001adcc0a96897e426d26444f07af9aa1Benjamin Franzke struct istream *is = test_istream_create_data(buf->data, buf->used);
af4ffe1001adcc0a96897e426d26444f07af9aa1Benjamin Franzke struct istream *is_2 = i_stream_create_decrypt(is, test_v1_kp.priv);
af4ffe1001adcc0a96897e426d26444f07af9aa1Benjamin Franzke
af4ffe1001adcc0a96897e426d26444f07af9aa1Benjamin Franzke /* read should not fail */
af4ffe1001adcc0a96897e426d26444f07af9aa1Benjamin Franzke size_t offset = 0;
96453f402831275a39d5fb89c33c9776e148d03fStephen Gallagher ssize_t ret;
96453f402831275a39d5fb89c33c9776e148d03fStephen Gallagher while ((ret = i_stream_read_data(is_2, &ptr, &siz, 0)) >= 0) {
96453f402831275a39d5fb89c33c9776e148d03fStephen Gallagher test_assert(ret == 0);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher if (offset == buf->used)
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_istream_set_allow_eof(is, TRUE);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_istream_set_size(is, offset);
c7919a4fe41133cc466aa3d9431bfceee5784e7bJan Cholasta };
b35f20cd8ecdc8308a3201e55752fb0443ec6ae4Jan Cholasta
c7919a4fe41133cc466aa3d9431bfceee5784e7bJan Cholasta test_assert(is_2->stream_errno == 0);
69aaef8719c5cf33ed1c4090fa313ba281bf8a02Jakub Hrozek if (is_2->stream_errno != 0)
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek i_debug("error: %s", i_stream_get_error(is_2));
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher i_stream_unref(&is);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher i_stream_unref(&is_2);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher buffer_free(&buf);
c7919a4fe41133cc466aa3d9431bfceee5784e7bJan Cholasta test_end();
b35f20cd8ecdc8308a3201e55752fb0443ec6ae4Jan Cholasta}
c7919a4fe41133cc466aa3d9431bfceee5784e7bJan Cholasta
69aaef8719c5cf33ed1c4090fa313ba281bf8a02Jakub Hrozekstatic
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallaghervoid test_write_read_v2(void)
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher{
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_begin("test_write_read_v2");
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher unsigned char payload[IO_BLOCK_SIZE];
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher const unsigned char *ptr;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher size_t pos = 0, siz;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher random_fill_weak(payload, IO_BLOCK_SIZE);
3b1df539835367cb81cd5ff0f9959947d5642e55Stephen Gallagher
3b1df539835367cb81cd5ff0f9959947d5642e55Stephen Gallagher buffer_t *buf = buffer_create_dynamic(default_pool, sizeof(payload));
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct ostream *os = o_stream_create_buffer(buf);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct ostream *os_2 = o_stream_create_encrypt(os, "aes-256-gcm-sha256", test_v1_kp.pub, IO_STREAM_ENC_INTEGRITY_AEAD);
96453f402831275a39d5fb89c33c9776e148d03fStephen Gallagher o_stream_nsend(os_2, payload, sizeof(payload));
96453f402831275a39d5fb89c33c9776e148d03fStephen Gallagher test_assert(o_stream_nfinish(os_2) == 0);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher if (os_2->stream_errno != 0)
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher i_debug("error: %s", o_stream_get_error(os_2));
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
667db40da4db362d7ca0a1f7f1c4ba40fb71795aJakub Hrozek o_stream_unref(&os_2);
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose struct istream *is = test_istream_create_data(buf->data, buf->used);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose struct istream *is_2 = i_stream_create_decrypt(is, test_v1_kp.priv);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher size_t offset = 0;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher while(i_stream_read_data(is_2, &ptr, &siz, 0)>=0) {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher if (offset == buf->used)
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_istream_set_allow_eof(is, TRUE);
1467daed400d6c186bd0c99c057c42e764309ff3Stephen Gallagher test_istream_set_size(is, offset);
1467daed400d6c186bd0c99c057c42e764309ff3Stephen Gallagher
15b266d9f14dad26da8678a79019749d0f69532eStephen Gallagher test_assert_idx(pos + siz <= sizeof(payload), pos);
1467daed400d6c186bd0c99c057c42e764309ff3Stephen Gallagher if (pos + siz > sizeof(payload)) break;
b97595ae059c69b1960a6e7e56d74660388a683bJan Zeleny test_assert_idx(memcmp(ptr, payload + pos, siz) == 0, pos);
6a6a821866091e0f722808566c25b951aa346d7cStephen Gallagher i_stream_skip(is_2, siz);
48d7840cae22c5ff4d786149b0d8ecee7efb8306Lukas Slebodnik }
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashov
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashov test_assert(is_2->stream_errno == 0);
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashov if (is_2->stream_errno != 0)
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashov i_debug("error: %s", i_stream_get_error(is_2));
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashov
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose i_stream_unref(&is);
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose i_stream_unref(&is_2);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher buffer_free(&buf);
84ae5edab16ad6be5e3be956cb6fa031c1428eb5Stephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_end();
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher}
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagherstatic
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallaghervoid test_write_read_v2_short(void)
17f08cbd0f909181536b93d6c12c7cd69995f09eSumit Bose{
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashov test_begin("test_write_read_v2_short");
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashov unsigned char payload[1];
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashov const unsigned char *ptr;
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashov size_t pos = 0, siz;
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashov random_fill_weak(payload, 1);
6398f22526303343193a18e514602f1af6fb29cbNikolai Kondrashov
6398f22526303343193a18e514602f1af6fb29cbNikolai Kondrashov buffer_t *buf = buffer_create_dynamic(default_pool, 64);
6398f22526303343193a18e514602f1af6fb29cbNikolai Kondrashov struct ostream *os = o_stream_create_buffer(buf);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct ostream *os_2 = o_stream_create_encrypt(os, "aes-256-gcm-sha256", test_v1_kp.pub, IO_STREAM_ENC_INTEGRITY_AEAD);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher o_stream_nsend(os_2, payload, sizeof(payload));
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_assert(o_stream_nfinish(os_2) == 0);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher if (os_2->stream_errno != 0)
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher i_debug("error: %s", o_stream_get_error(os_2));
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher o_stream_unref(&os_2);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct istream *is = test_istream_create_data(buf->data, buf->used);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct istream *is_2 = i_stream_create_decrypt(is, test_v1_kp.priv);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher size_t offset = 0;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher while(i_stream_read_data(is_2, &ptr, &siz, 0)>=0) {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher if (offset == buf->used)
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_istream_set_allow_eof(is, TRUE);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_istream_set_size(is, offset);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_assert_idx(pos + siz <= sizeof(payload), pos);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher if (pos + siz > sizeof(payload)) break;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_assert_idx(memcmp(ptr, payload + pos, siz) == 0, pos);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher i_stream_skip(is_2, siz);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_assert(is_2->stream_errno == 0);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher if (is_2->stream_errno != 0)
bfbf5cb0f00c60c0f000f56c282377b13b9a89abSumit Bose i_debug("error: %s", i_stream_get_error(is_2));
b32159300fea63222d8dd9200ed634087704ea74Stephen Gallagher
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek i_stream_unref(&is);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher i_stream_unref(&is_2);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher buffer_free(&buf);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_end();
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher}
eb2e21b764d03544d8161e9956d7f70b07b75f77Simo Sorce
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagherstatic
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallaghervoid test_write_read_v2_empty(void)
b9e5bd09a5ff7009537a18914dbebcf10498f592Sumit Bose{
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher const unsigned char *ptr;
0ef783e186ef1c9f60e61a4e8e54c44cb366fdfePavel Březina size_t siz;
2cbdd12983eb85eddb90f64cfafb24eae5b448f4Jakub Hrozek test_begin("test_write_read_v2_empty");
e7311aec8d691e5427317442387af1bc8fff3742Jan Cholasta buffer_t *buf = buffer_create_dynamic(default_pool, 64);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct ostream *os = o_stream_create_buffer(buf);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct ostream *os_2 = o_stream_create_encrypt(os, "aes-256-gcm-sha256", test_v1_kp.pub, IO_STREAM_ENC_INTEGRITY_AEAD);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_assert(o_stream_nfinish(os_2) == 0);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher if (os_2->stream_errno != 0)
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher i_debug("error: %s", o_stream_get_error(os_2));
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher o_stream_unref(&os_2);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher /* this should've been enough */
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny struct istream *is = test_istream_create_data(buf->data, buf->used);
769347ad4d35d43488eb98f980143495b0db415dStef Walter struct istream *is_2 = i_stream_create_decrypt(is, test_v1_kp.priv);
115de6d50f0d0bdd5745a5d8eb0d067be9128528Sumit Bose
769347ad4d35d43488eb98f980143495b0db415dStef Walter /* read should not fail */
769347ad4d35d43488eb98f980143495b0db415dStef Walter size_t offset = 0;
769347ad4d35d43488eb98f980143495b0db415dStef Walter ssize_t ret;
769347ad4d35d43488eb98f980143495b0db415dStef Walter while ((ret = i_stream_read_data(is_2, &ptr, &siz, 0)) >= 0) {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_assert(ret == 0);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher if (offset == buf->used)
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_istream_set_allow_eof(is, TRUE);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_istream_set_size(is, offset);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher };
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
db78f4c750943fcd4b60bca5f3fdfd6cc5d3d4f8Ondrej Kos test_assert(is_2->stream_errno == 0);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher if (is_2->stream_errno != 0)
543676afec3c08fdc0a5a794976adc8dfdca974bJakub Hrozek i_debug("error: %s", i_stream_get_error(is_2));
543676afec3c08fdc0a5a794976adc8dfdca974bJakub Hrozek i_stream_unref(&is);
543676afec3c08fdc0a5a794976adc8dfdca974bJakub Hrozek i_stream_unref(&is_2);
543676afec3c08fdc0a5a794976adc8dfdca974bJakub Hrozek buffer_free(&buf);
543676afec3c08fdc0a5a794976adc8dfdca974bJakub Hrozek test_end();
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher}
ca261795ce61c41d7e62217ccb2ee913923040ffPavel Březina
ca261795ce61c41d7e62217ccb2ee913923040ffPavel Březinastatic int no_op_cb(const char *digest ATTR_UNUSED,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct dcrypt_private_key **priv_key_r ATTR_UNUSED,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher const char **error_r ATTR_UNUSED,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher void *context ATTR_UNUSED)
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher{
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher return 0;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher}
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagherstatic void test_read_0_to_400_byte_garbage(void)
77d165f0629966db65753a3aee84a8b4971673afPavel Březina{
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_begin("test_read_0_to_100_byte_garbage");
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher char data[512];
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher memset(data, 0, sizeof(data));
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher for (size_t s = 0; s <= 400; ++s) {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct istream *is = test_istream_create_data(data, s);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct istream *ds = i_stream_create_decrypt_callback(is,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher no_op_cb, NULL);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_istream_set_allow_eof(is, FALSE);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher ssize_t siz = 0;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher for (size_t offset = 0; offset <= s && siz == 0; offset++) {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher if (offset == s)
126c9338cf12a3e4404c36bbe4ec14b18f23537cMaxim test_istream_set_allow_eof(is, TRUE);
fe2091327ff44f80d6681c261494e4432404e9baStephen Gallagher test_istream_set_size(is, offset);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher siz = i_stream_read(ds);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_assert_idx(siz < 0, s);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher i_stream_unref(&ds);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher i_stream_unref(&is);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_end();
2e6087c6cc903d5164b9a1d5e3d791fd046001d9Jakub Hrozek}
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
126c9338cf12a3e4404c36bbe4ec14b18f23537cMaximstatic
fe2091327ff44f80d6681c261494e4432404e9baStephen Gallaghervoid test_free_keys() {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher dcrypt_key_unref_private(&test_v1_kp.priv);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher dcrypt_key_unref_public(&test_v1_kp.pub);
eaa723b4d06b4c1e588df67bef44a84bbfaebf1aLukas Slebodnik dcrypt_key_unref_private(&test_v2_kp.priv);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher dcrypt_key_unref_public(&test_v2_kp.pub);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher}
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagherint main(void) {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher if (!dcrypt_initialize(NULL, NULL, NULL)) {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher i_error("No functional dcrypt backend found - skipping tests");
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher return 0;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher random_init();
bfbf5cb0f00c60c0f000f56c282377b13b9a89abSumit Bose
fe2091327ff44f80d6681c261494e4432404e9baStephen Gallagher test_assert(dcrypt_key_load_private(&test_v1_kp.priv, key_v1_priv, NULL, NULL, NULL));
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_assert(dcrypt_key_load_public(&test_v1_kp.pub, key_v1_pub, NULL));
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_assert(dcrypt_key_load_private(&test_v2_kp.priv, key_v2_priv, NULL, NULL, NULL));
2e6087c6cc903d5164b9a1d5e3d791fd046001d9Jakub Hrozek test_assert(dcrypt_key_load_public(&test_v2_kp.pub, key_v2_pub, NULL));
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher static void (*test_functions[])(void) = {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_static_v1_input,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_static_v1_input_short,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_static_v2_input,
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek test_write_read_v1,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_write_read_v1_short,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_write_read_v1_empty,
3a4186ae40d0c3b7be46a4c973166f6048fcfe38Lukas Slebodnik test_write_read_v2,
8bcabb97d988d1602882a1f036aac2eaf5e09234Simo Sorce test_write_read_v2_short,
b9d8c6172e48a2633ebe196b2e88bebdf9523c20Stef Walter test_write_read_v2_empty,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_free_keys,
1658c567191c35beaddffafdb079abe33248037bLukas Slebodnik test_read_0_to_400_byte_garbage,
29be7d76c949b82350c7603cfd362a1fcb47eb1bJan Zeleny NULL
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher };
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher return test_run(test_functions);
823a5b3f4375f12b6edae4dd5169ee01771baebeJan Zeleny}
b32159300fea63222d8dd9200ed634087704ea74Stephen Gallagher