Exemplo n.º 1
0
int main(void)
{
	void *ctx = tal_strdup(NULL, "toplevel");
	char *a, *b;
	/* If it accesses this, it will crash. */
	char **invalid = (char **)1L;

	plan_tests(41);
	/* Simple matching. */
	ok1(tal_strreg(ctx, "hello world!", "hello") == true);
	ok1(tal_strreg(ctx, "hello world!", "hi") == false);

	/* No parentheses means we don't use any extra args. */
	ok1(tal_strreg(ctx, "hello world!", "hello", invalid) == true);
	ok1(tal_strreg(ctx, "hello world!", "hi", invalid) == false);

	ok1(tal_strreg(ctx, "hello world!", "[a-z]+", invalid) == true);
	ok1(tal_strreg(ctx, "hello world!", "([a-z]+)", &a, invalid) == true);
	/* Found string */
	ok1(streq(a, "hello"));
	/* Allocated off ctx */
	ok1(find_parent(a, ctx));
	tal_free(a);

	ok1(tal_strreg(ctx, "hello world!", "([a-z]*) ([a-z]+)",
		       &a, &b, invalid) == true);
	ok1(streq(a, "hello"));
	ok1(streq(b, "world"));
	ok1(find_parent(a, ctx));
	ok1(find_parent(b, ctx));
	tal_free(a);
	tal_free(b);

	/* * after parentheses returns last match. */
	ok1(tal_strreg(ctx, "hello world!", "([a-z])* ([a-z]+)",
		       &a, &b, invalid) == true);
	ok1(streq(a, "o"));
	ok1(streq(b, "world"));
	tal_free(a);
	tal_free(b);

	/* Nested parentheses are ordered by open brace. */
	ok1(tal_strreg(ctx, "hello world!", "(([a-z]*) world)",
		       &a, &b, invalid) == true);
	ok1(streq(a, "hello world"));
	ok1(streq(b, "hello"));
	tal_free(a);
	tal_free(b);

	/* Nested parentheses are ordered by open brace. */
	ok1(tal_strreg(ctx, "hello world!", "(([a-z]*) world)",
		       &a, &b, invalid) == true);
	ok1(streq(a, "hello world"));
	ok1(streq(b, "hello"));
	tal_free(a);
	tal_free(b);

	/* NULL means we're not interested. */
	ok1(tal_strreg(ctx, "hello world!", "((hello|goodbye) world)",
		       &a, NULL, invalid) == true);
	ok1(streq(a, "hello world"));
	tal_free(a);

	/* No leaks! */
	ok1(no_children(ctx));

	/* NULL arg with take means always fail. */
	ok1(tal_strreg(ctx, take(NULL), "((hello|goodbye) world)",
		       &b, NULL, invalid) == false);

	/* Take string. */
	a = tal_strdup(ctx, "hello world!");
	ok1(tal_strreg(ctx, take(a), "([a-z]+)", &b, invalid) == true);
	ok1(streq(b, "hello"));
	ok1(tal_parent(b) == ctx);
	tal_free(b);
	ok1(no_children(ctx));

	/* Take regex. */
	a = tal_strdup(ctx, "([a-z]+)");
	ok1(tal_strreg(ctx, "hello world!", take(a), &b, invalid) == true);
	ok1(streq(b, "hello"));
	ok1(tal_parent(b) == ctx);
	tal_free(b);
	ok1(no_children(ctx));

	/* Take both. */
	a = tal_strdup(ctx, "([a-z]+)");
	ok1(tal_strreg(ctx, take(tal_strdup(ctx, "hello world!")),
		       take(a), &b, invalid) == true);
	ok1(streq(b, "hello"));
	ok1(tal_parent(b) == ctx);
	tal_free(b);
	ok1(no_children(ctx));

	/* ... even if we fail to match. */
	a = tal_strdup(ctx, "([a-z]+)");
	ok1(tal_strreg(ctx, take(tal_strdup(ctx, "HELLO WORLD!")),
		       take(a), &b, invalid) == false);
	ok1(no_children(ctx));
	tal_free(ctx);

	/* Don't get fooled by \(! */
	ok1(tal_strreg(ctx, "(hello) (world)!", "\\([a-z]*\\) \\([a-z]+\\)",
		       invalid) == true);

	return exit_status();
}
Exemplo n.º 2
0
int main(void)
{
	struct protocol_double_sha dsha;
	struct protocol_net_address netaddr;
	int fds[2];
	char *p, *mem1, *mem2, *mem3;
	int status;
	size_t maxmem = sizeof(struct log_entry) * 4 + 25 + 25 + 28 + 161;
	void *ctx = tal(NULL, char);
	struct log *log = new_log(ctx, NULL, "PREFIX", LOG_BROKEN+1, maxmem);

	assert(tal_parent(log) == ctx);
	my_time.ts.tv_sec = 1384064855;
	my_time.ts.tv_nsec = 500;

	log_debug(log, "This is a debug %s!", "message");
	my_time.ts.tv_nsec++;
	log_info(log, "This is an info %s!", "message");
	my_time.ts.tv_nsec++;
	log_unusual(log, "This is an unusual %s!", "message");
	my_time.ts.tv_nsec++;
	log_broken(log, "This is a broken %s!", "message");
	my_time.ts.tv_nsec++;

	log_add(log, "the sha is ");
	memset(&dsha, 0xFF, sizeof(dsha));
	log_add_struct(log, struct protocol_double_sha, &dsha);

	log_add(log, " and the address is: ");
	memset(netaddr.addr, 0, 10);
	memset(netaddr.addr + 10, 0xFF, 2);
	netaddr.addr[12] = 127;
	netaddr.addr[13] = 0;
	netaddr.addr[14] = 0;
	netaddr.addr[15] = 1;
	netaddr.port = cpu_to_le16(65000);
	netaddr.time = time_now().ts.tv_sec - 10;
	log_add_struct(log, struct protocol_net_address, &netaddr);

	/* Make child write log, be sure it's correct. */
	pipe(fds);
	switch (fork()) {
	case -1:
		err(1, "forking");
	case 0:
		close(fds[0]);
		setenv("TZ", "UTC", 1);
		log_to_file(fds[1], log);
		tal_free(ctx);
		exit(0);
	}

	close(fds[1]);
	p = read_from(ctx, fds[0]);
	/* Shouldn't contain any NUL chars */
	assert(strlen(p) + 1 == tal_count(p));

	assert(tal_strreg(p, p,
			  "PREFIX ([0-9])* bytes, Sun Nov 10 06:27:35 2013\n"
			  "\\+0\\.000000500 DEBUG: This is a debug message!\n"
			  "\\+0\\.000000501 INFO: This is an info message!\n"
			  "\\+0\\.000000502 UNUSUAL: This is an unusual message!\n"
			  "\\+0\\.000000503 BROKEN: This is a broken message!the sha is ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff and the address is: ::ffff:127\\.0\\.0\\.1:65000 \\(10 seconds old\\)\n\n", &mem1));
	assert(atoi(mem1) < maxmem);
	tal_free(p);

	wait(&status);
	assert(WIFEXITED(status) && WEXITSTATUS(status) == 0);

	/* This cleans us out! */
	log_debug(log, "Overflow!");
	
	/* Make child write log, be sure it's correct. */
	pipe(fds);
	switch (fork()) {
	case -1:
		err(1, "forking");
	case 0:
		close(fds[0]);
		setenv("TZ", "UTC", 1);
		log_to_file(fds[1], log);
		tal_free(ctx);
		exit(0);
	}

	close(fds[1]);

	p = read_from(ctx, fds[0]);
	/* Shouldn't contain any NUL chars */
	assert(strlen(p) + 1 == tal_count(p));

	assert(tal_strreg(p, p,
			  "PREFIX ([0-9]*) bytes, Sun Nov 10 06:27:35 2013\n"
			  "\\.\\.\\. 4 skipped\\.\\.\\.\n"
			  "\\+0.000000504 DEBUG: Overflow!\n"
			  "\\+0.000000504 DEBUG: Log pruned 4 entries \\(mem ([0-9]*) -> ([0-9]*)\\)\n\n", &mem1, &mem2, &mem3));
	assert(atoi(mem1) < maxmem);
	assert(atoi(mem2) >= maxmem);
	assert(atoi(mem3) < maxmem);
	tal_free(ctx);
	wait(&status);
	assert(WIFEXITED(status) && WEXITSTATUS(status) == 0);
	return 0;
}