| 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/log.h> |
| 21 |
#include <pj/os.h> |
| 22 |
#include <string.h> |
| 23 |
#include <stdio.h> |
| 24 |
|
| 25 |
#define THIS_FILE "os.c" |
| 26 |
|
| 27 |
#if INCLUDE_OS_TEST |
| 28 |
static int endianness_test32(void) |
| 29 |
{ |
| 30 |
union t |
| 31 |
{ |
| 32 |
pj_uint32_t u32; |
| 33 |
pj_uint16_t u16[2]; |
| 34 |
pj_uint8_t u8[4]; |
| 35 |
} t; |
| 36 |
|
| 37 |
PJ_LOG(3,("", " Testing endianness..")); |
| 38 |
|
| 39 |
t.u32 = 0x11223344; |
| 40 |
|
| 41 |
#if defined(PJ_IS_LITTLE_ENDIAN) && PJ_IS_LITTLE_ENDIAN |
| 42 |
PJ_LOG(3,("", " Library is set to little endian")); |
| 43 |
|
| 44 |
# if defined(PJ_IS_BIG_ENDIAN) && PJ_IS_BIG_ENDIAN |
| 45 |
# error Error: Both PJ_IS_LITTLE_ENDIAN and PJ_IS_BIG_ENDIAN are set! |
| 46 |
# endif |
| 47 |
|
| 48 |
if ((t.u16[0] & 0xFFFF) != 0x3344 || |
| 49 |
(t.u16[1] & 0xFFFF) != 0x1122) |
| 50 |
{ |
| 51 |
PJ_LOG(3,("", " Error: wrong 16bit values 0x%x and 0x%x", |
| 52 |
(t.u16[0] & 0xFFFF), (t.u16[1] & 0xFFFF))); |
| 53 |
return 10; |
| 54 |
} |
| 55 |
|
| 56 |
if ((t.u8[0] & 0xFF) != 0x44 || |
| 57 |
(t.u8[1] & 0xFF) != 0x33 || |
| 58 |
(t.u8[2] & 0xFF) != 0x22 || |
| 59 |
(t.u8[3] & 0xFF) != 0x11) |
| 60 |
{ |
| 61 |
PJ_LOG(3,("", " Error: wrong 8bit values")); |
| 62 |
return 12; |
| 63 |
} |
| 64 |
|
| 65 |
#elif defined(PJ_IS_BIG_ENDIAN) && PJ_IS_BIG_ENDIAN |
| 66 |
PJ_LOG(3,("", " Library is set to big endian")); |
| 67 |
|
| 68 |
if ((t.u16[0] & 0xFFFF) != 0x1122 || |
| 69 |
(t.u16[1] & 0xFFFF) != 0x3344) |
| 70 |
{ |
| 71 |
PJ_LOG(3,("", " Error: wrong 16bit values 0x%x and 0x%x", |
| 72 |
(t.u16[0] & 0xFFFF), (t.u16[1] & 0xFFFF))); |
| 73 |
return 20; |
| 74 |
} |
| 75 |
|
| 76 |
if ((t.u8[0] & 0xFF) != 0x11 || |
| 77 |
(t.u8[1] & 0xFF) != 0x22 || |
| 78 |
(t.u8[2] & 0xFF) != 0x33 || |
| 79 |
(t.u8[3] & 0xFF) != 0x44) |
| 80 |
{ |
| 81 |
PJ_LOG(3,("", " Error: wrong 8bit values")); |
| 82 |
return 22; |
| 83 |
} |
| 84 |
|
| 85 |
# if defined(PJ_IS_LITTLE_ENDIAN) && PJ_IS_LITTLE_ENDIAN |
| 86 |
# error Error: Both PJ_IS_LITTLE_ENDIAN and PJ_IS_BIG_ENDIAN are set! |
| 87 |
# endif |
| 88 |
|
| 89 |
|
| 90 |
#else |
| 91 |
# error Error: Endianness is not set properly! |
| 92 |
#endif |
| 93 |
|
| 94 |
return 0; |
| 95 |
} |
| 96 |
|
| 97 |
static int log_written; |
| 98 |
static char test_large_msg[PJ_LOG_MAX_SIZE]; |
| 99 |
|
| 100 |
static void log_write(int level, const char *buffer, int len) |
| 101 |
{ |
| 102 |
PJ_UNUSED_ARG(level); |
| 103 |
PJ_UNUSED_ARG(buffer); |
| 104 |
|
| 105 |
log_written = len; |
| 106 |
|
| 107 |
} |
| 108 |
|
| 109 |
int log_test(void) |
| 110 |
{ |
| 111 |
pj_log_func *old_func = pj_log_get_log_func(); |
| 112 |
struct log_test_t { |
| 113 |
const char *title; |
| 114 |
unsigned decor; |
| 115 |
const char *fmt; |
| 116 |
const char *arg; |
| 117 |
int min_len; |
| 118 |
int max_len; |
| 119 |
} log_tests[] = { |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
{ |
| 127 |
"normal log", |
| 128 |
PJ_LOG_HAS_TIME | PJ_LOG_HAS_MICRO_SEC | |
| 129 |
PJ_LOG_HAS_SENDER | PJ_LOG_HAS_NEWLINE, |
| 130 |
"Hello %s!", |
| 131 |
"world", |
| 132 |
13+PJ_LOG_SENDER_WIDTH+1+12+1, |
| 133 |
13+PJ_LOG_SENDER_WIDTH+1+12+1, |
| 134 |
}, |
| 135 |
{ |
| 136 |
"normal log with no decor", |
| 137 |
0, |
| 138 |
"Hello %s!", |
| 139 |
"world", |
| 140 |
12, |
| 141 |
12, |
| 142 |
}, |
| 143 |
{ |
| 144 |
"normal log with just newline", |
| 145 |
PJ_LOG_HAS_NEWLINE, |
| 146 |
"Hello %s!", |
| 147 |
"world", |
| 148 |
13, |
| 149 |
13, |
| 150 |
}, |
| 151 |
{ |
| 152 |
"empty string with normal decor", |
| 153 |
PJ_LOG_HAS_TIME | PJ_LOG_HAS_MICRO_SEC | |
| 154 |
PJ_LOG_HAS_SENDER | PJ_LOG_HAS_NEWLINE, |
| 155 |
"%s", |
| 156 |
"", |
| 157 |
13+PJ_LOG_SENDER_WIDTH+1+1, |
| 158 |
13+PJ_LOG_SENDER_WIDTH+1+1, |
| 159 |
}, |
| 160 |
{ |
| 161 |
"empty string with nodecor", |
| 162 |
0, |
| 163 |
"%s", |
| 164 |
"", |
| 165 |
0, |
| 166 |
0, |
| 167 |
}, |
| 168 |
{ |
| 169 |
"empty string with just newline", |
| 170 |
PJ_LOG_HAS_NEWLINE, |
| 171 |
"%s", |
| 172 |
"", |
| 173 |
1, |
| 174 |
1, |
| 175 |
}, |
| 176 |
{ |
| 177 |
"large message with normal decor", |
| 178 |
PJ_LOG_HAS_TIME | PJ_LOG_HAS_MICRO_SEC | |
| 179 |
PJ_LOG_HAS_SENDER | PJ_LOG_HAS_NEWLINE, |
| 180 |
"%s", |
| 181 |
test_large_msg, |
| 182 |
PJ_LOG_MAX_SIZE-1, |
| 183 |
PJ_LOG_MAX_SIZE-1, |
| 184 |
}, |
| 185 |
{ |
| 186 |
"large message with no decor", |
| 187 |
0, |
| 188 |
"%s", |
| 189 |
test_large_msg, |
| 190 |
PJ_LOG_MAX_SIZE-1, |
| 191 |
PJ_LOG_MAX_SIZE-1, |
| 192 |
}, |
| 193 |
{ |
| 194 |
"large message with just newline", |
| 195 |
PJ_LOG_HAS_NEWLINE, |
| 196 |
"%s", |
| 197 |
test_large_msg, |
| 198 |
PJ_LOG_MAX_SIZE-1, |
| 199 |
PJ_LOG_MAX_SIZE-1, |
| 200 |
}, |
| 201 |
}; |
| 202 |
unsigned old_decor = pj_log_get_decor(); |
| 203 |
unsigned i; |
| 204 |
|
| 205 |
memset(test_large_msg, 'A', sizeof(test_large_msg)); |
| 206 |
test_large_msg[sizeof(test_large_msg)-1] = '\0'; |
| 207 |
|
| 208 |
pj_log_set_log_func( &log_write ); |
| 209 |
|
| 210 |
for (i=0; i<PJ_ARRAY_SIZE(log_tests); ++i) { |
| 211 |
struct log_test_t *t = &log_tests[i]; |
| 212 |
|
| 213 |
log_written = -1; |
| 214 |
pj_log_set_decor(t->decor); |
| 215 |
|
| 216 |
PJ_LOG(1,(THIS_FILE, t->fmt, t->arg)); |
| 217 |
|
| 218 |
if (log_written < t->min_len || log_written > t->max_len) { |
| 219 |
pj_log_set_log_func( old_func ); |
| 220 |
pj_log_set_decor(old_decor); |
| 221 |
|
| 222 |
PJ_LOG(1,(THIS_FILE, |
| 223 |
" Error: in test %d (%s), writing (\"%s\", \"%s\"), expecting %d<=len<=%d, got len=%d", |
| 224 |
i, t->title, t->fmt, t->arg, t->min_len, t->max_len, log_written)); |
| 225 |
return 33; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
pj_log_set_log_func( old_func ); |
| 230 |
pj_log_set_decor(old_decor); |
| 231 |
|
| 232 |
return 0; |
| 233 |
} |
| 234 |
|
| 235 |
int os_test(void) |
| 236 |
{ |
| 237 |
const pj_sys_info *si; |
| 238 |
int rc = 0; |
| 239 |
|
| 240 |
PJ_LOG(3,("", " Sys info:")); |
| 241 |
si = pj_get_sys_info(); |
| 242 |
PJ_LOG(3,("", " machine: %s", si->machine.ptr)); |
| 243 |
PJ_LOG(3,("", " os_name: %s", si->os_name.ptr)); |
| 244 |
PJ_LOG(3,("", " os_ver: 0x%x", si->os_ver)); |
| 245 |
PJ_LOG(3,("", " sdk_name: %s", si->sdk_name.ptr)); |
| 246 |
PJ_LOG(3,("", " sdk_ver: 0x%x", si->sdk_ver)); |
| 247 |
PJ_LOG(3,("", " info: %s", si->info.ptr)); |
| 248 |
|
| 249 |
rc = endianness_test32(); |
| 250 |
|
| 251 |
return rc; |
| 252 |
} |
| 253 |
|
| 254 |
#else |
| 255 |
int dummy_os_var; |
| 256 |
#endif |
| 257 |
|