/*
 * Purple - Internet Messaging Library
 * Copyright (C) Pidgin Developers <devel@pidgin.im>
 *
 * 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 <birb.h>

#include <purple.h>

/******************************************************************************
 * Empty PurpleCredentialProvider Implementation
 *****************************************************************************/
#define TEST_PURPLE_TYPE_CREDENTIAL_PROVIDER (test_purple_credential_provider_get_type())

G_DECLARE_FINAL_TYPE(TestPurpleCredentialProvider,
                     test_purple_credential_provider,
                     TEST_PURPLE,
                     CREDENTIAL_PROVIDER,
                     PurpleCredentialProvider);

struct _TestPurpleCredentialProvider {
	PurpleCredentialProvider parent;

	guint clear_password_async;
	guint clear_password_finish;
	guint get_locked_async;
	guint get_locked_finish;
	guint read_password_async;
	guint read_password_finish;
	guint unlock_async;
	guint unlock_finish;
	guint write_password_async;
	guint write_password_finish;
};

static void
test_purple_credential_provider_clear_password_async(PurpleCredentialProvider *provider,
                                                     PurpleAccount *account,
                                                     GCancellable *cancellable,
                                                     GAsyncReadyCallback callback,
                                                     gpointer data)
{
	TestPurpleCredentialProvider *test_provider = NULL;
	GTask *task = NULL;

	birb_assert_type(provider, TEST_PURPLE_TYPE_CREDENTIAL_PROVIDER);
	birb_assert_type(account, PURPLE_TYPE_ACCOUNT);

	task = g_task_new(provider, cancellable, callback, data);
	g_task_return_boolean(task, TRUE);
	g_clear_object(&task);

	test_provider = TEST_PURPLE_CREDENTIAL_PROVIDER(provider);
	test_provider->clear_password_async += 1;
}

static gboolean
test_purple_credential_provider_clear_password_finish(PurpleCredentialProvider *provider,
                                                      GAsyncResult *result,
                                                      GError **error)
{
	TestPurpleCredentialProvider *test_provider = NULL;

	test_provider = TEST_PURPLE_CREDENTIAL_PROVIDER(provider);
	test_provider->clear_password_finish += 1;

	return g_task_propagate_boolean(G_TASK(result), error);
}

static void
test_purple_credential_provider_get_locked_async(PurpleCredentialProvider *provider,
                                                 GCancellable *cancellable,
                                                 GAsyncReadyCallback callback,
                                                 gpointer data)
{
	TestPurpleCredentialProvider *test_provider = NULL;
	GTask *task = NULL;

	birb_assert_type(provider, TEST_PURPLE_TYPE_CREDENTIAL_PROVIDER);

	task = g_task_new(provider, cancellable, callback, data);
	g_task_return_boolean(task, TRUE);
	g_clear_object(&task);

	test_provider = TEST_PURPLE_CREDENTIAL_PROVIDER(provider);
	test_provider->get_locked_async += 1;
}

static gboolean
test_purple_credential_provider_get_locked_finish(PurpleCredentialProvider *provider,
                                                  GAsyncResult *result,
                                                  GError **error)
{
	TestPurpleCredentialProvider *test_provider = NULL;

	test_provider = TEST_PURPLE_CREDENTIAL_PROVIDER(provider);
	test_provider->get_locked_finish += 1;

	return g_task_propagate_boolean(G_TASK(result), error);
}

static void
test_purple_credential_provider_read_password_async(PurpleCredentialProvider *provider,
                                                    PurpleAccount *account,
                                                    GCancellable *cancellable,
                                                    GAsyncReadyCallback callback,
                                                    gpointer data)
{
	TestPurpleCredentialProvider *test_provider = NULL;
	GTask *task = NULL;

	birb_assert_type(provider, TEST_PURPLE_TYPE_CREDENTIAL_PROVIDER);
	birb_assert_type(account, PURPLE_TYPE_ACCOUNT);

	task = g_task_new(provider, cancellable, callback, data);
	g_task_return_pointer(task, g_strdup("hunter2"), g_free);
	g_clear_object(&task);

	test_provider = TEST_PURPLE_CREDENTIAL_PROVIDER(provider);
	test_provider->read_password_async += 1;
}

