1 /* $NetBSD: t_memcpy.c,v 1.5 2013/03/17 02:23:31 christos Exp $ */
4 * Copyright (c) 2010 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
37 #include <sys/types.h>
45 typedef unsigned char testBlock_t
[ALIGNMENTS
* LENGTHS
];
47 testBlock_t bss1
, bss2
;
49 unsigned char *start
[BLOCKTYPES
] = {
54 const char goodResult
[] = "7b405d24bc03195474c70ddae9e1f8fb";
57 runTest(unsigned char *b1
, unsigned char *b2
)
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();
70 memcpy(b1
+ i
, b2
+ j
, m
);
71 MD5Update(mc
, b1
, sizeof(testBlock_t
));
72 MD5Update(mc
, b2
, sizeof(testBlock_t
));
79 ATF_TC_HEAD(memcpy_basic
, tc
)
81 atf_tc_set_md_var(tc
, "descr", "Test memcpy results");
84 ATF_TC_BODY(memcpy_basic
, tc
)
87 testBlock_t auto1
, auto2
;
94 for (i
= 0; i
< BLOCKTYPES
; ++i
)
95 for (j
= 0; j
< BLOCKTYPES
; ++j
)
97 runTest(start
[i
], start
[j
]);
99 ATF_REQUIRE_EQ(strcmp(result
, goodResult
), 0);
102 ATF_TC(memccpy_simple
);
103 ATF_TC_HEAD(memccpy_simple
, tc
)
105 atf_tc_set_md_var(tc
, "descr", "Test memccpy(3) results");
108 ATF_TC_BODY(memccpy_simple
, tc
)
113 (void)memset(buf
, c
, sizeof(buf
));
115 ATF_CHECK(memccpy(buf
, "foo bar", c
, sizeof(buf
)) != NULL
);
116 ATF_CHECK(buf
[4] == c
);
118 ATF_CHECK(memccpy(buf
, "foo bar", '\0', sizeof(buf
) - 1) != NULL
);
119 ATF_CHECK(buf
[8] == c
);
121 ATF_CHECK(memccpy(buf
, "foo bar", 'x', 7) == NULL
);
122 ATF_CHECK(strncmp(buf
, "foo bar", 7) == 0);
124 ATF_CHECK(memccpy(buf
, "xxxxxxx", 'r', 7) == NULL
);
125 ATF_CHECK(strncmp(buf
, "xxxxxxx", 7) == 0);
128 ATF_TC(memcpy_return
);
129 ATF_TC_HEAD(memcpy_return
, tc
)
131 atf_tc_set_md_var(tc
, "descr", "Test memcpy(3) return value");
134 ATF_TC_BODY(memcpy_return
, tc
)
136 char *b
= (char *)0x1;
138 ATF_REQUIRE_EQ(memcpy(b
, b
, 0), b
);
139 ATF_REQUIRE_EQ(memcpy(c
, "ab", sizeof(c
)), c
);
145 ATF_TP_ADD_TC(tp
, memcpy_basic
);
146 ATF_TP_ADD_TC(tp
, memcpy_return
);
147 ATF_TP_ADD_TC(tp
, memccpy_simple
);
149 return atf_no_error();