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