Path: blob/main/lib/libc/tests/string/stpncpy_test.c
39491 views
/*-1* Copyright (c) 2009 David Schultz <[email protected]>2* Copyright (c) 2023 The FreeBSD Foundation3* All rights reserved.4*5* Portions of this software were developed by Robert Clausecker6* <[email protected]> under sponsorship from the FreeBSD Foundation.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16*17* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND18* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE21* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL22* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS23* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)24* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT25* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY26* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF27* SUCH DAMAGE.28*/2930#include <sys/param.h>31#include <sys/mman.h>32#include <assert.h>33#include <dlfcn.h>34#include <stdio.h>35#include <stdlib.h>36#include <string.h>3738#include <atf-c.h>3940static char *(*stpncpy_fn)(char *restrict, const char *restrict, size_t);4142static char *43makebuf(size_t len, int guard_at_end)44{45char *buf;46size_t alloc_size, page_size;4748page_size = getpagesize();49alloc_size = roundup2(len, page_size) + page_size;5051buf = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);52assert(buf);53if (guard_at_end) {54assert(munmap(buf + alloc_size - page_size, page_size) == 0);55return (buf + alloc_size - page_size - len);56} else {57assert(munmap(buf, page_size) == 0);58return (buf + page_size);59}60}6162static void63test_stpncpy(const char *s)64{65char *src, *dst;66size_t size, len, bufsize, x;67int i, j;6869size = strlen(s) + 1;70for (i = 0; i <= 1; i++) {71for (j = 0; j <= 1; j++) {72for (bufsize = 0; bufsize <= size + 10; bufsize++) {73src = makebuf(size, i);74memcpy(src, s, size);75dst = makebuf(bufsize, j);76memset(dst, 'X', bufsize);77len = (bufsize < size) ? bufsize : size - 1;78assert(stpncpy_fn(dst, src, bufsize) == dst+len);79assert(memcmp(src, dst, len) == 0);80for (x = len; x < bufsize; x++)81assert(dst[x] == '\0');82}83}84}85}8687static void88test_sentinel(char *dest, char *src, size_t destlen, size_t srclen)89{90size_t i;91const char *res, *wantres;92const char *fail = NULL;9394for (i = 0; i < srclen; i++)95/* src will never include (){} */96src[i] = '0' + i;97src[srclen] = '\0';9899/* source sentinels: not to be copied */100src[-1] = '(';101src[srclen+1] = ')';102103memset(dest, 0xee, destlen);104105/* destination sentinels: not to be touched */106dest[-1] = '{';107dest[destlen] = '}';108109wantres = dest + (srclen > destlen ? destlen : srclen);110res = stpncpy_fn(dest, src, destlen);111112if (dest[-1] != '{')113fail = "start sentinel overwritten";114else if (dest[destlen] != '}')115fail = "end sentinel overwritten";116else if (strncmp(src, dest, destlen) != 0)117fail = "string not copied correctly";118else if (res != wantres)119fail = "incorrect return value";120else for (i = srclen; i < destlen; i++)121if (dest[i] != '\0') {122fail = "incomplete NUL padding";123break;124}125126if (fail)127atf_tc_fail_nonfatal("%s\n"128"stpncpy(%p \"%s\", %p \"%s\", %zu) = %p (want %p)\n",129fail, dest, dest, src, src, destlen, res, wantres);130}131132ATF_TC_WITHOUT_HEAD(null);133ATF_TC_BODY(null, tc)134{135ATF_CHECK_EQ(stpncpy_fn(NULL, NULL, 0), NULL);136}137138ATF_TC_WITHOUT_HEAD(bounds);139ATF_TC_BODY(bounds, tc)140{141size_t i;142char buf[64+1];143144for (i = 0; i < sizeof(buf) - 1; i++) {145buf[i] = ' ' + i;146buf[i+1] = '\0';147test_stpncpy(buf);148}149}150151ATF_TC_WITHOUT_HEAD(alignments);152ATF_TC_BODY(alignments, tc)153{154size_t srcalign, destalign, srclen, destlen;155char src[15+3+64]; /* 15 offsets + 64 max length + NUL + sentinels */156char dest[15+2+64]; /* 15 offsets + 64 max length + sentinels */157158for (srcalign = 0; srcalign < 16; srcalign++)159for (destalign = 0; destalign < 16; destalign++)160for (srclen = 0; srclen < 64; srclen++)161for (destlen = 0; destlen < 64; destlen++)162test_sentinel(dest+destalign+1,163src+srcalign+1, destlen, srclen);164}165166ATF_TP_ADD_TCS(tp)167{168void *dl_handle;169170dl_handle = dlopen(NULL, RTLD_LAZY);171stpncpy_fn = dlsym(dl_handle, "test_stpncpy");172if (stpncpy_fn == NULL)173stpncpy_fn = stpncpy;174175ATF_TP_ADD_TC(tp, null);176ATF_TP_ADD_TC(tp, bounds);177ATF_TP_ADD_TC(tp, alignments);178179return (atf_no_error());180}181182183