Converted tests for identification_t
[strongswan.git] / src / libstrongswan / tests / test_identification.c
1 /*
2 * Copyright (C) 2013 Tobias Brunner
3 * Copyright (C) 2009 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include <check.h>
18
19 #include <utils/identification.h>
20
21 /*******************************************************************************
22 * equals
23 */
24
25 static bool id_equals(identification_t *a, char *b_str)
26 {
27 identification_t *b;
28 bool equals;
29
30 b = identification_create_from_string(b_str);
31 equals = a->equals(a, b);
32 b->destroy(b);
33 return equals;
34 }
35
36 START_TEST(test_equals)
37 {
38 identification_t *a;
39 chunk_t encoding, fuzzed;
40 int i;
41
42 a = identification_create_from_string(
43 "C=CH, E=martin@strongswan.org, CN=martin");
44
45 ck_assert(id_equals(a, "C=CH, E=martin@strongswan.org, CN=martin"));
46 ck_assert(id_equals(a, "C=ch, E=martin@STRONGSWAN.ORG, CN=Martin"));
47 ck_assert(!id_equals(a, "C=CN, E=martin@strongswan.org, CN=martin"));
48 ck_assert(!id_equals(a, "E=martin@strongswan.org, C=CH, CN=martin"));
49 ck_assert(!id_equals(a, "E=martin@strongswan.org, C=CH, CN=martin"));
50
51 encoding = chunk_clone(a->get_encoding(a));
52 a->destroy(a);
53
54 /* simple fuzzing, increment each byte of encoding */
55 for (i = 0; i < encoding.len; i++)
56 {
57 if (i == 11 || i == 30 || i == 62)
58 { /* skip ASN.1 type fields, as equals() handles them graceful */
59 continue;
60 }
61 fuzzed = chunk_clone(encoding);
62 fuzzed.ptr[i]++;
63 a = identification_create_from_encoding(ID_DER_ASN1_DN, fuzzed);
64 ck_assert(!id_equals(a, "C=CH, E=martin@strongswan.org, CN=martin"));
65 a->destroy(a);
66 free(fuzzed.ptr);
67 }
68
69 /* and decrement each byte of encoding */
70 for (i = 0; i < encoding.len; i++)
71 {
72 if (i == 11 || i == 30 || i == 62)
73 {
74 continue;
75 }
76 fuzzed = chunk_clone(encoding);
77 fuzzed.ptr[i]--;
78 a = identification_create_from_encoding(ID_DER_ASN1_DN, fuzzed);
79 ck_assert(!id_equals(a, "C=CH, E=martin@strongswan.org, CN=martin"));
80 a->destroy(a);
81 free(fuzzed.ptr);
82 }
83 free(encoding.ptr);
84 }
85 END_TEST
86
87 /*******************************************************************************
88 * matches
89 */
90
91 static bool id_matches(identification_t *a, char *b_str, id_match_t expected)
92 {
93 identification_t *b;
94 id_match_t match;
95
96 b = identification_create_from_string(b_str);
97 match = a->matches(a, b);
98 b->destroy(b);
99 return match == expected;
100 }
101
102 START_TEST(test_matches)
103 {
104 identification_t *a;
105
106 a = identification_create_from_string("C=CH, E=martin@strongswan.org, CN=martin");
107
108 ck_assert(id_matches(a, "C=CH, E=martin@strongswan.org, CN=martin", ID_MATCH_PERFECT));
109 ck_assert(id_matches(a, "C=CH, E=*, CN=martin", ID_MATCH_ONE_WILDCARD));
110 ck_assert(id_matches(a, "C=CH, E=*, CN=*", ID_MATCH_ONE_WILDCARD - 1));
111 ck_assert(id_matches(a, "C=*, E=*, CN=*", ID_MATCH_ONE_WILDCARD - 2));
112 ck_assert(id_matches(a, "C=*, E=*, CN=*, O=BADInc", ID_MATCH_NONE));
113 ck_assert(id_matches(a, "C=*, E=*", ID_MATCH_NONE));
114 ck_assert(id_matches(a, "C=*, E=a@b.c, CN=*", ID_MATCH_NONE));
115
116 a->destroy(a);
117 }
118 END_TEST
119
120 /*******************************************************************************
121 * identification part enumeration
122 */
123
124 START_TEST(test_parts)
125 {
126 identification_t *id;
127 enumerator_t *enumerator;
128 id_part_t part;
129 chunk_t data;
130 int i = 0;
131
132 id = identification_create_from_string("C=CH, O=strongSwan, CN=tester");
133
134 enumerator = id->create_part_enumerator(id);
135 while (enumerator->enumerate(enumerator, &part, &data))
136 {
137 switch (i++)
138 {
139 case 0:
140 ck_assert(part == ID_PART_RDN_C &&
141 chunk_equals(data, chunk_create("CH", 2)));
142 break;
143 case 1:
144 ck_assert(part == ID_PART_RDN_O &&
145 chunk_equals(data, chunk_from_str("strongSwan")));
146 break;
147 case 2:
148 ck_assert(part == ID_PART_RDN_CN &&
149 chunk_equals(data, chunk_from_str("tester")));
150 break;
151 default:
152 fail("unexpected identification part %d", part);
153 }
154 }
155 ck_assert_int_eq(i, 3);
156 enumerator->destroy(enumerator);
157 id->destroy(id);
158 }
159 END_TEST
160
161 /*******************************************************************************
162 * wildcards
163 */
164
165 static bool id_contains_wildcards(char *string)
166 {
167 identification_t *id;
168 bool contains;
169
170 id = identification_create_from_string(string);
171 contains = id->contains_wildcards(id);
172 id->destroy(id);
173 return contains;
174 }
175
176 START_TEST(test_contains_wildcards)
177 {
178 ck_assert(id_contains_wildcards("%any"));
179 ck_assert(id_contains_wildcards("C=*, O=strongSwan, CN=gw"));
180 ck_assert(id_contains_wildcards("C=CH, O=strongSwan, CN=*"));
181 ck_assert(id_contains_wildcards("*@strongswan.org"));
182 ck_assert(id_contains_wildcards("*.strongswan.org"));
183 ck_assert(!id_contains_wildcards("C=**, O=a*, CN=*a"));
184 }
185 END_TEST
186
187 Suite *identification_suite_create()
188 {
189 Suite *s;
190 TCase *tc;
191
192 s = suite_create("identification");
193
194 tc = tcase_create("equals");
195 tcase_add_test(tc, test_equals);
196 suite_add_tcase(s, tc);
197
198 tc = tcase_create("matches");
199 tcase_add_test(tc, test_matches);
200 suite_add_tcase(s, tc);
201
202 tc = tcase_create("part enumeration");
203 tcase_add_test(tc, test_parts);
204 suite_add_tcase(s, tc);
205
206 tc = tcase_create("wildcards");
207 tcase_add_test(tc, test_contains_wildcards);
208 suite_add_tcase(s, tc);
209
210 return s;
211 }