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