diff --git a/helper.c b/helper.c index c4f92ff..38d06c7 100644 --- a/helper.c +++ b/helper.c @@ -85,7 +85,7 @@ int is_ip(char *str) { } /* three dots with upto three digits in before, between and after ? */ - if (dotcount == 3 && i > 0 && i <= 3) + if (*str == '\0' && dotcount == 3 && i > 0 && i <= 3) return 1; else return 0; @@ -108,8 +108,12 @@ unsigned long getaddress(char *host) { struct hostent* pent; long l, *lp; - if (is_ip(host)) + if (strlen(host) == 0) { + return 0; + } + if (is_ip(host)) { return inet_addr(host); + } /* try the system's own resolution mechanism for dns lookup: required only for domain names. @@ -650,13 +654,14 @@ void replace_strings(char *mes, char *strings) { #endif } -/* insert \r in front of all \n if it is not present allready */ +/* insert \r in front of all \n if it is not present allready + * and and a trailing \r\n is not present */ void insert_cr(char *mes){ char *lf, *pos, *backup; pos = mes; lf = strchr(pos, '\n'); - while ((lf != NULL) && (*(--lf) != '\r')) { + while ((lf != NULL) && (lf >= mes+1) && (*(--lf) != '\r')) { backup=str_alloc(strlen(lf)+2); strcpy(backup, lf+1); *(lf+1) = '\r'; diff --git a/tests/check_helper.c b/tests/check_helper.c index 05a1ab5..085d80e 100644 --- a/tests/check_helper.c +++ b/tests/check_helper.c @@ -64,6 +64,77 @@ START_TEST (test_str_to_int) { } END_TEST +START_TEST (test_is_ip) { + fail_unless(is_ip("") == 0, + "is_ip(\"\") returned %d, instead of 0", is_ip("")); + fail_unless(is_ip("0") == 0, + "is_ip(\"0\") returned %d, instead of 0", is_ip("0")); + fail_unless(is_ip("100") == 0, + "is_ip(\"100\") returned %d, instead of 0", is_ip("100")); + fail_unless(is_ip("1000") == 0, + "is_ip(\"1000\") returned %d, instead of 0", is_ip("1000")); + fail_unless(is_ip("1.0") == 0, + "is_ip(\"1.0\") returned %d, instead of 0", is_ip("1.0")); + fail_unless(is_ip("1.2.0") == 0, + "is_ip(\"1.2.0\") returned %d, instead of 0", is_ip("1.2.0")); + fail_unless(is_ip("1.2.3.4.5") == 0, + "is_ip(\"1.2.3.4.5\") returned %d, instead of 0", is_ip("1.2.3.4.5")); + fail_unless(is_ip("1000.0.0.0") == 0, + "is_ip(\"1000.0.0.0\") returned %d, instead of 0", is_ip("1000.0.0.0")); + fail_unless(is_ip("0.1000.0.0") == 0, + "is_ip(\"0.1000.0.0\") returned %d, instead of 0", is_ip("0.1000.0.0")); + fail_unless(is_ip("0.0.1000.0") == 0, + "is_ip(\"0.0.1000.0\") returned %d, instead of 0", is_ip("0.0.1000.0")); + fail_unless(is_ip("0.0.0.1000") == 0, + "is_ip(\"0.0.0.1000\") returned %d, instead of 0", is_ip("0.0.0.1000")); + fail_unless(is_ip("0.0.0.0") == 1, + "is_ip(\"0.0.0.0\") returned %d, instead of 1", is_ip("0.0.0.0")); + fail_unless(is_ip("1.2.3.4") == 1, + "is_ip(\"1.2.3.4\") returned %d, instead of 1", is_ip("1.2.3.4")); + fail_unless(is_ip("192.168.1.1") == 1, + "is_ip(\"192.168.1.1\") returned %d, instead of 1", is_ip("192.168.1.1")); + // this is a "known bug" ;) + fail_unless(is_ip("999.999.999.999") == 1, + "is_ip(\"999.999.999.999\") returned %d, instead of 1", is_ip("999.999.999.999")); +} +END_TEST + +START_TEST (test_getaddress) { + fail_unless(getaddress("") == 0, + "getaddress(\"\") returned %lu, instead of 0", getaddress("")); + fail_unless(getaddress("127.0.0.1") == 16777343, + "getaddress(\"127.0.0.1\") returned %lu, instead of 16777343", getaddress("127.0.0.1")); + // this should work also without DNS + fail_unless(getaddress("localhost") == 16777343, + "getaddress(\"localhost\") returned %lu, instead of 16777343", getaddress("localhost")); +} +END_TEST + +START_TEST (test_insert_cr) { + char ta[15]; + + memset(ta, '\0', 15); + insert_cr(ta); + fail_unless(memcmp(ta, "\r\n\0\0\0\0\0\0\0\0\0\0\0\0\0", 15) == 0, + "insert_cr(\"\") returned '%s', instead of \"\r\n\"", ta); + memset(ta, '\0', 15); + memcpy(ta, "test", 4); + insert_cr(ta); + fail_unless(memcmp(ta, "test\r\n\0\0\0\0\0\0\0\0\0", 15) == 0, + "insert_cr(\"test\") returned '%s', instead of \"test\r\n\"", ta); + memset(ta, '\0', 15); + memcpy(ta, "test\n", 5); + insert_cr(ta); + fail_unless(memcmp(ta, "test\r\n\r\n\0\0\0\0\0\0\0", 15) == 0, + "insert_cr(\"test\\n\") returned '%s', instead of \"test\\r\\n\"", ta); + memset(ta, '\0', 15); + memcpy(ta, "foo\nbar\n", 8); + insert_cr(ta); + fail_unless(memcmp(ta, "foo\r\nbar\r\n\r\n\0\0\0", 15) == 0, + "insert_cr(\"foo\\nbar\\n\") returned '%s', instead of \"foo\\r\\nbar\\r\\n\"", ta); +} +END_TEST + Suite *helper_suite(void) { Suite *s = suite_create("Helper"); @@ -75,9 +146,24 @@ Suite *helper_suite(void) { TCase *tc_str_to_int = tcase_create("str_to_int"); tcase_add_test(tc_str_to_int, test_str_to_int); + /* is_ip test case */ + TCase *tc_is_ip = tcase_create("is_ip"); + tcase_add_test(tc_is_ip, test_is_ip); + + /* getaddress test case */ + TCase *tc_getaddress = tcase_create("getaddress"); + tcase_add_test(tc_getaddress, test_getaddress); + + /* insert_cr test case */ + TCase *tc_insert_cr = tcase_create("insert_cr"); + tcase_add_test(tc_insert_cr, test_insert_cr); + /* add test cases to suite */ suite_add_tcase(s, tc_is_number); suite_add_tcase(s, tc_str_to_int); + suite_add_tcase(s, tc_is_ip); + suite_add_tcase(s, tc_getaddress); + suite_add_tcase(s, tc_insert_cr); return s; }