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