diff options
Diffstat (limited to 'c_implementation/tests')
-rw-r--r-- | c_implementation/tests/test_akfs.c | 307 | ||||
-rw-r--r-- | c_implementation/tests/test_akfs_mkdir.c | 42 | ||||
-rw-r--r-- | c_implementation/tests/test_aklog.c | 78 | ||||
-rw-r--r-- | c_implementation/tests/test_aklogcatter.c | 7 | ||||
-rw-r--r-- | c_implementation/tests/test_aklogwrite.c | 16 | ||||
-rw-r--r-- | c_implementation/tests/test_aksettings.c | 122 | ||||
-rw-r--r-- | c_implementation/tests/test_aksettings_read.c | 23 | ||||
-rw-r--r-- | c_implementation/tests/test_akutils.c | 7 | ||||
-rw-r--r-- | c_implementation/tests/test_sha512_string.c | 70 |
9 files changed, 672 insertions, 0 deletions
diff --git a/c_implementation/tests/test_akfs.c b/c_implementation/tests/test_akfs.c new file mode 100644 index 0000000..d08ea3b --- /dev/null +++ b/c_implementation/tests/test_akfs.c @@ -0,0 +1,307 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <libakfs.h> +#include <libaklog.h> + +static void test_correct_string_correct_length() +{ + const char *queried_string = "921618bc6d9f8059437c5e0397b13f973ab7c7a7b81f0ca31b70bf448fd800a460b67efda0020088bc97bf7d9da97a9e2ce7b20d46e066462ec44cf60284f9a7"; + // printf("Hash given:\t%s\n", queried_string); + // printf("Is a hash: %s\n", ak_fs_verify_input_is_hash(queried_string) ? "true": "false"); + sha512sum hash = {0}; + sha512sum *resulted_hash = &hash; + ak_fs_sha512sum_string_to_struct(queried_string, resulted_hash); + char resulted_string[129] = {0}; + ak_fs_sha512sum_struct_to_string(resulted_hash, resulted_string); + // printf("Hash returned:\t%s\n", resulted_string); + if ( strcmp(queried_string, resulted_string) == 0 ) + { + ak_log_test(__func__, "PASSED"); + } + else + { + ak_log_test(__func__, "FAILED"); + } +} + +static void test_bad_string_correct_length() +{ + const char *queried_string = "921618bc6d9f8059437c5e0397b13f973ab7c7a7b81f0ca31b70bf448fd800a460b67efda0020088bc97bf7d9da97a9e2ce7b20d46e066462ec44cf60284f9az"; + // printf("Hash given:\t%s\n", queried_string); + // printf("Is a hash: %s\n", ak_fs_verify_input_is_hash(queried_string) ? "true": "false"); + sha512sum hash = {0}; + sha512sum *resulted_hash = &hash; + ak_fs_sha512sum_string_to_struct(queried_string, resulted_hash); + char resulted_string[129] = {0}; + ak_fs_sha512sum_struct_to_string(resulted_hash, resulted_string); + // printf("Hash returned:\t%s\n", resulted_string); + if ( strcmp(queried_string, resulted_string) != 0 ) + { + ak_log_test(__func__, "PASSED"); + } + else + { + ak_log_test(__func__, "FAILED"); + } +} + +static void test_less_than_length() +{ + const char *queried_string = "921618bc6d9f8059437c5e0397b13f973ab7c7a7b81f0ca31b70bf"; + // printf("Hash given:\t%s\n", queried_string); + // printf("Is a hash: %s\n", ak_fs_verify_input_is_hash(queried_string) ? "true": "false"); + sha512sum hash = {0}; + sha512sum *resulted_hash = &hash; + ak_fs_sha512sum_string_to_struct(queried_string, resulted_hash); + char resulted_string[129] = {0}; + ak_fs_sha512sum_struct_to_string(resulted_hash, resulted_string); + // printf("Hash returned:\t%s\n", resulted_string); + if ( strcmp(queried_string, resulted_string) != 0 ) + { + ak_log_test(__func__, "PASSED"); + } + else + { + ak_log_test(__func__, "FAILED"); + } +} + +static void test_more_than_length() +{ + const char *queried_string = "921618bc6d9f8059437c5e0397b13f973ab7c7a7b81f0ca31b70bf448fd800a460b67efda0020088bc97bf7d9da97a9e2ce7b20d46e066462ec44cf60284f9a7aaa"; + // printf("Hash given:\t%s\n", queried_string); + // printf("Is a hash: %s\n", ak_fs_verify_input_is_hash(queried_string) ? "true": "false"); + sha512sum hash = {0}; + sha512sum *resulted_hash = &hash; + ak_fs_sha512sum_string_to_struct(queried_string, resulted_hash); + char resulted_string[129] = {0}; + ak_fs_sha512sum_struct_to_string(resulted_hash, resulted_string); + // printf("Hash returned:\t%s\n", resulted_string); + if ( strcmp(queried_string, resulted_string) != 0 ) + { + ak_log_test(__func__, "PASSED"); + } + else + { + ak_log_test(__func__, "FAILED"); + } +} + +static void test_string_is_empty() +{ + const char *queried_string = ""; + // printf("Hash given:\t%s\n", queried_string); + //printf("Is a hash: %s\n", ak_fs_verify_input_is_hash(queried_string) ? "true": "false"); + sha512sum hash = {0}; + sha512sum *resulted_hash = &hash; + ak_fs_sha512sum_string_to_struct(queried_string, resulted_hash); + char resulted_string[129] = {0}; + ak_fs_sha512sum_struct_to_string(resulted_hash, resulted_string); + // printf("Hash returned:\t%s\n", resulted_string); + if ( strcmp(queried_string, resulted_string) != 0 ) + { + ak_log_test(__func__, "PASSED"); + } + else + { + ak_log_test(__func__, "FAILED"); + } +} + +static void test_hash_path_test() +{ + const char *queried_string = "921618bc6d9f8059437c5e0397b13f973ab7c7a7b81f0ca31b70bf448fd800a460b67efda0020088bc97bf7d9da97a9e2ce7b20d46e066462ec44cf60284f9a7"; + // printf("Hash given:\t%s\n", queried_string); + // printf("Is a hash: %s\n", ak_fs_verify_input_is_hash(queried_string) ? "true": "false"); + const char *resulted_string = ak_fs_return_hash_path(queried_string); + // printf("Path returned:\t%s\n", resulted_string); + if ( strcmp(queried_string, resulted_string) != 0 ) + { + ak_log_test(__func__, "PASSED"); + } + else + { + ak_log_test(__func__, "FAILED"); + } +} + +static void test_hash_dir_test() +{ + const char *queried_string = "921618bc6d9f8059437c5e0397b13f973ab7c7a7b81f0ca31b70bf448fd800a460b67efda0020088bc97bf7d9da97a9e2ce7b20d46e066462ec44cf60284f9a7"; + // printf("Hash given:\t%s\n", queried_string); + // printf("Is a hash: %s\n", ak_fs_verify_input_is_hash(queried_string) ? "true": "false"); + const char *resulted_string = ak_fs_return_hash_dir(queried_string); + // printf("Path returned:\t%s\n", resulted_string); + if ( strcmp(queried_string, resulted_string) != 0 ) + { + ak_log_test(__func__, "PASSED"); + } + else + { + ak_log_test(__func__, "FAILED"); + } +} + +static void test_hash_save_to_file() +{ + const char *queried_string = "921618bc6d9f8059437c5e0397b13f973ab7c7a7b81f0ca31b70bf448fd800a460b67efda0020088bc97bf7d9da97a9e2ce7b20d46e066462ec44cf60284f9a7"; + // printf("Hash given:\t%s\n", queried_string); + // printf("Is a hash: %s\n", ak_fs_verify_input_is_hash(queried_string) ? "true": "false"); + sha512sum hash = {0}; + sha512sum *resulted_hash = &hash; + ak_fs_sha512sum_string_to_struct(queried_string, resulted_hash); + FILE* fd = fopen("tmpfile", "wb"); + if ( fd == NULL ) + { + printf("Some error occured"); + exit(1); + } + fwrite(&resulted_hash, sizeof(sha512sum),1,fd); + fclose(fd); + sha512sum readone = {0}; + fd = fopen("tmpfile", "rb"); + if ( fd == NULL ) + { + printf("Some error occured"); + exit(1); + } + fread (&readone, sizeof(sha512sum),1,fd); + char resulted_string[129] = {0}; + ak_fs_sha512sum_struct_to_string(resulted_hash, resulted_string); + if ( strcmp(queried_string, resulted_string) == 0 ) + { + ak_log_test(__func__, "PASSED"); + } + else + { + ak_log_test(__func__, "FAILED"); + } + fclose(fd); +} + +static void test_hash_check() +{ + const char *queried_string = "921618bc6d9f8059437c5e0397b13f973ab7c7a7b81f0ca31b70bf448fd800a460b67efda0020088bc97bf7d9da97a9e2ce7b20d46e066462ec44cf60284f9a7"; + sha512sum a; + sha512sum b; + ak_fs_sha512sum_string_to_struct(queried_string, &a); + ak_fs_sha512sum_string_to_struct(queried_string, &b); + if ( ak_fs_sha512sum_compare(&a,&b) ) + { + ak_log_test(__func__, "PASSED"); + } + else + { + ak_log_test(__func__, "FAILED"); + } +} + +static void test_map_opener() +{ + akfs_map_v3 map; + ak_fs_map_v3_init(&map); + char *map_string = "28bde5fa7aacd8da0ec84b61cf3a69141686906c00f8cff904c9a0b12f5a4cf061da254feb188c32b711b2e1d6a3853d5ac3fb0bcd3564899bae55dd30470392"; + ak_fs_sha512sum_string_to_struct(map_string, &(map.mh)); + if ( ak_fs_map_v3_open_from_file(&map) != 0 ) + { + ak_log_test(__func__, "FAILED"); + return; + } + const char *orig_string = "fa19bdc471bedc42abf3ff52069214bc7339a7eafc03f8551e8af892a0e3ce175cff0dde6f815da031cd0566fded455c937f7cae27181f7a90ab92e6131ba2be"; + const char *root_string = "438aebe24c89d36f84a68ea29327b27af1abc05f8f85e69af650159c4928834bd6fd2b3df690de74d42f861a8dbe30cebc6cba6afe07fabb1066d1380cd3adea"; + const char *filename = "mixtapes-v0.0.0.tar.gz"; + // printf("%s\n",ak_fs_sha512sum_struct_read_as_string((const sha512sum*)&(map.mh))); + // printf("%s\n",ak_fs_sha512sum_struct_read_as_string((const sha512sum*)&(map.oh))); + // printf("%s\n",ak_fs_sha512sum_struct_read_as_string((const sha512sum*)&(map.rh))); + // printf("%s\n",map.filename); + + ak_fs_map_v3_print(&map); + ak_fs_map_v3_print_as_json(&map); + + if ( + (strcmp(map_string, ak_fs_sha512sum_struct_read_as_string(&(map.mh)))!=0) || + (strcmp(orig_string, ak_fs_sha512sum_struct_read_as_string(&(map.oh)))!=0) || + (strcmp(root_string, ak_fs_sha512sum_struct_read_as_string(&(map.rh)))!=0) || + (strcmp(filename, map.filename)!=0)) + { + ak_log_test(__func__, "FAILED"); + return; + } + else + { + ak_log_test(__func__, "PASSED"); + return; + } +} + +static void test_ak_fs_ls() +{ + ak_log_test(__func__, ".....=====....."); + ak_fs_ls(); +} + +static void test_ak_fs_cfm() +{ + akfs_map_v3 map; + ak_fs_map_v3_init(&map); + char *map_string = "28bde5fa7aacd8da0ec84b61cf3a69141686906c00f8cff904c9a0b12f5a4cf061da254feb188c32b711b2e1d6a3853d5ac3fb0bcd3564899bae55dd30470392"; + ak_fs_sha512sum_string_to_struct(map_string, &(map.mh)); + if ( ak_fs_map_v3_open_from_file(&map) != 0 ) + { + ak_log_test(__func__, "FAILED"); + return; + } + if ( ak_fs_cfm(&map) == 0 ) + { + ak_log_test(__func__, "PASSED"); + } + else + { + ak_log_test(__func__, "FAILED"); + } +} + + +int main(void) +{ + // Correct one + test_correct_string_correct_length(); + + // Supposingly a bad string but in correct length + test_bad_string_correct_length(); + + // Less than must be length + test_less_than_length(); + + // More than must be length + test_more_than_length(); + + // Empty string + test_string_is_empty(); + + // Hash path + test_hash_path_test(); + + // Hash dir + test_hash_dir_test(); + + // Tempfile test read-write + test_hash_save_to_file(); + + // Hash checking + test_hash_check(); + + // Map file opener + test_map_opener(); + + // Test ak_fs_ls + test_ak_fs_ls(); + printf("%lu\n", (unsigned long)sizeof(sha512sum)); + printf("%lu\n", (unsigned long)sizeof(akfs_map_v3)); + printf("%lu\n", (unsigned long)sizeof(akfs_map_v4)); + + // Test ak_fs_cfm + test_ak_fs_cfm(); + return 0; +} diff --git a/c_implementation/tests/test_akfs_mkdir.c b/c_implementation/tests/test_akfs_mkdir.c new file mode 100644 index 0000000..ceb450e --- /dev/null +++ b/c_implementation/tests/test_akfs_mkdir.c @@ -0,0 +1,42 @@ +#include <stdio.h> +#include <libakfs.h> + +static void test_non_hash_string() +{ + const char *path = "tes"; + int ec = ak_fs_create_dir_for_hash(path); + printf("mkdir return code: %d\n", ec); + path = "tes/t"; + ec = ak_fs_create_dir_for_hash(path); + printf("mkdir return code: %d\n", ec); +} + +static void test_hash_string() +{ + const char *path = "ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff"; + int ec = ak_fs_create_dir_for_hash(path); + printf("mkdir return code: %d\n", ec); +} + +static void test_hash_string2() +{ + const char *path = "ee2ab0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff"; + int ec = ak_fs_create_dir_for_hash(path); + printf("mkdir return code: %d\n", ec); +} + +static void test_hash_string3() +{ + const char *path = "ee2ab0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a800"; + int ec = ak_fs_create_dir_for_hash(path); + printf("mkdir return code: %d\n", ec); +} + +int main() +{ + test_non_hash_string(); + test_hash_string(); + test_hash_string2(); + test_hash_string3(); + return 0; +} diff --git a/c_implementation/tests/test_aklog.c b/c_implementation/tests/test_aklog.c new file mode 100644 index 0000000..fba4ad2 --- /dev/null +++ b/c_implementation/tests/test_aklog.c @@ -0,0 +1,78 @@ +#include <libaklog.h> + +static void test_message_output() +{ + ak_log_print_log_line("1731664790 <TEST> [INFO] test message info"); +} + +static void test_follow() +{ + ak_log_follow(); +} + +static void test_grep() +{ + ak_log_grep("-h"); +} + +static void test_rotate() +{ + ak_log_rotate(); +} + +static void test_log_message() +{ + ak_log_message(__func__, DEBUG, "test message info"); +} + +static void test_exit() +{ + ak_log_exit(__func__, "test message exit"); +} + +static void test_warning() +{ + ak_log_warning(__func__, "test message WARNING"); +} + +static void test_debug() +{ + ak_log_debug(__func__, "test message debug"); +} + +static void test_error() +{ + ak_log_error(__func__, "test message error"); +} + +static void test_info() +{ + ak_log_info(__func__, "test message info"); +} + +static void test_test() +{ + ak_log_test(__func__, "test message test"); +} + +static void test_one_word() +{ + ak_log_info(__func__, "test"); +} + +int main (void) +{ + test_follow(); + test_message_output(); + test_info(); + test_exit(); + test_error(); + test_debug(); + test_warning(); + test_test(); + test_log_message(); + test_rotate(); + test_grep(); + test_one_word(); + return 0; +} diff --git a/c_implementation/tests/test_aklogcatter.c b/c_implementation/tests/test_aklogcatter.c new file mode 100644 index 0000000..0fd4219 --- /dev/null +++ b/c_implementation/tests/test_aklogcatter.c @@ -0,0 +1,7 @@ +#include <libaklogcatter.h> + +int main() +{ + ak_logcatter(); + return 0; +} diff --git a/c_implementation/tests/test_aklogwrite.c b/c_implementation/tests/test_aklogwrite.c new file mode 100644 index 0000000..2362e35 --- /dev/null +++ b/c_implementation/tests/test_aklogwrite.c @@ -0,0 +1,16 @@ +#include <libaklog.h> + +static void test_write() +{ + ak_log_info(__func__, "test message info"); + ak_log_warning(__func__, "test message warning"); + ak_log_error(__func__, "test message error"); + ak_log_debug(__func__, "test message debug"); +} + +int main (void) +{ + test_write(); + return 0; +} + diff --git a/c_implementation/tests/test_aksettings.c b/c_implementation/tests/test_aksettings.c new file mode 100644 index 0000000..de9e8e3 --- /dev/null +++ b/c_implementation/tests/test_aksettings.c @@ -0,0 +1,122 @@ +#include <libaksettings.h> +#include <libaklog.h> +#include <stdio.h> + +static int test_ak_settings_read_example() +{ + printf("Testing: %s\n", __func__); + if (!ak_settings_load_settings_binary()) + { + ak_log_warning(__func__, "No existing settings or error loading.\n"); + } + const char *bindir = ak_settings_get_setting("AK_BINDIR"); + if (bindir) + { + char *some_text; + asprintf(&some_text, "Current bin directory: %s\n", bindir); + ak_log_info(__func__, some_text); + } + ak_settings_free_settings(); + return 0; +} + +static int test_ak_settings_read_write_example() +{ + printf("Testing: %s\n", __func__); + if (!ak_settings_load_settings_binary()) + { + ak_log_warning(__func__, "No existing settings or error loading.\n"); + } + ak_settings_import_from_environment(); + ak_settings_set_setting("username", "john_doe"); + ak_settings_set_setting("theme", "dark"); + ak_settings_set_setting("volume", "75"); + ak_settings_set_setting("theme", "light"); + if (!ak_settings_save_settings_binary()) + { + printf("Error saving settings!\n"); + ak_settings_free_settings(); + return 1; + } + const char *theme = ak_settings_get_setting("theme"); + if (theme) + { + char *some_text; + asprintf(&some_text, "Current theme: %s\n", theme); + ak_log_info(__func__, some_text); + } + const char *bindir = ak_settings_get_setting("AK_BINDIR"); + if (bindir) + { + char *some_text; + asprintf(&some_text, "Current bin directory: %s\n", bindir); + ak_log_info(__func__, some_text); + } + ak_settings_free_settings(); + return 0; +} + +static int test_ak_settings() +{ + char *some_text; + if ( test_ak_settings_read_write_example() == 0 ) + { + asprintf(&some_text, "Passed test"); + ak_log_info(__func__, some_text); + return 0; + } + else + { + asprintf(&some_text, "Failed test"); + ak_log_error(__func__, some_text); + return 1; + } +} + +static void test_import() +{ + ak_settings_import_from_environment(); +} + +static int test_read_and_dump() +{ + printf("Testing: %s\n", __func__); + if (!ak_settings_load_settings_binary()) + { + ak_log_warning(__func__, "No existing settings or error loading.\n"); + } + AKSetting* ak_settings; + ak_settings = ak_settings_get_all(); + for ( int i = 0; ak_settings[i].key != NULL; ++i ) + { + printf("%s=%s\n", ak_settings[i].key, ak_settings[i].value); + } + ak_settings_free_settings(); + return 0; +} + +static int test_find() +{ + printf("Testing: %s\n", __func__); + if (!ak_settings_load_settings_binary()) + { + ak_log_warning(__func__, "No existing settings or error loading.\n"); + } + AKSetting* ak_settings; + ak_settings = ak_settings_get_all(); + int i = ak_settings_find_setting("volume"); + if ( i > -1 ) + printf("%s=%s\n", ak_settings[i].key, ak_settings[i].value); + ak_settings_free_settings(); + return 0; +} + +int main() +{ + test_import(); + test_ak_settings(); + test_ak_settings_read_example(); + test_read_and_dump(); + test_find(); + return 0; +} diff --git a/c_implementation/tests/test_aksettings_read.c b/c_implementation/tests/test_aksettings_read.c new file mode 100644 index 0000000..e4a1061 --- /dev/null +++ b/c_implementation/tests/test_aksettings_read.c @@ -0,0 +1,23 @@ +#include <libaksettings.h> +#include <libaklog.h> +#include <stdio.h> + +static void test_ak_settings_from_file() +{ + printf("Testing: %s\n", __func__); + if (!ak_settings_load_settings_binary()) { + ak_log_warning(__func__, "No existing settings or error loading.\n"); + } + const char *theme = ak_settings_get_setting("theme"); + if (theme) { + printf("Current theme: %s\n", theme); + } + ak_settings_free_settings(); +} + + +int main() +{ + test_ak_settings_from_file(); + return 0; +} diff --git a/c_implementation/tests/test_akutils.c b/c_implementation/tests/test_akutils.c new file mode 100644 index 0000000..8619e97 --- /dev/null +++ b/c_implementation/tests/test_akutils.c @@ -0,0 +1,7 @@ +#include <libakutils.h> + +int main() +{ + ak_utils(); + return 0; +} diff --git a/c_implementation/tests/test_sha512_string.c b/c_implementation/tests/test_sha512_string.c new file mode 100644 index 0000000..fbca23c --- /dev/null +++ b/c_implementation/tests/test_sha512_string.c @@ -0,0 +1,70 @@ +#include <stdio.h> +#include <stdbool.h> +#include <assert.h> + +//#include <test_sha512_string.h> + +typedef struct { + long unsigned int sum[8]; +} sha512sum; + +static void shifting_example() +{ + long unsigned int X = 0xf; + for ( long unsigned i = 0; i < 64; i=i+4 ) + { + printf("shift[%02lu]:\t%#018lx\n", i, X << i); + } +} + +static void structed_sum() +{ + sha512sum struct_sample = { + .sum[0] = 0x921618bc6d9f8059, + .sum[1] = 0x437c5e0397b13f97, + .sum[2] = 0x3ab7c7a7b81f0ca3, + .sum[3] = 0x1b70bf448fd800a4, + .sum[4] = 0x60b67efda0020088, + .sum[5] = 0xbc97bf7d9da97a9e, + .sum[6] = 0x2ce7b20d46e06646, + .sum[7] = 0x2ec44cf60284f9a7 + }; + printf("stru:\t"); + for ( long unsigned i = 0; i < 8; ++i ) + { + printf("%lx", struct_sample.sum[i]); + } + printf("\n"); +} + +static void long_unsigned_example() +{ + printf("hex:\t%#018lx\n",0xffffffffffffffff); +} + +static void char_based_sum() +{ + char sum_sample[] = "921618bc6d9f8059437c5e0397b13f973ab7c7a7b81f0ca31b70bf448fd800a460b67efda0020088bc97bf7d9da97a9e2ce7b20d46e066462ec44cf60284f9a7"; +// printf("Size:\t%08lu\n",sizeof(sum_sample)); +// printf("String:\t%s\n",sum_sample); +// printf("Last:\t%c\n",sum_sample[sizeof(sum_sample)-2]); // Null byte + printf("Loop:\t"); // Null byte + for ( long unsigned i = 0; i < sizeof(sum_sample)-1; ++i ) + { + assert (( sum_sample[i] >= 0x30 ) && (( sum_sample[i] <= 0x39) || ( sum_sample[i] >= 0x60 )) && ( sum_sample[i] <= 0x66 )); + printf("%c", sum_sample[i]); +// printf(" 0x%x", sum_sample[i]); + } + printf("\n"); +} + +int main (void) +{ + char_based_sum(); + structed_sum(); + long_unsigned_example(); + shifting_example(); + + return 0; +} + |