mirror of
https://github.com/profanity-im/profanity.git
synced 2024-10-06 20:03:46 -04:00
357 lines
10 KiB
C
357 lines
10 KiB
C
|
#include <stdarg.h>
|
||
|
#include <string.h>
|
||
|
#include <stddef.h>
|
||
|
#include <setjmp.h>
|
||
|
#include <cmocka.h>
|
||
|
#include <stdlib.h>
|
||
|
|
||
|
#include "xmpp/form.h"
|
||
|
#include "xmpp/mock_xmpp.h"
|
||
|
|
||
|
xmpp_ctx_t* connection_get_ctx(void)
|
||
|
{
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
|
static DataForm*
|
||
|
_new_form(void)
|
||
|
{
|
||
|
DataForm *form = malloc(sizeof(DataForm));
|
||
|
form->type = NULL;
|
||
|
form->title = NULL;
|
||
|
form->instructions = NULL;
|
||
|
form->fields = NULL;
|
||
|
form->var_to_tag = g_hash_table_new_full(g_str_hash, g_str_equal, free, free);
|
||
|
form->tag_to_var = g_hash_table_new_full(g_str_hash, g_str_equal, free, free);
|
||
|
form->tag_ac = NULL;
|
||
|
|
||
|
return form;
|
||
|
}
|
||
|
|
||
|
static FormField*
|
||
|
_new_field(void)
|
||
|
{
|
||
|
FormField *field = malloc(sizeof(FormField));
|
||
|
field->label = NULL;
|
||
|
field->type = NULL;
|
||
|
field->description = NULL;
|
||
|
field->required = FALSE;
|
||
|
field->options = NULL;
|
||
|
field->var = NULL;
|
||
|
field->values = NULL;
|
||
|
|
||
|
return field;
|
||
|
|
||
|
}
|
||
|
|
||
|
void get_form_type_field_returns_null_no_fields(void **state)
|
||
|
{
|
||
|
form_init_module();
|
||
|
|
||
|
DataForm *form = _new_form();
|
||
|
|
||
|
char *result = form_get_form_type_field(form);
|
||
|
|
||
|
assert_null(result);
|
||
|
|
||
|
form_destroy(form);
|
||
|
}
|
||
|
|
||
|
void get_form_type_field_returns_null_when_not_present(void **state)
|
||
|
{
|
||
|
form_init_module();
|
||
|
|
||
|
DataForm *form = _new_form();
|
||
|
FormField *field = _new_field();
|
||
|
field->var = strdup("var1");
|
||
|
field->values = g_slist_append(field->values, strdup("value1"));
|
||
|
form->fields = g_slist_append(form->fields, field);
|
||
|
|
||
|
char *result = form_get_form_type_field(form);
|
||
|
|
||
|
assert_null(result);
|
||
|
|
||
|
form_destroy(form);
|
||
|
}
|
||
|
|
||
|
void get_form_type_field_returns_value_when_present(void **state)
|
||
|
{
|
||
|
form_init_module();
|
||
|
|
||
|
DataForm *form = _new_form();
|
||
|
|
||
|
FormField *field1 = _new_field();
|
||
|
field1->var = strdup("var1");
|
||
|
field1->values = g_slist_append(field1->values, strdup("value1"));
|
||
|
form->fields = g_slist_append(form->fields, field1);
|
||
|
|
||
|
FormField *field2 = _new_field();
|
||
|
field2->var = strdup("FORM_TYPE");
|
||
|
field2->values = g_slist_append(field2->values, strdup("value2"));
|
||
|
form->fields = g_slist_append(form->fields, field2);
|
||
|
|
||
|
FormField *field3 = _new_field();
|
||
|
field3->var = strdup("var3");
|
||
|
field3->values = g_slist_append(field3->values, strdup("value3"));
|
||
|
form->fields = g_slist_append(form->fields, field3);
|
||
|
|
||
|
char *result = form_get_form_type_field(form);
|
||
|
|
||
|
assert_string_equal(result, "value2");
|
||
|
|
||
|
form_destroy(form);
|
||
|
}
|
||
|
|
||
|
void get_field_type_returns_unknown_when_no_fields(void **state)
|
||
|
{
|
||
|
form_init_module();
|
||
|
|
||
|
DataForm *form = _new_form();
|
||
|
|
||
|
form_field_type_t result = form_get_field_type(form, "tag");
|
||
|
|
||
|
assert_int_equal(result, FIELD_UNKNOWN);
|
||
|
|
||
|
form_destroy(form);
|
||
|
}
|
||
|
|
||
|
void get_field_type_returns_correct_type(void **state)
|
||
|
{
|
||
|
form_init_module();
|
||
|
|
||
|
DataForm *form = _new_form();
|
||
|
g_hash_table_insert(form->tag_to_var, strdup("tag1"), strdup("var1"));
|
||
|
g_hash_table_insert(form->tag_to_var, strdup("tag2"), strdup("var2"));
|
||
|
|
||
|
FormField *field1 = _new_field();
|
||
|
field1->var = strdup("var1");
|
||
|
field1->type_t = FIELD_TEXT_SINGLE;
|
||
|
field1->values = g_slist_append(field1->values, strdup("value1"));
|
||
|
form->fields = g_slist_append(form->fields, field1);
|
||
|
|
||
|
FormField *field2 = _new_field();
|
||
|
field2->var = strdup("var2");
|
||
|
field2->type_t = FIELD_TEXT_MULTI;
|
||
|
field2->values = g_slist_append(field2->values, strdup("value2"));
|
||
|
form->fields = g_slist_append(form->fields, field2);
|
||
|
|
||
|
form_field_type_t result = form_get_field_type(form, "tag2");
|
||
|
|
||
|
assert_int_equal(result, FIELD_TEXT_MULTI);
|
||
|
|
||
|
form_destroy(form);
|
||
|
}
|
||
|
|
||
|
void set_value_adds_when_none(void **state)
|
||
|
{
|
||
|
form_init_module();
|
||
|
|
||
|
DataForm *form = _new_form();
|
||
|
g_hash_table_insert(form->tag_to_var, strdup("tag1"), strdup("var1"));
|
||
|
g_hash_table_insert(form->tag_to_var, strdup("tag2"), strdup("var2"));
|
||
|
|
||
|
FormField *field1 = _new_field();
|
||
|
field1->var = strdup("var1");
|
||
|
field1->type_t = FIELD_TEXT_SINGLE;
|
||
|
field1->values = g_slist_append(field1->values, strdup("value1"));
|
||
|
form->fields = g_slist_append(form->fields, field1);
|
||
|
|
||
|
FormField *field2 = _new_field();
|
||
|
field2->var = strdup("var2");
|
||
|
field2->type_t = FIELD_LIST_SINGLE;
|
||
|
form->fields = g_slist_append(form->fields, field2);
|
||
|
|
||
|
form_set_value(form, "tag2", "a new value");
|
||
|
|
||
|
int length = 0;
|
||
|
char *value = NULL;
|
||
|
GSList *curr_field = form->fields;
|
||
|
while (curr_field != NULL) {
|
||
|
FormField *field = curr_field->data;
|
||
|
if (g_strcmp0(field->var, "var2") == 0) {
|
||
|
length = g_slist_length(field->values);
|
||
|
value = field->values->data;
|
||
|
break;
|
||
|
}
|
||
|
curr_field = g_slist_next(curr_field);
|
||
|
}
|
||
|
|
||
|
assert_int_equal(length, 1);
|
||
|
assert_string_equal(value, "a new value");
|
||
|
|
||
|
form_destroy(form);
|
||
|
}
|
||
|
|
||
|
void set_value_updates_when_one(void **state)
|
||
|
{
|
||
|
form_init_module();
|
||
|
|
||
|
DataForm *form = _new_form();
|
||
|
g_hash_table_insert(form->tag_to_var, strdup("tag1"), strdup("var1"));
|
||
|
g_hash_table_insert(form->tag_to_var, strdup("tag2"), strdup("var2"));
|
||
|
|
||
|
FormField *field1 = _new_field();
|
||
|
field1->var = strdup("var1");
|
||
|
field1->type_t = FIELD_TEXT_SINGLE;
|
||
|
form->fields = g_slist_append(form->fields, field1);
|
||
|
|
||
|
FormField *field2 = _new_field();
|
||
|
field2->var = strdup("var2");
|
||
|
field2->type_t = FIELD_LIST_SINGLE;
|
||
|
field2->values = g_slist_append(field2->values, strdup("value2"));
|
||
|
form->fields = g_slist_append(form->fields, field2);
|
||
|
|
||
|
form_set_value(form, "tag2", "a new value");
|
||
|
|
||
|
int length = 0;
|
||
|
char *value = NULL;
|
||
|
GSList *curr_field = form->fields;
|
||
|
while (curr_field != NULL) {
|
||
|
FormField *field = curr_field->data;
|
||
|
if (g_strcmp0(field->var, "var2") == 0) {
|
||
|
length = g_slist_length(field->values);
|
||
|
value = field->values->data;
|
||
|
break;
|
||
|
}
|
||
|
curr_field = g_slist_next(curr_field);
|
||
|
}
|
||
|
|
||
|
assert_int_equal(length, 1);
|
||
|
assert_string_equal(value, "a new value");
|
||
|
|
||
|
form_destroy(form);
|
||
|
}
|
||
|
|
||
|
void add_unique_value_adds_when_none(void **state)
|
||
|
{
|
||
|
form_init_module();
|
||
|
|
||
|
DataForm *form = _new_form();
|
||
|
g_hash_table_insert(form->tag_to_var, strdup("tag1"), strdup("var1"));
|
||
|
g_hash_table_insert(form->tag_to_var, strdup("tag2"), strdup("var2"));
|
||
|
|
||
|
FormField *field1 = _new_field();
|
||
|
field1->var = strdup("var1");
|
||
|
field1->type_t = FIELD_JID_MULTI;
|
||
|
form->fields = g_slist_append(form->fields, field1);
|
||
|
|
||
|
FormField *field2 = _new_field();
|
||
|
field2->var = strdup("var2");
|
||
|
field2->type_t = FIELD_LIST_SINGLE;
|
||
|
field2->values = g_slist_append(field2->values, strdup("value2"));
|
||
|
form->fields = g_slist_append(form->fields, field2);
|
||
|
|
||
|
gboolean ret = form_add_unique_value(form, "tag1", "me@server.com");
|
||
|
|
||
|
int length = 0;
|
||
|
char *value = NULL;
|
||
|
GSList *curr_field = form->fields;
|
||
|
while (curr_field != NULL) {
|
||
|
FormField *field = curr_field->data;
|
||
|
if (g_strcmp0(field->var, "var1") == 0) {
|
||
|
length = g_slist_length(field->values);
|
||
|
value = field->values->data;
|
||
|
break;
|
||
|
}
|
||
|
curr_field = g_slist_next(curr_field);
|
||
|
}
|
||
|
|
||
|
assert_true(ret);
|
||
|
assert_int_equal(length, 1);
|
||
|
assert_string_equal(value, "me@server.com");
|
||
|
|
||
|
form_destroy(form);
|
||
|
}
|
||
|
|
||
|
void add_unique_value_does_nothing_when_exists(void **state)
|
||
|
{
|
||
|
form_init_module();
|
||
|
|
||
|
DataForm *form = _new_form();
|
||
|
g_hash_table_insert(form->tag_to_var, strdup("tag1"), strdup("var1"));
|
||
|
g_hash_table_insert(form->tag_to_var, strdup("tag2"), strdup("var2"));
|
||
|
|
||
|
FormField *field1 = _new_field();
|
||
|
field1->var = strdup("var1");
|
||
|
field1->type_t = FIELD_JID_MULTI;
|
||
|
field1->values = g_slist_append(field1->values, strdup("me@server.com"));
|
||
|
form->fields = g_slist_append(form->fields, field1);
|
||
|
|
||
|
FormField *field2 = _new_field();
|
||
|
field2->var = strdup("var2");
|
||
|
field2->type_t = FIELD_LIST_SINGLE;
|
||
|
field2->values = g_slist_append(field2->values, strdup("value2"));
|
||
|
form->fields = g_slist_append(form->fields, field2);
|
||
|
|
||
|
gboolean ret = form_add_unique_value(form, "tag1", "me@server.com");
|
||
|
|
||
|
int length = 0;
|
||
|
char *value = NULL;
|
||
|
GSList *curr_field = form->fields;
|
||
|
while (curr_field != NULL) {
|
||
|
FormField *field = curr_field->data;
|
||
|
if (g_strcmp0(field->var, "var1") == 0) {
|
||
|
length = g_slist_length(field->values);
|
||
|
value = field->values->data;
|
||
|
break;
|
||
|
}
|
||
|
curr_field = g_slist_next(curr_field);
|
||
|
}
|
||
|
|
||
|
assert_false(ret);
|
||
|
assert_int_equal(length, 1);
|
||
|
assert_string_equal(value, "me@server.com");
|
||
|
|
||
|
form_destroy(form);
|
||
|
}
|
||
|
|
||
|
void add_unique_value_adds_when_doesnt_exist(void **state)
|
||
|
{
|
||
|
form_init_module();
|
||
|
|
||
|
DataForm *form = _new_form();
|
||
|
g_hash_table_insert(form->tag_to_var, strdup("tag1"), strdup("var1"));
|
||
|
g_hash_table_insert(form->tag_to_var, strdup("tag2"), strdup("var2"));
|
||
|
|
||
|
FormField *field1 = _new_field();
|
||
|
field1->var = strdup("var1");
|
||
|
field1->type_t = FIELD_JID_MULTI;
|
||
|
field1->values = g_slist_append(field1->values, strdup("dolan@server.com"));
|
||
|
field1->values = g_slist_append(field1->values, strdup("kieran@server.com"));
|
||
|
field1->values = g_slist_append(field1->values, strdup("chi@server.com"));
|
||
|
form->fields = g_slist_append(form->fields, field1);
|
||
|
|
||
|
FormField *field2 = _new_field();
|
||
|
field2->var = strdup("var2");
|
||
|
field2->type_t = FIELD_LIST_SINGLE;
|
||
|
field2->values = g_slist_append(field2->values, strdup("value2"));
|
||
|
form->fields = g_slist_append(form->fields, field2);
|
||
|
|
||
|
gboolean ret = form_add_unique_value(form, "tag1", "me@server.com");
|
||
|
|
||
|
int length = 0;
|
||
|
int count = 0;
|
||
|
GSList *curr_field = form->fields;
|
||
|
while (curr_field != NULL) {
|
||
|
FormField *field = curr_field->data;
|
||
|
if (g_strcmp0(field->var, "var1") == 0) {
|
||
|
length = g_slist_length(field->values);
|
||
|
GSList *curr_value = field->values;
|
||
|
while (curr_value != NULL) {
|
||
|
if (g_strcmp0(curr_value->data, "me@server.com") == 0) {
|
||
|
count++;
|
||
|
}
|
||
|
curr_value = g_slist_next(curr_value);
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
curr_field = g_slist_next(curr_field);
|
||
|
}
|
||
|
|
||
|
assert_true(ret);
|
||
|
assert_int_equal(length, 4);
|
||
|
assert_int_equal(count, 1);
|
||
|
|
||
|
form_destroy(form);
|
||
|
}
|