static char *
test_purple_credential_provider_read_password_finish(PurpleCredentialProvider *provider,
                                                     GAsyncResult *result,
                                                     GError **error)
{
	TestPurpleCredentialProvider *test_provider = NULL;

	test_provider = TEST_PURPLE_CREDENTIAL_PROVIDER(provider);
	test_provider->read_password_finish += 1;

	return g_task_propagate_pointer(G_TASK(result), error);
}

static void
test_purple_credential_provider_unlock_async(PurpleCredentialProvider *provider,
                                             GCancellable *cancellable,
                                             GAsyncReadyCallback callback,
                                             gpointer data)
{
	TestPurpleCredentialProvider *test_provider = NULL;
	GTask *task = NULL;

	birb_assert_type(provider, TEST_PURPLE_TYPE_CREDENTIAL_PROVIDER);

	task = g_task_new(provider, cancellable, callback, data);
	g_task_return_boolean(task, TRUE);
	g_clear_object(&task);

	test_provider = TEST_PURPLE_CREDENTIAL_PROVIDER(provider);
	test_provider->unlock_async += 1;
}

static gboolean
test_purple_credential_provider_unlock_finish(PurpleCredentialProvider *provider,
                                              GAsyncResult *result,
                                              GError **error)
{
	TestPurpleCredentialProvider *test_provider = NULL;

	test_provider = TEST_PURPLE_CREDENTIAL_PROVIDER(provider);
	test_provider->unlock_finish += 1;

	return g_task_propagate_boolean(G_TASK(result), error);
}

static void
test_purple_credential_provider_write_password_async(PurpleCredentialProvider *provider,
                                                     PurpleAccount *account,
                                                     const char *password,
                                                     GCancellable *cancellable,
                                                     GAsyncReadyCallback callback,
                                                     gpointer data)
{
	TestPurpleCredentialProvider *test_provider = NULL;
	GTask *task = NULL;

	birb_assert_type(provider, TEST_PURPLE_TYPE_CREDENTIAL_PROVIDER);
	birb_assert_type(account, PURPLE_TYPE_ACCOUNT);

	g_assert_cmpstr(password, ==, "hunter2");

	task = g_task_new(provider, cancellable, callback, data);
	g_task_return_boolean(task, TRUE);
	g_clear_object(&task);

	test_provider = TEST_PURPLE_CREDENTIAL_PROVIDER(provider);
	test_provider->write_password_async += 1;
}

static gboolean
test_purple_credential_provider_write_password_finish(PurpleCredentialProvider *provider,
                                                      GAsyncResult *result,
                                                      GError **error)
{
	TestPurpleCredentialProvider *test_provider = NULL;

	test_provider = TEST_PURPLE_CREDENTIAL_PROVIDER(provider);
	test_provider->write_password_finish += 1;

	return g_task_propagate_boolean(G_TASK(result), error);
}

G_DEFINE_FINAL_TYPE(TestPurpleCredentialProvider,
                    test_purple_credential_provider,
                    PURPLE_TYPE_CREDENTIAL_PROVIDER);

static void
test_purple_credential_provider_init(TestPurpleCredentialProvider *provider) {
	provider->clear_password_async = 0;
	provider->clear_password_finish = 0;
	provider->get_locked_async = 0;
	provider->get_locked_finish = 0;
	provider->read_password_async = 0;
	provider->read_password_finish = 0;
	provider->unlock_async = 0;
	provider->unlock_finish = 0;
	provider->write_password_async = 0;
	provider->write_password_finish = 0;
}

