Debian Codegraph @

test/config/hostnqn-order.c

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /**
3 * This file is part of libnvme.
4 * Copyright (c) 2024 Daniel Wagner, SUSE LLC
5 */
6
7 #include <string.h>
8 #include <stdbool.h>
9 #include <stdlib.h>
10 #include <errno.h>
11
12 #include <libnvme.h>
13
14 static bool command_line(void)
15 {
16 bool pass = false;
17 nvme_root_t r;
18 int err;
19 char *hostnqn, *hostid, *hnqn, *hid;
20
21 r = nvme_create_root(stderr, LOG_ERR);
22 if (!r)
23 return false;
24
25 err = nvme_scan_topology(r, NULL, NULL);
26 if (err) {
27 if (errno != ENOENT)
28 goto out;
29 }
30
31 hostnqn = "nqn.2014-08.org.nvmexpress:uuid:ce4fee3e-c02c-11ee-8442-830d068a36c6";
32 hostid = "ce4fee3e-c02c-11ee-8442-830d068a36c6";
33
34 err = nvme_host_get_ids(r, hostnqn, hostid, &hnqn, &hid);
35 if (err)
36 goto out;
37
38 if (strcmp(hostnqn, hnqn)) {
39 printf("json config hostnqn '%s' does not match '%s'\n", hostnqn, hnqn);
40 goto out;
41 }
42 if (strcmp(hostid, hid)) {
43 printf("json config hostid '%s' does not match '%s'\n", hostid, hid);
44 goto out;
45 }
46
47 free(hnqn);
48 free(hid);
49
50 pass = true;
51
52 out:
53 nvme_free_tree(r);
54 return pass;
55 }
56
57 static bool json_config(char *file)
58 {
59 bool pass = false;
60 nvme_root_t r;
61 int err;
62 char *hostnqn, *hostid, *hnqn, *hid;
63
64 setenv("LIBNVME_HOSTNQN", "", 1);
65 setenv("LIBNVME_HOSTID", "", 1);
66
67 r = nvme_create_root(stderr, LOG_ERR);
68 if (!r)
69 return false;
70
71 /* We need to read the config in before we scan */
72 err = nvme_read_config(r, file);
73 if (err)
74 goto out;
75
76 err = nvme_scan_topology(r, NULL, NULL);
77 if (err) {
78 if (errno != ENOENT)
79 goto out;
80 }
81
82 hostnqn = "nqn.2014-08.org.nvmexpress:uuid:2cd2c43b-a90a-45c1-a8cd-86b33ab273b5";
83 hostid = "2cd2c43b-a90a-45c1-a8cd-86b33ab273b5";
84
85 err = nvme_host_get_ids(r, NULL, NULL, &hnqn, &hid);
86 if (err)
87 goto out;
88
89 if (strcmp(hostnqn, hnqn)) {
90 printf("json config hostnqn '%s' does not match '%s'\n", hostnqn, hnqn);
91 goto out;
92 }
93 if (strcmp(hostid, hid)) {
94 printf("json config hostid '%s' does not match '%s'\n", hostid, hid);
95 goto out;
96 }
97
98 free(hnqn);
99 free(hid);
100
101 pass = true;
102
103 out:
104 nvme_free_tree(r);
105 return pass;
106 }
107
108 static bool from_file(void)
109 {
110 bool pass = false;
111 nvme_root_t r;
112 int err;
113 char *hostnqn, *hostid, *hnqn, *hid;
114
115 hostnqn = "nqn.2014-08.org.nvmexpress:uuid:ce4fee3e-c02c-11ee-8442-830d068a36c6";
116 hostid = "ce4fee3e-c02c-11ee-8442-830d068a36c6";
117
118 setenv("LIBNVME_HOSTNQN", hostnqn, 1);
119 setenv("LIBNVME_HOSTID", hostid, 1);
120
121 r = nvme_create_root(stderr, LOG_ERR);
122 if (!r)
123 return false;
124
125 err = nvme_scan_topology(r, NULL, NULL);
126 if (err) {
127 if (errno != ENOENT)
128 goto out;
129 }
130
131 err = nvme_host_get_ids(r, NULL, NULL, &hnqn, &hid);
132 if (err)
133 goto out;
134
135 if (strcmp(hostnqn, hnqn)) {
136 printf("json config hostnqn '%s' does not match '%s'\n", hostnqn, hnqn);
137 goto out;
138 }
139 if (strcmp(hostid, hid)) {
140 printf("json config hostid '%s' does not match '%s'\n", hostid, hid);
141 goto out;
142 }
143
144 free(hnqn);
145 free(hid);
146
147 pass = true;
148
149 out:
150 nvme_free_tree(r);
151 return pass;
152 }
153
154 int main(int argc, char *argv[])
155 {
156 bool pass;
157
158 pass = command_line();
159 pass &= json_config(argv[1]);
160 pass &= from_file();
161 fflush(stdout);
162
163 exit(pass ? EXIT_SUCCESS : EXIT_FAILURE);
164 }