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