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
|
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <glib.h>
#include <wocky/wocky.h>
#include "wocky-test-helper.h"
static void
test_new_from_form (void)
{
WockyStanza *stanza;
WockyNode *node;
WockyDataForm *form;
GError *error = NULL;
stanza = wocky_stanza_build (
WOCKY_STANZA_TYPE_IQ,WOCKY_STANZA_SUB_TYPE_RESULT,
NULL, NULL, NULL);
/* node doesn't contain a form */
form = wocky_data_form_new_from_form (wocky_stanza_get_top_node (stanza),
&error);
g_assert (form == NULL);
g_assert_error (error, WOCKY_DATA_FORM_ERROR,
WOCKY_DATA_FORM_ERROR_NOT_FORM);
g_clear_error (&error);
/* add 'x' node */
node = wocky_node_add_child_ns (wocky_stanza_get_top_node (stanza),
"x", WOCKY_XMPP_NS_DATA);
/* the x node doesn't have a 'type' attribute */
form = wocky_data_form_new_from_form (wocky_stanza_get_top_node (stanza),
&error);
g_assert (form == NULL);
g_assert_error (error, WOCKY_DATA_FORM_ERROR,
WOCKY_DATA_FORM_ERROR_WRONG_TYPE);
g_clear_error (&error);
/* set wrong type */
wocky_node_set_attribute (node, "type", "badger");
form = wocky_data_form_new_from_form (wocky_stanza_get_top_node (stanza),
&error);
g_assert (form == NULL);
g_assert_error (error, WOCKY_DATA_FORM_ERROR,
WOCKY_DATA_FORM_ERROR_WRONG_TYPE);
g_clear_error (&error);
/* set the right type */
wocky_node_set_attribute (node, "type", "form");
form = wocky_data_form_new_from_form (wocky_stanza_get_top_node (stanza),
&error);
g_assert (form != NULL);
g_assert_no_error (error);
g_object_unref (form);
g_object_unref (stanza);
}
static WockyStanza *
create_bot_creation_form_stanza (void)
{
/* This stanza is inspired from Example 2 of XEP-0004: Data Forms */
return wocky_stanza_build (
WOCKY_STANZA_TYPE_IQ,WOCKY_STANZA_SUB_TYPE_RESULT,
NULL, NULL,
'(', "x",
':', WOCKY_XMPP_NS_DATA,
'@', "type", "form",
'(', "title", '$', "My Title", ')',
'(', "instructions", '$', "Badger", ')',
/* hidden field */
'(', "field",
'@', "type", "hidden",
'@', "var", "FORM_TYPE",
'(', "value", '$', "jabber:bot", ')',
')',
/* fixed field */
'(', "field",
'@', "type", "fixed",
'(', "value", '$', "fixed value", ')',
')',
/* text-single field */
'(', "field",
'@', "type", "text-single",
'@', "var", "botname",
'@', "label", "The name of your bot",
')',
/* field with no type. type='' is only a SHOULD; the default is
* text-single */
'(', "field",
'@', "var", "pseudonym",
'@', "label", "Your bot's name at the weekend",
')',
/* text-multi field */
'(', "field",
'@', "type", "text-multi",
'@', "var", "description",
'@', "label", "Helpful description of your bot",
')',
/* boolean field */
'(', "field",
'@', "type", "boolean",
'@', "var", "public",
'@', "label", "Public bot?",
'(', "required", ')',
'(', "value", '$', "false", ')',
')',
/* text-private field */
'(', "field",
'@', "type", "text-private",
'@', "var", "password",
'@', "label", "Password for special access",
')',
/* list-multi field */
'(', "field",
'@', "type", "list-multi",
'@', "var", "features",
'@', "label", "What features will the bot support?",
'(', "option",
'@', "label", "Contests",
'(', "value", '$', "contests", ')',
')',
'(', "option",
'@', "label", "News",
'(', "value", '$', "news", ')',
')',
'(', "option",
'@', "label", "Polls",
'(', "value", '$', "polls", ')',
')',
'(', "option",
'@', "label", "Reminders",
'(', "value", '$', "reminders", ')',
')',
'(', "option",
'@', "label", "Search",
'(', "value", '$', "search", ')',
')',
'(', "value", '$', "news", ')',
'(', "value", '$', "search", ')',
')',
/* list-single field */
'(', "field",
'@', "type", "list-single",
'@', "var", "maxsubs",
'@', "label", "Maximum number of subscribers",
'(', "value", '$', "20", ')',
'(', "option",
'@', "label", "10",
'(', "value", '$', "10", ')',
')',
'(', "option",
'@', "label", "20",
'(', "value", '$', "20", ')',
')',
'(', "option",
'@', "label", "30",
'(', "value", '$', "30", ')',
')',
'(', "option",
'@', "label", "50",
'(', "value", '$', "50", ')',
')',
'(', "option",
'@', "label", "100",
'(', "value", '$', "100", ')',
')',
'(', "option",
'@', "label", "None",
'(', "value", '$', "none", ')',
')',
')',
/* jid-multi field */
'(', "field",
'@', "type", "jid-multi",
'@', "var", "invitelist",
'@', "label", "People to invite",
'(', "desc", '$', "Tell friends", ')',
')',
/* jid-single field */
'(', "field",
'@', "type", "jid-single",
'@', "var", "botjid",
'@', "label", "The JID of the bot",
')',
')', NULL);
}
static void
test_parse_form (void)
{
WockyStanza *stanza;
WockyDataForm *form;
GSList *l;
/* used to check that fields are stored in the right order */
WockyDataFormField expected_types[] = {
{ WOCKY_DATA_FORM_FIELD_TYPE_HIDDEN, "FORM_TYPE",
NULL, NULL, FALSE, NULL, NULL, NULL },
{ WOCKY_DATA_FORM_FIELD_TYPE_FIXED, NULL,
NULL, NULL, FALSE, NULL, NULL, NULL },
{ WOCKY_DATA_FORM_FIELD_TYPE_TEXT_SINGLE, "botname",
"The name of your bot", NULL, FALSE, NULL, NULL, NULL },
{ WOCKY_DATA_FORM_FIELD_TYPE_TEXT_SINGLE, "pseudonym",
"Your bot's name at the weekend", NULL, FALSE, NULL, NULL, NULL },
{ WOCKY_DATA_FORM_FIELD_TYPE_TEXT_MULTI, "description",
"Helpful description of your bot", NULL, FALSE, NULL, NULL, NULL },
{ WOCKY_DATA_FORM_FIELD_TYPE_BOOLEAN, "public",
"Public bot?", NULL, TRUE, NULL, NULL, NULL },
{ WOCKY_DATA_FORM_FIELD_TYPE_TEXT_PRIVATE, "password",
"Password for special access", NULL, FALSE, NULL, NULL, NULL },
{ WOCKY_DATA_FORM_FIELD_TYPE_LIST_MULTI, "features",
"What features will the bot support?", NULL, FALSE, NULL, NULL, NULL },
{ WOCKY_DATA_FORM_FIELD_TYPE_LIST_SINGLE, "maxsubs",
"Maximum number of subscribers", NULL, FALSE, NULL, NULL, NULL },
{ WOCKY_DATA_FORM_FIELD_TYPE_JID_MULTI, "invitelist",
"People to invite", "Tell friends", FALSE, NULL, NULL, NULL },
{ WOCKY_DATA_FORM_FIELD_TYPE_JID_SINGLE, "botjid",
"The JID of the bot", NULL, FALSE, NULL, NULL, NULL },
};
guint i;
WockyDataFormField *field;
GStrv strv;
WockyDataFormFieldOption features_options[] = {
{ "Contests", "contests" },
{ "News", "news" },
{ "Polls", "polls" },
{ "Reminders", "reminders" },
{ "Search", "search" },
};
WockyDataFormFieldOption maxsubs_options[] = {
{ "10", "10" },
{ "20", "20" },
{ "30", "30" },
{ "50", "50" },
{ "100", "100" },
{ "None", "none" },
};
stanza = create_bot_creation_form_stanza ();
form = wocky_data_form_new_from_form (wocky_stanza_get_top_node (stanza),
NULL);
g_assert (form != NULL);
g_object_unref (stanza);
g_assert_cmpstr (wocky_data_form_get_title (form), ==, "My Title");
g_assert_cmpstr (wocky_data_form_get_instructions (form), ==, "Badger");
g_assert_cmpuint (g_slist_length (form->fields_list), ==, 11);
for (l = form->fields_list, i = 0; l != NULL; l = g_slist_next (l), i++)
{
field = l->data;
g_assert (field != NULL);
g_assert_cmpuint (field->type, ==, expected_types[i].type);
g_assert_cmpstr (field->var, ==, expected_types[i].var);
g_assert_cmpstr (field->label, ==, expected_types[i].label);
g_assert_cmpstr (field->desc, ==, expected_types[i].desc);
g_assert (field->required == expected_types[i].required);
g_assert (field->value == NULL);
}
g_assert_cmpuint (g_hash_table_size (form->fields), ==, 10);
/* check hidden field */
field = g_hash_table_lookup (form->fields, "FORM_TYPE");
g_assert (field != NULL);
g_assert (G_VALUE_TYPE (field->default_value) == G_TYPE_STRING);
g_assert_cmpstr (g_value_get_string (field->default_value), ==, "jabber:bot");
g_assert (field->options == NULL);
/* check text-single field */
field = g_hash_table_lookup (form->fields, "botname");
g_assert (field != NULL);
g_assert (field->default_value == NULL);
g_assert (field->options == NULL);
/* check implicitly text-single field */
field = g_hash_table_lookup (form->fields, "pseudonym");
g_assert (field != NULL);
g_assert (field->default_value == NULL);
g_assert (field->options == NULL);
/* check text-multi field */
field = g_hash_table_lookup (form->fields, "description");
g_assert (field != NULL);
g_assert (field->default_value == NULL);
g_assert (field->options == NULL);
/* check boolean field */
field = g_hash_table_lookup (form->fields, "public");
g_assert (field != NULL);
g_assert (G_VALUE_TYPE (field->default_value) == G_TYPE_BOOLEAN);
g_assert (!g_value_get_boolean (field->default_value));
g_assert (field->options == NULL);
/* check text-private field */
field = g_hash_table_lookup (form->fields, "password");
g_assert (field != NULL);
g_assert (field->default_value == NULL);
g_assert (field->options == NULL);
/* check list-multi field */
field = g_hash_table_lookup (form->fields, "features");
g_assert (field != NULL);
g_assert (G_VALUE_TYPE (field->default_value) == G_TYPE_STRV);
strv = g_value_get_boxed (field->default_value);
g_assert_cmpuint (g_strv_length (strv), ==, 2);
g_assert_cmpstr (strv[0], ==, "news");
g_assert_cmpstr (strv[1], ==, "search");
g_assert_cmpuint (g_slist_length (field->options), ==, 5);
for (l = field->options, i = 0; l != NULL; l = g_slist_next (l), i++)
{
WockyDataFormFieldOption *option = l->data;
g_assert_cmpstr (option->value, ==, features_options[i].value);
g_assert_cmpstr (option->label, ==, features_options[i].label);
}
/* check list-single field */
field = g_hash_table_lookup (form->fields, "maxsubs");
g_assert (field != NULL);
g_assert (G_VALUE_TYPE (field->default_value) == G_TYPE_STRING);
g_assert_cmpstr (g_value_get_string (field->default_value), ==, "20");
g_assert_cmpuint (g_slist_length (field->options), ==, 6);
for (l = field->options, i = 0; l != NULL; l = g_slist_next (l), i++)
{
WockyDataFormFieldOption *option = l->data;
g_assert_cmpstr (option->value, ==, maxsubs_options[i].value);
g_assert_cmpstr (option->label, ==, maxsubs_options[i].label);
}
/* check jid-multi field */
field = g_hash_table_lookup (form->fields, "invitelist");
g_assert (field != NULL);
g_assert (field->default_value == NULL);
g_assert (field->options == NULL);
/* check boolean field */
field = g_hash_table_lookup (form->fields, "botjid");
g_assert (field != NULL);
g_assert (field->default_value == NULL);
g_assert (field->options == NULL);
g_object_unref (form);
}
static void
test_raw_value_contents (void)
{
WockyStanza *stanza;
WockyDataForm *form;
const gchar *description[] = { "Badger", "Mushroom", "Snake", NULL };
gboolean set_succeeded;
/* Create fields without WockyNode to trigger this bug:
* https://bugs.freedesktop.org/show_bug.cgi?id=43584
*/
form = g_object_new (WOCKY_TYPE_DATA_FORM, NULL);
g_assert (form != NULL);
/* set text-single field */
set_succeeded = wocky_data_form_set_string (form, "botname",
"The Jabber Google Bot", FALSE);
g_assert (!set_succeeded);
set_succeeded = wocky_data_form_set_string (form, "botname",
"The Jabber Google Bot", TRUE);
g_assert (set_succeeded);
/* set text-multi field */
set_succeeded = wocky_data_form_set_strv (form, "description",
description, FALSE);
g_assert (!set_succeeded);
set_succeeded = wocky_data_form_set_strv (form, "description",
description, TRUE);
g_assert (set_succeeded);
/* set boolean field */
set_succeeded = wocky_data_form_set_boolean (form, "public", FALSE, FALSE);
g_assert (!set_succeeded);
set_succeeded = wocky_data_form_set_boolean (form, "public", FALSE, TRUE);
g_assert (set_succeeded);
stanza = wocky_stanza_build (
WOCKY_STANZA_TYPE_IQ, WOCKY_STANZA_SUB_TYPE_SET,
NULL, NULL, NULL);
wocky_data_form_add_to_node (form, wocky_stanza_get_top_node (stanza));
g_object_unref (stanza);
g_object_unref (form);
}
static void
test_submit (void)
{
WockyStanza *stanza;
WockyDataForm *form;
WockyNode *x;
GSList *l;
const gchar *description[] = { "Badger", "Mushroom", "Snake", NULL };
const gchar *features[] = { "news", "search", NULL };
const gchar *invitees[] = { "juliet@example.org",
"romeo@example.org", NULL };
gboolean set_succeeded;
stanza = create_bot_creation_form_stanza ();
form = wocky_data_form_new_from_form (wocky_stanza_get_top_node (stanza),
NULL);
g_assert (form != NULL);
g_object_unref (stanza);
/* set text-single field */
set_succeeded = wocky_data_form_set_string (form, "botname",
"The Jabber Google Bot", FALSE);
g_assert (set_succeeded);
/* set text-multi field */
set_succeeded = wocky_data_form_set_strv (form, "description",
description, FALSE);
g_assert (set_succeeded);
/* set boolean field */
set_succeeded = wocky_data_form_set_boolean (form, "public", FALSE, FALSE);
g_assert (set_succeeded);
/* set text-private field */
set_succeeded = wocky_data_form_set_string (form, "password",
"S3cr1t", FALSE);
g_assert (set_succeeded);
/* set list-multi field */
set_succeeded = wocky_data_form_set_strv (form, "features",
features, FALSE);
g_assert (set_succeeded);
/* set list-single field */
set_succeeded = wocky_data_form_set_string (form, "maxsubs", "20", FALSE);
g_assert (set_succeeded);
/* set jid-multi field */
set_succeeded = wocky_data_form_set_strv (form, "invitelist", invitees,
FALSE);
g_assert (set_succeeded);
/* set jid-single field */
set_succeeded = wocky_data_form_set_string (form, "botjid",
"bobot@example.org", FALSE);
g_assert (set_succeeded);
stanza = wocky_stanza_build (
WOCKY_STANZA_TYPE_IQ, WOCKY_STANZA_SUB_TYPE_SET,
NULL, NULL, NULL);
wocky_data_form_submit (form, wocky_stanza_get_top_node (stanza));
x = wocky_node_get_child_ns (wocky_stanza_get_top_node (stanza),
"x", WOCKY_XMPP_NS_DATA);
g_assert (x != NULL);
g_assert_cmpstr (wocky_node_get_attribute (x, "type"), ==, "submit");
for (l = x->children; l != NULL; l = g_slist_next (l))
{
WockyNode *v, *node = l->data;
const gchar *var, *type, *value = NULL;
g_assert_cmpstr (node->name, ==, "field");
var = wocky_node_get_attribute (node, "var");
g_assert (var != NULL);
type = wocky_node_get_attribute (node, "type");
v = wocky_node_get_child (node, "value");
if (v != NULL)
value = v->content;
if (!wocky_strdiff (var, "FORM_TYPE"))
{
g_assert_cmpstr (type, ==, "hidden");
g_assert_cmpstr (value, ==, "jabber:bot");
}
else if (!wocky_strdiff (var, "botname"))
{
g_assert_cmpstr (type, ==, "text-single");
g_assert_cmpstr (value, ==, "The Jabber Google Bot");
}
else if (!wocky_strdiff (var, "description"))
{
GSList *m;
gboolean badger = FALSE, mushroom = FALSE, snake = FALSE;
g_assert_cmpstr (type, ==, "text-multi");
for (m = node->children; m != NULL; m = g_slist_next (m))
{
WockyNode *tmp = m->data;
g_assert_cmpstr (tmp->name, ==, "value");
if (!wocky_strdiff (tmp->content, "Badger"))
badger = TRUE;
else if (!wocky_strdiff (tmp->content, "Mushroom"))
mushroom = TRUE;
else if (!wocky_strdiff (tmp->content, "Snake"))
snake = TRUE;
else
g_assert_not_reached ();
}
g_assert (badger && mushroom && snake);
}
else if (!wocky_strdiff (var, "public"))
{
g_assert_cmpstr (type, ==, "boolean");
g_assert_cmpstr (value, ==, "0");
}
else if (!wocky_strdiff (var, "password"))
{
g_assert_cmpstr (type, ==, "text-private");
g_assert_cmpstr (value, ==, "S3cr1t");
}
else if (!wocky_strdiff (var, "features"))
{
GSList *m;
gboolean news = FALSE, search = FALSE;
g_assert_cmpstr (type, ==, "list-multi");
for (m = node->children; m != NULL; m = g_slist_next (m))
{
WockyNode *tmp = m->data;
g_assert_cmpstr (tmp->name, ==, "value");
if (!wocky_strdiff (tmp->content, "news"))
news = TRUE;
else if (!wocky_strdiff (tmp->content, "search"))
search = TRUE;
else
g_assert_not_reached ();
}
g_assert (news && search);
}
else if (!wocky_strdiff (var, "maxsubs"))
{
g_assert_cmpstr (type, ==, "list-single");
g_assert_cmpstr (value, ==, "20");
}
else if (!wocky_strdiff (var, "invitelist"))
{
GSList *m;
gboolean juliet = FALSE, romeo = FALSE;
g_assert_cmpstr (type, ==, "jid-multi");
for (m = node->children; m != NULL; m = g_slist_next (m))
{
WockyNode *tmp = m->data;
g_assert_cmpstr (tmp->name, ==, "value");
if (!wocky_strdiff (tmp->content, "juliet@example.org"))
juliet = TRUE;
else if (!wocky_strdiff (tmp->content, "romeo@example.org"))
romeo = TRUE;
else
g_assert_not_reached ();
}
g_assert (juliet && romeo);
}
else if (!wocky_strdiff (var, "botjid"))
{
g_assert_cmpstr (type, ==, "jid-single");
g_assert_cmpstr (value, ==, "bobot@example.org");
}
else
g_assert_not_reached ();
}
g_object_unref (stanza);
g_object_unref (form);
}
/* Test creating and submitting a form response blindly, without first asking
* the server for the form fields.
*/
static void
test_submit_blindly (void)
{
WockyDataForm *form = g_object_new (WOCKY_TYPE_DATA_FORM, NULL);
const gchar * const the_xx[] = { "Romy", "Oliver", "Jamie", NULL };
gboolean succeeded;
WockyStanza *stanza, *expected;
/* We didn't actually parse a form, so it doesn't have any pre-defined
* fields. Thus, the setters should all fail if we don't tell them to create
* the fields if missing.
*/
succeeded = wocky_data_form_set_string (form, "band-name", "The XX", FALSE);
g_assert (!succeeded);
succeeded = wocky_data_form_set_strv (form, "band-members", the_xx, FALSE);
g_assert (!succeeded);
succeeded = wocky_data_form_set_boolean (form, "is-meh", TRUE, FALSE);
g_assert (!succeeded);
g_assert (form->fields_list == NULL);
g_assert_cmpuint (0, ==, g_hash_table_size (form->fields));
/* Since the form doesn't have a FORM_TYPE yet, we should be able to set it.
*/
succeeded = wocky_data_form_set_type (form, "http://example.com/band-info");
g_assert (succeeded);
/* But now that it does have one, we shouldn't be able to change it. */
succeeded = wocky_data_form_set_type (form, "stoats");
g_assert (!succeeded);
/* If we forcibly create the fields we care about, setting them should
* succeed, and they should show up when we submit the form!
*/
succeeded = wocky_data_form_set_string (form, "band-name", "The XX", TRUE);
g_assert (succeeded);
succeeded = wocky_data_form_set_strv (form, "band-members", the_xx, TRUE);
g_assert (succeeded);
succeeded = wocky_data_form_set_boolean (form, "is-meh", TRUE, TRUE);
g_assert (succeeded);
stanza = wocky_stanza_build (WOCKY_STANZA_TYPE_IQ,
WOCKY_STANZA_SUB_TYPE_SET, NULL, NULL,
NULL);
wocky_data_form_submit (form, wocky_stanza_get_top_node (stanza));
expected = wocky_stanza_build (WOCKY_STANZA_TYPE_IQ,
WOCKY_STANZA_SUB_TYPE_SET, NULL, NULL,
'(', "x",
':', WOCKY_XMPP_NS_DATA,
'@', "type", "submit",
'(', "field",
'@', "type", "hidden",
'@', "var", "FORM_TYPE",
'(', "value",
'$', "http://example.com/band-info",
')',
')',
'(', "field",
'@', "var", "band-name",
'(', "value",
'$', "The XX",
')',
')',
'(', "field",
'@', "var", "band-members",
'(', "value",
'$', "Romy",
')',
'(', "value",
'$', "Oliver",
')',
'(', "value",
'$', "Jamie",
')',
')',
'(', "field",
'@', "var", "is-meh",
'(', "value",
'$', "1",
')',
')',
')',
NULL);
test_assert_stanzas_equal_no_id (expected, stanza);
g_object_unref (expected);
g_object_unref (stanza);
g_object_unref (form);
}
static WockyStanza *
create_search_form_stanza (void)
{
/* This stanza is inspired from Example 6 of XEP-0004: Data Forms */
return wocky_stanza_build (
WOCKY_STANZA_TYPE_IQ,WOCKY_STANZA_SUB_TYPE_RESULT,
NULL, NULL,
'(', "x",
':', WOCKY_XMPP_NS_DATA,
'@', "type", "form",
'(', "title", '$', "My Title", ')',
'(', "instructions", '$', "Badger", ')',
'(', "field",
'@', "type", "text-single",
'@', "var", "search_request",
')',
')', NULL);
}
static void
test_parse_multi_result (void)
{
WockyStanza *stanza;
WockyDataForm *form;
GSList *l;
gboolean item1 = FALSE, item2 = FALSE;
stanza = create_search_form_stanza ();
form = wocky_data_form_new_from_form (wocky_stanza_get_top_node (stanza),
NULL);
g_assert (form != NULL);
g_object_unref (stanza);
/* create the result stanza */
stanza = wocky_stanza_build (
WOCKY_STANZA_TYPE_IQ, WOCKY_STANZA_SUB_TYPE_RESULT,
NULL, NULL,
'(', "x",
':', WOCKY_XMPP_NS_DATA,
'@', "type", "result",
'(', "title", '$', "Search result", ')',
'(', "reported",
'(', "field",
'@', "var", "name",
'@', "type", "text-single",
')',
'(', "field",
'@', "var", "url",
'@', "type", "text-single",
')',
')',
/* first item */
'(', "item",
'(', "field",
'@', "var", "name",
'(', "value", '$', "name1", ')',
')',
'(', "field",
'@', "var", "url",
'(', "value", '$', "url1", ')',
')',
')',
/* second item */
'(', "item",
'(', "field",
'@', "var", "name",
'(', "value", '$', "name2", ')',
')',
'(', "field",
'@', "var", "url",
'(', "value", '$', "url2", ')',
')',
')',
')',
NULL);
g_assert (wocky_data_form_parse_result (form,
wocky_stanza_get_top_node (stanza), NULL));
g_object_unref (stanza);
g_assert_cmpuint (g_slist_length (form->results), ==, 2);
for (l = form->results; l != NULL; l = g_slist_next (l))
{
GSList *result = l->data, *m;
gboolean name = FALSE, url = FALSE;
for (m = result; m != NULL; m = g_slist_next (m))
{
WockyDataFormField *field = m->data;
if (!wocky_strdiff (field->var, "name"))
{
if (!wocky_strdiff (g_value_get_string (field->value), "name1"))
item1 = TRUE;
else if (!wocky_strdiff (g_value_get_string (field->value),
"name2"))
item2 = TRUE;
else
g_assert_not_reached ();
name = TRUE;
}
else if (!wocky_strdiff (field->var, "url"))
{
if (item2)
g_assert_cmpstr (g_value_get_string (field->value), ==, "url2");
else if (item1)
g_assert_cmpstr (g_value_get_string (field->value), ==, "url1");
else
g_assert_not_reached ();
url = TRUE;
}
else
g_assert_not_reached ();
}
g_assert (name && url);
}
g_assert (item1 && item2);
g_object_unref (form);
}
static void
test_parse_single_result (void)
{
WockyStanza *stanza;
WockyDataForm *form;
GSList *result, *l;
gboolean form_type = FALSE, botname = FALSE;
stanza = create_bot_creation_form_stanza ();
form = wocky_data_form_new_from_form (wocky_stanza_get_top_node (stanza),
NULL);
g_assert (form != NULL);
g_object_unref (stanza);
/* create the result stanza */
stanza = wocky_stanza_build (
WOCKY_STANZA_TYPE_IQ, WOCKY_STANZA_SUB_TYPE_RESULT,
NULL, NULL,
'(', "x",
':', WOCKY_XMPP_NS_DATA,
'@', "type", "result",
/* hidden field */
'(', "field",
'@', "type", "hidden",
'@', "var", "FORM_TYPE",
'(', "value", '$', "jabber:bot", ')',
')',
/* text-single field */
'(', "field",
'@', "type", "text-single",
'@', "var", "botname",
'(', "value", '$', "The Bot", ')',
')',
')',
NULL);
g_assert (wocky_data_form_parse_result (form,
wocky_stanza_get_top_node (stanza), NULL));
g_object_unref (stanza);
g_assert_cmpuint (g_slist_length (form->results), ==, 1);
result = form->results->data;
g_assert_cmpuint (g_slist_length (result), ==, 2);
for (l = result; l != NULL; l = g_slist_next (l))
{
WockyDataFormField *field = l->data;
if (!wocky_strdiff (field->var, "FORM_TYPE"))
{
g_assert_cmpstr (g_value_get_string (field->value), ==, "jabber:bot");
g_assert (field->type == WOCKY_DATA_FORM_FIELD_TYPE_HIDDEN);
form_type = TRUE;
}
else if (!wocky_strdiff (field->var, "botname"))
{
g_assert_cmpstr (g_value_get_string (field->value), ==, "The Bot");
g_assert (field->type == WOCKY_DATA_FORM_FIELD_TYPE_TEXT_SINGLE);
botname = TRUE;
}
else
g_assert_not_reached ();
}
g_assert (form_type && botname);
g_object_unref (form);
}
int
main (int argc, char **argv)
{
int result;
test_init (argc, argv);
g_test_add_func ("/data-form/instantiation", test_new_from_form);
g_test_add_func ("/data-form/parse-form", test_parse_form);
g_test_add_func ("/data-form/raw_value_contents", test_raw_value_contents);
g_test_add_func ("/data-form/submit", test_submit);
g_test_add_func ("/data-form/submit-blindly", test_submit_blindly);
g_test_add_func ("/data-form/parse-multi-result", test_parse_multi_result);
g_test_add_func ("/data-form/parse-single-result", test_parse_single_result);
result = g_test_run ();
test_deinit ();
return result;
}
|