aboutsummaryrefslogtreecommitdiff
path: root/src/proone-test_util.c
blob: c704d70adffe4264400055cb4bb58d81b12b2e45 (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
#include "util_rt.h"

#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <errno.h>


static void test_uuid (void);
static void test_alloc (void);
static void test_str (void);

int main (void) {
	test_alloc();
	test_str();
	test_uuid();

	return 0;
}

static void test_str (void) {
	static const char *const str_a = "a";
	static const char *const str_b = "b";
	static const char *const str_sample = "abcdefg";

	assert(prne_nstreq(NULL, NULL));
	assert(!prne_nstreq(str_a, NULL));
	assert(!prne_nstreq(NULL, str_b));
	assert(!prne_nstreq(str_a, str_b));
	assert(prne_nstreq(str_a, str_a));

	assert(prne_nstrlen(NULL) == 0);
	assert(prne_nstrlen("") == 0);
	assert(prne_nstrlen(str_a) == 1);
	assert(prne_nstrlen(str_sample) == 7);

	assert(prne_strnchr(str_sample, 'a', 7) == str_sample);
	assert(prne_strnchr(str_sample, 'g', 7) == str_sample + 6);
	assert(prne_strnchr(str_sample, 0, 8) == str_sample + 7);
	assert(prne_strnchr(str_sample, 'g', 6) == NULL);
	assert(prne_strnchr(str_sample, 'a', 0) == NULL);
	assert(prne_strnchr(str_sample, 'x', 7) == NULL);

	for (int i = 0; i <= UINT_FAST8_MAX; i += 1) {
		char exp_str[3], str[3];
		uint_fast8_t out;

		str[2] = 0;

		sprintf(exp_str, "%02hhx", i);
		prne_hex_tochar((uint8_t)i, str, false);
		assert(memcmp(exp_str, str, 3) == 0);
		assert(prne_hex_fromstr(str, &out) && out == i);

		sprintf(exp_str, "%02hhX", i);
		prne_hex_tochar((uint8_t)i, str, true);
		assert(memcmp(exp_str, str, 3) == 0);
		assert(prne_hex_fromstr(str, &out) && out == i);
	}
}

static void test_alloc (void) {
	prne_free(NULL);

	errno = 0;
	assert(
		prne_malloc(0, 0) == NULL &&
		prne_malloc(1, 0) == NULL &&
		prne_malloc(0, 1) == NULL &&
		errno == 0);
	errno = 0;
	assert(prne_malloc(2, SIZE_MAX / 2 + 1) == NULL);
	assert(errno == ENOMEM);
	errno = 0;
	assert(prne_malloc(SIZE_MAX / 2 + 1, 2) == NULL);
	assert(errno == ENOMEM);

	errno = 0;
	assert(
		prne_calloc(0, 0) == NULL &&
		prne_calloc(1, 0) == NULL &&
		prne_calloc(0, 1) == NULL &&
		errno == 0);
	errno = 0;
	assert(prne_calloc(2, SIZE_MAX / 2 + 1) == NULL);
	assert(errno == ENOMEM);
	errno = 0;
	assert(prne_calloc(SIZE_MAX / 2 + 1, 2) == NULL);
	assert(errno == ENOMEM);

	errno = 0;
	assert(
		prne_realloc(NULL, 0, 0) == NULL &&
		prne_realloc(NULL, 1, 0) == NULL &&
		prne_realloc(NULL, 0, 1) == NULL &&
		errno == 0);
	errno = 0;
	assert(prne_realloc(NULL, 2, SIZE_MAX / 2 + 1) == NULL);
	assert(errno == ENOMEM);
	errno = 0;
	assert(prne_realloc(NULL, SIZE_MAX / 2 + 1, 2) == NULL);
	assert(errno == ENOMEM);

	errno = 0;
	assert(prne_alloc_str(SIZE_MAX) == NULL);
	assert(errno == ENOMEM);
}

static void test_uuid (void) {
	static const char *sample_str = "f31605bb-5ec9-46e7-918d-4810a39a858d";
	static const uint8_t sample_arr[16] = {
		0xf3, 0x16, 0x05, 0xbb, 0x5e, 0xc9, 0x46, 0xe7,
		0x91, 0x8d, 0x48, 0x10, 0xa3, 0x9a, 0x85, 0x8d
	};
	static const char *empty_str = "00000000-0000-0000-0000-000000000000";
	static const uint8_t empty_arr[16];
	uint8_t out_arr[16];
	char out_str[37];

	prne_memzero(out_arr, 16);
	prne_memzero(out_str, 37);
	assert(prne_uuid_fromstr(sample_str, out_arr));
	assert(memcmp(sample_arr, out_arr, 16) == 0);
	prne_uuid_tostr(out_arr, out_str);
	assert(memcmp(sample_str, out_str, 37) == 0);

	memset(out_arr, 0xFF, 16);
	prne_memzero(out_str, 37);
	assert(prne_uuid_fromstr(empty_str, out_arr));
	assert(memcmp(empty_arr, out_arr, 16) == 0);
	prne_uuid_tostr(out_arr, out_str);
	assert(memcmp(empty_str, out_str, 37) == 0);

	errno = 0;
	assert(!prne_uuid_fromstr("", out_arr));
	assert(errno == EINVAL);
	errno = 0;
	assert(!prne_uuid_fromstr(
		"f31605bb-5ec9-46e7-918d-4810a39a858da",
		out_arr));
	assert(errno == EINVAL);
	assert(!prne_uuid_fromstr(
		"f31605bb-5ec9-46e7-918d-4810a39a858d-",
		out_arr));
	assert(errno == EINVAL);
	assert(!prne_uuid_fromstr(
		"f31605bb-5ec9-46e7-918d-4810Z39a858d",
		out_arr));
	assert(errno == EINVAL);
	assert(!prne_uuid_fromstr(
		"f31605bb-5ec9046e7-918d-4810a39a858d",
		out_arr));
	assert(errno == EINVAL);
}