added unit test for identification_t.contains_wildcard()
[strongswan.git] / src / charon / plugins / unit_tester / tests / test_id.c
1 /*
2 * Copyright (C) 2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <daemon.h>
17
18 /*******************************************************************************
19 * identification part enumeration test
20 ******************************************************************************/
21 bool test_id_parts()
22 {
23 identification_t *id;
24 enumerator_t *enumerator;
25 id_part_t part;
26 chunk_t data;
27 int i = 0;
28
29 id = identification_create_from_string("C=CH, O=strongSwan, CN=tester");
30
31 enumerator = id->create_part_enumerator(id);
32 while (enumerator->enumerate(enumerator, &part, &data))
33 {
34 switch (i++)
35 {
36 case 0:
37 if (part != ID_PART_RDN_C ||
38 !chunk_equals(data, chunk_create("CH", 2)))
39 {
40 return FALSE;
41 }
42 break;
43 case 1:
44 if (part != ID_PART_RDN_O ||
45 !chunk_equals(data, chunk_create("strongSwan", 10)))
46 {
47 return FALSE;
48 }
49 break;
50 case 2:
51 if (part != ID_PART_RDN_CN ||
52 !chunk_equals(data, chunk_create("tester", 6)))
53 {
54 return FALSE;
55 }
56 break;
57 default:
58 return FALSE;
59 }
60 }
61 if (i < 3)
62 {
63 return FALSE;
64 }
65 enumerator->destroy(enumerator);
66 id->destroy(id);
67 return TRUE;
68 }
69
70 /*******************************************************************************
71 * identification contains_wildcards() test
72 ******************************************************************************/
73
74 static bool test_id_wildcards_has(char *string)
75 {
76 identification_t *id;
77 bool contains;
78
79 id = identification_create_from_string(string);
80 contains = id->contains_wildcards(id);
81 id->destroy(id);
82 return contains;
83 }
84
85 bool test_id_wildcards()
86 {
87 if (!test_id_wildcards_has("C=*, O=strongSwan, CN=gw"))
88 {
89 return FALSE;
90 }
91 if (!test_id_wildcards_has("C=CH, O=strongSwan, CN=*"))
92 {
93 return FALSE;
94 }
95 if (test_id_wildcards_has("C=**, O=a*, CN=*a"))
96 {
97 return FALSE;
98 }
99 if (!test_id_wildcards_has("*@strongswan.org"))
100 {
101 return FALSE;
102 }
103 if (!test_id_wildcards_has("*.strongswan.org"))
104 {
105 return FALSE;
106 }
107 return TRUE;
108 }
109