xref: /netbsd-src/tests/lib/libc/string/t_memcpy.c (revision 88fcb00c0357f2d7c1774f86a352637bfda96184)
1 /* $NetBSD: t_memcpy.c,v 1.2 2011/04/07 18:14:09 jruoho Exp $ */
2 
3 /*-
4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <atf-c.h>
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <assert.h>
35 #include <md5.h>
36 
37 #include <sys/types.h>
38 
39 #define	ALIGNMENTS 16
40 #define	LENGTHS	    4
41 #define BLOCKTYPES 4
42 
43 MD5_CTX mc[1];
44 
45 typedef	unsigned char testBlock_t[ALIGNMENTS * LENGTHS];
46 
47 testBlock_t bss1, bss2;
48 
49 unsigned char *start[BLOCKTYPES] = {
50 		bss1, bss2
51 };
52 
53 char result[100];
54 const char goodResult[] = "7b405d24bc03195474c70ddae9e1f8fb";
55 
56 static void
57 runTest(unsigned char *b1, unsigned char *b2)
58 {
59 	int	i, j, k, m;
60 	size_t	n;
61 
62 	for (i = 0; i < ALIGNMENTS; ++i) {
63 		for (j = 0; j < ALIGNMENTS; ++j) {
64 			k = sizeof(testBlock_t) - (i > j ? i : j);
65 			for (m = 0; m < k; ++m) {
66 				for (n = 0; n < sizeof(testBlock_t); ++n) {
67 					b1[n] = (unsigned char)random();
68 					b2[n] = (unsigned char)random();
69 				}
70 				memcpy(b1 + i, b2 + j, m);
71 				MD5Update(mc, b1, sizeof(testBlock_t));
72 				MD5Update(mc, b2, sizeof(testBlock_t));
73 			}
74 		}
75 	}
76 }
77 
78 ATF_TC(check_memcpy);
79 
80 ATF_TC_HEAD(check_memcpy, tc)
81 {
82 
83 	atf_tc_set_md_var(tc, "descr", "Test memcpy results");
84 }
85 
86 ATF_TC_BODY(check_memcpy, tc)
87 {
88 	int i, j;
89 	testBlock_t auto1, auto2;
90 
91 	start[2] = auto1;
92 	start[3] = auto2;
93 
94 	srandom(0L);
95 	MD5Init(mc);
96 	for (i = 0; i < BLOCKTYPES; ++i)
97 		for (j = 0; j < BLOCKTYPES; ++j)
98 			if (i != j)
99 				runTest(start[i], start[j]);
100 	MD5End(mc, result);
101 	ATF_REQUIRE_EQ(strcmp(result, goodResult), 0);
102 }
103 
104 ATF_TP_ADD_TCS(tp)
105 {
106 
107 	ATF_TP_ADD_TC(tp, check_memcpy);
108 
109 	return atf_no_error();
110 }
111