- identification_t supports now almost all id types
[strongswan.git] / Source / testing / identification_test.c
1 /**
2 * @file identification_test.c
3 *
4 * @brief Tests for the identification_t class.
5 *
6 */
7
8 /*
9 * Copyright (C) 2006 Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include <string.h>
24
25 #include "identification_test.h"
26
27 #include <utils/identification.h>
28 #include <utils/logger.h>
29
30 /*
31 * described in Header-File
32 */
33 void test_identification(protected_tester_t *tester)
34 {
35 identification_t *a, *b, *c, *d;
36 bool result;
37
38 { /* test RFC822_ADDR */
39 char *bob_string = "bob@wonderland.net";
40 chunk_t bob_chunk = {bob_string, strlen(bob_string)};
41
42 a = identification_create_from_string("alice@wonderland.net");
43 b = identification_create_from_encoding(ID_RFC822_ADDR, bob_chunk);
44 c = identification_create_from_string("*@wonderland.net");
45 d = identification_create_from_string("*@badlands.com");
46
47 result = a->belongs_to(a, c);
48 tester->assert_true(tester, result, "alice belongs to wonderland");
49 result = b->belongs_to(b, c);
50 tester->assert_true(tester, result, "bob belongs to wonderland");
51 result = a->belongs_to(a, d);
52 tester->assert_false(tester, result, "alice does not belong to badlands");
53 result = b->belongs_to(b, d);
54 tester->assert_false(tester, result, "bob does not belong to badlands");
55 result = c->belongs_to(c, d);
56 tester->assert_false(tester, result, "wonderland is not in badlands");
57 result = a->belongs_to(a, a);
58 tester->assert_true(tester, result, "alice belongs to alice alice");
59 result = a->equals(a, a);
60 tester->assert_true(tester, result, "alice is alice");
61 result = a->equals(a, b);
62 tester->assert_false(tester, result, "alice is not bob");
63
64 a->destroy(a);
65 b->destroy(b);
66 c->destroy(c);
67 d->destroy(d);
68 }
69
70 { /* test FQDN */
71 char *bob_string = "@dave.nirvana.org";
72 chunk_t bob_chunk = {bob_string, strlen(bob_string)};
73
74 a = identification_create_from_string("@carol.nirvana.org");
75 b = identification_create_from_encoding(ID_FQDN, bob_chunk);
76 c = identification_create_from_string("@*.nirvana.org");
77 d = identification_create_from_string("@*.samsara.com");
78
79 result = a->belongs_to(a, c);
80 tester->assert_true(tester, result, "carol belongs to nirvana");
81 result = b->belongs_to(b, c);
82 tester->assert_true(tester, result, "dave belongs to nirvana");
83 result = a->belongs_to(a, d);
84 tester->assert_false(tester, result, "carol does not belong to samsara");
85 result = b->belongs_to(b, d);
86 tester->assert_false(tester, result, "dave does not belong to samsara");
87 result = c->belongs_to(c, d);
88 tester->assert_false(tester, result, "nirvana is not in samsara");
89 result = a->belongs_to(a, a);
90 tester->assert_true(tester, result, "carol belongs to carol carol");
91 result = a->equals(a, a);
92 tester->assert_true(tester, result, "carol is carol");
93 result = a->equals(a, b);
94 tester->assert_false(tester, result, "carol is not dave");
95
96 a->destroy(a);
97 b->destroy(b);
98 c->destroy(c);
99 d->destroy(d);
100 }
101
102
103 { /* test ID IPV4 ADDR, no wildcards yet */
104 char bob_addr[] = {192,168,0,2};
105 chunk_t bob_chunk = chunk_from_buf(bob_addr);
106
107 a = identification_create_from_string("192.168.0.1");
108 b = identification_create_from_encoding(ID_IPV4_ADDR, bob_chunk);
109 c = identification_create_from_string("192.168.0.2"); /* as bob */
110
111 result = a->equals(a, a);
112 tester->assert_true(tester, result, "IPV4_ADDR of alice equals IPV4_ADDR of alice");
113 result = b->equals(b, c);
114 tester->assert_true(tester, result, "IPV4_ADDR of bob equals IPV4_ADDR of carol");
115 result = a->equals(a, b);
116 tester->assert_false(tester, result, "IPV4_ADDR of alice doesn't equal IPV4_ADDR of bob");
117
118 a->destroy(a);
119 b->destroy(b);
120 c->destroy(c);
121 }
122
123 { /* test ID IPV6 ADDR, no wildcards yet */
124 char bob_addr[] = {0x20,0x01,0x0d,0xb8,0x85,0xa3,0x08,0xd3,0x13,0x19,0x8a,0x2e,0x03,0x70,0x73,0x44};
125 chunk_t bob_chunk = chunk_from_buf(bob_addr);
126
127 a = identification_create_from_string("2001:0db8:85a3:08d3:1319:8a2e:0370:7345");
128 b = identification_create_from_encoding(ID_IPV6_ADDR, bob_chunk);
129 c = identification_create_from_string("2001:0db8:85a3:08d3:1319:8a2e:0370:7344"); /* as bob */
130
131 result = a->equals(a, a);
132 tester->assert_true(tester, result, "IPV6_ADDR of alice equals IPV6_ADDR of alice");
133 result = b->equals(b, c);
134 tester->assert_true(tester, result, "IPV6_ADDR of bob equals IPV6_ADDR of carol");
135 result = a->equals(a, b);
136 tester->assert_false(tester, result, "IPV6_ADDR of alice doesn't equal IPV6_ADDR of bob");
137
138 a->destroy(a);
139 b->destroy(b);
140 c->destroy(c);
141 }
142
143 { /* test ID DER_ASN1_DN */
144 a = identification_create_from_string("C=CH, O=Linux strongSwan, CN=alice");
145 b = identification_create_from_string("O=Linux strongSwan, C=CH, CN=bob");
146 c = identification_create_from_string("C=CH, O=Linux strongSwan, CN=*");
147 d = identification_create_from_string("C=CH, O=Linux openswan, CN=*");
148
149 result = a->equals(a, a);
150 tester->assert_true(tester, result, "DN of alice equals DN of alice");
151 result = a->equals(a, b);
152 tester->assert_false(tester, result, "DN of alice doesn't equal DN of bob");
153 result = a->belongs_to(a, c);
154 tester->assert_true(tester, result, "DN of alice belongs to DN of carol");
155 /* TODO: This does NOT work, wildcard check should work with unordered RDNs */
156 result = b->belongs_to(b, c);
157 tester->assert_true(tester, result, "DN of bob belongs to DN of carol");
158 result = b->belongs_to(b, d);
159 tester->assert_false(tester, result, "DN of bob doesn't belong to DN of dave");
160
161 a->destroy(a);
162 b->destroy(b);
163 c->destroy(c);
164 d->destroy(d);
165 }
166 }