int main (int argc, char * argv[]) { int x; int lo; int hi; int res = 0; lo = 0; hi = (sizeof (the_tests) / sizeof (the_tests[0])) - 1; if (argc > 1) { lo = atoi (argv[1]); hi = lo + 1; if (argc > 2) hi = atoi (argv[2]); } for (x = lo; x < hi; ++x) { printf ("#%d:", x); res |= run_a_test (x, &the_tests[x]); } return res != 0; }
static int test_service(int live, const char *svc) { /* Set environment to force testing of NAMERD (unless it's already set). */ if ( ! is_env_set("CONN_LBSMD_DISABLE")) CORE_REG_SET("", "CONN_LBSMD_DISABLE", "1", eREG_Transient); if ( ! is_env_set("CONN_DISPD_DISABLE")) CORE_REG_SET("", "CONN_DISPD_DISABLE", "1", eREG_Transient); if ( ! is_env_set("CONN_NAMERD_ENABLE")) CORE_REG_SET("", "CONN_NAMERD_ENABLE", "1", eREG_Transient); /* Run the test */ return run_a_test(0, 1 ,svc, s_user_header, 0, 0, NULL, 0, 0) ? 0 : 1; }
int main(int argc, char *argv[]) { int i; int total_lsp_bits = 0; float snr; if (argc != 2) { printf("usage: %s RawFile\n", argv[0]); exit(1); } for(i=0; i<LPC_ORD; i++) total_lsp_bits += lsp_bits(i); for(i=0; i<total_lsp_bits; i++) { snr = run_a_test(argv[1], i); printf("%d %5.2f\n", i, snr); } return 0; }
/* mostly error-handling-free for the moment :-/ */ static int run_tests(int live, const char *test_nums) { x_JSON_Value *root_value = NULL; x_JSON_Object *root_obj; x_JSON_Array *test_arr; const char *test_num = test_nums; size_t n_tests = 0, n_pass = 0, n_fail = 0, n_skip = 0; size_t n_comm_unset, n_comm_set; int all_enabled = 0; CORE_LOG(eLOG_Note, "============================================================"); CORE_LOGF(eLOG_Note, ("Test file: %s", s_json_file)); root_value = x_json_parse_file(s_json_file); root_obj = x_json_value_get_object(root_value); if (x_json_object_has_value_of_type(root_obj, "all_enabled", JSONNumber) && (int)x_json_object_get_number(root_obj, "all_enabled") != 0) { all_enabled = 1; } test_arr = x_json_object_get_array(root_obj, "tests"); n_tests = x_json_array_get_count(test_arr); assert(n_tests == x_json_array_get_count(test_arr)); size_t it; for (it = 0; it < n_tests; ++it) { x_JSON_Object *test_obj; x_JSON_Array *hit_arr; const char *svc, *hdr; int err = 0; test_obj = x_json_array_get_object(test_arr, it); s_results[it].name = x_json_object_get_string(test_obj, "alias"); /* Skip tests not in user-supplied test numbers list. */ if (test_nums && *test_nums) { size_t next_test; if ( ! test_num) break; if (sscanf(test_num, FMT_SIZE_T, &next_test) == 1) { if (it+1 != next_test) { s_results[it].result = "-"; continue; } else { test_num = strchr(test_num, ','); if (test_num && *test_num) test_num++; } } else { CORE_LOGF(eLOG_Error, ("Invalid test numbers list: %s", test_num)); return 0; } } CORE_LOG(eLOG_Note, "============================================================"); /* Skip disabled tests. */ if ( ! all_enabled) { if (x_json_object_has_value_of_type(test_obj, "disabled", JSONNumber)) { if ((int)x_json_object_get_number(test_obj, "disabled") == 1) { ++n_skip; CORE_LOGF(eLOG_Note, ("Skipping test " FMT_SIZE_T ": %s", it+1, x_json_object_get_string(test_obj, "alias"))); s_results[it].result = "skip"; continue; } } } CORE_LOGF(eLOG_Note, ("Running test " FMT_SIZE_T ": %s", it+1, s_results[it].name)); svc = x_json_object_get_string(test_obj, "service"); if (x_json_object_has_value_of_type(test_obj, "http_user_header", JSONString)) hdr = x_json_object_get_string(test_obj, "http_user_header"); else hdr = ""; if (x_json_object_has_value_of_type(test_obj, "expect_error", JSONString)) { if (strcmp(x_json_object_get_string(test_obj, "expect_error"), "yes") == 0) { err = 1; } } if (x_json_object_has_value_of_type(test_obj, "url", JSONString)) CORE_LOGF(eLOG_Note, (" url: %s", x_json_object_get_string(test_obj, "url"))); CORE_LOGF(eLOG_Note, (" service: %s", svc)); CORE_LOGF(eLOG_Note, (" header: %s", *hdr ? hdr : "(none)")); CORE_LOGF(eLOG_Note, (" expected error: %s", err ? "yes" : "no")); /* Redo common unset/set - they could have been changed by a prior test. */ if (x_json_object_dothas_value_of_type(root_obj, "common.env_unset", JSONArray)) { x_JSON_Array *comm_unset_arr; comm_unset_arr = x_json_object_dotget_array(root_obj, "common.env_unset"); n_comm_unset = x_json_array_get_count(comm_unset_arr); for (size_t it2 = 0; it2 < n_comm_unset; ++it2) { const char *name = x_json_array_get_string( comm_unset_arr, it2); CORE_LOGF(eLOG_Note, (" Unsetting common var: %s", name)); unsetenv(name); assert(getenv(name) == NULL); } } if (x_json_object_dothas_value_of_type(root_obj, "common.env_set", JSONArray)) { x_JSON_Array *comm_set_arr; comm_set_arr = x_json_object_dotget_array(root_obj, "common.env_set"); n_comm_set = x_json_array_get_count(comm_set_arr); for (size_t it2 = 0; it2 < n_comm_set; ++it2) { x_JSON_Object *env_obj = x_json_array_get_object(comm_set_arr, it2); const char *name = x_json_object_get_name(env_obj, 0); const char *val = x_json_value_get_string( x_json_object_get_value_at(env_obj, 0)); CORE_LOGF(eLOG_Note, (" Setting common var: %s=%s", name, val)); setenv(name, val, 1); assert(strcmp(val, getenv(name)) == 0); } } /* Per-test unset/set */ if (x_json_object_has_value_of_type(test_obj, "env_unset", JSONArray)) { x_JSON_Array *unset_arr; size_t n_unset; unset_arr = x_json_object_get_array(test_obj, "env_unset"); n_unset = x_json_array_get_count(unset_arr); CORE_LOGF(eLOG_Note, ( " Unset per-test environment variables: " FMT_SIZE_T, n_unset)); if (n_unset) { for (size_t it2 = 0; it2 < n_unset; ++it2) { const char *name = x_json_array_get_string(unset_arr, it2); CORE_LOGF(eLOG_Note, (" %s", name)); unsetenv(name); assert(getenv(name) == NULL); } } } if (x_json_object_has_value_of_type(test_obj, "env_set", JSONArray)) { x_JSON_Array *set_arr; size_t n_set; set_arr = x_json_object_get_array(test_obj, "env_set"); n_set = x_json_array_get_count(set_arr); CORE_LOGF(eLOG_Note, ( " Set per-test environment variables: " FMT_SIZE_T, n_set)); if (n_set) { for (size_t it2 = 0; it2 < n_set; ++it2) { x_JSON_Object *env_obj = x_json_array_get_object(set_arr, it2); const char *name = x_json_object_get_name(env_obj, 0); const char *val = x_json_value_get_string( x_json_object_get_value_at(env_obj, 0)); CORE_LOGF(eLOG_Note, (" %s=%s", name, val)); setenv(name, val, 1); assert(strcmp(val, getenv(name)) == 0); } } } hit_arr = x_json_object_get_array(test_obj, "expect_hits"); s_n_hits_exp = (int)x_json_array_get_count(hit_arr); assert(s_n_hits_exp < MAX_HITS); CORE_LOGF(eLOG_Note, (" Expected hits: %d", s_n_hits_exp)); int it2; for (it2 = 0; it2 < s_n_hits_exp; ++it2) { x_JSON_Object *hit_obj = x_json_array_get_object(hit_arr, it2); const char *type = "HTTP", *loc = "no", *priv = "no"; const char *xtra = "", *stfl = "no"; unsigned short port = 0; if (x_json_object_has_value_of_type(hit_obj, "type", JSONString)) type = x_json_object_get_string(hit_obj, "type"); if (x_json_object_has_value_of_type(hit_obj, "xtra", JSONString)) xtra = x_json_object_get_string(hit_obj, "xtra"); if (x_json_object_has_value_of_type(hit_obj, "loc", JSONString)) loc = x_json_object_get_string(hit_obj, "loc" ); if (x_json_object_has_value_of_type(hit_obj, "priv", JSONString)) priv = x_json_object_get_string(hit_obj, "priv"); if (x_json_object_has_value_of_type(hit_obj, "stfl", JSONString)) stfl = x_json_object_get_string(hit_obj, "stfl"); assert(strlen(type) <= LEN_TYPE); assert(strlen(xtra) <= LEN_XTRA); assert(strlen(loc ) <= LEN_LOC ); assert(strlen(priv) <= LEN_PRIV); assert(strlen(stfl) <= LEN_STFL); strcpy(s_hits_exp[it2].type, type); strcpy(s_hits_exp[it2].xtra, xtra); strcpy(s_hits_exp[it2].loc , loc ); strcpy(s_hits_exp[it2].priv, priv); strcpy(s_hits_exp[it2].stfl, stfl); assert(x_json_object_has_value_of_type(hit_obj, "host", JSONString)); assert(strlen(x_json_object_get_string(hit_obj, "host")) <= LEN_HOST); strcpy(s_hits_exp[it2].host, x_json_object_get_string(hit_obj, "host")); assert(strcmp(s_hits_exp[it2].loc , "no" ) == 0 || strcmp(s_hits_exp[it2].loc , "yes") == 0); assert(strcmp(s_hits_exp[it2].priv, "no" ) == 0 || strcmp(s_hits_exp[it2].priv, "yes") == 0); assert(strcmp(s_hits_exp[it2].stfl, "no" ) == 0 || strcmp(s_hits_exp[it2].stfl, "yes") == 0); assert(x_json_object_has_value_of_type(hit_obj, "port", JSONNumber)); port = (unsigned short)x_json_object_get_number(hit_obj, "port"); assert(port > 0); s_hits_exp[it2].port = port; s_hits_exp[it2].match = 0; CORE_LOGF(eLOG_Note, ( " Expected server %d: %s %s:%hu L=%s P=%s S=%s %s", it2, s_hits_exp[it2].type, s_hits_exp[it2].host, s_hits_exp[it2].port, s_hits_exp[it2].loc, s_hits_exp[it2].priv, s_hits_exp[it2].stfl, s_hits_exp[it2].xtra)); } const char *mock_body = NULL; if (x_json_object_has_value_of_type(test_obj, "mock_body", JSONString)) { mock_body = x_json_object_get_string(test_obj, "mock_body"); if (mock_body && *mock_body) CORE_LOG(eLOG_Note, " Mock HTTP body found."); else CORE_LOG(eLOG_Note, " Empty mock HTTP body found."); } int repop = 0, reset = 0; if (x_json_object_has_value_of_type( test_obj, "iter-repop", JSONString) && strcmp(x_json_object_get_string( test_obj, "iter-repop"), "yes") == 0) { repop = 1; } if (x_json_object_has_value_of_type( test_obj, "iter-reset", JSONString) && strcmp(x_json_object_get_string( test_obj, "iter-reset"), "yes") == 0) { reset = 1; } /* Run the test */ if (run_a_test(it, live, svc, hdr, 1, err, mock_body, repop, reset)) { ++n_pass; s_results[it].result = "ok"; } else { ++n_fail; s_results[it].result = "FAIL"; } } CORE_LOG (eLOG_Note, "============================================================"); CORE_LOGF(eLOG_Note, (FMT_SIZE_T " tests: " FMT_SIZE_T " passed, " FMT_SIZE_T " failed, " FMT_SIZE_T " skipped", n_tests, n_pass, n_fail, n_skip)); CORE_LOG (eLOG_Note, "============================================================"); CORE_LOG (eLOG_Note, "Test Source Result Description"); CORE_LOG (eLOG_Note, "---- ------ ------ ----------------------------------------------"); for (it = 0; it < n_tests; ++it) { if ( ! s_results[it].result || ! *s_results[it].result || *s_results[it].result == '-') { continue; } char tests_buf[6]; if (it+1 > 9999) strcpy (tests_buf, "####"); else sprintf(tests_buf, FMT_SIZE_T, it+1); CORE_LOGF(eLOG_Note, ("%4s %6s %6s %s", tests_buf, s_results[it].live ? "live" : "mock", s_results[it].result, s_results[it].name)); } if (root_value) x_json_value_free(root_value); return (n_tests > 0 && n_pass == n_tests - n_skip) ? 1 : 0; }