static void
test_purple_credential_provider_class_init(TestPurpleCredentialProviderClass *klass)
{
	PurpleCredentialProviderClass *provider_class = NULL;

	provider_class = PURPLE_CREDENTIAL_PROVIDER_CLASS(klass);
	provider_class->clear_password_async =
		test_purple_credential_provider_clear_password_async;
	provider_class->clear_password_finish =
		test_purple_credential_provider_clear_password_finish;
	provider_class->get_locked_async =
		test_purple_credential_provider_get_locked_async;
	provider_class->get_locked_finish =
		test_purple_credential_provider_get_locked_finish;
	provider_class->read_password_async =
		test_purple_credential_provider_read_password_async;
	provider_class->read_password_finish =
		test_purple_credential_provider_read_password_finish;
	provider_class->unlock_async =
		test_purple_credential_provider_unlock_async;
	provider_class->unlock_finish =
		test_purple_credential_provider_unlock_finish;
	provider_class->write_password_async =
		test_purple_credential_provider_write_password_async;
	provider_class->write_password_finish =
		test_purple_credential_provider_write_password_finish;
}

/******************************************************************************
 * clear_password tests
 *****************************************************************************/
static void
test_purple_credential_provider_clear_password_cb(GObject *source,
                                                  GAsyncResult *result,
                                                  gpointer data)
{
	GError *error = NULL;
	guint *counter = data;
	gboolean success = FALSE;

	birb_assert_type(source, TEST_PURPLE_TYPE_CREDENTIAL_PROVIDER);

	success = purple_credential_provider_clear_password_finish(PURPLE_CREDENTIAL_PROVIDER(source),
	                                                           result,
	                                                           &error);
	g_assert_no_error(error);

	g_assert_true(success);

	*counter = *counter + 1;
}

static void
test_purple_credential_provider_clear_password(void) {
	TestPurpleCredentialProvider *provider = NULL;
	PurpleAccount *account = NULL;
	PurpleCredentialProvider *purple_provider = NULL;
	guint counter = 0;

	provider = g_object_new(TEST_PURPLE_TYPE_CREDENTIAL_PROVIDER, NULL);
	purple_provider = PURPLE_CREDENTIAL_PROVIDER(provider);
	account = purple_account_new("test-account", "test");

	purple_credential_provider_clear_password_async(purple_provider, account,
	                                                NULL,
	                                                test_purple_credential_provider_clear_password_cb,
	                                                &counter);

	g_main_context_iteration(NULL, FALSE);

	g_assert_cmpuint(counter, ==, 1);

	g_assert_cmpuint(provider->clear_password_async, ==, 1);
	g_assert_cmpuint(provider->clear_password_finish, ==, 1);
	g_assert_cmpuint(provider->get_locked_async, ==, 0);
	g_assert_cmpuint(provider->get_locked_finish, ==, 0);
	g_assert_cmpuint(provider->read_password_async, ==, 0);
	g_assert_cmpuint(provider->read_password_finish, ==, 0);
	g_assert_cmpuint(provider->unlock_async, ==, 0);
	g_assert_cmpuint(provider->unlock_finish, ==, 0);
	g_assert_cmpuint(provider->write_password_async, ==, 0);
	g_assert_cmpuint(provider->write_password_finish, ==, 0);

	g_assert_finalize_object(provider);
	g_assert_finalize_object(account);
}

/******************************************************************************
 * get_locked tests
 *****************************************************************************/
static void
test_purple_credential_provider_get_locked_cb(GObject *source,
                                              GAsyncResult *result,
                                              gpointer data)
{
	GError *error = NULL;
	guint *counter = data;
	gboolean locked = FALSE;

	birb_assert_type(source, TEST_PURPLE_TYPE_CREDENTIAL_PROVIDER);

	locked = purple_credential_provider_get_locked_finish(PURPLE_CREDENTIAL_PROVIDER(source),
	                                                      result,
	                                                      &error);
	g_assert_no_error(error);

	g_assert_true(locked);

	*counter = *counter + 1;
}

