blob: 1a2560c2a61209c7d16032f1239551574960ba88 [file] [log] [blame]
Benny Prijonoe7224612005-11-13 19:40:44 +00001/* $Id$ */
2/*
Benny Prijonoe0312a72005-11-18 00:16:43 +00003 * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org>
Benny Prijonoe7224612005-11-13 19:40:44 +00004 *
Benny Prijonoe0312a72005-11-18 00:16:43 +00005 * 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.
Benny Prijonoe7224612005-11-13 19:40:44 +00009 *
Benny Prijonoe0312a72005-11-18 00:16:43 +000010 * This program is distributed in the hope that it will be useful,
Benny Prijonoe7224612005-11-13 19:40:44 +000011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Benny Prijonoe0312a72005-11-18 00:16:43 +000012 * 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
Benny Prijonoe7224612005-11-13 19:40:44 +000018 */
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# include <windows.h>
32#endif
33
34#if defined(PJ_HAS_ERRNO_H) && PJ_HAS_ERRNO_H != 0
35# include <errno.h>
36#endif
37
38static void trim_newlines(char *s)
39{
40 while (*s) {
41 if (*s == '\r' || *s == '\n')
42 *s = ' ';
43 ++s;
44 }
45}
46
47int my_strncasecmp(const char *s1, const char *s2, int max_len)
48{
49 while (*s1 && *s2 && max_len > 0) {
50 if (pj_tolower(*s1) != pj_tolower(*s2))
51 return -1;
52 ++s1;
53 ++s2;
54 --max_len;
55 }
56 return 0;
57}
58
59const char *my_stristr(const char *whole, const char *part)
60{
61 int part_len = strlen(part);
62 while (*whole) {
63 if (my_strncasecmp(whole, part, part_len) == 0)
64 return whole;
65 ++whole;
66 }
67 return NULL;
68}
69
70int errno_test(void)
71{
72 enum { CUT = 6 };
73 pj_status_t rc;
74 char errbuf[256];
75
76 PJ_LOG(3,(THIS_FILE, "...errno test: check the msg carefully"));
77
78 /*
79 * Windows platform error.
80 */
81# ifdef ERROR_INVALID_DATA
82 rc = PJ_STATUS_FROM_OS(ERROR_INVALID_DATA);
83 pj_set_os_error(rc);
84
85 /* Whole */
86 pj_strerror(rc, errbuf, sizeof(errbuf));
87 trim_newlines(errbuf);
88 PJ_LOG(3,(THIS_FILE, "...msg for rc=ERROR_INVALID_DATA: '%s'", errbuf));
89 if (my_stristr(errbuf, "invalid") == NULL) {
90 PJ_LOG(3, (THIS_FILE,
91 "...error: expecting \"invalid\" string in the msg"));
92 return -20;
93 }
94
95 /* Cut version. */
96 pj_strerror(rc, errbuf, CUT);
97 PJ_LOG(3,(THIS_FILE, "...msg for rc=ERROR_INVALID_DATA (cut): '%s'", errbuf));
98# endif
99
100 /*
101 * Unix errors
102 */
103# ifdef EINVAL
104 rc = PJ_STATUS_FROM_OS(EINVAL);
105 pj_set_os_error(rc);
106
107 /* Whole */
108 pj_strerror(rc, errbuf, sizeof(errbuf));
109 trim_newlines(errbuf);
110 PJ_LOG(3,(THIS_FILE, "...msg for rc=EINVAL: '%s'", errbuf));
111 if (my_stristr(errbuf, "invalid") == NULL) {
112 PJ_LOG(3, (THIS_FILE,
113 "...error: expecting \"invalid\" string in the msg"));
114 return -30;
115 }
116
117 /* Cut */
118 pj_strerror(rc, errbuf, CUT);
119 PJ_LOG(3,(THIS_FILE, "...msg for rc=EINVAL (cut): '%s'", errbuf));
120# endif
121
122 /*
123 * Windows WSA errors
124 */
125# ifdef WSAEINVAL
126 rc = PJ_STATUS_FROM_OS(WSAEINVAL);
127 pj_set_os_error(rc);
128
129 /* Whole */
130 pj_strerror(rc, errbuf, sizeof(errbuf));
131 trim_newlines(errbuf);
132 PJ_LOG(3,(THIS_FILE, "...msg for rc=WSAEINVAL: '%s'", errbuf));
133 if (my_stristr(errbuf, "invalid") == NULL) {
134 PJ_LOG(3, (THIS_FILE,
135 "...error: expecting \"invalid\" string in the msg"));
136 return -40;
137 }
138
139 /* Cut */
140 pj_strerror(rc, errbuf, CUT);
141 PJ_LOG(3,(THIS_FILE, "...msg for rc=WSAEINVAL (cut): '%s'", errbuf));
142# endif
143
144 pj_strerror(PJ_EBUG, errbuf, sizeof(errbuf));
145 PJ_LOG(3,(THIS_FILE, "...msg for rc=PJ_EBUG: '%s'", errbuf));
146 if (my_stristr(errbuf, "BUG") == NULL) {
147 PJ_LOG(3, (THIS_FILE,
148 "...error: expecting \"BUG\" string in the msg"));
149 return -20;
150 }
151
152 pj_strerror(PJ_EBUG, errbuf, CUT);
153 PJ_LOG(3,(THIS_FILE, "...msg for rc=PJ_EBUG, cut at %d chars: '%s'",
154 CUT, errbuf));
155
156 return 0;
157}
158
159
160#endif /* INCLUDE_ERRNO_TEST */
161
162