--- a/test/crypto.c Thu Sep 06 12:51:37 2018 +0200 +++ b/test/crypto.c Sat Sep 15 11:56:36 2018 +0200 @@ -1,7 +1,7 @@ /* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * - * Copyright 2017 Olaf Wintermann. All rights reserved. + * Copyright 2018 Olaf Wintermann. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -247,6 +247,34 @@ UCX_TEST_END; } +UCX_TEST(test_crypto_buffer) { + 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++) { + UcxBuffer *content = ucx_buffer_new(NULL, 256, UCX_BUFFER_AUTOEXTEND); + ucx_buffer_puts(content, strings[j]); + content->pos = 0; + + UcxBuffer *enc = aes_encrypt_buffer(content, key); + UCX_TEST_ASSERT(enc->size >= content->size + 16, "aes_encrypt_buffer failed"); + + UcxBuffer *dec = aes_decrypt_buffer(enc, key); + UCX_TEST_ASSERT(dec->size == content->size, "aes_decrypt_buffer failed"); + + UCX_TEST_ASSERT(!memcmp(content->space, dec->space, dec->size), "decrypted buffer has wrong content"); + + ucx_buffer_free(content); + ucx_buffer_free(enc); + ucx_buffer_free(dec); + } + } + + 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); @@ -283,6 +311,13 @@ 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"); + + data->pos = 0; + UcxBuffer *enc2 = aes_encrypt_buffer(data, key); + UcxBuffer *dec2 = aes_decrypt_buffer(enc2, key); + + UCX_TEST_ASSERT(dec2->size == data->size, "dec2 has wrong size"); + UCX_TEST_ASSERT(!memcmp(strings[j], dec2->space, dec2->size), "dec2 has wrong content"); } }