diff --git a/src/netrc.c b/src/netrc.c index 76e52485..ae98fa35 100644 --- a/src/netrc.c +++ b/src/netrc.c @@ -481,6 +481,56 @@ free_netrc(acc_t *l) } #endif +#ifdef TESTING +#include "../tests/unit-tests.h" +const char * +test_parse_netrc(void) +{ + static const struct test { + const char *pw_in; + const char *pw_expected; + } tests[] = { + { "a\\b", "ab" }, + { "a\\\\b", "a\\b" }, + { "\"a\\\\b\"", "a\\b" }, + { "\"a\\\"b\"", "a\"b" }, + { "a\"b", "a\"b" }, + { "a\\\\\\\\b", "a\\\\b" }, + { "a\\\\", "a\\" }, + { "\"a\\\\\"", "a\\" }, + { "a\\", "a" }, + { "\"a b\"", "a b" }, + { "a b", "a" }, + }; + unsigned i; + static char errmsg[128]; + + for (i = 0; i < countof(tests); ++i) + { + const struct test *t = &tests[i]; + char netrc[128]; + FILE *fp; + acc_t *acc; + int n; + + n = snprintf (netrc, sizeof(netrc), "machine localhost\n\tlogin me\n\tpassword %s", t->pw_in); + mu_assert ("test_parse_netrc: failed to fmemopen() netrc", (fp = fmemopen(netrc, n, "r")) != NULL); + + acc = parse_netrc_fp ("memory", fp); + fclose(fp); + + if (strcmp(acc->passwd, t->pw_expected)) + { + snprintf(errmsg, sizeof(errmsg), "test_parse_netrc: wrong result [%u]. Expected '%s', got '%s'", + i, t->pw_expected, acc->passwd); + return errmsg; + } + } + + return NULL; +} +#endif + #ifdef STANDALONE #include #include diff --git a/tests/unit-tests.c b/tests/unit-tests.c index 443e628f..8994d3a3 100644 --- a/tests/unit-tests.c +++ b/tests/unit-tests.c @@ -65,6 +65,7 @@ all_tests(void) mu_run_test (test_hsts_url_rewrite_congruent); mu_run_test (test_hsts_read_database); #endif + mu_run_test (test_parse_netrc); return NULL; } diff --git a/tests/unit-tests.h b/tests/unit-tests.h index dd10e545..3495bbdf 100644 --- a/tests/unit-tests.h +++ b/tests/unit-tests.h @@ -61,6 +61,7 @@ const char *test_hsts_new_entry(void); const char *test_hsts_url_rewrite_superdomain(void); const char *test_hsts_url_rewrite_congruent(void); const char *test_hsts_read_database(void); +const char *test_parse_netrc(void); #endif /* TEST_H */