xref: /netbsd-src/tests/lib/libc/string/t_memcpy.c (revision 22f9a6b27111d1d95433e6ef3d4a45d156ee07b5)
1*22f9a6b2Schristos /* $NetBSD: t_memcpy.c,v 1.6 2017/01/11 18:05:54 christos Exp $ */
23ca5a7dbSpgoyette 
33ca5a7dbSpgoyette /*-
43ca5a7dbSpgoyette  * Copyright (c) 2010 The NetBSD Foundation, Inc.
53ca5a7dbSpgoyette  * All rights reserved.
63ca5a7dbSpgoyette  *
73ca5a7dbSpgoyette  * Redistribution and use in source and binary forms, with or without
83ca5a7dbSpgoyette  * modification, are permitted provided that the following conditions
93ca5a7dbSpgoyette  * are met:
103ca5a7dbSpgoyette  * 1. Redistributions of source code must retain the above copyright
113ca5a7dbSpgoyette  *    notice, this list of conditions and the following disclaimer.
123ca5a7dbSpgoyette  * 2. Redistributions in binary form must reproduce the above copyright
133ca5a7dbSpgoyette  *    notice, this list of conditions and the following disclaimer in the
143ca5a7dbSpgoyette  *    documentation and/or other materials provided with the distribution.
153ca5a7dbSpgoyette  *
163ca5a7dbSpgoyette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
173ca5a7dbSpgoyette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
183ca5a7dbSpgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
193ca5a7dbSpgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
203ca5a7dbSpgoyette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
213ca5a7dbSpgoyette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
223ca5a7dbSpgoyette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
233ca5a7dbSpgoyette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
243ca5a7dbSpgoyette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
253ca5a7dbSpgoyette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
263ca5a7dbSpgoyette  * POSSIBILITY OF SUCH DAMAGE.
273ca5a7dbSpgoyette  */
283ca5a7dbSpgoyette 
293ca5a7dbSpgoyette #include <atf-c.h>
303ca5a7dbSpgoyette 
313ca5a7dbSpgoyette #include <stdio.h>
323ca5a7dbSpgoyette #include <stdlib.h>
333ca5a7dbSpgoyette #include <string.h>
343ca5a7dbSpgoyette #include <assert.h>
353ca5a7dbSpgoyette #include <md5.h>
363ca5a7dbSpgoyette 
373ca5a7dbSpgoyette #include <sys/types.h>
383ca5a7dbSpgoyette 
393ca5a7dbSpgoyette #define	ALIGNMENTS 16
403ca5a7dbSpgoyette #define	LENGTHS	    4
413ca5a7dbSpgoyette #define BLOCKTYPES 4
423ca5a7dbSpgoyette 
433ca5a7dbSpgoyette MD5_CTX mc[1];
443ca5a7dbSpgoyette 
453ca5a7dbSpgoyette typedef	unsigned char testBlock_t[ALIGNMENTS * LENGTHS];
463ca5a7dbSpgoyette 
473ca5a7dbSpgoyette testBlock_t bss1, bss2;
483ca5a7dbSpgoyette 
493ca5a7dbSpgoyette unsigned char *start[BLOCKTYPES] = {
503ca5a7dbSpgoyette 		bss1, bss2
513ca5a7dbSpgoyette };
523ca5a7dbSpgoyette 
533ca5a7dbSpgoyette char result[100];
543ca5a7dbSpgoyette const char goodResult[] = "7b405d24bc03195474c70ddae9e1f8fb";
553ca5a7dbSpgoyette 
563ca5a7dbSpgoyette static void
runTest(unsigned char * b1,unsigned char * b2)573ca5a7dbSpgoyette runTest(unsigned char *b1, unsigned char *b2)
583ca5a7dbSpgoyette {
593ca5a7dbSpgoyette 	int	i, j, k, m;
603ca5a7dbSpgoyette 	size_t	n;
613ca5a7dbSpgoyette 
623ca5a7dbSpgoyette 	for (i = 0; i < ALIGNMENTS; ++i) {
633ca5a7dbSpgoyette 		for (j = 0; j < ALIGNMENTS; ++j) {
643ca5a7dbSpgoyette 			k = sizeof(testBlock_t) - (i > j ? i : j);
653ca5a7dbSpgoyette 			for (m = 0; m < k; ++m) {
663ca5a7dbSpgoyette 				for (n = 0; n < sizeof(testBlock_t); ++n) {
673ca5a7dbSpgoyette 					b1[n] = (unsigned char)random();
683ca5a7dbSpgoyette 					b2[n] = (unsigned char)random();
693ca5a7dbSpgoyette 				}
703ca5a7dbSpgoyette 				memcpy(b1 + i, b2 + j, m);
713ca5a7dbSpgoyette 				MD5Update(mc, b1, sizeof(testBlock_t));
723ca5a7dbSpgoyette 				MD5Update(mc, b2, sizeof(testBlock_t));
733ca5a7dbSpgoyette 			}
743ca5a7dbSpgoyette 		}
753ca5a7dbSpgoyette 	}
763ca5a7dbSpgoyette }
773ca5a7dbSpgoyette 
781c3ef2c7Sjruoho ATF_TC(memcpy_basic);
ATF_TC_HEAD(memcpy_basic,tc)791c3ef2c7Sjruoho ATF_TC_HEAD(memcpy_basic, tc)
803ca5a7dbSpgoyette {
813ca5a7dbSpgoyette 	atf_tc_set_md_var(tc, "descr", "Test memcpy results");
823ca5a7dbSpgoyette }
833ca5a7dbSpgoyette 
ATF_TC_BODY(memcpy_basic,tc)841c3ef2c7Sjruoho ATF_TC_BODY(memcpy_basic, tc)
853ca5a7dbSpgoyette {
863ca5a7dbSpgoyette 	int i, j;
873ca5a7dbSpgoyette 	testBlock_t auto1, auto2;
883ca5a7dbSpgoyette 
893ca5a7dbSpgoyette 	start[2] = auto1;
903ca5a7dbSpgoyette 	start[3] = auto2;
913ca5a7dbSpgoyette 
923ca5a7dbSpgoyette 	srandom(0L);
933ca5a7dbSpgoyette 	MD5Init(mc);
943ca5a7dbSpgoyette 	for (i = 0; i < BLOCKTYPES; ++i)
953ca5a7dbSpgoyette 		for (j = 0; j < BLOCKTYPES; ++j)
963ca5a7dbSpgoyette 			if (i != j)
973ca5a7dbSpgoyette 				runTest(start[i], start[j]);
983ca5a7dbSpgoyette 	MD5End(mc, result);
99*22f9a6b2Schristos 	ATF_REQUIRE_EQ_MSG(strcmp(result, goodResult), 0, "%s != %s",
100*22f9a6b2Schristos 	    result, goodResult);
1013ca5a7dbSpgoyette }
1023ca5a7dbSpgoyette 
1035fb118d0Sjruoho ATF_TC(memccpy_simple);
ATF_TC_HEAD(memccpy_simple,tc)1045fb118d0Sjruoho ATF_TC_HEAD(memccpy_simple, tc)
1055fb118d0Sjruoho {
1065fb118d0Sjruoho         atf_tc_set_md_var(tc, "descr", "Test memccpy(3) results");
1075fb118d0Sjruoho }
1085fb118d0Sjruoho 
ATF_TC_BODY(memccpy_simple,tc)1095fb118d0Sjruoho ATF_TC_BODY(memccpy_simple, tc)
1105fb118d0Sjruoho {
1115fb118d0Sjruoho 	char buf[100];
1125fb118d0Sjruoho 	char c = ' ';
1135fb118d0Sjruoho 
1145fb118d0Sjruoho 	(void)memset(buf, c, sizeof(buf));
1155fb118d0Sjruoho 
1165fb118d0Sjruoho 	ATF_CHECK(memccpy(buf, "foo bar", c, sizeof(buf)) != NULL);
1175fb118d0Sjruoho 	ATF_CHECK(buf[4] == c);
1185fb118d0Sjruoho 
1195fb118d0Sjruoho 	ATF_CHECK(memccpy(buf, "foo bar", '\0', sizeof(buf) - 1) != NULL);
1205fb118d0Sjruoho 	ATF_CHECK(buf[8] == c);
1215fb118d0Sjruoho 
1225fb118d0Sjruoho 	ATF_CHECK(memccpy(buf, "foo bar", 'x', 7) == NULL);
1235fb118d0Sjruoho 	ATF_CHECK(strncmp(buf, "foo bar", 7) == 0);
1245fb118d0Sjruoho 
1255fb118d0Sjruoho 	ATF_CHECK(memccpy(buf, "xxxxxxx", 'r', 7) == NULL);
1265fb118d0Sjruoho 	ATF_CHECK(strncmp(buf, "xxxxxxx", 7) == 0);
1275fb118d0Sjruoho }
1285fb118d0Sjruoho 
129d3d55324Schristos ATF_TC(memcpy_return);
ATF_TC_HEAD(memcpy_return,tc)130d3d55324Schristos ATF_TC_HEAD(memcpy_return, tc)
131d3d55324Schristos {
132d3d55324Schristos 	atf_tc_set_md_var(tc, "descr", "Test memcpy(3) return value");
133d3d55324Schristos }
134d3d55324Schristos 
ATF_TC_BODY(memcpy_return,tc)135d3d55324Schristos ATF_TC_BODY(memcpy_return, tc)
136d3d55324Schristos {
137d3d55324Schristos 	char *b = (char *)0x1;
138d3d55324Schristos 	char c[2];
139d3d55324Schristos 	ATF_REQUIRE_EQ(memcpy(b, b, 0), b);
140d3d55324Schristos 	ATF_REQUIRE_EQ(memcpy(c, "ab", sizeof(c)), c);
141d3d55324Schristos }
142d3d55324Schristos 
ATF_TP_ADD_TCS(tp)1433ca5a7dbSpgoyette ATF_TP_ADD_TCS(tp)
1443ca5a7dbSpgoyette {
1453ca5a7dbSpgoyette 
1461c3ef2c7Sjruoho 	ATF_TP_ADD_TC(tp, memcpy_basic);
147d3d55324Schristos 	ATF_TP_ADD_TC(tp, memcpy_return);
1485fb118d0Sjruoho 	ATF_TP_ADD_TC(tp, memccpy_simple);
1493ca5a7dbSpgoyette 
1503ca5a7dbSpgoyette 	return atf_no_error();
1513ca5a7dbSpgoyette }
152