summaryrefslogtreecommitdiffstats
path: root/clib/test/checksum.c
blob: c615ccae0c374b36587ae20b4204d75e0e7e8695 (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
/*
 * Copyright (c) International Business Machines Corp., 2014
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <stdio.h>
#include <stdlib.h>

#include <clib/checksum.h>
#include <clib/exception.h>

int main(void)
{
	unsigned int i;
	uint8_t *buf = malloc(32);
	uint8_t align[8] =       { 0x01, 0x02, 0x04, 0x08,
				   0x10, 0x20, 0x40, 0x80 };
	uint8_t unaligned1[9] =  { 0x11, 0x11, 0x11, 0x11,
				   0x22, 0x22, 0x22, 0x22, 0x33 };
	uint8_t unaligned2[10] = { 0x11, 0x11, 0x11, 0x11,
				   0x22, 0x22, 0x22, 0x22, 0x33, 0x33 };
	uint8_t unaligned3[11] = { 0x11, 0x11, 0x11, 0x11,
				   0x22, 0x22, 0x22, 0x22, 0x33, 0x33, 0x33 };

	memcpy(buf, align, sizeof(align));
	uint32_t csum = memcpy_checksum(NULL, buf, sizeof(align));
	if (csum != 0x11224488) {
		printf("fail %d a:%08x e:%08x\n", __LINE__, csum, 0x11224488);
		return 1;
	}

	memcpy(buf, unaligned1, sizeof(unaligned1));
	csum = memcpy_checksum(NULL, buf, sizeof(unaligned1));
	if (csum != 0x00333333) {
		printf("fail %d a:%08x e:%08x\n", __LINE__, csum, 0x00333333);
		return 1;
	}

	memcpy(buf, unaligned2, sizeof(unaligned2));
	csum = memcpy_checksum(NULL, buf, sizeof(unaligned2));
	if (csum != 0x00003333) {
		printf("fail %d a:%08x e:%08x\n", __LINE__, csum, 0x00003333);
		return 1;
	}

	memcpy(buf, unaligned3, sizeof(unaligned3));
	csum = memcpy_checksum(NULL, buf, sizeof(unaligned3));
	if (csum != 0x00000033) {
		printf("fail %d a:%08x e:%08x\n", __LINE__, csum, 0x00000033);
		return 1;
	}

	char dst[16];
	csum = memcpy_checksum(dst, align, sizeof(align));
	if (csum != 0x11224488) {
		printf("fail %d a:%08x e:%08x\n", __LINE__, csum, 0x11224488);
		return 1;
	}

	csum = memcpy_checksum(dst, unaligned1, sizeof(unaligned1));
	for (i = 0; i < sizeof(unaligned1); i++) {
		if (dst[i] != unaligned1[i])
			printf("fail %d byte %i a:%08x e:%08x\n",
					__LINE__, i, dst[i], unaligned1[i]);
	}
	if (csum != 0x00333333) {
		printf("fail %d a:%08x e:%08x\n", __LINE__, csum, 0x00333333);
		return 1;
	}

	csum = memcpy_checksum(dst, unaligned2, sizeof(unaligned2));
	for (i = 0; i < sizeof(unaligned2); i++) {
		if (dst[i] != unaligned2[i])
			printf("fail %d byte %i a:%08x e:%08x\n",
					__LINE__, i, dst[i], unaligned2[i]);
	}
	if (csum != 0x00003333) {
		printf("fail %d a:%08x e:%08x\n", __LINE__, csum, 0x00003333);
		return 1;
	}

	csum = memcpy_checksum(dst, unaligned3, sizeof(unaligned3));
	for (i = 0; i < sizeof(unaligned3); i++) {
		if (dst[i] != unaligned3[i])
			printf("fail %d byte %i a:%08x e:%08x\n",
					__LINE__, i, dst[i], unaligned3[i]);
	}
	if (csum != 0x00000033) {
		printf("fail %d a:%08x e:%08x\n", __LINE__, csum, 0x00000033);
		return 1;
	}

#if 0
	exception_t ex;
	try {
		(void)memcpy_checksum(NULL, &align[1], sizeof(align) - 1);
	} else (ex) {
		printf("Hey, what's this in the weeds? It's a baby, awesome!\n");
	} end_try;

#endif

	return 0;
}
OpenPOWER on IntegriCloud