Path: blob/main/lib/libc/tests/string/strlcpy_test.c
39485 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/cdefs.h>31#include <sys/param.h>32#include <sys/mman.h>33#include <assert.h>34#include <dlfcn.h>35#include <stdio.h>36#include <stdlib.h>37#include <string.h>3839#include <atf-c.h>4041size_t (*strlcpy_fn)(char *restrict, const char *restrict, size_t);4243static char *44makebuf(size_t len, int guard_at_end)45{46char *buf;47size_t alloc_size, page_size;4849page_size = getpagesize();50alloc_size = roundup2(len, page_size) + page_size;5152buf = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);53assert(buf);54if (guard_at_end) {55assert(munmap(buf + alloc_size - page_size, page_size) == 0);56return (buf + alloc_size - page_size - len);57} else {58assert(munmap(buf, page_size) == 0);59return (buf + page_size);60}61}6263static void64test_strlcpy(const char *s)65{66char *src, *dst;67size_t size, bufsize, x;68int i, j;6970size = strlen(s) + 1;71for (i = 0; i <= 1; i++) {72for (j = 0; j <= 1; j++) {73for (bufsize = 0; bufsize <= size + 10; bufsize++) {74src = makebuf(size, i);75memcpy(src, s, size);76dst = makebuf(bufsize, j);77memset(dst, 'X', bufsize);78assert(strlcpy_fn(dst, src, bufsize) == size-1);79assert(bufsize == 0 || strncmp(src, dst, bufsize - 1) == 0);80for (x = size; x < bufsize; x++)81assert(dst[x] == 'X');82}83}84}85}8687static void88test_sentinel(char *dest, char *src, size_t destlen, size_t srclen)89{90size_t i;91size_t 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, '\xee', destlen);104105/* destination sentinels: not to be touched */106dest[-1] = '{';107dest[destlen] = '}';108109wantres = srclen;110res = strlcpy_fn(dest, src, destlen);111112if (dest[-1] != '{')113fail = "start sentinel overwritten";114else if (dest[destlen] != '}')115fail = "end sentinel overwritten";116else if (res != wantres)117fail = "incorrect return value";118else if (destlen > 0 && strncmp(src, dest, destlen - 1) != 0)119fail = "string not copied correctly";120else if (destlen > 0 && srclen >= destlen - 1 && dest[destlen-1] != '\0')121fail = "string not NUL terminated";122else for (i = srclen + 1; i < destlen; i++)123if (dest[i] != '\xee') {124fail = "buffer mutilated behind string";125break;126}127128if (fail)129atf_tc_fail_nonfatal("%s\n"130"strlcpy(%p \"%s\", %p \"%s\", %zu) = %zu (want %zu)\n",131fail, dest, dest, src, src, destlen, res, wantres);132}133134ATF_TC_WITHOUT_HEAD(null);135ATF_TC_BODY(null, tc)136{137ATF_CHECK_EQ(strlcpy_fn(NULL, "foo", 0), 3);138}139140ATF_TC_WITHOUT_HEAD(bounds);141ATF_TC_BODY(bounds, tc)142{143size_t i;144char buf[64+1];145146for (i = 0; i < sizeof(buf) - 1; i++) {147buf[i] = ' ' + i;148buf[i+1] = '\0';149test_strlcpy(buf);150}151}152153ATF_TC_WITHOUT_HEAD(alignments);154ATF_TC_BODY(alignments, tc)155{156size_t srcalign, destalign, srclen, destlen;157char src[15+3+64]; /* 15 offsets + 64 max length + NUL + sentinels */158char dest[15+2+64]; /* 15 offsets + 64 max length + sentinels */159160for (srcalign = 0; srcalign < 16; srcalign++)161for (destalign = 0; destalign < 16; destalign++)162for (srclen = 0; srclen < 64; srclen++)163for (destlen = 0; destlen < 64; destlen++)164test_sentinel(dest+destalign+1,165src+srcalign+1, destlen, srclen);166}167168ATF_TP_ADD_TCS(tp)169{170void *dl_handle;171172dl_handle = dlopen(NULL, RTLD_LAZY);173strlcpy_fn = dlsym(dl_handle, "test_strlcpy");174if (strlcpy_fn == NULL)175strlcpy_fn = strlcpy;176177ATF_TP_ADD_TC(tp, null);178ATF_TP_ADD_TC(tp, bounds);179ATF_TP_ADD_TC(tp, alignments);180181return (atf_no_error());182}183184185