Exemple #1
0
inline void do_str(const char *src) {
    mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
    if (lex == NULL) {
        tt_abort_msg("Lexer initialization error");
    }

    nlr_buf_t nlr;
    if (nlr_push(&nlr) == 0) {
        qstr source_name = lex->source_name;
        mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT);
        mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, true);
        mp_call_function_0(module_fun);
        nlr_pop();
    } else {
        mp_obj_t exc = (mp_obj_t)nlr.ret_val;
        if (mp_obj_is_subclass_fast(mp_obj_get_type(exc), &mp_type_SystemExit)) {
            // Assume that sys.exit() is called to skip the test.
            // TODO: That can be always true, we should set up convention to
            // use specific exit code as skip indicator.
            tinytest_set_test_skipped_();
            return;
        }
        mp_obj_print_exception(&mp_plat_print, exc);
        tt_abort_msg("Uncaught exception");
    }
end:
    ;
}
Exemple #2
0
static void
thread_basic(void *arg)
{
	THREAD_T threads[NUM_THREADS];
	struct event ev;
	struct timeval tv;
	int i;
	struct basic_test_data *data = arg;
	struct event_base *base = data->base;

	struct event *notification_event = NULL;
	struct event *sigchld_event = NULL;

	EVTHREAD_ALLOC_LOCK(count_lock, 0);
	tt_assert(count_lock);

	tt_assert(base);
	if (evthread_make_base_notifiable(base)<0) {
		tt_abort_msg("Couldn't make base notifiable!");
	}

#ifndef WIN32
	if (data->setup_data && !strcmp(data->setup_data, "forking")) {
		pid_t pid;
		int status;
		sigchld_event = evsignal_new(base, SIGCHLD, sigchld_cb, base);
		/* This piggybacks on the th_notify_fd weirdly, and looks
		 * inside libevent internals.  Not a good idea in non-testing
		 * code! */
		notification_event = event_new(base,
		    base->th_notify_fd[0], EV_READ|EV_PERSIST, notify_fd_cb,
		    NULL);
		event_add(sigchld_event, NULL);
		event_add(notification_event, NULL);

		if ((pid = fork()) == 0) {
			event_del(notification_event);
			if (event_reinit(base) < 0) {
				TT_FAIL(("reinit"));
				exit(1);
			}
			event_assign(notification_event, base,
			    base->th_notify_fd[0], EV_READ|EV_PERSIST,
			    notify_fd_cb, NULL);
			event_add(notification_event, NULL);
	 		goto child;
		}

		event_base_dispatch(base);

		if (waitpid(pid, &status, 0) == -1)
			tt_abort_perror("waitpid");
		TT_BLATHER(("Waitpid okay\n"));

		tt_assert(got_sigchld);
		tt_int_op(notification_fd_used, ==, 0);

		goto end;
	}
Exemple #3
0
static void
NS(logv)(int severity, log_domain_mask_t domain,
  const char *funcname, const char *suffix, const char *format, va_list ap)
{
  switch (CALLED(logv))
  {
    case 0:
      tt_int_op(severity, OP_EQ, LOG_NOTICE);
      tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
      tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
      tt_ptr_op(suffix, OP_EQ, NULL);
      tt_str_op(format, OP_EQ,
          "Heartbeat: It seems like we are not in the cached consensus.");
      break;
    case 1:
      tt_int_op(severity, OP_EQ, LOG_NOTICE);
      tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
      tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
      tt_ptr_op(suffix, OP_EQ, NULL);
      tt_str_op(format, OP_EQ,
          "Heartbeat: Tor's uptime is %s, with %d circuits open. "
          "I've sent %s and received %s.%s");
      tt_str_op(va_arg(ap, char *), OP_EQ, "0:00 hours");  /* uptime */
      tt_int_op(va_arg(ap, int), OP_EQ, 0);  /* count_circuits() */
      tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB");  /* bw_sent */
      tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB");  /* bw_rcvd */
      tt_str_op(va_arg(ap, char *), OP_EQ, "");  /* hibernating */
      break;
    case 2:
      tt_int_op(severity, OP_EQ, LOG_INFO);
      break;
    case 3:
      tt_int_op(severity, OP_EQ, LOG_NOTICE);
      tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
      tt_ptr_op(strstr(funcname, "rep_hist_log_circuit_handshake_stats"),
                OP_NE, NULL);
      tt_ptr_op(suffix, OP_EQ, NULL);
      tt_str_op(format, OP_EQ,
        "Circuit handshake stats since last time: %d/%d TAP, %d/%d NTor.");
      tt_int_op(va_arg(ap, int), OP_EQ, 1);  /* handshakes assigned (TAP) */
      tt_int_op(va_arg(ap, int), OP_EQ, 1);  /* handshakes requested (TAP) */
      tt_int_op(va_arg(ap, int), OP_EQ, 1);  /* handshakes assigned (NTOR) */
      tt_int_op(va_arg(ap, int), OP_EQ, 1);  /* handshakes requested (NTOR) */
      break;
    default:
      tt_abort_msg("unexpected call to logv()");  // TODO: prettyprint args
      break;
  }

  done:
    CALLED(logv)++;
}
Exemple #4
0
inline void do_str(const char *src) {
    mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
    if (lex == NULL) {
        tt_abort_msg("Lexer initialization error");
    }

    mp_parse_error_kind_t parse_error_kind;
    mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT, &parse_error_kind);

    if (pn == MP_PARSE_NODE_NULL) {
        mp_parse_show_exception(lex, parse_error_kind);
        mp_lexer_free(lex);
        tt_abort_msg("Parser error");
    }

    // parse okay
    qstr source_name = mp_lexer_source_name(lex);
    mp_lexer_free(lex);
    mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, true);
    mp_parse_node_free(pn);

    if (module_fun == mp_const_none) {
        tt_abort_msg("Computer error");
    }

    nlr_buf_t nlr;
    if (nlr_push(&nlr) == 0) {
        mp_call_function_0(module_fun);
        nlr_pop();
    } else {
        mp_obj_print_exception((mp_obj_t)nlr.ret_val);
        tt_abort_msg("Uncaught exception");
    }
