Path: blob/main/lib/libc/tests/string/wcsnlen_test.c
39491 views
/*-1* Copyright (c) 2009 David Schultz <[email protected]>2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526#include <sys/param.h>27#include <sys/mman.h>28#include <assert.h>29#include <stdio.h>30#include <stdlib.h>31#include <string.h>32#include <wchar.h>3334#include <atf-c.h>3536static void *37makebuf(size_t len, int guard_at_end)38{39char *buf;40size_t alloc_size, page_size;4142page_size = getpagesize();43alloc_size = roundup2(len, page_size) + page_size;4445buf = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);46ATF_CHECK(buf);47if (guard_at_end) {48ATF_CHECK(munmap(buf + alloc_size - page_size, page_size) == 0);49return (buf + alloc_size - page_size - len);50} else {51ATF_CHECK(munmap(buf, page_size) == 0);52return (buf + page_size);53}54}5556static void57test_wcsnlen(const wchar_t *s)58{59wchar_t *s1;60size_t size, len, bufsize;61int i;6263size = wcslen(s) + 1;64for (i = 0; i <= 1; i++) {65for (bufsize = 0; bufsize <= size + 10; bufsize++) {66s1 = makebuf(bufsize * sizeof(wchar_t), i);67wmemcpy(s1, s, bufsize <= size ? bufsize : size);68len = (size > bufsize) ? bufsize : size - 1;69ATF_CHECK(wcsnlen(s1, bufsize) == len);70}71}72}7374ATF_TC_WITHOUT_HEAD(nul);75ATF_TC_BODY(nul, tc)76{7778test_wcsnlen(L"");79}8081ATF_TC_WITHOUT_HEAD(foo);82ATF_TC_BODY(foo, tc)83{8485test_wcsnlen(L"foo");86}8788ATF_TC_WITHOUT_HEAD(glorp);89ATF_TC_BODY(glorp, tc)90{9192test_wcsnlen(L"glorp");93}9495ATF_TP_ADD_TCS(tp)96{9798ATF_TP_ADD_TC(tp, nul);99ATF_TP_ADD_TC(tp, foo);100ATF_TP_ADD_TC(tp, glorp);101102return (atf_no_error());103}104105106