/*
 * 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_handle_get_stats"

static void test(void)
{
	int logfd;
	knet_handle_t knet_h1, knet_h[2] = {0};
	struct knet_handle_stats test_byte_array[2];
	struct knet_handle_stats ref_byte_array[2];
	struct knet_handle_stats stats;

	logfd = start_logging(stdout);

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

	memset(&stats, 0, sizeof(struct knet_handle_stats));

	FAIL_ON_SUCCESS(knet_handle_get_stats(NULL, &stats, sizeof(struct knet_handle_stats)), EINVAL);


	knet_h1 = _ts_knet_handle_start(logfd, KNET_LOG_DEBUG, knet_h);

	log_test(logfd, "Test knet_handle_get_stats with NULL structure pointer");
	FAIL_ON_SUCCESS(knet_handle_get_stats(knet_h1, NULL, 0), EINVAL);

	log_test(logfd, "Test knet_handle_get_stats with small structure size");
	memset(test_byte_array, 0x55, sizeof(struct knet_handle_stats) * 2);
	memset(ref_byte_array, 0x55, sizeof(struct knet_handle_stats) * 2);
	FAIL_ON_ERR(knet_handle_get_stats(knet_h1, (struct knet_handle_stats *)test_byte_array, sizeof(size_t)));

	if (memcmp(&test_byte_array[1], ref_byte_array, sizeof(struct knet_handle_stats))) {
		log_test(logfd, "knet_handle_get_stats corrupted memory after stats structure");
		TEST_EXIT_CLEAN(FAIL);
	}

	log_test(logfd, "Test knet_handle_get_stats with valid input");
	FAIL_ON_ERR(knet_handle_get_stats(knet_h1, &stats, sizeof(struct knet_handle_stats)));

	TEST_EXIT_CLEAN(CONTINUE);
}

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

	test();

	TEST_EXIT(PASS);
}
