~ubuntu-branches/ubuntu/feisty/apache2/feisty

« back to all changes in this revision

Viewing changes to srclib/apr/test/testhash.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2006-12-09 21:05:45 UTC
  • mfrom: (0.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061209210545-h70s0xaqc2v8vqr2
Tags: 2.2.3-3.2
* Non-maintainer upload.
* 043_ajp_connection_reuse: Patch from upstream Bugzilla, fixing a critical
  issue with regard to connection reuse in mod_proxy_ajp.
  Closes: #396265

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
 
2
 * applicable.
 
3
 *
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * You may obtain a copy of the License at
 
7
 *
 
8
 *     http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
 
15
 */
 
16
 
 
17
#include "testutil.h"
 
18
#include "apr.h"
 
19
#include "apr_strings.h"
 
20
#include "apr_general.h"
 
21
#include "apr_pools.h"
 
22
#include "apr_hash.h"
 
23
 
 
24
static void dump_hash(apr_pool_t *p, apr_hash_t *h, char *str) 
 
25
{
 
26
    apr_hash_index_t *hi;
 
27
    char *val, *key;
 
28
    apr_ssize_t len;
 
29
    int i = 0;
 
30
 
 
31
    str[0] = '\0';
 
32
 
 
33
    for (hi = apr_hash_first(p, h); hi; hi = apr_hash_next(hi)) {
 
34
        apr_hash_this(hi,(void*) &key, &len, (void*) &val);
 
35
        apr_snprintf(str, 8196, "%sKey %s (%" APR_SSIZE_T_FMT ") Value %s\n", 
 
36
                     str, key, len, val);
 
37
        i++;
 
38
    }
 
39
    apr_snprintf(str, 8196, "%s#entries %d\n", str, i);
 
40
}
 
41
 
 
42
static void sum_hash(apr_pool_t *p, apr_hash_t *h, int *pcount, int *keySum, int *valSum) 
 
43
{
 
44
    apr_hash_index_t *hi;
 
45
    void *val, *key;
 
46
    int count = 0;
 
47
 
 
48
    *keySum = 0;
 
49
    *valSum = 0;
 
50
    *pcount = 0;
 
51
    for (hi = apr_hash_first(p, h); hi; hi = apr_hash_next(hi)) {
 
52
        apr_hash_this(hi, (void*)&key, NULL, &val);
 
53
        *valSum += *(int *)val;
 
54
        *keySum += *(int *)key;
 
55
        count++;
 
56
    }
 
57
    *pcount=count;
 
58
}
 
59
 
 
60
static void hash_make(abts_case *tc, void *data)
 
61
{
 
62
    apr_hash_t *h = NULL;
 
63
 
 
64
    h = apr_hash_make(p);
 
65
    ABTS_PTR_NOTNULL(tc, h);
 
66
}
 
67
 
 
68
static void hash_set(abts_case *tc, void *data)
 
69
{
 
70
    apr_hash_t *h = NULL;
 
71
    char *result = NULL;
 
72
 
 
73
    h = apr_hash_make(p);
 
74
    ABTS_PTR_NOTNULL(tc, h);
 
75
 
 
76
    apr_hash_set(h, "key", APR_HASH_KEY_STRING, "value");
 
77
    result = apr_hash_get(h, "key", APR_HASH_KEY_STRING);
 
78
    ABTS_STR_EQUAL(tc, "value", result);
 
79
}
 
80
 
 
81
static void hash_reset(abts_case *tc, void *data)
 
82
{
 
83
    apr_hash_t *h = NULL;
 
84
    char *result = NULL;
 
85
 
 
86
    h = apr_hash_make(p);
 
87
    ABTS_PTR_NOTNULL(tc, h);
 
88
 
 
89
    apr_hash_set(h, "key", APR_HASH_KEY_STRING, "value");
 
90
    result = apr_hash_get(h, "key", APR_HASH_KEY_STRING);
 
91
    ABTS_STR_EQUAL(tc, "value", result);
 
92
 
 
93
    apr_hash_set(h, "key", APR_HASH_KEY_STRING, "new");
 
94
    result = apr_hash_get(h, "key", APR_HASH_KEY_STRING);
 
95
    ABTS_STR_EQUAL(tc, "new", result);
 
96
}
 
97
 
 
98
static void same_value(abts_case *tc, void *data)
 
99
{
 
100
    apr_hash_t *h = NULL;
 
101
    char *result = NULL;
 
102
 
 
103
    h = apr_hash_make(p);
 
104
    ABTS_PTR_NOTNULL(tc, h);
 
105
 
 
106
    apr_hash_set(h, "same1", APR_HASH_KEY_STRING, "same");
 
107
    result = apr_hash_get(h, "same1", APR_HASH_KEY_STRING);
 
108
    ABTS_STR_EQUAL(tc, "same", result);
 
109
 
 
110
    apr_hash_set(h, "same2", APR_HASH_KEY_STRING, "same");
 
111
    result = apr_hash_get(h, "same2", APR_HASH_KEY_STRING);
 
112
    ABTS_STR_EQUAL(tc, "same", result);
 
113
}
 
114
 
 
115
static unsigned int hash_custom( const char *key, apr_ssize_t *klen)
 
116
{
 
117
    unsigned int hash = 0;
 
118
    while( *klen ) {
 
119
        (*klen) --;
 
120
        hash = hash * 33 + key[ *klen ];
 
121
    }
 
122
    return hash;
 
123
}
 
124
 
 
125
static void same_value_custom(abts_case *tc, void *data)
 
126
{
 
127
    apr_hash_t *h = NULL;
 
128
    char *result = NULL;
 
129
 
 
130
    h = apr_hash_make_custom(p, hash_custom);
 
131
    ABTS_PTR_NOTNULL(tc, h);
 
132
 
 
133
    apr_hash_set(h, "same1", 5, "same");
 
134
    result = apr_hash_get(h, "same1", 5);
 
135
    ABTS_STR_EQUAL(tc, "same", result);
 
136
 
 
137
    apr_hash_set(h, "same2", 5, "same");
 
138
    result = apr_hash_get(h, "same2", 5);
 
139
    ABTS_STR_EQUAL(tc, "same", result);
 
140
}
 
141
 
 
142
static void key_space(abts_case *tc, void *data)
 
143
{
 
144
    apr_hash_t *h = NULL;
 
145
    char *result = NULL;
 
146
 
 
147
    h = apr_hash_make(p);
 
148
    ABTS_PTR_NOTNULL(tc, h);
 
149
 
 
150
    apr_hash_set(h, "key with space", APR_HASH_KEY_STRING, "value");
 
151
    result = apr_hash_get(h, "key with space", APR_HASH_KEY_STRING);
 
152
    ABTS_STR_EQUAL(tc, "value", result);
 
153
}
 
154
 
 
155
/* This is kind of a hack, but I am just keeping an existing test.  This is
 
156
 * really testing apr_hash_first, apr_hash_next, and apr_hash_this which 
 
157
 * should be tested in three separate tests, but this will do for now.
 
158
 */
 
159
static void hash_traverse(abts_case *tc, void *data)
 
160
{
 
161
    apr_hash_t *h;
 
162
    char str[8196];
 
163
 
 
164
    h = apr_hash_make(p);
 
165
    ABTS_PTR_NOTNULL(tc, h);
 
166
 
 
167
    apr_hash_set(h, "OVERWRITE", APR_HASH_KEY_STRING, "should not see this");
 
168
    apr_hash_set(h, "FOO3", APR_HASH_KEY_STRING, "bar3");
 
169
    apr_hash_set(h, "FOO3", APR_HASH_KEY_STRING, "bar3");
 
170
    apr_hash_set(h, "FOO1", APR_HASH_KEY_STRING, "bar1");
 
171
    apr_hash_set(h, "FOO2", APR_HASH_KEY_STRING, "bar2");
 
172
    apr_hash_set(h, "FOO4", APR_HASH_KEY_STRING, "bar4");
 
173
    apr_hash_set(h, "SAME1", APR_HASH_KEY_STRING, "same");
 
174
    apr_hash_set(h, "SAME2", APR_HASH_KEY_STRING, "same");
 
175
    apr_hash_set(h, "OVERWRITE", APR_HASH_KEY_STRING, "Overwrite key");
 
176
 
 
177
    dump_hash(p, h, str);
 
178
    ABTS_STR_EQUAL(tc, "Key FOO1 (4) Value bar1\n"
 
179
                          "Key FOO2 (4) Value bar2\n"
 
180
                          "Key OVERWRITE (9) Value Overwrite key\n"
 
181
                          "Key FOO3 (4) Value bar3\n"
 
182
                          "Key SAME1 (5) Value same\n"
 
183
                          "Key FOO4 (4) Value bar4\n"
 
184
                          "Key SAME2 (5) Value same\n"
 
185
                          "#entries 7\n", str);
 
186
}
 
187
 
 
188
/* This is kind of a hack, but I am just keeping an existing test.  This is
 
189
 * really testing apr_hash_first, apr_hash_next, and apr_hash_this which 
 
190
 * should be tested in three separate tests, but this will do for now.
 
191
 */
 
192
static void summation_test(abts_case *tc, void *data)
 
193
{
 
194
    apr_hash_t *h;
 
195
    int sumKeys, sumVal, trySumKey, trySumVal;
 
196
    int i, j, *val, *key;
 
197
 
 
198
    h =apr_hash_make(p);
 
199
    ABTS_PTR_NOTNULL(tc, h);
 
200
 
 
201
    sumKeys = 0;
 
202
    sumVal = 0;
 
203
    trySumKey = 0;
 
204
    trySumVal = 0;
 
205
 
 
206
    for (i = 0; i < 100; i++) {
 
207
        j = i * 10 + 1;
 
208
        sumKeys += j;
 
209
        sumVal += i;
 
210
        key = apr_palloc(p, sizeof(int));
 
211
        *key = j;
 
212
        val = apr_palloc(p, sizeof(int));
 
213
        *val = i;
 
214
        apr_hash_set(h, key, sizeof(int), val);
 
215
    }
 
216
 
 
217
    sum_hash(p, h, &i, &trySumKey, &trySumVal);
 
218
    ABTS_INT_EQUAL(tc, 100, i);
 
219
    ABTS_INT_EQUAL(tc, sumVal, trySumVal);
 
220
    ABTS_INT_EQUAL(tc, sumKeys, trySumKey);
 
221
}
 
222
 
 
223
static void delete_key(abts_case *tc, void *data)
 
224
{
 
225
    apr_hash_t *h = NULL;
 
226
    char *result = NULL;
 
227
 
 
228
    h = apr_hash_make(p);
 
229
    ABTS_PTR_NOTNULL(tc, h);
 
230
 
 
231
    apr_hash_set(h, "key", APR_HASH_KEY_STRING, "value");
 
232
    apr_hash_set(h, "key2", APR_HASH_KEY_STRING, "value2");
 
233
 
 
234
    result = apr_hash_get(h, "key", APR_HASH_KEY_STRING);
 
235
    ABTS_STR_EQUAL(tc, "value", result);
 
236
 
 
237
    result = apr_hash_get(h, "key2", APR_HASH_KEY_STRING);
 
238
    ABTS_STR_EQUAL(tc, "value2", result);
 
239
 
 
240
    apr_hash_set(h, "key", APR_HASH_KEY_STRING, NULL);
 
241
 
 
242
    result = apr_hash_get(h, "key", APR_HASH_KEY_STRING);
 
243
    ABTS_PTR_EQUAL(tc, NULL, result);
 
244
 
 
245
    result = apr_hash_get(h, "key2", APR_HASH_KEY_STRING);
 
246
    ABTS_STR_EQUAL(tc, "value2", result);
 
247
}
 
248
 
 
249
static void hash_count_0(abts_case *tc, void *data)
 
250
{
 
251
    apr_hash_t *h = NULL;
 
252
    int count;
 
253
 
 
254
    h = apr_hash_make(p);
 
255
    ABTS_PTR_NOTNULL(tc, h);
 
256
 
 
257
    count = apr_hash_count(h);
 
258
    ABTS_INT_EQUAL(tc, 0, count);
 
259
}
 
260
 
 
261
static void hash_count_1(abts_case *tc, void *data)
 
262
{
 
263
    apr_hash_t *h = NULL;
 
264
    int count;
 
265
 
 
266
    h = apr_hash_make(p);
 
267
    ABTS_PTR_NOTNULL(tc, h);
 
268
 
 
269
    apr_hash_set(h, "key", APR_HASH_KEY_STRING, "value");
 
270
 
 
271
    count = apr_hash_count(h);
 
272
    ABTS_INT_EQUAL(tc, 1, count);
 
273
}
 
274
 
 
275
static void hash_count_5(abts_case *tc, void *data)
 
276
{
 
277
    apr_hash_t *h = NULL;
 
278
    int count;
 
279
 
 
280
    h = apr_hash_make(p);
 
281
    ABTS_PTR_NOTNULL(tc, h);
 
282
 
 
283
    apr_hash_set(h, "key1", APR_HASH_KEY_STRING, "value1");
 
284
    apr_hash_set(h, "key2", APR_HASH_KEY_STRING, "value2");
 
285
    apr_hash_set(h, "key3", APR_HASH_KEY_STRING, "value3");
 
286
    apr_hash_set(h, "key4", APR_HASH_KEY_STRING, "value4");
 
287
    apr_hash_set(h, "key5", APR_HASH_KEY_STRING, "value5");
 
288
 
 
289
    count = apr_hash_count(h);
 
290
    ABTS_INT_EQUAL(tc, 5, count);
 
291
}
 
292
 
 
293
static void overlay_empty(abts_case *tc, void *data)
 
294
{
 
295
    apr_hash_t *base = NULL;
 
296
    apr_hash_t *overlay = NULL;
 
297
    apr_hash_t *result = NULL;
 
298
    int count;
 
299
    char str[8196];
 
300
 
 
301
    base = apr_hash_make(p);
 
302
    overlay = apr_hash_make(p);
 
303
    ABTS_PTR_NOTNULL(tc, base);
 
304
    ABTS_PTR_NOTNULL(tc, overlay);
 
305
 
 
306
    apr_hash_set(base, "key1", APR_HASH_KEY_STRING, "value1");
 
307
    apr_hash_set(base, "key2", APR_HASH_KEY_STRING, "value2");
 
308
    apr_hash_set(base, "key3", APR_HASH_KEY_STRING, "value3");
 
309
    apr_hash_set(base, "key4", APR_HASH_KEY_STRING, "value4");
 
310
    apr_hash_set(base, "key5", APR_HASH_KEY_STRING, "value5");
 
311
 
 
312
    result = apr_hash_overlay(p, overlay, base);
 
313
 
 
314
    count = apr_hash_count(result);
 
315
    ABTS_INT_EQUAL(tc, 5, count);
 
316
 
 
317
    dump_hash(p, result, str);
 
318
    ABTS_STR_EQUAL(tc, "Key key1 (4) Value value1\n"
 
319
                          "Key key2 (4) Value value2\n"
 
320
                          "Key key3 (4) Value value3\n"
 
321
                          "Key key4 (4) Value value4\n"
 
322
                          "Key key5 (4) Value value5\n"
 
323
                          "#entries 5\n", str);
 
324
}
 
325
 
 
326
static void overlay_2unique(abts_case *tc, void *data)
 
327
{
 
328
    apr_hash_t *base = NULL;
 
329
    apr_hash_t *overlay = NULL;
 
330
    apr_hash_t *result = NULL;
 
331
    int count;
 
332
    char str[8196];
 
333
 
 
334
    base = apr_hash_make(p);
 
335
    overlay = apr_hash_make(p);
 
336
    ABTS_PTR_NOTNULL(tc, base);
 
337
    ABTS_PTR_NOTNULL(tc, overlay);
 
338
 
 
339
    apr_hash_set(base, "base1", APR_HASH_KEY_STRING, "value1");
 
340
    apr_hash_set(base, "base2", APR_HASH_KEY_STRING, "value2");
 
341
    apr_hash_set(base, "base3", APR_HASH_KEY_STRING, "value3");
 
342
    apr_hash_set(base, "base4", APR_HASH_KEY_STRING, "value4");
 
343
    apr_hash_set(base, "base5", APR_HASH_KEY_STRING, "value5");
 
344
 
 
345
    apr_hash_set(overlay, "overlay1", APR_HASH_KEY_STRING, "value1");
 
346
    apr_hash_set(overlay, "overlay2", APR_HASH_KEY_STRING, "value2");
 
347
    apr_hash_set(overlay, "overlay3", APR_HASH_KEY_STRING, "value3");
 
348
    apr_hash_set(overlay, "overlay4", APR_HASH_KEY_STRING, "value4");
 
349
    apr_hash_set(overlay, "overlay5", APR_HASH_KEY_STRING, "value5");
 
350
 
 
351
    result = apr_hash_overlay(p, overlay, base);
 
352
 
 
353
    count = apr_hash_count(result);
 
354
    ABTS_INT_EQUAL(tc, 10, count);
 
355
 
 
356
    dump_hash(p, result, str);
 
357
    /* I don't know why these are out of order, but they are.  I would probably
 
358
     * consider this a bug, but others should comment.
 
359
     */
 
360
    ABTS_STR_EQUAL(tc, "Key base5 (5) Value value5\n"
 
361
                          "Key overlay1 (8) Value value1\n"
 
362
                          "Key overlay2 (8) Value value2\n"
 
363
                          "Key overlay3 (8) Value value3\n"
 
364
                          "Key overlay4 (8) Value value4\n"
 
365
                          "Key overlay5 (8) Value value5\n"
 
366
                          "Key base1 (5) Value value1\n"
 
367
                          "Key base2 (5) Value value2\n"
 
368
                          "Key base3 (5) Value value3\n"
 
369
                          "Key base4 (5) Value value4\n"
 
370
                          "#entries 10\n", str);
 
371
}
 
372
 
 
373
static void overlay_same(abts_case *tc, void *data)
 
374
{
 
375
    apr_hash_t *base = NULL;
 
376
    apr_hash_t *result = NULL;
 
377
    int count;
 
378
    char str[8196];
 
379
 
 
380
    base = apr_hash_make(p);
 
381
    ABTS_PTR_NOTNULL(tc, base);
 
382
 
 
383
    apr_hash_set(base, "base1", APR_HASH_KEY_STRING, "value1");
 
384
    apr_hash_set(base, "base2", APR_HASH_KEY_STRING, "value2");
 
385
    apr_hash_set(base, "base3", APR_HASH_KEY_STRING, "value3");
 
386
    apr_hash_set(base, "base4", APR_HASH_KEY_STRING, "value4");
 
387
    apr_hash_set(base, "base5", APR_HASH_KEY_STRING, "value5");
 
388
 
 
389
    result = apr_hash_overlay(p, base, base);
 
390
 
 
391
    count = apr_hash_count(result);
 
392
    ABTS_INT_EQUAL(tc, 5, count);
 
393
 
 
394
    dump_hash(p, result, str);
 
395
    /* I don't know why these are out of order, but they are.  I would probably
 
396
     * consider this a bug, but others should comment.
 
397
     */
 
398
    ABTS_STR_EQUAL(tc, "Key base5 (5) Value value5\n"
 
399
                          "Key base1 (5) Value value1\n"
 
400
                          "Key base2 (5) Value value2\n"
 
401
                          "Key base3 (5) Value value3\n"
 
402
                          "Key base4 (5) Value value4\n"
 
403
                          "#entries 5\n", str);
 
404
}
 
405
 
 
406
abts_suite *testhash(abts_suite *suite)
 
407
{
 
408
    suite = ADD_SUITE(suite)
 
409
 
 
410
    abts_run_test(suite, hash_make, NULL);
 
411
    abts_run_test(suite, hash_set, NULL);
 
412
    abts_run_test(suite, hash_reset, NULL);
 
413
    abts_run_test(suite, same_value, NULL);
 
414
    abts_run_test(suite, same_value_custom, NULL);
 
415
    abts_run_test(suite, key_space, NULL);
 
416
    abts_run_test(suite, delete_key, NULL);
 
417
 
 
418
    abts_run_test(suite, hash_count_0, NULL);
 
419
    abts_run_test(suite, hash_count_1, NULL);
 
420
    abts_run_test(suite, hash_count_5, NULL);
 
421
 
 
422
    abts_run_test(suite, hash_traverse, NULL);
 
423
    abts_run_test(suite, summation_test, NULL);
 
424
 
 
425
    abts_run_test(suite, overlay_empty, NULL);
 
426
    abts_run_test(suite, overlay_2unique, NULL);
 
427
    abts_run_test(suite, overlay_same, NULL);
 
428
 
 
429
    return suite;
 
430
}
 
431