end:
    ;
}
void
regress_threads(void *arg)
{
	struct event_base *base;
        (void) arg;

	pthread_mutex_init(&count_lock, NULL);

        if (evthread_use_pthreads()<0)
		tt_abort_msg("Couldn't initialize pthreads!");

        base = event_base_new();
        if (evthread_make_base_notifiable(base)<0) {
                tt_abort_msg("Couldn't make base notifiable!");
        }

	pthread_basic(base);

	pthread_mutex_destroy(&count_lock);

	event_base_free(base);
end:
        ;
}
Exemple #6
0
static void
NS(logv)(int severity, log_domain_mask_t domain,
  const char *funcname, const char *suffix, const char *format, va_list ap)
{
  switch (CALLED(logv))
  {
    case 0:
      tt_int_op(severity, OP_EQ, LOG_NOTICE);
      tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
      tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
      tt_ptr_op(suffix, OP_EQ, NULL);
      tt_str_op(format, OP_EQ,
          "Heartbeat: Tor's uptime is %s, with %d circuits open. "
          "I've sent %s and received %s.%s");
      tt_str_op(va_arg(ap, char *), OP_EQ, "0:00 hours");  /* uptime */
      tt_int_op(va_arg(ap, int), OP_EQ, 0);  /* count_circuits() */
      tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB");  /* bw_sent */
      tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB");  /* bw_rcvd */
      tt_str_op(va_arg(ap, char *), OP_EQ, "");  /* hibernating */
      break;
    case 1:
      tt_int_op(severity, OP_EQ, LOG_NOTICE);
      tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
      tt_ptr_op(strstr(funcname, "log_accounting"), OP_NE, NULL);
      tt_ptr_op(suffix, OP_EQ, NULL);
      tt_str_op(format, OP_EQ,
          "Heartbeat: Accounting enabled. Sent: %s / %s, Received: %s / %s. "
          "The current accounting interval ends on %s, in %s.");
      tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB");  /* acc_sent */
      tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB");  /* acc_max */
      tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB");  /* acc_rcvd */
      tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB");  /* acc_max */
      /* format_local_iso_time uses local tz, just check mins and secs. */
      tt_ptr_op(strstr(va_arg(ap, char *), ":01:00"),
                OP_NE, NULL); /* end_buf */
      tt_str_op(va_arg(ap, char *), OP_EQ, "0:01 hours");   /* remaining */
      break;
    case 2:
      tt_int_op(severity, OP_EQ, LOG_INFO);
      break;
    default:
      tt_abort_msg("unexpected call to logv()");  // TODO: prettyprint args
      break;
  }

  done:
    CALLED(logv)++;
}
Exemple #7
0
/* This function is the implementation of every legacy test case.  It
   sets test_ok to 0, invokes the test function, and tells tinytest that
   the test failed if the test didn't set test_ok to 1.
 */
