summaryrefslogtreecommitdiffstats
path: root/3rdparty/openpgm-svn-r1085/pgm/checksum_unittest.c
blob: a25d36a92240c7ebf986372bc004ad0e853d7314 (plain) (blame)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* vim:ts=8:sts=8:sw=4:noai:noexpandtab
 *
 * unit tests for PGM checksum routines
 *
 * Copyright (c) 2009-2010 Miru Limited.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


#include <signal.h>
#include <stdbool.h>
#include <stdlib.h>
#include <glib.h>
#include <check.h>


/* mock state */


/* mock functions for external references */

size_t
pgm_transport_pkt_offset2 (
        const bool                      can_fragment,
        const bool                      use_pgmcc
        )
{
        return 0;
}


#define CHECKSUM_DEBUG
#include "checksum.c"


/* target:
 *	uint16_t
 *	pgm_inet_checksum (
 *		const void*		src,
 *		uint16_t		len,
 *		uint16_t		csum
 *	)
 */

START_TEST (test_inet_pass_001)
{
	const char source[]  = "i am not a string";
	const guint16 answer = 0x1fda;		/* network order */

	guint16 csum = pgm_inet_checksum (source, sizeof(source), 0);
/* function calculates answer in host order */
	csum = g_htons (csum);
	g_message ("IP checksum of \"%s\" (%d) is %u (%u)",
		source, sizeof(source), csum, answer);

	fail_unless (answer == csum, "checksum mismatch");
}
END_TEST

START_TEST (test_inet_pass_002)
{
	char* source = alloca (65535);
	for (unsigned i = 0, j = 0; i < 65535; i++) {
		j = j * 1103515245 + 12345;
		source[i] = j;
	}
	const guint16 answer = 0x3fc0;		/* network order */

	guint16 csum = pgm_inet_checksum (source, 65535, 0);
/* function calculates answer in host order */
	csum = g_htons (csum);
	g_message ("IP checksum of 64KB is %u (%u)",
		csum, answer);

	fail_unless (answer == csum, "checksum mismatch");
}
END_TEST

START_TEST (test_inet_fail_001)
{
	pgm_inet_checksum (NULL, 0, 0);
	fail ("reached");
}
END_TEST

/* target:
 *	guint16
 *	pgm_csum_fold (
 *		guint32			csum
 *	)
 */

START_TEST (test_fold_pass_001)
{
	const guint32 csum   = 0xdd250300;	/* network order */
	const guint16 answer = 0x1fda;

	guint16 folded_csum = pgm_csum_fold (g_ntohl (csum));
	folded_csum = g_htons (folded_csum);
	g_message ("32-bit checksum %u folds into %u (%u)", csum, folded_csum, answer);

	fail_unless (answer == folded_csum, "folded checksum mismatch");
}
END_TEST


/* target:
 *	guint32
 *	pgm_csum_partial (
 *		const void*		src,
 *		guint			len,
 *		guint32			sum
 *	)
 */

START_TEST (test_partial_pass_001)
{
	const char source[]  = "i am not a string";
#if __BYTE_ORDER == __BIG_ENDIAN
	const guint32 answer = 0x0000e025;	/* network order */
#elif __BYTE_ORDER == __LITTLE_ENDIAN
	const guint32 answer = 0xe0250000;	/* network order */
#else
#	error "__BYTE_ORDER not supported."
#endif

	guint32 csum = pgm_csum_partial (source, sizeof(source), 0);
	csum = g_htonl (csum);
	g_message ("Partial checksum of \"%s\" is %u (%u)", source, csum, answer);

	fail_unless (answer == csum, "checksum mismatch");
}
END_TEST

START_TEST (test_partial_fail_001)
{
	pgm_csum_partial (NULL, 0, 0);
	fail ("reached");
}
END_TEST

/* target:
 *	guint32
 *	pgm_csum_partial_copy (
 *		const void*		src,
 *		void*			dst,
 *		guint			len,
 *		guint32			sum
 *	)
 */

