strerror_test.c revision 220376
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 220376 2011-04-05 21:56:05Z jilles $ 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 36141092Snik#include <tap.h> 37141092Snik 38108045Smikeint 39108045Smikemain(void) 40108045Smike{ 41108045Smike char buf[64]; 42108045Smike char *sret; 43108045Smike int iret; 44108045Smike 45220376Sjilles plan_tests(27); 46137587Snik 47108045Smike /* 48108045Smike * strerror() failure tests. 49108045Smike */ 50108045Smike errno = 0; 51108045Smike sret = strerror(INT_MAX); 52108045Smike snprintf(buf, sizeof(buf), "Unknown error: %d", INT_MAX); 53141092Snik ok1(strcmp(sret, buf) == 0); 54141092Snik ok1(errno == EINVAL); 55108045Smike 56108045Smike /* 57108045Smike * strerror() success tests. 58108045Smike */ 59108045Smike errno = 0; 60220376Sjilles sret = strerror(0); 61220376Sjilles ok1(strcmp(sret, "No error: 0") == 0); 62220376Sjilles ok1(errno == 0); 63220376Sjilles 64220376Sjilles errno = 0; 65108045Smike sret = strerror(EPERM); 66141092Snik ok1(strcmp(sret, "Operation not permitted") == 0); 67141092Snik ok1(errno == 0); 68108045Smike 69108045Smike errno = 0; 70108045Smike sret = strerror(EPFNOSUPPORT); 71141092Snik ok1(strcmp(sret, "Protocol family not supported") == 0); 72141092Snik ok1(errno == 0); 73108045Smike 74108045Smike errno = 0; 75108045Smike sret = strerror(ELAST); 76141092Snik ok1(errno == 0); 77108045Smike 78108045Smike /* 79108045Smike * strerror_r() failure tests. 80108045Smike */ 81108045Smike memset(buf, '*', sizeof(buf)); 82220376Sjilles iret = strerror_r(-1, buf, sizeof(buf)); 83220376Sjilles ok1(strcmp(buf, "Unknown error: -1") == 0); 84141092Snik ok1(iret == EINVAL); 85108045Smike 86108045Smike memset(buf, '*', sizeof(buf)); 87108045Smike /* One byte too short. */ 88108045Smike iret = strerror_r(EPERM, buf, strlen("Operation not permitted")); 89141092Snik ok1(strcmp(buf, "Operation not permitte") == 0); 90141092Snik ok1(iret == ERANGE); 91108045Smike 92108045Smike memset(buf, '*', sizeof(buf)); 93108045Smike /* One byte too short. */ 94108045Smike iret = strerror_r(-1, buf, strlen("Unknown error: -1")); 95141092Snik ok1(strcmp(buf, "Unknown error: -") == 0); 96141092Snik ok1(iret == EINVAL); 97108045Smike 98108045Smike memset(buf, '*', sizeof(buf)); 99108045Smike /* Two bytes too short. */ 100108045Smike iret = strerror_r(-2, buf, strlen("Unknown error: -2") - 1); 101141092Snik ok1(strcmp(buf, "Unknown error: ") == 0); 102141092Snik ok1(iret == EINVAL); 103108045Smike 104108045Smike memset(buf, '*', sizeof(buf)); 105108045Smike /* Three bytes too short. */ 106108045Smike iret = strerror_r(-2, buf, strlen("Unknown error: -2") - 2); 107141092Snik ok1(strcmp(buf, "Unknown error:") == 0); 108141092Snik ok1(iret == EINVAL); 109108045Smike 110108045Smike memset(buf, '*', sizeof(buf)); 111108045Smike /* One byte too short. */ 112108045Smike iret = strerror_r(12345, buf, strlen("Unknown error: 12345")); 113141092Snik ok1(strcmp(buf, "Unknown error: 1234") == 0); 114141092Snik ok1(iret == EINVAL); 115108045Smike 116108045Smike /* 117108045Smike * strerror_r() success tests. 118108045Smike */ 119108045Smike memset(buf, '*', sizeof(buf)); 120220376Sjilles iret = strerror_r(0, buf, sizeof(buf)); 121220376Sjilles ok1(strcmp(buf, "No error: 0") == 0); 122220376Sjilles ok1(iret == 0); 123220376Sjilles 124220376Sjilles memset(buf, '*', sizeof(buf)); 125108045Smike iret = strerror_r(EDEADLK, buf, sizeof(buf)); 126141092Snik ok1(strcmp(buf, "Resource deadlock avoided") == 0); 127141092Snik ok1(iret == 0); 128108045Smike 129108045Smike memset(buf, '*', sizeof(buf)); 130108045Smike iret = strerror_r(EPROCLIM, buf, sizeof(buf)); 131141092Snik ok1(strcmp(buf, "Too many processes") == 0); 132141092Snik ok1(iret == 0); 133108045Smike 134141092Snik return exit_status(); 135108045Smike} 136