static bool test_unsorted_table() { emit_test("Test UNSORTED_TOKENER_TABLE"); MyString msg; for (size_t ii = 0; ii < TestTbl.cItems; ++ii) { const char * key = TestTbl.pTable[ii].key; const TestTableItem * tti = UnTestTbl.lookup(key); if ( ! tti) { emit_step_failure(__LINE__, msg.formatstr("lookup('%s') returned NULL", key)); continue; } if (tti->id != TestTbl.pTable[ii].id) { emit_step_failure(__LINE__, msg.formatstr("lookup('%s') returned '%s'", key, tti->key)); } tokener toke(key); toke.next(); tti = UnTestTbl.lookup_token(toke); if ( ! tti) { emit_step_failure(__LINE__, msg.formatstr("lookup_token('%s') returned NULL", key)); continue; } if (tti->id != TestTbl.pTable[ii].id) { emit_step_failure(__LINE__, msg.formatstr("lookup_token('%s') returned '%s'", key, tti->key)); } } return REQUIRED_RESULT(); }
static bool test_tokener_parse_regex() { emit_test("Test tokener regex parsing"); MyString msg; int fl; std::string temp; tokener toke("/(.*)/i"); REQUIRE(toke.next() && toke.is_regex()); REQUIRE(toke.copy_regex(temp,fl) && temp=="(.*)"); if (fl != 1) { emit_step_failure(__LINE__, msg.formatstr("toke.copy_regex() returned pcre_flags==%d, should be 1", fl)); } toke.set(" /([\\d]*)/i "); REQUIRE(toke.next() && toke.is_regex()); REQUIRE(toke.copy_regex(temp,fl) && temp=="([\\d]*)"); if (fl != 1) { emit_step_failure(__LINE__, msg.formatstr("toke.copy_regex() returned pcre_flags==%d, should be 1", fl)); } toke.set(" /^Now is the|Time$/ "); REQUIRE(toke.next() && toke.is_regex()); REQUIRE(toke.copy_regex(temp,fl) && temp=="^Now is the|Time$"); if (fl != 0) { emit_step_failure(__LINE__, msg.formatstr("toke.copy_regex() returned pcre_flags==%d, should be 1", fl)); } return REQUIRED_RESULT(); }
static bool test_nocase_sorted_table() { emit_test("Test NOCASE SORTED_TOKENER_TABLE"); MyString msg; // test the case sensitive sorted table for (size_t ii = 0; ii < UnTestTbl.cItems; ++ii) { const char * key = UnTestTbl.pTable[ii].key; int id = UnTestTbl.pTable[ii].id; if (id == item_aaa) { id = item_AAA; } // This table has only the AAA item, not the aaa item. const TestTableItem * tti = NcTestTbl.lookup(key); if ( ! tti) { emit_step_failure(__LINE__, msg.formatstr("lookup('%s') returned NULL", key)); continue; } if (tti->id != id) { emit_step_failure(__LINE__, msg.formatstr("lookup('%s') returned '%s'", key, tti->key)); } tokener toke(key); toke.next(); tti = NcTestTbl.lookup_token(toke); if ( ! tti) { emit_step_failure(__LINE__, msg.formatstr("lookup_token('%s') returned NULL", key)); continue; } if (tti->id != id) { emit_step_failure(__LINE__, msg.formatstr("lookup_token('%s') returned '%s'", key, tti->key)); } } return REQUIRED_RESULT(); }
static bool test_slash_notation() { emit_test("Is normal input parsed correctly with slash notation for the mask?"); char* inputstring = strdup("192.168.3.4/8"); emit_input_header(); emit_param("STRING", inputstring); struct in_addr ip; unsigned char* ipbyte = (unsigned char*) &ip; struct in_addr mask; unsigned char* maskbyte = (unsigned char*) &mask; int result = is_valid_network(inputstring, &ip, &mask); free(inputstring); emit_output_expected_header(); int expected_return = TRUE; emit_retval("%s", tfstr(expected_return)); emit_param("IP", "192.168.3.4"); emit_param("MASK", "255.0.0.0"); emit_output_actual_header(); emit_retval("%s", tfstr(result)); emit_param("IP", "%d.%d.%d.%d", *ipbyte, *(ipbyte + 1), *(ipbyte + 2), *(ipbyte + 3)); emit_param("MASK", "%d.%d.%d.%d", *maskbyte, *(maskbyte + 1), *(maskbyte + 2), *(maskbyte + 3)); if( expected_return != result || 192 != *ipbyte || 168 != *(ipbyte + 1) || 3 != *(ipbyte + 2) || 4 != *(ipbyte + 3) || 255 != *maskbyte || 0 != *(maskbyte + 1) || 0 != *(maskbyte + 2) || 0 != *(maskbyte + 3)) { FAIL; } PASS; }
static bool test_upper_case_non_empty() { emit_test("Test upper_case() on a non-empty std::string."); std::string a("lower UPPER"); upper_case(a); emit_output_expected_header(); emit_retval("%s", "LOWER UPPER"); emit_output_actual_header(); emit_retval("%s", a.c_str()); if(strcmp(a.c_str(), "LOWER UPPER") != MATCH) { FAIL; } PASS; }
static bool test_trim_none() { emit_test("Test trim() on a std::string with no white space."); std::string a("Indurain"); trim(a); emit_output_expected_header(); emit_retval("%s", "Indurain"); emit_output_actual_header(); emit_retval("%s", a.c_str()); if(strcmp(a.c_str(), "Indurain") != MATCH) { FAIL; } PASS; }
static bool test_trim_beginning() { emit_test("Test trim() on a std::string with white space at the beginning."); std::string a(" Merckx"); trim(a); emit_output_expected_header(); emit_retval("%s", "Merckx"); emit_output_actual_header(); emit_retval("%s", a.c_str()); if(strcmp(a.c_str(), "Merckx") != MATCH) { FAIL; } PASS; }
static bool test_trim_empty() { emit_test("Test trim() on an empty std::string."); std::string a; trim(a); emit_output_expected_header(); emit_retval("%s", ""); emit_output_actual_header(); emit_retval("%s", a.c_str()); if(strcmp(a.c_str(), "") != MATCH) { FAIL; } PASS; }
static bool tokenize_null() { emit_test("Does calling GetNextToken() before calling Tokenize() return " "NULL?"); const char* tok = GetNextToken(",", false); emit_input_header(); emit_param("delim", "%s", ","); emit_param("skipBlankTokens", "%d", false); emit_output_expected_header(); emit_retval("%s", "NULL"); if(tok != NULL) { FAIL; } PASS; }
static bool test_chomp_return_true() { emit_test("Does chomp() return true if the std::string has a newLine at the " "end?"); std::string a("stuff\n"); bool result = chomp(a); emit_output_expected_header(); emit_retval("%d", 1); emit_output_actual_header(); emit_retval("%d", result); if(!result) { FAIL; } PASS; }
static bool test_chomp_new_line_beginning() { emit_test("Does chomp() do nothing if the newLine if it's not the last " "character in the std::string?"); std::string a("stuff\nmore stuff"); chomp(a); emit_output_expected_header(); emit_retval("%s", "stuff\nmore stuff"); emit_output_actual_header(); emit_retval("%s", a.c_str()); if(strcmp(a.c_str(), "stuff\nmore stuff") != MATCH) { FAIL; } PASS; }
static bool test_chomp_return_false() { emit_test("Does chomp() return false if the std::string doesn't have a " "newLine?"); std::string a("stuff"); bool result = chomp(a); emit_output_expected_header(); emit_retval("%d", 0); emit_output_actual_header(); emit_retval("%d", result); if(result) { FAIL; } PASS; }
static bool test_sprintf_cat_MyString() { emit_test("Test sprintf_cat overloading for MyString"); MyString s = "foo"; int r = sprintf_cat(s, "%s", "bar"); if (s != "foobar") { FAIL; } if (r != 3) { FAIL; } PASS; }
static bool test_chomp_crlf_end() { emit_test("Does chomp() remove CR and LF if they are the last characters" " in the std::string?"); std::string a("stuff\r\n"); chomp(a); emit_output_expected_header(); emit_retval("%s", "stuff"); emit_output_actual_header(); emit_retval("%s", a.c_str()); if(strcmp(a.c_str(), "stuff") != MATCH) { FAIL; } PASS; }
static bool tokenize_empty() { emit_test("Test GetNextToken() on an empty MyString."); Tokenize(""); const char* tok = GetNextToken(" ", false); emit_input_header(); emit_param("delim", "%s", " "); emit_param("skipBlankTokens", "%d", false); emit_output_expected_header(); emit_retval("%s", "NULL"); if(tok != NULL) { FAIL; } PASS; }
static bool test_chomp_new_line_end() { emit_test("Does chomp() remove the newLine if its the last character in " "the std::string?"); std::string a("stuff\n"); chomp(a); emit_output_expected_header(); emit_retval("%s", "stuff"); emit_output_actual_header(); emit_retval("%s", a.c_str()); if(strcmp(a.c_str(), "stuff") != MATCH) { FAIL; } PASS; }
static bool test_colon_forward_slash() { emit_test("Does a path with a colon followed by a forward slash return false?"); const char *param = ":/"; int expected = 0; emit_input_header(); emit_param("STRING", param); int result = fullpath(param); emit_output_expected_header(); emit_retval("%d", expected); emit_output_actual_header(); emit_retval("%d", result); if(expected != result) { FAIL; } PASS; }
static bool test_forward_slash() { emit_test("Does a path starting with a forward slash return true?"); const char *param = "/tmp/foo"; int expected = 1; emit_input_header(); emit_param("STRING", param); int result = fullpath(param); emit_output_expected_header(); emit_retval("%d", expected); emit_output_actual_header(); emit_retval("%d", result); if(expected != result) { FAIL; } PASS; }
static bool test_hostname() { emit_test("Are hostnames instead of IP addresses rejected like they should be?"); char* input = strdup( "<balthazar.cs.wisc.edu:47>" ); emit_input_header(); emit_param("SINFUL", input); int result = is_valid_sinful( input ); free( input ); emit_output_expected_header(); emit_retval("%s", tfstr(FALSE)); emit_output_actual_header(); emit_retval("%s", tfstr(result)); if(result != FALSE) { FAIL; } PASS; }
static bool test_name() { emit_test("Does a path starting with a name return false?"); const char *param = "tmp/foo"; int expected = 0; emit_input_header(); emit_param("STRING", param); int result = fullpath(param); emit_output_expected_header(); emit_retval("%d", expected); emit_output_actual_header(); emit_retval("%d", result); if(expected != result) { FAIL; } PASS; }
static bool test_drive_path_backslash() { emit_test("Does a path with a drive letter followed by a colon and backslash return true?"); const char *param = "c:\\"; int expected = 1; emit_input_header(); emit_param("STRING", param); int result = fullpath(param); emit_output_expected_header(); emit_retval("%d", expected); emit_output_actual_header(); emit_retval("%d", result); if(expected != result) { FAIL; } PASS; }
static bool test_start_wildcard() { emit_test("Does it fail correctly with a wildcard and then some octets?"); char* input = strdup( "*.0.42.1" ); emit_input_header(); emit_param("IP", input); int result = is_ipaddr( input, NULL ); free( input ); emit_output_expected_header(); emit_retval("%s", tfstr(FALSE)); emit_output_actual_header(); emit_retval("%s", tfstr(result)); if(result != FALSE) { FAIL; } PASS; }
static bool test_lower_bound() { emit_test("Does it work with one octet <0?"); char* input = strdup( "-2.71.82.81" ); emit_input_header(); emit_param("IP", input); int result = is_ipaddr( input, NULL ); free( input ); emit_output_expected_header(); emit_retval("%s", tfstr(FALSE)); emit_output_actual_header(); emit_retval("%s", tfstr(result)); if(result != FALSE) { FAIL; } PASS; }
static bool test_no_angle_brackets() { emit_test("Is the string correctly rejected if there are no angle brackets?"); char* input = strdup( "209.172.63.167:8080" ); emit_input_header(); emit_param("SINFUL", input); int result = is_valid_sinful( input ); free( input ); emit_output_expected_header(); emit_retval("%s", tfstr(FALSE)); emit_output_actual_header(); emit_retval("%s", tfstr(result)); if(result != FALSE) { FAIL; } PASS; }
static bool test_backslash() { emit_test("Does a path with only a backslash return true?"); const char *param = "\\"; int expected = 1; emit_input_header(); emit_param("STRING", param); int result = fullpath(param); emit_output_expected_header(); emit_retval("%d", expected); emit_output_actual_header(); emit_retval("%d", result); if(expected != result) { FAIL; } PASS; }
static bool test_only_wildcard() { emit_test("Does it work with nothing but a single wildcard?"); char* input = strdup( "*" ); emit_input_header(); emit_param("IP", input); int result = is_ipaddr( input, NULL ); free( input ); emit_output_expected_header(); emit_retval("%s", tfstr(TRUE)); emit_output_actual_header(); emit_retval("%s", tfstr(result)); if(result != TRUE) { FAIL; } PASS; }
static bool test_normal_case() { emit_test("Is normal input identified correctly?"); char* input = strdup( "<208.122.19.56:47>" ); emit_input_header(); emit_param("SINFUL", input); int result = is_valid_sinful( input ); free( input ); emit_output_expected_header(); emit_retval("%s", tfstr(TRUE)); emit_output_actual_header(); emit_retval("%s", tfstr(result)); if(result != TRUE) { FAIL; } PASS; }
static bool test_one_octet_wildcard() { emit_test("Is it identified as an IP address with one octet and a wildcard?"); char* input = strdup( "66.*" ); emit_input_header(); emit_param("IP", input); int result = is_ipaddr( input, NULL ); free( input ); emit_output_expected_header(); emit_retval("%s", tfstr(TRUE)); emit_output_actual_header(); emit_retval("%s", tfstr(result)); if(result != TRUE) { FAIL; } PASS; }
static bool test_alpha_input() { emit_test("Does an error occur on alpha-only input?"); struct sockaddr_in sa_in; char* input = strdup("Iamafish"); int result = string_to_sin(input, &sa_in); emit_input_header(); emit_param("STRING", input); free(input); emit_output_expected_header(); emit_retval("%s", tfstr(FALSE)); emit_output_actual_header(); emit_retval("%s", tfstr(result)); if(result != 0) { FAIL; } PASS; }
static bool tokenize_end() { emit_test("Test GetNextToken() after getting to the end."); const char *a = "foo;"; Tokenize(a); const char* tok = GetNextToken(";", false); tok = GetNextToken(";", false); tok = GetNextToken(";", false); emit_input_header(); emit_param("delim", "%s", ";"); emit_param("skipBlankTokens", "%d", false); emit_output_expected_header(); emit_retval("%s", "NULL"); if(tok != NULL) { FAIL; } PASS; }