aboutsummaryrefslogtreecommitdiff
path: root/tests/lxdrgen.c
blob: 14f40e20f682c0ff8da0631b56fd96165223b290 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * tests/lxdrgen.c
 *
 * Copyright (c) 2022, Přemysl Eric Janouch <p@janouch.name>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

#define PROGRAM_NAME "test"
#define PROGRAM_VERSION "0"

#include "../liberty.c"
#include "lxdrgen.lxdr.c"

static void
test_ser_deser_free (void)
{
	hard_assert (PROTO_GEN_VERSION == 1);

	enum { CASES = 3 };

	struct proto_gen_struct a = {}, b = {};
	a.u = xcalloc ((a.u_len = CASES + rand () % 100), sizeof *a.u);
	for (size_t i = 0; i < a.u_len; i++)
	{
		union proto_gen_union *u = a.u + i;
		switch (i % CASES)
		{
		case 0:
			u->tag = PROTO_GEN_ENUM_NUMBERS;
			u->numbers.a = rand () % UINT8_MAX;
			u->numbers.b = rand () % UINT16_MAX;
			u->numbers.c = rand () % UINT32_MAX;
			u->numbers.d = rand () % UINT64_MAX;
			u->numbers.e = rand () % UINT8_MAX;
			u->numbers.f = rand () % UINT16_MAX;
			u->numbers.g = rand () % UINT32_MAX;
			u->numbers.h = rand () % UINT64_MAX;
			break;
		case 1:
			u->tag = PROTO_GEN_ENUM_OTHERS;
			u->others.foo = rand () % 2;
			u->others.bar = str_make ();
			for (int i = rand () % 0x30; i > 0; i--)
				str_append_c (&u->others.bar, 0x30 + i);
			break;
		case 2:
			u->tag = PROTO_GEN_ENUM_NOTHING;
			break;
		default:
			hard_assert (!"unhandled case");
		}
	}

	struct str buf = str_make ();
	hard_assert (proto_gen_struct_serialize (&a, &buf));
	struct msg_unpacker r = msg_unpacker_make (buf.str, buf.len);
	hard_assert (proto_gen_struct_deserialize (&b, &r));
	hard_assert (!msg_unpacker_get_available (&r));
	str_free (&buf);

	hard_assert (a.u_len == b.u_len);
	for (size_t i = 0; i < a.u_len; i++)
	{
		union proto_gen_union *ua = a.u + i;
		union proto_gen_union *ub = b.u + i;
		hard_assert (ua->tag == ub->tag);
		switch (ua->tag)
		{
		case PROTO_GEN_ENUM_NUMBERS:
			hard_assert (ua->numbers.a == ub->numbers.a);
			hard_assert (ua->numbers.b == ub->numbers.b);
			hard_assert (ua->numbers.c == ub->numbers.c);
			hard_assert (ua->numbers.d == ub->numbers.d);
			hard_assert (ua->numbers.e == ub->numbers.e);
			hard_assert (ua->numbers.f == ub->numbers.f);
			hard_assert (ua->numbers.g == ub->numbers.g);
			hard_assert (ua->numbers.h == ub->numbers.h);
			break;
		case PROTO_GEN_ENUM_OTHERS:
			hard_assert (ua->others.foo == ub->others.foo);
			hard_assert (ua->others.bar.len == ub->others.bar.len);
			hard_assert (!memcmp (ua->others.bar.str, ub->others.bar.str,
				ua->others.bar.len));
			break;
		case PROTO_GEN_ENUM_NOTHING:
			break;
		default:
			hard_assert (!"unexpected case");
		}
	}

	// Emulate partially deserialized data to test disposal of that.
	for (size_t i = b.u_len - CASES; i < b.u_len; i++)
	{
		proto_gen_union_free (&b.u[i]);
		memset (&b.u[i], 0, sizeof b.u[i]);
	}

	proto_gen_struct_free (&a);
	proto_gen_struct_free (&b);
}

int
main (int argc, char *argv[])
{
	struct test test;
	test_init (&test, argc, argv);

	test_add_simple (&test, "/ser-deser-free", NULL, test_ser_deser_free);

	return test_run (&test);
}