1    	/*
2    	 * Copyright (C) 2016-2025 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   	static void test(void)
23   	{
24   		knet_handle_t knet_h1, knet_h[2];
25   		int logfds[2];
26   		int res;
27   		char longhostname[KNET_MAX_HOST_LEN+2];
28   	
29   		printf("Test knet_host_set_name incorrect knet_h\n");
30   	
(1) Event cond_false: Condition "!knet_host_set_name(NULL, 1, "test")", taking false branch.
(2) Event cond_false: Condition "*__errno_location() != 22", taking false branch.
31   		if ((!knet_host_set_name(NULL, 1, "test")) || (errno != EINVAL)) {
32   			printf("knet_host_set_name accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno));
33   			exit(FAIL);
(3) Event if_end: End of if statement.
34   		}
35   	
36   		setup_logpipes(logfds);
37   	
38   		knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h);
39   	
40   		flush_logs(logfds[0], stdout);
41   	
42   		printf("Test knet_host_set_name with incorrect hostid 1\n");
(4) Event cond_false: Condition "(res = knet_host_set_name(knet_h1, 2, "test")) == 0", taking false branch.
(5) Event cond_true: Condition "res == -1", taking true branch.
(6) Event cond_false: Condition "*__errno_location() != 22", taking false branch.
(7) Event else_branch: Reached else branch.
43   		FAIL_ON_SUCCESS(knet_host_set_name(knet_h1, 2, "test"), EINVAL);
44   	
45   		printf("Test knet_host_set_name with correct values\n");
(8) Event cond_false: Condition "(res = knet_host_add(knet_h1, 1)) != 0", taking false branch.
(9) Event else_branch: Reached else branch.
46   		FAIL_ON_ERR(knet_host_add(knet_h1, 1));
(10) Event cond_false: Condition "(res = knet_host_set_name(knet_h1, 1, "test")) != 0", taking false branch.
(11) Event else_branch: Reached else branch.
47   		FAIL_ON_ERR(knet_host_set_name(knet_h1, 1, "test"));
(12) Event cond_false: Condition "strcmp("test", knet_h1->host_index[1]->name)", taking false branch.
48   		if (strcmp("test", knet_h1->host_index[1]->name)) {
49   			printf("knet_host_set_name failed to copy name\n");
50   			CLEAN_EXIT(FAIL);
(13) Event if_end: End of if statement.
51   		}
52   	
53   		printf("Test knet_host_set_name with correct values (name change)\n");
(14) Event cond_false: Condition "(res = knet_host_set_name(knet_h1, 1, "tes")) != 0", taking false branch.
(15) Event else_branch: Reached else branch.
54   		FAIL_ON_ERR(knet_host_set_name(knet_h1, 1, "tes"));
(16) Event cond_false: Condition "strcmp("tes", knet_h1->host_index[1]->name)", taking false branch.
55   		if (strcmp("tes", knet_h1->host_index[1]->name)) {
56   			printf("knet_host_set_name failed to change name\n");
57   			CLEAN_EXIT(FAIL);
(17) Event if_end: End of if statement.
58   		}
59   	
60   		printf("Test knet_host_set_name with NULL name\n");
(18) Event cond_false: Condition "(res = knet_host_set_name(knet_h1, 1, NULL)) == 0", taking false branch.
(19) Event cond_true: Condition "res == -1", taking true branch.
(20) Event cond_false: Condition "*__errno_location() != 22", taking false branch.
(21) Event else_branch: Reached else branch.
61   		FAIL_ON_SUCCESS(knet_host_set_name(knet_h1, 1, NULL), EINVAL);
62   	
63   		printf("Test knet_host_set_name with duplicate name\n");
(22) Event cond_false: Condition "(res = knet_host_add(knet_h1, 2)) != 0", taking false branch.
(23) Event else_branch: Reached else branch.
64   		FAIL_ON_ERR(knet_host_add(knet_h1, 2));
65   	
(24) Event cond_false: Condition "!knet_host_set_name(knet_h1, 2, "tes")", taking false branch.
(25) Event cond_false: Condition "*__errno_location() != 17", taking false branch.
66   		if ((!knet_host_set_name(knet_h1, 2, "tes")) || (errno != EEXIST)) {
67   			printf("knet_host_set_name accepted duplicated name or returned incorrect error: %s\n", strerror(errno));
68   			CLEAN_EXIT(FAIL);
(26) Event if_end: End of if statement.
69   		}
70   	
(27) 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]
71   		knet_host_remove(knet_h1, 2);
72   		flush_logs(logfds[0], stdout);
73   	
74   		printf("Test knet_host_set_name with (too) long name\n");
75   	
76   		memset(longhostname, 'a', sizeof(longhostname));
77   		longhostname[KNET_MAX_HOST_LEN] = '\0';
78   	
79   		if ((!knet_host_set_name(knet_h1, 1, longhostname)) || (errno != EINVAL)) {
80   			printf("knet_host_set_name accepted invalid (too long) name or returned incorrect error: %s\n", strerror(errno));
81   			CLEAN_EXIT(FAIL);
82   		}
83   	
84   		CLEAN_EXIT(CONTINUE);
85   	}
86   	
87   	int main(int argc, char *argv[])
88   	{
89   		test();
90   	
91   		return PASS;
92   	}
93