6
#include "../src/xen.h"
12
static struct xs_handle *xs_handle_dummy = (struct xs_handle *) 0x1234abcd;
14
static struct xs_handle *xs_daemon_open_success(void);
15
static struct xs_handle *xs_daemon_open_fail(void);
17
struct xs_handle *(*xs_daemon_open_handler)(void) = xs_daemon_open_success;
19
struct xs_handle *xs_daemon_open(void) {
20
return xs_daemon_open_handler();
23
static struct xs_handle *xs_daemon_open_success(void) {
24
return xs_handle_dummy;
27
static struct xs_handle *xs_daemon_open_fail(void) {
28
return (struct xs_handle *) NULL;
31
START_TEST(test_xen_init_success)
33
fail_if(xen_init(), "xen_init failed!");
37
START_TEST(test_xen_init_failure)
39
xs_daemon_open_handler = xs_daemon_open_fail;
40
fail_unless(xen_init(), "xen_init succeeded, but should've failed!");
45
* xen_register_watch tests
48
extern void xen_fire_callback_real(const char *path, const char *token, void *buf, unsigned int buflen);
49
extern void *xen_read_path_real(const char *path, unsigned int *buflen);
51
#define test_read_path "/somepath/something"
52
#define test_watch_path "/somepath/something"
53
#define test_token "somedata"
54
#define test_data "some\0random\0date"
55
static int testcb_called = 0;
56
static const char *xs_read_watch_retval[] = { test_read_path, test_token };
57
static xs_transaction_t dummy_transaction;
60
static int testcb(const char *path, const char *buf, size_t buflen, const char *data) {
61
fail_unless(strcmp(path,
62
"/somepath/something") == 0,
63
"Data was at /somepath/something, but that's not what "
64
"our callback was told.");
65
fail_unless(buflen == sizeof(test_data),
66
"Callback was called with an incorrectly sized size "
68
fail_unless(memcmp(buf, test_data, buflen) == 0,
69
"Buffer passed to callback did not match the original "
71
fail_unless(strcmp(data, test_token) == 0, "Token did not match");
76
char **xs_read_watch(struct xs_handle *h, unsigned int *num) {
78
return (char **) xs_read_watch_retval;
81
xs_transaction_t xs_transaction_start(struct xs_handle *h) {
82
fail_unless(h == xs_handle_dummy,
83
"Invalid xs_handle passed to xs_transaction_start");
84
return dummy_transaction;
87
void *xs_read(struct xs_handle *h, xs_transaction_t t,
88
const char *path, unsigned int *len)
90
fail_unless(h == xs_handle_dummy,
91
"Invalid xs_handle passed to xs_read");
92
fail_unless(t == dummy_transaction,
93
"Invalid transaction passed to xs_read");
94
fail_unless(strcmp(path,
96
"Data was at /somepath/something, but that's not what "
97
"xs_read was asked to read.");
99
*len = sizeof(test_data);
103
bool xs_watch(struct xs_handle *h, const char *path, const char *token) {
104
fail_unless(h == xs_handle_dummy,
105
"Invalid xs_handle passed to xs_watch");
106
fail_unless(strcmp(path,
107
test_watch_path) == 0,
108
"xs_watch asked to watch something other than " test_watch_path);
109
fail_unless(strcmp(token,
111
"xs_watch was passed wrong token");
115
bool xs_transaction_end(struct xs_handle *h, xs_transaction_t t,
117
fail_unless(t == dummy_transaction,
118
"Invalid transaction passed to xs_transaction_end");
122
void xen_fire_callback(const char *path, const char *token, void *buf,
123
unsigned int buflen) {
124
return xen_fire_callback_real(path, token, buf, buflen);
127
void *xen_read_path(const char *path, unsigned int *buflen) {
128
return xen_read_path_real(path, buflen);
132
START_TEST(test_xen_fire_callback)
135
xen_fire_callback_real(test_read_path, test_token, test_data, sizeof(test_data));
139
START_TEST(test_xen_read_path)
145
buf = xen_read_path_real(test_read_path, &buflen);
146
fail_unless(buflen == sizeof(test_data),
147
"xen_read_path altered the length of the data buffer");
148
fail_unless(memcmp(buf, test_data, buflen) == 0,
149
"xen_read_path mangled data buffer");
153
START_TEST(test_xen_wait_for_event)
155
xs_daemon_open_handler = xs_daemon_open_success;
157
xen_register_watch(test_watch_path, testcb, (void *) test_token);
158
xen_wait_for_event();
159
fail_unless(testcb_called, "Callback didn't fire at all.");
163
Suite *xen_suite(void)
165
Suite *s = suite_create("Xen");
167
TCase *tc_xen_init = tcase_create("xen_init");
168
tcase_add_test(tc_xen_init, test_xen_init_success);
169
tcase_add_test(tc_xen_init, test_xen_init_failure);
170
suite_add_tcase(s, tc_xen_init);
172
TCase *tc_xen_read_path = tcase_create("xen_read_path");
173
tcase_add_test(tc_xen_read_path, test_xen_read_path);
174
suite_add_tcase(s, tc_xen_read_path);
176
TCase *tc_xen_fire_callback = tcase_create("xen_fire_callback");
177
tcase_add_test(tc_xen_fire_callback, test_xen_fire_callback);
178
suite_add_tcase(s, tc_xen_fire_callback);
180
TCase *tc_xen_wait_for_event = tcase_create("xen_wait_for_event");
181
tcase_add_test(tc_xen_wait_for_event, test_xen_wait_for_event);
182
suite_add_tcase(s, tc_xen_wait_for_event);
191
Suite *s = xen_suite();
192
SRunner *sr = srunner_create(s);
193
srunner_run_all(sr, CK_NORMAL);
194
number_failed = srunner_ntests_failed(sr);
196
return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;