/*
 * Ibis - IRCv3 Library
 * Copyright (C) 2022-2024 Ibis Developers
 *
 * Ibis is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, see <https://www.gnu.org/licenses/>.
 */

#include <glib.h>

#include <ibis.h>

/******************************************************************************
 * Parse Tests
 *****************************************************************************/
static void
test_ibis_source_parse_return_address_required(void) {
	if(g_test_subprocess()) {
		ibis_source_parse("pidgy", NULL, NULL, NULL);
	}

	g_test_trap_subprocess(NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
	g_test_trap_assert_stderr("*nick != NULL || user != NULL || host != NULL' failed*");
}

static void
test_ibis_source_parse_nick(void) {
	char *nick = NULL;
	char *user = NULL;
	char *host = NULL;

	/* Once to make sure that user and host are null. */
	ibis_source_parse("pidgy", &nick, &user, &host);
	g_assert_cmpstr(nick, ==, "pidgy");
	g_clear_pointer(&nick, g_free);

	g_assert_null(user);
	g_assert_null(host);

	/* Again to make sure it works without user and host. */
	ibis_source_parse("pidgy", &nick, NULL, NULL);
	g_assert_cmpstr(nick, ==, "pidgy");
	g_clear_pointer(&nick, g_free);
}

static void
test_ibis_source_parse_user(void) {
	char *nick = NULL;
	char *user = NULL;
	char *host = NULL;

	/* Once to make sure host is null. */
	ibis_source_parse("pidgy!~u", &nick, &user, &host);
	g_assert_cmpstr(nick, ==, "pidgy");
	g_clear_pointer(&nick, g_free);

	g_assert_cmpstr(user, ==, "~u");
	g_clear_pointer(&user, g_free);

	g_assert_null(host);

	/* Again to make sure nick and host aren't required. */
	ibis_source_parse("pidgy!~u", NULL, &user, NULL);
	g_assert_cmpstr(user, ==, "~u");
	g_clear_pointer(&user, g_free);
}

static void
test_ibis_source_parse_host(void) {
	char *nick = NULL;
	char *user = NULL;
	char *host = NULL;

	/* Once to make sure everything works. */
	ibis_source_parse("pidgy!~u@53unc8n42i868.irc", &nick, &user, &host);
	g_assert_cmpstr(nick, ==, "pidgy");
	g_clear_pointer(&nick, g_free);

	g_assert_cmpstr(user, ==, "~u");
	g_clear_pointer(&user, g_free);

	g_assert_cmpstr(host, ==, "53unc8n42i868.irc");
	g_clear_pointer(&host, g_free);

	/* Again to make sure nick and host aren't required. */
	ibis_source_parse("pidgy!~u@53unc8n42i868.irc", NULL, NULL, &host);
	g_assert_cmpstr(host, ==, "53unc8n42i868.irc");
	g_clear_pointer(&host, g_free);
}

static void
test_ibis_source_parse_baddies(void) {
	char *nick = NULL;
	char *user = NULL;
	char *server = NULL;

	ibis_source_parse("\n", &nick, NULL, NULL);
	g_assert_null(nick);

	ibis_source_parse("a\nb", &nick, NULL, NULL);
	g_assert_null(nick);

	ibis_source_parse("\r", &nick, NULL, NULL);
	g_assert_null(nick);

	ibis_source_parse("c\rd", &nick, NULL, NULL);
	g_assert_null(nick);

	ibis_source_parse(" ", &nick, NULL, NULL);
	g_assert_null(nick);

	ibis_source_parse("e f", &nick, NULL, NULL);
	g_assert_null(nick);

	ibis_source_parse("nick@foo!user@server", &nick, &user, &server);
	g_assert_cmpstr(nick, ==, "nick@foo");
	g_clear_pointer(&nick, g_free);
	g_assert_cmpstr(user, ==, "user");
	g_clear_pointer(&user, g_free);
	g_assert_cmpstr(server, ==, "server");
	g_clear_pointer(&server, g_free);

	ibis_source_parse("nick!user@server!foo", &nick, &user, &server);
	g_assert_cmpstr(nick, ==, "nick!user@server!foo");
	g_clear_pointer(&nick, g_free);
	g_assert_null(user);
	g_assert_null(server);
}

/******************************************************************************
 * Serialize Tests
 *****************************************************************************/
static void
test_ibis_source_serialize_nick_required(void) {
	if(g_test_subprocess()) {
		ibis_source_serialize(NULL, NULL, NULL);
	}

	g_test_trap_subprocess(NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
	g_test_trap_assert_stderr("*'!ibis_str_is_empty(nick)' failed*");
}

static void
test_ibis_source_serialize_nick(void) {
	char *result = NULL;

	result = ibis_source_serialize("irene", NULL, NULL);

	g_assert_cmpstr(result, ==, "irene");
	g_clear_pointer(&result, g_free);
}

static void
test_ibis_source_serialize_user(void) {
	char *result = NULL;

	result = ibis_source_serialize("irene", "~u", NULL);

	g_assert_cmpstr(result, ==, "irene!~u");
	g_clear_pointer(&result, g_free);
}

static void
test_ibis_source_serialize_host(void) {
	char *result = NULL;

	result = ibis_source_serialize("irene", "~u", "server");

	g_assert_cmpstr(result, ==, "irene!~u@server");
	g_clear_pointer(&result, g_free);
}

static void
test_ibis_source_serialize_host_without_user(void) {
	char *result = NULL;

	result = ibis_source_serialize("irene", NULL, "server");

	g_assert_cmpstr(result, ==, "irene");
	g_clear_pointer(&result, g_free);
}

/******************************************************************************
 * Strip Prefix Tests
 *****************************************************************************/
static void
test_ibis_source_strip_prefix_none(void) {
	char *result = NULL;

	result = ibis_source_strip_prefix("irene", "@+");
	g_assert_cmpstr(result, ==, "irene");
	g_clear_pointer(&result, g_free);
}

static void
test_ibis_source_strip_prefix_single(void) {
	char *result = NULL;

	result = ibis_source_strip_prefix("+irene", "@+");
	g_assert_cmpstr(result, ==, "irene");
	g_clear_pointer(&result, g_free);
}

static void
test_ibis_source_strip_prefix_multiple(void) {
	char *result = NULL;

	result = ibis_source_strip_prefix("@+irene", "@+");
	g_assert_cmpstr(result, ==, "irene");
	g_clear_pointer(&result, g_free);
}

static void
test_ibis_source_strip_prefix_out_of_order(void) {
	char *result = NULL;

	result = ibis_source_strip_prefix("+@irene", "@+");
	g_assert_cmpstr(result, ==, "@irene");
	g_clear_pointer(&result, g_free);
}

static void
test_ibis_source_strip_prefix_prefix_only(void) {
	char *result = NULL;

	result = ibis_source_strip_prefix("@", "@+");
	g_assert_cmpstr(result, ==, "");
	g_free(result);
}

/******************************************************************************
 * Get Prefix Tests
 *****************************************************************************/
static void
test_ibis_source_get_prefix_none(void) {
	char *result = NULL;

	result = ibis_source_get_prefix("irene", "@+");
	g_assert_cmpstr(result, ==, NULL);
}

static void
test_ibis_source_get_prefix_single(void) {
	char *result = NULL;

	result = ibis_source_get_prefix("+irene", "@+");
	g_assert_cmpstr(result, ==, "+");
	g_clear_pointer(&result, g_free);
}

static void
test_ibis_source_get_prefix_multiple(void) {
	char *result = NULL;

	result = ibis_source_get_prefix("@+irene", "@+");
	g_assert_cmpstr(result, ==, "@+");
	g_clear_pointer(&result, g_free);
}

static void
test_ibis_source_get_prefix_out_of_order(void) {
	char *result = NULL;

	result = ibis_source_get_prefix("+@irene", "@+");
	g_assert_cmpstr(result, ==, "+");
	g_clear_pointer(&result, g_free);
}

static void
test_ibis_source_get_prefix_prefix_only(void) {
	char *result = NULL;

	result = ibis_source_get_prefix("+", "@+");
	g_assert_cmpstr(result, ==, "+");
	g_clear_pointer(&result, g_free);
}

/******************************************************************************
 * Main
 *****************************************************************************/
int
main(int argc, char *argv[]) {
	g_test_init(&argc, &argv, NULL);
	g_test_set_nonfatal_assertions();

	g_test_add_func("/ibis/source/parse/return-address-required",
	                test_ibis_source_parse_return_address_required);
	g_test_add_func("/ibis/source/parse/nick", test_ibis_source_parse_nick);
	g_test_add_func("/ibis/source/parse/user", test_ibis_source_parse_user);
	g_test_add_func("/ibis/source/parse/host", test_ibis_source_parse_host);
	g_test_add_func("/ibis/source/parse/baddies",
	                test_ibis_source_parse_baddies);

	g_test_add_func("/ibis/source/serialize/nick-required",
	                test_ibis_source_serialize_nick_required);
	g_test_add_func("/ibis/source/serialize/nick",
	                test_ibis_source_serialize_nick);
	g_test_add_func("/ibis/source/serialize/user",
	                test_ibis_source_serialize_user);
	g_test_add_func("/ibis/source/serialize/host",
	                test_ibis_source_serialize_host);
	g_test_add_func("/ibis/source/serialize/host-without-user",
	                test_ibis_source_serialize_host_without_user);

	g_test_add_func("/ibis/source/strip-prefix/none",
	                test_ibis_source_strip_prefix_none);
	g_test_add_func("/ibis/source/strip-prefix/single",
	                test_ibis_source_strip_prefix_single);
	g_test_add_func("/ibis/source/strip-prefix/multiple",
	                test_ibis_source_strip_prefix_multiple);
	g_test_add_func("/ibis/source/strip-prefix/out-of-order",
	                test_ibis_source_strip_prefix_out_of_order);
	g_test_add_func("/ibis/source/strip-prefix/prefix-only",
	                test_ibis_source_strip_prefix_prefix_only);

	g_test_add_func("/ibis/source/get-prefix/none",
	                test_ibis_source_get_prefix_none);
	g_test_add_func("/ibis/source/get-prefix/single",
	                test_ibis_source_get_prefix_single);
	g_test_add_func("/ibis/source/get-prefix/multiple",
	                test_ibis_source_get_prefix_multiple);
	g_test_add_func("/ibis/source/get-prefix/out-of-order",
	                test_ibis_source_get_prefix_out_of_order);
	g_test_add_func("/ibis/source/get-prefix/prefix-only",
	                test_ibis_source_get_prefix_prefix_only);

	return g_test_run();
}
