OpenDNSSEC-enforcer  1.3.16
test_dd_string.c
Go to the documentation of this file.
1 /*
2  * $Id: test_dd_string.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_dd_string.c - Test dd_string
31  *
32  * Description:
33  * This is a short test module to check the functions in the code that
34  * constructs a DELETE statement.
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/database_statement.h"
48 #include "test_routines.h"
49 
50 
51 
52 /*+
53  * TestDdsBasic - Test Basic Dds Routines
54  *
55  * Description:
56  * Constructs a database DELETE statement and checks the string so
57  * constructed.
58 -*/
59 
60 static void TestDdsBasic(void)
61 {
62  char* sql = NULL;
63 
64  sql = DdsInit("TEST");
65  DdsEnd(&sql);
66 
67  CU_ASSERT_STRING_EQUAL(sql, "DELETE FROM TEST");
68  DdsFree(sql);
69 
70  return;
71 }
72 
73 /*+
74  * TestDdsConditionInt - Test Conditional
75  *
76  * Description:
77  * Checks that the deletion can be constrained by a WHERE clause comparing
78  * fields to integers.
79 -*/
80 
81 static void TestDdsConditionInt(void)
82 {
83  char* sql = NULL;
84  int clause = 0;
85 
86  sql = DdsInit("TEST");
87  DdsConditionInt(&sql, "ALPHA", DQS_COMPARE_LT, 1, clause++);
88  DdsConditionInt(&sql, "BETA", DQS_COMPARE_LE, 2, clause++);
89  DdsConditionInt(&sql, "GAMMA", DQS_COMPARE_EQ, 3, clause++);
90  DdsConditionInt(&sql, "DELTA", DQS_COMPARE_NE, 4, clause++);
91  DdsConditionInt(&sql, "EPSILON", DQS_COMPARE_GE, 5, clause++);
92  DdsConditionInt(&sql, "ZETA", DQS_COMPARE_GT, 6, clause++);
93  DdsEnd(&sql);
94 
95  CU_ASSERT_STRING_EQUAL(sql,
96  "DELETE FROM TEST WHERE ALPHA < 1 AND BETA <= 2 AND GAMMA = 3 "
97  "AND DELTA != 4 AND EPSILON >= 5 AND ZETA > 6");
98  DdsFree(sql);
99 
100  return;
101 }
102 
103 /*+
104  * TestDdsConditionString - Test Conditional
105  *
106  * Description:
107  * Checks that the deletion can be constrained by a WHERE clause comparing
108  * fields to strings.
109 -*/
110 
111 static void TestDdsConditionString(void)
112 {
113  char* sql = NULL;
114  int clause = 0;
115  static const char* TEST =
116  "DELETE FROM TEST WHERE ALPHA < \"PETER\" AND BETA <= \"PIPER\" "
117  "AND GAMMA = \"PICKED\" AND DELTA != \"A\" AND EPSILON >= \"PECK\" "
118  "AND ZETA > \"OF\"";
119 
120  sql = DdsInit("TEST");
121  DdsConditionString(&sql, "ALPHA", DQS_COMPARE_LT, "PETER", clause++);
122  DdsConditionString(&sql, "BETA", DQS_COMPARE_LE, "PIPER", clause++);
123  DdsConditionString(&sql, "GAMMA", DQS_COMPARE_EQ, "PICKED", clause++);
124  DdsConditionString(&sql, "DELTA", DQS_COMPARE_NE, "A", clause++);
125  DdsConditionString(&sql, "EPSILON", DQS_COMPARE_GE, "PECK", clause++);
126  DdsConditionString(&sql, "ZETA", DQS_COMPARE_GT, "OF", clause++);
127  DdsEnd(&sql);
128 
129  CU_ASSERT_STRING_EQUAL(sql, TEST);
130  DdsFree(sql);
131 
132  return;
133 }
134 
135 /*+
136  * TestDdsConditionKeyword - Test Conditional
137  *
138  * Description:
139  * Checks that the deletion can be constrained by a WHERE clause comprising
140  * an IN clause.
141 -*/
142 
143 
144 static void TestDdsConditionKeyword(void)
145 {
146  char* sql = NULL;
147  int clause = 0;
148  static const char* TEST =
149  "DELETE FROM TEST WHERE ALPHA IN (1, 2, 3) "
150  "AND BETA IN (\"ALEPH\", \"BETH\")";
151 
152  sql = DdsInit("TEST");
153  DdsConditionKeyword(&sql, "ALPHA", DQS_COMPARE_IN, "(1, 2, 3)", clause++);
154  DdsConditionKeyword(&sql, "BETA", DQS_COMPARE_IN, "(\"ALEPH\", \"BETH\")",
155  clause++);
156  DdsEnd(&sql);
157 
158  CU_ASSERT_STRING_EQUAL(sql, TEST);
159  DdsFree(sql);
160 
161  return;
162 }
163 
164 
165 /*+
166  * TestDds - Create Test Suite
167  *
168  * Description:
169  * Adds the test suite to the CUnit test registry and adds all the tests
170  * to it.
171  *
172  * Arguments:
173  * None.
174  *
175  * Returns:
176  * int
177  * Return status. 0 => Success.
178  */
179 
180 int TestDds(void); /* Declaration */
181 int TestDds(void)
182 {
183  struct test_testdef tests[] = {
184  {"TestDdsBasic", TestDdsBasic},
185  {"TestDdsConditionInt", TestDdsConditionInt},
186  {"TestDdsConditionString", TestDdsConditionString},
187  {"TestDdsConditionKeyword", TestDdsConditionKeyword},
188  {NULL, NULL}
189  };
190 
191  return TcuCreateSuite("Dds", NULL, NULL, tests);
192 }
int TestDds(void)
int TcuCreateSuite(const char *title, int(*init)(), int(*teardown)(), struct test_testdef *tests)
void DdsConditionKeyword(char **query, const char *field, DQS_COMPARISON compare, const char *value, int index)
Definition: dd_string.c:104
void DdsFree(char *query)
Definition: dd_string.c:117
void DdsConditionInt(char **query, const char *field, DQS_COMPARISON compare, int value, int index)
Definition: dd_string.c:90
void DdsConditionString(char **query, const char *field, DQS_COMPARISON compare, const char *value, int index)
Definition: dd_string.c:97
char * DdsInit(const char *table)
Definition: dd_string.c:62
void DdsEnd(char **query)
Definition: dd_string.c:111