adds test for crypto streams

Sun, 17 Sep 2017 16:36:45 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 17 Sep 2017 16:36:45 +0200
changeset 308
82275f589d8d
parent 307
a4419589ea26
child 309
8a474d2ad209

adds test for crypto streams

test/crypto.c file | annotate | diff | comparison | revisions
test/crypto.h file | annotate | diff | comparison | revisions
test/main.c file | annotate | diff | comparison | revisions
ucx/buffer.c file | annotate | diff | comparison | revisions
--- 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 <ucx/string.h>
 #include <ucx/utils.h>
+#include <ucx/buffer.h>
 #include <libidav/utils.h>
+#include <libidav/crypto.h>
 
 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;
+}
--- 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
 }
--- 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);
--- 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;

mercurial