Implement TKM kernel SA database (SAD)
[strongswan.git] / src / charon-tkm / tests / kernel_sad_tests.c
1 /*
2 * Copyright (C) 2012 Reto Buerki
3 * Copyright (C) 2012 Adrian-Ken Rueegsegger
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 "tkm_kernel_sad.h"
20
21 START_TEST(test_sad_creation)
22 {
23 tkm_kernel_sad_t *sad = NULL;
24
25 sad = tkm_kernel_sad_create();
26 fail_if(!sad, "Error creating tkm kernel SAD");
27
28 sad->destroy(sad);
29 }
30 END_TEST
31
32 START_TEST(test_insert)
33 {
34 host_t *addr = host_create_from_string("127.0.0.1", 1024);
35 tkm_kernel_sad_t *sad = tkm_kernel_sad_create();
36
37 fail_unless(sad->insert(sad, 1, addr, addr, 42, 50),
38 "Error inserting SAD entry");
39
40 sad->destroy(sad);
41 addr->destroy(addr);
42 }
43 END_TEST
44
45 START_TEST(test_insert_duplicate)
46 {
47 host_t *addr = host_create_from_string("127.0.0.1", 1024);
48 tkm_kernel_sad_t *sad = tkm_kernel_sad_create();
49
50 fail_unless(sad->insert(sad, 1, addr, addr, 42, 50),
51 "Error inserting SAD entry");
52 fail_if(sad->insert(sad, 1, addr, addr, 42, 50),
53 "Expected error inserting duplicate entry");
54
55 sad->destroy(sad);
56 addr->destroy(addr);
57 }
58 END_TEST
59
60 START_TEST(test_get_esa_id)
61 {
62 host_t *addr = host_create_from_string("127.0.0.1", 1024);
63 tkm_kernel_sad_t *sad = tkm_kernel_sad_create();
64 fail_unless(sad->insert(sad, 23, addr, addr, 42, 50),
65 "Error inserting SAD entry");
66 fail_unless(sad->get_esa_id(sad, addr, addr, 42, 50) == 23,
67 "Error getting esa id");
68 sad->destroy(sad);
69 addr->destroy(addr);
70 }
71 END_TEST
72
73 START_TEST(test_get_esa_id_nonexistent)
74 {
75 host_t *addr = host_create_from_string("127.0.0.1", 1024);
76 tkm_kernel_sad_t *sad = tkm_kernel_sad_create();
77 fail_unless(sad->get_esa_id(sad, addr, addr, 42, 50) == 0,
78 "Got esa id for nonexistent SAD entry");
79 sad->destroy(sad);
80 addr->destroy(addr);
81 }
82 END_TEST
83
84 START_TEST(test_remove)
85 {
86 host_t *addr = host_create_from_string("127.0.0.1", 1024);
87 tkm_kernel_sad_t *sad = tkm_kernel_sad_create();
88 fail_unless(sad->insert(sad, 23, addr, addr, 42, 50),
89 "Error inserting SAD entry");
90 fail_unless(sad->get_esa_id(sad, addr, addr, 42, 50) == 23,
91 "Error getting esa id");
92 fail_unless(sad->remove(sad, 23),
93 "Error removing SAD entry");
94 fail_unless(sad->get_esa_id(sad, addr, addr, 42, 50) == 0,
95 "Got esa id for removed SAD entry");
96 sad->destroy(sad);
97 addr->destroy(addr);
98 }
99 END_TEST
100
101 START_TEST(test_remove_nonexistent)
102 {
103 tkm_kernel_sad_t *sad = tkm_kernel_sad_create();
104 fail_if(sad->remove(sad, 1),
105 "Expected error removing nonexistent SAD entry");
106 sad->destroy(sad);
107 }
108 END_TEST
109
110 TCase *make_kernel_sad_tests(void)
111 {
112 TCase *tc = tcase_create("Kernel SAD tests");
113 tcase_add_test(tc, test_sad_creation);
114 tcase_add_test(tc, test_insert);
115 tcase_add_test(tc, test_insert_duplicate);
116 tcase_add_test(tc, test_get_esa_id);
117 tcase_add_test(tc, test_get_esa_id_nonexistent);
118 tcase_add_test(tc, test_remove);
119 tcase_add_test(tc, test_remove_nonexistent);
120
121 return tc;
122 }