File: cas_saml_attr_test.c

package info (click to toggle)
libapache2-mod-auth-cas 1.2-1.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,772 kB
  • sloc: sh: 4,181; ansic: 4,156; makefile: 102
file content (217 lines) | stat: -rw-r--r-- 6,595 bytes parent folder | download | duplicates (3)
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
/*
 *
 * Copyright 2011 the mod_auth_cas team.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 *
 * mod_auth_cas.c
 * Apache CAS Authentication Module
 * Version 1.0.10
 *
 * Contact: cas-user@apereo.org
 *
 */

#include <check.h>
#include <stdio.h>

#include <apr.h>

#include "../src/cas_saml_attr.h"

TCase *cas_saml_attr_tcase(void);

static apr_pool_t *pool;

/* XXX: This is duplicated from cas_saml_attr.c
 *
 * It's an opaque structure by design, but the tests need to see
 * inside of it to check the invariants.
 */
struct cas_attr_builder
{
    apr_pool_t *pool;
    cas_saml_attr **next_attr;
    cas_saml_attr_val **next_val;
    const char *last_attr;
};

/* Compare two lists of attributes. The lists are equal if they have
 * exactly the same structure (including ordering, etc.). It is a deep
 * value comparison (i.e. object identity is not required).
 */
static int cas_saml_attr_cmp(cas_saml_attr *a, cas_saml_attr *b) {
    cas_saml_attr_val *va;
    cas_saml_attr_val *vb;

    /* Walk both lists together until we hit the end of one of
     * them. */
    for ( ; a && b; a = a->next, b = b->next) {
        /* If the attribute names are not equal, that is our
           ordering. */
        const int v = strcmp(a->attr, b->attr);
        if (v) {
            return v;
        }

        /* Walk both value lists for this attribute together in the
         * same way */
        for (va = a->values, vb = b->values;
             va && vb;
             va = va->next, vb = vb->next) {
            const int cv = strcmp(va->value, vb->value);
            if (cv) {
                return cv;
            }
        }
        if (va) return 1;
        if (vb) return -1;
    }
    /* If we are not at the end of both lists, the shorter (NULL) one
     * is less */
    if (a) return 1;
    if (b) return -1;

    /* We've examined both structures completely and found no
     * differences */
    return 0;
}

/* This function should always succeed when passed a builder and a
 * pointer to the cas_saml_attr list that its building.
 */
static void cas_attr_builder_check_invariants(cas_attr_builder *builder,
                                              cas_saml_attr **result) {
    cas_saml_attr *attrs = *result;
    cas_saml_attr_val *vals;
    if (attrs) {
        /* All attributes have at least one value */
        for (; attrs->next != NULL; attrs = attrs->next) {
            fail_unless(attrs->values != NULL);
        }
        fail_unless(attrs->values != NULL);

        /* attrs is now the last cell in the list */
        fail_unless(attrs->next == NULL);

        /* The next_attr pointer references the spot where the null
         * terminator currently lives */
        fail_unless(builder->next_attr == &(attrs->next));

        /* last_attr is a string that compares equal to the name of
         * the last attribute in the list */
        fail_unless(strcmp(attrs->attr, builder->last_attr) == 0);

        if (attrs->values) {
            for (vals = attrs->values; vals->next != NULL; vals = vals->next);

            /* The next_val pointer points to the end of the values
             * list for the last attribute */
            fail_unless(builder->next_val == &(vals->next));
        }
    } else {
        /* When no attributes have been defined, there is no
         * next_val or last_attr*/
        fail_unless(builder->next_val == NULL);
        fail_unless(builder->last_attr == NULL);

        /* When no attributes have been defined, result is where
         * next_attr goes. */
        fail_unless(builder->next_attr == result);
    }
}

static int cas_saml_attr_len(cas_saml_attr *attrs) {
    int l = 0;
    for ( ; attrs; attrs = attrs->next) {
        l += 1;
    }
    return l;
}

START_TEST(cas_attr_builder_test) {
    const char *foo = "foo";
    struct test_data {
        const char *const k;
        const char *const v;
        const int len;
    } test_data_list[] = {
        {"foo", "bar", 1},
        {"foo", "bar", 1},
        {"foo", "baz", 1},
        {"quux", "blitz", 2},
        {"foo", "bar", 3},
        {foo, "1", 3},
        {foo, "2", 3},
        {NULL, NULL, 0} /* NULL terminator */
    };
    struct test_data *d;

    int i = 0;
    cas_saml_attr *attrs;
    cas_attr_builder *builder = cas_attr_builder_new(pool, &attrs);
    fail_unless(cas_saml_attr_len(attrs) == 0);

    while (1) {
        cas_attr_builder_check_invariants(builder, &attrs);

        d = &test_data_list[i];
        if (d->v == NULL) break;

        cas_attr_builder_add(builder, d->k, d->v);
        fail_unless(cas_saml_attr_len(attrs) == d->len);
        i++;
    }
}
END_TEST

START_TEST(cas_attr_cmp_test) {
    cas_saml_attr *attrs1, *attrs2;
    cas_attr_builder *builder1 = cas_attr_builder_new(pool, &attrs1);
    cas_attr_builder *builder2 = cas_attr_builder_new(pool, &attrs2);

    /* Newly created, empty attribute lists compare equal. */
    fail_unless(cas_saml_attr_cmp(attrs1, attrs2) == 0);

    /* Adding equal pairs to each lists still compares equal. */
    cas_attr_builder_add(builder1, "foo", "bar");
    cas_attr_builder_add(builder2, "foo", "bar");
    fail_unless(cas_saml_attr_cmp(attrs1, attrs2) == 0);

    /* An explicitly empty (NULL) list compares less than a list that
       we have added a value to. */
    fail_unless(cas_saml_attr_cmp(NULL, attrs2) < 0);

    /* A duplicated list behaves the same as the original list. */
    fail_unless(cas_saml_attr_cmp(cas_saml_attr_pdup(pool, attrs1),
                                  attrs2) == 0);
}
END_TEST

static void cas_saml_attr_setup(void) {
  apr_pool_create(&pool, NULL);
}

static void cas_saml_attr_teardown(void) {
  apr_pool_destroy(pool);
}

TCase *cas_saml_attr_tcase(void) {
  TCase *tc_builder = tcase_create("cas_saml_attr_builder");
  tcase_add_checked_fixture(tc_builder,
                            cas_saml_attr_setup,
                            cas_saml_attr_teardown);
  tcase_add_test(tc_builder, cas_attr_builder_test);
  tcase_add_test(tc_builder, cas_attr_cmp_test);
  return tc_builder;
}