Beispiel #1
0
static int
test_binls(void)
{
	plan(78);
	header();

	test_binl(0x00U, "\xc4\x00", 2);
	test_binl(0x01U, "\xc4\x01", 2);
	test_binl(0x1eU, "\xc4\x1e", 2);
	test_binl(0x1fU, "\xc4\x1f", 2);

	test_binl(0x20U, "\xc4\x20", 2);
	test_binl(0xfeU, "\xc4\xfe", 2);
	test_binl(0xffU, "\xc4\xff", 2);

	test_binl(0x0100U, "\xc5\x01\x00", 3);
	test_binl(0xfffeU, "\xc5\xff\xfe", 3);
	test_binl(0xffffU, "\xc5\xff\xff", 3);

	test_binl(0x00010000U, "\xc6\x00\x01\x00\x00", 5);
	test_binl(0xfffffffeU, "\xc6\xff\xff\xff\xfe", 5);
	test_binl(0xffffffffU, "\xc6\xff\xff\xff\xff", 5);

	footer();
	return check_plan();
}
Beispiel #2
0
int main()
{
	plan(20);
	test_uints();
	test_ints();
	test_bools();
	test_floats();
	test_doubles();
	test_nils();
	test_strls();
	test_binls();
	test_strs();
	test_bins();
	test_arrays();
	test_maps();
	test_next_on_arrays();
	test_next_on_maps();
	test_compare_uints();
	test_format();
	test_mp_print();
	test_mp_check();
	test_numbers();
	test_overflow();

	return check_plan();
}
Beispiel #3
0
int
test(const char *s, const char *scheme, const char *login, const char *password,
     const char *host, const char *service, const char *path,
     const char *query, const char *fragment, int host_hint)
{
	plan(10);

	struct uri uri;
	is(uri_parse(&uri, s), 0, "%s: parse", s);
	/* fprintf(stdout, #key ": %p %d %.*s\n", uri.key,
	   (int) uri.key ## _len, (int) uri.key ## _len, uri.key); */

#define chk(key) do { \
	ok((key && uri.key && strlen(key) == uri.key ## _len && \
	   memcmp(key, uri.key, uri.key ## _len) == 0) || \
	   (!key && !uri.key), "%s: " #key, s); } while (0);

	chk(scheme);
	chk(login);
	chk(password);
	chk(host);
	chk(service);
	chk(path);
	chk(query);
	chk(fragment);
	is(uri.host_hint, host_hint, "%s: host_hint", s);

#undef chk
	return check_plan();
}
Beispiel #4
0
static int
test_ints(void)
{
	plan(153);
	header();

	test_int(-0x01, "\xff", 1);
	test_int(-0x1e, "\xe2", 1);
	test_int(-0x1f, "\xe1", 1);
	test_int(-0x20, "\xe0", 1);
	test_int(-0x21, "\xd0\xdf", 2);

	test_int(-0x7f, "\xd0\x81", 2);
	test_int(-0x80, "\xd0\x80", 2);

	test_int(-0x81, "\xd1\xff\x7f", 3);
	test_int(-0x7fff, "\xd1\x80\x01", 3);
	test_int(-0x8000, "\xd1\x80\x00", 3);

	test_int(-0x8001, "\xd2\xff\xff\x7f\xff", 5);
	test_int(-0x7fffffff, "\xd2\x80\x00\x00\x01", 5);
	test_int(-0x80000000LL, "\xd2\x80\x00\x00\x00", 5);

	test_int(-0x80000001LL,
	     "\xd3\xff\xff\xff\xff\x7f\xff\xff\xff", 9);
	test_int(-0x80000001LL,
	     "\xd3\xff\xff\xff\xff\x7f\xff\xff\xff", 9);
	test_int(-0x7fffffffffffffffLL,
	     "\xd3\x80\x00\x00\x00\x00\x00\x00\x01", 9);
	test_int((int64_t)-0x8000000000000000LL,
	     "\xd3\x80\x00\x00\x00\x00\x00\x00\x00", 9);

	footer();
	return check_plan();
}
Beispiel #5
0
static int
test_compare_uints(void)
{
	plan(227);
	header();

	test_compare_uint(0, 0);
	test_compare_uint(0, 0);

	uint64_t nums[] = {
		0, 1, 0x7eU, 0x7fU, 0x80U, 0xfeU, 0xffU, 0xfffeU, 0xffffU,
		0x10000U, 0xfffffffeU, 0xffffffffU, 0x100000000ULL,
		0xfffffffffffffffeULL, 0xffffffffffffffffULL
	};

	int count = sizeof(nums) / sizeof(*nums);
	for (int i = 0; i < count; i++) {
		for (int j = 0; j < count; j++) {
			test_compare_uint(nums[i], nums[j]);
		}
	}

	footer();
	return check_plan();
}
Beispiel #6
0
static void
base64_test(const char *str, int options, const char *no_symbols,
	    int no_symbols_len)
{
	plan(3 + no_symbols_len);

	int len = strlen(str);
	int base64_buflen = base64_bufsize(len + 1, options);
	char *base64_buf = malloc(base64_buflen);
	char *strbuf = malloc(len + 1);
	int rc = base64_encode(str, len + 1, base64_buf, base64_buflen,
			       options);
	ok(rc <= base64_buflen, "length");
	for (int i = 0; i < no_symbols_len; ++i) {
		char c = no_symbols[i];
		if (c == '\n') {
			is(memchr(base64_buf, no_symbols[i], base64_buflen),
			   NULL, "no \\n symbols");
		} else {
			is(memchr(base64_buf, no_symbols[i], base64_buflen),
			   NULL, "no %c symbols", no_symbols[i]);
		}
	}

	is(base64_decode(base64_buf, rc, strbuf, len + 1), len + 1,
	   "decode length ok");
	is(strcmp(str, strbuf), 0, "encode/decode");

	free(base64_buf);
	free(strbuf);

	check_plan();
}
Beispiel #7
0
static int
test_strls(void)
{
	plan(78);
	header();

	test_strl(0x00U, "\xa0", 1);
	test_strl(0x01U, "\xa1", 1);
	test_strl(0x1eU, "\xbe", 1);
	test_strl(0x1fU, "\xbf", 1);

	test_strl(0x20U, "\xd9\x20", 2);
	test_strl(0xfeU, "\xd9\xfe", 2);
	test_strl(0xffU, "\xd9\xff", 2);

	test_strl(0x0100U, "\xda\x01\x00", 3);
	test_strl(0xfffeU, "\xda\xff\xfe", 3);
	test_strl(0xffffU, "\xda\xff\xff", 3);

	test_strl(0x00010000U, "\xdb\x00\x01\x00\x00", 5);
	test_strl(0xfffffffeU, "\xdb\xff\xff\xff\xfe", 5);
	test_strl(0xffffffffU, "\xdb\xff\xff\xff\xff", 5);

	footer();
	return check_plan();
}
Beispiel #8
0
static int
test_uints(void)
{
	plan(135);
	header();

	test_uint(0U, "\x00", 1);
	test_uint(1U, "\x01", 1);
	test_uint(0x7eU, "\x7e", 1);
	test_uint(0x7fU, "\x7f", 1);

	test_uint(0x80U, "\xcc\x80", 2);
	test_uint(0xfeU, "\xcc\xfe", 2);
	test_uint(0xffU, "\xcc\xff", 2);

	test_uint(0xfffeU, "\xcd\xff\xfe", 3);
	test_uint(0xffffU, "\xcd\xff\xff", 3);

	test_uint(0x10000U, "\xce\x00\x01\x00\x00", 5);
	test_uint(0xfffffffeU, "\xce\xff\xff\xff\xfe", 5);
	test_uint(0xffffffffU, "\xce\xff\xff\xff\xff", 5);

	test_uint(0x100000000ULL,
	     "\xcf\x00\x00\x00\x01\x00\x00\x00\x00", 9);
	test_uint(0xfffffffffffffffeULL,
	     "\xcf\xff\xff\xff\xff\xff\xff\xff\xfe", 9);
	test_uint(0xffffffffffffffffULL,
	     "\xcf\xff\xff\xff\xff\xff\xff\xff\xff", 9);

	footer();
	return check_plan();
}
Beispiel #9
0
void
test_basic()
{
	header();
	plan(71);
	const char *path;
	int len;
	struct json_lexer lexer;
	struct json_token token;

	reset_to_new_path("[1].field1.field2['field3'][5]");
	is_next_index(3, 0);
	is_next_key("field1");
	is_next_key("field2");
	is_next_key("field3");
	is_next_index(3, 4);

	reset_to_new_path("[3].field[2].field")
	is_next_index(3, 2);
	is_next_key("field");
	is_next_index(3, 1);
	is_next_key("field");

	reset_to_new_path("[\"f1\"][\"f2'3'\"]");
	is_next_key("f1");
	is_next_key("f2'3'");

	/* Support both '.field1...' and 'field1...'. */
	reset_to_new_path(".field1");
	is_next_key("field1");
	reset_to_new_path("field1");
	is_next_key("field1");

	/* Long number. */
	reset_to_new_path("[1234]");
	is_next_index(6, 1233);

	/* Empty path. */
	reset_to_new_path("");
	is(json_lexer_next_token(&lexer, &token), 0, "parse empty path");
	is(token.type, JSON_TOKEN_END, "is str");

	/* Path with no '.' at the beginning. */
	reset_to_new_path("field1.field2");
	is_next_key("field1");

	/* Unicode. */
	reset_to_new_path("[2][6]['привет中国world']['中国a']");
	is_next_index(3, 1);
	is_next_index(3, 5);
	is_next_key("привет中国world");
	is_next_key("中国a");

	check_plan();
	footer();
}
Beispiel #10
0
int
test_invalid()
{
	plan(2);

	/* Invalid */
	struct uri u;
	isnt(uri_parse(&u, ""), 0 , "empty is invalid");
	isnt(uri_parse(&u, "://"), 0 , ":// is invalid");

	return check_plan();
}
Beispiel #11
0
static int
test_bools(void)
{
	plan(18);
	header();

	test_bool(true, "\xc3", 1);
	test_bool(false, "\xc2", 1);

	footer();
	return check_plan();
}
Beispiel #12
0
int
main()
{
	plan(7);
	memory_init();
	fiber_init(fiber_c_invoke);
	struct fiber *f = fiber_new("main", main_f);
	fiber_wakeup(f);
	ev_run(loop(), 0);
	fiber_free();
	memory_free();
	return check_plan();
}
Beispiel #13
0
static int
test_floats(void)
{
	plan(27);
	header();

	test_float((float) 1.0, "\xca\x3f\x80\x00\x00", 5);
	test_float((float) 3.141593, "\xca\x40\x49\x0f\xdc", 5);
	test_float((float) -1e38f, "\xca\xfe\x96\x76\x99", 5);

	footer();
	return check_plan();
}
Beispiel #14
0
static int
test_numbers()
{
	plan(96);
	header();

	test_read_int32(uint, 123, true);
	test_read_int32(uint, 12345, true);
	test_read_int32(uint, 2147483647, true);
	test_read_int32(uint, 2147483648, false);
	test_read_int32(int, -123, true);
	test_read_int32(int, -12345, true);
	test_read_int32(int, -2147483648, true);
	test_read_int32(int, -2147483649LL, false);
	test_read_int32(float, -1e2, false);
	test_read_int32(double, 1.2345, false);
	test_read_int32(map, 5, false);

	test_read_int64(uint, 123, true);
	test_read_int64(uint, 12345, true);
	test_read_int64(uint, 123456789, true);
	test_read_int64(uint, 9223372036854775807ULL, true);
	test_read_int64(uint, 9223372036854775808ULL, false);
	test_read_int64(int, -123, true);
	test_read_int64(int, -12345, true);
	test_read_int64(int, -123456789, true);
	test_read_int64(int, -9223372036854775807LL, true);
	test_read_int64(float, 100, false);
	test_read_int64(double, -5.4321, false);
	test_read_int64(array, 10, false);

	test_read_double(uint, 123, true);
	test_read_double(uint, 12345, true);
	test_read_double(uint, 123456789, true);
	test_read_double(uint, 1234567890000ULL, true);
	test_read_double(uint, 123456789123456789ULL, false);
	test_read_double(int, -123, true);
	test_read_double(int, -12345, true);
	test_read_double(int, -123456789, true);
	test_read_double(int, -1234567890000LL, true);
	test_read_double(int, -123456789123456789LL, false);
	test_read_double(float, 6.565e6, true);
	test_read_double(double, -5.555, true);
	test_read_double(strl, 100, false);

	footer();
	return check_plan();
}
Beispiel #15
0
static int
test_doubles(void)
{
	plan(27);
	header();

	test_double((double) 1.0,
		    "\xcb\x3f\xf0\x00\x00\x00\x00\x00\x00", 9);
	test_double((double) 3.141592653589793,
		    "\xcb\x40\x09\x21\xfb\x54\x44\x2d\x18", 9);
	test_double((double) -1e99,
		    "\xcb\xd4\x7d\x42\xae\xa2\x87\x9f\x2e", 9);

	footer();
	return check_plan();
}
Beispiel #16
0
static int
test_arrays(void)
{
	plan(54);
	header();

	test_array(0, "\x90", 1);
	test_array(1, "\x91", 1);
	test_array(15, "\x9f", 1);
	test_array(16, "\xdc\x00\x10", 3);
	test_array(0xfffe, "\xdc\xff\xfe", 3);
	test_array(0xffff, "\xdc\xff\xff", 3);
	test_array(0x10000, "\xdd\x00\x01\x00\x00", 5);
	test_array(0xfffffffeU, "\xdd\xff\xff\xff\xfe", 5);
	test_array(0xffffffffU, "\xdd\xff\xff\xff\xff", 5);

	footer();
	return check_plan();
}
Beispiel #17
0
static int
test_maps(void)
{
	plan(54);
	header();

	test_map(0, "\x80", 1);
	test_map(1, "\x81", 1);
	test_map(15, "\x8f", 1);
	test_map(16, "\xde\x00\x10", 3);
	test_map(UINT16_MAX - 1, "\xde\xff\xfe", 3);
	test_map(UINT16_MAX, "\xde\xff\xff", 3);
	test_map(UINT16_MAX + 1, "\xdf\x00\x01\x00\x00", 5);
	test_map(UINT32_MAX - 1, "\xdf\xff\xff\xff\xfe", 5);
	test_map(UINT32_MAX, "\xdf\xff\xff\xff\xff", 5);

	footer();
	return check_plan();
}
Beispiel #18
0
static int
test_arrays(void)
{
	plan(54);
	header();

	test_array(0, "\x90", 1);
	test_array(1, "\x91", 1);
	test_array(15, "\x9f", 1);
	test_array(16, "\xdc\x00\x10", 3);
	test_array(UINT16_MAX - 1, "\xdc\xff\xfe", 3);
	test_array(UINT16_MAX, "\xdc\xff\xff", 3);
	test_array(UINT16_MAX + 1, "\xdd\x00\x01\x00\x00", 5);
	test_array(UINT32_MAX - 1, "\xdd\xff\xff\xff\xfe", 5);
	test_array(UINT32_MAX, "\xdd\xff\xff\xff\xff", 5);

	footer();
	return check_plan();
}
Beispiel #19
0
static int
test_maps(void)
{
	plan(54);
	header();

	test_map(0, "\x80", 1);
	test_map(1, "\x81", 1);
	test_map(15, "\x8f", 1);
	test_map(16, "\xde\x00\x10", 3);
	test_map(0xfffe, "\xde\xff\xfe", 3);
	test_map(0xffff, "\xde\xff\xff", 3);
	test_map(0x10000, "\xdf\x00\x01\x00\x00", 5);
	test_map(0xfffffffeU, "\xdf\xff\xff\xff\xfe", 5);
	test_map(0xffffffffU, "\xdf\xff\xff\xff\xff", 5);

	footer();
	return check_plan();
}
Beispiel #20
0
static int
test_strs(void)
{
	plan(84);
	header();

	test_str(0x01);
	test_str(0x1e);
	test_str(0x1f);
	test_str(0x20);
	test_str(0xfe);
	test_str(0xff);
	test_str(0x100);
	test_str(0x101);
	test_str(0xfffe);
	test_str(0xffff);
	test_str(0x10000);
	test_str(0x10001);

	footer();
	return check_plan();
}
Beispiel #21
0
int main()
{
    plan(15);

    test_uints();
    test_ints();
    test_bools();
    test_floats();
    test_doubles();
    test_nils();
    test_strls();
    test_binls();
    test_strs();
    test_bins();
    test_arrays();
    test_maps();
    test_next_on_arrays();
    test_next_on_maps();
    test_compare_uints();

    return check_plan();
}
Beispiel #22
0
static int
test_bins(void)
{
	plan(96);
	header();

	test_bin(0x01);
	test_bin(0x1e);
	test_bin(0x1f);
	test_bin(0x20);
	test_bin(0xfe);
	test_bin(0xff);
	test_bin(0x100);
	test_bin(0x101);
	test_bin(0xfffe);
	test_bin(0xffff);
	test_bin(0x10000);
	test_bin(0x10001);

	footer();
	return check_plan();
}
Beispiel #23
0
int main(int argc, char *argv[])
{
	plan(28);
	header();

	const char *option_tests[] = {
		"", "a", "123", "1234567", "12345678",
		"\001\002\003\004\005\006\253\254\255",
		"Test +/+/+/ test test test test test test test test test "\
		"test test test test test test test test test test test test "\
		"test test test test test test test test test test test test "\
		"test test test test test test test test test test\n\n"
	};
	for (size_t i = 0; i < lengthof(option_tests); ++i) {
		base64_test(option_tests[i], 0, NULL, 0);
		base64_urlsafe_test(option_tests[i]);
		base64_nopad_test(option_tests[i]);
		base64_nowrap_test(option_tests[i]);
	}

	footer();
	return check_plan();
}
Beispiel #24
0
static int
test_next_on_maps(void)
{
	plan(52);
	header();

	test_next_on_map(0x00);
	test_next_on_map(0x01);
	test_next_on_map(0x0f);
	test_next_on_map(0x10);
	test_next_on_map(0x11);
	test_next_on_map(0xfe);
	test_next_on_map(0xff);
	test_next_on_map(0x100);
	test_next_on_map(0x101);
	test_next_on_map(0xfffe);
	test_next_on_map(0xffff);
	test_next_on_map(0x10000);
	test_next_on_map(0x10001);

	footer();
	return check_plan();
}
Beispiel #25
0
static int
test_nils(void)
{
	plan(6);
	header();

	const char *d1 = mp_encode_nil(data);
	const char *d2 = data;
	const char *d3 = data;
	const char *d4 = data;
	note("nil");
	mp_decode_nil(&d2);
	mp_next(&d3);
	ok(!mp_check(&d4, d3 + 1), "mp_check_nil()");
	is((d1 - data), 1, "len(mp_encode_nil() == 1");
	is(d1, d2, "len(mp_decode_nil()) == 1");
	is(d1, d3, "len(mp_next_nil()) == 1");
	is(d1, d4, "len(mp_check_nil()) == 1");
	is(mp_sizeof_nil(), 1, "mp_sizeof_nil() == 1");

	footer();
	return check_plan();
}
Beispiel #26
0
static int
test_overflow()
{
	plan(4);
	header();

	const char *chk;
	char *d;
	d = data;
	chk = data;
	d = mp_encode_array(d, 1);
	d = mp_encode_array(d, UINT32_MAX);
	is(mp_check(&chk, d), 1, "mp_check array overflow")

	d = data;
	chk = data;
	d = mp_encode_array(d, 1);
	d = mp_encode_map(d, UINT32_MAX);
	is(mp_check(&chk, d), 1, "mp_check map overflow")

	d = data;
	chk = data;
	d = mp_encode_array(d, 2);
	d = mp_encode_str(d, "", 0);
	d = mp_encode_strl(d, UINT32_MAX);
	is(mp_check(&chk, d), 1, "mp_check str overflow")

	d = data;
	chk = data;
	d = mp_encode_array(d, 2);
	d = mp_encode_bin(d, "", 0);
	d = mp_encode_binl(d, UINT32_MAX);
	is(mp_check(&chk, d), 1, "mp_check bin overflow")

	footer();
	return check_plan();
}
Beispiel #27
0
int
main(void)
{
	plan(PLAN);

	char *td = mkdtemp(strdup("/tmp/fiob.XXXXXX"));
	isnt(td, NULL, "tempdir is created");
	if (td == 0) {
		diag("Can't create temporary dir: %s", strerror(errno));
		return -1;
	}

	static char buf[4096];

	{

		FILE *f = fiob_open(catfile(td, "t0"), "w+d");
		isnt(f, NULL, "common open");
		size_t done = fwrite("Hello, world", 1, 12, f);
		is(done, 12, "Hello world is written (%zu bytes)", done);

		is(ftell(f), 12, "current position");
		is(fseek(f, 0L, SEEK_SET), 0, "set new position");
		is(ftell(f), 0, "current position %li", ftell(f));


		done = fread(buf, 1, 12, f);
		is(done, 12, "Hello world is read (%zu bytes)", done);
		is(memcmp(buf, "Hello, world", 12), 0, "data");

		is(fseek(f, 0L, SEEK_SET), 0, "set new position");
		done = fread(buf + 1, 1, 12, f);
		is(done, 12, "Hello world is read (%zu bytes)", done);
		is(memcmp(buf + 1, "Hello, world", 12), 0, "data");


		is(fseek(f, 0L, SEEK_SET), 0, "set new position");
		fwrite("ololo ololo ololo", 1, 17, f);
		is(fseek(f, 1L, SEEK_SET), 0, "set new position");

		done = fread(buf + 1, 1, 12, f);
		is(done, 12, "data is read");
		is(memcmp(buf + 1, "lolo ololo ololo", 12), 0, "data is read");

		is(fclose(f), 0, "fclose");

		f = fopen(catfile(td, "t0"), "r");
		isnt(f, NULL, "reopened file");
		is(fseek(f, 0L, SEEK_END), 0, "move pos at finish");
		is(ftell(f), 17, "file size");
		is(fclose(f), 0, "fclose");

		f = fiob_open(catfile(td, "t0"), "w+x");
		is(f, NULL, "common open: O_EXCL");
	}

	{
		FILE *f = fiob_open(catfile(td, "t1"), "w+");
		isnt(f, NULL, "common open");
		size_t done = fwrite("Hello, world", 1, 12, f);
		is(done, 12, "Hello world is written (%zu bytes)", done);

		is(fseek(f, 1, SEEK_SET), 0, "move pos");
		done = fwrite("Hello, world", 1, 12, f);
		is(done, 12, "Hello world is written (%zu bytes)", done);

		is(fseek(f, 2, SEEK_SET), 0, "move pos");
		done = fread(buf, 1, 12, f);
		is(done, 11, "read 11 bytes");
		is(memcmp(buf, "ello, world", 11), 0, "content was read");

		is(fclose(f), 0, "fclose");
	}

	{
		FILE *f = fiob_open(catfile(td, "tm"), "wxd");
		isnt(f, NULL, "open big file");
		size_t done = fwrite("Hello, world\n", 1, 13, f);
		is(done, 13, "Hello world is written (%zu bytes)", done);

		size_t i;

		for (i = 0; i < 1000000; i++) {
			done += fwrite("Hello, world\n", 1, 13, f);
		}
		is(done, 13 + 13 * 1000000, "all bytes were written");
		is(fclose(f), 0, "fclose");

		f = fopen(catfile(td, "tm"), "r");
		isnt(f, NULL, "reopen file for reading");
		done = 0;
		for (i = 0; i < 1000000 + 1; i++) {
			buf[0] = 0;
			fgets(buf, 4096, f);
			if (strcmp(buf, "Hello, world\n") == 0)
				done++;
		}
		is(done, 1000000 + 1, "all records were written properly");

		is(fgets(buf, 4096, f), NULL, "eof");
		isnt(feof(f), 0, "feof");
		is(fclose(f), 0, "fclose");
	}
	{
		FILE *f = fiob_open(catfile(td, "tm"), "w+d");
		setvbuf(f, NULL, _IONBF, 0);
		isnt(f, NULL, "open big file");
		size_t done = fwrite("Hello, world\n", 1, 13, f);
		is(done, 13, "Hello world is written (%zu bytes)", done);

		size_t i;

		for (i = 0; i < 1000000; i++) {
			done += fwrite("Hello, world\n", 1, 13, f);
		}
		is(done, 13 + 13 * 1000000, "all bytes were written");
		is(fclose(f), 0, "fclose");

		f = fopen(catfile(td, "tm"), "r");
		isnt(f, NULL, "reopen file for reading");
		done = 0;
		for (i = 0; i < 1000000 + 1; i++) {
			memset(buf, 0, 4096);
			fgets(buf, 4096, f);
			if (strcmp(buf, "Hello, world\n") == 0)
				done++;
/*                         else */
/*                                 fprintf(stderr, "#   wrong line %zu: %s", */
/*                                         i, buf); */
		}
		is(done, 1000000 + 1, "all records were written properly");

		is(fgets(buf, 4096, f), NULL, "eof");
		isnt(feof(f), 0, "feof");
		is(fclose(f), 0, "fclose");
	}



	if (fork() == 0)
		execl("/bin/rm", "/bin/rm", "-fr", td, NULL);

	free(td);
	return check_plan();
}
Beispiel #28
0
int
test_mp_check()
{
	plan(65);
	header();

#define invalid(data, fmt, ...) ({ \
	const char *p = data; \
	isnt(mp_check(&p, p + sizeof(data) - 1), 0, fmt, ## __VA_ARGS__); \
});

	/* fixmap */
	invalid("\x81", "invalid fixmap 1");
	invalid("\x81\x01", "invalid fixmap 2");
	invalid("\x8f\x01", "invalid fixmap 3");

	/* fixarray */
	invalid("\x91", "invalid fixarray 1");
	invalid("\x92\x01", "invalid fixarray 2");
	invalid("\x9f\x01", "invalid fixarray 3");

	/* fixstr */
	invalid("\xa1", "invalid fixstr 1");
	invalid("\xa2\x00", "invalid fixstr 2");
	invalid("\xbf\x00", "invalid fixstr 3");

	/* bin8 */
	invalid("\xc4", "invalid bin8 1");
	invalid("\xc4\x01", "invalid bin8 2");

	/* bin16 */
	invalid("\xc5", "invalid bin16 1");
	invalid("\xc5\x00\x01", "invalid bin16 2");

	/* bin32 */
	invalid("\xc6", "invalid bin32 1");
	invalid("\xc6\x00\x00\x00\x01", "invalid bin32 2");

	/* ext8 */
	invalid("\xc7", "invalid ext8 1");
	invalid("\xc7\x00", "invalid ext8 2");
	invalid("\xc7\x01\xff", "invalid ext8 3");
	invalid("\xc7\x02\xff\x00", "invalid ext8 4");

	/* ext16 */
	invalid("\xc8", "invalid ext16 1");
	invalid("\xc8\x00\x00", "invalid ext16 2");
	invalid("\xc8\x00\x01\xff", "invalid ext16 3");
	invalid("\xc8\x00\x02\xff\x00", "invalid ext16 4");

	/* ext32 */
	invalid("\xc9", "invalid ext32 1");
	invalid("\xc9\x00\x00\x00\x00", "invalid ext32 2");
	invalid("\xc9\x00\x00\x00\x01\xff", "invalid ext32 3");
	invalid("\xc9\x00\x00\x00\x02\xff\x00", "invalid ext32 4");

	/* float32 */
	invalid("\xca", "invalid float32 1");
	invalid("\xca\x00\x00\x00", "invalid float32 2");

	/* float64 */
	invalid("\xcb", "invalid float64 1");
	invalid("\xcb\x00\x00\x00\x00\x00\x00\x00", "invalid float64 2");

	/* uint8 */
	invalid("\xcc", "invalid uint8 1");

	/* uint16 */
	invalid("\xcd\x00", "invalid uint16 1");

	/* uint32 */
	invalid("\xce\x00\x00\x00", "invalid uint32 1");

	/* uint64 */
	invalid("\xcf\x00\x00\x00\x00\x00\x00\x00", "invalid uint64 1");

	/* int8 */
	invalid("\xd0", "invalid int8 1");

	/* int16 */
	invalid("\xd1\x00", "invalid int16 1");

	/* int32 */
	invalid("\xd2\x00\x00\x00", "invalid int32 1");

	/* int64 */
	invalid("\xd3\x00\x00\x00\x00\x00\x00\x00", "invalid int64 1");

	/* fixext8 */
	invalid("\xd4", "invalid fixext8 1");
	invalid("\xd4\x05", "invalid fixext8 2");

	/* fixext16 */
	invalid("\xd5", "invalid fixext16 1");
	invalid("\xd5\x05\x05", "invalid fixext16 2");

	/* fixext32 */
	invalid("\xd6", "invalid fixext32 1");
	invalid("\xd6\x00\x00\x05\x05", "invalid fixext32 2");

	/* fixext64 */
	invalid("\xd7", "invalid fixext64 1");
	invalid("\xd7\x00\x00\x00\x00\x00\x00\x05\x05", "invalid fixext64 2");

	/* fixext128 */
	invalid("\xd8", "invalid fixext128 1");
	invalid("\xd8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
		"\x00\x05\x05", "invalid fixext128 2");

	/* str8 */
	invalid("\xd9", "invalid str8 1");
	invalid("\xd9\x01", "invalid str8 2");

	/* str16 */
	invalid("\xda", "invalid str16 1");
	invalid("\xda\x00\x01", "invalid str16 2");

	/* str32 */
	invalid("\xdb", "invalid str32 1");
	invalid("\xdb\x00\x00\x00\x01", "invalid str32 2");

	/* array16 */
	invalid("\xdc", "invalid array16 1");
	invalid("\xdc\x00\x01", "invalid array16 2");

	/* array32 */
	invalid("\xdd", "invalid array32 1");
	invalid("\xdd\x00\x00\x00\x01", "invalid array32 2");

	/* map16 */
	invalid("\xde", "invalid map16 1");
	invalid("\xde\x00\x01", "invalid map16 2");
	invalid("\xde\x00\x01\x5", "invalid map16 2");

	/* map32 */
	invalid("\xdf", "invalid map32 1");
	invalid("\xdf\x00\x00\x00\x01", "invalid map32 2");
	invalid("\xdf\x00\x00\x00\x01\x5", "invalid map32 3");

	footer();

	return check_plan();
}
Beispiel #29
0
int
test_mp_print()
{
	plan(10);
	header();

	char msgpack[128];
	char *d = msgpack;
	d = mp_encode_array(d, 6);
	d = mp_encode_int(d, -5);
	d = mp_encode_uint(d, 42);
	d = mp_encode_str(d, "kill bill", 9);
	d = mp_encode_map(d, 6);
	d = mp_encode_str(d, "bool true", 9);
	d = mp_encode_bool(d, true);
	d = mp_encode_str(d, "bool false", 10);
	d = mp_encode_bool(d, false);
	d = mp_encode_str(d, "null", 4);
	d = mp_encode_nil(d);
	d = mp_encode_str(d, "float", 5);
	d = mp_encode_float(d, 3.14);
	d = mp_encode_str(d, "double", 6);
	d = mp_encode_double(d, 3.14);
	d = mp_encode_uint(d, 100);
	d = mp_encode_uint(d, 500);
	*d++ = 0xd4; /* let's pack smallest fixed ext */
	*d++ = 0;
	*d++ = 0;
	char bin[] = "\x12test\x34\b\t\n\"bla\\-bla\"\f\r";
	d = mp_encode_bin(d, bin, sizeof(bin));
	assert(d <= msgpack + sizeof(msgpack));

	const char *expected =
		"[-5, 42, \"kill bill\", "
		"{\"bool true\": true, \"bool false\": false, \"null\": null, "
		"\"float\": 3.14, \"double\": 3.14, 100: 500}, undefined, "
		"\"\\u0012test4\\b\\t\\n\\\"bla\\\\-bla\\\"\\f\\r\\u0000\"]";
	int esize = strlen(expected);

	char result[256];

	int fsize = mp_snprint(result, sizeof(result), msgpack);
	ok(fsize == esize, "mp_snprint return value");
	ok(strcmp(result, expected) == 0, "mp_snprint result");

	fsize = mp_snprint(NULL, 0, msgpack);
	ok(fsize == esize, "mp_snprint limit = 0");

	fsize = mp_snprint(result, 1, msgpack);
	ok(fsize == esize && result[0] == '\0', "mp_snprint limit = 1");

	fsize = mp_snprint(result, 2, msgpack);
	ok(fsize == esize && result[1] == '\0', "mp_snprint limit = 2");

	fsize = mp_snprint(result, esize, msgpack);
	ok(fsize == esize && result[esize - 1] == '\0',
	   "mp_snprint limit = expected");

	fsize = mp_snprint(result, esize + 1, msgpack);
	ok(fsize == esize && result[esize] == '\0',
	   "mp_snprint limit = expected + 1");

	FILE *tmpf = tmpfile();
	if (tmpf != NULL) {
		int fsize = mp_fprint(tmpf, msgpack);
		ok(fsize == esize, "mp_fprint return value");
		(void) rewind(tmpf);
		int rsize = fread(result, 1, sizeof(result), tmpf);
		ok(rsize == esize && memcmp(result, expected, esize) == 0,
		   "mp_fprint result");
		fclose(tmpf);
	}

	/* stdin is read-only */
	int rc = mp_fprint(stdin, msgpack);
	is(rc, -1, "mp_fprint I/O error");

	footer();
	return check_plan();
}
Beispiel #30
0
static int
test_format(void)
{
	plan(282);
	header();

	const size_t buf_size = 1024;
	char buf[buf_size];
	size_t sz;
	const char *fmt;
	const char *p, *c, *e;
	uint32_t len = 0;

	fmt = "%d %u %i  %ld %lu %li  %lld %llu %lli"
	      "%hd %hu %hi  %hhd %hhu %hhi";
	sz = mp_format(buf, buf_size, fmt, 1, 2, 3,
		       (long)4, (long)5, (long)6,
		       (long long)7, (long long)8, (long long)9,
		       (short)10, (short)11, (short)12,
		       (char)13, (char)14, (char)15);
	p = buf;
	for (unsigned i = 0; i < 15; i++) {
		ok(mp_typeof(*p) == MP_UINT, "Test type on step %d", i);
		ok(mp_decode_uint(&p) == i + 1, "Test value on step %d", i);
	}
	sz = mp_format(buf, buf_size, fmt, -1, -2, -3,
		       (long)-4, (long)-5, (long)-6,
		       (long long)-7, (long long)-8, (long long)-9,
		       (short)-10, (unsigned short)-11, (short)-12,
		       (signed char)-13, (unsigned char)-14, (signed char)-15);
	p = buf;
	for (int i = 0; i < 15; i++) {
		uint64_t expects[5] = { UINT_MAX - 1,
					ULONG_MAX - 4,
					ULLONG_MAX - 7,
					USHRT_MAX - 10,
					UCHAR_MAX - 13 };
		if (i % 3 == 1) {
			ok(mp_typeof(*p) == MP_UINT, "Test type on step %d", i);
			ok(mp_decode_uint(&p) == expects[i / 3],
			   "Test value on step %d", i);
		} else {
			ok(mp_typeof(*p) == MP_INT, "Test type on step %d", i);
			ok(mp_decode_int(&p) == - i - 1,
			   "Test value on step %d", i);
		}
	}

	char data1[32];
	char *data1_end = data1;
	data1_end = mp_encode_array(data1_end, 2);
	data1_end = mp_encode_str(data1_end, "ABC", 3);
	data1_end = mp_encode_uint(data1_end, 11);
	size_t data1_len = data1_end - data1;
	assert(data1_len <= sizeof(data1));

	char data2[32];
	char *data2_end = data2;
	data2_end = mp_encode_int(data2_end, -1234567890);
	data2_end = mp_encode_str(data2_end, "DEFGHIJKLMN", 11);
	data2_end = mp_encode_uint(data2_end, 321);
	size_t data2_len = data2_end - data2;
	assert(data2_len <= sizeof(data2));

	fmt = "%d NIL [%d %b %b] this is test"
		"[%d %%%% [[ %d {%s %f %%  %.*s %lf %.*s NIL}"
		"%p %d %.*p ]] %d%d%d]";
#define TEST_PARAMS 0, 1, true, false, -1, 2, \
	"flt", 0.1, 6, "double#ignored", 0.2, 0, "ignore", \
	data1, 3, data2_len, data2, 4, 5, 6
	sz = mp_format(buf, buf_size, fmt, TEST_PARAMS);
	p = buf;
	e = buf + sz;

	c = p;
	ok(mp_check(&c, e) == 0, "check");
	ok(mp_typeof(*p) == MP_UINT, "type");
	ok(mp_decode_uint(&p) == 0, "decode");

	c = p;
	ok(mp_check(&c, e) == 0, "check");
	ok(mp_typeof(*p) == MP_NIL, "type");
	mp_decode_nil(&p);

	c = p;
	ok(mp_check(&c, e) == 0, "check");
	ok(mp_typeof(*p) == MP_ARRAY, "type");
	ok(mp_decode_array(&p) == 3, "decode");

	c = p;
	ok(mp_check(&c, e) == 0, "check");
	ok(mp_typeof(*p) == MP_UINT, "type");
	ok(mp_decode_uint(&p) == 1, "decode");

	c = p;
	ok(mp_check(&c, e) == 0, "check");
	ok(mp_typeof(*p) == MP_BOOL, "type");
	ok(mp_decode_bool(&p) == true, "decode");

	c = p;
	ok(mp_check(&c, e) == 0, "check");
	ok(mp_typeof(*p) == MP_BOOL, "type");
	ok(mp_decode_bool(&p) == false, "decode");

	c = p;
	ok(mp_check(&c, e) == 0, "check");
	ok(mp_typeof(*p) == MP_ARRAY, "type");
	ok(mp_decode_array(&p) == 5, "decode");

	c = p;
	ok(mp_check(&c, e) == 0, "check");
	ok(mp_typeof(*p) == MP_INT, "type");
	ok(mp_decode_int(&p) == -1, "decode");

	c = p;
	ok(mp_check(&c, e) == 0, "check");
	ok(mp_typeof(*p) == MP_ARRAY, "type");
	ok(mp_decode_array(&p) == 1, "decode");

	c = p;
	ok(mp_check(&c, e) == 0, "check");
	ok(mp_typeof(*p) == MP_ARRAY, "type");
	ok(mp_decode_array(&p) == 5, "decode");

	c = p;
	ok(mp_check(&c, e) == 0, "check");
	ok(mp_typeof(*p) == MP_UINT, "type");
	ok(mp_decode_uint(&p) == 2, "decode");

	c = p;
	ok(mp_check(&c, e) == 0, "check");
	ok(mp_typeof(*p) == MP_MAP, "type");
	ok(mp_decode_map(&p) == 3, "decode");

	c = p;
	ok(mp_check(&c, e) == 0, "check");
	ok(mp_typeof(*p) == MP_STR, "type");
	c = mp_decode_str(&p, &len);
	ok(len == 3, "decode");
	ok(memcmp(c, "flt", 3) == 0, "compare");

	c = p;
	ok(mp_check(&c, e) == 0, "check");
	ok(mp_typeof(*p) == MP_FLOAT, "type");
	ok(fequal(mp_decode_float(&p), 0.1), "decode");

	c = p;
	ok(mp_check(&c, e) == 0, "check");
	ok(mp_typeof(*p) == MP_STR, "type");
	c = mp_decode_str(&p, &len);
	ok(len == 6, "decode");
	ok(memcmp(c, "double", 6) == 0, "compare");

	c = p;
	ok(mp_check(&c, e) == 0, "check");
	ok(mp_typeof(*p) == MP_DOUBLE, "type");
	ok(dequal(mp_decode_double(&p), 0.2), "decode");

	c = p;
	ok(mp_check(&c, e) == 0, "check");
	ok(mp_typeof(*p) == MP_STR, "type");
	c = mp_decode_str(&p, &len);
	ok(len == 0, "decode");

	c = p;
	ok(mp_check(&c, e) == 0, "check");
	ok(mp_typeof(*p) == MP_NIL, "type");
	mp_decode_nil(&p);

	c = p;
	ok(mp_check(&c, e) == 0, "check");
	ok(((size_t)(c - p) == data1_len) &&
	   memcmp(p, data1, data1_len) == 0, "compare");
	p = c;

	c = p;
	ok(mp_check(&c, e) == 0, "check");
	ok(mp_typeof(*p) == MP_UINT, "type");
	ok(mp_decode_uint(&p) == 3, "decode");

	c = p;
	ok(mp_check(&c, e) == 0, "check");
	ok(mp_typeof(*p) == MP_INT, "type");
	ok(mp_decode_int(&p) == -1234567890, "decode");

	c = p;
	ok(mp_check(&c, e) == 0, "check");
	ok(mp_typeof(*p) == MP_STR, "type");
	c = mp_decode_str(&p, &len);
	ok(len == 11, "decode");
	ok(memcmp(c, "DEFGHIJKLMN", 11) == 0, "compare");

	c = p;
	ok(mp_check(&c, e) == 0, "check");
	ok(mp_typeof(*p) == MP_UINT, "type");
	ok(mp_decode_uint(&p) == 321, "decode");

	c = p;
	ok(mp_check(&c, e) == 0, "check");
	ok(mp_typeof(*p) == MP_UINT, "type");
	ok(mp_decode_uint(&p) == 4, "decode");

	c = p;
	ok(mp_check(&c, e) == 0, "check");
	ok(mp_typeof(*p) == MP_UINT, "type");
	ok(mp_decode_uint(&p) == 5, "decode");

	c = p;
	ok(mp_check(&c, e) == 0, "check");
	ok(mp_typeof(*p) == MP_UINT, "type");
	ok(mp_decode_uint(&p) == 6, "decode");

	ok(p == e, "nothing more");

	ok(sz < 70, "no magic detected");

	for (size_t lim = 0; lim <= 70; lim++) {
		memset(buf, 0, buf_size);
		size_t test_sz = mp_format(buf, lim, fmt, TEST_PARAMS);
		ok(test_sz == sz, "return value on step %d", (int)lim);
		bool all_zero = true;
		for(size_t z = lim; z < buf_size; z++)
			all_zero = all_zero && (buf[z] == 0);
		ok(all_zero, "buffer overflow on step %d", (int)lim);

	}

#undef TEST_PARAMS

	footer();
	return check_plan();
}