summaryrefslogtreecommitdiff
path: root/tests/unit-test-storage.c
blob: 11e9680593b8f451414b842bdbb538d3ad8b1918 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241

#include "config.h"

#include "run-tests.h"

#include "module/mod_auth_singleid.h"

#include <string.h>
#include <time.h>

/*
 * Each test looks like (on one line):
 *     void unit_test_xxxxx (CuTest* cu)
 *
 * Each setup looks like (on one line):
 *     void unit_setup_xxxxx (void)
 *
 * Each teardown looks like (on one line):
 *     void unit_teardown_xxxxx (void)
 *
 * Tests be run in the order specified here.
 */

static void *memory = NULL;
static sid_storage_t *store = NULL;

#define LONG_DATA \
	"0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" \
	"0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" \
	"0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" \
	"0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" \
	"0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" \
	"0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" \
	"0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"

void unit_setup_storage (void)
{
	memory = malloc (4096);
	store = sid_storage_initialize (memory, 4096);
}

void unit_teardown_storage (void)
{
	free (memory);
	store = NULL;
	memory = NULL;
}

void unit_test_storage_initialize_null (CuTest *cu)
{
	sid_storage_t *st = sid_storage_initialize (NULL, 0);
	CuAssertPtrEquals (cu, NULL, st);
}

void unit_test_storage_initialize_short (CuTest *cu)
{
	void *mem = malloc (16);
	sid_storage_t *st = sid_storage_initialize (mem, 16);
	CuAssertPtrEquals (cu, NULL, st);
	free (mem);
}

void unit_test_storage_store_retrieve (CuTest *cu)
{
	sid_assoc_t assoc = {
		"server name", "handle name", "type name",
		(unsigned char*)"mysecret", 2, time (NULL)
	};

	sid_assoc_t check;
	int res;

	res = sid_storage_store_assoc (store, &assoc);
	CuAssertIntEquals (cu, 1, res);

	res = sid_storage_find_assoc (store, "server name", NULL, &check);
	CuAssertIntEquals (cu, 1, res);

	CuAssertStrEquals (cu, "server name", check.server);
	CuAssertStrEquals (cu, "handle name", check.handle);
	CuAssertStrEquals (cu, "type name", check.type);
	CuAssertIntEquals (cu, 2, check.n_secret);
	CuAssert (cu, "invalid secret", memcmp (check.secret, "my", 2) == 0);
	CuAssertIntEquals (cu, assoc.expires, check.expires);
}

void unit_test_storage_store_too_long (CuTest *cu)
{
	sid_assoc_t assoc = {
		"server", LONG_DATA, "type name",
		(unsigned char*)"mysecret", 2, time (NULL)
	};

	int res;

	res = sid_storage_store_assoc (store, &assoc);
	CuAssertIntEquals (cu, 0, res);

	res = sid_storage_find_assoc (store, "server", NULL, &assoc);
	CuAssertIntEquals (cu, 0, res);
}

void unit_test_storage_find_wrong (CuTest *cu)
{
	sid_assoc_t assoc = {
		"server", "handle", "type name",
		(unsigned char*)"mysecret", 2, time (NULL)
	};

	int res;

	res = sid_storage_store_assoc (store, &assoc);
	CuAssertIntEquals (cu, 1, res);

	res = sid_storage_find_assoc (store, "other server", NULL, &assoc);
	CuAssertIntEquals (cu, 0, res);

	res = sid_storage_find_assoc (store, NULL, "other handle", &assoc);
	CuAssertIntEquals (cu, 0, res);

	res = sid_storage_find_assoc (store, "server", "handle", &assoc);
	CuAssertIntEquals (cu, 1, res);
}

