-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest.c
2920 lines (2499 loc) · 98 KB
/
test.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* A test suite for uSHET. Tests functionality of underlying protocol
* implementation and API but does not test network connectivity etc.
*/
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <limits.h>
// Include the C files so that static functions can be tested
#include "lib/jsmn.c"
#include "lib/shet.c"
#include "lib/shet_json.c"
#include "lib/ezshet.c"
////////////////////////////////////////////////////////////////////////////////
// JSON test utilities
////////////////////////////////////////////////////////////////////////////////
// Given two JSON strings and their tokens return the token which points to the
// first differing componenst in each via differing_{a,b}. Returns a bool which
// is true if the JSON is the same and false if not. If differing_{a,b} are
// NULL, they will not be set. tokens_{a,b} are expected to be double pointers
// to a token pointer initialised to the first token to process.
bool cmp_json_tokens(const char *json_a, const char *json_b,
jsmntok_t **tokens_a, jsmntok_t **tokens_b,
jsmntok_t *differing_a, jsmntok_t *differing_b) {
// Initially just check to see if the types are even comparable
if ((**tokens_a).type != (**tokens_b).type ||
(**tokens_a).size != (**tokens_b).size) {
*differing_a = **tokens_a;
*differing_b = **tokens_b;
return false;
// Compare atomic objects
} else if ((*tokens_a)->type == JSMN_PRIMITIVE ||
(*tokens_a)->type == JSMN_STRING) {
if ( ((*tokens_a)->end - (*tokens_a)->start) !=
((*tokens_b)->end - (*tokens_b)->start) ||
strncmp(json_a+(*tokens_a)->start,
json_b+(*tokens_b)->start,
(*tokens_a)->end - (*tokens_a)->start) != 0) {
if (differing_a != NULL) *differing_a = **tokens_a;
if (differing_b != NULL) *differing_b = **tokens_b;
return false;
} else {
(*tokens_a)++;
(*tokens_b)++;
return true;
}
// Iterate over and compare compound objects
} else {
int size = (**tokens_a).size;
(*tokens_a)++;
(*tokens_b)++;
int i;
for (i = 0; i < size; i++)
if (!cmp_json_tokens(json_a, json_b,
tokens_a, tokens_b,
differing_a, differing_b))
return false;
return true;
}
}
////////////////////////////////////////////////////////////////////////////////
// Test suite assertions
////////////////////////////////////////////////////////////////////////////////
// Standard "assert true" assertion
#define TASSERT(x) do { if (!(x)) { \
fprintf(stderr, "TASSERT Failed: %s:%s:%d: "#x"\n",\
__FILE__,__func__,__LINE__);\
return false; \
} } while (0)
// Assert equal integers (prints the value of the integers on error)
#define TASSERT_INT_EQUAL(a,b) do { \
int va = (a); \
int vb = (b); \
if ((va) != (vb)) { \
fprintf(stderr, "TASSERT_INT_EQUAL Failed: %s:%s:%d: "#a" (%d) == "#b" (%d)\n",\
__FILE__,__func__,__LINE__, (va), (vb));\
return false; \
} } while (0)
// Compare two JSON strings given as a set of tokens and assert that they are
// equivalent. If they are not, prints the two strings along with an indicator
// pointing at the first non-matching parts.
#define TASSERT_JSON_EQUAL_TOK_TOK(ja,jb) do { \
char *sa = ja.line;\
jsmntok_t *ta = ja.token;\
char *sb = jb.line;\
jsmntok_t *tb = jb.token;\
TASSERT((sa) != NULL);\
TASSERT((ta) != NULL);\
TASSERT((sb) != NULL);\
TASSERT((tb) != NULL);\
const char *a = (sa); /* JSON String a */ \
const char *b = (sb); /* JSON String b */ \
jsmntok_t *ca = (ta); /* Cur token pointer a */ \
jsmntok_t *cb = (tb); /* Cur token pointer b */ \
jsmntok_t da; /* Differing token a */ \
da.start = (ta)->start; \
jsmntok_t db; /* Differing token b */ \
db.start = (tb)->start; \
if (!cmp_json_tokens(a,b,&ca,&cb,&da,&db)) { \
fprintf(stderr, "TASSERT_JSON_EQUAL Failed: %s:%s:%d:\n",\
__FILE__,__func__,__LINE__);\
/* Print strings with their quotes */ \
if ((ta)->type == JSMN_STRING) { \
(ta)->start--; \
(ta)->end++; \
} \
if ((tb)->type == JSMN_STRING) { \
(tb)->start--; \
(tb)->end++; \
} \
/* Print the difference */ \
fprintf(stderr, " > \"%.*s\"\n", (ta)->end-(ta)->start, a+(ta)->start);\
fprintf(stderr, " > ");\
if (da.start-(ta)->start == db.start-(tb)->start) { \
int i; \
for (i = (ta)->start; i < da.start-(ta)->start; i++) fprintf(stderr, " "); \
fprintf(stderr, "|\n"); \
} else if (da.start-(ta)->start < db.start-(tb)->start) { \
int i; \
for (i = (ta)->start; i < da.start-(ta)->start; i++) fprintf(stderr, " "); \
fprintf(stderr, "^"); \
for (i = da.start-(ta)->start; i < db.start-(tb)->start-1; i++) fprintf(stderr, "-"); \
fprintf(stderr, "v\n"); \
} else { \
int i; \
for (i = (tb)->start; i < db.start-(tb)->start; i++) fprintf(stderr, " "); \
fprintf(stderr, "v"); \
for (i = db.start-(tb)->start; i < da.start-(ta)->start-1; i++) fprintf(stderr, "-"); \
fprintf(stderr, "^\n"); \
} \
fprintf(stderr, " > \"%.*s\"\n", (tb)->end-(tb)->start, b+(tb)->start);\
return false; \
} \
} while (0)
// Compare two JSON strings the first given as a string and token and the other
// as a string and assert that they are equivilent.
#define TASSERT_JSON_EQUAL_TOK_STR(ja,sb) do { \
jsmn_parser p; \
jsmn_init(&p); \
jsmntok_t tb[100]; \
jsmnerr_t e = jsmn_parse(&p, (sb), strlen((sb)), \
tb, 100); \
if (e <= 0) { \
fprintf(stderr, "TASSERT_JSON_EQUAL Could not parse JSON: %s:%s:%d: %s\n",\
__FILE__,__func__,__LINE__,(sb)); \
return false; \
} \
shet_json_t jb; \
/* XXX: Cast off the const for shet_json_t. This macro is known not to modify
* the string. */ \
jb.line = (char *)(sb); \
jb.token = tb; \
TASSERT_JSON_EQUAL_TOK_TOK((ja), jb);\
} while (0)
// Compare two JSON strings given as strings.
#define TASSERT_JSON_EQUAL_STR_STR(sa,sb) do { \
jsmn_parser p; \
jsmn_init(&p); \
jsmntok_t ta[100]; \
jsmnerr_t e = jsmn_parse(&p, (sa), strlen((sa)), \
ta, 100); \
if (e <= 0) { \
fprintf(stderr, "TASSERT_JSON_EQUAL Could not parse JSON: %s:%s:%d: %s\n",\
__FILE__,__func__,__LINE__,(sa)); \
return false; \
} \
shet_json_t ja; \
/* XXX: Cast off the const for shet_json_t. This macro is known not to modify
* the string. */ \
ja.line = (char *)(sa); \
ja.token = ta; \
TASSERT_JSON_EQUAL_TOK_STR(ja, (sb));\
} while (0)
////////////////////////////////////////////////////////////////////////////////
// Utility callbacks
////////////////////////////////////////////////////////////////////////////////
// A transmit function which simply counts the transmissions and records a
// pointer to the last transmitted value (obviously this pointer only has a
// limited lifetime).
static const char *transmit_last_data = NULL;
static void *transmit_last_user_data = NULL;
static int transmit_count = 0;
static void transmit_cb(const char *data, void *user_data) {
transmit_last_data = data;
transmit_last_user_data = user_data;
transmit_count++;
}
#define RESET_TRANSMIT_CB() do { \
transmit_last_data = NULL; \
transmit_last_user_data = NULL; \
transmit_count = 0; \
} while (0)
// Respond to a register command whose ID is supplied as an argument
#define RESPOND_TO_REGISTER(state, id) do { \
char str[100]; \
sprintf(str, "[%d,\"return\",0,null]", (id)); \
TASSERT_INT_EQUAL(shet_process_line((state), str, strlen(str)), SHET_PROC_OK); \
} while (0)
// A generic callback which simply places the callback arguments into a
// callback_result_t structure pointed to by the user variable.
typedef struct {
shet_state_t *state;
shet_json_t json;
int count;
const char *return_value;
} callback_result_t;
static void callback(shet_state_t *state, shet_json_t json, void *user_data) {
callback_result_t *result = (callback_result_t *)user_data;
if (result != NULL) {
result->state = state;
result->json = json;
result->count++;
} else {
fprintf(stderr, "TEST ERROR: callback didn't receive result pointer!\n");
}
}
// A callback like the above but which returns its argument
static void echo_callback(shet_state_t *state, shet_json_t json, void *user_data) {
callback(state, json, user_data);
// Return back the argument
char response[100];
strncpy(response, json.line + json.token[0].start, json.token[0].end - json.token[0].start);
response[json.token[0].end - json.token[0].start] = '\0';
shet_return(state, 0, response);
}
// A callback like the above but which returns success and returns a null value
static void success_callback(shet_state_t *state, shet_json_t json, void *user_data) {
callback(state, json, user_data);
shet_return(state, 0, "null");
}
// A callback like the above but which returns success and the value in
// the return_value field of callback_result_t.
static void const_callback(shet_state_t *state, shet_json_t json, void *user_data) {
callback(state, json, user_data);
shet_return(state, 0, ((callback_result_t *)user_data)->return_value);
}
////////////////////////////////////////////////////////////////////////////////
// Test token type checking
////////////////////////////////////////////////////////////////////////////////
bool test_SHET_JSON_IS_TYPE(void) {
// Make sure that the function can distinguish between primitives which are
// and are not integers
char *strings[] = {
"0",
"123",
"-123",
"true",
"false",
"null",
"\"str\"",
"[]",
"{}",
};
enum {
SHET_INT = 0,
SHET_FLOAT = 0,
SHET_BOOL,
SHET_NULL,
SHET_STRING,
SHET_ARRAY,
SHET_OBJECT,
} types[] = {
SHET_INT,
SHET_INT,
SHET_INT,
SHET_BOOL,
SHET_BOOL,
SHET_NULL,
SHET_STRING,
SHET_ARRAY,
SHET_OBJECT,
};
size_t num_strings = sizeof(strings)/sizeof(char *);
size_t i;
for (i = 0; i < num_strings; i++) {
jsmn_parser p;
jsmn_init(&p);
jsmntok_t tokens[4];
jsmnerr_t e = jsmn_parse( &p
, strings[i]
, strlen(strings[i])
, tokens
, 4
);
// Make sure the string tokenzied successfully
TASSERT(e >= 1);
shet_json_t json;
json.line = strings[i];
json.token = tokens;
// Check the types
if (types[i] == SHET_INT)
TASSERT(SHET_JSON_IS_TYPE(json, SHET_INT));
else
TASSERT(!SHET_JSON_IS_TYPE(json, SHET_INT));
if (types[i] == SHET_BOOL)
TASSERT(SHET_JSON_IS_TYPE(json, SHET_BOOL));
else
TASSERT(!SHET_JSON_IS_TYPE(json, SHET_BOOL));
if (types[i] == SHET_NULL)
TASSERT(SHET_JSON_IS_TYPE(json, SHET_NULL));
else
TASSERT(!SHET_JSON_IS_TYPE(json, SHET_NULL));
if (types[i] == SHET_STRING)
TASSERT(SHET_JSON_IS_TYPE(json, SHET_STRING));
else
TASSERT(!SHET_JSON_IS_TYPE(json, SHET_STRING));
if (types[i] == SHET_ARRAY)
TASSERT(SHET_JSON_IS_TYPE(json, SHET_ARRAY));
else
TASSERT(!SHET_JSON_IS_TYPE(json, SHET_ARRAY));
if (types[i] == SHET_OBJECT)
TASSERT(SHET_JSON_IS_TYPE(json, SHET_OBJECT));
else
TASSERT(!SHET_JSON_IS_TYPE(json, SHET_OBJECT));
}
return true;
}
////////////////////////////////////////////////////////////////////////////////
// Test token type conversion
////////////////////////////////////////////////////////////////////////////////
bool test_SHET_PARSE_JSON_VALUE_INT(void) {
char *json_ints[] = {"[0]", "[123]", "[-1]", "[+1]"};
int c_ints[] = { 0, 123, -1, +1 };
size_t num = sizeof(c_ints)/sizeof(int);
size_t i;
for (i = 0; i < num; i++) {
jsmn_parser p;
jsmn_init(&p);
jsmntok_t tokens[4];
jsmnerr_t e = jsmn_parse( &p
, json_ints[i]
, strlen(json_ints[i])
, tokens
, 4
);
TASSERT(e == 2);
shet_json_t json;
json.line = json_ints[i];
json.token = tokens + 1;
TASSERT_INT_EQUAL(SHET_PARSE_JSON_VALUE(json, SHET_INT), c_ints[i]);
}
return true;
}
bool test_SHET_PARSE_JSON_VALUE_FLOAT(void) {
char *json_floats[] = {"[0]", "[1.5]", "[-1.5]", "[+1.5]", "[1e7]"};
double c_floats[] = { 0.0, 1.5, -1.5, +1.5 , 1e7};
size_t num = sizeof(c_floats)/sizeof(double);
size_t i;
for (i = 0; i < num; i++) {
jsmn_parser p;
jsmn_init(&p);
jsmntok_t tokens[4];
jsmnerr_t e = jsmn_parse( &p
, json_floats[i]
, strlen(json_floats[i])
, tokens
, 4
);
TASSERT(e == 2);
shet_json_t json;
json.line = json_floats[i];
json.token = tokens + 1;
TASSERT(SHET_PARSE_JSON_VALUE(json, SHET_FLOAT) == c_floats[i]);
}
return true;
}
bool test_SHET_PARSE_JSON_VALUE_BOOL(void) {
char *json_bools[] = {"[true]", "[false]"};
bool c_bools[] = { true, false};
size_t num = sizeof(c_bools)/sizeof(bool);
size_t i;
for (i = 0; i < num; i++) {
jsmn_parser p;
jsmn_init(&p);
jsmntok_t tokens[4];
jsmnerr_t e = jsmn_parse( &p
, json_bools[i]
, strlen(json_bools[i])
, tokens
, 4
);
TASSERT(e == 2);
shet_json_t json;
json.line = json_bools[i];
json.token = tokens + 1;
TASSERT(SHET_PARSE_JSON_VALUE(json, SHET_BOOL) == c_bools[i]);
}
return true;
}
bool test_SHET_PARSE_JSON_VALUE_STRING(void) {
char s0[] = "[\"\"]";
char s1[] = "[\"I am a magical string!\"]";
char *json_strings[] = {s0,s1};
char *c_strings[] = {"", "I am a magical string!"};
size_t num = sizeof(c_strings)/sizeof(char *);
size_t i;
for (i = 0; i < num; i++) {
jsmn_parser p;
jsmn_init(&p);
jsmntok_t tokens[4];
jsmnerr_t e = jsmn_parse( &p
, json_strings[i]
, strlen(json_strings[i])
, tokens
, 4
);
TASSERT(e == 2);
shet_json_t json;
json.line = json_strings[i];
json.token = tokens + 1;
TASSERT(strcmp(SHET_PARSE_JSON_VALUE(json, SHET_STRING), c_strings[i]) == 0);
}
return true;
}
////////////////////////////////////////////////////////////////////////////////
// Test internal message generation functions
////////////////////////////////////////////////////////////////////////////////
bool test_send_command(void) {
shet_state_t state;
RESET_TRANSMIT_CB();
shet_state_init(&state, NULL, transmit_cb, NULL);
RESPOND_TO_REGISTER(&state, 0);
// Test sending with just a command
send_command(&state, "test1", NULL, NULL,
NULL, NULL, NULL, NULL);
TASSERT(transmit_count == 2);
TASSERT_JSON_EQUAL_STR_STR(transmit_last_data, "[1, \"test1\"]");
// Test that paths get quoted
send_command(&state, "test2", "/test", NULL,
NULL, NULL, NULL, NULL);
TASSERT(transmit_count == 3);
TASSERT_JSON_EQUAL_STR_STR(transmit_last_data, "[2, \"test2\", \"/test\"]");
// Test that arguments don't get quoted
send_command(&state, "test3", "/test", "[1,2,3], 4",
NULL, NULL, NULL, NULL);
TASSERT(transmit_count == 4);
TASSERT_JSON_EQUAL_STR_STR(transmit_last_data, "[3, \"test3\", \"/test\", [1,2,3],4]");
// And test that path-less commands still support arguments
send_command(&state, "test4", NULL, "5, [6,7,8]",
NULL, NULL, NULL, NULL);
TASSERT(transmit_count == 5);
TASSERT_JSON_EQUAL_STR_STR(transmit_last_data, "[4, \"test4\", 5,[6,7,8]]");
// Make sure no deferreds were created
TASSERT(state.callbacks == NULL);
// Make sure they are when required!
shet_deferred_t test_deferred;
callback_result_t result;
result.count = 0;
send_command(&state, "test5", NULL, NULL,
&test_deferred, callback, NULL, &result);
TASSERT(transmit_count == 6);
TASSERT(result.count == 0);
TASSERT_JSON_EQUAL_STR_STR(transmit_last_data, "[5, \"test5\"]");
char response[] = "[5, \"return\", 0, [1,2,3,4]]";
TASSERT(shet_process_line(&state, response, strlen(response)) == SHET_PROC_OK);
TASSERT(result.count == 1);
TASSERT_JSON_EQUAL_TOK_STR(result.json, "[1,2,3,4]");
return true;
}
////////////////////////////////////////////////////////////////////////////////
// Test deferred utility functions
////////////////////////////////////////////////////////////////////////////////
bool test_deferred_utilities(void) {
shet_state_t state;
RESET_TRANSMIT_CB();
shet_state_init(&state, NULL, transmit_cb, NULL);
RESPOND_TO_REGISTER(&state, 0);
// Make sure that the deferred list is initially empty
TASSERT(state.callbacks == NULL);
// Make sure searches don't find stuff or crash on empty lists
TASSERT(find_return_cb(&state, 0) == NULL);
TASSERT(find_named_cb(&state, "/", SHET_EVENT_CCB) == NULL);
TASSERT(find_named_cb(&state, "/", SHET_EVENT_DELETED_CCB) == NULL);
TASSERT(find_named_cb(&state, "/", SHET_EVENT_CREATED_CCB) == NULL);
TASSERT(find_named_cb(&state, "/", SHET_GET_PROP_CCB) == NULL);
TASSERT(find_named_cb(&state, "/", SHET_SET_PROP_CCB) == NULL);
TASSERT(find_named_cb(&state, "/", SHET_CALL_CCB) == NULL);
// ...and that searching doesn't add stuff
TASSERT(state.callbacks == NULL);
// Add a return
shet_deferred_t d1;
d1.type = SHET_RETURN_CB;
d1.data.return_cb.id = 0;
add_deferred(&state, &d1);
// Make sure it is there the hard way
TASSERT(state.callbacks == &d1);
TASSERT(state.callbacks->next == NULL);
// Make sure it is found (but not found by anything else)
TASSERT(find_return_cb(&state, 0) == &d1);
TASSERT(find_return_cb(&state, 1) == NULL);
TASSERT(find_named_cb(&state, "/", SHET_EVENT_CCB) == NULL);
TASSERT(find_named_cb(&state, "/", SHET_EVENT_DELETED_CCB) == NULL);
TASSERT(find_named_cb(&state, "/", SHET_EVENT_CREATED_CCB) == NULL);
TASSERT(find_named_cb(&state, "/", SHET_GET_PROP_CCB) == NULL);
TASSERT(find_named_cb(&state, "/", SHET_SET_PROP_CCB) == NULL);
TASSERT(find_named_cb(&state, "/", SHET_CALL_CCB) == NULL);
// Make sure it can be added a second time without duplicating
add_deferred(&state, &d1);
// Make sure it is there the hard way
TASSERT(state.callbacks == &d1);
TASSERT(state.callbacks->next == NULL);
// Make sure it is found (but not found by anything else)
TASSERT(find_return_cb(&state, 0) == &d1);
TASSERT(find_return_cb(&state, 1) == NULL);
TASSERT(find_named_cb(&state, "/", SHET_EVENT_CCB) == NULL);
TASSERT(find_named_cb(&state, "/", SHET_EVENT_DELETED_CCB) == NULL);
TASSERT(find_named_cb(&state, "/", SHET_EVENT_CREATED_CCB) == NULL);
TASSERT(find_named_cb(&state, "/", SHET_GET_PROP_CCB) == NULL);
TASSERT(find_named_cb(&state, "/", SHET_SET_PROP_CCB) == NULL);
TASSERT(find_named_cb(&state, "/", SHET_CALL_CCB) == NULL);
// Make sure we can remove it
remove_deferred(&state, &d1);
// Make sure it is gone
TASSERT(state.callbacks == NULL);
TASSERT(find_return_cb(&state, 0) == NULL);
// Add more than one item
shet_deferred_t d2;
d2.type = SHET_RETURN_CB;
d2.data.return_cb.id = 1;
add_deferred(&state, &d2);
add_deferred(&state, &d1);
// Make sure they get in the hard way (Note: test will fail if the ordering is
// different, even if the list is technically correct)
TASSERT(state.callbacks == &d1);
TASSERT(state.callbacks->next == &d2);
TASSERT(state.callbacks->next->next == NULL);
// Test both can be found
TASSERT(find_return_cb(&state, 0) == &d1);
TASSERT(find_return_cb(&state, 1) == &d2);
// Ensure both can be re-added to no ill effect
add_deferred(&state, &d1);
add_deferred(&state, &d2);
TASSERT(state.callbacks == &d1);
TASSERT(state.callbacks->next == &d2);
TASSERT(state.callbacks->next->next == NULL);
// Ensure we can remove the tail
remove_deferred(&state, &d2);
TASSERT(state.callbacks == &d1);
TASSERT(state.callbacks->next == NULL);
// ...and add again
add_deferred(&state, &d2);
TASSERT(state.callbacks == &d2);
TASSERT(state.callbacks->next == &d1);
TASSERT(state.callbacks->next->next == NULL);
// Ensure we can remove the head (again, this test will fail if insertion
// order changes!)
remove_deferred(&state, &d2);
TASSERT(state.callbacks == &d1);
TASSERT(state.callbacks->next == NULL);
// Leave us with an empty list again
remove_deferred(&state, &d1);
TASSERT(state.callbacks == NULL);
// Test that we can find event callbacks
d1.type = SHET_EVENT_CB;
d1.data.event_cb.watch_deferred = NULL;
d1.data.event_cb.event_name = "/event/d1";
add_deferred(&state, &d1);
d2.type = SHET_EVENT_CB;
d2.data.event_cb.watch_deferred = NULL;
d2.data.event_cb.event_name = "/event/d2";
add_deferred(&state, &d2);
TASSERT(find_named_cb(&state, "/event/d1", SHET_EVENT_CB) == &d1);
TASSERT(find_named_cb(&state, "/event/d2", SHET_EVENT_CB) == &d2);
// And that non-existant stuff doesn't get found
TASSERT(find_named_cb(&state, "/non_existant", SHET_EVENT_CB) == NULL);
TASSERT(find_named_cb(&state, "/event/d1", SHET_ACTION_CB) == NULL);
TASSERT(find_named_cb(&state, "/event/d1", SHET_PROP_CB) == NULL);
TASSERT(find_named_cb(&state, "/event/d1", SHET_RETURN_CB) == NULL);
// Try actions...
remove_deferred(&state, &d1);
remove_deferred(&state, &d2);
d1.type = SHET_ACTION_CB;
d1.data.action_cb.mkaction_deferred = NULL;
d1.data.action_cb.action_name = "/action/d1";
d2.type = SHET_ACTION_CB;
d2.data.action_cb.mkaction_deferred = NULL;
d2.data.action_cb.action_name = "/action/d2";
add_deferred(&state, &d1);
add_deferred(&state, &d2);
TASSERT(find_named_cb(&state, "/action/d1", SHET_ACTION_CB) == &d1);
TASSERT(find_named_cb(&state, "/action/d2", SHET_ACTION_CB) == &d2);
TASSERT(find_named_cb(&state, "/action/d1", SHET_EVENT_CB) == NULL);
TASSERT(find_named_cb(&state, "/action/d1", SHET_PROP_CB) == NULL);
TASSERT(find_named_cb(&state, "/action/d1", SHET_RETURN_CB) == NULL);
// Try properties...
remove_deferred(&state, &d1);
remove_deferred(&state, &d2);
d1.type = SHET_PROP_CB;
d1.data.prop_cb.mkprop_deferred = NULL;
d1.data.prop_cb.prop_name = "/prop/d1";
d2.type = SHET_PROP_CB;
d2.data.prop_cb.mkprop_deferred = NULL;
d2.data.prop_cb.prop_name = "/prop/d2";
add_deferred(&state, &d1);
add_deferred(&state, &d2);
TASSERT(find_named_cb(&state, "/prop/d1", SHET_PROP_CB) == &d1);
TASSERT(find_named_cb(&state, "/prop/d2", SHET_PROP_CB) == &d2);
TASSERT(find_named_cb(&state, "/prop/d1", SHET_EVENT_CB) == NULL);
TASSERT(find_named_cb(&state, "/prop/d1", SHET_ACTION_CB) == NULL);
TASSERT(find_named_cb(&state, "/prop/d1", SHET_RETURN_CB) == NULL);
return true;
}
////////////////////////////////////////////////////////////////////////////////
// Test general library functions
////////////////////////////////////////////////////////////////////////////////
bool test_shet_state_init(void) {
RESET_TRANSMIT_CB();
shet_state_t state;
shet_state_init(&state, "\"tester\"", transmit_cb, (void *)test_shet_state_init);
RESPOND_TO_REGISTER(&state, 0);
// Make sure a registration command is sent and that the transmit callback
// gets the right data
TASSERT(transmit_count == 1);
TASSERT(transmit_last_user_data == (void *)test_shet_state_init);
TASSERT_JSON_EQUAL_STR_STR(transmit_last_data, "[0, \"register\", \"tester\"]");
return true;
}
bool test_shet_process_line_errors(void) {
RESET_TRANSMIT_CB();
shet_state_t state;
shet_state_init(&state, "\"tester\"", transmit_cb, (void *)test_shet_state_init);
RESPOND_TO_REGISTER(&state, 0);
// Send an empty string
TASSERT(shet_process_line(&state, "", 0) == SHET_PROC_INVALID_JSON);
// Send an invalid piece of JSON
char line1[] = "[not valid : even 1 bit}";
TASSERT(shet_process_line(&state, line1, strlen(line1)) == SHET_PROC_INVALID_JSON);
// Send an command of the wrong type
char line2[] = "{\"wrong\":\"type\"}";
TASSERT(shet_process_line(&state, line2, strlen(line2)) == SHET_PROC_MALFORMED_COMMAND);
// Send an command which is too short
char line3[] = "[0]";
TASSERT(shet_process_line(&state, line3, strlen(line3)) == SHET_PROC_MALFORMED_COMMAND);
// Send an command with a non-string name
char line4[] = "[0, 123]";
TASSERT(shet_process_line(&state, line4, strlen(line4)) == SHET_PROC_MALFORMED_COMMAND);
// Send an command which is unrecognised
char line5[] = "[0, \"thiswillneverexist\"]";
TASSERT(shet_process_line(&state, line5, strlen(line5)) == SHET_PROC_UNKNOWN_COMMAND);
// Send a return with a non-integer ID
char line6[] = "[false, \"return\", 0, none]";
TASSERT(shet_process_line(&state, line6, strlen(line6)) == SHET_PROC_MALFORMED_RETURN);
// Send a return with a non-integer success
char line7[] = "[0, \"return\", false, none]";
TASSERT(shet_process_line(&state, line7, strlen(line7)) == SHET_PROC_MALFORMED_RETURN);
// Send a return with no argument
char line8[] = "[0, \"return\", 0]";
TASSERT(shet_process_line(&state, line8, strlen(line8)) == SHET_PROC_MALFORMED_RETURN);
// Send a return with too-many arguments
char line9[] = "[0, \"return\", 0, 1,2,3]";
TASSERT(shet_process_line(&state, line9, strlen(line9)) == SHET_PROC_MALFORMED_RETURN);
// Send a command without a path
char line10[] = "[0, \"event\"]";
TASSERT(shet_process_line(&state, line10, strlen(line10)) == SHET_PROC_MALFORMED_ARGUMENTS);
// Send an event created with arguments (supposed to be none)
char line11[] = "[0, \"eventcreated\", \"/test\", 1]";
TASSERT(shet_process_line(&state, line11, strlen(line11)) == SHET_PROC_MALFORMED_ARGUMENTS);
// Send an event deleted with arguments (supposed to be none)
char line12[] = "[0, \"eventdeleted\", \"/test\", 1]";
TASSERT(shet_process_line(&state, line12, strlen(line12)) == SHET_PROC_MALFORMED_ARGUMENTS);
// Send a get property with arguments (supposed to be none)
char line13[] = "[0, \"getprop\", \"/test\", 1]";
TASSERT(shet_process_line(&state, line13, strlen(line13)) == SHET_PROC_MALFORMED_ARGUMENTS);
// Send a set property with too-many arguments
char line14[] = "[0, \"setprop\", \"/test\", 1,2,3]";
TASSERT(shet_process_line(&state, line14, strlen(line14)) == SHET_PROC_MALFORMED_ARGUMENTS);
return true;
}
bool test_shet_set_error_callback(void) {
RESET_TRANSMIT_CB();
shet_state_t state;
shet_state_init(&state, "\"tester\"", transmit_cb, NULL);
RESPOND_TO_REGISTER(&state, 0);
// Make sure nothing happens when an unknown (non-successful) return arrives
// without an error callback setup
char line1[] = "[0,\"return\",1,[1,2,3]]";
TASSERT(shet_process_line(&state, line1, strlen(line1)) == SHET_PROC_OK);
// Set up the error callback
callback_result_t result;
result.count = 0;
shet_set_error_callback(&state, callback, &result);
// Make sure unhandled unsuccessful returns get caught
char line2[] = "[1,\"return\",1,[1,2,3]]";
TASSERT(shet_process_line(&state, line2, strlen(line2)) == SHET_PROC_OK);
TASSERT_INT_EQUAL(result.count, 1);
TASSERT_JSON_EQUAL_TOK_STR(result.json, "[1,2,3]");
char line3[] = "[1,\"return\",0,[3,2,1]]";
TASSERT(shet_process_line(&state, line3, strlen(line3)) == SHET_PROC_OK);
TASSERT_INT_EQUAL(result.count, 1);
return true;
}
#define TYPE_EVENT 1
#define TYPE_ACTION 2
#define TYPE_PROPERTY 3
#define TYPE_WATCH 4
bool test_shet_register(void) {
// Paths of the things which will be registered
const char *paths[] = {
"/event/e1",
"/event/e2",
"/action/a1",
"/action/a2",
"/property/p1",
"/property/p2",
"/watch/w1",
"/watch/w2",
};
// Types of the above paths
const int types[] = {TYPE_EVENT,TYPE_EVENT,
TYPE_ACTION,TYPE_ACTION,
TYPE_PROPERTY,TYPE_PROPERTY,
TYPE_WATCH,TYPE_WATCH};
// Number of times the above paths have been registered (or unregistered)
int reg_counts[] = {0,0,0,0,0,0,0,0};
// Number of times the above paths have been registered with the wrong command
int wrong_reg_counts[] = {0,0,0,0,0,0,0,0};
// Return IDs for the most recent reg command for the above paths
int reg_return_ids[] = {0,0,0,0,0,0,0,0};
// Number of times the above paths' registration callbacks have been called
int cb_counts[] = {0,0,0,0,0,0,0,0};
// Number of paths
const int num = sizeof(paths)/sizeof(const char *);
// Deferreds for the "make" parts of the element
shet_deferred_t make_deferreds[num];
// Deferreds for the live part of the element (e.g. event, call, set, get).
shet_deferred_t deferreds[num];
// Event objects for any events
shet_event_t events[num];
// Number of "register" commands received
int register_count = 0;
// Last ID sent with a register command
int register_id = -1;
// Unexpected commands received to transmit
int bad_tx_count = 0;
// Unknown paths requested
int bad_path_count = 0;
// Transmit callback. Simply returns the number of registrations which have
// occurred. Note: this function assumes only register and
// event/action/property/watch calls.
void transmit(const char *data, void *user_data) {
USE(user_data);
jsmn_parser p;
jsmn_init(&p);
jsmntok_t tokens[100];
jsmnerr_t e = jsmn_parse(&p, data, strlen(data),
tokens, 100);
// Die if something strange is passed rather than crashing reading funny
// memory locations...
if (e < 3 || e > 4 || tokens[0].type != JSMN_ARRAY) {
bad_tx_count++;
return;
}
// Is this a register?
if (strncmp("register", data + tokens[2].start, tokens[2].end - tokens[2].start) == 0) {
register_count++;
register_id = atoi(data + tokens[1].start);
} else {
// Check through the paths for a match
int i;
for (i = 0; i < num; i++) {
if (strncmp(paths[i], data + tokens[3].start, tokens[3].end - tokens[3].start) == 0) {
// Check that creationg commands are correct
const char *str = data + tokens[2].start;
size_t len = tokens[2].end - tokens[2].start;
if ((strncmp("mkevent", str, len) == 0 && types[i] == TYPE_EVENT) ||
(strncmp("mkprop", str, len) == 0 && types[i] == TYPE_PROPERTY) ||
(strncmp("mkaction", str, len) == 0 && types[i] == TYPE_ACTION) ||
(strncmp("watch", str, len) == 0 && types[i] == TYPE_WATCH) ||
(strncmp("rmevent", str, len) == 0 && types[i] == TYPE_EVENT) ||
(strncmp("rmprop", str, len) == 0 && types[i] == TYPE_PROPERTY) ||
(strncmp("rmaction", str, len) == 0 && types[i] == TYPE_ACTION) ||
(strncmp("ignore", str, len) == 0 && types[i] == TYPE_WATCH))
reg_counts[i]++;
else {
wrong_reg_counts[i]++;
}
reg_return_ids[i] = atoi(data + tokens[1].start);
break;
}
}
if (i == num)
bad_path_count++;
}
}
// Callback for "make" functions and the like
void make(shet_state_t *state, shet_json_t json, void *user_data) {
USE(state);
USE(json);
size_t i = (const char **)user_data - paths;
cb_counts[i]++;
}
shet_state_t state;
shet_state_init(&state, NULL, transmit, NULL);
TASSERT_INT_EQUAL(register_count, 1);
RESPOND_TO_REGISTER(&state, register_id);
int i;
for (i = 0; i < num; i++) {
TASSERT_INT_EQUAL(reg_counts[i], 0);
TASSERT_INT_EQUAL(wrong_reg_counts[i], 0);
}
TASSERT_INT_EQUAL(bad_path_count, 0);
TASSERT_INT_EQUAL(bad_tx_count, 0);
// Test that re-registering an empty system does no damage
shet_reregister(&state);
RESPOND_TO_REGISTER(&state, register_id);
TASSERT_INT_EQUAL(register_count, 2);
for (i = 0; i < num; i++) {
TASSERT_INT_EQUAL(reg_counts[i], 0);
TASSERT_INT_EQUAL(wrong_reg_counts[i], 0);
}
TASSERT_INT_EQUAL(bad_path_count, 0);
TASSERT_INT_EQUAL(bad_tx_count, 0);
// Test registering all the test paths
for (i = 0; i < num; i++) {
switch (types[i]) {
case TYPE_EVENT:
shet_make_event(&state, paths[i], &(events[i]),
&(make_deferreds[i]), make, NULL, &(paths[i]));
break;
case TYPE_ACTION:
shet_make_action(&state, paths[i],
&(deferreds[i]), NULL, NULL,
&(make_deferreds[i]), make, NULL, &(paths[i]));
break;
case TYPE_PROPERTY:
shet_make_prop(&state, paths[i],
&(deferreds[i]), NULL, NULL, NULL,
&(make_deferreds[i]), make, NULL, &(paths[i]));
break;
case TYPE_WATCH:
shet_watch_event(&state, paths[i],
&(deferreds[i]), NULL, NULL, NULL, NULL,
&(make_deferreds[i]), make, NULL, &(paths[i]));
break;
default:
TASSERT(false);
break;
}
}
TASSERT_INT_EQUAL(register_count, 2);
for (i = 0; i < num; i++) {