void
run_legacy_test_fn(void *ptr)
{
	struct basic_test_data *data = ptr;
	test_ok = called = 0;

	in_legacy_test_wrapper = 1;
	data->legacy_test_fn(); /* This part actually calls the test */
	in_legacy_test_wrapper = 0;

	if (!test_ok)
		tt_abort_msg("Legacy unit test failed");

end:
	test_ok = 0;
}
Exemple #8
0
/* First, let's see if strcmp is working.  (All your test cases should be
 * functions declared to take a single void * as) an argument. */
void
test_strcmp(void *data)
{
	(void)data; /* This testcase takes no data. */

	/* Let's make sure the empty string is equal to itself */
	if (strcmp("","")) {
		/* This macro tells tinytest to stop the current test
		 * and go straight to the "end" label. */
		tt_abort_msg("The empty string was not equal to itself");
	}

	/* Pretty often, calling tt_abort_msg to indicate failure is more
	   heavy-weight than you want.	Instead, just say: */
	tt_assert(strcmp("testcase", "testcase") == 0);

	/* Occasionally, you don't want to stop the current testcase just
	   because a single assertion has failed.  In that case, use
	   tt_want: */
	tt_want(strcmp("tinytest", "testcase") > 0);

	/* You can use the tt_*_op family of macros to compare values and to
	   fail unless they have the relationship you want.  They produce
	   more useful output than tt_assert, since they display the actual
	   values of the failing things.

	   Fail unless strcmp("abc, "abc") == 0 */
	tt_int_op(strcmp("abc", "abc"), ==, 0);

	/* Fail unless strcmp("abc, "abcd") is less than 0 */
	tt_int_op(strcmp("abc", "abcd"), < , 0);

	/* Incidentally, there's a test_str_op that uses strcmp internally. */
	tt_str_op("abc", <, "abcd");


	/* Every test-case function needs to finish with an "end:"
	   label and (optionally) code to clean up local variables. */
 end:
	;
}
Exemple #9
0
static void
NS(logv)(int severity, log_domain_mask_t domain, const char *funcname,
    const char *suffix, const char *format, va_list ap)
{
  switch (CALLED(logv))
  {
    case 0:
      tt_int_op(severity, OP_EQ, LOG_NOTICE);
      tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
      tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
      tt_ptr_op(suffix, OP_EQ, NULL);
      tt_str_op(format, OP_EQ,
          "Heartbeat: Tor's uptime is %s, with %d circuits open. "
          "I've sent %s and received %s.%s");
      tt_str_op(va_arg(ap, char *), OP_EQ, "0:00 hours");  /* uptime */
      tt_int_op(va_arg(ap, int), OP_EQ, 0);  /* count_circuits() */
      tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB");  /* bw_sent */
      tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB");  /* bw_rcvd */
      tt_str_op(va_arg(ap, char *), OP_EQ, "");  /* hibernating */
      break;
    case 1:
      tt_int_op(severity, OP_EQ, LOG_NOTICE);
      tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
      tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
      tt_ptr_op(suffix, OP_EQ, NULL);
      tt_str_op(format, OP_EQ,
          "Average packaged cell fullness: %2.3f%%. TLS write overhead: %.f%%");
      tt_double_op(fabs(va_arg(ap, double) - 50.0), <=, DBL_EPSILON);
      tt_double_op(fabs(va_arg(ap, double) - 0.0), <=, DBL_EPSILON);
      break;
    default:
      tt_abort_msg("unexpected call to logv()");  // TODO: prettyprint args
      break;
  }

  done:
    CALLED(logv)++;
}
Exemple #10
0
static void
test_evbuffer(void *ptr)
{
	static char buffer[512], *tmp;
	struct evbuffer *evb = evbuffer_new();
	struct evbuffer *evb_two = evbuffer_new();
	size_t sz_tmp;
	int i;

	evbuffer_validate(evb);
	evbuffer_add_printf(evb, "%s/%d", "hello", 1);
	evbuffer_validate(evb);

	tt_assert(evbuffer_get_length(evb) == 7);
	tt_assert(!memcmp((char*)EVBUFFER_DATA(evb), "hello/1", 1));

	evbuffer_add_buffer(evb, evb_two);
	evbuffer_validate(evb);

	evbuffer_drain(evb, strlen("hello/"));
	evbuffer_validate(evb);
	tt_assert(evbuffer_get_length(evb) == 1);
	tt_assert(!memcmp((char*)EVBUFFER_DATA(evb), "1", 1));

	evbuffer_add_printf(evb_two, "%s", "/hello");
	evbuffer_validate(evb);
	evbuffer_add_buffer(evb, evb_two);
	evbuffer_validate(evb);

	tt_assert(evbuffer_get_length(evb_two) == 0);
	tt_assert(evbuffer_get_length(evb) == 7);
	tt_assert(!memcmp((char*)EVBUFFER_DATA(evb), "1/hello", 7) != 0);

	memset(buffer, 0, sizeof(buffer));
	evbuffer_add(evb, buffer, sizeof(buffer));
	evbuffer_validate(evb);
	tt_assert(evbuffer_get_length(evb) == 7 + 512);

	tmp = (char *)evbuffer_pullup(evb, 7 + 512);
	tt_assert(tmp);
	tt_assert(!strncmp(tmp, "1/hello", 7));
	tt_assert(!memcmp(tmp + 7, buffer, sizeof(buffer)));
	evbuffer_validate(evb);

	evbuffer_prepend(evb, "something", 9);
	evbuffer_validate(evb);
	evbuffer_prepend(evb, "else", 4);
	evbuffer_validate(evb);

	tmp = (char *)evbuffer_pullup(evb, 4 + 9 + 7);
	tt_assert(!strncmp(tmp, "elsesomething1/hello", 4 + 9 + 7));
	evbuffer_validate(evb);

	evbuffer_drain(evb, -1);
	evbuffer_validate(evb);
	evbuffer_drain(evb_two, -1);
	evbuffer_validate(evb);

	for (i = 0; i < 3; ++i) {
		evbuffer_add(evb_two, buffer, sizeof(buffer));
		evbuffer_validate(evb_two);
		evbuffer_add_buffer(evb, evb_two);
		evbuffer_validate(evb);
		evbuffer_validate(evb_two);
	}

	tt_assert(evbuffer_get_length(evb_two) == 0);
	tt_assert(evbuffer_get_length(evb) == i * sizeof(buffer));

	/* test remove buffer */
	sz_tmp = (size_t)(sizeof(buffer)*2.5);
	evbuffer_remove_buffer(evb, evb_two, sz_tmp);
	tt_assert(evbuffer_get_length(evb_two) == sz_tmp);
	tt_assert(evbuffer_get_length(evb) == sizeof(buffer) / 2);
	evbuffer_validate(evb);

	if (memcmp(evbuffer_pullup(
			   evb, -1), buffer, sizeof(buffer) / 2) != 0 ||
	    memcmp(evbuffer_pullup(
			   evb_two, -1), buffer, sizeof(buffer) != 0))
		tt_abort_msg("Pullup did not preserve content");

	evbuffer_validate(evb);


	/* testing one-vector reserve and commit */
	{
		struct evbuffer_iovec v[1];
		char *buf;
		int i, j, r;

		for (i = 0; i < 3; ++i) {
			r = evbuffer_reserve_space(evb, 10000, v, 1);
			tt_int_op(r, ==, 1);
			tt_assert(v[0].iov_len >= 10000);
			tt_assert(v[0].iov_base != NULL);

			evbuffer_validate(evb);
			buf = v[0].iov_base;
			for (j = 0; j < 10000; ++j) {
				buf[j] = j;
			}
			evbuffer_validate(evb);

			tt_int_op(evbuffer_commit_space(evb, v, 1), ==, 0);
			evbuffer_validate(evb);

			tt_assert(evbuffer_get_length(evb) >= 10000);

			evbuffer_drain(evb, j * 5000);
			evbuffer_validate(evb);
		}
	}

 end:
	evbuffer_free(evb);
	evbuffer_free(evb_two);
}
Exemple #11
0
void
test_km_readline_realloc (void *ptr)
{
    char *lorem_fn = NULL;
    char *buf = NULL;
    char *smallbuf = NULL;
    const size_t smallbuf_len = 1<<4;
    FILE *fp = NULL;
    ssize_t ret = 0;
    size_t line_num;
    char *nulcp = NULL;
    FILE *nulfp = NULL;
    char *tmpcp = NULL;
    size_t tmpsz = 0;
    size_t our_bufsize = bufsize;
    size_t our_smallbuf_len = smallbuf_len;
    km_test_err = 0;
    /* This should always work, so long as you run it from the right dir */
    lorem_fn = get_test_filename("loremipsum.txt");
    if (lorem_fn == NULL)
        tt_abort_msg("Broken test - get_test_filename failed\n");
    if ((fp = fopen(lorem_fn, "r")) == NULL) {
        fprintf(stderr, "Could not open test file '%s' -- %s\n",
                lorem_fn, strerror(errno));
        tt_skip();
    } else {
        buf = calloc(our_bufsize, sizeof(*buf));
        smallbuf = calloc(our_smallbuf_len, sizeof(*smallbuf));
    }
    for (line_num = 0; line_num < n_loremipsum_lines; line_num++) {
        ret = km_readline_realloc(&buf, fp, &our_bufsize, &test_err_handler);
        tt_int_op(km_test_err, ==, 0);
        tt_int_op(strncmp(buf, loremipsum_lines[line_num], our_bufsize), ==, 0);
        tt_int_op(strlen(buf), ==, loremipsum_line_lens[line_num]);
        tt_int_op(ret, ==, loremipsum_line_lens[line_num]);
        tt_int_op(our_bufsize, ==, bufsize);
        km_test_err = 0;
    }
    ret = km_readline_realloc(&buf, fp, &our_bufsize, &test_err_handler);
    tt_int_op(km_test_err, ==, 0);
    /* check it leaves  \0 in buf */
    tt_int_op(strncmp(buf, "", our_bufsize), ==, 0);
    tt_int_op(strlen(buf), ==, 0);
    tt_int_op(ret, ==, EOF);
    tt_int_op(our_bufsize, ==, bufsize);
    km_test_err = 0;
    /* Naughty tests that try and make it fail */
    rewind(fp);
    /* Null buf */
    ret = km_readline_realloc(&nulcp, fp, &our_bufsize, &test_err_handler);
    tt_int_op(km_test_err, ==, 3);
    tt_int_op(ret, ==, -2);
    tt_int_op(our_bufsize, ==, bufsize);
    km_test_err = 0;
    /* Null fp */
    ret = km_readline_realloc(&buf, nulfp, &our_bufsize, &test_err_handler);
    tt_int_op(km_test_err, ==, 3);
    tt_int_op(ret, ==, -2);
    tt_int_op(our_bufsize, ==, bufsize);
    km_test_err = 0;
    /* Both buf & fp null */
    ret = km_readline_realloc(&nulcp, nulfp, &our_bufsize, &test_err_handler);
    tt_int_op(km_test_err, ==, 3);
    tt_int_op(ret, ==, -2);
    tt_int_op(our_bufsize, ==, bufsize);
    km_test_err = 0;
    /* Test that should require it to resize the buffer */
    rewind(fp);
    ret = km_readline_realloc(&smallbuf, fp, &our_smallbuf_len,
            &test_err_handler);
    tt_int_op(km_test_err, ==, 0);
    tt_int_op(ret, ==, loremipsum_line_lens[0]);
    tt_int_op(strlen(smallbuf), ==, loremipsum_line_lens[0]);
    tmpsz = loremipsum_line_lens[0];
    tt_int_op(our_smallbuf_len, ==, kmroundupz(tmpsz));
end:
    if (lorem_fn != NULL) free(lorem_fn);
    if (buf != NULL) free(buf);
    if (smallbuf != NULL) free(smallbuf);
    if (fp != NULL) fclose(fp);
}