1189140Sdas/*- 2189140Sdas * Copyright (c) 2009 David Schultz <das@FreeBSD.org> 3189140Sdas * All rights reserved. 4189140Sdas * 5189140Sdas * Redistribution and use in source and binary forms, with or without 6189140Sdas * modification, are permitted provided that the following conditions 7189140Sdas * are met: 8189140Sdas * 1. Redistributions of source code must retain the above copyright 9189140Sdas * notice, this list of conditions and the following disclaimer. 10189140Sdas * 2. Redistributions in binary form must reproduce the above copyright 11189140Sdas * notice, this list of conditions and the following disclaimer in the 12189140Sdas * documentation and/or other materials provided with the distribution. 13189140Sdas * 14189140Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15189140Sdas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16189140Sdas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17189140Sdas * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18189140Sdas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19189140Sdas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20189140Sdas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21189140Sdas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22189140Sdas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23189140Sdas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24189140Sdas * SUCH DAMAGE. 25189140Sdas */ 26189140Sdas 27189140Sdas#include <sys/cdefs.h> 28189140Sdas__FBSDID("$FreeBSD: releng/10.3/lib/libc/tests/string/wcsnlen_test.c 290875 2015-11-15 20:08:34Z ngie $"); 29189140Sdas 30290539Sngie#include <sys/param.h> 31189140Sdas#include <sys/mman.h> 32189140Sdas#include <assert.h> 33189140Sdas#include <stdio.h> 34189140Sdas#include <stdlib.h> 35189140Sdas#include <string.h> 36189140Sdas#include <wchar.h> 37189140Sdas 38290539Sngie#include <atf-c.h> 39290539Sngie 40189140Sdasstatic void * 41189140Sdasmakebuf(size_t len, int guard_at_end) 42189140Sdas{ 43189140Sdas char *buf; 44189140Sdas size_t alloc_size = roundup2(len, PAGE_SIZE) + PAGE_SIZE; 45189140Sdas 46189140Sdas buf = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0); 47290539Sngie ATF_CHECK(buf); 48189140Sdas if (guard_at_end) { 49290539Sngie ATF_CHECK(munmap(buf + alloc_size - PAGE_SIZE, PAGE_SIZE) == 0); 50189140Sdas return (buf + alloc_size - PAGE_SIZE - len); 51189140Sdas } else { 52290539Sngie ATF_CHECK(munmap(buf, PAGE_SIZE) == 0); 53189140Sdas return (buf + PAGE_SIZE); 54189140Sdas } 55189140Sdas} 56189140Sdas 57189140Sdasstatic void 58189140Sdastest_wcsnlen(const wchar_t *s) 59189140Sdas{ 60189140Sdas wchar_t *s1; 61189140Sdas size_t size, len, bufsize; 62189140Sdas int i; 63189140Sdas 64189140Sdas size = wcslen(s) + 1; 65189140Sdas for (i = 0; i <= 1; i++) { 66290539Sngie for (bufsize = 0; bufsize <= size + 10; bufsize++) { 67290539Sngie s1 = makebuf(bufsize * sizeof(wchar_t), i); 68290539Sngie wmemcpy(s1, s, bufsize); 69290539Sngie len = (size > bufsize) ? bufsize : size - 1; 70290539Sngie ATF_CHECK(wcsnlen(s1, bufsize) == len); 71290539Sngie } 72189140Sdas } 73189140Sdas} 74189140Sdas 75290539SngieATF_TC_WITHOUT_HEAD(nul); 76290539SngieATF_TC_BODY(nul, tc) 77189140Sdas{ 78189140Sdas 79290539Sngie test_wcsnlen(L""); 80290539Sngie} 81189140Sdas 82290539SngieATF_TC_WITHOUT_HEAD(foo); 83290539SngieATF_TC_BODY(foo, tc) 84290539Sngie{ 85290539Sngie 86189140Sdas test_wcsnlen(L"foo"); 87290539Sngie} 88290539Sngie 89290539SngieATF_TC_WITHOUT_HEAD(glorp); 90290539SngieATF_TC_BODY(glorp, tc) 91290539Sngie{ 92290539Sngie 93189140Sdas test_wcsnlen(L"glorp"); 94290539Sngie} 95189140Sdas 96290539SngieATF_TP_ADD_TCS(tp) 97290539Sngie{ 98290539Sngie 99290539Sngie ATF_TP_ADD_TC(tp, nul); 100290539Sngie ATF_TP_ADD_TC(tp, foo); 101290539Sngie ATF_TP_ADD_TC(tp, glorp); 102290539Sngie 103290539Sngie return (atf_no_error()); 104189140Sdas} 105