Skip to content

Commit

Permalink
test: add pre-shared key json tests
Browse files Browse the repository at this point in the history
Add a test case for the PSK API to ensure that the generated JSON is
correct.

Signed-off-by: Daniel Wagner <dwagner@suse.de>
  • Loading branch information
igaw committed Oct 10, 2024
1 parent 7935bce commit f989166
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 0 deletions.
21 changes: 21 additions & 0 deletions test/config/data/psk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[
{
"hostnqn":"nqn.2014-08.org.nvmexpress:uuid:befdec4c-2234-11b2-a85c-ca77c773af36",
"hostid":"2cd2c43b-a90a-45c1-a8cd-86b33ab273b6",
"subsystems":[
{
"nqn":"nqn.io-1",
"ports":[
{
"transport":"tcp",
"traddr":"192.168.154.148",
"trsvcid":"4420",
"dhchap_key":"none",
"tls":true,
"tls_key":"NVMeTLSkey-1:01:Hhc5sFjwSZ6w5hPY19tqprajYtuYci3tN+Z2wGViDk3rpSR+:"
}
]
}
]
}
]
21 changes: 21 additions & 0 deletions test/config/data/psk.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[
{
"hostnqn":"nqn.2014-08.org.nvmexpress:uuid:befdec4c-2234-11b2-a85c-ca77c773af36",
"hostid":"2cd2c43b-a90a-45c1-a8cd-86b33ab273b6",
"subsystems":[
{
"nqn":"nqn.io-1",
"ports":[
{
"transport":"tcp",
"traddr":"192.168.154.148",
"trsvcid":"4420",
"dhchap_key":"none",
"tls":true,
"tls_key":"NVMeTLSkey-1:01:Hhc5sFjwSZ6w5hPY19tqprajYtuYci3tN+Z2wGViDk3rpSR+:"
}
]
}
]
}
]
18 changes: 18 additions & 0 deletions test/config/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,22 @@ if diff.found()
depends : test_hostnqn_order,
)

test_psk_json = executable(
'test-psk-json',
['psk-json.c'],
dependencies: libnvme_dep,
include_directories: [incdir],
)
test(
'psk-json',
config_diff,
args : [
test_psk_json.full_path(),
builddir,
srcdir + '/data/psk.out',
'--config-json', srcdir + '/data/psk.json',
],
depends : test_psk_json,
)

endif
86 changes: 86 additions & 0 deletions test/config/psk-json.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// SPDX-License-Identifier: LGPL-2.1-or-later
/**
* This file is part of libnvme.
* Copyright (c) 2024 Daniel Wagner, SUSE LLC
*/

#include "nvme/linux.h"
#include "nvme/tree.h"
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <errno.h>

#include <libnvme.h>

static bool import_export_key(nvme_ctrl_t c)
{
unsigned char version, hmac, *key;
char *encoded_key;
size_t len;

key = nvme_import_tls_key_versioned(nvme_ctrl_get_tls_key(c),
&version, &hmac, &len);
if (!key) {
printf("ERROR: nvme_import_tls_key_versioned failed with %d\n",
errno);
return false;

}

encoded_key = nvme_export_tls_key_versioned(version, hmac,
key, len);
if (!encoded_key) {
printf("ERROR: nvme_export_tls_key_versioned failed with %d\n",
errno);
return false;
}

nvme_ctrl_set_tls_key(c, encoded_key);
return true;
}

static bool psk_json_test(char *file)
{
bool pass = false;
nvme_root_t r;
nvme_host_t h;
nvme_subsystem_t s;
nvme_ctrl_t c;
int err;

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

err = nvme_read_config(r, file);
if (err)
goto out;


nvme_for_each_host(r, h)
nvme_for_each_subsystem(h, s)
nvme_subsystem_for_each_ctrl(s, c)
if (!import_export_key(c))
goto out;

err = nvme_dump_config(r);
if (err)
goto out;

pass = true;

out:
nvme_free_tree(r);
return pass;
}

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

pass = psk_json_test(argv[1]);
fflush(stdout);

exit(pass ? EXIT_SUCCESS : EXIT_FAILURE);
}

0 comments on commit f989166

Please sign in to comment.