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