OpenDNSSEC-enforcer  1.4.7
test_ksm_parameter.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_parameter.c - Test Key Parameter Module
29  *
30  * Description:
31  * This is a short test module to check the functions in the Ksm Parameter
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  * TestKsmParameterSet - Test Parameter Set code
52  *
53  * Description:
54  * Tests that a parameter can be set
55 -*/
56 
57 static void TestKsmParameterSet(void)
58 {
59  char* sql; /* Constructed query */
60  int status; /* Status return */
61  int where = 0; /* WHERE clause count */
62  char buffer[2]; /* User buffer */
63  DB_RESULT result; /* Result object */
64  DB_ROW row; /* Row object */
65 
66  /* Check that a genuine parameter can be set (for the first time) */
67  status = KsmParameterSet("Blah","Test", 2, 2);
68  CU_ASSERT_EQUAL(status, 0);
69 
70  sql = DqsSpecifyInit("PARAMETER_VIEW", DB_PARAMETER_VIEW_FIELDS);
71  DqsConditionString(&sql, "NAME", DQS_COMPARE_EQ, "Blah", where++);
72  DqsConditionString(&sql, "CATEGORY", DQS_COMPARE_EQ, "Test", where++);
73  DqsEnd(&sql);
74  status = DbExecuteSql(DbHandle(), sql, &result);
75  CU_ASSERT_EQUAL(status, 0);
76  DqsFree(sql);
77 
78  status = DbFetchRow(result, &row);
79  CU_ASSERT_EQUAL(status, 0);
80  status = DbStringBuffer(row, DB_PARAMETER_VALUE, buffer, sizeof(buffer));
81  CU_ASSERT_EQUAL(status, 0);
82  CU_ASSERT_STRING_EQUAL(buffer, "2");
83 
84  DbFreeRow(row);
85  DbFreeResult(result);
86 
87  /* Check that an existing parameter can be overwritten */
88  status = KsmParameterSet("Blah2", "Test", 2, 2);
89  CU_ASSERT_EQUAL(status, 0);
90 
91  where = 0;
92  sql = DqsSpecifyInit("PARAMETER_VIEW", DB_PARAMETER_VIEW_FIELDS);
93  DqsConditionString(&sql, "NAME", DQS_COMPARE_EQ, "Blah2", where++);
94  DqsConditionString(&sql, "CATEGORY", DQS_COMPARE_EQ, "Test", where++);
95  DqsEnd(&sql);
96  status = DbExecuteSql(DbHandle(), sql, &result);
97  CU_ASSERT_EQUAL(status, 0);
98  DqsFree(sql);
99 
100  status = DbFetchRow(result, &row);
101  CU_ASSERT_EQUAL(status, 0);
102  status = DbStringBuffer(row, DB_PARAMETER_VALUE, buffer, sizeof(buffer));
103  CU_ASSERT_EQUAL(status, 0);
104  CU_ASSERT_STRING_EQUAL(buffer, "2");
105 
106  /* Check that a non-existing parameter can not be */
107  status = KsmParameterSet("Blah3", "Test", 2, 2);
108  CU_ASSERT_EQUAL(status, 65548); /* Parameter doesn't exist */
109 
110  DbFreeRow(row);
111  DbFreeResult(result);
112 }
113 
114 /*+
115  * TestKsmParameterShow - Test Parameter Show code
116  *
117  * Description:
118  * Tests that a parameter can be shown
119 -*/
120 
121 static void TestKsmParameterShow(void)
122 {
123  int status; /* Status return */
124 
125  /*
126  * Check that an existing parameter can be shown
127  * not sure how useful this is as a test
128  */
129  status = KsmParameterShow("Blah", "Test", 2);
130  CU_ASSERT_EQUAL(status, 0);
131 
132 }
133 
134 /*
135  * TestKsmParameter - Create Test Suite
136  *
137  * Description:
138  * Adds the test suite to the CUnit test registry and adds all the tests
139  * to it.
140  *
141  * Arguments:
142  * None.
143  *
144  * Returns:
145  * int
146  * Return status. 0 => Success.
147  */
148 
149 int TestKsmParameter(void); /* Declaration */
151 {
152  struct test_testdef tests[] = {
153  {"KsmParameterSet", TestKsmParameterSet},
154  {"KsmParameterShow", TestKsmParameterShow},
155  {NULL, NULL}
156  };
157 
158  /* TODO
159  * have been a bit lazy here and reuse TdbSetup etc...
160  * this has the consequence of all the setups running for each suite
161  * if this gets too slow then we will need to separate them out
162  * */
163  return TcuCreateSuite("KsmParameter", TdbSetup, TdbTeardown, tests);
164 }
void DbFreeResult(DB_RESULT result)
int DbFetchRow(DB_RESULT result, DB_ROW *row)
char * DqsSpecifyInit(const char *table, const char *fields)
Definition: dq_string.c:117
int TcuCreateSuite(const char *title, int(*init)(), int(*teardown)(), struct test_testdef *tests)
#define DB_PARAMETER_VIEW_FIELDS
Definition: db_fields.h:75
void DqsFree(char *query)
Definition: dq_string.c:320
DB_HANDLE DbHandle(void)
void DbFreeRow(DB_ROW row)
int DbExecuteSql(DB_HANDLE handle, const char *stmt_str, DB_RESULT *result)
int DbStringBuffer(DB_ROW row, int field_index, char *buffer, size_t buflen)
int TestKsmParameter(void)
int KsmParameterShow(const char *name, const char *category, int policy_id)
int TdbTeardown(void)
#define DB_PARAMETER_VALUE
Definition: db_fields.h:80
int TdbSetup(void)
int KsmParameterSet(const char *name, const char *category, int value, int policy_id)
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