|
| 1 | +#ifndef TEST_CURL_HELPERS_H |
| 2 | +#define TEST_CURL_HELPERS_H |
| 3 | + |
| 4 | +#include "acquire_common_defs.h" |
| 5 | +#include <greatest.h> |
| 6 | + |
| 7 | +int get_oname_from_cd(char const *const cd, char *oname); |
| 8 | +int get_oname_from_url(char const *url, char *oname); |
| 9 | + |
| 10 | +TEST test_get_oname_from_url_simple(void) { |
| 11 | + char oname[MAX_FILENAME]; |
| 12 | + int result = get_oname_from_url("http://example.com/somefile.txt", oname); |
| 13 | + ASSERT_EQ(EXIT_SUCCESS, result); |
| 14 | + ASSERT_STR_EQ("somefile.txt", oname); |
| 15 | + PASS(); |
| 16 | +} |
| 17 | + |
| 18 | +TEST test_get_oname_from_url_with_query(void) { |
| 19 | + char oname[MAX_FILENAME]; |
| 20 | + /* This implementation is simple and doesn't handle query strings. */ |
| 21 | + int result = |
| 22 | + get_oname_from_url("https://site.com/archive.zip?version=1.2", oname); |
| 23 | + ASSERT_EQ(EXIT_SUCCESS, result); |
| 24 | + ASSERT_STR_EQ("archive.zip?version=1.2", oname); |
| 25 | + PASS(); |
| 26 | +} |
| 27 | + |
| 28 | +TEST test_get_oname_from_url_no_path(void) { |
| 29 | + char oname[MAX_FILENAME]; |
| 30 | + int result = get_oname_from_url("http://example.com", oname); |
| 31 | + ASSERT_EQ(EXIT_SUCCESS, result); |
| 32 | + ASSERT_STR_EQ("http://example.com", oname); |
| 33 | + PASS(); |
| 34 | +} |
| 35 | + |
| 36 | +TEST test_get_oname_from_url_trailing_slash(void) { |
| 37 | + char oname[MAX_FILENAME]; |
| 38 | + int result = get_oname_from_url("http://example.com/some/path/", oname); |
| 39 | + ASSERT_EQ(EXIT_SUCCESS, result); |
| 40 | + ASSERT_STR_EQ("", oname); |
| 41 | + PASS(); |
| 42 | +} |
| 43 | + |
| 44 | +TEST test_get_oname_from_cd_simple(void) { |
| 45 | + char oname[MAX_FILENAME]; |
| 46 | + const char *cd = "attachment; filename=simple.dat"; |
| 47 | + int result = get_oname_from_cd(cd, oname); |
| 48 | + ASSERT_EQ(EXIT_SUCCESS, result); |
| 49 | + ASSERT_STR_EQ("simple.dat", oname); |
| 50 | + PASS(); |
| 51 | +} |
| 52 | + |
| 53 | +TEST test_get_oname_from_cd_case_insensitive(void) { |
| 54 | + char oname[MAX_FILENAME]; |
| 55 | + const char *cd = "attachment; FILENAME=case.txt"; |
| 56 | + int result = get_oname_from_cd(cd, oname); |
| 57 | + ASSERT_EQ(EXIT_SUCCESS, result); |
| 58 | + ASSERT_STR_EQ("case.txt", oname); |
| 59 | + PASS(); |
| 60 | +} |
| 61 | + |
| 62 | +TEST test_get_oname_from_cd_with_other_params(void) { |
| 63 | + char oname[MAX_FILENAME]; |
| 64 | + const char *cd = "attachment; filename=ext.zip; charset=UTF-8"; |
| 65 | + int result = get_oname_from_cd(cd, oname); |
| 66 | + ASSERT_EQ(EXIT_SUCCESS, result); |
| 67 | + ASSERT_STR_EQ("ext.zip", oname); |
| 68 | + PASS(); |
| 69 | +} |
| 70 | + |
| 71 | +TEST test_get_oname_from_cd_no_filename(void) { |
| 72 | + char oname[MAX_FILENAME] = "initial"; |
| 73 | + const char *cd = "attachment; something=else"; |
| 74 | + int result = get_oname_from_cd(cd, oname); |
| 75 | + ASSERT_EQ(EXIT_FAILURE, result); |
| 76 | + ASSERT_STR_EQ("initial", oname); /* Should not be modified */ |
| 77 | + PASS(); |
| 78 | +} |
| 79 | + |
| 80 | +SUITE(curl_helpers_suite) { |
| 81 | + RUN_TEST(test_get_oname_from_url_simple); |
| 82 | + RUN_TEST(test_get_oname_from_url_with_query); |
| 83 | + RUN_TEST(test_get_oname_from_url_no_path); |
| 84 | + RUN_TEST(test_get_oname_from_url_trailing_slash); |
| 85 | + |
| 86 | + RUN_TEST(test_get_oname_from_cd_simple); |
| 87 | + RUN_TEST(test_get_oname_from_cd_case_insensitive); |
| 88 | + RUN_TEST(test_get_oname_from_cd_with_other_params); |
| 89 | + RUN_TEST(test_get_oname_from_cd_no_filename); |
| 90 | +} |
| 91 | + |
| 92 | +#endif /* !TEST_CURL_HELPERS_H */ |
0 commit comments