test/crypto.c

changeset 308
82275f589d8d
parent 305
c3d98b2cccf3
child 470
6bf798ad3aec
child 481
ff477f1f7765
--- 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;
+}

mercurial