# HG changeset patch # User Olaf Wintermann # Date 1505659005 -7200 # Node ID 82275f589d8dc3a89a69b33154330b92ce3a4f53 # Parent a4419589ea26a70650459915a2b08e9f845bdafb adds test for crypto streams diff -r a4419589ea26 -r 82275f589d8d test/crypto.c --- a/test/crypto.c Sun Sep 17 11:22:16 2017 +0200 +++ b/test/crypto.c Sun Sep 17 16:36:45 2017 +0200 @@ -35,7 +35,9 @@ #include #include +#include #include +#include static DavKey keys256[16]; static DavKey keys128[16]; @@ -244,3 +246,45 @@ UCX_TEST_END; } + +UCX_TEST(test_crypto_stream) { + UcxBuffer *data = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND); + UcxBuffer *cbuf = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND); + UcxBuffer *pbuf = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND); + + UCX_TEST_BEGIN; + + for(int i=0;i<32;i++) { + DavKey *key = i < 16 ? &keys256[i] : &keys128[i%16]; + for(int j=0;j<20;j++) { + data->pos = 0; + data->size = 0; + size_t slen = strlen(strings[j]); + ucx_buffer_write(strings[j], 1, slen, data); + ucx_buffer_seek(data, 0, SEEK_SET); + + cbuf->pos = 0; + cbuf->size = 0; + pbuf->pos = 0; + pbuf->size = 0; + + AESEncrypter *enc = aes_encrypter_new(key, data, (dav_read_func)ucx_buffer_read); + char buf[1024]; + size_t r = 0; + while((r = aes_read(buf, 1, 1024, enc)) != 0) { + ucx_buffer_write(buf, 1, r, cbuf); + } + aes_encrypter_close(enc); + + AESDecrypter *dec = aes_decrypter_new(key, pbuf, (dav_write_func)ucx_buffer_write); + aes_write(cbuf->space, 1, cbuf->pos, dec); + aes_decrypter_shutdown(dec); + aes_decrypter_close(dec); + + UCX_TEST_ASSERT(slen == pbuf->pos, "wrong length after enc-dec"); + UCX_TEST_ASSERT(!memcmp(strings[j], pbuf->space, slen), "wrong content after enc-dec"); + } + } + + UCX_TEST_END; +} diff -r a4419589ea26 -r 82275f589d8d test/crypto.h --- a/test/crypto.h Sun Sep 17 11:22:16 2017 +0200 +++ b/test/crypto.h Sun Sep 17 16:36:45 2017 +0200 @@ -38,6 +38,8 @@ UCX_TEST(test_util_decrypt_str_k); UCX_TEST(test_util_encrypt_str_k); +UCX_TEST(test_crypto_stream); + #ifdef __cplusplus } diff -r a4419589ea26 -r 82275f589d8d test/main.c --- a/test/main.c Sun Sep 17 11:22:16 2017 +0200 +++ b/test/main.c Sun Sep 17 16:36:45 2017 +0200 @@ -45,6 +45,7 @@ ucx_test_register(suite, test_util_base64encode); ucx_test_register(suite, test_util_decrypt_str_k); ucx_test_register(suite, test_util_encrypt_str_k); + ucx_test_register(suite, test_crypto_stream); ucx_test_run(suite, stdout); fflush(stdout); diff -r a4419589ea26 -r 82275f589d8d ucx/buffer.c --- a/ucx/buffer.c Sun Sep 17 11:22:16 2017 +0200 +++ b/ucx/buffer.c Sun Sep 17 16:36:45 2017 +0200 @@ -150,6 +150,7 @@ size_t ucx_buffer_write(const void *ptr, size_t size, size_t nitems, UcxBuffer *buffer) { size_t len = size * nitems; + const char *string = ptr; size_t required = buffer->pos + len; if (buffer->pos > required) { return 0;