strerror_test.c revision 108045
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/tools/regression/lib/libc/string/test-strerror.c 108045 2002-12-18 16:59:08Z mike $ 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 36108045Smikeint 37108045Smikemain(void) 38108045Smike{ 39108045Smike char buf[64]; 40108045Smike char *sret; 41108045Smike int iret; 42108045Smike 43108045Smike /* 44108045Smike * strerror() failure tests. 45108045Smike */ 46108045Smike errno = 0; 47108045Smike sret = strerror(0); 48108045Smike assert(strcmp(sret, "Unknown error: 0") == 0); 49108045Smike assert(errno == EINVAL); 50108045Smike 51108045Smike errno = 0; 52108045Smike sret = strerror(INT_MAX); 53108045Smike snprintf(buf, sizeof(buf), "Unknown error: %d", INT_MAX); 54108045Smike assert(strcmp(sret, buf) == 0); 55108045Smike assert(errno == EINVAL); 56108045Smike 57108045Smike /* 58108045Smike * strerror() success tests. 59108045Smike */ 60108045Smike errno = 0; 61108045Smike sret = strerror(EPERM); 62108045Smike assert(strcmp(sret, "Operation not permitted") == 0); 63108045Smike assert(errno == 0); 64108045Smike 65108045Smike errno = 0; 66108045Smike sret = strerror(EPFNOSUPPORT); 67108045Smike assert(strcmp(sret, "Protocol family not supported") == 0); 68108045Smike assert(errno == 0); 69108045Smike 70108045Smike errno = 0; 71108045Smike sret = strerror(ELAST); 72108045Smike assert(errno == 0); 73108045Smike 74108045Smike printf("PASS strerror()\n"); 75108045Smike 76108045Smike /* 77108045Smike * strerror_r() failure tests. 78108045Smike */ 79108045Smike memset(buf, '*', sizeof(buf)); 80108045Smike iret = strerror_r(0, buf, sizeof(buf)); 81108045Smike assert(strcmp(buf, "Unknown error: 0") == 0); 82108045Smike assert(iret == EINVAL); 83108045Smike 84108045Smike memset(buf, '*', sizeof(buf)); 85108045Smike /* One byte too short. */ 86108045Smike iret = strerror_r(EPERM, buf, strlen("Operation not permitted")); 87108045Smike assert(strcmp(buf, "Operation not permitte") == 0); 88108045Smike assert(iret == ERANGE); 89108045Smike 90108045Smike memset(buf, '*', sizeof(buf)); 91108045Smike /* One byte too short. */ 92108045Smike iret = strerror_r(-1, buf, strlen("Unknown error: -1")); 93108045Smike assert(strcmp(buf, "Unknown error: -") == 0); 94108045Smike assert(iret == EINVAL); 95108045Smike 96108045Smike memset(buf, '*', sizeof(buf)); 97108045Smike /* Two bytes too short. */ 98108045Smike iret = strerror_r(-2, buf, strlen("Unknown error: -2") - 1); 99108045Smike assert(strcmp(buf, "Unknown error: ") == 0); 100108045Smike assert(iret == EINVAL); 101108045Smike 102108045Smike memset(buf, '*', sizeof(buf)); 103108045Smike /* Three bytes too short. */ 104108045Smike iret = strerror_r(-2, buf, strlen("Unknown error: -2") - 2); 105108045Smike assert(strcmp(buf, "Unknown error:") == 0); 106108045Smike assert(iret == EINVAL); 107108045Smike 108108045Smike memset(buf, '*', sizeof(buf)); 109108045Smike /* One byte too short. */ 110108045Smike iret = strerror_r(12345, buf, strlen("Unknown error: 12345")); 111108045Smike assert(strcmp(buf, "Unknown error: 1234") == 0); 112108045Smike assert(iret == EINVAL); 113108045Smike 114108045Smike /* 115108045Smike * strerror_r() success tests. 116108045Smike */ 117108045Smike memset(buf, '*', sizeof(buf)); 118108045Smike iret = strerror_r(EDEADLK, buf, sizeof(buf)); 119108045Smike assert(strcmp(buf, "Resource deadlock avoided") == 0); 120108045Smike assert(iret == 0); 121108045Smike 122108045Smike memset(buf, '*', sizeof(buf)); 123108045Smike iret = strerror_r(EPROCLIM, buf, sizeof(buf)); 124108045Smike assert(strcmp(buf, "Too many processes") == 0); 125108045Smike assert(iret == 0); 126108045Smike 127108045Smike printf("PASS strerror_r()\n"); 128108045Smike 129108045Smike exit(0); 130108045Smike} 131