static void
test_purple_credential_provider_get_locked(void) {
	TestPurpleCredentialProvider *provider = NULL;
	PurpleCredentialProvider *purple_provider = NULL;
	guint counter = 0;

	provider = g_object_new(TEST_PURPLE_TYPE_CREDENTIAL_PROVIDER, NULL);
	purple_provider = PURPLE_CREDENTIAL_PROVIDER(provider);

	purple_credential_provider_get_locked_async(purple_provider, NULL,
	                                            test_purple_credential_provider_get_locked_cb,
	                                            &counter);

	g_main_context_iteration(NULL, FALSE);

	g_assert_cmpuint(counter, ==, 1);

	g_assert_cmpuint(provider->clear_password_async, ==, 0);
	g_assert_cmpuint(provider->clear_password_finish, ==, 0);
	g_assert_cmpuint(provider->get_locked_async, ==, 1);
	g_assert_cmpuint(provider->get_locked_finish, ==, 1);
	g_assert_cmpuint(provider->read_password_async, ==, 0);
	g_assert_cmpuint(provider->read_password_finish, ==, 0);
	g_assert_cmpuint(provider->unlock_async, ==, 0);
	g_assert_cmpuint(provider->unlock_finish, ==, 0);
	g_assert_cmpuint(provider->write_password_async, ==, 0);
	g_assert_cmpuint(provider->write_password_finish, ==, 0);

	g_assert_finalize_object(provider);
}

/******************************************************************************
 * read_password tests
 *****************************************************************************/
static void
test_purple_credential_provider_read_password_cb(GObject *source,
                                                 GAsyncResult *result,
                                                 gpointer data)
{
	GError *error = NULL;
	guint *counter = data;
	char *password = NULL;

	birb_assert_type(source, TEST_PURPLE_TYPE_CREDENTIAL_PROVIDER);

	password = purple_credential_provider_read_password_finish(PURPLE_CREDENTIAL_PROVIDER(source),
	                                                           result,
	                                                           &error);
	g_assert_no_error(error);

	g_assert_cmpstr(password, ==, "hunter2");
	g_clear_pointer(&password, g_free);

	*counter = *counter + 1;
}

static void
test_purple_credential_provider_read_password(void) {
	TestPurpleCredentialProvider *provider = NULL;
	PurpleAccount *account = NULL;
	PurpleCredentialProvider *purple_provider = NULL;
	guint counter = 0;

	provider = g_object_new(TEST_PURPLE_TYPE_CREDENTIAL_PROVIDER, NULL);
	purple_provider = PURPLE_CREDENTIAL_PROVIDER(provider);
	account = purple_account_new("test-account", "test");

	purple_credential_provider_read_password_async(purple_provider, account,
	                                               NULL,
	                                               test_purple_credential_provider_read_password_cb,
	                                               &counter);

	g_main_context_iteration(NULL, FALSE);

	g_assert_cmpuint(counter, ==, 1);

	g_assert_cmpuint(provider->clear_password_async, ==, 0);
	g_assert_cmpuint(provider->clear_password_finish, ==, 0);
	g_assert_cmpuint(provider->get_locked_async, ==, 0);
	g_assert_cmpuint(provider->get_locked_finish, ==, 0);
	g_assert_cmpuint(provider->read_password_async, ==, 1);
	g_assert_cmpuint(provider->read_password_finish, ==, 1);
	g_assert_cmpuint(provider->unlock_async, ==, 0);
	g_assert_cmpuint(provider->unlock_finish, ==, 0);
	g_assert_cmpuint(provider->write_password_async, ==, 0);
	g_assert_cmpuint(provider->write_password_finish, ==, 0);

	g_assert_finalize_object(provider);
	g_assert_finalize_object(account);
}

/******************************************************************************
 * unlock tests
 *****************************************************************************/
static void
test_purple_credential_provider_unlock_cb(GObject *source,
                                          GAsyncResult *result,
                                          gpointer data)
{
	GError *error = NULL;
	guint *counter = data;
	gboolean success = FALSE;

	birb_assert_type(source, TEST_PURPLE_TYPE_CREDENTIAL_PROVIDER);

	success = purple_credential_provider_unlock_finish(PURPLE_CREDENTIAL_PROVIDER(source),
	                                                   result,
	                                                   &error);
	g_assert_no_error(error);

	g_assert_true(success);

	*counter = *counter + 1;
}

