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 "netutils.h"
20 #include "test-common.h"
21
22 #define TEST_NAME "api_knet_host_remove"
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 knet_node_id_t host_ids[KNET_MAX_HOST];
31 size_t host_ids_entries;
32 struct sockaddr_storage lo;
33
34 log_test(logfd, "Test knet_host_add incorrect knet_h");
35
36 FAIL_ON_SUCCESS(knet_host_remove(NULL, 1), EINVAL);
37
38
39 knet_h1 = _ts_knet_handle_start(logfd, KNET_LOG_DEBUG, knet_h);
40
41
42 log_test(logfd, "Test knet_host_remove with unconfigured host_id");
43 FAIL_ON_SUCCESS(knet_host_remove(knet_h1, 1), EINVAL);
44 FAIL_ON_ERR(knet_host_add(knet_h1, 1));
45
46 log_test(logfd, "Test knet_host_remove with configured host_id and links");
47 FAIL_ON_ERR(_ts_knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, 0, AF_INET, 1, &lo, logfd));
48 FAIL_ON_ERR(knet_link_set_enable(knet_h1, 1, 0, 1));
49
50 if ((!knet_host_remove(knet_h1, 1)) || (errno != EBUSY)) {
51 log_test(logfd, "knet_host_remove accepted invalid request to remove host with link enabled or returned incorrect error: %s", strerror(errno));
52 TEST_EXIT_CLEAN(FAIL);
53 }
54
55 FAIL_ON_ERR(knet_link_set_enable(knet_h1, 1, 0, 0));
56 FAIL_ON_ERR(knet_link_clear_config(knet_h1, 1, 0));
57
58 log_test(logfd, "Test knet_host_remove with configured host_id (no links)");
59 FAIL_ON_ERR(knet_host_remove(knet_h1, 1));
60
61 FAIL_ON_ERR(knet_host_get_host_list(knet_h1, host_ids, &host_ids_entries));
62
63 if (host_ids_entries) {
64 log_test(logfd, "Too many hosts?");
65 TEST_EXIT_CLEAN(FAIL);
66 }
67
68 TEST_EXIT_CLEAN(CONTINUE);
69 }
70
71 int main(int argc, char *argv[])
72 {
73 printf("[TEST] %s: Test knet host remove\n", TEST_NAME);
74
75 test();
76
77 TEST_EXIT(PASS);
78 }
79