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