diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 2febd81911109dffc12e1e57ba4b27e4b0de45e7..9e312e7b1d3d6d7f35b903ecf965bcaf2ebf1b43 100755 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,6 +1,8 @@ cmake_minimum_required(VERSION 3.0) - project(test) + +set(CMAKE_C_STANDARD 11) + add_subdirectory(libdap-test) add_subdirectory(core) add_subdirectory(crypto) diff --git a/test/crypto/dap_enc_aes_test.c b/test/crypto/dap_enc_aes_test.c index 87a452d7bdd29741000c9c85d8e380a9c031feb6..b18e7842179ab066035c8d075b87c3d43c712316 100644 --- a/test/crypto/dap_enc_aes_test.c +++ b/test/crypto/dap_enc_aes_test.c @@ -1,33 +1,59 @@ #include "dap_enc_aes_test.h" -static const size_t BYTE_SIZE = 256; +static const int BYTE_SIZE = 256; -void generate_random_byte_array(uint8_t* array, int size){ - srand(time(NULL)); - for(int i=0 ; i< size; i++){ - array[i] = rand()%BYTE_SIZE; +void generate_random_byte_array(uint8_t* array, const size_t size) { + for(size_t i = 0; i < size; i++) { + array[i] = (uint8_t)rand() % BYTE_SIZE; } } -void test_encode_decode(int source_size){ +void test_encode_decode(const size_t source_size) { dap_enc_key_t* key = dap_enc_key_new_generate(DAP_ENC_KEY_TYPE_AES, 0); - uint8_t* source = (uint8_t*)malloc(source_size); - uint8_t* encrypted = (uint8_t*)malloc(source_size+16); - uint8_t* result = (uint8_t*)malloc(source_size+16); - generate_random_byte_array(source,source_size); - size_t encrypted_size = dap_enc_aes_encode(key,source,source_size,encrypted); - size_t result_size = dap_enc_aes_decode(key,encrypted,encrypted_size,result); - dap_assert(source_size == result_size,"Size error"); - dap_assert(memcmp(source,result,source_size) == 0,"Encryption error"); - free(source); - free(encrypted); - free(result); - dap_enc_key_delete(key); + + uint8_t source[source_size]; + uint8_t encrypted[source_size + AES_BLOCK_SIZE]; + uint8_t result[source_size + AES_BLOCK_SIZE]; + generate_random_byte_array(source, source_size); + + size_t encrypted_size = dap_enc_aes_encode(key, source, + source_size, encrypted); + + size_t result_size = dap_enc_aes_decode(key, encrypted, + encrypted_size, result); + + dap_assert_PIF(source_size == result_size, "Check result decode size"); + + dap_assert_PIF(memcmp(source,result,source_size) == 0, "Check source and encode->decode data"); + dap_enc_key_delete(key); } +//void test_encode_decode(int source_size){ +// dap_enc_key_t* key = dap_enc_key_new_generate(DAP_ENC_KEY_TYPE_AES, 0); +// uint8_t* source = (uint8_t*)malloc(source_size); +// uint8_t* encrypted = (uint8_t*)malloc(source_size+16); +// uint8_t* result = (uint8_t*)malloc(source_size+16); +// generate_random_byte_array(source,source_size); +// size_t encrypted_size = dap_enc_aes_encode(key,source,source_size,encrypted); +// size_t result_size = dap_enc_aes_decode(key,encrypted,encrypted_size,result); + +// if(source_size != result_size) { +// dap_test_msg("FAIL"); +// } + +// dap_assert(source_size == result_size,"Size error"); +// dap_assert(memcmp(source,result,source_size) == 0,"Encryption error"); +// free(source); +// free(encrypted); +// free(result); +// dap_enc_key_delete(key); +//} + + void init_test_case() { - dap_enc_key_init(); + srand((uint)time(NULL)); + dap_enc_key_init(); } void cleanup_test_case() { @@ -37,11 +63,12 @@ void cleanup_test_case() { void dap_enc_aes_tests_run() { dap_print_module_name("dap_enc_aes"); init_test_case(); - test_encode_decode(10); - test_encode_decode(100); - test_encode_decode(1000); - test_encode_decode(10000); - test_encode_decode(100000); - test_encode_decode(1000000); + + const size_t step = 3, count_steps = 1000; + + for(size_t i = 1; i <= count_steps; i++) { + //test_encode_decode(231); + test_encode_decode(step * i); + } cleanup_test_case(); -} \ No newline at end of file +} diff --git a/test/crypto/dap_enc_aes_test.h b/test/crypto/dap_enc_aes_test.h index fbf8eba3bc187ac19307668a0a05b2529d625f51..77e932fddec272882d91a7d9ce76c26bf5a6d4b4 100644 --- a/test/crypto/dap_enc_aes_test.h +++ b/test/crypto/dap_enc_aes_test.h @@ -2,11 +2,5 @@ #include "dap_enc_aes.h" #include "dap_enc_key.h" #include "dap_test.h" -#include "assert.h" -#include "stdbool.h" -#include "stdlib.h" -#include "time.h" -#include "stdio.h" -#include "string.h" -extern void dap_enc_aes_tests_run(void); \ No newline at end of file +extern void dap_enc_aes_tests_run(void);