OpenDNSSEC-enforcer  1.4.1
test_ksm_import.c
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Copyright (c) 2008-2009 Nominet UK. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
29 /*+
30  * Filename: test_ksm_import.c - Test ksm_import Module
31  *
32  * Description:
33  * This is a short test module to check the function in the Ksm Import
34  * module.
35  *
36  * The test program makes use of the CUnit framework, as described in
37  * http://cunit.sourceforge.net
38 -*/
39 
40 #include <stdlib.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <time.h>
44 
45 #include "CUnit/Basic.h"
46 
47 #include "ksm/ksm.h"
48 #include "ksm/db_fields.h"
49 #include "test_routines.h"
50 
51 
52 /*+
53  * TestKsmImportRepository - Test
54  *
55  * Description:
56  * Tests that a) we can create a new repository, and
57  * b) we can update an existing repository
58 -*/
59 
60 static void TestKsmImportRepository(void)
61 {
62  char* sql = NULL; /* SQL query */
63  int status = 0; /* Status return */
64  int count = 0; /* Do we already have a repository with this name? */
65 
66  char* repo_name = "myNewRepo";
67  char* repo_capacity = "500";
68 
69  /* Show that the repository X doesn't exist */
71  DqsConditionString(&sql, "NAME", DQS_COMPARE_EQ, repo_name, 0);
72  DqsEnd(&sql);
73 
74  /* Execute query and free up the query string */
75  status = DbIntQuery(DbHandle(), &count, sql);
76  CU_ASSERT_EQUAL(status, 0);
77  CU_ASSERT_EQUAL(count, 0);
78 
79  /* Create X */
80  status = KsmImportRepository(repo_name, repo_capacity, 0);
81  CU_ASSERT_EQUAL(status, 0);
82 
83  /* Show that the repository X does now exist */
84  status = DbIntQuery(DbHandle(), &count, sql);
85  DqsFree(sql);
86  CU_ASSERT_EQUAL(status, 0);
87  CU_ASSERT_EQUAL(count, 1);
88 
89  /* Get the capacity of X */
90  sql = DqsSpecifyInit(DB_SECURITY_MODULE_TABLE,"capacity");
91  DqsConditionString(&sql, "name", DQS_COMPARE_EQ, repo_name, 0);
92  DqsEnd(&sql);
93 
94  status = DbIntQuery(DbHandle(), &count, sql);
95  CU_ASSERT_EQUAL(status, 0);
96  CU_ASSERT_EQUAL(count, 500);
97 
98  /* update X */
99  status = KsmImportRepository(repo_name, "5000", 0);
100  CU_ASSERT_EQUAL(status, 0);
101 
102  /* Get the new capacity */
103  status = DbIntQuery(DbHandle(), &count, sql);
104  DqsFree(sql);
105  CU_ASSERT_EQUAL(status, 0);
106  CU_ASSERT_EQUAL(count, 5000);
107 
108 
109 }
110 
111 /*+
112  * TestKsmImportPolicy - Test
113  *
114  * Description:
115  * Tests that we can create a new policy
116 -*/
117 static void TestKsmImportPolicy(void)
118 {
119  char* sql = NULL; /* SQL query */
120  int status = 0; /* Status return */
121  int count = 0; /* Do we already have a repository with this name? */
122 
123  char* policy_name = "myNewPolicy";
124  char* policy_desc = "Pretty policy";
125 
126  /* Show that the policy X doesn't exist */
127  sql = DqsCountInit("policies");
128  DqsConditionString(&sql, "NAME", DQS_COMPARE_EQ, policy_name, 0);
129  DqsEnd(&sql);
130 
131  /* Execute query and free up the query string */
132  status = DbIntQuery(DbHandle(), &count, sql);
133  CU_ASSERT_EQUAL(status, 0);
134  CU_ASSERT_EQUAL(count, 0);
135 
136  /* Create X */
137  status = KsmImportPolicy(policy_name, policy_desc);
138  CU_ASSERT_EQUAL(status, 0);
139 
140  /* Show that the policy X does now exist */
141  status = DbIntQuery(DbHandle(), &count, sql);
142  DqsFree(sql);
143  CU_ASSERT_EQUAL(status, 0);
144  CU_ASSERT_EQUAL(count, 1);
145 }
146 
147 /*+
148  * TestKsmImportZone - Test
149  *
150  * Description:
151  * Tests that a) we can create a new Zone, and
152  * b) we can update an existing Zone
153 -*/
154 
155 static void TestKsmImportZone(void)
156 {
157  char* sql = NULL; /* SQL query */
158  int status = 0; /* Status return */
159  int count = 0; /* Do we already have a repository with this name? */
160 
161  char* zone_name = "myNewZone.test";
162  int policy_id = 1;
163  int new_zone = 0;
164 
165  /* Show that the Zone X doesn't exist */
167  DqsConditionString(&sql, "NAME", DQS_COMPARE_EQ, zone_name, 0);
168  DqsEnd(&sql);
169 
170  /* Execute query and free up the query string */
171  status = DbIntQuery(DbHandle(), &count, sql);
172  CU_ASSERT_EQUAL(status, 0);
173  CU_ASSERT_EQUAL(count, 0);
174 
175  /* Create X */
176  status = KsmImportZone(zone_name, policy_id, 1, &new_zone, "signconf", "input", "output", "File", "File");
177  CU_ASSERT_EQUAL(status, 0);
178  CU_ASSERT_EQUAL(new_zone, 1);
179 
180  /* Show that the Zone X does now exist */
181  status = DbIntQuery(DbHandle(), &count, sql);
182  DqsFree(sql);
183  CU_ASSERT_EQUAL(status, 0);
184  CU_ASSERT_EQUAL(count, 1);
185 
186  /* Get the policy of X */
187  sql = DqsSpecifyInit(DB_ZONE_TABLE,"policy_id");
188  DqsConditionString(&sql, "name", DQS_COMPARE_EQ, zone_name, 0);
189  DqsEnd(&sql);
190 
191  status = DbIntQuery(DbHandle(), &count, sql);
192  CU_ASSERT_EQUAL(status, 0);
193  CU_ASSERT_EQUAL(count, 1);
194 
195  /* update X */
196  status = KsmImportZone(zone_name, 2, 0, &new_zone, "signconf", "input", "output", "File", "File");
197  CU_ASSERT_EQUAL(status, 0);
198  CU_ASSERT_EQUAL(new_zone, 0);
199 
200  /* Get the new policy */
201  status = DbIntQuery(DbHandle(), &count, sql);
202  DqsFree(sql);
203  CU_ASSERT_EQUAL(status, 0);
204  CU_ASSERT_EQUAL(count, 2);
205 
206 
207 }
208 
209 /*+
210  * TestKsmSerialIdFromName - Test
211  *
212  * Description:
213  * Tests that a serial id can be returned
214 -*/
215 
216 static void TestKsmSerialIdFromName(void)
217 {
218  int status; /* Status return */
219  int serial_id; /* returned id */
220 
221  char* serial1 = "unixtime";
222  char* serial2 = "somethingElse";
223 
224  /* get the first repo */
225  status = KsmSerialIdFromName(serial1, &serial_id);
226  CU_ASSERT_EQUAL(status, 0);
227  CU_ASSERT_EQUAL(serial_id, 1);
228 
229  /* get the second repo */
230  status = KsmSerialIdFromName(serial2, &serial_id);
231  CU_ASSERT_EQUAL(status, 65557); /* doesn't exist */
232 
233 }
234 
235 /*
236  * TestKsmImport - Create Test Suite
237  *
238  * Description:
239  * Adds the test suite to the CUnit test registry and adds all the tests
240  * to it.
241  *
242  * Arguments:
243  * None.
244  *
245  * Returns:
246  * int
247  * Return status. 0 => Success.
248  */
249 
250 int TestKsmImport(void); /* Declaration */
251 int TestKsmImport(void)
252 {
253  struct test_testdef tests[] = {
254  {"KsmImportRepository", TestKsmImportRepository},
255  {"KsmImportPolicy", TestKsmImportPolicy},
256  {"KsmImportZone", TestKsmImportZone},
257  {"KsmSerialIdFromName", TestKsmSerialIdFromName},
258  {NULL, NULL}
259  };
260 
261  /* TODO
262  * have been a bit lazy here and reuse TdbSetup etc...
263  * this has the consequence of all the setups running for each suite
264  * if this gets too slow then we will need to separate them out
265  * */
266  return TcuCreateSuite("KsmImport", TdbSetup, TdbTeardown, tests);
267 }