1272343Sngie/* $NetBSD: t_memcpy.c,v 1.5 2013/03/17 02:23:31 christos Exp $ */ 2272343Sngie 3272343Sngie/*- 4272343Sngie * Copyright (c) 2010 The NetBSD Foundation, Inc. 5272343Sngie * All rights reserved. 6272343Sngie * 7272343Sngie * Redistribution and use in source and binary forms, with or without 8272343Sngie * modification, are permitted provided that the following conditions 9272343Sngie * are met: 10272343Sngie * 1. Redistributions of source code must retain the above copyright 11272343Sngie * notice, this list of conditions and the following disclaimer. 12272343Sngie * 2. Redistributions in binary form must reproduce the above copyright 13272343Sngie * notice, this list of conditions and the following disclaimer in the 14272343Sngie * documentation and/or other materials provided with the distribution. 15272343Sngie * 16272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19272343Sngie * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26272343Sngie * POSSIBILITY OF SUCH DAMAGE. 27272343Sngie */ 28272343Sngie 29272343Sngie#include <atf-c.h> 30272343Sngie 31272343Sngie#include <stdio.h> 32272343Sngie#include <stdlib.h> 33272343Sngie#include <string.h> 34272343Sngie#include <assert.h> 35272343Sngie#include <md5.h> 36272343Sngie 37272343Sngie#include <sys/types.h> 38272343Sngie 39272343Sngie#define ALIGNMENTS 16 40272343Sngie#define LENGTHS 4 41272343Sngie#define BLOCKTYPES 4 42272343Sngie 43272343SngieMD5_CTX mc[1]; 44272343Sngie 45272343Sngietypedef unsigned char testBlock_t[ALIGNMENTS * LENGTHS]; 46272343Sngie 47272343SngietestBlock_t bss1, bss2; 48272343Sngie 49272343Sngieunsigned char *start[BLOCKTYPES] = { 50272343Sngie bss1, bss2 51272343Sngie}; 52272343Sngie 53272343Sngiechar result[100]; 54276478Sngie#ifdef __NetBSD__ 55272343Sngieconst char goodResult[] = "7b405d24bc03195474c70ddae9e1f8fb"; 56276478Sngie#else 57276478Sngieconst char goodResult[] = "217b4fbe456916bf62a2f85df752e4ab"; 58276478Sngie#endif 59272343Sngie 60272343Sngiestatic void 61272343SngierunTest(unsigned char *b1, unsigned char *b2) 62272343Sngie{ 63272343Sngie int i, j, k, m; 64272343Sngie size_t n; 65272343Sngie 66272343Sngie for (i = 0; i < ALIGNMENTS; ++i) { 67272343Sngie for (j = 0; j < ALIGNMENTS; ++j) { 68272343Sngie k = sizeof(testBlock_t) - (i > j ? i : j); 69272343Sngie for (m = 0; m < k; ++m) { 70272343Sngie for (n = 0; n < sizeof(testBlock_t); ++n) { 71272343Sngie b1[n] = (unsigned char)random(); 72272343Sngie b2[n] = (unsigned char)random(); 73272343Sngie } 74272343Sngie memcpy(b1 + i, b2 + j, m); 75272343Sngie MD5Update(mc, b1, sizeof(testBlock_t)); 76272343Sngie MD5Update(mc, b2, sizeof(testBlock_t)); 77272343Sngie } 78272343Sngie } 79272343Sngie } 80272343Sngie} 81272343Sngie 82272343SngieATF_TC(memcpy_basic); 83272343SngieATF_TC_HEAD(memcpy_basic, tc) 84272343Sngie{ 85272343Sngie atf_tc_set_md_var(tc, "descr", "Test memcpy results"); 86272343Sngie} 87272343Sngie 88272343SngieATF_TC_BODY(memcpy_basic, tc) 89272343Sngie{ 90272343Sngie int i, j; 91272343Sngie testBlock_t auto1, auto2; 92272343Sngie 93272343Sngie start[2] = auto1; 94272343Sngie start[3] = auto2; 95272343Sngie 96276478Sngie#ifdef __NetBSD__ 97272343Sngie srandom(0L); 98276478Sngie#else 99276478Sngie /* 100276478Sngie * random() shall produce by default a sequence of numbers that can be 101276478Sngie * duplicated by calling srandom() with 1 as the seed. 102276478Sngie */ 103276478Sngie srandom(1); 104276478Sngie#endif 105272343Sngie MD5Init(mc); 106272343Sngie for (i = 0; i < BLOCKTYPES; ++i) 107272343Sngie for (j = 0; j < BLOCKTYPES; ++j) 108272343Sngie if (i != j) 109272343Sngie runTest(start[i], start[j]); 110272343Sngie MD5End(mc, result); 111272343Sngie ATF_REQUIRE_EQ(strcmp(result, goodResult), 0); 112272343Sngie} 113272343Sngie 114272343SngieATF_TC(memccpy_simple); 115272343SngieATF_TC_HEAD(memccpy_simple, tc) 116272343Sngie{ 117272343Sngie atf_tc_set_md_var(tc, "descr", "Test memccpy(3) results"); 118272343Sngie} 119272343Sngie 120272343SngieATF_TC_BODY(memccpy_simple, tc) 121272343Sngie{ 122272343Sngie char buf[100]; 123272343Sngie char c = ' '; 124272343Sngie 125272343Sngie (void)memset(buf, c, sizeof(buf)); 126272343Sngie 127272343Sngie ATF_CHECK(memccpy(buf, "foo bar", c, sizeof(buf)) != NULL); 128272343Sngie ATF_CHECK(buf[4] == c); 129272343Sngie 130272343Sngie ATF_CHECK(memccpy(buf, "foo bar", '\0', sizeof(buf) - 1) != NULL); 131272343Sngie ATF_CHECK(buf[8] == c); 132272343Sngie 133272343Sngie ATF_CHECK(memccpy(buf, "foo bar", 'x', 7) == NULL); 134272343Sngie ATF_CHECK(strncmp(buf, "foo bar", 7) == 0); 135272343Sngie 136272343Sngie ATF_CHECK(memccpy(buf, "xxxxxxx", 'r', 7) == NULL); 137272343Sngie ATF_CHECK(strncmp(buf, "xxxxxxx", 7) == 0); 138272343Sngie} 139272343Sngie 140272343SngieATF_TC(memcpy_return); 141272343SngieATF_TC_HEAD(memcpy_return, tc) 142272343Sngie{ 143272343Sngie atf_tc_set_md_var(tc, "descr", "Test memcpy(3) return value"); 144272343Sngie} 145272343Sngie 146272343SngieATF_TC_BODY(memcpy_return, tc) 147272343Sngie{ 148272343Sngie char *b = (char *)0x1; 149272343Sngie char c[2]; 150272343Sngie ATF_REQUIRE_EQ(memcpy(b, b, 0), b); 151272343Sngie ATF_REQUIRE_EQ(memcpy(c, "ab", sizeof(c)), c); 152272343Sngie} 153272343Sngie 154272343SngieATF_TP_ADD_TCS(tp) 155272343Sngie{ 156272343Sngie 157272343Sngie ATF_TP_ADD_TC(tp, memcpy_basic); 158272343Sngie ATF_TP_ADD_TC(tp, memcpy_return); 159272343Sngie ATF_TP_ADD_TC(tp, memccpy_simple); 160272343Sngie 161272343Sngie return atf_no_error(); 162272343Sngie} 163