summaryrefslogtreecommitdiffstats
path: root/test/test-util.c
blob: 88a3f42c4a88c6cea4397022b6eade233733e6ab (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
/**
 SPDX-License-Identifier: LGPL-2.1-or-later

 This file is part of libnvme.
 Copyright (c) 2023 Dell Inc.

 Authors: Martin Belanger <Martin.Belanger@dell.com>
*/

/**
 * In this file we test private and public functions found in
 * "src/nvme/util.c". Note that the source files are included
 * directly because the private functions are not available from
 * the libnvme.so.
 */

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <netdb.h>
#include <string.h>

#include "nvme/log.c"		/* to resolve __nvme_msg() */
#include "nvme/util.c"

static size_t safe_strlen(const char *p) {
	return p ? strlen(p) : strlen("null");
}

static bool test_nvme_get_version(enum nvme_version type, const char * exp_str) {
	const char * str;
	str = nvme_get_version(type);
	return !strcmp(str, exp_str);
}

static bool test_ipaddrs_eq() {
	int test_success = true;
	static const char *x = "1.1.1.1";
	struct {
		const char *a;
		const char *b;
		bool  exp_result;
	} addrs[] = {
		{"192.168.56.101", "192.168.56.101", true},
		{"2001:0db8:0000:0000:0000:ff00:0042:8329", "2001:0db8::ff00:0042:8329", true},
		{NULL, NULL, true},
		{x, x, true},
		{"::ffff:192.168.56.101", "::ffff:192.168.56.101", true},
		{"::ffff:192.168.56.101", "192.168.56.101", true},
		{"192.168.56.101", "::ffff:192.168.56.101", true},
		{"::ffff:192.168.56.222", "192.168.56.101", false},
		{"192.168.56.101", "::ffff:192.168.56.222", false},
		{"1.2.3.4", "192.168.56.101", false},
		{"!@#$", "192.168.56.101", false},
		{"192.168.56.101", "!@#$", false},
		{"2001:0db8:0001:0000:0000:ff00:0042:8329", "2001:0db8::ff00:0042:8329", false},
		{"2001:0db8:0001:0000:0000:ff00:0042:8329", NULL, false},
	};

	size_t i;
	size_t n = sizeof(addrs) / sizeof(addrs[0]);
	size_t longest_a = 0, longest_b = 0;

	for (i = 0; i < n; i++) {
		size_t l;
		l = safe_strlen(addrs[i].a);
		if (l > longest_a) longest_a = l;
		l = safe_strlen(addrs[i].b);
		if (l > longest_b) longest_b = l;
	}

	for (i = 0; i < n; i++) {
		bool result = nvme_ipaddrs_eq(addrs[i].a, addrs[i].b);
		bool pass = result == addrs[i].exp_result;
		int pad_a = longest_a - safe_strlen(addrs[i].a);
		int pad_b = longest_b - safe_strlen(addrs[i].b);
		printf("%s %*.*s %s %*.*s  -> %-10s  %s\n",
		       addrs[i].a ? addrs[i].a : "null",
		       pad_a, pad_a, "",
		       addrs[i].b ? addrs[i].b : "null",
		       pad_b, pad_b, "",
		       result ? "equal/same" : "different",
		       pass ? "[PASS]" : "[FAIL]");

		if (!pass)
			test_success = false;
	}

	return test_success;
}

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

	printf("\n------------------------------------------------------------------------------\n");
	pass = test_nvme_get_version(NVME_VERSION_PROJECT, PROJECT_VERSION);
	printf("nvme_get_version(NVME_VERSION_PROJECT) %s\n", pass ? "[PASS]" : "[FAIL]");
	if (!pass)
		exit_val = EXIT_FAILURE;

	printf("\n------------------------------------------------------------------------------\n");
	pass = test_nvme_get_version(NVME_VERSION_GIT, GIT_VERSION);
	printf("nvme_get_version(NVME_VERSION_GIT) %s\n", pass ? "[PASS]" : "[FAIL]");
	if (!pass)
		exit_val = EXIT_FAILURE;

	printf("\n------------------------------------------------------------------------------\n");
	pass = test_nvme_get_version(-1, "n/a");
	printf("nvme_get_version(-1) %s\n", pass ? "[PASS]" : "[FAIL]");
	if (!pass)
		exit_val = EXIT_FAILURE;

	printf("\n------------------------------------------------------------------------------\n");
	pass = test_ipaddrs_eq();
	printf("nvme_ipaddrs_eq() %s", pass ? "[PASS]" : "[FAIL]");
	if (!pass)
		exit_val = EXIT_FAILURE;

	exit(exit_val);
}