wcsnlen_test.c revision 290875
1147476Sdumbbell/*- 2147476Sdumbbell * Copyright (c) 2009 David Schultz <das@FreeBSD.org> 3147476Sdumbbell * All rights reserved. 4147476Sdumbbell * 5147476Sdumbbell * Redistribution and use in source and binary forms, with or without 6147476Sdumbbell * modification, are permitted provided that the following conditions 7147476Sdumbbell * are met: 8147476Sdumbbell * 1. Redistributions of source code must retain the above copyright 9147476Sdumbbell * notice, this list of conditions and the following disclaimer. 10147476Sdumbbell * 2. Redistributions in binary form must reproduce the above copyright 11147476Sdumbbell * notice, this list of conditions and the following disclaimer in the 12147476Sdumbbell * documentation and/or other materials provided with the distribution. 13147476Sdumbbell * 14147476Sdumbbell * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15147476Sdumbbell * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16147476Sdumbbell * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17147476Sdumbbell * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18147476Sdumbbell * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19147476Sdumbbell * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20147476Sdumbbell * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21147476Sdumbbell * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22147476Sdumbbell * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23147476Sdumbbell * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24147476Sdumbbell * SUCH DAMAGE. 25147476Sdumbbell */ 26147476Sdumbbell 27147476Sdumbbell#include <sys/cdefs.h> 28147476Sdumbbell__FBSDID("$FreeBSD: stable/10/lib/libc/tests/string/wcsnlen_test.c 290875 2015-11-15 20:08:34Z ngie $"); 29147476Sdumbbell 30147476Sdumbbell#include <sys/param.h> 31147476Sdumbbell#include <sys/mman.h> 32147476Sdumbbell#include <assert.h> 33147476Sdumbbell#include <stdio.h> 34147476Sdumbbell#include <stdlib.h> 35147476Sdumbbell#include <string.h> 36147476Sdumbbell#include <wchar.h> 37147476Sdumbbell 38147476Sdumbbell#include <atf-c.h> 39147476Sdumbbell 40147476Sdumbbellstatic void * 41147476Sdumbbellmakebuf(size_t len, int guard_at_end) 42147476Sdumbbell{ 43147476Sdumbbell char *buf; 44147476Sdumbbell size_t alloc_size = roundup2(len, PAGE_SIZE) + PAGE_SIZE; 45147476Sdumbbell 46147476Sdumbbell buf = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0); 47147476Sdumbbell ATF_CHECK(buf); 48147476Sdumbbell if (guard_at_end) { 49147476Sdumbbell ATF_CHECK(munmap(buf + alloc_size - PAGE_SIZE, PAGE_SIZE) == 0); 50147476Sdumbbell return (buf + alloc_size - PAGE_SIZE - len); 51147476Sdumbbell } else { 52147476Sdumbbell ATF_CHECK(munmap(buf, PAGE_SIZE) == 0); 53147476Sdumbbell return (buf + PAGE_SIZE); 54147476Sdumbbell } 55147476Sdumbbell} 56147476Sdumbbell 57147476Sdumbbellstatic void 58147476Sdumbbelltest_wcsnlen(const wchar_t *s) 59147476Sdumbbell{ 60147476Sdumbbell wchar_t *s1; 61147476Sdumbbell size_t size, len, bufsize; 62147476Sdumbbell int i; 63147476Sdumbbell 64147476Sdumbbell size = wcslen(s) + 1; 65147476Sdumbbell for (i = 0; i <= 1; i++) { 66147476Sdumbbell for (bufsize = 0; bufsize <= size + 10; bufsize++) { 67147476Sdumbbell s1 = makebuf(bufsize * sizeof(wchar_t), i); 68147476Sdumbbell wmemcpy(s1, s, bufsize); 69147476Sdumbbell len = (size > bufsize) ? bufsize : size - 1; 70147476Sdumbbell ATF_CHECK(wcsnlen(s1, bufsize) == len); 71147476Sdumbbell } 72147476Sdumbbell } 73147476Sdumbbell} 74147476Sdumbbell 75147476SdumbbellATF_TC_WITHOUT_HEAD(nul); 76147476SdumbbellATF_TC_BODY(nul, tc) 77147476Sdumbbell{ 78147476Sdumbbell 79147476Sdumbbell test_wcsnlen(L""); 80147476Sdumbbell} 81147476Sdumbbell 82147476SdumbbellATF_TC_WITHOUT_HEAD(foo); 83147476SdumbbellATF_TC_BODY(foo, tc) 84147476Sdumbbell{ 85147476Sdumbbell 86147476Sdumbbell test_wcsnlen(L"foo"); 87147476Sdumbbell} 88147476Sdumbbell 89147476SdumbbellATF_TC_WITHOUT_HEAD(glorp); 90147476SdumbbellATF_TC_BODY(glorp, tc) 91147476Sdumbbell{ 92147476Sdumbbell 93147476Sdumbbell test_wcsnlen(L"glorp"); 94147476Sdumbbell} 95147476Sdumbbell 96147476SdumbbellATF_TP_ADD_TCS(tp) 97147476Sdumbbell{ 98147476Sdumbbell 99147476Sdumbbell ATF_TP_ADD_TC(tp, nul); 100147476Sdumbbell ATF_TP_ADD_TC(tp, foo); 101147476Sdumbbell ATF_TP_ADD_TC(tp, glorp); 102147476Sdumbbell 103147476Sdumbbell return (atf_no_error()); 104147476Sdumbbell} 105147476Sdumbbell