blob: 33378fead234afd673cd555e100238794691fe39 [file] [log] [blame]
Benny Prijono5dcb38d2005-11-21 01:55:47 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C)2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijono5dcb38d2005-11-21 01:55:47 +00004 *
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# 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 };
Benny Prijonoba5926a2007-05-02 11:29:37 +000073 pj_status_t rc = 0;
Benny Prijono5dcb38d2005-11-21 01:55:47 +000074 char errbuf[256];
75
76 PJ_LOG(3,(THIS_FILE, "...errno test: check the msg carefully"));
77
Benny Prijonoba5926a2007-05-02 11:29:37 +000078 PJ_UNUSED_ARG(rc);
79
Benny Prijono5dcb38d2005-11-21 01:55:47 +000080 /*
81 * Windows platform error.
82 */
83# ifdef ERROR_INVALID_DATA
84 rc = PJ_STATUS_FROM_OS(ERROR_INVALID_DATA);
85 pj_set_os_error(rc);
86
87 /* Whole */
88 pj_strerror(rc, errbuf, sizeof(errbuf));
89 trim_newlines(errbuf);
90 PJ_LOG(3,(THIS_FILE, "...msg for rc=ERROR_INVALID_DATA: '%s'", errbuf));
91 if (my_stristr(errbuf, "invalid") == NULL) {
92 PJ_LOG(3, (THIS_FILE,
93 "...error: expecting \"invalid\" string in the msg"));
Benny Prijonoc4c8f242006-08-01 23:01:55 +000094#ifndef PJ_WIN32_WINCE
Benny Prijono5dcb38d2005-11-21 01:55:47 +000095 return -20;
Benny Prijonoc4c8f242006-08-01 23:01:55 +000096#endif
Benny Prijono5dcb38d2005-11-21 01:55:47 +000097 }
98
99 /* Cut version. */
100 pj_strerror(rc, errbuf, CUT);
101 PJ_LOG(3,(THIS_FILE, "...msg for rc=ERROR_INVALID_DATA (cut): '%s'", errbuf));
102# endif
103
104 /*
105 * Unix errors
106 */
Benny Prijonoaeeb1d12007-05-01 10:42:22 +0000107# if defined(EINVAL) && !defined(PJ_SYMBIAN)
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000108 rc = PJ_STATUS_FROM_OS(EINVAL);
109 pj_set_os_error(rc);
110
111 /* Whole */
112 pj_strerror(rc, errbuf, sizeof(errbuf));
113 trim_newlines(errbuf);
114 PJ_LOG(3,(THIS_FILE, "...msg for rc=EINVAL: '%s'", errbuf));
115 if (my_stristr(errbuf, "invalid") == NULL) {
116 PJ_LOG(3, (THIS_FILE,
117 "...error: expecting \"invalid\" string in the msg"));
118 return -30;
119 }
120
121 /* Cut */
122 pj_strerror(rc, errbuf, CUT);
123 PJ_LOG(3,(THIS_FILE, "...msg for rc=EINVAL (cut): '%s'", errbuf));
124# endif
125
126 /*
127 * Windows WSA errors
128 */
129# ifdef WSAEINVAL
130 rc = PJ_STATUS_FROM_OS(WSAEINVAL);
131 pj_set_os_error(rc);
132
133 /* Whole */
134 pj_strerror(rc, errbuf, sizeof(errbuf));
135 trim_newlines(errbuf);
136 PJ_LOG(3,(THIS_FILE, "...msg for rc=WSAEINVAL: '%s'", errbuf));
137 if (my_stristr(errbuf, "invalid") == NULL) {
138 PJ_LOG(3, (THIS_FILE,
139 "...error: expecting \"invalid\" string in the msg"));
140 return -40;
141 }
142
143 /* Cut */
144 pj_strerror(rc, errbuf, CUT);
145 PJ_LOG(3,(THIS_FILE, "...msg for rc=WSAEINVAL (cut): '%s'", errbuf));
146# endif
147
148 pj_strerror(PJ_EBUG, errbuf, sizeof(errbuf));
149 PJ_LOG(3,(THIS_FILE, "...msg for rc=PJ_EBUG: '%s'", errbuf));
150 if (my_stristr(errbuf, "BUG") == NULL) {
151 PJ_LOG(3, (THIS_FILE,
152 "...error: expecting \"BUG\" string in the msg"));
153 return -20;
154 }
155
156 pj_strerror(PJ_EBUG, errbuf, CUT);
157 PJ_LOG(3,(THIS_FILE, "...msg for rc=PJ_EBUG, cut at %d chars: '%s'",
158 CUT, errbuf));
159
160 return 0;
161}
162
163
164#endif /* INCLUDE_ERRNO_TEST */
165
166