Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | a771a51 | 2007-02-19 01:13:53 +0000 | [diff] [blame^] | 3 | * Copyright (C)2003-2007 Benny Prijono <benny@prijono.org> |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 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 | # 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 | |
| 38 | static void trim_newlines(char *s) |
| 39 | { |
| 40 | while (*s) { |
| 41 | if (*s == '\r' || *s == '\n') |
| 42 | *s = ' '; |
| 43 | ++s; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | int 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 | |
| 59 | const 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 | |
| 70 | int 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")); |
Benny Prijono | c4c8f24 | 2006-08-01 23:01:55 +0000 | [diff] [blame] | 92 | #ifndef PJ_WIN32_WINCE |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 93 | return -20; |
Benny Prijono | c4c8f24 | 2006-08-01 23:01:55 +0000 | [diff] [blame] | 94 | #endif |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /* Cut version. */ |
| 98 | pj_strerror(rc, errbuf, CUT); |
| 99 | PJ_LOG(3,(THIS_FILE, "...msg for rc=ERROR_INVALID_DATA (cut): '%s'", errbuf)); |
| 100 | # endif |
| 101 | |
| 102 | /* |
| 103 | * Unix errors |
| 104 | */ |
| 105 | # ifdef EINVAL |
| 106 | rc = PJ_STATUS_FROM_OS(EINVAL); |
| 107 | pj_set_os_error(rc); |
| 108 | |
| 109 | /* Whole */ |
| 110 | pj_strerror(rc, errbuf, sizeof(errbuf)); |
| 111 | trim_newlines(errbuf); |
| 112 | PJ_LOG(3,(THIS_FILE, "...msg for rc=EINVAL: '%s'", errbuf)); |
| 113 | if (my_stristr(errbuf, "invalid") == NULL) { |
| 114 | PJ_LOG(3, (THIS_FILE, |
| 115 | "...error: expecting \"invalid\" string in the msg")); |
| 116 | return -30; |
| 117 | } |
| 118 | |
| 119 | /* Cut */ |
| 120 | pj_strerror(rc, errbuf, CUT); |
| 121 | PJ_LOG(3,(THIS_FILE, "...msg for rc=EINVAL (cut): '%s'", errbuf)); |
| 122 | # endif |
| 123 | |
| 124 | /* |
| 125 | * Windows WSA errors |
| 126 | */ |
| 127 | # ifdef WSAEINVAL |
| 128 | rc = PJ_STATUS_FROM_OS(WSAEINVAL); |
| 129 | pj_set_os_error(rc); |
| 130 | |
| 131 | /* Whole */ |
| 132 | pj_strerror(rc, errbuf, sizeof(errbuf)); |
| 133 | trim_newlines(errbuf); |
| 134 | PJ_LOG(3,(THIS_FILE, "...msg for rc=WSAEINVAL: '%s'", errbuf)); |
| 135 | if (my_stristr(errbuf, "invalid") == NULL) { |
| 136 | PJ_LOG(3, (THIS_FILE, |
| 137 | "...error: expecting \"invalid\" string in the msg")); |
| 138 | return -40; |
| 139 | } |
| 140 | |
| 141 | /* Cut */ |
| 142 | pj_strerror(rc, errbuf, CUT); |
| 143 | PJ_LOG(3,(THIS_FILE, "...msg for rc=WSAEINVAL (cut): '%s'", errbuf)); |
| 144 | # endif |
| 145 | |
| 146 | pj_strerror(PJ_EBUG, errbuf, sizeof(errbuf)); |
| 147 | PJ_LOG(3,(THIS_FILE, "...msg for rc=PJ_EBUG: '%s'", errbuf)); |
| 148 | if (my_stristr(errbuf, "BUG") == NULL) { |
| 149 | PJ_LOG(3, (THIS_FILE, |
| 150 | "...error: expecting \"BUG\" string in the msg")); |
| 151 | return -20; |
| 152 | } |
| 153 | |
| 154 | pj_strerror(PJ_EBUG, errbuf, CUT); |
| 155 | PJ_LOG(3,(THIS_FILE, "...msg for rc=PJ_EBUG, cut at %d chars: '%s'", |
| 156 | CUT, errbuf)); |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | |
| 162 | #endif /* INCLUDE_ERRNO_TEST */ |
| 163 | |
| 164 | |