-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest_handlers.c
246 lines (218 loc) · 7 KB
/
test_handlers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* test_handlers.c
*
* Basic tests for POSIX OS handlers.
*
* Author: MontaVista Software, Inc.
* Corey Minyard <minyard@mvista.com>
* source@mvista.com
*
* Copyright 2006 MontaVista Software Inc.
*
* This software is available to you under a choice of one of two
* licenses. You may choose to be licensed under the terms of the GNU
* Lesser General Public License (GPL) Version 2 or the modified BSD
* license below. The following disclamer applies to both licenses:
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* GNU Lesser General Public Licence
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Modified BSD Licence
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <OpenIPMI/ipmi_tcl.h>
#include <tcl.h>
os_handler_t *test_os_hnd;
static void
err_leave(int err, char *format, ...)
{
va_list ap;
va_start(ap, format);
vfprintf(stderr, format, ap);
if (err)
fprintf(stderr, "error: %s (%d)\n", strerror(err), err);
va_end(ap);
exit(1);
}
int expect_log = 0;
static void
my_vlog(os_handler_t *handler,
const char *format,
enum ipmi_log_type_e log_type,
va_list ap)
{
if (expect_log == 0) {
int ival;
char *sval;
expect_log++;
if (strcmp(format, "This is a test: %d %s") != 0)
err_leave(0, "Wrong format\n");
if (log_type != IPMI_LOG_FATAL)
err_leave(0, "Invalid log type\n");
ival = va_arg(ap, int);
if (ival != 47)
err_leave(0, "Invalid log int val\n");
sval = va_arg(ap, char *);
if (strcmp(sval, "Hello") != 0)
err_leave(0, "Invalid log string val\n");
} else {
vfprintf(stderr, format, ap);
err_leave(0, "Unexpected log!\n");
}
}
static inline void
diff_timeval(struct timeval *dest,
struct timeval *left,
struct timeval *right)
{
if ( (left->tv_sec < right->tv_sec)
|| ( (left->tv_sec == right->tv_sec)
&& (left->tv_usec < right->tv_usec)))
{
/* If left < right, just force to zero, don't allow negative
numbers. */
dest->tv_sec = 0;
dest->tv_usec = 0;
return;
}
dest->tv_sec = left->tv_sec - right->tv_sec;
dest->tv_usec = left->tv_usec - right->tv_usec;
while (dest->tv_usec < 0) {
dest->tv_usec += 1000000;
dest->tv_sec--;
}
}
os_handler_waiter_t *timer_waiter;
int expect_timeout = 0;
static void
timeout_handler(void *cb_data, os_hnd_timer_id_t *id)
{
struct timeval *then = cb_data;
struct timeval now;
struct timeval diff;
int rv;
printf("Timeout!\n");
test_os_hnd->get_monotonic_time(test_os_hnd, &now);
diff_timeval(&diff, &now, then);
if (expect_timeout == 0) {
expect_timeout++;
if (diff.tv_sec != 1)
err_leave(0, "Invalid timeout diff 0: %ld %ld\n",
diff.tv_sec, diff.tv_usec);
diff.tv_sec = 0;
diff.tv_usec = 500000;
*then = now;
rv = test_os_hnd->start_timer(test_os_hnd, id, &diff,
timeout_handler, then);
if (rv)
err_leave(rv, "Unable to start timer\n");
} else if (expect_timeout == 1) {
expect_timeout++;
if (diff.tv_sec != 0)
err_leave(0, "Invalid timeout diff 1: %ld %ld\n",
diff.tv_sec, diff.tv_usec);
os_handler_waiter_release(timer_waiter);
} else {
err_leave(0, "Unexpected timeout!\n");
}
}
static void
test_os_handler(os_handler_t *os_hnd, os_handler_waiter_factory_t *factory)
{
os_hnd_timer_id_t *timer;
struct timeval now;
struct timeval tv;
int rv;
test_os_hnd = os_hnd;
/* Override the default log handler (so I can catch them). */
os_hnd->set_log_handler(os_hnd, my_vlog);
printf("Log test\n");
os_hnd->log(os_hnd, IPMI_LOG_FATAL, "This is a test: %d %s",
47, "Hello");
printf("Timer test\n");
timer_waiter = os_handler_alloc_waiter(factory);
if (!timer_waiter)
err_leave(0, "Unable to allocate waiter\n");
rv = os_hnd->alloc_timer(os_hnd, &timer);
if (rv)
err_leave(rv, "Unable to allocate timer");
os_hnd->get_monotonic_time(os_hnd, &now);
tv.tv_sec = 1;
tv.tv_usec = 500000;
rv = os_hnd->start_timer(os_hnd, timer, &tv, timeout_handler, &now);
tv.tv_sec = 3;
tv.tv_usec = 0;
os_handler_waiter_wait(timer_waiter, &tv);
if (expect_timeout != 2)
err_leave(0, "Error in timers: %d", expect_timeout);
os_handler_free_waiter(timer_waiter);
rv = os_handler_free_waiter_factory(factory);
if (rv)
err_leave(rv, "Error freeing factory\n");
os_hnd->free_os_handler(os_hnd);
}
static void
reset_tests(void)
{
expect_log = 0;
expect_timeout = 0;
}
int ipmi_malloc_init(os_handler_t *os_hnd);
int
main(int argc, char *argv[])
{
os_handler_waiter_factory_t *factory;
os_handler_t *os_hnd;
int rv;
Tcl_CreateInterp();
printf("*** Testing TCL OS handler\n");
reset_tests();
os_hnd = ipmi_tcl_get_os_handler(0);
if (!os_hnd) {
fprintf(stderr, "ipmi_smi_setup_con: Unable to allocate os handler\n");
exit(1);
}
ipmi_malloc_init(os_hnd);
rv = os_handler_alloc_waiter_factory(os_hnd, 0, 0, &factory);
if (rv)
err_leave(rv, "Unable to allocate waiter factory\n");
test_os_handler(os_hnd, factory);
return 0;
}