OpenDNSSEC-enforcer  1.4.6
test_ksm_key_delete.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_key_delete.c - Test Key Delete Module
29  *
30  * Description:
31  * This is a short test module to check the functions in the Ksm Key Delete
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 "test_routines.h"
47 
48 
49 /*+
50  * TestKsmKeyDeleteRange - Test KsmDeleteKeyRange code
51  *
52  * Description:
53  * Tests that a key range can be deleted
54 -*/
55 
56 static void TestKsmKeyDeleteRange(void)
57 {
58  char* sql; /* Constructed query */
59  int status; /* Status return */
60  int where = 0; /* WHERE clause count */
61  int rowcount; /* Result */
62 
63  /* First check that the rows exist */
64  sql = DqsCountInit("KEYDATA_VIEW");
65  DqsConditionInt(&sql, "ID", DQS_COMPARE_GT, 2, where++);
66  DqsConditionInt(&sql, "ID", DQS_COMPARE_LT, 5, where++);
67  DqsEnd(&sql);
68  status = DbIntQuery(DbHandle(), &rowcount, sql);
69  CU_ASSERT_EQUAL(status, 0);
70 
71  CU_ASSERT_EQUAL(rowcount, 2);
72 
73  /* Delete some */
74  status = KsmDeleteKeyRange(3, 4);
75  CU_ASSERT_EQUAL(status, 0);
76 
77  /* Make sure that they have gone */
78  status = DbIntQuery(DbHandle(), &rowcount, sql);
79  CU_ASSERT_EQUAL(status, 0);
80  DqsFree(sql);
81 
82  CU_ASSERT_EQUAL(rowcount, 0);
83 
84  /* Check that no other keys were harmed */
85  where = 0;
86  sql = DqsCountInit("KEYDATA_VIEW");
87  DqsConditionInt(&sql, "ID", DQS_COMPARE_GE, 1, where++);
88  DqsConditionInt(&sql, "ID", DQS_COMPARE_LE, 7, where++);
89  DqsEnd(&sql);
90  status = DbIntQuery(DbHandle(), &rowcount, sql);
91  CU_ASSERT_EQUAL(status, 0);
92  DqsFree(sql);
93 
94  /* 6 expected because key 1 has 2 instances */
95  CU_ASSERT_EQUAL(rowcount, 6);
96 
97  /*
98  * Start again, this time we will put min and max in the "wrong" way round
99  * First check that the rows exist
100  */
101  where = 0;
102  sql = DqsCountInit("KEYDATA_VIEW");
103  DqsConditionInt(&sql, "ID", DQS_COMPARE_GT, 4, where++);
104  DqsConditionInt(&sql, "ID", DQS_COMPARE_LT, 7, where++);
105  DqsEnd(&sql);
106  status = DbIntQuery(DbHandle(), &rowcount, sql);
107  CU_ASSERT_EQUAL(status, 0);
108 
109  CU_ASSERT_EQUAL(rowcount, 2);
110 
111  /* Delete some */
112  status = KsmDeleteKeyRange(6, 5);
113  CU_ASSERT_EQUAL(status, 0);
114 
115  /* Make sure that they have gone */
116  status = DbIntQuery(DbHandle(), &rowcount, sql);
117  CU_ASSERT_EQUAL(status, 0);
118  DqsFree(sql);
119 
120  CU_ASSERT_EQUAL(rowcount, 0);
121 
122  /* Check that no other keys were harmed */
123  where = 0;
124  sql = DqsCountInit("KEYDATA_VIEW");
125  DqsConditionInt(&sql, "ID", DQS_COMPARE_GE, 1, where++);
126  DqsConditionInt(&sql, "ID", DQS_COMPARE_LE, 7, where++);
127  DqsEnd(&sql);
128  status = DbIntQuery(DbHandle(), &rowcount, sql);
129  CU_ASSERT_EQUAL(status, 0);
130  DqsFree(sql);
131 
132  /* 4 expected because key 1 has 2 instances */
133  CU_ASSERT_EQUAL(rowcount, 4);
134 }
135 
136 /*+
137  * TestKsmKeyDeleteRanges - Test KsmDeleteKeyRanges code
138  *
139  * Description:
140  * Tests that key ranges can be deleted
141 -*/
142 
143 static void TestKsmKeyDeleteRanges(void)
144 {
145  char* sql; /* Constructed query */
146  int status; /* Status return */
147  int where = 0; /* WHERE clause count */
148  int rowcount; /* Result */
149  int limit[4]; /* ranges to delete */
150  int size; /* size of limit vector */
151 
152  /* First check that the rows exist */
153  sql = DqsCountInit("KEYDATA_VIEW");
154  DqsConditionInt(&sql, "ID", DQS_COMPARE_GT, 8, where++);
155  DqsConditionInt(&sql, "ID", DQS_COMPARE_LT, 14, where++);
156  DqsEnd(&sql);
157  status = DbIntQuery(DbHandle(), &rowcount, sql);
158  CU_ASSERT_EQUAL(status, 0);
159 
160  CU_ASSERT_EQUAL(rowcount, 5);
161 
162  /* Delete some */
163  limit[0] = 9;
164  limit[1] = 10;
165  limit[2] = 13;
166  limit[3] = 12;
167  size = 4;
168  status = KsmDeleteKeyRanges(limit, size);
169  CU_ASSERT_EQUAL(status, 0);
170 
171  /* Make sure that they have gone */
172  status = DbIntQuery(DbHandle(), &rowcount, sql);
173  CU_ASSERT_EQUAL(status, 0);
174  DqsFree(sql);
175 
176  CU_ASSERT_EQUAL(rowcount, 1);
177 
178  /* Check that no other keys were harmed */
179  where = 0;
180  sql = DqsCountInit("KEYDATA_VIEW");
181  DqsConditionInt(&sql, "ID", DQS_COMPARE_GE, 8, where++);
182  DqsConditionInt(&sql, "ID", DQS_COMPARE_LE, 15, where++);
183  DqsEnd(&sql);
184  status = DbIntQuery(DbHandle(), &rowcount, sql);
185  DqsFree(sql);
186  CU_ASSERT_EQUAL(status, 0);
187 
188  CU_ASSERT_EQUAL(rowcount, 4);
189 
190  where = 0;
191  sql = DqsCountInit("KEYDATA_VIEW");
192  DqsConditionInt(&sql, "ID", DQS_COMPARE_EQ, 11, where++);
193  DqsEnd(&sql);
194  status = DbIntQuery(DbHandle(), &rowcount, sql);
195  DqsFree(sql);
196  CU_ASSERT_EQUAL(status, 0);
197 
198  CU_ASSERT_EQUAL(rowcount, 1);
199 
200  /* TODO what happens if the limit vector is not set? */
201 }
202 
203 /*
204  * TestKsmKeyDelete - Create Test Suite
205  *
206  * Description:
207  * Adds the test suite to the CUnit test registry and adds all the tests
208  * to it.
209  *
210  * Arguments:
211  * None.
212  *
213  * Returns:
214  * int
215  * Return status. 0 => Success.
216  */
217 
218 int TestKsmKeyDelete(void); /* Declaration */
220 {
221  struct test_testdef tests[] = {
222  {"KsmKeyDeleteRange", TestKsmKeyDeleteRange},
223  {"KsmKeyDeleteRanges", TestKsmKeyDeleteRanges},
224  {NULL, NULL}
225  };
226 
227  /* TODO
228  * have been a bit lazy here and reuse TdbSetup etc...
229  * this has the consequence of all the setups running for each suite
230  * if this gets too slow then we will need to separate them out
231  * */
232  return TcuCreateSuite("KsmKeyDelete", TdbSetup, TdbTeardown, tests);
233 }
int KsmDeleteKeyRanges(int limit[], int size)
int TestKsmKeyDelete(void)
int TcuCreateSuite(const char *title, int(*init)(), int(*teardown)(), struct test_testdef *tests)
void DqsFree(char *query)
Definition: dq_string.c:320
char * DqsCountInit(const char *table)
Definition: dq_string.c:90
DB_HANDLE DbHandle(void)
void DqsConditionInt(char **query, const char *field, DQS_COMPARISON compare, int value, int index)
Definition: dq_string.c:224
int DbIntQuery(DB_HANDLE handle, int *value, const char *query)
int TdbTeardown(void)
int TdbSetup(void)
int KsmDeleteKeyRange(int minid, int maxid)
void DqsEnd(char **query)
Definition: dq_string.c:299