strerror_test.c revision 290539
1108045Smike/*- 2108045Smike * Copyright (c) 2001 Wes Peters <wes@FreeBSD.org> 3108045Smike * All rights reserved. 4108045Smike * 5108045Smike * Redistribution and use in source and binary forms, with or without 6108045Smike * modification, are permitted provided that the following conditions 7108045Smike * are met: 8108045Smike * 1. Redistributions of source code must retain the above copyright 9108045Smike * notice, this list of conditions and the following disclaimer. 10108045Smike * 2. Redistributions in binary form must reproduce the above copyright 11108045Smike * notice, this list of conditions and the following disclaimer in the 12108045Smike * documentation and/or other materials provided with the distribution. 13108045Smike * 14108045Smike * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15108045Smike * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16108045Smike * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17108045Smike * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18108045Smike * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19108045Smike * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20108045Smike * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21108045Smike * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22108045Smike * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23108045Smike * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24108045Smike * SUCH DAMAGE. 25108045Smike * 26108045Smike * $FreeBSD: head/lib/libc/tests/string/strerror_test.c 290539 2015-11-08 07:53:31Z ngie $ 27108045Smike */ 28108045Smike 29108045Smike#include <assert.h> 30108045Smike#include <errno.h> 31108045Smike#include <limits.h> 32108045Smike#include <stdio.h> 33108045Smike#include <stdlib.h> 34108045Smike#include <string.h> 35108045Smike 36290539Sngie#include <atf-c.h> 37141092Snik 38290539Sngiestatic char buf[64]; 39290539Sngiestatic char *sret; 40290539Sngiestatic int iret; 41263085Sjmmv 42290539SngieATF_TC_WITHOUT_HEAD(strerror_unknown_error); 43290539SngieATF_TC_BODY(strerror_unknown_error, tc) 44108045Smike{ 45108045Smike 46108045Smike errno = 0; 47108045Smike sret = strerror(INT_MAX); 48108045Smike snprintf(buf, sizeof(buf), "Unknown error: %d", INT_MAX); 49290539Sngie ATF_CHECK(strcmp(sret, buf) == 0); 50290539Sngie ATF_CHECK(errno == EINVAL); 51290539Sngie} 52108045Smike 53290539SngieATF_TC_WITHOUT_HEAD(strerror_no_error); 54290539SngieATF_TC_BODY(strerror_no_error, tc) 55290539Sngie{ 56290539Sngie 57108045Smike errno = 0; 58220376Sjilles sret = strerror(0); 59290539Sngie ATF_CHECK(strcmp(sret, "No error: 0") == 0); 60290539Sngie ATF_CHECK(errno == 0); 61290539Sngie} 62220376Sjilles 63290539SngieATF_TC_WITHOUT_HEAD(strerror_EPERM_test); 64290539SngieATF_TC_BODY(strerror_EPERM_test, tc) 65290539Sngie{ 66290539Sngie 67220376Sjilles errno = 0; 68108045Smike sret = strerror(EPERM); 69290539Sngie ATF_CHECK(strcmp(sret, "Operation not permitted") == 0); 70290539Sngie ATF_CHECK(errno == 0); 71290539Sngie} 72108045Smike 73290539SngieATF_TC_WITHOUT_HEAD(strerror_EPFNOSUPPORT_test); 74290539SngieATF_TC_BODY(strerror_EPFNOSUPPORT_test, tc) 75290539Sngie{ 76290539Sngie 77108045Smike errno = 0; 78108045Smike sret = strerror(EPFNOSUPPORT); 79290539Sngie ATF_CHECK(strcmp(sret, "Protocol family not supported") == 0); 80290539Sngie ATF_CHECK(errno == 0); 81290539Sngie} 82108045Smike 83290539SngieATF_TC_WITHOUT_HEAD(strerror_ELAST_test); 84290539SngieATF_TC_BODY(strerror_ELAST_test, tc) 85290539Sngie{ 86290539Sngie 87108045Smike errno = 0; 88108045Smike sret = strerror(ELAST); 89290539Sngie ATF_CHECK(errno == 0); 90290539Sngie} 91108045Smike 92290539SngieATF_TC_WITHOUT_HEAD(strerror_r__unknown_error); 93290539SngieATF_TC_BODY(strerror_r__unknown_error, tc) 94290539Sngie{ 95290539Sngie 96108045Smike memset(buf, '*', sizeof(buf)); 97220376Sjilles iret = strerror_r(-1, buf, sizeof(buf)); 98290539Sngie ATF_CHECK(strcmp(buf, "Unknown error: -1") == 0); 99290539Sngie ATF_CHECK(iret == EINVAL); 100290539Sngie} 101108045Smike 102290539SngieATF_TC_WITHOUT_HEAD(strerror_r__EPERM_one_byte_short); 103290539SngieATF_TC_BODY(strerror_r__EPERM_one_byte_short, tc) 104290539Sngie{ 105290539Sngie 106108045Smike memset(buf, '*', sizeof(buf)); 107108045Smike /* One byte too short. */ 108108045Smike iret = strerror_r(EPERM, buf, strlen("Operation not permitted")); 109290539Sngie ATF_CHECK(strcmp(buf, "Operation not permitte") == 0); 110290539Sngie ATF_CHECK(iret == ERANGE); 111290539Sngie} 112108045Smike 113290539SngieATF_TC_WITHOUT_HEAD(strerror_r__EPERM_unknown_error_one_byte_short); 114290539SngieATF_TC_BODY(strerror_r__EPERM_unknown_error_one_byte_short, tc) 115290539Sngie{ 116290539Sngie 117108045Smike memset(buf, '*', sizeof(buf)); 118108045Smike /* One byte too short. */ 119108045Smike iret = strerror_r(-1, buf, strlen("Unknown error: -1")); 120290539Sngie ATF_CHECK(strcmp(buf, "Unknown error: -") == 0); 121290539Sngie ATF_CHECK(iret == EINVAL); 122290539Sngie} 123108045Smike 124290539SngieATF_TC_WITHOUT_HEAD(strerror_r__EPERM_unknown_error_two_bytes_short); 125290539SngieATF_TC_BODY(strerror_r__EPERM_unknown_error_two_bytes_short, tc) 126290539Sngie{ 127290539Sngie 128108045Smike memset(buf, '*', sizeof(buf)); 129108045Smike /* Two bytes too short. */ 130108045Smike iret = strerror_r(-2, buf, strlen("Unknown error: -2") - 1); 131290539Sngie ATF_CHECK(strcmp(buf, "Unknown error: ") == 0); 132290539Sngie ATF_CHECK(iret == EINVAL); 133290539Sngie} 134108045Smike 135290539SngieATF_TC_WITHOUT_HEAD(strerror_r__EPERM_unknown_error_three_bytes_short); 136290539SngieATF_TC_BODY(strerror_r__EPERM_unknown_error_three_bytes_short, tc) 137290539Sngie{ 138290539Sngie 139108045Smike memset(buf, '*', sizeof(buf)); 140108045Smike /* Three bytes too short. */ 141108045Smike iret = strerror_r(-2, buf, strlen("Unknown error: -2") - 2); 142290539Sngie ATF_CHECK(strcmp(buf, "Unknown error:") == 0); 143290539Sngie ATF_CHECK(iret == EINVAL); 144290539Sngie} 145108045Smike 146290539SngieATF_TC_WITHOUT_HEAD(strerror_r__EPERM_unknown_error_12345_one_byte_short); 147290539SngieATF_TC_BODY(strerror_r__EPERM_unknown_error_12345_one_byte_short, tc) 148290539Sngie{ 149290539Sngie 150108045Smike memset(buf, '*', sizeof(buf)); 151108045Smike /* One byte too short. */ 152108045Smike iret = strerror_r(12345, buf, strlen("Unknown error: 12345")); 153290539Sngie ATF_CHECK(strcmp(buf, "Unknown error: 1234") == 0); 154290539Sngie ATF_CHECK(iret == EINVAL); 155290539Sngie} 156108045Smike 157290539SngieATF_TC_WITHOUT_HEAD(strerror_r__no_error); 158290539SngieATF_TC_BODY(strerror_r__no_error, tc) 159290539Sngie{ 160290539Sngie 161108045Smike memset(buf, '*', sizeof(buf)); 162220376Sjilles iret = strerror_r(0, buf, sizeof(buf)); 163290539Sngie ATF_CHECK(strcmp(buf, "No error: 0") == 0); 164290539Sngie ATF_CHECK(iret == 0); 165290539Sngie} 166220376Sjilles 167290539SngieATF_TC_WITHOUT_HEAD(strerror_r__EDEADLK); 168290539SngieATF_TC_BODY(strerror_r__EDEADLK, tc) 169290539Sngie{ 170290539Sngie 171220376Sjilles memset(buf, '*', sizeof(buf)); 172108045Smike iret = strerror_r(EDEADLK, buf, sizeof(buf)); 173290539Sngie ATF_CHECK(strcmp(buf, "Resource deadlock avoided") == 0); 174290539Sngie ATF_CHECK(iret == 0); 175290539Sngie} 176108045Smike 177290539SngieATF_TC_WITHOUT_HEAD(strerror_r__EPROCLIM); 178290539SngieATF_TC_BODY(strerror_r__EPROCLIM, tc) 179290539Sngie{ 180290539Sngie 181108045Smike memset(buf, '*', sizeof(buf)); 182108045Smike iret = strerror_r(EPROCLIM, buf, sizeof(buf)); 183290539Sngie ATF_CHECK(strcmp(buf, "Too many processes") == 0); 184290539Sngie ATF_CHECK(iret == 0); 185290539Sngie} 186108045Smike 187290539SngieATF_TP_ADD_TCS(tp) 188290539Sngie{ 189290539Sngie 190290539Sngie ATF_TP_ADD_TC(tp, strerror_unknown_error); 191290539Sngie ATF_TP_ADD_TC(tp, strerror_no_error); 192290539Sngie ATF_TP_ADD_TC(tp, strerror_EPERM_test); 193290539Sngie ATF_TP_ADD_TC(tp, strerror_EPFNOSUPPORT_test); 194290539Sngie ATF_TP_ADD_TC(tp, strerror_ELAST_test); 195290539Sngie ATF_TP_ADD_TC(tp, strerror_r__unknown_error); 196290539Sngie ATF_TP_ADD_TC(tp, strerror_r__EPERM_one_byte_short); 197290539Sngie ATF_TP_ADD_TC(tp, strerror_r__EPERM_unknown_error_one_byte_short); 198290539Sngie ATF_TP_ADD_TC(tp, strerror_r__EPERM_unknown_error_two_bytes_short); 199290539Sngie ATF_TP_ADD_TC(tp, strerror_r__EPERM_unknown_error_three_bytes_short); 200290539Sngie ATF_TP_ADD_TC(tp, strerror_r__EPERM_unknown_error_12345_one_byte_short); 201290539Sngie ATF_TP_ADD_TC(tp, strerror_r__no_error); 202290539Sngie ATF_TP_ADD_TC(tp, strerror_r__EDEADLK); 203290539Sngie ATF_TP_ADD_TC(tp, strerror_r__EPROCLIM); 204290539Sngie 205290539Sngie return (atf_no_error()); 206108045Smike} 207