| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 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 |
|
| 83 |
|
| 84 |
# ifdef ERROR_INVALID_DATA |
| 85 |
rc = PJ_STATUS_FROM_OS(ERROR_INVALID_DATA); |
| 86 |
pj_set_os_error(rc); |
| 87 |
|
| 88 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 135 |
pj_strerror(rc, errbuf, CUT); |
| 136 |
PJ_LOG(3,(THIS_FILE, "...msg for rc=EINVAL (cut): '%s'", errbuf)); |
| 137 |
# endif |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
# ifdef WSAEINVAL |
| 143 |
rc = PJ_STATUS_FROM_OS(WSAEINVAL); |
| 144 |
pj_set_os_error(rc); |
| 145 |
|
| 146 |
|
| 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 |
|
| 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 |
|
| 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 |
| 182 |
|
| 183 |
|