2 * This file is part of buxton.
4 * Copyright (C) 2013 Intel Corporation
6 * buxton is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1
9 * of the License, or (at your option) any later version.
21 #include <sys/types.h>
26 #include "buxtonresponse.h"
27 #include "check_utils.h"
28 #include "configurator.h"
31 #include "serialize.h"
35 #error "re-run configure with --enable-debug"
38 #define BUXTON_ROOT_CHECK_ENV "BUXTON_ROOT_CHECK"
40 START_TEST(buxton_direct_open_check)
43 fail_if(buxton_direct_open(&c) == false,
44 "Direct open failed without daemon.");
45 buxton_direct_close(&c);
49 START_TEST(buxton_direct_init_db_check)
52 BuxtonString slayer_name = buxton_string_pack("base");
53 BuxtonString ulayer_name = buxton_string_pack("user");
58 fail_if(buxton_direct_open(&c) == false,
59 "Direct open failed without daemon.");
61 fail_if(!buxton_direct_init_db(&c, &ulayer_name),
62 "Failed to run init_db for user");
64 sprintf(db, "%s/user-%d.db", buxton_db_path(), getuid());
66 fail_if(r != -1 && errno != ENOENT, "user db file created");
68 fail_if(!buxton_direct_init_db(&c, &slayer_name),
69 "Failed to run init_db");
71 sprintf(db, "%s/base.db", buxton_db_path());
73 fail_if(r != 0, "Failed to create db file");
75 buxton_direct_close(&c);
79 START_TEST(buxton_direct_create_group_check)
82 fail_if(buxton_direct_open(&c) == false,
83 "Direct open failed without daemon.");
86 group.layer = buxton_string_pack("base");
87 group.group = buxton_string_pack("tgroup");
88 group.name = (BuxtonString){ NULL, 0 };
90 fail_if(!buxton_direct_create_group(&c, &group, NULL),
91 "Create group failed");
95 START_TEST(buxton_direct_remove_group_check)
98 fail_if(buxton_direct_open(&c) == false,
99 "Direct open failed without daemon.");
102 group.layer = buxton_string_pack("base");
103 group.group = buxton_string_pack("tgroup");
104 group.name = (BuxtonString){ NULL, 0 };
106 fail_if(!buxton_direct_remove_group(&c, &group, NULL),
107 "Failed to remove group");
111 START_TEST(buxton_direct_set_value_check)
114 fail_if(buxton_direct_open(&c) == false,
115 "Direct open failed without daemon.");
121 group.layer = buxton_string_pack("test-gdbm");
122 group.group = buxton_string_pack("bxt_test_group");
123 group.name = (BuxtonString){ NULL, 0 };
125 glabel = buxton_string_pack("*");
127 key.layer = group.layer;
128 key.group = group.group;
129 key.name = buxton_string_pack("bxt_test_key");
132 c.client.uid = getuid();
133 fail_if(buxton_direct_create_group(&c, &group, NULL) == false,
134 "Creating group failed.");
135 fail_if(buxton_direct_set_label(&c, &group, &glabel) == false,
136 "Setting group label failed.");
138 data.store.d_string = buxton_string_pack("bxt_test_value");
139 fail_if(buxton_direct_set_value(&c, &key, &data, NULL) == false,
140 "Setting value in buxton directly failed.");
141 buxton_direct_close(&c);
145 START_TEST(buxton_direct_get_value_for_layer_check)
152 key.layer = buxton_string_pack("test-gdbm");
153 key.group = buxton_string_pack("bxt_test_group");
154 key.name = buxton_string_pack("bxt_test_key");
157 c.client.uid = getuid();
158 fail_if(buxton_direct_open(&c) == false,
159 "Direct open failed without daemon.");
160 fail_if(buxton_direct_get_value_for_layer(&c, &key, &result, &dlabel, NULL),
161 "Retrieving value from buxton gdbm backend failed.");
162 fail_if(result.type != STRING,
163 "Buxton gdbm backend returned incorrect result type.");
164 //FIXME get label test figured out
165 fail_if(strcmp(result.store.d_string.value, "bxt_test_value") != 0,
166 "Buxton gdbm returned a different value to that set.");
167 if (result.store.d_string.value)
168 free(result.store.d_string.value);
169 buxton_direct_close(&c);
173 START_TEST(buxton_direct_get_value_check)
176 BuxtonData data, result;
179 key.layer = buxton_string_pack("test-gdbm");
180 key.group = buxton_string_pack("bxt_test_group");
181 key.name = buxton_string_pack("bxt_test_key");
184 fail_if(buxton_direct_open(&c) == false,
185 "Direct open failed without daemon.");
187 c.client.uid = getuid();
189 data.store.d_string = buxton_string_pack("bxt_test_value2");
190 fail_if(data.store.d_string.value == NULL,
191 "Failed to allocate test string.");
192 fail_if(buxton_direct_set_value(&c, &key, &data, NULL) == false,
193 "Failed to set second value.");
194 fail_if(buxton_direct_get_value(&c, &key, &result, &dlabel, NULL) == -1,
195 "Retrieving value from buxton gdbm backend failed.");
196 fail_if(result.type != STRING,
197 "Buxton gdbm backend returned incorrect result type.");
198 //FIXME figure out label check
199 fail_if(strcmp(result.store.d_string.value, "bxt_test_value2") != 0,
200 "Buxton gdbm returned a different value to that set.");
201 if (result.store.d_string.value)
202 free(result.store.d_string.value);
203 buxton_direct_close(&c);
207 START_TEST(buxton_memory_backend_check)
210 BuxtonData data, result;
211 BuxtonString dlabel, glabel;
215 group.layer = buxton_string_pack("temp");
216 group.group = buxton_string_pack("bxt_mem_test_group");
217 group.name = (BuxtonString){ NULL, 0 };
219 glabel = buxton_string_pack("*");
221 key.layer = group.layer;
222 key.group = group.group;
223 key.name = buxton_string_pack("bxt_mem_test_key");
226 fail_if(buxton_direct_open(&c) == false,
227 "Direct open failed without daemon.");
229 c.client.uid = getuid();
230 fail_if(buxton_direct_create_group(&c, &group, NULL) == false,
231 "Creating group failed.");
232 fail_if(buxton_direct_set_label(&c, &group, &glabel) == false,
233 "Setting group label failed.");
235 data.store.d_string = buxton_string_pack("bxt_test_value");
236 fail_if(buxton_direct_set_value(&c, &key, &data, NULL) == false,
237 "Setting value in buxton memory backend directly failed.");
238 fail_if(buxton_direct_get_value_for_layer(&c, &key, &result, &dlabel, NULL),
239 "Retrieving value from buxton memory backend directly failed.");
240 // FIXME: BUXTON_GROUP_VALUE is the dummy group data value, but the memory
241 // backend doesn't understand groups, so this is the current workaround.
242 fail_if(!streq(result.store.d_string.value, "bxt_test_value"),
243 "Buxton memory returned a different value to that set.");
244 buxton_direct_close(&c);
248 START_TEST(buxton_key_check)
250 char *group = "group";
252 char *layer = "layer";
257 BuxtonDataType type = STRING;
259 key = buxton_key_create(group, name, layer, type);
260 fail_if(!key, "Failed to create buxton key");
261 g = buxton_key_get_group(key);
262 n = buxton_key_get_name(key);
263 l = buxton_key_get_layer(key);
264 fail_if(!g, "Failed to get group from key");
265 fail_if(!n, "Failed to get name from key");
266 fail_if(!l, "Failed to get layer from key");
267 fail_if(buxton_key_get_type(key) != type,
268 "Failed to get correct type from key");
269 fail_if(!streq(g, group), "Got different group back from key");
270 fail_if(!streq(n, name), "Got different name back from key");
271 fail_if(!streq(l, layer), "Got different layer back from key");
275 buxton_key_free(key);
277 fail_if(buxton_key_create(NULL, name, layer, type), "Got key back with invalid group");
278 fail_if(buxton_key_create(group, name, layer, BUXTON_TYPE_MIN),
279 "Got key back with invalid type 1");
280 fail_if(buxton_key_create(group, name, layer, BUXTON_TYPE_MAX),
281 "Got key back with invalid type 2");
283 key = buxton_key_create(group, NULL, layer, type);
284 fail_if(!key, "Failed to create buxton key with empty name");
285 fail_if(buxton_key_get_name(key), "Got name back with no name key");
286 buxton_key_free(key);
288 key = buxton_key_create(group, name, NULL, type);
289 fail_if(!key, "Failed to create buxton key with empty layer");
290 fail_if(buxton_key_get_layer(key), "Got layer back with no layer key");
291 buxton_key_free(key);
295 START_TEST(buxton_set_label_check)
298 BuxtonString label = buxton_string_pack("*");
300 key.layer = buxton_string_pack("test-gdbm");
301 key.group = buxton_string_pack("bxt_test");
302 key.name.value = NULL;
304 char *root_check = getenv(BUXTON_ROOT_CHECK_ENV);
305 bool skip_check = (root_check && streq(root_check, "0"));
308 fail_if(buxton_direct_open(&c) == false,
309 "Direct open failed without daemon.");
310 fail_if(buxton_direct_create_group(&c, &key, NULL) == false,
311 "Creating group failed.");
312 fail_if(buxton_direct_set_label(&c, &key, &label) == false,
313 "Failed to set label as root user.");
318 fail_if(!buxton_direct_set_label(&c, &key, &label),
319 "Unable to set label with root check disabled");
321 fail_if(buxton_direct_set_label(&c, &key, &label),
322 "Able to set label as non-root user.");
324 buxton_direct_close(&c);
328 START_TEST(buxton_group_label_check)
333 BuxtonString label = buxton_string_pack("*");
335 key.layer = buxton_string_pack("test-gdbm");
336 key.group = buxton_string_pack("test-group");
337 key.name.value = NULL;
342 fail_if(buxton_direct_open(&c) == false,
343 "Direct open failed without daemon.");
344 fail_if(buxton_direct_create_group(&c, &key, NULL) == false,
345 "Creating group failed.");
346 fail_if(buxton_direct_set_label(&c, &key, &label) == false,
347 "Failed to set group label.");
348 fail_if(buxton_direct_get_value_for_layer(&c, &key, &result, &dlabel, NULL),
349 "Retrieving group label failed.");
350 fail_if(!streq("*", dlabel.value),
351 "Retrieved group label is incorrect.");
354 buxton_direct_close(&c);
358 START_TEST(buxton_name_label_check)
361 BuxtonData data, result;
362 BuxtonString label, dlabel;
365 /* create the group first, and validate the label */
366 key.layer = buxton_string_pack("test-gdbm");
367 key.group = buxton_string_pack("group-foo");
368 key.name.value = NULL;
370 label = buxton_string_pack("*");
373 fail_if(buxton_direct_open(&c) == false,
374 "Direct open failed without daemon.");
375 fail_if(buxton_direct_create_group(&c, &key, NULL) == false,
376 "Creating group failed.");
377 fail_if(buxton_direct_set_label(&c, &key, &label) == false,
378 "Failed to set group label.");
379 fail_if(buxton_direct_get_value_for_layer(&c, &key, &result, &dlabel, NULL),
380 "Retrieving group label failed.");
381 fail_if(!streq("*", dlabel.value),
382 "Retrieved group label is incorrect.");
384 free(result.store.d_string.value);
386 /* then create the name (key), and validate the label */
387 key.name = buxton_string_pack("name-foo");
389 data.store.d_string = buxton_string_pack("value1-foo");
390 fail_if(buxton_direct_set_value(&c, &key, &data, NULL) == false,
391 "Failed to set key name-foo.");
392 fail_if(buxton_direct_get_value_for_layer(&c, &key, &result, &dlabel, NULL),
393 "Failed to get value for name-foo 1");
394 fail_if(!streq("value1-foo", result.store.d_string.value),
395 "Retrieved key value is incorrect 1");
396 fail_if(!streq(dlabel.value, "_"), "Failed to set default label");
398 free(result.store.d_string.value);
399 fail_if(buxton_direct_set_label(&c, &key, &label) == false,
400 "Failed to set name label.");
401 fail_if(buxton_direct_get_value_for_layer(&c, &key, &result, &dlabel, NULL),
402 "Failed to get value for name-foo 2");
403 fail_if(!streq("value1-foo", result.store.d_string.value),
404 "Retrieved key value is incorrect 2");
405 fail_if(!streq("*", dlabel.value),
406 "Retrieved key label is incorrect.");
408 free(result.store.d_string.value);
410 /* modify the same key, with a new value, and validate the label */
411 data.store.d_string = buxton_string_pack("value2-foo");
412 fail_if(buxton_direct_set_value(&c, &key, &data, NULL) == false,
413 "Failed to modify key name-foo.");
414 fail_if(buxton_direct_get_value_for_layer(&c, &key, &result, &dlabel, NULL),
415 "Failed to get new value for name-foo.");
416 fail_if(!streq("value2-foo", result.store.d_string.value),
417 "New key value is incorrect.");
418 fail_if(!streq("*", dlabel.value),
419 "Key label has been modified.");
421 /* modify the key label directly once it has been created */
422 fail_if(buxton_direct_set_label(&c, &key, &label) == false,
423 "Failed to modify label on key.");
426 free(result.store.d_string.value);
428 buxton_direct_close(&c);
432 static int run_callback_test_value = 0;
433 static void run_callback_cb_test(BuxtonResponse response, void *data)
435 _BuxtonResponse *r = (_BuxtonResponse *)response;
439 fail_if(r->type != BUXTON_CONTROL_SET, "Unexpected response type");
441 fail_if(!streq(r->key->group.value, "group"),
442 "Failed to set key's group");
444 switch (run_callback_test_value) {
446 /* first pass through */
447 run_callback_test_value = 1;
450 /* second pass through */
451 fail_if(r->data->len != 1, "Failed setup array size");
452 d = buxton_array_get(r->data, 0);
453 fail_if(!d, "Failed to set array element");
454 fail_if(d->type != INT32, "Failed to setup array element value");
455 run_callback_test_value = 2;
458 /* third pass through */
463 fail("Unexpected test value");
467 START_TEST(run_callback_check)
470 BuxtonData list[] = {
471 {INT32, {.d_int32 = 1}}
474 key.group = buxton_string_pack("group");
476 run_callback(NULL, (void *)&data, 1, list, BUXTON_CONTROL_SET, &key);
477 run_callback(run_callback_cb_test, NULL, 0, NULL, BUXTON_CONTROL_SET,
479 fail_if(run_callback_test_value != 1,
480 "Failed to update callback test value 1");
481 run_callback(run_callback_cb_test, NULL, 1, list, BUXTON_CONTROL_SET,
483 fail_if(run_callback_test_value != 2,
484 "Failed to update callback test value 2");
485 run_callback(run_callback_cb_test, (void *)&data, 0, NULL,
486 BUXTON_CONTROL_SET, &key);
487 fail_if(!data, "Failed to update callback test value 3");
491 START_TEST(send_message_check)
493 _BuxtonClient client;
494 BuxtonArray *out_list = NULL;
495 BuxtonData *list = NULL;
497 uint8_t *dest = NULL;
498 uint8_t *source = NULL;
502 setup_socket_pair(&(client.fd), &server);
503 fail_if(fcntl(client.fd, F_SETFL, O_NONBLOCK),
504 "Failed to set socket to non blocking");
505 fail_if(fcntl(server, F_SETFL, O_NONBLOCK),
506 "Failed to set socket to non blocking");
508 fail_if(!setup_callbacks(),
509 "Failed to setup callbacks");
511 out_list = buxton_array_new();
513 data.store.d_int32 = 0;
514 fail_if(!buxton_array_add(out_list, &data),
515 "Failed to add get response to array");
516 size = buxton_serialize_message(&source, BUXTON_CONTROL_STATUS,
518 fail_if(size == 0, "Failed to serialize message");
519 fail_if(!send_message(&client, source, size, NULL, NULL, 0,
520 BUXTON_CONTROL_STATUS, NULL),
521 "Failed to write message 1");
524 buxton_array_free(&out_list, NULL);
532 static void handle_response_cb_test(_BuxtonResponse *response, void *data)
534 bool *val = (bool *)data;
535 fail_if(!(*val), "Got unexpected response data");
538 START_TEST(handle_callback_response_check)
540 _BuxtonClient client;
541 BuxtonArray *out_list = NULL;
542 uint8_t *dest = NULL;
548 BuxtonData good[] = {
549 {INT32, {.d_int32 = 0}}
551 BuxtonData good_unnotify[] = {
552 {INT32, {.d_int32 = 0 }},
553 {STRING, {.d_string = {0}}},
554 {UINT32, {.d_uint32 = 4 }}
556 BuxtonData bad1[] = {
557 {INT64, {.d_int64 = 0}}
559 BuxtonData bad2[] = {
560 {INT32, {.d_int32 = 1}}
563 setup_socket_pair(&(client.fd), &server);
564 fail_if(fcntl(client.fd, F_SETFL, O_NONBLOCK),
565 "Failed to set socket to non blocking");
566 fail_if(fcntl(server, F_SETFL, O_NONBLOCK),
567 "Failed to set socket to non blocking");
569 /* done just to create a callback to be used */
570 fail_if(!setup_callbacks(),
571 "Failed to initialeze response callbacks");
572 out_list = buxton_array_new();
574 data.store.d_int32 = 0;
576 fail_if(!buxton_array_add(out_list, &data),
577 "Failed to add data to array");
578 size = buxton_serialize_message(&dest, BUXTON_CONTROL_STATUS,
580 buxton_array_free(&out_list, NULL);
581 fail_if(size == 0, "Failed to serialize message");
584 fail_if(!send_message(&client, dest, size, handle_response_cb_test,
585 &test_data, msgid, BUXTON_CONTROL_SET, NULL),
586 "Failed to send message %d", msgid);
587 handle_callback_response(BUXTON_CONTROL_STATUS, msgid, bad1, 1);
588 fail_if(test_data, "Failed to set cb data non notify type");
592 fail_if(!send_message(&client, dest, size, handle_response_cb_test,
593 &test_data, msgid, BUXTON_CONTROL_NOTIFY, NULL),
594 "Failed to send message %d", msgid);
595 handle_callback_response(BUXTON_CONTROL_STATUS, msgid, bad1, 1);
596 fail_if(test_data, "Failed to set notify bad1 data");
600 fail_if(!send_message(&client, dest, size, handle_response_cb_test,
601 &test_data, msgid, BUXTON_CONTROL_NOTIFY, NULL),
602 "Failed to send message %d", msgid);
603 handle_callback_response(BUXTON_CONTROL_STATUS, msgid, bad2, 1);
604 fail_if(test_data, "Failed to set notify bad2 data");
608 fail_if(!send_message(&client, dest, size, handle_response_cb_test,
609 &test_data, msgid, BUXTON_CONTROL_NOTIFY, NULL),
610 "Failed to send message %d", msgid);
611 handle_callback_response(BUXTON_CONTROL_STATUS, msgid, good, 1);
612 fail_if(!test_data, "Set notify good data");
614 /* ensure we run callback on duplicate msgid */
615 fail_if(!send_message(&client, dest, size, handle_response_cb_test,
616 &test_data, msgid, BUXTON_CONTROL_NOTIFY, NULL),
617 "Failed to send message %d-2", msgid);
618 handle_callback_response(BUXTON_CONTROL_STATUS, msgid, good, 1);
619 fail_if(test_data, "Failed to set notify duplicate msgid");
622 handle_callback_response(BUXTON_CONTROL_CHANGED, msgid, good, 1);
623 fail_if(test_data, "Failed to set changed data");
625 /* ensure we don't remove callback on changed */
627 handle_callback_response(BUXTON_CONTROL_CHANGED, msgid, good, 1);
628 fail_if(test_data, "Failed to set changed data");
632 fail_if(!send_message(&client, dest, size, handle_response_cb_test,
633 &test_data, msgid, BUXTON_CONTROL_UNNOTIFY, NULL),
634 "Failed to send message %d", msgid);
635 handle_callback_response(BUXTON_CONTROL_STATUS, msgid, bad1, 1);
636 fail_if(test_data, "Failed to set unnotify bad1 data");
640 fail_if(!send_message(&client, dest, size, handle_response_cb_test,
641 &test_data, msgid, BUXTON_CONTROL_UNNOTIFY, NULL),
642 "Failed to send message %d", msgid);
643 handle_callback_response(BUXTON_CONTROL_STATUS, msgid, bad2, 1);
644 fail_if(test_data, "Failed to set unnotify bad2 data");
648 fail_if(!send_message(&client, dest, size, handle_response_cb_test,
649 &test_data, msgid, BUXTON_CONTROL_UNNOTIFY, NULL),
650 "Failed to send message %d", msgid);
651 handle_callback_response(BUXTON_CONTROL_STATUS, msgid, good_unnotify, 1);
652 fail_if(!test_data, "Set unnotify good data");
656 handle_callback_response(BUXTON_CONTROL_CHANGED, msgid, good, 1);
657 fail_if(!test_data, "Didn't remove changed callback");
666 START_TEST(buxton_wire_handle_response_check)
668 _BuxtonClient client;
669 BuxtonArray *out_list = NULL;
671 uint8_t *dest = NULL;
674 bool test_data = true;
676 setup_socket_pair(&(client.fd), &server);
677 fail_if(fcntl(client.fd, F_SETFL, O_NONBLOCK),
678 "Failed to set socket to non blocking");
679 fail_if(fcntl(server, F_SETFL, O_NONBLOCK),
680 "Failed to set socket to non blocking");
682 /* done just to create a callback to be used */
683 fail_if(!setup_callbacks(),
684 "Failed to initialeze get response callbacks");
685 out_list = buxton_array_new();
687 data.store.d_int32 = 0;
688 fail_if(!buxton_array_add(out_list, &data),
689 "Failed to add get response to array");
690 size = buxton_serialize_message(&dest, BUXTON_CONTROL_STATUS,
692 buxton_array_free(&out_list, NULL);
693 fail_if(size == 0, "Failed to serialize message");
694 fail_if(!send_message(&client, dest, size, handle_response_cb_test,
695 &test_data, 0, BUXTON_CONTROL_STATUS, NULL),
696 "Failed to send message");
699 fail_if(!_write(server, dest, size),
700 "Failed to send get response");
703 fail_if(buxton_wire_handle_response(&client) != 1,
704 "Failed to handle response correctly");
705 fail_if(test_data, "Failed to update data");
714 START_TEST(buxton_wire_get_response_check)
716 _BuxtonClient client;
717 BuxtonArray *out_list = NULL;
719 uint8_t *dest = NULL;
722 bool test_data = true;
724 setup_socket_pair(&(client.fd), &server);
725 fail_if(fcntl(client.fd, F_SETFL, O_NONBLOCK),
726 "Failed to set socket to non blocking");
727 fail_if(fcntl(server, F_SETFL, O_NONBLOCK),
728 "Failed to set socket to non blocking");
730 /* done just to create a callback to be used */
731 fail_if(!setup_callbacks(),
732 "Failed to initialeze callbacks");
733 out_list = buxton_array_new();
735 data.store.d_int32 = 0;
736 fail_if(!buxton_array_add(out_list, &data),
737 "Failed to add data to array");
738 size = buxton_serialize_message(&dest, BUXTON_CONTROL_STATUS,
740 buxton_array_free(&out_list, NULL);
741 fail_if(size == 0, "Failed to serialize message");
742 fail_if(!send_message(&client, dest, size, handle_response_cb_test,
743 &test_data, 0, BUXTON_CONTROL_STATUS, NULL),
744 "Failed to send message");
747 fail_if(!_write(server, dest, size),
748 "Failed to send get response");
751 fail_if(!buxton_wire_get_response(&client),
752 "Failed to handle response correctly");
753 fail_if(test_data, "Failed to update data");
762 START_TEST(buxton_wire_set_value_check)
764 _BuxtonClient client;
767 BuxtonData *list = NULL;
771 BuxtonControlMessage msg;
774 setup_socket_pair(&(client.fd), &server);
775 fail_if(fcntl(client.fd, F_SETFL, O_NONBLOCK),
776 "Failed to set socket to non blocking");
777 fail_if(fcntl(server, F_SETFL, O_NONBLOCK),
778 "Failed to set socket to non blocking");
780 fail_if(!setup_callbacks(),
781 "Failed to initialeze callbacks");
783 key.layer = buxton_string_pack("layer");
784 key.group = buxton_string_pack("group");
785 key.name = buxton_string_pack("name");
788 fail_if(buxton_wire_set_value(&client, &key, "value", NULL,
790 "Failed to properly set value");
792 r = read(server, buf, 4096);
793 fail_if(r < 0, "Read from client failed");
794 size = buxton_deserialize_message(buf, &msg, (size_t)r, &msgid, &list);
795 fail_if(size != 4, "Failed to get valid message from buffer");
796 fail_if(msg != BUXTON_CONTROL_SET,
797 "Failed to get correct control type");
798 fail_if(list[0].type != STRING, "Failed to set correct layer type");
799 fail_if(list[1].type != STRING, "Failed to set correct group type");
800 fail_if(list[2].type != STRING, "Failed to set correct name type");
801 fail_if(list[3].type != STRING, "Failed to set correct value type");
802 fail_if(!streq(list[0].store.d_string.value, "layer"),
803 "Failed to set correct layer");
804 fail_if(!streq(list[1].store.d_string.value, "group"),
805 "Failed to set correct group");
806 fail_if(!streq(list[2].store.d_string.value, "name"),
807 "Failed to set correct name");
808 fail_if(!streq(list[3].store.d_string.value, "value"),
809 "Failed to set correct value");
811 free(list[0].store.d_string.value);
812 free(list[1].store.d_string.value);
813 free(list[2].store.d_string.value);
814 free(list[3].store.d_string.value);
822 START_TEST(buxton_wire_set_label_check)
824 _BuxtonClient client;
827 BuxtonData *list = NULL;
832 BuxtonControlMessage msg;
836 setup_socket_pair(&(client.fd), &server);
837 fail_if(fcntl(client.fd, F_SETFL, O_NONBLOCK),
838 "Failed to set socket to non blocking");
839 fail_if(fcntl(server, F_SETFL, O_NONBLOCK),
840 "Failed to set socket to non blocking");
842 fail_if(!setup_callbacks(),
843 "Failed to initialize callbacks");
845 /* first, set a label on a group */
846 key.layer = buxton_string_pack("layer");
847 key.group = buxton_string_pack("group");
848 key.name.value = NULL;
849 value = buxton_string_pack("*");
850 fail_if(buxton_wire_set_label(&client, &key, &value, NULL,
852 "Failed to properly set label");
854 r = read(server, buf, 4096);
855 fail_if(r < 0, "Read from client failed");
856 size = buxton_deserialize_message(buf, &msg, (size_t)r, &msgid, &list);
857 fail_if(size != 3, "Failed to get valid message from buffer");
858 fail_if(msg != BUXTON_CONTROL_SET_LABEL,
859 "Failed to get correct control type");
860 fail_if(list[0].type != STRING, "Failed to set correct layer type");
861 fail_if(list[1].type != STRING, "Failed to set correct group type");
862 fail_if(list[2].type != STRING, "Failed to set correct label type");
863 fail_if(!streq(list[0].store.d_string.value, "layer"),
864 "Failed to set correct layer");
865 fail_if(!streq(list[1].store.d_string.value, "group"),
866 "Failed to set correct group");
867 fail_if(!streq(list[2].store.d_string.value, "*"),
868 "Failed to set correct label");
870 free(list[0].store.d_string.value);
871 free(list[1].store.d_string.value);
872 free(list[2].store.d_string.value);
875 /* ... then, set a label on a key */
876 key.layer = buxton_string_pack("layer");
877 key.group = buxton_string_pack("group");
878 key.name = buxton_string_pack("name");
879 value = buxton_string_pack("*");
880 fail_if(buxton_wire_set_label(&client, &key, &value, NULL,
882 "Failed to properly set label");
884 r = read(server, buf, 4096);
885 fail_if(r < 0, "Read from client failed");
886 size = buxton_deserialize_message(buf, &msg, (size_t)r, &msgid, &list);
887 fail_if(size != 4, "Failed to get valid message from buffer");
888 fail_if(msg != BUXTON_CONTROL_SET_LABEL,
889 "Failed to get correct control type");
890 fail_if(list[0].type != STRING, "Failed to set correct layer type");
891 fail_if(list[1].type != STRING, "Failed to set correct group type");
892 fail_if(list[2].type != STRING, "Failed to set correct name type");
893 fail_if(list[3].type != STRING, "Failed to set correct label type");
894 fail_if(!streq(list[0].store.d_string.value, "layer"),
895 "Failed to set correct layer");
896 fail_if(!streq(list[1].store.d_string.value, "group"),
897 "Failed to set correct group");
898 fail_if(!streq(list[2].store.d_string.value, "name"),
899 "Failed to set correct name");
900 fail_if(!streq(list[3].store.d_string.value, "*"),
901 "Failed to set correct label");
903 free(list[0].store.d_string.value);
904 free(list[1].store.d_string.value);
905 free(list[2].store.d_string.value);
906 free(list[3].store.d_string.value);
915 START_TEST(buxton_wire_get_value_check)
917 _BuxtonClient client;
920 BuxtonData *list = NULL;
924 BuxtonControlMessage msg;
927 setup_socket_pair(&(client.fd), &server);
928 fail_if(fcntl(client.fd, F_SETFL, O_NONBLOCK),
929 "Failed to set socket to non blocking");
930 fail_if(fcntl(server, F_SETFL, O_NONBLOCK),
931 "Failed to set socket to non blocking");
933 fail_if(!setup_callbacks(),
934 "Failed to initialeze callbacks");
936 key.layer = buxton_string_pack("layer");
937 key.group = buxton_string_pack("group");
938 key.name = buxton_string_pack("name");
940 fail_if(buxton_wire_get_value(&client, &key, NULL,
942 "Failed to properly get value 1");
944 r = read(server, buf, 4096);
945 fail_if(r < 0, "Read from client failed 1");
946 size = buxton_deserialize_message(buf, &msg, (size_t)r, &msgid, &list);
947 fail_if(size != 4, "Failed to get valid message from buffer 1");
948 fail_if(msg != BUXTON_CONTROL_GET,
949 "Failed to get correct control type 1");
950 fail_if(list[0].type != STRING, "Failed to set correct layer type 1");
951 fail_if(list[1].type != STRING, "Failed to set correct group type 1");
952 fail_if(list[2].type != STRING, "Failed to set correct name type 1");
953 fail_if(list[3].type != UINT32, "Failed to set correct type type 1");
954 fail_if(!streq(list[0].store.d_string.value, "layer"),
955 "Failed to set correct layer 1");
956 fail_if(!streq(list[1].store.d_string.value, "group"),
957 "Failed to set correct group 1");
958 fail_if(!streq(list[2].store.d_string.value, "name"),
959 "Failed to set correct name 1");
960 fail_if(list[3].store.d_uint32 != STRING,
961 "Failed to set correct type 1");
963 free(list[0].store.d_string.value);
964 free(list[1].store.d_string.value);
965 free(list[2].store.d_string.value);
968 key.layer.value = NULL;
969 fail_if(buxton_wire_get_value(&client, &key, NULL,
971 "Failed to properly get value 2");
973 r = read(server, buf, 4096);
974 fail_if(r < 0, "Read from client failed 2");
975 size = buxton_deserialize_message(buf, &msg, (size_t)r, &msgid, &list);
976 fail_if(size != 3, "Failed to get valid message from buffer 2");
977 fail_if(msg != BUXTON_CONTROL_GET,
978 "Failed to get correct control type 2");
979 fail_if(list[0].type != STRING, "Failed to set correct group type 2");
980 fail_if(list[1].type != STRING, "Failed to set correct name type 2");
981 fail_if(list[2].type != UINT32, "Failed to set correct type type 2");
982 fail_if(!streq(list[0].store.d_string.value, "group"),
983 "Failed to set correct group 2");
984 fail_if(!streq(list[1].store.d_string.value, "name"),
985 "Failed to set correct name 2");
986 fail_if(list[2].store.d_uint32 != STRING,
987 "Failed to set correct type 2");
989 free(list[0].store.d_string.value);
990 free(list[1].store.d_string.value);
999 START_TEST(buxton_wire_unset_value_check)
1001 _BuxtonClient client;
1004 BuxtonData *list = NULL;
1008 BuxtonControlMessage msg;
1011 setup_socket_pair(&(client.fd), &server);
1012 fail_if(fcntl(client.fd, F_SETFL, O_NONBLOCK),
1013 "Failed to set socket to non blocking");
1014 fail_if(fcntl(server, F_SETFL, O_NONBLOCK),
1015 "Failed to set socket to non blocking");
1017 fail_if(!setup_callbacks(),
1018 "Failed to initialeze callbacks");
1020 key.layer = buxton_string_pack("layer");
1021 key.group = buxton_string_pack("group");
1022 key.name = buxton_string_pack("name");
1024 fail_if(!buxton_wire_unset_value(&client, &key, NULL,
1026 "Failed to properly unset value");
1028 r = read(server, buf, 4096);
1029 fail_if(r < 0, "Read from client failed");
1030 size = buxton_deserialize_message(buf, &msg, (size_t)r, &msgid, &list);
1031 fail_if(size != 4, "Failed to get valid message from buffer");
1032 fail_if(msg != BUXTON_CONTROL_UNSET,
1033 "Failed to get correct control type");
1034 fail_if(list[0].type != STRING, "Failed to set correct layer type");
1035 fail_if(list[1].type != STRING, "Failed to set correct group type");
1036 fail_if(list[2].type != STRING, "Failed to set correct name type");
1037 fail_if(list[3].type != UINT32, "Failed to set correct type type");
1038 fail_if(!streq(list[0].store.d_string.value, "layer"),
1039 "Failed to set correct layer");
1040 fail_if(!streq(list[1].store.d_string.value, "group"),
1041 "Failed to set correct group");
1042 fail_if(!streq(list[2].store.d_string.value, "name"),
1043 "Failed to set correct group");
1044 fail_if(list[3].store.d_uint32 != STRING,
1045 "Failed to set correct type");
1047 free(list[0].store.d_string.value);
1048 free(list[1].store.d_string.value);
1049 free(list[2].store.d_string.value);
1052 cleanup_callbacks();
1058 START_TEST(buxton_wire_create_group_check)
1060 _BuxtonClient client;
1063 BuxtonData *list = NULL;
1067 BuxtonControlMessage msg;
1071 setup_socket_pair(&(client.fd), &server);
1072 fail_if(fcntl(client.fd, F_SETFL, O_NONBLOCK),
1073 "Failed to set socket to non blocking");
1074 fail_if(fcntl(server, F_SETFL, O_NONBLOCK),
1075 "Failed to set socket to non blocking");
1077 fail_if(!setup_callbacks(),
1078 "Failed to initialize callbacks");
1080 key.layer = buxton_string_pack("layer");
1081 key.group = buxton_string_pack("group");
1082 key.name.value = NULL;
1083 fail_if(buxton_wire_create_group(&client, &key, NULL,
1085 "Failed to send message");
1087 r = read(server, buf, 4096);
1088 fail_if(r < 0, "Read from client failed");
1089 size = buxton_deserialize_message(buf, &msg, (size_t)r, &msgid, &list);
1090 fail_if(size != 2, "Failed to get valid message from buffer");
1091 fail_if(msg != BUXTON_CONTROL_CREATE_GROUP,
1092 "Failed to get correct control type");
1093 fail_if(list[0].type != STRING, "Failed to set correct layer type");
1094 fail_if(list[1].type != STRING, "Failed to set correct group type");
1095 fail_if(!streq(list[0].store.d_string.value, "layer"),
1096 "Failed to set correct layer");
1097 fail_if(!streq(list[1].store.d_string.value, "group"),
1098 "Failed to set correct group");
1100 free(list[0].store.d_string.value);
1101 free(list[1].store.d_string.value);
1103 cleanup_callbacks();
1109 START_TEST(buxton_wire_remove_group_check)
1111 _BuxtonClient client;
1114 BuxtonData *list = NULL;
1118 BuxtonControlMessage msg;
1122 setup_socket_pair(&(client.fd), &server);
1123 fail_if(fcntl(client.fd, F_SETFL, O_NONBLOCK),
1124 "Failed to set socket to non blocking");
1125 fail_if(fcntl(server, F_SETFL, O_NONBLOCK),
1126 "Failed to set socket to non blocking");
1128 fail_if(!setup_callbacks(),
1129 "Failed to initialize callbacks");
1131 key.layer = buxton_string_pack("layer");
1132 key.group = buxton_string_pack("group");
1133 key.name.value = NULL;
1134 fail_if(buxton_wire_remove_group(&client, &key, NULL,
1136 "Failed to send message");
1138 r = read(server, buf, 4096);
1139 fail_if(r < 0, "Read from client failed");
1140 size = buxton_deserialize_message(buf, &msg, (size_t)r, &msgid, &list);
1141 fail_if(size != 2, "Failed to get valid message from buffer");
1142 fail_if(msg != BUXTON_CONTROL_REMOVE_GROUP,
1143 "Failed to get correct control type");
1144 fail_if(list[0].type != STRING, "Failed to set correct layer type");
1145 fail_if(list[1].type != STRING, "Failed to set correct group type");
1146 fail_if(!streq(list[0].store.d_string.value, "layer"),
1147 "Failed to set correct layer");
1148 fail_if(!streq(list[1].store.d_string.value, "group"),
1149 "Failed to set correct group");
1151 free(list[0].store.d_string.value);
1152 free(list[1].store.d_string.value);
1154 cleanup_callbacks();
1166 s = suite_create("buxton");
1168 tc = tcase_create("buxton_client_lib_functions");
1169 tcase_add_test(tc, buxton_direct_init_db_check);
1170 tcase_add_test(tc, buxton_direct_open_check);
1171 tcase_add_test(tc, buxton_direct_create_group_check);
1172 tcase_add_test(tc, buxton_direct_remove_group_check);
1173 tcase_add_test(tc, buxton_direct_set_value_check);
1174 tcase_add_test(tc, buxton_direct_get_value_for_layer_check);
1175 tcase_add_test(tc, buxton_direct_get_value_check);
1176 tcase_add_test(tc, buxton_memory_backend_check);
1177 tcase_add_test(tc, buxton_key_check);
1178 tcase_add_test(tc, buxton_set_label_check);
1179 tcase_add_test(tc, buxton_group_label_check);
1180 tcase_add_test(tc, buxton_name_label_check);
1181 suite_add_tcase(s, tc);
1183 tc = tcase_create("buxton_protocol_functions");
1184 tcase_add_test(tc, run_callback_check);
1185 tcase_add_test(tc, handle_callback_response_check);
1186 tcase_add_test(tc, send_message_check);
1187 tcase_add_test(tc, buxton_wire_handle_response_check);
1188 tcase_add_test(tc, buxton_wire_get_response_check);
1189 tcase_add_test(tc, buxton_wire_set_value_check);
1190 tcase_add_test(tc, buxton_wire_set_label_check);
1191 tcase_add_test(tc, buxton_wire_get_value_check);
1192 tcase_add_test(tc, buxton_wire_unset_value_check);
1193 tcase_add_test(tc, buxton_wire_create_group_check);
1194 tcase_add_test(tc, buxton_wire_remove_group_check);
1195 suite_add_tcase(s, tc);
1206 putenv("BUXTON_CONF_FILE=" ABS_TOP_BUILDDIR "/test/test.conf");
1207 putenv("BUXTON_ROOT_CHECK=0");
1209 sr = srunner_create(s);
1210 srunner_run_all(sr, CK_VERBOSE);
1211 number_failed = srunner_ntests_failed(sr);
1214 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
1219 * Editor modelines - http://www.wireshark.org/tools/modelines.html
1224 * indent-tabs-mode: t
1227 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
1228 * :indentSize=8:tabSize=8:noTabs=false: