/*
 * Copyright (C) 2023-2025 Hasl Developers
 *
 * 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.1 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 <hasl.h>

static struct {
	const char *decoded;
	const char *encoded;
} test_data[] = {
	{"", ""},
	{"plain", "plain"},
	{",", "=2C"},
	{"=", "=3D"},
	{",=", "=2C=3D"},
	{"foo,bar", "foo=2Cbar"},
	{"baz=quux", "baz=3Dquux"},
	{NULL, NULL},
};

/******************************************************************************
 * Tests
 *****************************************************************************/
static void
test_hasl_saslname_encode_null(void) {
	g_assert_null(hasl_saslname_encode(NULL));
}

static void
test_hasl_saslname_encode_normal(void) {
	for(guint i = 0; test_data[i].decoded != NULL; i++) {
		char *encoded = hasl_saslname_encode(test_data[i].decoded);

		g_assert_cmpstr(encoded, ==, test_data[i].encoded);

		g_clear_pointer(&encoded, g_free);
	}
}

static void
test_hasl_saslname_decode_null(void) {
	g_assert_null(hasl_saslname_decode(NULL));
}

static void
test_hasl_saslname_decode_normal(void) {
	for(guint i = 0; test_data[i].encoded != NULL; i++) {
		char *decoded = hasl_saslname_decode(test_data[i].encoded);

		g_assert_cmpstr(decoded, ==, test_data[i].decoded);

		g_clear_pointer(&decoded, g_free);
	}
}

static void
test_hasl_saslname_decode_invalid(void) {
	char *decoded = NULL;

	decoded = hasl_saslname_decode("=");
	g_assert_cmpstr(decoded, ==, "");
	g_clear_pointer(&decoded, g_free);

	decoded = hasl_saslname_decode("=2");
	g_assert_cmpstr(decoded, ==, "2");
	g_clear_pointer(&decoded, g_free);

	decoded = hasl_saslname_decode("=3");
	g_assert_cmpstr(decoded, ==, "3");
	g_clear_pointer(&decoded, g_free);

	decoded = hasl_saslname_decode(",");
	g_assert_cmpstr(decoded, ==, "");
	g_clear_pointer(&decoded, g_free);

	decoded = hasl_saslname_decode(",2");
	g_assert_cmpstr(decoded, ==, "2");
	g_clear_pointer(&decoded, g_free);
}

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

	g_test_set_nonfatal_assertions();

	g_test_add_func("/hasl/saslname/encode/null",
	                test_hasl_saslname_encode_null);
	g_test_add_func("/hasl/saslname/encode/normal",
	                test_hasl_saslname_encode_normal);

	g_test_add_func("/hasl/saslname/decode/null",
	                test_hasl_saslname_decode_null);
	g_test_add_func("/hasl/saslname/decode/normal",
	                test_hasl_saslname_decode_normal);
	g_test_add_func("/hasl/saslname/decode/invalid",
	                test_hasl_saslname_decode_invalid);

	return g_test_run();
}
