|
| 1 | +/** |
| 2 | + SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | +
|
| 4 | + This file is part of libnvme. |
| 5 | + Copyright (c) 2023 Dell Inc. |
| 6 | +
|
| 7 | + Authors: Martin Belanger <[email protected]> |
| 8 | +*/ |
| 9 | + |
| 10 | +/** |
| 11 | + * In this file we test private functions found in |
| 12 | + * "src/nvme/util.c". Note that the source files are included |
| 13 | + * directly because the private functions are not available from |
| 14 | + * the libnvme.so. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <stdlib.h> |
| 18 | +#include <stdio.h> |
| 19 | +#include <stdbool.h> |
| 20 | +#include <netdb.h> |
| 21 | +#include <string.h> |
| 22 | + |
| 23 | +#include "nvme/cleanup.c" /* to resolve cleanup_charp() */ |
| 24 | +#include "nvme/log.c" /* to resolve __nvme_msg() */ |
| 25 | +#include "nvme/util.c" |
| 26 | + |
| 27 | +static size_t safe_strlen(const char *p) { |
| 28 | + return p ? strlen(p) : strlen("null"); |
| 29 | +} |
| 30 | + |
| 31 | +static bool test_nvme_get_version(enum nvme_version type, const char * exp_str) { |
| 32 | + const char * str; |
| 33 | + str = nvme_get_version(type); |
| 34 | + return !strcmp(str, exp_str); |
| 35 | +} |
| 36 | + |
| 37 | +static bool test_ipaddrs_eq() { |
| 38 | + int test_success = true; |
| 39 | + static const char *x = "1.1.1.1"; |
| 40 | + struct { |
| 41 | + const char *a; |
| 42 | + const char *b; |
| 43 | + bool exp_result; |
| 44 | + } addrs[] = { |
| 45 | + {"192.168.56.101", "192.168.56.101", true}, |
| 46 | + {"2001:0db8:0000:0000:0000:ff00:0042:8329", "2001:0db8::ff00:0042:8329", true}, |
| 47 | + {NULL, NULL, true}, |
| 48 | + {x, x, true}, |
| 49 | + {"::ffff:192.168.56.101", "::ffff:192.168.56.101", true}, |
| 50 | + {"::ffff:192.168.56.101", "192.168.56.101", true}, |
| 51 | + {"::ffff:192.168.56.222", "192.168.56.101", false}, |
| 52 | + {"1.2.3.4", "192.168.56.101", false}, |
| 53 | + {"2001:0db8:0001:0000:0000:ff00:0042:8329", "2001:0db8::ff00:0042:8329", false}, |
| 54 | + {"2001:0db8:0001:0000:0000:ff00:0042:8329", NULL, false}, |
| 55 | + }; |
| 56 | + |
| 57 | + size_t i; |
| 58 | + size_t n = sizeof(addrs) / sizeof(addrs[0]); |
| 59 | + size_t longest_a = 0, longest_b = 0; |
| 60 | + |
| 61 | + for (i = 0; i < n; i++) { |
| 62 | + size_t l; |
| 63 | + l = safe_strlen(addrs[i].a); |
| 64 | + if (l > longest_a) longest_a = l; |
| 65 | + l = safe_strlen(addrs[i].b); |
| 66 | + if (l > longest_b) longest_b = l; |
| 67 | + } |
| 68 | + |
| 69 | + for (i = 0; i < n; i++) { |
| 70 | + bool result = ipaddrs_eq(addrs[i].a, addrs[i].b); |
| 71 | + bool pass = result == addrs[i].exp_result; |
| 72 | + int pad_a = longest_a - safe_strlen(addrs[i].a); |
| 73 | + int pad_b = longest_b - safe_strlen(addrs[i].b); |
| 74 | + printf("%s %*.*s %s %*.*s -> %-10s %s\n", |
| 75 | + addrs[i].a ? addrs[i].a : "null", |
| 76 | + pad_a, pad_a, "", |
| 77 | + addrs[i].b ? addrs[i].b : "null", |
| 78 | + pad_b, pad_b, "", |
| 79 | + result ? "equal/same" : "different", |
| 80 | + pass ? "[PASS]" : "[FAIL]"); |
| 81 | + |
| 82 | + if (!pass) |
| 83 | + test_success = false; |
| 84 | + } |
| 85 | + |
| 86 | + return test_success; |
| 87 | +} |
| 88 | + |
| 89 | +int main(int argc, char *argv[]) { |
| 90 | + int exit_val = EXIT_SUCCESS; |
| 91 | + bool pass; |
| 92 | + |
| 93 | + printf("\n------------------------------------------------------------------------------\n"); |
| 94 | + pass = test_nvme_get_version(NVME_VERSION_PROJECT, PROJECT_VERSION); |
| 95 | + printf("nvme_get_version(NVME_VERSION_PROJECT) %s\n", pass ? "[PASS]" : "[FAIL]"); |
| 96 | + if (!pass) |
| 97 | + exit_val = EXIT_FAILURE; |
| 98 | + |
| 99 | + printf("\n------------------------------------------------------------------------------\n"); |
| 100 | + pass = test_nvme_get_version(NVME_VERSION_GIT, GIT_VERSION); |
| 101 | + printf("nvme_get_version(NVME_VERSION_GIT) %s\n", pass ? "[PASS]" : "[FAIL]"); |
| 102 | + if (!pass) |
| 103 | + exit_val = EXIT_FAILURE; |
| 104 | + |
| 105 | + printf("\n------------------------------------------------------------------------------\n"); |
| 106 | + pass = test_nvme_get_version(-1, "n/a"); |
| 107 | + printf("nvme_get_version(-1) %s\n", pass ? "[PASS]" : "[FAIL]"); |
| 108 | + if (!pass) |
| 109 | + exit_val = EXIT_FAILURE; |
| 110 | + |
| 111 | + printf("\n------------------------------------------------------------------------------\n"); |
| 112 | + pass = test_ipaddrs_eq(); |
| 113 | + printf("ipaddrs_eq() %s", pass ? "[PASS]" : "[FAIL]"); |
| 114 | + if (!pass) |
| 115 | + exit_val = EXIT_FAILURE; |
| 116 | + |
| 117 | + exit(exit_val); |
| 118 | +} |
0 commit comments