OpenDNSSEC-enforcer  1.4.6
test_ksm_purge.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_purge.c - Test Key Purge Module
29  *
30  * Description:
31  * This is a short test module to check the function in the Ksm Purge
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  * TestKsmPurgeInternal - Test Key Purge code
51  *
52  * Description:
53  * Tests that all dead keys are removed when requested
54 -*/
55 
56 static void TestKsmPurgeInternal(void)
57 {
58  int rowcount; /* Number of rows returned */
59  char* sql; /* Constructed query */
60  char* sql2; /* Constructed query */
61  char* sql3; /* Constructed query */
62  int status; /* Status return */
63  int where = 0; /* WHERE clause count */
64 
65  /* Check that only one key is "dead" (STATE=6) */
66 
67  sql = DqsCountInit("dnsseckeys");
68  DqsConditionInt(&sql, "STATE", DQS_COMPARE_EQ, 6, where++);
69  StrAppend(&sql, " group by id");
70  DqsEnd(&sql);
71  status = DbIntQuery(DbHandle(), &rowcount, sql);
72  DqsFree(sql);
73 
74  CU_ASSERT_EQUAL(status, 0);
75  CU_ASSERT_EQUAL(rowcount, 1);
76 
77  /* With 2 entries in dnsseckeys */
78  where = 0;
79  sql2 = DqsCountInit("dnsseckeys");
80  DqsConditionInt(&sql2, "keypair_id", DQS_COMPARE_EQ, 1, where++);
81  DqsEnd(&sql2);
82  status = DbIntQuery(DbHandle(), &rowcount, sql2);
83 
84  CU_ASSERT_EQUAL(status, 0);
85  CU_ASSERT_EQUAL(rowcount, 2);
86 
87 
88  /* Call KsmPurge */
89  KsmPurge();
90 
91  /* Now make sure that we have no dead keys */
92  sql3 = DqsCountInit("dnsseckeys");
93  DqsConditionInt(&sql3, "STATE", DQS_COMPARE_EQ, 6, 0);
94  status = DbIntQuery(DbHandle(), &rowcount, sql3);
95  DqsFree(sql3);
96 
97  CU_ASSERT_EQUAL(status, 0);
98 
99  CU_ASSERT_EQUAL(rowcount, 0);
100 
101  /* Make sure that the entries in dnsseckeys have gone too */
102  status = DbIntQuery(DbHandle(), &rowcount, sql2);
103  DqsFree(sql2);
104 
105  CU_ASSERT_EQUAL(status, 0);
106  CU_ASSERT_EQUAL(rowcount, 0);
107 
108 }
109 
110 /*
111  * TestKsmPurge - Create Test Suite
112  *
113  * Description:
114  * Adds the test suite to the CUnit test registry and adds all the tests
115  * to it.
116  *
117  * Arguments:
118  * None.
119  *
120  * Returns:
121  * int
122  * Return status. 0 => Success.
123  */
124 
125 int TestKsmPurge(void); /* Declaration */
126 int TestKsmPurge(void)
127 {
128  struct test_testdef tests[] = {
129  {"KsmPurge", TestKsmPurgeInternal},
130  {NULL, NULL}
131  };
132 
133  /* TODO
134  * have been a bit lazy here and reuse TdbSetup etc...
135  * this has the consequence of all the setups running for each suite
136  * if this gets too slow then we will need to separate them out
137  * */
138  return TcuCreateSuite("KsmPurge", TdbSetup, TdbTeardown, tests);
139 }
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
void StrAppend(char **str1, const char *str2)
Definition: string_util2.c:76
int DbIntQuery(DB_HANDLE handle, int *value, const char *query)
int TdbTeardown(void)
int TestKsmPurge(void)
int TdbSetup(void)
void DqsEnd(char **query)
Definition: dq_string.c:299
void KsmPurge(void)
Definition: ksm_purge.c:52