void unit_test_storage_invalidate (CuTest *cu)
{
	sid_assoc_t assoc = {
		"server", "handle", "type",
		(unsigned char*)"mysecret", 8, time (NULL)
	};

	int res;

	res = sid_storage_store_assoc (store, &assoc);
	CuAssertIntEquals (cu, 1, res);

	res = sid_storage_find_assoc (store, "server", "handle", &assoc);
	CuAssertIntEquals (cu, 1, res);

	sid_storage_invalidate_assoc (store, "bad server", NULL);

	/* Should still find */
	res = sid_storage_find_assoc (store, "server", "handle", &assoc);
	CuAssertIntEquals (cu, 1, res);

	sid_storage_invalidate_assoc (store, "server", NULL);

	/* Gone now */
	res = sid_storage_find_assoc (store, "server", "handle", &assoc);
	CuAssertIntEquals (cu, 0, res);
}

void unit_test_storage_check_nonce (CuTest *cu)
{
	char nonce[256];
	int capacity;
	int i, res;

	capacity = sid_storage_nonce_capacity (store);
	CuAssert (cu, "should be greater than two", capacity > 2);

	/* Fill to half capacity, and do some checks */
	for (i = 0; i < capacity / 2; ++i) {
		snprintf (nonce, 256, "2009-01-01T00:00:00Z_A%d", i);
		res = sid_storage_check_nonce (store, "server", nonce);
		CuAssert (cu, "should not be seen", res == 0);
	}

	/* All those should be present */
	for (i = 0; i < capacity / 2; ++i) {
		snprintf (nonce, 256, "2009-01-01T00:00:00Z_A%d", i);
		res = sid_storage_check_nonce (store, "server", nonce);
		CuAssert (cu, "should be seen", res == 1);
	}

	/* Fill capacity, and blow first set away */
	for (i = 0; i < capacity; ++i) {
		snprintf (nonce, 256, "2009-01-01T00:00:00Z_B%d", i);
		res = sid_storage_check_nonce (store, "server", nonce);
		CuAssert (cu, "should not be seen", res == 0);
	}

	/* First set should still be seen, even though we blew them away */
	for (i = 0; i < capacity / 2; ++i) {
		snprintf (nonce, 256, "2009-01-01T00:00:00Z_A%d", i);
		res = sid_storage_check_nonce (store, "server", nonce);
		CuAssert (cu, "should be seen", res == 1);
	}

	/* Stuff never entered should not be seen */
	for (i = 0; i < capacity; ++i) {
		snprintf (nonce, 256, "2009-01-01T00:00:00Z_C%d", i);
		res = sid_storage_check_nonce (store, "server", nonce);
		CuAssert (cu, "should not be seen", res == 0);
	}

	/* Fill odd ones in reverse*/
	for (i = capacity - 1; i >= 0; --i) {
		snprintf (nonce, 256, "2009-01-01T00:00:00Z_D%d", i);
		res = sid_storage_check_nonce (store, "server", nonce);
		CuAssert (cu, "should not be seen", res == 0);
	}

	/* Should all be present */
	for (i = capacity - 1; i >= 0; --i) {
		snprintf (nonce, 256, "2009-01-01T00:00:00Z_D%d", i);
		res = sid_storage_check_nonce (store, "server", nonce);
		CuAssert (cu, "should not seen", res == 1);
	}

}

void unit_test_storage_long_nonce (CuTest *cu)
{
	char nonce[256];
	int capacity;
	int i, res;

	#define SUFFIX "xxxxxxxxx xxxxxxxxx "
	capacity = sid_storage_nonce_capacity (store);

	/* Fill to capacity, and do some checks */
	for (i = 0; i < capacity; ++i) {
		snprintf (nonce, 256, "2009-06-01T00:00:00Z_X%d" SUFFIX, i);
		res = sid_storage_check_nonce (store, "server", nonce);
		CuAssert (cu, "should not be seen", res == 0);
	}

	/* All those should be present */
	for (i = 0; i < capacity; ++i) {
		snprintf (nonce, 256, "2009-06-01T00:00:00Z_X%d" SUFFIX, i);
		res = sid_storage_check_nonce (store, "server", nonce);
		CuAssert (cu, "should be seen", res == 1);
	}
}

/* -----------------------------------------------------------------------------
 * Code being tested
 */

#include "module/storage.c"