/*
 * 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;
};

G_DEFINE_FINAL_TYPE(TestPurpleCredentialProvider,
                    test_purple_credential_provider,
                    PURPLE_TYPE_CREDENTIAL_PROVIDER);

static void
test_purple_credential_provider_init(G_GNUC_UNUSED TestPurpleCredentialProvider *provider)
{
}

static void
test_purple_credential_provider_class_init(G_GNUC_UNUSED TestPurpleCredentialProviderClass *klass)
{
}

/******************************************************************************
 * 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_error(error,
	               PURPLE_CREDENTIAL_PROVIDER_ERROR,
	               PURPLE_CREDENTIAL_PROVIDER_ERROR_VFUNC_NOT_IMPLEMENTED);
	g_clear_error(&error);

	g_assert_false(success);

	*counter = *counter + 1;
}

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

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

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

	g_main_context_iteration(NULL, FALSE);

	g_assert_cmpuint(counter, ==, 1);

	g_assert_finalize_object(provider);
	g_assert_finalize_object(account);
}

static void
test_purple_credential_provider_clear_password_finish(void) {
	PurpleCredentialProvider *provider = NULL;
	GError *error = NULL;
	GTask *task = NULL;
	gboolean success = FALSE;

	provider = g_object_new(TEST_PURPLE_TYPE_CREDENTIAL_PROVIDER, NULL);

	/* Create a dummy task that's completed to get past the normal checks. */
	task = g_task_new(G_OBJECT(provider), NULL, NULL, NULL);
	g_task_return_boolean(task, TRUE);

	success = purple_credential_provider_clear_password_finish(provider,
	                                                           G_ASYNC_RESULT(task),
	                                                           &error);
	g_assert_error(error,
	               PURPLE_CREDENTIAL_PROVIDER_ERROR,
	               PURPLE_CREDENTIAL_PROVIDER_ERROR_VFUNC_NOT_IMPLEMENTED);
	g_clear_error(&error);

	g_assert_false(success);

	/* We use g_clear_object here because there's some weird accounting from
	 * our task to get past the normal error checkers.
	 */
	g_clear_object(&task);
	g_clear_object(&provider);
}

/******************************************************************************
 * 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 success = FALSE;

	birb_assert_type(source, TEST_PURPLE_TYPE_CREDENTIAL_PROVIDER);

	success = purple_credential_provider_get_locked_finish(PURPLE_CREDENTIAL_PROVIDER(source),
	                                                       result,
	                                                       &error);
	g_assert_error(error,
	               PURPLE_CREDENTIAL_PROVIDER_ERROR,
	               PURPLE_CREDENTIAL_PROVIDER_ERROR_VFUNC_NOT_IMPLEMENTED);
	g_clear_error(&error);

	g_assert_false(success);

	*counter = *counter + 1;
}

static void
test_purple_credential_provider_get_locked_async(void) {
	PurpleCredentialProvider *provider = NULL;
	guint counter = 0;

	provider = g_object_new(TEST_PURPLE_TYPE_CREDENTIAL_PROVIDER, NULL);

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

	g_main_context_iteration(NULL, FALSE);

	g_assert_cmpuint(counter, ==, 1);

	g_assert_finalize_object(provider);
}

static void
test_purple_credential_provider_get_locked_finish(void) {
	PurpleCredentialProvider *provider = NULL;
	GError *error = NULL;
	GTask *task = NULL;
	gboolean success = FALSE;

	provider = g_object_new(TEST_PURPLE_TYPE_CREDENTIAL_PROVIDER, NULL);

	/* Create a dummy task that's completed to get past the normal checks. */
	task = g_task_new(G_OBJECT(provider), NULL, NULL, NULL);
	g_task_return_boolean(task, TRUE);

	success = purple_credential_provider_get_locked_finish(provider,
	                                                       G_ASYNC_RESULT(task),
	                                                       &error);
	g_assert_error(error,
	               PURPLE_CREDENTIAL_PROVIDER_ERROR,
	               PURPLE_CREDENTIAL_PROVIDER_ERROR_VFUNC_NOT_IMPLEMENTED);
	g_clear_error(&error);

	g_assert_false(success);

	/* We use g_clear_object here because there's some weird accounting from
	 * our task to get past the normal error checkers.
	 */
	g_clear_object(&task);
	g_clear_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_error(error,
	               PURPLE_CREDENTIAL_PROVIDER_ERROR,
	               PURPLE_CREDENTIAL_PROVIDER_ERROR_VFUNC_NOT_IMPLEMENTED);
	g_clear_error(&error);

	g_assert_cmpstr(password, ==, NULL);

	*counter = *counter + 1;
}

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

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

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

	g_main_context_iteration(NULL, FALSE);

	g_assert_cmpuint(counter, ==, 1);

	g_assert_finalize_object(provider);
	g_assert_finalize_object(account);
}

