Benny Prijono | e722461 | 2005-11-13 19:40:44 +0000 | [diff] [blame^] | 1 | /* $Id$ */
|
| 2 | /*
|
| 3 | * PJLIB - PJ Foundation Library
|
| 4 | * (C)2003-2005 Benny Prijono <bennylp@bulukucing.org>
|
| 5 | *
|
| 6 | * Author:
|
| 7 | * Benny Prijono <bennylp@bulukucing.org>
|
| 8 | *
|
| 9 | * This library is free software; you can redistribute it and/or
|
| 10 | * modify it under the terms of the GNU Lesser General Public
|
| 11 | * License as published by the Free Software Foundation; either
|
| 12 | * version 2.1 of the License, or (at your option) any later version.
|
| 13 | *
|
| 14 | * This library is distributed in the hope that it will be useful,
|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
| 17 | * Lesser General Public License for more details.
|
| 18 | *
|
| 19 | * You should have received a copy of the GNU Lesser General Public
|
| 20 | * License along with this library; if not, write to the Free Software
|
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
| 22 | */
|
| 23 | #include "test.h"
|
| 24 | #include <pj/errno.h>
|
| 25 | #include <pj/log.h>
|
| 26 | #include <pj/ctype.h>
|
| 27 | #include <pj/compat/socket.h>
|
| 28 | #include <pj/string.h>
|
| 29 |
|
| 30 | #if INCLUDE_ERRNO_TEST
|
| 31 |
|
| 32 | #define THIS_FILE "errno"
|
| 33 |
|
| 34 | #if defined(PJ_WIN32) && PJ_WIN32 != 0
|
| 35 | # include <windows.h>
|
| 36 | #endif
|
| 37 |
|
| 38 | #if defined(PJ_HAS_ERRNO_H) && PJ_HAS_ERRNO_H != 0
|
| 39 | # include <errno.h>
|
| 40 | #endif
|
| 41 |
|
| 42 | static void trim_newlines(char *s)
|
| 43 | {
|
| 44 | while (*s) {
|
| 45 | if (*s == '\r' || *s == '\n')
|
| 46 | *s = ' ';
|
| 47 | ++s;
|
| 48 | }
|
| 49 | }
|
| 50 |
|
| 51 | int my_strncasecmp(const char *s1, const char *s2, int max_len)
|
| 52 | {
|
| 53 | while (*s1 && *s2 && max_len > 0) {
|
| 54 | if (pj_tolower(*s1) != pj_tolower(*s2))
|
| 55 | return -1;
|
| 56 | ++s1;
|
| 57 | ++s2;
|
| 58 | --max_len;
|
| 59 | }
|
| 60 | return 0;
|
| 61 | }
|
| 62 |
|
| 63 | const char *my_stristr(const char *whole, const char *part)
|
| 64 | {
|
| 65 | int part_len = strlen(part);
|
| 66 | while (*whole) {
|
| 67 | if (my_strncasecmp(whole, part, part_len) == 0)
|
| 68 | return whole;
|
| 69 | ++whole;
|
| 70 | }
|
| 71 | return NULL;
|
| 72 | }
|
| 73 |
|
| 74 | int errno_test(void)
|
| 75 | {
|
| 76 | enum { CUT = 6 };
|
| 77 | pj_status_t rc;
|
| 78 | char errbuf[256];
|
| 79 |
|
| 80 | PJ_LOG(3,(THIS_FILE, "...errno test: check the msg carefully"));
|
| 81 |
|
| 82 | /*
|
| 83 | * Windows platform error.
|
| 84 | */
|
| 85 | # ifdef ERROR_INVALID_DATA
|
| 86 | rc = PJ_STATUS_FROM_OS(ERROR_INVALID_DATA);
|
| 87 | pj_set_os_error(rc);
|
| 88 |
|
| 89 | /* Whole */
|
| 90 | pj_strerror(rc, errbuf, sizeof(errbuf));
|
| 91 | trim_newlines(errbuf);
|
| 92 | PJ_LOG(3,(THIS_FILE, "...msg for rc=ERROR_INVALID_DATA: '%s'", errbuf));
|
| 93 | if (my_stristr(errbuf, "invalid") == NULL) {
|
| 94 | PJ_LOG(3, (THIS_FILE,
|
| 95 | "...error: expecting \"invalid\" string in the msg"));
|
| 96 | return -20;
|
| 97 | }
|
| 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 | */
|
| 107 | # ifdef EINVAL
|
| 108 | 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 |
|