diff options
Diffstat (limited to '')
-rw-r--r-- | tests/Makefile | 7 | ||||
-rw-r--r-- | tests/driver.c | 46 | ||||
-rw-r--r-- | tests/test.c | 221 | ||||
-rw-r--r-- | tests/test_driver.h | 8 | ||||
-rw-r--r-- | tests/test_fcopy.c | 41 | ||||
-rw-r--r-- | tests/test_join_str.c | 35 | ||||
-rw-r--r-- | tests/test_malloc_free_secure.c | 29 | ||||
-rw-r--r-- | tests/test_memset_s.c | 30 | ||||
-rw-r--r-- | tests/test_min_max_cap.c | 47 | ||||
-rw-r--r-- | tests/test_replace_str.c | 26 | ||||
-rw-r--r-- | tests/test_strlwr_strupr.c | 32 | ||||
-rw-r--r-- | tests/test_trim.c | 35 | ||||
-rw-r--r-- | tests/tests.c | 22 |
13 files changed, 357 insertions, 222 deletions
diff --git a/tests/Makefile b/tests/Makefile index b4ed98e..37f9d46 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -2,9 +2,14 @@ CC = cc CFLAGS = -g -fsanitize=address,undefined +TESTS = $(wildcard test_*.c) + +all: test test: FORCE - $(CC) test.c ../src/extlib.c ../src/extstring.c ../src/constraint_handler.c ../src/memset_s.c $(CFLAGS) -o test + $(CC) -c tests.c -o tests.o + $(CC) driver.c tests.o ../src/extlib.c ../src/extstring.c ../src/constraint_handler.c ../src/memset_s.c $(CFLAGS) -o test ./test + rm tests.o FORCE: ; # PHONY is a non standard extension diff --git a/tests/driver.c b/tests/driver.c new file mode 100644 index 0000000..235d598 --- /dev/null +++ b/tests/driver.c @@ -0,0 +1,46 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <sys/types.h> +#define USE_SECURE_MEM +#include "../src/extlib.h" +#include "test_driver.h" + +const char* success = "\033[1;32m"; +const char* fail = " \033[1;31m"; +const char *reset = "\033[0m"; + +int tests_count; +struct test_t** registered_tests; + +int +test_runner (struct test_t *test, int n) +{ + printf ("Test Case %d: %s -- %s\n", n, test->test_name, test->test_desc); + int test_result = test->test_func(); + if (test_result == 0) + printf ("Test Case %d: %s -- %sSUCCESS%s\n\n", n, test->test_name, success, reset); + else + printf ("Test Case %d: %s -- %sFAIL %d%s\n\n", n, test->test_name, test_result, fail, reset); +} + +void +register_test (struct test_t *test) +{ + tests_count += 1; + registered_tests = reallocarray (registered_tests, sizeof (struct test_t), tests_count); + + registered_tests[tests_count-1] = test; +} + +int +main (int argc, char *argv[]) +{ + registered_tests = malloc (8); + tests_entrypoint(); + for (int i = 0; i < tests_count; i++) + { + test_runner(registered_tests[i], i+1); + } + return 0; +} diff --git a/tests/test.c b/tests/test.c deleted file mode 100644 index 0d1ff7c..0000000 --- a/tests/test.c +++ /dev/null @@ -1,221 +0,0 @@ -#include <stdlib.h> -#include <string.h> -#include <stdio.h> -#include <assert.h> -#include <sys/types.h> -#define USE_SECURE_MEM -#include "../src/extlib.h" - -int -test_min_max_cap () -{ - int val = 5; - int max_v = 10; - int min_v = 1; - if (cap(val, min_v, max_v) != 5) - return 1; - if (max(val, max_v) != 5) - return 2; - if (min(val, min_v) != 5) - return 3; - - val = 11; - if (cap(val, min_v, max_v) != 10) - return 4; - if (max(val, max_v) != 10) - return 5; - if (min(val, min_v) != 11) - return 6; - - val = 0; - if (cap(val, min_v, max_v) != 1) - return 7; - if (max(val, max_v) != 0) - return 8; - if (min(val, min_v) != 1) - return 9; - - return 0; -} - -int -test_malloc_free_secure (size_t size) -{ - char *mall_test = malloc_secure (size); - if (!mall_test) - return 1; - if (memvcmp (mall_test, 0, size) != 0) - return 2; - free_secure ((void **) &mall_test, size); - if (mall_test) - return 3; - return 0; -} - -int -test_fcopy () -{ - int errno = 0; - char *buf_a; - char *buf_b; - size_t len_a; - size_t len_b; - FILE *file_test_a = open_memstream (&buf_a, &len_a); - FILE *file_test_b = open_memstream (&buf_b, &len_b); - - fprintf (file_test_a, "hello"); - fflush (file_test_a); - fclose (file_test_a); - file_test_a = fmemopen (buf_a, len_a, "r"); - - fcopy (file_test_a, file_test_b); - fclose (file_test_b); - - if (memcmp (buf_a, buf_b, len_a) != 0) - errno = 1; - - free_secure ((void **) &buf_a, len_a); - free_secure ((void **) &buf_b, len_b); - return errno; -} - -int -test_strlwr_strupr () -{ - int errno = 0; - char *test_string = strdup ("hello"); - char *uppr_string = strupr (test_string); - char *lower_string = strlwr (uppr_string); - - if (strcmp (uppr_string, "HELLO") != 0) - errno = 1; - - if (strcmp (lower_string, "hello") != 0) - errno = 2; - - EXIT: - free_secure ((void **) &test_string, strlen (test_string)); - return errno; -} - -int -test_trim () -{ -#undef free - int errno = 0; - char *test_string = strdup ("\t\thi\t\t"); - int rem_front = 0; - int rem_back = 0; - char *trimmed_string = trim (test_string, &rem_front, &rem_back); - if (strstr (trimmed_string, "\t") != NULL) - { - errno = 1; - goto EXIT; - } - if (rem_front != 2 && rem_back != 2) - { - errno = 2; - goto EXIT; - } - - EXIT: - free (trimmed_string - rem_front); - free_secure ((void **) &test_string, strlen (test_string)); - return errno; -} - -int -test_replace_str () -{ - int errno = 0; - char *test_string = strdup ("replace world!"); - char *replaced_string = replace_str (test_string, "replace", "hello"); - if (strcmp (replaced_string, "hello world!") != 0) - errno = 1; - - free_secure ((void **) &test_string, strlen (test_string)); - free_secure ((void **) &replaced_string, strlen (replaced_string)); - return errno; -} - -int -test_join_str () -{ - int errno = 0; - char **test_string = malloc (sizeof (char *) * 4); - for (int i = 0; i < 3; i++) - test_string[i] = strdup ("hello"); - - char *joined_string = join_str (test_string, 3, ' '); - - if (strcmp (joined_string, "hello hello hello") != 0) - errno = 1; - - free_secure ((void **) &test_string[0], strlen (test_string[0])); - free_secure ((void **) &test_string[1], strlen (test_string[1])); - free_secure ((void **) &test_string[2], strlen (test_string[2])); - free (test_string); - free_secure ((void **) &joined_string, strlen (joined_string)); - return errno; -} - -int -main (void) -{ - printf ("=== extlib unit tests ===\n"); - printf ("\n"); - - printf - ("Test Case 1: test_malloc_free_secure -- malloc_secure free_secure memvcmp\n"); - int test_1_result = test_malloc_free_secure (12); - if (test_1_result == 0) - printf ("Test Case 1: test_malloc_free_secure -- SUCCESS\n\n"); - else - printf ("Test Case 1: test_malloc_free_secure -- FAILED %d\n\n", - test_1_result); - - printf ("Test Case 2: test_fcopy -- fcopy\n"); - int test_2_result = test_fcopy (); - if (test_2_result == 0) - printf ("Test Case 2: test_fcopy -- SUCCESS\n\n"); - else - printf ("Test Case 2: test_fcopy -- FAILED %d\n\n", test_2_result); - - printf ("Test Case 3: test_strlwr_strupr -- strlwr strupr\n"); - int test_3_result = test_strlwr_strupr (); - if (test_2_result == 0) - printf ("Test Case 3: test_strlwr_strupr -- SUCCESS\n\n"); - else - printf ("Test Case 3: test_strlwr_strupr -- FAILED %d\n\n", - test_3_result); - - printf ("Test Case 4: test_trim -- trim\n"); - int test_4_result = test_trim (); - if (test_4_result == 0) - printf ("Test Case 4: test_trim -- SUCCESS\n\n"); - else - printf ("Test Case 4: test_trim -- FAILED %d\n\n", test_4_result); - - printf ("Test Case 5: test_replace_str -- replace_str\n"); - int test_5_result = test_replace_str (); - if (test_5_result == 0) - printf ("Test Case 5: test_replace_str -- SUCCESS\n\n"); - else - printf ("Test Case 5: test_replace_str -- FAILED %d\n\n", - test_5_result); - - printf ("Test Case 6: test_join_str -- join_str\n"); - int test_6_result = test_join_str (); - if (test_6_result == 0) - printf ("Test Case 6: test_join_str -- SUCCESS\n\n"); - else - printf ("Test Case 6: test_join_str -- FAILED %d\n\n", test_6_result); - - printf ("Test Case 7: test_min_max_cap\n"); - int test_7_result = test_min_max_cap (); - if (test_7_result == 0) - printf ("Test Case 7: test_min_max_cap -- SUCCESS\n\n"); - else - printf ("Test Case 7: test_min_max_cap -- FAILED %d\n\n", test_7_result); - return 0; -} diff --git a/tests/test_driver.h b/tests/test_driver.h new file mode 100644 index 0000000..4c157d9 --- /dev/null +++ b/tests/test_driver.h @@ -0,0 +1,8 @@ +struct test_t { + int (*test_func)(void); + char *test_name; + char *test_desc; +}; + +void register_test (struct test_t *test); +void tests_entrypoint (); diff --git a/tests/test_fcopy.c b/tests/test_fcopy.c new file mode 100644 index 0000000..3847ed3 --- /dev/null +++ b/tests/test_fcopy.c @@ -0,0 +1,41 @@ +#include <string.h> +#define USE_SECURE_MEM +#include "../src/extlib.h" +#undef free + +int +test_fcopy () +{ + int errno = 0; + char *buf_a; + char *buf_b; + size_t len_a; + size_t len_b; + FILE *file_test_a = open_memstream (&buf_a, &len_a); + FILE *file_test_b = open_memstream (&buf_b, &len_b); + + fprintf (file_test_a, "meow"); + fflush (file_test_a); + fclose (file_test_a); + file_test_a = fmemopen (buf_a, len_a, "r"); + + fcopy (file_test_a, file_test_b); + fclose (file_test_b); + + if (memcmp (buf_a, buf_b, len_a) != 0) + errno = 1; + + free_secure ((void **) &buf_a, len_a); + free_secure ((void **) &buf_b, len_b); + return errno; +} + +struct test_t* +test_fcopy_t () +{ + struct test_t* test = malloc (sizeof (struct test_t)); + test->test_func=test_fcopy; + test->test_name="test_fcopy"; + test->test_desc="fcopy"; + return test; +} diff --git a/tests/test_join_str.c b/tests/test_join_str.c new file mode 100644 index 0000000..711c864 --- /dev/null +++ b/tests/test_join_str.c @@ -0,0 +1,35 @@ +#include <string.h> +#define USE_SECURE_MEM +#include "../src/extlib.h" +#undef free + +int +test_join_str () +{ + int errno = 0; + char **test_string = malloc (sizeof (char *) * 4); + for (int i = 0; i < 3; i++) + test_string[i] = strdup ("hello"); + + char *joined_string = join_str (test_string, 3, ' '); + + if (strcmp (joined_string, "hello hello hello") != 0) + errno = 1; + + free_secure ((void **) &test_string[0], strlen (test_string[0])); + free_secure ((void **) &test_string[1], strlen (test_string[1])); + free_secure ((void **) &test_string[2], strlen (test_string[2])); + free (test_string); + free_secure ((void **) &joined_string, strlen (joined_string)); + return errno; +} + +struct test_t* +test_join_str_t() +{ + struct test_t* test = malloc (sizeof (struct test_t)); + test->test_func=test_join_str; + test->test_name="test_join_str"; + test->test_desc="join_str"; + return test; +} diff --git a/tests/test_malloc_free_secure.c b/tests/test_malloc_free_secure.c new file mode 100644 index 0000000..0339cea --- /dev/null +++ b/tests/test_malloc_free_secure.c @@ -0,0 +1,29 @@ +#include <string.h> +#define USE_SECURE_MEM +#include "../src/extlib.h" +#undef free + +int +test_malloc_free_secure () +{ + size_t size = 12; + char *mall_test = malloc_secure (size); + if (!mall_test) + return 1; + if (memvcmp (mall_test, 0, size) != 0) + return 2; + free_secure ((void **) &mall_test, size); + if (mall_test) + return 3; + return 0; +} + +struct test_t* +test_malloc_free_secure_t () +{ + struct test_t* test = malloc (sizeof (struct test_t)); + test->test_func=test_malloc_free_secure; + test->test_name="test_malloc_free_secure"; + test->test_desc="malloc_secure free_secure"; + return test; +} diff --git a/tests/test_memset_s.c b/tests/test_memset_s.c new file mode 100644 index 0000000..7716341 --- /dev/null +++ b/tests/test_memset_s.c @@ -0,0 +1,30 @@ +#include <string.h> +#define USE_MEM_SECURE +#define __STDC_WANT_LIB_EXT1__ 1 +#include "../src/extlib.h" + +int +test_memset_s () +{ + int errno = 0; + char *buf = malloc (100); + + if (memset_s (buf, 100, 'a', 100) != 0) + errno = 1; + + if (memvcmp (buf, 'a', 100) != 0) + errno = 2; + + free_secure ((void **) &buf, 100); + return errno; +} + +struct test_t* +test_memset_s_t () +{ + struct test_t* test = malloc (sizeof (struct test_t)); + test->test_func = test_memset_s; + test->test_name = "test_memset_s"; + test->test_desc = "memset_s"; + return test; +} diff --git a/tests/test_min_max_cap.c b/tests/test_min_max_cap.c new file mode 100644 index 0000000..6880492 --- /dev/null +++ b/tests/test_min_max_cap.c @@ -0,0 +1,47 @@ +#include <string.h> +#define USE_SECURE_MEM +#include "../src/extlib.h" +#undef free + +int +test_min_max_cap () +{ + int val = 5; + int max_v = 10; + int min_v = 1; + if (cap(val, min_v, max_v) != 5) + return 1; + if (max(val, max_v) != 5) + return 2; + if (min(val, min_v) != 5) + return 3; + + val = 11; + if (cap(val, min_v, max_v) != 10) + return 4; + if (max(val, max_v) != 10) + return 5; + if (min(val, min_v) != 11) + return 6; + + val = 0; + if (cap(val, min_v, max_v) != 1) + return 7; + if (max(val, max_v) != 0) + return 8; + if (min(val, min_v) != 1) + return 9; + + return 0; +} + + +struct test_t* +test_min_max_cap_t () +{ + struct test_t* test = malloc (sizeof (struct test_t)); + test->test_func=test_min_max_cap; + test->test_name="test_min_max_cap"; + test->test_desc="min max cap"; + return test; +} diff --git a/tests/test_replace_str.c b/tests/test_replace_str.c new file mode 100644 index 0000000..020149f --- /dev/null +++ b/tests/test_replace_str.c @@ -0,0 +1,26 @@ +#include <string.h> +#define USE_SECURE_MEM +#include "../src/extlib.h" + +int +test_replace_str () +{ + int errno = 0; + char *test_string = strdup ("replace world!"); + char *replaced_string = replace_str (test_string, "replace", "hello"); + if (strcmp (replaced_string, "hello world!") != 0) + errno = 1; + + free_secure ((void **) &test_string, strlen (test_string)); + free_secure ((void **) &replaced_string, strlen (replaced_string)); + return errno; +} + +struct test_t* +test_replace_str_t () +{ + struct test_t* test = malloc (sizeof (struct test_t)); + test->test_func = test_replace_str; + test->test_name = "test_replace_str"; + test->test_desc = "replace_str"; +} diff --git a/tests/test_strlwr_strupr.c b/tests/test_strlwr_strupr.c new file mode 100644 index 0000000..ce166f1 --- /dev/null +++ b/tests/test_strlwr_strupr.c @@ -0,0 +1,32 @@ +#include <string.h> +#define USE_SECURE_MEM +#include "../src/extlib.h" + +int +test_strlwr_strupr () +{ + int errno = 0; + char *test_string = strdup ("meow"); + + char *uppr_string = strupr (test_string); + if (strcmp (uppr_string, "MEOW") != 0) + errno = 1; + + char *lower_string = strlwr (uppr_string); + if (strcmp (lower_string, "meow") != 0) + errno = 2; + + free_secure ((void **) &test_string, strlen (test_string)); + return errno; +} + +struct test_t* +test_strlwr_strupr_t () +{ + struct test_t* test = malloc (sizeof (struct test_t)); + test->test_func=test_strlwr_strupr; + test->test_name="test_strlwr_strupr"; + test->test_desc="strlwr strupr"; + return test; +} + diff --git a/tests/test_trim.c b/tests/test_trim.c new file mode 100644 index 0000000..f0aeed6 --- /dev/null +++ b/tests/test_trim.c @@ -0,0 +1,35 @@ +#include <string.h> +#define USE_SECURE_MEM +#include "../src/extlib.h" +#undef free + +int +test_trim () +{ + int errno = 0; + char *test_string = strdup ("\t\thi\t\t"); + int rem_front = 0; + int rem_back = 0; + char *trimmed_string = trim (test_string, &rem_front, &rem_back); + if (strstr (trimmed_string, "\t") != NULL) + { + errno = 1; + } + else if (rem_front != 2 && rem_back != 2) + { + errno = 2; + } + + free (trimmed_string - rem_front); + free_secure ((void **) &test_string, strlen (test_string)); + return errno; +} + +struct test_t* +test_trim_t () +{ + struct test_t *test = malloc (sizeof (struct test_t)); + test->test_func = test_trim; + test->test_name = "test_trim"; + test->test_desc = "trim"; +} diff --git a/tests/tests.c b/tests/tests.c new file mode 100644 index 0000000..73dd1af --- /dev/null +++ b/tests/tests.c @@ -0,0 +1,22 @@ +#include "test_driver.h" +#include "test_fcopy.c" +#include "test_join_str.c" +#include "test_malloc_free_secure.c" +#include "test_memset_s.c" +#include "test_min_max_cap.c" +#include "test_replace_str.c" +#include "test_strlwr_strupr.c" +#include "test_trim.c" + +inline void +tests_entrypoint () +{ + register_test (test_fcopy_t ()); + register_test (test_join_str_t( )); + register_test (test_malloc_free_secure_t ()); + register_test (test_memset_s_t ()); + register_test (test_min_max_cap_t ()); + register_test (test_replace_str_t ()); + register_test (test_strlwr_strupr_t ()); + register_test (test_trim_t ()); +} |