1 /* $Id$ */
2 /*
3 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20 #include "test.h"
21 #include <pj/errno.h>
22 #include <pj/log.h>
23 #include <pj/ctype.h>
24 #include <pj/compat/socket.h>
25 #include <pj/string.h>
26
27 #if INCLUDE_ERRNO_TEST
28
29 #define THIS_FILE "errno"
30
31 #if (defined(PJ_WIN32) && PJ_WIN32 != 0) || \
32 (defined(PJ_WIN64) && PJ_WIN64 != 0)
33 # include <windows.h>
34 #endif
35
36 #if defined(PJ_HAS_ERRNO_H) && PJ_HAS_ERRNO_H != 0
37 # include <errno.h>
38 #endif
39
trim_newlines(char * s)40 static void trim_newlines(char *s)
41 {
42 while (*s) {
43 if (*s == '\r' || *s == '\n')
44 *s = ' ';
45 ++s;
46 }
47 }
48
my_strncasecmp(const char * s1,const char * s2,int max_len)49 int my_strncasecmp(const char *s1, const char *s2, int max_len)
50 {
51 while (*s1 && *s2 && max_len > 0) {
52 if (pj_tolower(*s1) != pj_tolower(*s2))
53 return -1;
54 ++s1;
55 ++s2;
56 --max_len;
57 }
58 return 0;
59 }
60
my_stristr(const char * whole,const char * part)61 const char *my_stristr(const char *whole, const char *part)
62 {
63 int part_len = (int)strlen(part);
64 while (*whole) {
65 if (my_strncasecmp(whole, part, part_len) == 0)
66 return whole;
67 ++whole;
68 }
69 return NULL;
70 }
71
errno_test(void)72 int errno_test(void)
73 {
74 enum { CUT = 6 };
75 pj_status_t rc = 0;
76 char errbuf[256];
77
78 PJ_LOG(3,(THIS_FILE, "...errno test: check the msg carefully"));
79
80 PJ_UNUSED_ARG(rc);
81
82 /*
83 * Windows platform error.
84 */
85 # ifdef ERROR_INVALID_DATA
86 rc = PJ_STATUS_FROM_OS(ERROR_INVALID_DATA);
87 pj_set_os_error(rc);
88
89 /* Whole */
90 pj_strerror(rc, errbuf, sizeof(errbuf));
91 trim_newlines(errbuf);
92 PJ_LOG(3,(THIS_FILE, "...msg for rc=ERROR_INVALID_DATA: '%s'", errbuf));
93 if (my_stristr(errbuf, "invalid") == NULL) {
94 PJ_LOG(3, (THIS_FILE,
95 "...error: expecting \"invalid\" string in the msg"));
96 #ifndef PJ_WIN32_WINCE
97 return -20;
98 #endif
99 }
100
101 /* Cut version. */
102 pj_strerror(rc, errbuf, CUT);
103 PJ_LOG(3,(THIS_FILE, "...msg for rc=ERROR_INVALID_DATA (cut): '%s'", errbuf));
104 # endif
105
106 /*
107 * Unix errors
108 */
109 # if defined(EINVAL) && !defined(PJ_SYMBIAN) && !defined(PJ_WIN32) \
110 && !defined(PJ_WIN64)
111
112 rc = PJ_STATUS_FROM_OS(EINVAL);
113 pj_set_os_error(rc);
114
115 /* Whole */
116 pj_strerror(rc, errbuf, sizeof(errbuf));
117 trim_newlines(errbuf);
118 PJ_LOG(3,(THIS_FILE, "...msg for rc=EINVAL: '%s'", errbuf));
119 if (my_stristr(errbuf, "invalid") == NULL) {
120 PJ_LOG(3, (THIS_FILE,
121 "...error: expecting \"invalid\" string in the msg"));
122 return -30;
123 }
124
125 /* Cut */
126 pj_strerror(rc, errbuf, CUT);
127 PJ_LOG(3,(THIS_FILE, "...msg for rc=EINVAL (cut): '%s'", errbuf));
128 # endif
129
130 /*
131 * Windows WSA errors
132 */
133 # ifdef WSAEINVAL
134 rc = PJ_STATUS_FROM_OS(WSAEINVAL);
135 pj_set_os_error(rc);
136
137 /* Whole */
138 pj_strerror(rc, errbuf, sizeof(errbuf));
139 trim_newlines(errbuf);
140 PJ_LOG(3,(THIS_FILE, "...msg for rc=WSAEINVAL: '%s'", errbuf));
141 if (my_stristr(errbuf, "invalid") == NULL) {
142 PJ_LOG(3, (THIS_FILE,
143 "...error: expecting \"invalid\" string in the msg"));
144 return -40;
145 }
146
147 /* Cut */
148 pj_strerror(rc, errbuf, CUT);
149 PJ_LOG(3,(THIS_FILE, "...msg for rc=WSAEINVAL (cut): '%s'", errbuf));
150 # endif
151
152 pj_strerror(PJ_EBUG, errbuf, sizeof(errbuf));
153 PJ_LOG(3,(THIS_FILE, "...msg for rc=PJ_EBUG: '%s'", errbuf));
154 if (my_stristr(errbuf, "BUG") == NULL) {
155 PJ_LOG(3, (THIS_FILE,
156 "...error: expecting \"BUG\" string in the msg"));
157 return -20;
158 }
159
160 pj_strerror(PJ_EBUG, errbuf, CUT);
161 PJ_LOG(3,(THIS_FILE, "...msg for rc=PJ_EBUG, cut at %d chars: '%s'",
162 CUT, errbuf));
163
164 /* Perror */
165 pj_perror(3, THIS_FILE, PJ_SUCCESS, "...testing %s", "pj_perror");
166 PJ_PERROR(3,(THIS_FILE, PJ_SUCCESS, "...testing %s", "PJ_PERROR"));
167
168 return 0;
169 }
170
171
172 #endif /* INCLUDE_ERRNO_TEST */
173
174
175