Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | 844653c | 2008-12-23 17:27:53 +0000 | [diff] [blame] | 3 | * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com) |
| 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | #include "test.h" |
| 21 | #include <pj/errno.h> |
| 22 | #include <pj/log.h> |
| 23 | #include <pj/ctype.h> |
| 24 | #include <pj/compat/socket.h> |
| 25 | #include <pj/string.h> |
| 26 | |
| 27 | #if INCLUDE_ERRNO_TEST |
| 28 | |
| 29 | #define THIS_FILE "errno" |
| 30 | |
| 31 | #if defined(PJ_WIN32) && PJ_WIN32 != 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 = 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 }; |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 74 | pj_status_t rc = 0; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 75 | char errbuf[256]; |
| 76 | |
| 77 | PJ_LOG(3,(THIS_FILE, "...errno test: check the msg carefully")); |
| 78 | |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 79 | PJ_UNUSED_ARG(rc); |
| 80 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 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 | pj_strerror(rc, errbuf, sizeof(errbuf)); |
| 90 | trim_newlines(errbuf); |
| 91 | PJ_LOG(3,(THIS_FILE, "...msg for rc=ERROR_INVALID_DATA: '%s'", errbuf)); |
| 92 | if (my_stristr(errbuf, "invalid") == NULL) { |
| 93 | PJ_LOG(3, (THIS_FILE, |
| 94 | "...error: expecting \"invalid\" string in the msg")); |
Benny Prijono | c4c8f24 | 2006-08-01 23:01:55 +0000 | [diff] [blame] | 95 | #ifndef PJ_WIN32_WINCE |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 96 | return -20; |
Benny Prijono | c4c8f24 | 2006-08-01 23:01:55 +0000 | [diff] [blame] | 97 | #endif |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /* Cut version. */ |
| 101 | pj_strerror(rc, errbuf, CUT); |
| 102 | PJ_LOG(3,(THIS_FILE, "...msg for rc=ERROR_INVALID_DATA (cut): '%s'", errbuf)); |
| 103 | # endif |
| 104 | |
| 105 | /* |
| 106 | * Unix errors |
| 107 | */ |
Benny Prijono | aeeb1d1 | 2007-05-01 10:42:22 +0000 | [diff] [blame] | 108 | # if defined(EINVAL) && !defined(PJ_SYMBIAN) |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 109 | rc = PJ_STATUS_FROM_OS(EINVAL); |
| 110 | pj_set_os_error(rc); |
| 111 | |
| 112 | /* Whole */ |
| 113 | pj_strerror(rc, errbuf, sizeof(errbuf)); |
| 114 | trim_newlines(errbuf); |
| 115 | PJ_LOG(3,(THIS_FILE, "...msg for rc=EINVAL: '%s'", errbuf)); |
| 116 | if (my_stristr(errbuf, "invalid") == NULL) { |
| 117 | PJ_LOG(3, (THIS_FILE, |
| 118 | "...error: expecting \"invalid\" string in the msg")); |
| 119 | return -30; |
| 120 | } |
| 121 | |
| 122 | /* Cut */ |
| 123 | pj_strerror(rc, errbuf, CUT); |
| 124 | PJ_LOG(3,(THIS_FILE, "...msg for rc=EINVAL (cut): '%s'", errbuf)); |
| 125 | # endif |
| 126 | |
| 127 | /* |
| 128 | * Windows WSA errors |
| 129 | */ |
| 130 | # ifdef WSAEINVAL |
| 131 | rc = PJ_STATUS_FROM_OS(WSAEINVAL); |
| 132 | pj_set_os_error(rc); |
| 133 | |
| 134 | /* Whole */ |
| 135 | pj_strerror(rc, errbuf, sizeof(errbuf)); |
| 136 | trim_newlines(errbuf); |
| 137 | PJ_LOG(3,(THIS_FILE, "...msg for rc=WSAEINVAL: '%s'", errbuf)); |
| 138 | if (my_stristr(errbuf, "invalid") == NULL) { |
| 139 | PJ_LOG(3, (THIS_FILE, |
| 140 | "...error: expecting \"invalid\" string in the msg")); |
| 141 | return -40; |
| 142 | } |
| 143 | |
| 144 | /* Cut */ |
| 145 | pj_strerror(rc, errbuf, CUT); |
| 146 | PJ_LOG(3,(THIS_FILE, "...msg for rc=WSAEINVAL (cut): '%s'", errbuf)); |
| 147 | # endif |
| 148 | |
| 149 | pj_strerror(PJ_EBUG, errbuf, sizeof(errbuf)); |
| 150 | PJ_LOG(3,(THIS_FILE, "...msg for rc=PJ_EBUG: '%s'", errbuf)); |
| 151 | if (my_stristr(errbuf, "BUG") == NULL) { |
| 152 | PJ_LOG(3, (THIS_FILE, |
| 153 | "...error: expecting \"BUG\" string in the msg")); |
| 154 | return -20; |
| 155 | } |
| 156 | |
| 157 | pj_strerror(PJ_EBUG, errbuf, CUT); |
| 158 | PJ_LOG(3,(THIS_FILE, "...msg for rc=PJ_EBUG, cut at %d chars: '%s'", |
| 159 | CUT, errbuf)); |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | |
| 165 | #endif /* INCLUDE_ERRNO_TEST */ |
| 166 | |
| 167 | |