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 #include "internals.h"
19
20 #include "test-common.h"
21
22 #define TESTNODES 1
23 static void test(void)
24 {
25 knet_handle_t knet_h1, knet_h[2];
26 int logfds[2];
27 int res;
28
29 setup_logpipes(logfds);
30
31 printf("Test knet_handle_free with invalid knet_h (part 1)\n");
32 if ((!knet_handle_free(NULL)) || (errno != EINVAL)) {
33 printf("knet_handle_free failed to detect invalid parameter\n");
34 exit(FAIL);
35 }
36
37 knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h);
38
39 printf("Test knet_handle_free with one host configured\n");
40 FAIL_ON_ERR(knet_host_add(knet_h1, 1));
41
42 if ((!knet_handle_free(knet_h1)) || (errno != EBUSY)) {
43 CLEAN_EXIT(FAIL);
44 }
45
46 FAIL_ON_ERR(knet_host_remove(knet_h1, 1));
47
48 printf("Test knet_handle_free with invalid knet_h (part 2)\n");
49 // coverity[BAD_FREE:SUPPRESS] - deliberate bad handle
50 FAIL_ON_SUCCESS(knet_handle_free(knet_h1 + 1), EINVAL);
51
52 FAIL_ON_ERR(knet_handle_free(knet_h1));
53
54 CLEAN_EXIT(CONTINUE);
55 }
56
57 int main(int argc, char *argv[])
58 {
59 test();
60
61 return PASS;
62 }
63