1    	/*
2    	 * Copyright (C) 2016-2026 Red Hat, Inc.  All rights reserved.
3    	 *
4    	 * Authors: Fabio M. Di Nitto <fabbione@kronosnet.org>
5    	 *
6    	 * This software licensed under GPL-2.0+
7    	 */
8    	
9    	#include "config.h"
10   	
11   	#include <errno.h>
12   	#include <stdio.h>
13   	#include <stdlib.h>
14   	#include <string.h>
15   	#include <unistd.h>
16   	
17   	#include "libknet.h"
18   	
19   	#include "internals.h"
20   	#include "test-common.h"
21   	
22   	#define TEST_NAME "api_knet_host_set_name"
23   	
24   	static void test(void)
25   	{
26   		int logfd;
27   	
28   		logfd = start_logging(stdout);
29   		knet_handle_t knet_h1, knet_h[2] = {0};
30   		char longhostname[KNET_MAX_HOST_LEN+2];
31   	
(1) Event path: Condition "write(logfd, &_log_msg, 256UL /* sizeof (_log_msg) */) != 256UL /* sizeof (_log_msg) */", taking true branch.
32   		log_test(logfd, "Test knet_host_set_name incorrect knet_h");
33   	
(2) Event path: Condition "write(logfd, &_log_msg, 256UL /* sizeof (_log_msg) */) != 256UL /* sizeof (_log_msg) */", taking true branch.
(3) Event path: Condition "(_fos_res = knet_host_set_name(NULL, 1, "test")) == 0", taking false branch.
(4) Event path: Condition "_fos_res == -1", taking true branch.
(5) Event path: Condition "*__errno_location() != 22", taking false branch.
34   		FAIL_ON_SUCCESS(knet_host_set_name(NULL, 1, "test"), EINVAL);
35   	
36   	
37   		knet_h1 = _ts_knet_handle_start(logfd, KNET_LOG_DEBUG, knet_h);
38   	
39   	
(6) Event path: Condition "write(logfd, &_log_msg, 256UL /* sizeof (_log_msg) */) != 256UL /* sizeof (_log_msg) */", taking true branch.
40   		log_test(logfd, "Test knet_host_set_name with incorrect hostid 1");
(7) Event path: Condition "write(logfd, &_log_msg, 256UL /* sizeof (_log_msg) */) != 256UL /* sizeof (_log_msg) */", taking true branch.
(8) Event path: Condition "(_fos_res = knet_host_set_name(knet_h1, 2, "test")) == 0", taking false branch.
(9) Event path: Condition "_fos_res == -1", taking true branch.
(10) Event path: Condition "*__errno_location() != 22", taking false branch.
41   		FAIL_ON_SUCCESS(knet_host_set_name(knet_h1, 2, "test"), EINVAL);
42   	
(11) Event path: Condition "write(logfd, &_log_msg, 256UL /* sizeof (_log_msg) */) != 256UL /* sizeof (_log_msg) */", taking true branch.
43   		log_test(logfd, "Test knet_host_set_name with correct values");
(12) Event path: Condition "write(logfd, &_log_msg, 256UL /* sizeof (_log_msg) */) != 256UL /* sizeof (_log_msg) */", taking true branch.
(13) Event path: Condition "(_foe_res = knet_host_add(knet_h1, 1)) != 0", taking false branch.
44   		FAIL_ON_ERR(knet_host_add(knet_h1, 1));
(14) Event path: Condition "write(logfd, &_log_msg, 256UL /* sizeof (_log_msg) */) != 256UL /* sizeof (_log_msg) */", taking true branch.
(15) Event path: Condition "(_foe_res = knet_host_set_name(knet_h1, 1, "test")) != 0", taking false branch.
45   		FAIL_ON_ERR(knet_host_set_name(knet_h1, 1, "test"));
(16) Event path: Condition "strcmp("test", knet_h1->host_index[1]->name)", taking false branch.
46   		if (strcmp("test", knet_h1->host_index[1]->name)) {
47   			log_test(logfd, "knet_host_set_name failed to copy name");
48   			TEST_EXIT_CLEAN(FAIL);
49   		}
50   	
(17) Event path: Condition "write(logfd, &_log_msg, 256UL /* sizeof (_log_msg) */) != 256UL /* sizeof (_log_msg) */", taking true branch.
51   		log_test(logfd, "Test knet_host_set_name with correct values (name change)");
(18) Event path: Condition "write(logfd, &_log_msg, 256UL /* sizeof (_log_msg) */) != 256UL /* sizeof (_log_msg) */", taking true branch.
(19) Event path: Condition "(_foe_res = knet_host_set_name(knet_h1, 1, "tes")) != 0", taking false branch.
52   		FAIL_ON_ERR(knet_host_set_name(knet_h1, 1, "tes"));
(20) Event path: Condition "strcmp("tes", knet_h1->host_index[1]->name)", taking false branch.
53   		if (strcmp("tes", knet_h1->host_index[1]->name)) {
54   			log_test(logfd, "knet_host_set_name failed to change name");
55   			TEST_EXIT_CLEAN(FAIL);
56   		}
57   	
(21) Event path: Condition "write(logfd, &_log_msg, 256UL /* sizeof (_log_msg) */) != 256UL /* sizeof (_log_msg) */", taking true branch.
58   		log_test(logfd, "Test knet_host_set_name with NULL name");
(22) Event path: Condition "write(logfd, &_log_msg, 256UL /* sizeof (_log_msg) */) != 256UL /* sizeof (_log_msg) */", taking true branch.
(23) Event path: Condition "(_fos_res = knet_host_set_name(knet_h1, 1, NULL)) == 0", taking false branch.
(24) Event path: Condition "_fos_res == -1", taking true branch.
(25) Event path: Condition "*__errno_location() != 22", taking false branch.
59   		FAIL_ON_SUCCESS(knet_host_set_name(knet_h1, 1, NULL), EINVAL);
60   	
(26) Event path: Condition "write(logfd, &_log_msg, 256UL /* sizeof (_log_msg) */) != 256UL /* sizeof (_log_msg) */", taking true branch.
61   		log_test(logfd, "Test knet_host_set_name with duplicate name");
(27) Event path: Condition "write(logfd, &_log_msg, 256UL /* sizeof (_log_msg) */) != 256UL /* sizeof (_log_msg) */", taking true branch.
(28) Event path: Condition "(_foe_res = knet_host_add(knet_h1, 2)) != 0", taking false branch.
62   		FAIL_ON_ERR(knet_host_add(knet_h1, 2));
63   	
(29) Event path: Condition "!knet_host_set_name(knet_h1, 2, "tes")", taking false branch.
(30) Event path: Condition "*__errno_location() != 17", taking false branch.
64   		if ((!knet_host_set_name(knet_h1, 2, "tes")) || (errno != EEXIST)) {
65   			log_test(logfd, "knet_host_set_name accepted duplicated name or returned incorrect error: %s", strerror(errno));
66   			TEST_EXIT_CLEAN(FAIL);
67   		}
68   	
CID (unavailable; MK=74bc054f2b8b69f656a97199c2764932) (#1 of 1): Unchecked return value (CHECKED_RETURN):
(31) Event check_return: Calling "knet_host_remove" without checking return value (as is done elsewhere 7 out of 8 times).
Also see events: [example_checked][example_checked][example_checked][example_checked][example_assign][example_checked]
69   		knet_host_remove(knet_h1, 2);
70   	
71   		log_test(logfd, "Test knet_host_set_name with (too) long name");
72   	
73   		memset(longhostname, 'a', sizeof(longhostname));
74   		longhostname[KNET_MAX_HOST_LEN] = '\0';
75   	
76   		if ((!knet_host_set_name(knet_h1, 1, longhostname)) || (errno != EINVAL)) {
77   			log_test(logfd, "knet_host_set_name accepted invalid (too long) name or returned incorrect error: %s", strerror(errno));
78   			TEST_EXIT_CLEAN(FAIL);
79   		}
80   	
81   		TEST_EXIT_CLEAN(CONTINUE);
82   	}
83   	
84   	int main(int argc, char *argv[])
85   	{
86   		printf("[TEST] %s: Test knet host set name\n", TEST_NAME);
87   	
88   		test();
89   	
90   		TEST_EXIT(PASS);
91   	}
92