static void
test_purple_credential_provider_read_password_finish(void) {
	PurpleCredentialProvider *provider = NULL;
	GError *error = NULL;
	GTask *task = NULL;
	char *password = NULL;

	provider = g_object_new(TEST_PURPLE_TYPE_CREDENTIAL_PROVIDER, NULL);

	/* Create a dummy task that's completed to get past the normal checks. */
	task = g_task_new(G_OBJECT(provider), NULL, NULL, NULL);
	g_task_return_boolean(task, TRUE);

	password = purple_credential_provider_read_password_finish(provider,
	                                                           G_ASYNC_RESULT(task),
	                                                           &error);
	g_assert_error(error,
	               PURPLE_CREDENTIAL_PROVIDER_ERROR,
	               PURPLE_CREDENTIAL_PROVIDER_ERROR_VFUNC_NOT_IMPLEMENTED);
	g_clear_error(&error);

	g_assert_cmpstr(password, ==, NULL);

	/* We use g_clear_object here because there's some weird accounting from
	 * our task to get past the normal error checkers.
	 */
	g_clear_object(&task);
	g_clear_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_error(error,
	               PURPLE_CREDENTIAL_PROVIDER_ERROR,
	               PURPLE_CREDENTIAL_PROVIDER_ERROR_VFUNC_NOT_IMPLEMENTED);
	g_clear_error(&error);

	g_assert_false(success);

	*counter = *counter + 1;
}

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

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

	purple_credential_provider_write_password_async(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_finalize_object(provider);
	g_assert_finalize_object(account);
}

static void
test_purple_credential_provider_write_password_finish(void) {
	PurpleCredentialProvider *provider = NULL;
	GError *error = NULL;
	GTask *task = NULL;
	gboolean success = FALSE;

	provider = g_object_new(TEST_PURPLE_TYPE_CREDENTIAL_PROVIDER, NULL);

	/* Create a dummy task that's completed to get past the normal checks. */
	task = g_task_new(G_OBJECT(provider), NULL, NULL, NULL);
	g_task_return_boolean(task, TRUE);

	success = purple_credential_provider_write_password_finish(provider,
	                                                           G_ASYNC_RESULT(task),
	                                                           &error);
	g_assert_error(error,
	               PURPLE_CREDENTIAL_PROVIDER_ERROR,
	               PURPLE_CREDENTIAL_PROVIDER_ERROR_VFUNC_NOT_IMPLEMENTED);
	g_clear_error(&error);

	g_assert_false(success);

	/* We use g_clear_object here because there's some weird accounting from
	 * our task to get past the normal error checkers.
	 */
	g_clear_object(&task);
	g_clear_object(&provider);
}

/******************************************************************************
 * 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_error(error,
	               PURPLE_CREDENTIAL_PROVIDER_ERROR,
	               PURPLE_CREDENTIAL_PROVIDER_ERROR_VFUNC_NOT_IMPLEMENTED);
	g_clear_error(&error);

	g_assert_false(success);

	*counter = *counter + 1;
}

static void
test_purple_credential_provider_unlock_async(void) {
	PurpleCredentialProvider *provider = NULL;
	guint counter = 0;

	provider = g_object_new(TEST_PURPLE_TYPE_CREDENTIAL_PROVIDER, NULL);

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

	g_main_context_iteration(NULL, FALSE);

	g_assert_cmpuint(counter, ==, 1);

	g_assert_finalize_object(provider);
}

static void
test_purple_credential_provider_unlock_finish(void) {
	PurpleCredentialProvider *provider = NULL;
	GError *error = NULL;
	GTask *task = NULL;
	gboolean success = FALSE;

	provider = g_object_new(TEST_PURPLE_TYPE_CREDENTIAL_PROVIDER, NULL);

	/* Create a dummy task that's completed to get past the normal checks. */
	task = g_task_new(G_OBJECT(provider), NULL, NULL, NULL);
	g_task_return_boolean(task, TRUE);

	success = purple_credential_provider_unlock_finish(provider,
	                                                   G_ASYNC_RESULT(task),
	                                                   &error);
	g_assert_error(error,
	               PURPLE_CREDENTIAL_PROVIDER_ERROR,
	               PURPLE_CREDENTIAL_PROVIDER_ERROR_VFUNC_NOT_IMPLEMENTED);
	g_clear_error(&error);

	g_assert_false(success);

	/* We use g_clear_object here because there's some weird accounting from
	 * our task to get past the normal error checkers.
	 */
	g_clear_object(&task);
	g_clear_object(&provider);
}

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

	g_test_add_func("/credential-provider/empty/clear-password-async",
	                test_purple_credential_provider_clear_password_async);
	g_test_add_func("/credential-provider/empty/clear-password-finish",
	                test_purple_credential_provider_clear_password_finish);

	g_test_add_func("/credential-provider/empty/get-locked-async",
	                test_purple_credential_provider_get_locked_async);
	g_test_add_func("/credential-provider/empty/get-locked-finish",
	                test_purple_credential_provider_get_locked_finish);

	g_test_add_func("/credential-provider/empty/read-password-async",
	                test_purple_credential_provider_read_password_async);
	g_test_add_func("/credential-provider/empty/read-password-finish",
	                test_purple_credential_provider_read_password_finish);

	g_test_add_func("/credential-provider/empty/write-password-async",
	                test_purple_credential_provider_write_password_async);
	g_test_add_func("/credential-provider/empty/write-password-finish",
	                test_purple_credential_provider_write_password_finish);

	g_test_add_func("/credential-provider/empty/unlock-async",
	                test_purple_credential_provider_unlock_async);
	g_test_add_func("/credential-provider/empty/unlock-finish",
	                test_purple_credential_provider_unlock_finish);

	return g_test_run();
}
