summaryrefslogtreecommitdiffstats
path: root/src/ccapi/test/test_ccapi_iterators.c
blob: e51c7cf32aa649a5f64997f5a869d9cef0c99df2 (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
242
243
244
245
246
#include "test_ccapi_globals.h"
#include "test_ccapi_iterators.h"
#include "test_ccapi_check.h"
#include "test_ccapi_util.h"

// ---------------------------------------------------------------------------

int check_cc_ccache_iterator_next(void) {
	cc_int32 err = 0;
	cc_context_t context = NULL;
	cc_ccache_t ccache = NULL;
	cc_ccache_iterator_t iterator = NULL;
	unsigned int i;

    BEGIN_TEST("cc_ccache_iterator_next");

	err = cc_initialize(&context, ccapi_version_3, NULL, NULL);

	if (!err) {
		err = destroy_all_ccaches(context);
	}

	// iterate with no ccaches
	if (!err) {
		err = cc_context_new_ccache_iterator(context, &iterator);
	}
	check_once_cc_ccache_iterator_next(iterator, 0, ccNoError, "iterating over an empty collection");
	if (iterator) {
		cc_ccache_iterator_release(iterator);
		iterator = NULL;
	}

	// iterate with one ccache
	if (!err) {
		destroy_all_ccaches(context);
		err = cc_context_create_new_ccache(context, cc_credentials_v5, "foo@BAR.ORG", &ccache);
	}
	if (ccache) {
		cc_ccache_release(ccache);
		ccache = NULL;
	}
	if (!err) {
		err = cc_context_new_ccache_iterator(context, &iterator);
	}
	check_once_cc_ccache_iterator_next(iterator, 1, ccNoError, "iterating over a collection of 1 ccache");
	if (iterator) {
		cc_ccache_iterator_release(iterator);
		iterator = NULL;
	}

	// iterate with several ccaches
	if (!err) {
		destroy_all_ccaches(context);
	}
	for(i = 0; !err && (i < 1000); i++)
	{
        if (i%100 == 0) fprintf(stdout, ".");
        err = cc_context_create_new_ccache(context, cc_credentials_v5, "foo@BAR.ORG", &ccache);
		if (ccache) {
			cc_ccache_release(ccache);
			ccache = NULL;
		}
	}
	if (!err) {
		err = cc_context_new_ccache_iterator(context, &iterator);
	}
	check_once_cc_ccache_iterator_next(iterator, 1000, ccNoError, "iterating over a collection of 1000 ccache");
	if (iterator) {
		cc_ccache_iterator_release(iterator);
		iterator = NULL;
	}


	if (ccache) { cc_ccache_release(ccache); }
	if (iterator) { cc_ccache_iterator_release(iterator); }
	if (context) {
		destroy_all_ccaches(context);
		cc_context_release(context);
	}

	END_TEST_AND_RETURN
}

cc_int32 check_once_cc_ccache_iterator_next(cc_ccache_iterator_t iterator, cc_uint32 expected_count, cc_int32 expected_err, const char *description) {
	cc_int32 err = ccNoError;

//	BEGIN_CHECK_ONCE(description);

	cc_int32 possible_return_values[6] = {
		ccNoError,
		ccIteratorEnd,
		ccErrBadParam,
		ccErrNoMem,
		ccErrInvalidCCacheIterator,
		ccErrCCacheNotFound,
	};
	#define possible_ret_val_count sizeof(possible_return_values)/sizeof(possible_return_values[0])

	cc_ccache_t ccache = NULL;
	cc_uint32 actual_count = 0;

	while (!err) {
		err = cc_ccache_iterator_next(iterator, &ccache);
		if (ccache) {
			actual_count++;
			cc_ccache_release(ccache);
			ccache = NULL;
		}
	}
	if (err == ccIteratorEnd) {
		err = ccNoError;
	}

	// check returned error
	check_err(err, expected_err, possible_return_values);

	check_if(actual_count != expected_count, "iterator didn't iterate over all ccaches");

//	END_CHECK_ONCE;

	return err;
}


// ---------------------------------------------------------------------------

int check_cc_credentials_iterator_next(void) {
	cc_int32 err = 0;
	cc_context_t context = NULL;
	cc_ccache_t ccache = NULL;
	cc_credentials_union creds_union;
	cc_credentials_iterator_t iterator = NULL;
	unsigned int i;

	BEGIN_TEST("cc_credentials_iterator_next");

	err = cc_initialize(&context, ccapi_version_3, NULL, NULL);

	if (!err) {
		err = destroy_all_ccaches(context);
	}

	// iterate with no creds
	if (!err) {
		err = cc_context_create_new_ccache(context, cc_credentials_v5, "foo@BAR.ORG", &ccache);
	}
	if (!err) {
		err = cc_ccache_new_credentials_iterator(ccache, &iterator);
	}
	check_once_cc_credentials_iterator_next(iterator, 0, ccNoError, "iterating over an empty ccache");
	if (iterator) {
		cc_ccache_iterator_release(iterator);
		iterator = NULL;
	}
	if (ccache) {
		cc_ccache_release(ccache);
		ccache = NULL;
	}

	// iterate with one cred
	if (!err) {
		destroy_all_ccaches(context);
		err = cc_context_create_new_ccache(context, cc_credentials_v5, "foo@BAR.ORG", &ccache);
	}
	if (!err) {
		new_v5_creds_union(&creds_union, "BAR.ORG");
		err = cc_ccache_store_credentials(ccache, &creds_union);
		release_v5_creds_union(&creds_union);
	}
	if (!err) {
		err = cc_ccache_new_credentials_iterator(ccache, &iterator);
	}
	check_once_cc_credentials_iterator_next(iterator, 1, ccNoError, "iterating over a ccache with 1 cred");
	if (iterator) {
		cc_credentials_iterator_release(iterator);
		iterator = NULL;
	}
	if (ccache) {
		cc_ccache_release(ccache);
		ccache = NULL;
	}

	// iterate with several creds
	if (!err) {
		destroy_all_ccaches(context);
		err = cc_context_create_new_ccache(context, cc_credentials_v5, "foo@BAR.ORG", &ccache);
	}
	for(i = 0; !err && (i < 1000); i++) {
        if (i%100 == 0) fprintf(stdout, ".");
		new_v5_creds_union(&creds_union, "BAR.ORG");
		err = cc_ccache_store_credentials(ccache, &creds_union);
		release_v5_creds_union(&creds_union);
	}
	if (!err) {
		err = cc_ccache_new_credentials_iterator(ccache, &iterator);
	}
	check_once_cc_credentials_iterator_next(iterator, 1000, ccNoError, "iterating over a ccache with 1000 creds");

	if (ccache) { cc_ccache_release(ccache); }
	if (iterator) { cc_credentials_iterator_release(iterator); }
	if (context) {
		destroy_all_ccaches(context);
		cc_context_release(context);
	}

	END_TEST_AND_RETURN
}

cc_int32 check_once_cc_credentials_iterator_next(cc_credentials_iterator_t iterator, cc_uint32 expected_count, cc_int32 expected_err, const char *description) {
	cc_int32            err             = ccNoError;
	cc_credentials_t    creds           = NULL;
	cc_uint32           actual_count    = 0;

	cc_int32 possible_return_values[5] = {
		ccNoError,
		ccIteratorEnd,
		ccErrBadParam,
		ccErrNoMem,
		ccErrInvalidCredentialsIterator,
	};

    BEGIN_CHECK_ONCE(description);

	#define possible_ret_val_count sizeof(possible_return_values)/sizeof(possible_return_values[0])

	while (!err) {
		err = cc_credentials_iterator_next(iterator, &creds);
		if (creds) {
			actual_count++;
			cc_credentials_release(creds);
			creds = NULL;
		}
	}
	if (err == ccIteratorEnd) {
		err = ccNoError;
	}

	// check returned error
	check_err(err, expected_err, possible_return_values);

	check_if(actual_count != expected_count, "iterator didn't iterate over all ccaches");

	END_CHECK_ONCE;

	return err;
}