static void
test_purple_credential_provider_unlock(void) {
	TestPurpleCredentialProvider *provider = NULL;
	PurpleCredentialProvider *purple_provider = NULL;
	guint counter = 0;

	provider = g_object_new(TEST_PURPLE_TYPE_CREDENTIAL_PROVIDER, NULL);
	purple_provider = PURPLE_CREDENTIAL_PROVIDER(provider);

	purple_credential_provider_unlock_async(purple_provider, NULL,
	                                        test_purple_credential_provider_unlock_cb,
	                                        &counter);

	g_main_context_iteration(NULL, FALSE);

	g_assert_cmpuint(counter, ==, 1);

	g_assert_cmpuint(provider->clear_password_async, ==, 0);
	g_assert_cmpuint(provider->clear_password_finish, ==, 0);
	g_assert_cmpuint(provider->get_locked_async, ==, 0);
	g_assert_cmpuint(provider->get_locked_finish, ==, 0);
	g_assert_cmpuint(provider->read_password_async, ==, 0);
	g_assert_cmpuint(provider->read_password_finish, ==, 0);
	g_assert_cmpuint(provider->unlock_async, ==, 1);
	g_assert_cmpuint(provider->unlock_finish, ==, 1);
	g_assert_cmpuint(provider->write_password_async, ==, 0);
	g_assert_cmpuint(provider->write_password_finish, ==, 0);

	g_assert_finalize_object(provider);
}

/******************************************************************************
 * write_password tests
 *****************************************************************************/
static void
test_purple_credential_provider_write_password_cb(GObject *source,
                                                  GAsyncResult *result,
                                                  gpointer data)
{
	GError *error = NULL;
	guint *counter = data;
	gboolean success = FALSE;

	birb_assert_type(source, TEST_PURPLE_TYPE_CREDENTIAL_PROVIDER);

	success = purple_credential_provider_write_password_finish(PURPLE_CREDENTIAL_PROVIDER(source),
	                                                           result,
	                                                           &error);
	g_assert_no_error(error);

	g_assert_true(success);

	*counter = *counter + 1;
}

static void
test_purple_credential_provider_write_password(void) {
	TestPurpleCredentialProvider *provider = NULL;
	PurpleAccount *account = NULL;
	PurpleCredentialProvider *purple_provider = NULL;
	guint counter = 0;

	provider = g_object_new(TEST_PURPLE_TYPE_CREDENTIAL_PROVIDER, NULL);
	purple_provider = PURPLE_CREDENTIAL_PROVIDER(provider);
	account = purple_account_new("test-account", "test");

	purple_credential_provider_write_password_async(purple_provider, account,
	                                                "hunter2", NULL,
	                                                test_purple_credential_provider_write_password_cb,
	                                                &counter);

	g_main_context_iteration(NULL, FALSE);

	g_assert_cmpuint(counter, ==, 1);

	g_assert_cmpuint(provider->clear_password_async, ==, 0);
	g_assert_cmpuint(provider->clear_password_finish, ==, 0);
	g_assert_cmpuint(provider->get_locked_async, ==, 0);
	g_assert_cmpuint(provider->get_locked_finish, ==, 0);
	g_assert_cmpuint(provider->read_password_async, ==, 0);
	g_assert_cmpuint(provider->read_password_finish, ==, 0);
	g_assert_cmpuint(provider->unlock_async, ==, 0);
	g_assert_cmpuint(provider->unlock_finish, ==, 0);
	g_assert_cmpuint(provider->write_password_async, ==, 1);
	g_assert_cmpuint(provider->write_password_finish, ==, 1);

	g_assert_finalize_object(provider);
	g_assert_finalize_object(account);
}

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

	g_test_add_func("/credential-provider/normal/clear-password",
	                test_purple_credential_provider_clear_password);
	g_test_add_func("/credential-provider/normal/get-locked",
	                test_purple_credential_provider_get_locked);
	g_test_add_func("/credential-provider/normal/read-password",
	                test_purple_credential_provider_read_password);
	g_test_add_func("/credential-provider/normal/unlock",
	                test_purple_credential_provider_unlock);
	g_test_add_func("/credential-provider/normal/write-password",
	                test_purple_credential_provider_write_password);

	return g_test_run();
}
