1 /*-
2 * Copyright (c) 2001 Wes Peters <wes@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: stable/9/tools/regression/lib/libc/string/test-strerror.c 220376 2011-04-05 21:56:05Z jilles $
27 */
28
29 #include <assert.h>
30 #include <errno.h>
31 #include <limits.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include <tap.h>
37
38 int
main(void)39 main(void)
40 {
41 char buf[64];
42 char *sret;
43 int iret;
44
45 plan_tests(27);
46
47 /*
48 * strerror() failure tests.
49 */
50 errno = 0;
51 sret = strerror(INT_MAX);
52 snprintf(buf, sizeof(buf), "Unknown error: %d", INT_MAX);
53 ok1(strcmp(sret, buf) == 0);
54 ok1(errno == EINVAL);
55
56 /*
57 * strerror() success tests.
58 */
59 errno = 0;
60 sret = strerror(0);
61 ok1(strcmp(sret, "No error: 0") == 0);
62 ok1(errno == 0);
63
64 errno = 0;
65 sret = strerror(EPERM);
66 ok1(strcmp(sret, "Operation not permitted") == 0);
67 ok1(errno == 0);
68
69 errno = 0;
70 sret = strerror(EPFNOSUPPORT);
71 ok1(strcmp(sret, "Protocol family not supported") == 0);
72 ok1(errno == 0);
73
74 errno = 0;
75 sret = strerror(ELAST);
76 ok1(errno == 0);
77
78 /*
79 * strerror_r() failure tests.
80 */
81 memset(buf, '*', sizeof(buf));
82 iret = strerror_r(-1, buf, sizeof(buf));
83 ok1(strcmp(buf, "Unknown error: -1") == 0);
84 ok1(iret == EINVAL);
85
86 memset(buf, '*', sizeof(buf));
87 /* One byte too short. */
88 iret = strerror_r(EPERM, buf, strlen("Operation not permitted"));
89 ok1(strcmp(buf, "Operation not permitte") == 0);
90 ok1(iret == ERANGE);
91
92 memset(buf, '*', sizeof(buf));
93 /* One byte too short. */
94 iret = strerror_r(-1, buf, strlen("Unknown error: -1"));
95 ok1(strcmp(buf, "Unknown error: -") == 0);
96 ok1(iret == EINVAL);
97
98 memset(buf, '*', sizeof(buf));
99 /* Two bytes too short. */
100 iret = strerror_r(-2, buf, strlen("Unknown error: -2") - 1);
101 ok1(strcmp(buf, "Unknown error: ") == 0);
102 ok1(iret == EINVAL);
103
104 memset(buf, '*', sizeof(buf));
105 /* Three bytes too short. */
106 iret = strerror_r(-2, buf, strlen("Unknown error: -2") - 2);
107 ok1(strcmp(buf, "Unknown error:") == 0);
108 ok1(iret == EINVAL);
109
110 memset(buf, '*', sizeof(buf));
111 /* One byte too short. */
112 iret = strerror_r(12345, buf, strlen("Unknown error: 12345"));
113 ok1(strcmp(buf, "Unknown error: 1234") == 0);
114 ok1(iret == EINVAL);
115
116 /*
117 * strerror_r() success tests.
118 */
119 memset(buf, '*', sizeof(buf));
120 iret = strerror_r(0, buf, sizeof(buf));
121 ok1(strcmp(buf, "No error: 0") == 0);
122 ok1(iret == 0);
123
124 memset(buf, '*', sizeof(buf));
125 iret = strerror_r(EDEADLK, buf, sizeof(buf));
126 ok1(strcmp(buf, "Resource deadlock avoided") == 0);
127 ok1(iret == 0);
128
129 memset(buf, '*', sizeof(buf));
130 iret = strerror_r(EPROCLIM, buf, sizeof(buf));
131 ok1(strcmp(buf, "Too many processes") == 0);
132 ok1(iret == 0);
133
134 return exit_status();
135 }
136