Debian Codegraph @

Xpjproject/pjlib/src/pjlib-test/errno.c

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