/*
 * Copyright (C) 2016-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 "link.h"
#include "netutils.h"
#include "test-common.h"

#define TEST_NAME "api_knet_link_get_link_list"

static void test(void)
{
	int logfd;
	knet_handle_t knet_h1, knet_h[2] = {0};
	uint8_t link_ids[KNET_MAX_LINK];
	size_t link_ids_entries = 0;
	struct sockaddr_storage lo;

	logfd = start_logging(stdout);

	memset(&link_ids, 1, sizeof(link_ids));

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

	FAIL_ON_SUCCESS(knet_link_get_link_list(NULL, 1, link_ids, &link_ids_entries), EINVAL);

	knet_h1 = _ts_knet_handle_start(logfd, KNET_LOG_DEBUG, knet_h);

	log_test(logfd, "Test knet_link_get_link_list with unconfigured host_id");
	FAIL_ON_SUCCESS(knet_link_get_link_list(knet_h1, 1, link_ids, &link_ids_entries), EINVAL);

	log_test(logfd, "Test knet_link_get_link_list with incorrect link_id");
	FAIL_ON_ERR(knet_host_add(knet_h1, 1));
	FAIL_ON_SUCCESS(knet_link_get_link_list(knet_h1, 1, NULL, &link_ids_entries), EINVAL);

	log_test(logfd, "Test knet_link_get_link_list with incorrect link_ids_entries");
	FAIL_ON_SUCCESS(knet_link_get_link_list(knet_h1, 1, link_ids, NULL), EINVAL);

	log_test(logfd, "Test knet_link_get_link_list with no links");
	FAIL_ON_ERR(knet_link_get_link_list(knet_h1, 1, link_ids, &link_ids_entries));
	if (link_ids_entries != 0) {
		log_test(logfd, "knet_link_get_link_list returned incorrect number of links");
		TEST_EXIT_CLEAN(FAIL);
	}

	log_test(logfd, "Test knet_link_get_link_list with 1 link");
	FAIL_ON_ERR(_ts_knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, 0, AF_INET, 0, &lo, logfd));
	FAIL_ON_ERR(knet_link_get_link_list(knet_h1, 1, link_ids, &link_ids_entries));
	if ((link_ids_entries != 1) || (link_ids[0] != 0)) {
		log_test(logfd, "knet_link_get_link_list returned incorrect values");
		TEST_EXIT_CLEAN(FAIL);
	}
	TEST_EXIT_CLEAN(CONTINUE);
}

int main(int argc, char *argv[])
{
	printf("[TEST] %s: Test knet link get link list\n", TEST_NAME);

	test();

	TEST_EXIT(PASS);
}
