xref: /freebsd/contrib/netbsd-tests/lib/libc/string/t_memcpy.c (revision 02e9120893770924227138ba49df1edb3896112a)
1 /* $NetBSD: t_memcpy.c,v 1.6 2017/01/11 18:05:54 christos 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 #ifdef __NetBSD__
55 const char goodResult[] = "7b405d24bc03195474c70ddae9e1f8fb";
56 #else
57 const char goodResult[] = "5ab4443f0e3e058d94087d9f2a11ef5e";
58 #endif
59 
60 static void
61 runTest(unsigned char *b1, unsigned char *b2)
62 {
63 	int	i, j, k, m;
64 	size_t	n;
65 
66 	for (i = 0; i < ALIGNMENTS; ++i) {
67 		for (j = 0; j < ALIGNMENTS; ++j) {
68 			k = sizeof(testBlock_t) - (i > j ? i : j);
69 			for (m = 0; m < k; ++m) {
70 				for (n = 0; n < sizeof(testBlock_t); ++n) {
71 					b1[n] = (unsigned char)random();
72 					b2[n] = (unsigned char)random();
73 				}
74 				memcpy(b1 + i, b2 + j, m);
75 				MD5Update(mc, b1, sizeof(testBlock_t));
76 				MD5Update(mc, b2, sizeof(testBlock_t));
77 			}
78 		}
79 	}
80 }
81 
82 ATF_TC(memcpy_basic);
83 ATF_TC_HEAD(memcpy_basic, tc)
84 {
85 	atf_tc_set_md_var(tc, "descr", "Test memcpy results");
86 }
87 
88 ATF_TC_BODY(memcpy_basic, tc)
89 {
90 	int i, j;
91 	testBlock_t auto1, auto2;
92 
93 	start[2] = auto1;
94 	start[3] = auto2;
95 
96 #ifdef __NetBSD__
97 	srandom(0L);
98 #else
99 	/*
100 	 * random() shall produce by default a sequence of numbers that can be
101 	 * duplicated by calling srandom() with 1 as the seed.
102 	 */
103 	srandom(1);
104 #endif
105 	MD5Init(mc);
106 	for (i = 0; i < BLOCKTYPES; ++i)
107 		for (j = 0; j < BLOCKTYPES; ++j)
108 			if (i != j)
109 				runTest(start[i], start[j]);
110 	MD5End(mc, result);
111 	ATF_REQUIRE_EQ_MSG(strcmp(result, goodResult), 0, "%s != %s",
112 	    result, goodResult);
113 }
114 
115 ATF_TC(memccpy_simple);
116 ATF_TC_HEAD(memccpy_simple, tc)
117 {
118         atf_tc_set_md_var(tc, "descr", "Test memccpy(3) results");
119 }
120 
121 ATF_TC_BODY(memccpy_simple, tc)
122 {
123 	char buf[100];
124 	char c = ' ';
125 
126 	(void)memset(buf, c, sizeof(buf));
127 
128 	ATF_CHECK(memccpy(buf, "foo bar", c, sizeof(buf)) != NULL);
129 	ATF_CHECK(buf[4] == c);
130 
131 	ATF_CHECK(memccpy(buf, "foo bar", '\0', sizeof(buf) - 1) != NULL);
132 	ATF_CHECK(buf[8] == c);
133 
134 	ATF_CHECK(memccpy(buf, "foo bar", 'x', 7) == NULL);
135 	ATF_CHECK(strncmp(buf, "foo bar", 7) == 0);
136 
137 	ATF_CHECK(memccpy(buf, "xxxxxxx", 'r', 7) == NULL);
138 	ATF_CHECK(strncmp(buf, "xxxxxxx", 7) == 0);
139 }
140 
141 ATF_TC(memcpy_return);
142 ATF_TC_HEAD(memcpy_return, tc)
143 {
144 	atf_tc_set_md_var(tc, "descr", "Test memcpy(3) return value");
145 }
146 
147 ATF_TC_BODY(memcpy_return, tc)
148 {
149 	char *b = (char *)0x1;
150 	char c[2];
151 	ATF_REQUIRE_EQ(memcpy(b, b, 0), b);
152 	ATF_REQUIRE_EQ(memcpy(c, "ab", sizeof(c)), c);
153 }
154 
155 ATF_TP_ADD_TCS(tp)
156 {
157 
158 	ATF_TP_ADD_TC(tp, memcpy_basic);
159 	ATF_TP_ADD_TC(tp, memcpy_return);
160 	ATF_TP_ADD_TC(tp, memccpy_simple);
161 
162 	return atf_no_error();
163 }
164