Skip to content

Commit e36b430

Browse files
dwsuseigaw
authored andcommitted
test: add pre-shared key json tests
Add a test case for the PSK API to ensure that the generated JSON is correct. Signed-off-by: Daniel Wagner <[email protected]>
1 parent 9c4a34b commit e36b430

6 files changed

Lines changed: 199 additions & 0 deletions

File tree

test/config/data/tls_key-1.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[
2+
{
3+
"hostnqn":"nqn.2014-08.org.nvmexpress:uuid:befdec4c-2234-11b2-a85c-ca77c773af36",
4+
"hostid":"2cd2c43b-a90a-45c1-a8cd-86b33ab273b6",
5+
"subsystems":[
6+
{
7+
"nqn":"nqn.io-1",
8+
"ports":[
9+
{
10+
"transport":"tcp",
11+
"traddr":"192.168.154.148",
12+
"trsvcid":"4420",
13+
"dhchap_key":"none",
14+
"tls":true,
15+
"tls_key":"NVMeTLSkey-1:01:Hhc5sFjwSZ6w5hPY19tqprajYtuYci3tN+Z2wGViDk3rpSR+:"
16+
}
17+
]
18+
}
19+
]
20+
}
21+
]

test/config/data/tls_key-1.out

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[
2+
{
3+
"hostnqn":"nqn.2014-08.org.nvmexpress:uuid:befdec4c-2234-11b2-a85c-ca77c773af36",
4+
"hostid":"2cd2c43b-a90a-45c1-a8cd-86b33ab273b6",
5+
"subsystems":[
6+
{
7+
"nqn":"nqn.io-1",
8+
"ports":[
9+
{
10+
"transport":"tcp",
11+
"traddr":"192.168.154.148",
12+
"trsvcid":"4420",
13+
"dhchap_key":"none",
14+
"tls":true,
15+
"tls_key":"NVMeTLSkey-1:01:Hhc5sFjwSZ6w5hPY19tqprajYtuYci3tN+Z2wGViDk3rpSR+:"
16+
}
17+
]
18+
}
19+
]
20+
}
21+
]

test/config/data/tls_key-2.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[
2+
{
3+
"hostnqn":"nqn.2014-08.org.nvmexpress:uuid:befdec4c-2234-11b2-a85c-ca77c773af36",
4+
"hostid":"2cd2c43b-a90a-45c1-a8cd-86b33ab273b6",
5+
"subsystems":[
6+
{
7+
"nqn":"nqn.io-1",
8+
"ports":[
9+
{
10+
"transport":"tcp",
11+
"traddr":"192.168.154.148",
12+
"trsvcid":"4420",
13+
"dhchap_key":"none",
14+
"tls":true,
15+
"tls_psk_identity":"NVMe1R01 nqn.2014-08.org.nvmexpress:uuid:befdec4c-2234-11b2-a85c-ca77c773af36 nqn.io-1 QMFIifx2SCVnlE2hc4MQb0r+2g56x3G7P6jJtDiYK+I=",
16+
"tls_key":"NVMeTLSkey-1:01:Hhc5sFjwSZ6w5hPY19tqprajYtuYci3tN+Z2wGViDk3rpSR+:"
17+
}
18+
]
19+
}
20+
]
21+
}
22+
]

test/config/data/tls_key-2.out

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[
2+
{
3+
"hostnqn":"nqn.2014-08.org.nvmexpress:uuid:befdec4c-2234-11b2-a85c-ca77c773af36",
4+
"hostid":"2cd2c43b-a90a-45c1-a8cd-86b33ab273b6",
5+
"subsystems":[
6+
{
7+
"nqn":"nqn.io-1",
8+
"ports":[
9+
{
10+
"transport":"tcp",
11+
"traddr":"192.168.154.148",
12+
"trsvcid":"4420",
13+
"dhchap_key":"none",
14+
"tls":true,
15+
"tls_key":"NVMeTLSkey-1:01:Hhc5sFjwSZ6w5hPY19tqprajYtuYci3tN+Z2wGViDk3rpSR+:"
16+
}
17+
]
18+
}
19+
]
20+
}
21+
]

test/config/meson.build

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,29 @@ if diff.found()
6060
depends : test_hostnqn_order,
6161
)
6262

63+
test_psk_json = executable(
64+
'test-psk-json',
65+
['psk-json.c'],
66+
dependencies: libnvme_dep,
67+
include_directories: [incdir],
68+
)
69+
70+
config_data = [
71+
'tls_key-1',
72+
'tls_key-2',
73+
]
74+
75+
foreach t_file : config_data
76+
test(
77+
'psk-json-' + t_file,
78+
config_diff,
79+
args : [
80+
test_psk_json.full_path(),
81+
builddir,
82+
srcdir + '/data/' + t_file + '.out',
83+
'--config-json', srcdir + '/data/' + t_file + '.json',
84+
],
85+
depends : test_psk_json,
86+
)
87+
endforeach
6388
endif

test/config/psk-json.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 "nvme/linux.h"
8+
#include "nvme/tree.h"
9+
#include <string.h>
10+
#include <stdbool.h>
11+
#include <stdlib.h>
12+
#include <errno.h>
13+
14+
#include <libnvme.h>
15+
16+
static bool import_export_key(nvme_ctrl_t c)
17+
{
18+
unsigned char version, hmac, *key;
19+
char *encoded_key;
20+
size_t len;
21+
22+
key = nvme_import_tls_key_versioned(nvme_ctrl_get_tls_key(c),
23+
&version, &hmac, &len);
24+
if (!key) {
25+
printf("ERROR: nvme_import_tls_key_versioned failed with %d\n",
26+
errno);
27+
return false;
28+
29+
}
30+
31+
encoded_key = nvme_export_tls_key_versioned(version, hmac, key, len);
32+
free(key);
33+
if (!encoded_key) {
34+
printf("ERROR: nvme_export_tls_key_versioned failed with %d\n",
35+
errno);
36+
return false;
37+
}
38+
39+
nvme_ctrl_set_tls_key(c, encoded_key);
40+
41+
free(encoded_key);
42+
43+
return true;
44+
}
45+
46+
static bool psk_json_test(char *file)
47+
{
48+
bool pass = false;
49+
nvme_root_t r;
50+
nvme_host_t h;
51+
nvme_subsystem_t s;
52+
nvme_ctrl_t c;
53+
int err;
54+
55+
r = nvme_create_root(stderr, LOG_ERR);
56+
if (!r)
57+
return false;
58+
59+
err = nvme_read_config(r, file);
60+
if (err)
61+
goto out;
62+
63+
64+
nvme_for_each_host(r, h)
65+
nvme_for_each_subsystem(h, s)
66+
nvme_subsystem_for_each_ctrl(s, c)
67+
if (!import_export_key(c))
68+
goto out;
69+
70+
err = nvme_dump_config(r);
71+
if (err)
72+
goto out;
73+
74+
pass = true;
75+
76+
out:
77+
nvme_free_tree(r);
78+
return pass;
79+
}
80+
81+
int main(int argc, char *argv[])
82+
{
83+
bool pass;
84+
85+
pass = psk_json_test(argv[1]);
86+
fflush(stdout);
87+
88+
exit(pass ? EXIT_SUCCESS : EXIT_FAILURE);
89+
}

0 commit comments

Comments
 (0)