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