From 70cbd59ed6684e12cdc6ce1e10f122ff3608c411 Mon Sep 17 00:00:00 2001 From: Hubert Tarasiuk Date: Tue, 23 Jun 2015 16:36:21 +0200 Subject: [PATCH] Unit test for find_key_value. * src/http.c: Add test_find_key_value. * src/test.c (main): Run new test. * src/test.h: Add test_find_key_value. --- src/http.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- src/test.c | 3 +++ src/test.h | 2 ++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/http.c b/src/http.c index afbc2543..347a2491 100644 --- a/src/http.c +++ b/src/http.c @@ -4998,9 +4998,56 @@ ensure_extension (struct http_stat *hs, const char *ext, int *dt) } } - #ifdef TESTING +const char * +test_find_key_value (void) +{ + static const char *header_data = "key1=val1;key2=val2 ;key3=val3; key4=val4"\ + " ; key5=val5;key6 =val6;key7= val7; "\ + "key8 = val8 ; key9 = val9 "; + static const struct + { + const char *key; + const char *val; + bool result; + } test_array[] = + { + { "key1", "val1", true }, + { "key2", "val2", true }, + { "key3", "val3", true }, + { "key4", "val4", true }, + { "key5", "val5", true }, + { "key6", "val6", true }, + { "key7", "val7", true }, + { "key8", "val8", true }, + { "key9", "val9", true }, + { "key10", NULL, false }, + { "ey1", NULL, false }, + { "dey1", NULL, false } + }; + size_t i; + + for (i=0; i < countof (test_array); ++i) + { + bool result; + char *value; + + result = find_key_value (header_data, + header_data + strlen(header_data), + test_array[i].key, &value); + + mu_assert ("test_find_key_value: wrong result", + result == test_array[i].result && + ((!test_array[i].result && !value) || + !strcmp (value, test_array[i].val))); + + xfree (value); + } + + return NULL; +} + const char * test_parse_content_disposition(void) { diff --git a/src/test.c b/src/test.c index 833bc2d7..e2d3ac2e 100644 --- a/src/test.c +++ b/src/test.c @@ -48,6 +48,9 @@ static int tests_run; static const char * all_tests(void) { +#ifdef HAVE_METALINK + mu_run_test (test_find_key_value); +#endif mu_run_test (test_parse_content_disposition); mu_run_test (test_subdir_p); mu_run_test (test_dir_matches_p); diff --git a/src/test.h b/src/test.h index abccc6d7..1eb14461 100644 --- a/src/test.h +++ b/src/test.h @@ -43,6 +43,8 @@ do { \ puts("PASSED\n"); \ } while (0) + +const char *test_find_key_value (void); const char *test_parse_content_disposition(void); const char *test_commands_sorted(void); const char *test_cmd_spec_restrict_file_names(void);