/*
 * unittests.c
 *
 * Copyright (C) 2015 - 2018 James Booth <boothj5@gmail.com>
 * Copyright (C) 2018 - 2026 Michael Vetter <jubalh@iodoru.org>
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include "prof_cmocka.h"
#include <sys/stat.h>
#include <stdlib.h>
#include <locale.h>
#include <langinfo.h>

#include "config.h"
#include "xmpp/chat_session.h"
#include "helpers.h"
#include "tools/test_autocomplete.h"
#include "xmpp/test_chat_session.h"
#include "test_common.h"
#include "xmpp/test_contact.h"
#include "command/test_cmd_connect.h"
#include "command/test_cmd_account.h"
#include "command/test_cmd_rooms.h"
#include "command/test_cmd_sub.h"
#include "command/test_cmd_presence.h"
#include "command/test_cmd_otr.h"
#include "command/test_cmd_pgp.h"
#include "xmpp/test_jid.h"
#include "tools/test_parser.h"
#include "xmpp/test_roster_list.h"
#include "config/test_preferences.h"
#include "event/test_server_events.h"
#include "command/test_cmd_alias.h"
#include "command/test_cmd_bookmark.h"
#include "command/test_cmd_join.h"
#include "xmpp/test_muc.h"
#include "command/test_cmd_ac.h"
#include "command/test_cmd_roster.h"
#include "command/test_cmd_disconnect.h"
#include "xmpp/test_form.h"
#include "plugins/test_callbacks.h"
#include "plugins/test_plugins_disco.h"

#define muc_unit_test(f) cmocka_unit_test_setup_teardown(f, muc_before_test, muc_after_test)

int
main(int argc, char* argv[])
{
    setlocale(LC_ALL, "en_GB.UTF-8");
    char* codeset = nl_langinfo(CODESET);
    char* lang = getenv("LANG");

    printf("Charset information:\n");

    if (lang) {
        printf("  LANG:       %s\n", lang);
    }
    if (codeset) {
        printf("  CODESET:    %s\n", codeset);
    }
    printf("  MB_CUR_MAX: %d\n", (int)MB_CUR_MAX);
    printf("  MB_LEN_MAX: %d\n", (int)MB_LEN_MAX);

    if (argc > 1)
        cmocka_set_test_filter(argv[1]);

    const struct CMUnitTest all_tests[] = {

        cmocka_unit_test(str_replace__returns__one_substr),
        cmocka_unit_test(str_replace__returns__one_substr_beginning),
        cmocka_unit_test(str_replace__returns__one_substr_end),
        cmocka_unit_test(str_replace__returns__two_substr),
        cmocka_unit_test(str_replace__returns__char),
        cmocka_unit_test(str_replace__returns__original_when_none),
        cmocka_unit_test(str_replace__returns__replaced_when_match),
        cmocka_unit_test(str_replace__returns__empty_when_string_empty),
        cmocka_unit_test(str_replace__returns__null_when_string_null),
        cmocka_unit_test(str_replace__returns__original_when_sub_empty),
        cmocka_unit_test(str_replace__returns__original_when_sub_null),
        cmocka_unit_test(str_replace__returns__empty_when_new_empty),
        cmocka_unit_test(str_replace__returns__original_when_new_null),
        cmocka_unit_test(valid_resource_presence_string__is__true_for_online),
        cmocka_unit_test(valid_resource_presence_string__is__true_for_chat),
        cmocka_unit_test(valid_resource_presence_string__is__true_for_away),
        cmocka_unit_test(valid_resource_presence_string__is__true_for_xa),
        cmocka_unit_test(valid_resource_presence_string__is__true_for_dnd),
        cmocka_unit_test(valid_resource_presence_string__is__false_for_available),
        cmocka_unit_test(valid_resource_presence_string__is__false_for_unavailable),
        cmocka_unit_test(valid_resource_presence_string__is__false_for_blah),
        cmocka_unit_test(utf8_display_len__returns__0_for_null),
        cmocka_unit_test(utf8_display_len__returns__1_for_non_wide),
        cmocka_unit_test(utf8_display_len__returns__2_for_wide),
        cmocka_unit_test(utf8_display_len__returns__correct_for_non_wide),
        cmocka_unit_test(utf8_display_len__returns__correct_for_wide),
        cmocka_unit_test(utf8_display_len__returns__correct_for_all_wide),
        cmocka_unit_test(strip_arg_quotes__returns__original_when_no_quotes),
        cmocka_unit_test(strip_arg_quotes__returns__stripped_first),
        cmocka_unit_test(strip_arg_quotes__returns__stripped_last),
        cmocka_unit_test(strip_arg_quotes__returns__stripped_both),
        cmocka_unit_test(strip_arg_quotes__unescapes),
        cmocka_unit_test(format_call_external_argv__tests__table_driven),
        cmocka_unit_test(unique_filename_from_url__tests__table_driven),
        cmocka_unit_test(string_to_verbosity__returns__correct_values),
        cmocka_unit_test(get_expanded_path__returns__expanded),
        cmocka_unit_test_setup_teardown(cmd_ac_complete_filepath__segfaults_when_empty, load_preferences, close_preferences),
        cmocka_unit_test_setup_teardown(cmd_ac_complete_filepath__finds_files_in_current_dir, load_preferences, close_preferences),
        cmocka_unit_test_setup_teardown(cmd_ac_complete_filepath__finds_files_with_dot_slash, load_preferences, close_preferences),
        cmocka_unit_test_setup_teardown(cmd_ac_complete_filepath__cycles_through_files, load_preferences, close_preferences),
        cmocka_unit_test(strtoi_range__returns__true_for_valid_input),
        cmocka_unit_test(strtoi_range__returns__false_for_out_of_range),
        cmocka_unit_test(strtoi_range__returns__false_for_invalid_input),
        cmocka_unit_test(strtoi_range__returns__false_for_null_empty_input),
        cmocka_unit_test(strtoi_range__returns__correct_values_when_err_msg_null),
        cmocka_unit_test(string_matches_one_of__tests__edge_cases),
        cmocka_unit_test(valid_tls_policy_option__is__correct_for_various_inputs),
        cmocka_unit_test(autocomplete_complete__returns__null_when_empty),
        cmocka_unit_test(autocomplete_reset__updates__after_create),
        cmocka_unit_test(autocomplete_complete__returns__null_after_create),
        cmocka_unit_test(autocomplete_complete__handles__escaped_spaces),
        cmocka_unit_test(autocomplete_complete__handles__escaped_quotes),
        cmocka_unit_test(autocomplete_create_list__returns__null_after_create),
        cmocka_unit_test(autocomplete_add__updates__one_and_complete),
        cmocka_unit_test(autocomplete_complete__returns__first_of_two),
        cmocka_unit_test(autocomplete_complete__returns__second_of_two),
        cmocka_unit_test(autocomplete_add__updates__two_elements),
        cmocka_unit_test(autocomplete_add__updates__only_once_for_same_value),
        cmocka_unit_test(autocomplete_add__updates__existing_value),
        cmocka_unit_test(autocomplete_complete__returns__accented_with_accented),
        cmocka_unit_test(autocomplete_complete__returns__accented_with_base),
        cmocka_unit_test(autocomplete_complete__returns__both_with_accented),
        cmocka_unit_test(autocomplete_complete__returns__both_with_base),
        cmocka_unit_test(autocomplete_complete__is__case_insensitive),
        cmocka_unit_test(autocomplete_complete__returns__previous),
        cmocka_unit_test(autocomplete_complete__returns__greek),
        cmocka_unit_test(autocomplete_complete__returns__greek_false_match),
        cmocka_unit_test(autocomplete_complete__returns__greek_case_insensitive),
        cmocka_unit_test(autocomplete_complete__returns__russian),
        cmocka_unit_test(autocomplete_complete__returns__chinese),
        cmocka_unit_test(autocomplete_complete__returns__transliterated),
        cmocka_unit_test(autocomplete_complete__returns__regular_ascii),

        cmocka_unit_test(jid_create__returns__null_from_null),
        cmocka_unit_test(jid_create__returns__null_from_empty_string),
        cmocka_unit_test(jid_create__returns__full_from_full),
        cmocka_unit_test(jid_create__returns__bare_from_full),
        cmocka_unit_test(jid_create__returns__resourcepart_from_full),
        cmocka_unit_test(jid_create__returns__localpart_from_full),
        cmocka_unit_test(jid_create__returns__domainpart_from_full),
        cmocka_unit_test(jid_create__returns__full_from_full_nolocal),
        cmocka_unit_test(jid_create__returns__bare_from_full_nolocal),
        cmocka_unit_test(jid_create__returns__resourcepart_from_full_nolocal),
        cmocka_unit_test(jid_create__returns__domainpart_from_full_nolocal),
        cmocka_unit_test(jid_create__returns__null_localpart_from_full_nolocal),
        cmocka_unit_test(jid_create__returns__null_full_from_bare),
        cmocka_unit_test(jid_create__returns__null_resource_from_bare),
        cmocka_unit_test(jid_create__returns__bare_from_bare),
        cmocka_unit_test(jid_create__returns__localpart_from_bare),
        cmocka_unit_test(jid_create__returns__domainpart_from_bare),
        cmocka_unit_test(jid_create_from_bare_and_resource__returns__room),
        cmocka_unit_test(jid_create_from_bare_and_resource__returns__nick),
        cmocka_unit_test(jid_create__returns__correct_parts_with_slash_in_resource),
        cmocka_unit_test(jid_create__returns__correct_parts_with_at_in_resource),
        cmocka_unit_test(jid_create__returns__correct_parts_with_at_and_slash_in_resource),
        cmocka_unit_test(jid_create__returns__correct_parts_with_trailing_slash),
        cmocka_unit_test(jid_fulljid_or_barejid__returns__fulljid_when_exists),
        cmocka_unit_test(jid_fulljid_or_barejid__returns__barejid_when_fulljid_not_exists),
        cmocka_unit_test(jid_create__returns__bare_from_trailing_slash),
        cmocka_unit_test(jid_create__returns__null_from_invalid_localpart_chars),
        cmocka_unit_test(jid_create__returns__null_from_empty_parts),
        cmocka_unit_test(jid_create__returns__null_from_multiple_at_in_bare),
        cmocka_unit_test(jid_create__returns__correct_parts_with_at_in_resource_only),
        cmocka_unit_test(jid_is_valid_user_jid__is__true_for_valid_user_jid),
        cmocka_unit_test(jid_is_valid_user_jid__is__false_for_domain_jid),
        cmocka_unit_test(jid_is_valid_user_jid__is__false_for_invalid_jid),
        cmocka_unit_test(jid_is_valid__is__true_for_valid_jid),
        cmocka_unit_test(jid_is_valid__is__false_for_invalid_jid),
        cmocka_unit_test(jid_is_valid__is__false_for_null),
        cmocka_unit_test(jid_is_valid__is__false_for_empty_string),

        cmocka_unit_test(parse_args__returns__null_from_null),
        cmocka_unit_test(parse_args__returns__null_from_empty),
        cmocka_unit_test(parse_args__returns__null_from_space),
        cmocka_unit_test(parse_args__returns__null_when_no_args),
        cmocka_unit_test(parse_args__returns__null_from_cmd_with_space),
        cmocka_unit_test(parse_args__returns__null_when_too_few),
        cmocka_unit_test(parse_args__returns__null_when_too_many),
        cmocka_unit_test(parse_args__returns__one_arg),
        cmocka_unit_test(parse_args__returns__two_args),
        cmocka_unit_test(parse_args__returns__three_args),
        cmocka_unit_test(parse_args__returns__three_args_with_spaces),
        cmocka_unit_test(parse_args_with_freetext__returns__freetext),
        cmocka_unit_test(parse_args_with_freetext__returns__one_arg_with_freetext),
        cmocka_unit_test(parse_args_with_freetext__returns__two_args_with_freetext),
        cmocka_unit_test(parse_args__returns__zero_args_when_min_zero),
        cmocka_unit_test(parse_args_with_freetext__returns__zero_args_when_min_zero),
        cmocka_unit_test(parse_args__returns__quoted_args),
        cmocka_unit_test(parse_args__returns__quoted_args_with_space),
        cmocka_unit_test(parse_args__returns__quoted_args_with_many_spaces),
        cmocka_unit_test(parse_args__returns__many_quoted_args_with_many_spaces),
        cmocka_unit_test(parse_args_with_freetext__returns__quoted_args),
        cmocka_unit_test(parse_args_with_freetext__returns__quoted_args_with_space),
        cmocka_unit_test(parse_args_with_freetext__returns__quoted_args_with_many_spaces),
        cmocka_unit_test(parse_args_with_freetext__returns__many_quoted_args_with_many_spaces),
        cmocka_unit_test(parse_args_with_freetext__returns__quoted_freetext),
        cmocka_unit_test(parse_args_with_freetext__returns__third_arg_quoted),
        cmocka_unit_test(parse_args_with_freetext__returns__second_arg_quoted),
        cmocka_unit_test(parse_args_with_freetext__returns__second_and_third_arg_quoted),
        cmocka_unit_test(parse_args__returns__escaped_quotes),
        cmocka_unit_test(parse_args__returns__escaped_spaces),
        cmocka_unit_test(parse_args__returns__escaped_backslash),
        cmocka_unit_test(count_tokens__returns__one_token),
        cmocka_unit_test(count_tokens__returns__one_token_quoted_no_whitespace),
        cmocka_unit_test(count_tokens__returns__one_token_quoted_with_whitespace),
        cmocka_unit_test(count_tokens__returns__two_tokens),
        cmocka_unit_test(count_tokens__returns__two_tokens_first_quoted),
        cmocka_unit_test(count_tokens__returns__two_tokens_second_quoted),
        cmocka_unit_test(count_tokens__returns__two_tokens_both_quoted),
        cmocka_unit_test(count_tokens__handles__escapes),
        cmocka_unit_test(count_tokens__handles__multiple_spaces),
        cmocka_unit_test(get_start__returns__first_of_one),
        cmocka_unit_test(get_start__returns__first_of_two),
        cmocka_unit_test(get_start__returns__first_two_of_three),
        cmocka_unit_test(get_start__returns__first_two_of_three_first_quoted),
        cmocka_unit_test(get_start__returns__first_two_of_three_second_quoted),
        cmocka_unit_test(get_start__returns__first_two_of_three_first_and_second_quoted),
        cmocka_unit_test(get_start__handles__escapes),
        cmocka_unit_test(get_start__handles__multiple_spaces),
        cmocka_unit_test(parse_options__returns__empty_hashmap_when_none),
        cmocka_unit_test(parse_options__returns__error_when_opt1_no_val),
        cmocka_unit_test(parse_options__returns__map_when_one),
        cmocka_unit_test(parse_options__returns__error_when_opt2_no_val),
        cmocka_unit_test(parse_options__returns__map_when_two),
        cmocka_unit_test(parse_options__returns__error_when_opt3_no_val),
        cmocka_unit_test(parse_options__returns__map_when_three),
        cmocka_unit_test(parse_options__returns__error_when_unknown_opt),
        cmocka_unit_test(parse_options__returns__error_when_duplicated_option),

        cmocka_unit_test(roster_get_contacts__returns__empty_list_when_none_added),
        cmocka_unit_test(roster_get_contacts__returns__one_element),
        cmocka_unit_test(roster_get_contacts__returns__correct_first_element),
        cmocka_unit_test(roster_get_contacts__returns__two_elements),
        cmocka_unit_test(roster_get_contacts__returns__correct_first_and_second_elements),
        cmocka_unit_test(roster_get_contacts__returns__three_elements),
        cmocka_unit_test(roster_get_contacts__returns__correct_first_three_elements),
        cmocka_unit_test(roster_add__updates__adds_once_when_called_twice_at_beginning),
        cmocka_unit_test(roster_add__updates__adds_once_when_called_twice_in_middle),
        cmocka_unit_test(roster_add__updates__adds_once_when_called_twice_at_end),
        cmocka_unit_test(roster_contact_autocomplete__returns__first_exists),
        cmocka_unit_test(roster_contact_autocomplete__returns__second_exists),
        cmocka_unit_test(roster_contact_autocomplete__returns__third_exists),
        cmocka_unit_test(roster_contact_autocomplete__returns__null_when_no_match),
        cmocka_unit_test(roster_contact_autocomplete__returns__null_on_empty_roster),
        cmocka_unit_test(roster_contact_autocomplete__returns__second_when_two_match),
        cmocka_unit_test(roster_contact_autocomplete__returns__fifth_when_multiple_match),
        cmocka_unit_test(roster_contact_autocomplete__returns__first_when_two_match_and_reset),
        cmocka_unit_test(roster_contact_autocomplete__returns__utf8),
        cmocka_unit_test(roster_get_groups__returns__empty_for_no_group),
        cmocka_unit_test(roster_get_groups__returns__one_group),
        cmocka_unit_test(roster_get_groups__returns__two_groups),
        cmocka_unit_test(roster_get_groups__returns__three_groups),
        cmocka_unit_test(roster_update__updates__adding_two_groups),
        cmocka_unit_test(roster_update__updates__removing_one_group),
        cmocka_unit_test(roster_update__updates__removing_two_groups),
        cmocka_unit_test(roster_update__updates__removing_three_groups),
        cmocka_unit_test(roster_update__updates__two_new_groups),
        cmocka_unit_test(roster_remove__updates__contact_groups),
        cmocka_unit_test(roster_add__updates__different_groups),
        cmocka_unit_test(roster_add__updates__same_groups),
        cmocka_unit_test(roster_add__updates__overlapping_groups),
        cmocka_unit_test(roster_remove__updates__remaining_in_group),
        cmocka_unit_test(roster_get_display_name__returns__nickname_when_exists),
        cmocka_unit_test(roster_get_display_name__returns__barejid_when_nickname_empty),
        cmocka_unit_test(roster_get_display_name__returns__barejid_when_not_exists),

        cmocka_unit_test_setup_teardown(chat_session_get__returns__null_when_no_session,
                                        init_chat_sessions,
                                        close_chat_sessions),
        cmocka_unit_test_setup_teardown(chat_session_recipient_active__updates__new_session,
                                        init_chat_sessions,
                                        close_chat_sessions),
        cmocka_unit_test_setup_teardown(chat_session_recipient_active__updates__replace_resource,
                                        init_chat_sessions,
                                        close_chat_sessions),
        cmocka_unit_test_setup_teardown(chat_session_remove__updates__session_removed,
                                        init_chat_sessions,
                                        close_chat_sessions),

        cmocka_unit_test_setup_teardown(cmd_connect__shows__message_when_disconnecting,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_connect__shows__message_when_connecting,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_connect__shows__message_when_connected,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_connect__tests__no_account,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_connect__shows__fail_message,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_connect__tests__lowercases_argument_with_account,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_connect__tests__lowercases_argument_with_no_account,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_connect__tests__asks_password_when_not_in_account,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_connect__shows__usage_when_no_server_value,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_connect__shows__message_when_connecting_with_account,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_connect__tests__connects_with_account,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_connect__shows__usage_when_server_no_port_value,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_connect__shows__usage_when_no_port_value,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_connect__shows__usage_when_port_no_server_value,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_connect__shows__message_when_port_0,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_connect__shows__message_when_port_minus1,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_connect__shows__message_when_port_65536,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_connect__shows__message_when_port_contains_chars,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_connect__tests__with_server_when_provided,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_connect__tests__with_port_when_provided,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_connect__tests__with_server_and_port_when_provided,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_connect__shows__usage_when_server_provided_twice,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_connect__shows__usage_when_port_provided_twice,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_connect__shows__usage_when_invalid_first_property,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_connect__shows__usage_when_invalid_second_property,
                                        load_preferences,
                                        close_preferences),

        cmocka_unit_test(cmd_rooms__shows__message_when_disconnected),
        cmocka_unit_test(cmd_rooms__shows__message_when_disconnecting),
        cmocka_unit_test(cmd_rooms__shows__message_when_connecting),
        cmocka_unit_test(cmd_rooms__tests__account_default_when_no_arg),
        cmocka_unit_test(cmd_rooms__tests__service_arg_used_when_passed),
        cmocka_unit_test(cmd_rooms__tests__filter_arg_used_when_passed),

        cmocka_unit_test(cmd_account__shows__usage_when_not_connected_and_no_args),
        cmocka_unit_test(cmd_account__shows__account_when_connected_and_no_args),
        cmocka_unit_test(cmd_account_list__shows__accounts),
        cmocka_unit_test(cmd_account_show__shows__usage_when_no_arg),
        cmocka_unit_test(cmd_account_show__shows__message_when_account_does_not_exist),
        cmocka_unit_test(cmd_account_show__shows__account_when_exists),
        cmocka_unit_test(cmd_account_add__shows__usage_when_no_arg),
        cmocka_unit_test(cmd_account_add__updates__adds_account),
        cmocka_unit_test(cmd_account_enable__shows__usage_when_no_arg),
        cmocka_unit_test(cmd_account_enable__updates__enables_account),
        cmocka_unit_test(cmd_account_enable__shows__message_when_account_doesnt_exist),
        cmocka_unit_test(cmd_account_disable__shows__usage_when_no_arg),
        cmocka_unit_test(cmd_account_disable__updates__disables_account),
        cmocka_unit_test(cmd_account_disable__shows__message_when_account_doesnt_exist),
        cmocka_unit_test(cmd_account_rename__shows__usage_when_no_args),
        cmocka_unit_test(cmd_account_rename__shows__usage_when_one_arg),
        cmocka_unit_test(cmd_account_rename__updates__renames_account),
        cmocka_unit_test(cmd_account_rename__shows__message_when_not_renamed),
        cmocka_unit_test(cmd_account_set__shows__usage_when_no_args),
        cmocka_unit_test(cmd_account_set__shows__usage_when_one_arg),
        cmocka_unit_test(cmd_account_set__shows__usage_when_two_args),
        cmocka_unit_test(cmd_account_set__shows__message_when_account_doesnt_exist),
        cmocka_unit_test(cmd_account_set__shows__message_for_malformed_jid),
        cmocka_unit_test(cmd_account_set__updates__jid_sets_barejid),
        cmocka_unit_test(cmd_account_set__updates__jid_sets_resource),
        cmocka_unit_test(cmd_account_set__updates__server_sets_server),
        cmocka_unit_test(cmd_account_set__updates__resource_sets_resource),
        cmocka_unit_test(cmd_account_set__shows__resource_sets_resource_with_online_message),
        cmocka_unit_test(cmd_account_set__updates__password_sets_password),
        cmocka_unit_test(cmd_account_set__updates__eval_password_sets_eval_password),
        cmocka_unit_test(cmd_account_set__shows__password_when_eval_password_set),
        cmocka_unit_test(cmd_account_set__shows__eval_password_when_password_set),
        cmocka_unit_test(cmd_account_set__updates__muc_sets_muc),
        cmocka_unit_test(cmd_account_set__updates__nick_sets_nick),
#ifdef HAVE_LIBOTR
        cmocka_unit_test(cmd_account_set__shows__message_for_missing_otr_policy),
        cmocka_unit_test(cmd_account_set__shows__message_for_invalid_otr_policy),
        cmocka_unit_test(cmd_account_set__updates__otr_sets_otr),
#endif
        cmocka_unit_test(cmd_account_set__shows__message_when_invalid_status),
        cmocka_unit_test(cmd_account_set__updates__status_sets_status_when_valid),
        cmocka_unit_test(cmd_account_set__updates__status_sets_status_when_last),
        cmocka_unit_test(cmd_account_set__shows__invalid_presence_string_priority_message),
        cmocka_unit_test(cmd_account_set__shows__last_priority_message),
        cmocka_unit_test(cmd_account_set__updates__online_priority_sets_preference),
        cmocka_unit_test(cmd_account_set__updates__chat_priority_sets_preference),
        cmocka_unit_test(cmd_account_set__updates__away_priority_sets_preference),
        cmocka_unit_test(cmd_account_set__updates__xa_priority_sets_preference),
        cmocka_unit_test(cmd_account_set__updates__dnd_priority_sets_preference),
        cmocka_unit_test(cmd_account_set__shows__priority_too_low_message),
        cmocka_unit_test(cmd_account_set__shows__priority_too_high_message),
        cmocka_unit_test(cmd_account_set__shows__priority_when_not_number_message),
        cmocka_unit_test(cmd_account_set__shows__priority_when_empty_message),
        cmocka_unit_test(cmd_account_set__updates__priority_updates_presence_when_connected),
        cmocka_unit_test(cmd_account_clear__shows__usage_when_no_args),
        cmocka_unit_test(cmd_account_clear__shows__usage_when_one_arg),
        cmocka_unit_test(cmd_account_clear__shows__message_when_account_doesnt_exist),
        cmocka_unit_test(cmd_account_clear__shows__message_when_invalid_property),

        cmocka_unit_test(cmd_sub__shows__message_when_not_connected),
        cmocka_unit_test(cmd_sub__shows__usage_when_no_arg),

        cmocka_unit_test(p_contact_in_group__is__true_when_in_group),
        cmocka_unit_test(p_contact_in_group__is__false_when_not_in_group),
        cmocka_unit_test(p_contact_name_or_jid__returns__name_when_exists),
        cmocka_unit_test(p_contact_name_or_jid__returns__jid_when_name_not_exists),
        cmocka_unit_test(p_contact_create_display_string__returns__name_and_resource_when_name_exists),
        cmocka_unit_test(p_contact_create_display_string__returns__jid_and_resource_when_name_not_exists),
        cmocka_unit_test(p_contact_create_display_string__returns__name_when_default_resource),
        cmocka_unit_test(p_contact_presence__returns__offline_when_no_resources),
        cmocka_unit_test(p_contact_presence__returns__highest_priority_presence),
        cmocka_unit_test(p_contact_presence__returns__chat_when_same_priority),
        cmocka_unit_test(p_contact_presence__returns__online_when_same_priority),
        cmocka_unit_test(p_contact_presence__returns__away_when_same_priority),
        cmocka_unit_test(p_contact_presence__returns__xa_when_same_priority),
        cmocka_unit_test(p_contact_presence__returns__dnd),
        cmocka_unit_test(p_contact_subscribed__is__true_when_to),
        cmocka_unit_test(p_contact_subscribed__is__true_when_both),
        cmocka_unit_test(p_contact_subscribed__is__false_when_from),
        cmocka_unit_test(p_contact_subscribed__is__false_when_no_subscription_value),
        cmocka_unit_test(p_contact_is_available__is__false_when_offline),
        cmocka_unit_test(p_contact_is_available__is__false_when_highest_priority_away),
        cmocka_unit_test(p_contact_is_available__is__false_when_highest_priority_xa),
        cmocka_unit_test(p_contact_is_available__is__false_when_highest_priority_dnd),
        cmocka_unit_test(p_contact_is_available__is__true_when_highest_priority_online),
        cmocka_unit_test(p_contact_is_available__is__true_when_highest_priority_chat),

        cmocka_unit_test(cmd_presence__shows__usage_when_bad_subcmd),
        cmocka_unit_test(cmd_presence__shows__usage_when_bad_console_setting),
        cmocka_unit_test(cmd_presence__shows__usage_when_bad_chat_setting),
        cmocka_unit_test(cmd_presence__shows__usage_when_bad_muc_setting),
        cmocka_unit_test_setup_teardown(cmd_presence__updates__console_all,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_presence__updates__console_online,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_presence__updates__console_none,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_presence__updates__chat_all,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_presence__updates__chat_online,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_presence__updates__chat_none,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_presence__updates__room_all,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_presence__updates__room_online,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_presence__updates__room_none,
                                        load_preferences,
                                        close_preferences),

        cmocka_unit_test_setup_teardown(prefs_get_string__returns__all_for_console_default,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(prefs_get_string__returns__none_for_chat_default,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(prefs_get_string__returns__none_for_muc_default,
                                        load_preferences,
                                        close_preferences),

        cmocka_unit_test_setup_teardown(sv_ev_contact_online__shows__presence_in_console_when_set_online,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(sv_ev_contact_online__shows__presence_in_console_when_set_all,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(sv_ev_contact_online__shows__dnd_presence_in_console_when_set_all,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(sv_ev_contact_offline__updates__removes_chat_session,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(sv_ev_lost_connection__updates__clears_chat_sessions,
                                        load_preferences,
                                        close_preferences),

        cmocka_unit_test(cmd_alias__shows__usage_when_add_no_args),
        cmocka_unit_test(cmd_alias__shows__usage_when_add_no_value),
        cmocka_unit_test(cmd_alias__shows__usage_when_remove_no_args),
        cmocka_unit_test(cmd_alias__shows__usage_when_invalid_subcmd),
        cmocka_unit_test_setup_teardown(cmd_alias__updates__adds_alias,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_alias__shows__message_when_add_exists,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_alias__updates__removes_alias,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_alias__shows__message_when_remove_no_alias,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_alias__shows__all_aliases,
                                        load_preferences,
                                        close_preferences),

        cmocka_unit_test_setup_teardown(muc_invites_add__updates__invites_list, muc_before_test, muc_after_test),
        cmocka_unit_test_setup_teardown(muc_invites_remove__updates__invites_list, muc_before_test, muc_after_test),
        cmocka_unit_test_setup_teardown(muc_invites_count__returns__0_when_no_invites, muc_before_test, muc_after_test),
        cmocka_unit_test_setup_teardown(muc_invites_count__returns__5_when_five_invites_added, muc_before_test, muc_after_test),
        cmocka_unit_test_setup_teardown(muc_active__is__false_when_not_joined, muc_before_test, muc_after_test),
        cmocka_unit_test_setup_teardown(muc_active__is__true_when_joined, muc_before_test, muc_after_test),

        cmocka_unit_test(cmd_bookmark__shows__message_when_disconnected),
        cmocka_unit_test(cmd_bookmark__shows__message_when_disconnecting),
        cmocka_unit_test(cmd_bookmark__shows__message_when_connecting),
        cmocka_unit_test(cmd_bookmark__shows__usage_when_no_args),
        cmocka_unit_test(cmd_bookmark_list__shows__bookmarks),
        cmocka_unit_test(cmd_bookmark_add__shows__message_when_invalid_jid),
        cmocka_unit_test(cmd_bookmark_add__updates__bookmark_with_jid),
        muc_unit_test(cmd_bookmark__tests__uses_roomjid_in_room),
        muc_unit_test(cmd_bookmark_add__tests__uses_roomjid_in_room),
        muc_unit_test(cmd_bookmark_add__tests__uses_supplied_jid_in_room),
        muc_unit_test(cmd_bookmark_remove__tests__uses_roomjid_in_room),
        muc_unit_test(cmd_bookmark_remove__tests__uses_supplied_jid_in_room),
        cmocka_unit_test(cmd_bookmark_add__updates__bookmark_with_jid_nick),
        cmocka_unit_test(cmd_bookmark_add__updates__bookmark_with_jid_autojoin),
        cmocka_unit_test(cmd_bookmark_add__updates__bookmark_with_jid_nick_autojoin),
        cmocka_unit_test(cmd_bookmark_remove__updates__removes_bookmark),
        cmocka_unit_test(cmd_bookmark_remove__shows__message_when_no_bookmark),

#ifdef HAVE_LIBOTR
        cmocka_unit_test(cmd_otr_log__shows__usage_when_no_args),
        cmocka_unit_test(cmd_otr_log__shows__usage_when_invalid_subcommand),
        cmocka_unit_test_setup_teardown(cmd_otr_log__updates__enables_logging,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_otr_log__updates__disables_logging,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_otr_log__updates__redacts_logging,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_otr_log__shows__warning_when_chlog_disabled,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(cmd_otr_log__shows__redact_warning_when_chlog_disabled,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test(cmd_otr_libver__shows__libotr_version),
        cmocka_unit_test(cmd_otr_gen__shows__message_when_not_connected),
        cmocka_unit_test(cmd_otr_gen__tests__generates_key_for_connected_account),
        cmocka_unit_test(cmd_otr_gen__shows__message_when_disconnected),
        cmocka_unit_test(cmd_otr_gen__shows__message_when_connecting),
        cmocka_unit_test(cmd_otr_gen__shows__message_when_disconnecting),
        cmocka_unit_test(cmd_otr_myfp__shows__message_when_disconnected),
        cmocka_unit_test(cmd_otr_myfp__shows__message_when_connecting),
        cmocka_unit_test(cmd_otr_myfp__shows__message_when_disconnecting),
        cmocka_unit_test(cmd_otr_myfp__shows__message_when_no_key),
        cmocka_unit_test(cmd_otr_myfp__shows__my_fingerprint),
        cmocka_unit_test(cmd_otr_theirfp__shows__message_when_in_console),
        cmocka_unit_test(cmd_otr_theirfp__shows__message_when_in_muc),
        cmocka_unit_test(cmd_otr_theirfp__shows__message_when_in_private),
        cmocka_unit_test(cmd_otr_theirfp__shows__message_when_non_otr_chat_window),
        cmocka_unit_test(cmd_otr_theirfp__shows__fingerprint),
        cmocka_unit_test(cmd_otr_start__shows__message_when_in_console),
        cmocka_unit_test(cmd_otr_start__shows__message_when_in_muc),
        cmocka_unit_test(cmd_otr_start__shows__message_when_in_private),
        cmocka_unit_test(cmd_otr_start__shows__message_when_already_started),
        cmocka_unit_test(cmd_otr_start__shows__message_when_no_key),
        cmocka_unit_test_setup_teardown(cmd_otr_start__tests__sends_otr_query_message_to_current_recipeint,
                                        load_preferences,
                                        close_preferences),
#else
        cmocka_unit_test(cmd_otr__shows__message_when_otr_unsupported),
#endif

#ifdef HAVE_LIBGPGME
        cmocka_unit_test(cmd_pgp__shows__usage_when_no_args),
        cmocka_unit_test(cmd_pgp_start__shows__message_when_disconnected),
        cmocka_unit_test(cmd_pgp_start__shows__message_when_disconnecting),
        cmocka_unit_test(cmd_pgp_start__shows__message_when_connecting),
        cmocka_unit_test(cmd_pgp_start__shows__message_when_no_arg_in_console),
        cmocka_unit_test(cmd_pgp_start__shows__message_when_no_arg_in_muc),
        cmocka_unit_test(cmd_pgp_start__shows__message_when_no_arg_in_conf),
        cmocka_unit_test(cmd_pgp_start__shows__message_when_no_arg_in_private),
        cmocka_unit_test(cmd_pgp_start__shows__message_when_no_arg_in_xmlconsole),
#else
        cmocka_unit_test(cmd_pgp__shows__message_when_pgp_unsupported),
#endif

        cmocka_unit_test(cmd_join__shows__message_when_disconnecting),
        cmocka_unit_test(cmd_join__shows__message_when_connecting),
        cmocka_unit_test(cmd_join__shows__message_when_disconnected),
        cmocka_unit_test(cmd_join__shows__error_message_when_invalid_room_jid),
        muc_unit_test(cmd_join__tests__uses_account_mucservice_when_no_service_specified),
        muc_unit_test(cmd_join__tests__uses_supplied_nick),
        muc_unit_test(cmd_join__tests__uses_account_nick_when_not_supplied),
        muc_unit_test(cmd_join__tests__uses_password_when_supplied),

        cmocka_unit_test(cmd_roster__shows__message_when_disconnecting),
        cmocka_unit_test(cmd_roster__shows__message_when_connecting),
        cmocka_unit_test(cmd_roster__shows__message_when_disconnected),
        cmocka_unit_test(cmd_roster__shows__roster_when_no_args),
        cmocka_unit_test(cmd_roster__shows__message_when_add_no_jid),
        cmocka_unit_test(cmd_roster__tests__add_sends_roster_add_request),
        cmocka_unit_test(cmd_roster__shows__message_when_remove_no_jid),
        cmocka_unit_test(cmd_roster__tests__remove_sends_roster_remove_request),
        cmocka_unit_test(cmd_roster__tests__remove_nickname_sends_roster_remove_request),
        cmocka_unit_test(cmd_roster__shows__message_when_nick_no_jid),
        cmocka_unit_test(cmd_roster__shows__message_when_nick_no_nick),
        cmocka_unit_test(cmd_roster__shows__message_when_nick_no_contact_exists),
        cmocka_unit_test(cmd_roster__tests__nick_sends_name_change_request),
        cmocka_unit_test(cmd_roster__shows__message_when_clearnick_no_jid),
        cmocka_unit_test(cmd_roster__shows__message_when_clearnick_no_contact_exists),
        cmocka_unit_test(cmd_roster__tests__clearnick_sends_name_change_request_with_empty_nick),

        cmocka_unit_test(form_get_form_type_field__returns__null_no_fields),
        cmocka_unit_test(form_get_form_type_field__returns__null_when_not_present),
        cmocka_unit_test(form_get_form_type_field__returns__value_when_present),
        cmocka_unit_test(form_get_field_type__returns__unknown_when_no_fields),
        cmocka_unit_test(form_get_field_type__returns__correct_type),
        cmocka_unit_test(form_set_value__updates__adds_when_none),
        cmocka_unit_test(form_set_value__updates__updates_when_one),
        cmocka_unit_test(form_add_unique_value__updates__adds_when_none),
        cmocka_unit_test(form_add_unique_value__updates__does_nothing_when_exists),
        cmocka_unit_test(form_add_unique_value__updates__adds_when_doesnt_exist),
        cmocka_unit_test(form_add_value__updates__adds_when_none),
        cmocka_unit_test(form_add_value__updates__adds_when_some),
        cmocka_unit_test(form_add_value__updates__adds_when_exists),
        cmocka_unit_test(form_remove_value__updates__does_nothing_when_none),
        cmocka_unit_test(form_remove_value__updates__does_nothing_when_doesnt_exist),
        cmocka_unit_test(form_remove_value__updates__removes_when_one),
        cmocka_unit_test(form_remove_value__updates__removes_when_many),
        cmocka_unit_test(form_remove_text_multi_value__updates__does_nothing_when_none),
        cmocka_unit_test(form_remove_text_multi_value__updates__does_nothing_when_doesnt_exist),
        cmocka_unit_test(form_remove_text_multi_value__updates__removes_when_one),
        cmocka_unit_test(form_remove_text_multi_value__updates__removes_when_many),

        cmocka_unit_test_setup_teardown(cmd_disconnect__updates__clears_chat_sessions,
                                        load_preferences,
                                        close_preferences),

        cmocka_unit_test(prof_occurrences__tests__partial),
        cmocka_unit_test(prof_occurrences__tests__whole),
        cmocka_unit_test(prof_occurrences__tests__large_message),
        cmocka_unit_test(get_mentions__tests__various),
        cmocka_unit_test(release_is_new__tests__various),
        cmocka_unit_test(str_xml_sanitize__strips_illegal_characters),

        cmocka_unit_test_setup_teardown(plugins_get_command_names__returns__no_commands,
                                        load_preferences,
                                        close_preferences),
        cmocka_unit_test_setup_teardown(plugins_get_command_names__returns__commands_when_added,
                                        load_preferences,
                                        close_preferences),

        cmocka_unit_test(disco_get_features__returns__empty_list_when_none),
        cmocka_unit_test(disco_add_feature__updates__added_feature),
        cmocka_unit_test(disco_close__updates__resets_features),
        cmocka_unit_test(disco_get_features__returns__all_added_features),
        cmocka_unit_test(disco_add_feature__updates__not_duplicate_feature),
        cmocka_unit_test(disco_remove_features__updates__removes_plugin_features),
        cmocka_unit_test(disco_remove_features__updates__not_remove_when_more_than_one_reference),
    };
    return cmocka_run_group_tests(all_tests, NULL, NULL);
}
