/*
 * Copyright (C) 2020-2026 Red Hat, Inc.  All rights reserved.
 *
 * Authors: Fabio M. Di Nitto <fabbione@kronosnet.org>
 *
 * This software licensed under GPL-2.0+
 */

#include "config.h"

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "libknet.h"

#include "internals.h"
#include "crypto_model.h"
#include "test-common.h"

#define TEST_NAME "api_knet_handle_crypto_use_config"

static void test(const char *model, const char *model2)
{
	int logfd;
	knet_handle_t knet_h1, knet_h[2] = {0};
	struct knet_handle_crypto_cfg knet_handle_crypto_cfg;

	logfd = start_logging(stdout);

	memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg));

	log_test(logfd, "Test knet_handle_crypto_use_config incorrect knet_h");

	FAIL_ON_SUCCESS(knet_handle_crypto_use_config(NULL, 1), EINVAL);

	knet_h1 = _ts_knet_handle_start(logfd, KNET_LOG_DEBUG, knet_h);

	log_test(logfd, "Test knet_handle_crypto_use_config with invalid config num");
	FAIL_ON_SUCCESS(knet_handle_crypto_use_config(knet_h1, KNET_MAX_CRYPTO_INSTANCES + 1), EINVAL);

	log_test(logfd, "Test knet_handle_crypto_use_config with un-initialized cfg");
	FAIL_ON_SUCCESS(knet_handle_crypto_use_config(knet_h1, 1), EINVAL);
	FAIL_ON_SUCCESS(knet_handle_crypto_use_config(knet_h1, 2), EINVAL);

	log_test(logfd, "Test knet_handle_crypto_set_config with %s/aes128/sha1 and normal key", model);
	memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg));
	strncpy(knet_handle_crypto_cfg.crypto_model, model, sizeof(knet_handle_crypto_cfg.crypto_model) - 1);
	strncpy(knet_handle_crypto_cfg.crypto_cipher_type, "aes128", sizeof(knet_handle_crypto_cfg.crypto_cipher_type) - 1);
	strncpy(knet_handle_crypto_cfg.crypto_hash_type, "sha1", sizeof(knet_handle_crypto_cfg.crypto_hash_type) - 1);
	knet_handle_crypto_cfg.private_key_len = 2000;
	FAIL_ON_ERR(knet_handle_crypto_set_config(knet_h1, &knet_handle_crypto_cfg, 1));

	log_test(logfd, "Test knet_handle_crypto_use_config with un-initialized cfg (part 2)");
	FAIL_ON_SUCCESS(knet_handle_crypto_use_config(knet_h1, 2), EINVAL);
	FAIL_ON_ERR(knet_handle_crypto_use_config(knet_h1, 1));

	log_test(logfd, "Test knet_handle_crypto_set_config for second config with %s/aes128/sha1 and normal key", model);
	memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg));
	strncpy(knet_handle_crypto_cfg.crypto_model, model, sizeof(knet_handle_crypto_cfg.crypto_model) - 1);
	strncpy(knet_handle_crypto_cfg.crypto_cipher_type, "aes128", sizeof(knet_handle_crypto_cfg.crypto_cipher_type) - 1);
	strncpy(knet_handle_crypto_cfg.crypto_hash_type, "sha1", sizeof(knet_handle_crypto_cfg.crypto_hash_type) - 1);
	knet_handle_crypto_cfg.private_key_len = 2000;
	FAIL_ON_ERR(knet_handle_crypto_set_config(knet_h1, &knet_handle_crypto_cfg, 2));

	log_test(logfd, "Test knet_handle_crypto_use_config with valid data");
	FAIL_ON_ERR(knet_handle_crypto_use_config(knet_h1, 2));
	if (knet_h1->crypto_in_use_config != 2) {
		log_test(logfd, "knet_handle_crypto_set_config failed to set crypto in-use config to 2");
		TEST_EXIT_CLEAN(FAIL);
	}

	log_test(logfd, "Shutdown crypto");

	log_test(logfd, "Test knet_handle_crypto_use_config with valid data");

	FAIL_ON_ERR(knet_handle_crypto_use_config(knet_h1, 0));
	if (knet_h1->crypto_in_use_config != 0) {
		log_test(logfd, "knet_handle_crypto_set_config failed to set crypto in-use config to 2");
		TEST_EXIT_CLEAN(FAIL);
	}

	memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg));
	strncpy(knet_handle_crypto_cfg.crypto_model, "none", sizeof(knet_handle_crypto_cfg.crypto_model) - 1);
	strncpy(knet_handle_crypto_cfg.crypto_cipher_type, "none", sizeof(knet_handle_crypto_cfg.crypto_cipher_type) - 1);
	strncpy(knet_handle_crypto_cfg.crypto_hash_type, "none", sizeof(knet_handle_crypto_cfg.crypto_hash_type) - 1);
	knet_handle_crypto_cfg.private_key_len = 2000;

	FAIL_ON_ERR(knet_handle_crypto_set_config(knet_h1, &knet_handle_crypto_cfg, 1));
	if (knet_h1->crypto_instance[1]) {
		log_test(logfd, "knet_handle_crypto_set_config failed to wipe first config but reported success");
		TEST_EXIT_CLEAN(FAIL);
	}

	FAIL_ON_ERR(knet_handle_crypto_set_config(knet_h1, &knet_handle_crypto_cfg, 2));
	if (knet_h1->crypto_instance[2]) {
		log_test(logfd, "knet_handle_crypto_set_config failed to wipe first config but reported success");
		TEST_EXIT_CLEAN(FAIL);
	}
	TEST_EXIT_CLEAN(CONTINUE);
}

int main(int argc, char *argv[])
{
	struct knet_crypto_info crypto_list[16];
	size_t crypto_list_entries;
	size_t i;

	printf("[TEST] %s: Test knet handle crypto use config\n", TEST_NAME);

	memset(crypto_list, 0, sizeof(crypto_list));

	if (knet_get_crypto_list(crypto_list, &crypto_list_entries) < 0) {
		printf("knet_get_crypto_list failed: %s\n", strerror(errno));
		TEST_EXIT(FAIL);
	}

	if (crypto_list_entries == 0) {
		printf("no crypto modules detected. Skipping\n");
		TEST_EXIT(SKIP);
	}

	for (i=0; i < crypto_list_entries; i++) {
		test(crypto_list[i].name, crypto_list[0].name);
	}

	TEST_EXIT(PASS);
}
