summaryrefslogtreecommitdiffstats
path: root/test/config/hostnqn-order.c
blob: 22fc2278e93ce80c0c50ec8b5daea85640482668 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// SPDX-License-Identifier: LGPL-2.1-or-later
/**
 * This file is part of libnvme.
 * Copyright (c) 2024 Daniel Wagner, SUSE LLC
 */

#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <errno.h>

#include <libnvme.h>

static bool command_line(void)
{
	bool pass = false;
	nvme_root_t r;
	int err;
	char *hostnqn, *hostid, *hnqn, *hid;

	r = nvme_create_root(stderr, LOG_ERR);
	if (!r)
		return false;

	err = nvme_scan_topology(r, NULL, NULL);
	if (err) {
		if (errno != ENOENT)
			goto out;
	}

	hostnqn = "nqn.2014-08.org.nvmexpress:uuid:ce4fee3e-c02c-11ee-8442-830d068a36c6";
	hostid = "ce4fee3e-c02c-11ee-8442-830d068a36c6";

	err = nvme_host_get_ids(r, hostnqn, hostid, &hnqn, &hid);
	if (err)
		goto out;

	if (strcmp(hostnqn, hnqn)) {
		printf("json config hostnqn '%s' does not match '%s'\n", hostnqn, hnqn);
		goto out;
	}
	if (strcmp(hostid, hid)) {
		printf("json config hostid '%s' does not match '%s'\n", hostid, hid);
		goto out;
	}

	free(hnqn);
	free(hid);

	pass = true;

out:
	nvme_free_tree(r);
	return pass;
}

static bool json_config(char *file)
{
	bool pass = false;
	nvme_root_t r;
	int err;
	char *hostnqn, *hostid, *hnqn, *hid;

	setenv("LIBNVME_HOSTNQN", "", 1);
	setenv("LIBNVME_HOSTID", "", 1);

	r = nvme_create_root(stderr, LOG_ERR);
	if (!r)
		return false;

	/* We need to read the config in before we scan */
	err = nvme_read_config(r, file);
	if (err)
		goto out;

	err = nvme_scan_topology(r, NULL, NULL);
	if (err) {
		if (errno != ENOENT)
			goto out;
	}

	hostnqn = "nqn.2014-08.org.nvmexpress:uuid:2cd2c43b-a90a-45c1-a8cd-86b33ab273b5";
	hostid = "2cd2c43b-a90a-45c1-a8cd-86b33ab273b5";

	err = nvme_host_get_ids(r, NULL, NULL, &hnqn, &hid);
	if (err)
		goto out;

	if (strcmp(hostnqn, hnqn)) {
		printf("json config hostnqn '%s' does not match '%s'\n", hostnqn, hnqn);
		goto out;
	}
	if (strcmp(hostid, hid)) {
		printf("json config hostid '%s' does not match '%s'\n", hostid, hid);
		goto out;
	}

	free(hnqn);
	free(hid);

	pass = true;

out:
	nvme_free_tree(r);
	return pass;
}

static bool from_file(void)
{
	bool pass = false;
	nvme_root_t r;
	int err;
	char *hostnqn, *hostid, *hnqn, *hid;

	hostnqn = "nqn.2014-08.org.nvmexpress:uuid:ce4fee3e-c02c-11ee-8442-830d068a36c6";
	hostid = "ce4fee3e-c02c-11ee-8442-830d068a36c6";

	setenv("LIBNVME_HOSTNQN", hostnqn, 1);
	setenv("LIBNVME_HOSTID", hostid, 1);

	r = nvme_create_root(stderr, LOG_ERR);
	if (!r)
		return false;

	err = nvme_scan_topology(r, NULL, NULL);
	if (err) {
		if (errno != ENOENT)
			goto out;
	}

	err = nvme_host_get_ids(r, NULL, NULL, &hnqn, &hid);
	if (err)
		goto out;

	if (strcmp(hostnqn, hnqn)) {
		printf("json config hostnqn '%s' does not match '%s'\n", hostnqn, hnqn);
		goto out;
	}
	if (strcmp(hostid, hid)) {
		printf("json config hostid '%s' does not match '%s'\n", hostid, hid);
		goto out;
	}

	free(hnqn);
	free(hid);

	pass = true;

out:
	nvme_free_tree(r);
	return pass;
}

int main(int argc, char *argv[])
{
	bool pass;

	pass = command_line();
	pass &= json_config(argv[1]);
	pass &= from_file();
	fflush(stdout);

	exit(pass ? EXIT_SUCCESS : EXIT_FAILURE);
}