START_TEST (test_partial_copy_pass_001)
{
	const char source[] = "i am not a string";
#if __BYTE_ORDER == __BIG_ENDIAN
	const guint32 answer = 0x0000e025;	/* network order */
#elif __BYTE_ORDER == __LITTLE_ENDIAN
	const guint32 answer = 0xe0250000;	/* network order */
#else
#	error "__BYTE_ORDER not supported."
#endif

	char dest[1024];
	guint32 csum_source = pgm_csum_partial_copy (source, dest, sizeof(source), 0);
	csum_source = g_htonl (csum_source);
	guint32 csum_dest   = pgm_csum_partial (dest, sizeof(source), 0);
	csum_dest = g_htonl (csum_dest);
	g_message ("Partial copy checksum of \"%s\" is %u, partial checksum is %u (%u)",
		   source, csum_source, csum_dest, answer);
	fail_unless (answer == csum_source, "checksum mismatch in partial-copy");
	fail_unless (answer == csum_dest,   "checksum mismatch in partial");
}
END_TEST

START_TEST (test_partial_copy_fail_001)
{
	pgm_csum_partial_copy (NULL, NULL, 0, 0);
	fail ("reached");
}
END_TEST

/* target:
 *	guint32
 *	pgm_csum_block_add (
 *		guint32			csum,
 *		guint32			csum2,
 *		guint			offset
 *	)
 */

START_TEST (test_block_add_pass_001)
{
	const char source[] = "i am not a string";
	const guint16 answer = 0x1fda;		/* network order */

	guint32 csum_a = pgm_csum_partial (source, sizeof(source) / 2, 0);
	guint32 csum_b = pgm_csum_partial (source + (sizeof(source) / 2), sizeof(source) - (sizeof(source) / 2), 0);
	guint32 csum   = pgm_csum_block_add (csum_a, csum_b, sizeof(source) / 2);
	guint16 fold   = pgm_csum_fold (csum);
/* convert to display in network order */
	csum_a = g_htonl (csum_a);
	csum_b = g_htonl (csum_b);
	csum   = g_htonl (csum);
	fold   = g_htons (fold);
	g_message ("Checksum A:%u + B:%u = %u -> %u (%u)",
		   csum_a, csum_b, csum, fold, answer);
	fail_unless (answer == fold, "checksum mismatch");
}
END_TEST


static
Suite*
make_test_suite (void)
{
	Suite* s;

	s = suite_create (__FILE__);

	TCase* tc_inet = tcase_create ("inet");
	suite_add_tcase (s, tc_inet);
	tcase_add_test (tc_inet, test_inet_pass_001);
	tcase_add_test (tc_inet, test_inet_pass_002);
	tcase_add_test_raise_signal (tc_inet, test_inet_fail_001, SIGABRT);

	TCase* tc_fold = tcase_create ("fold");
	suite_add_tcase (s, tc_fold);
	tcase_add_test (tc_fold, test_fold_pass_001);

	TCase* tc_block_add = tcase_create ("block-add");
	suite_add_tcase (s, tc_block_add);
	tcase_add_test (tc_block_add, test_block_add_pass_001);

	TCase* tc_partial = tcase_create ("partial");
	suite_add_tcase (s, tc_partial);
	tcase_add_test (tc_partial, test_partial_pass_001);
	tcase_add_test_raise_signal (tc_partial, test_partial_fail_001, SIGABRT);

	TCase* tc_partial_copy = tcase_create ("partial-copy");
	suite_add_tcase (s, tc_partial_copy);
	tcase_add_test (tc_partial_copy, test_partial_copy_pass_001);
	tcase_add_test_raise_signal (tc_partial_copy, test_partial_copy_fail_001, SIGABRT);
	return s;
}

static
Suite*
make_master_suite (void)
{
	Suite* s = suite_create ("Master");
	return s;
}

int
main (void)
{
	SRunner* sr = srunner_create (make_master_suite ());
	srunner_add_suite (sr, make_test_suite ());
	srunner_run_all (sr, CK_ENV);
	int number_failed = srunner_ntests_failed (sr);
	srunner_free (sr);
	return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}

/* eof */