OpenDNSSEC-enforcer  1.4.7
test_du_string.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_dd_string.c - Test dd_string
29  *
30  * Description:
31  * This is a short test module to check the functions in the code that
32  * constructs a DELETE statement.
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/database_statement.h"
46 #include "test_routines.h"
47 
48 
49 
50 /*+
51  * TestDusSetInt - Test Basic Dus SET With Integer
52  *
53  * Description:
54  * Constructs a database UPDATE statement setting an integer attribute and
55  * checks the string so constructed.
56 -*/
57 
58 static void TestDusSetInt(void)
59 {
60  char* sql = NULL;
61  int set = 0;
62 
63  /* Check a single integer update */
64 
65  set = 0;
66  sql = DusInit("TEST");
67  DusSetInt(&sql, "ALPHA", 1, set++);
68  DusEnd(&sql);
69 
70  CU_ASSERT_STRING_EQUAL(sql, "UPDATE TEST SET ALPHA = 1");
71  DusFree(sql);
72 
73  /* Check multiple updates */
74 
75  set = 0;
76  sql = DusInit("TEST");
77  DusSetInt(&sql, "ALPHA", 1, set++);
78  DusSetInt(&sql, "BETA", 2, set++);
79  DusEnd(&sql);
80 
81  CU_ASSERT_STRING_EQUAL(sql, "UPDATE TEST SET ALPHA = 1, BETA = 2");
82  DusFree(sql);
83 
84  return;
85 }
86 
87 
88 
89 /*+
90  * TestDusSetString - Test Basic Dus SET With String
91  *
92  * Description:
93  * Constructs a database UPDATE statement setting a string attribute and
94  * checks the string so constructed.
95 -*/
96 
97 static void TestDusSetString(void)
98 {
99  char* sql = NULL;
100  int set = 0;
101 
102  /* Check a single string update */
103 
104  set = 0;
105  sql = DusInit("TEST");
106  DusSetString(&sql, "ALPHA", "XYZZY", set++);
107  DusEnd(&sql);
108 
109  CU_ASSERT_STRING_EQUAL(sql, "UPDATE TEST SET ALPHA = 'XYZZY'");
110  DusFree(sql);
111 
112  /* Check a single string update of a NULL value */
113 
114  set = 0;
115  sql = DusInit("TEST");
116  DusSetString(&sql, "BETA", NULL, set++);
117  DusEnd(&sql);
118 
119  CU_ASSERT_STRING_EQUAL(sql, "UPDATE TEST SET BETA = NULL");
120  DusFree(sql);
121 
122  /* Check a combination */
123 
124  set = 0;
125  sql = DusInit("TEST");
126  DusSetString(&sql, "ALPHA", "XYZZY", set++);
127  DusSetString(&sql, "BETA", NULL, set++);
128  DusEnd(&sql);
129 
130  CU_ASSERT_STRING_EQUAL(sql,
131  "UPDATE TEST SET ALPHA = 'XYZZY', BETA = NULL");
132  DusFree(sql);
133 
134  return;
135 }
136 
137 /*+
138  * TestDusConditionInt - Test Conditional
139  *
140  * Description:
141  * Checks that the deletion can be constrained by a WHERE clause comparing
142  * fields to integers.
143 -*/
144 
145 static void TestDusConditionInt(void)
146 {
147  char* sql = NULL;
148  int set = 0;
149  int where = 0;
150  static const char* TEST =
151  "UPDATE TEST SET ALPHA = 0 WHERE ALPHA < 1 AND BETA <= 2 AND GAMMA = 3 "
152  "AND DELTA != 4 AND EPSILON >= 5 AND ZETA > 6";
153 
154  sql = DusInit("TEST");
155  DusSetInt(&sql, "ALPHA", 0, set++);
156  DusConditionInt(&sql, "ALPHA", DQS_COMPARE_LT, 1, where++);
157  DusConditionInt(&sql, "BETA", DQS_COMPARE_LE, 2, where++);
158  DusConditionInt(&sql, "GAMMA", DQS_COMPARE_EQ, 3, where++);
159  DusConditionInt(&sql, "DELTA", DQS_COMPARE_NE, 4, where++);
160  DusConditionInt(&sql, "EPSILON", DQS_COMPARE_GE, 5, where++);
161  DusConditionInt(&sql, "ZETA", DQS_COMPARE_GT, 6, where++);
162  DusEnd(&sql);
163 
164  CU_ASSERT_STRING_EQUAL(sql, TEST);
165  DusFree(sql);
166 
167  return;
168 }
169 
170 /*+
171  * TestDusConditionString - Test Conditional
172  *
173  * Description:
174  * Checks that the deletion can be constrained by a WHERE clause comparing
175  * fields to strings.
176 -*/
177 
178 static void TestDusConditionString(void)
179 {
180  char* sql = NULL;
181  int set = 0;
182  int where = 0;
183  static const char* TEST =
184  "UPDATE TEST SET ALPHA = 0 "
185  "WHERE ALPHA < 'PETER' AND BETA <= 'PIPER' "
186  "AND GAMMA = 'PICKED' AND DELTA != 'A' AND EPSILON >= 'PECK' "
187  "AND ZETA > 'OF'";
188 
189  sql = DusInit("TEST");
190  DusSetInt(&sql, "ALPHA", 0, set++);
191  DusConditionString(&sql, "ALPHA", DQS_COMPARE_LT, "PETER", where++);
192  DusConditionString(&sql, "BETA", DQS_COMPARE_LE, "PIPER", where++);
193  DusConditionString(&sql, "GAMMA", DQS_COMPARE_EQ, "PICKED", where++);
194  DusConditionString(&sql, "DELTA", DQS_COMPARE_NE, "A", where++);
195  DusConditionString(&sql, "EPSILON", DQS_COMPARE_GE, "PECK", where++);
196  DusConditionString(&sql, "ZETA", DQS_COMPARE_GT, "OF", where++);
197  DusEnd(&sql);
198 
199  CU_ASSERT_STRING_EQUAL(sql, TEST);
200  DusFree(sql);
201 
202  return;
203 }
204 
205 /*+
206  * TestDusConditionKeyword - Test Conditional
207  *
208  * Description:
209  * Checks that the deletion can be constrained by a WHERE clause comprising
210  * an IN clause.
211 -*/
212 
213 
214 static void TestDusConditionKeyword(void)
215 {
216  char* sql = NULL;
217  int set = 0;
218  int where = 0;
219  static const char* TEST =
220  "UPDATE TEST SET ALPHA = 0, BETA = 'GIMMEL' WHERE ALPHA IN (1, 2, 3) "
221  "AND BETA IN ('ALEPH', 'BETH')";
222 
223  sql = DusInit("TEST");
224  DusSetInt(&sql, "ALPHA", 0, set++);
225  DusSetString(&sql, "BETA", "GIMMEL", set++);
226  DusConditionKeyword(&sql, "ALPHA", DQS_COMPARE_IN, "(1, 2, 3)", where++);
227  DusConditionKeyword(&sql, "BETA", DQS_COMPARE_IN, "('ALEPH', 'BETH')",
228  where++);
229  DusEnd(&sql);
230 
231  CU_ASSERT_STRING_EQUAL(sql, TEST);
232  DusFree(sql);
233 
234  return;
235 }
236 
237 
238 /*+
239  * TestDus - Create Test Suite
240  *
241  * Description:
242  * Adds the test suite to the CUnit test registry and adds all the tests
243  * to it.
244  *
245  * Arguments:
246  * None.
247  *
248  * Returns:
249  * int
250  * Return status. 0 => Success.
251  */
252 
253 int TestDus(void); /* Declaration */
254 int TestDus(void)
255 {
256  struct test_testdef tests[] = {
257  {"TestDusSetInt", TestDusSetInt},
258  {"TestDusSetString", TestDusSetString},
259  {"TestDusConditionInt", TestDusConditionInt},
260  {"TestDusConditionString", TestDusConditionString},
261  {"TestDusConditionKeyword", TestDusConditionKeyword},
262  {NULL, NULL}
263  };
264 
265  return TcuCreateSuite("Dus", NULL, NULL, tests);
266 }
void DusConditionKeyword(char **query, const char *field, DQS_COMPARISON compare, const char *value, int clause)
Definition: du_string.c:182
void DusFree(char *sql)
Definition: du_string.c:223
int TcuCreateSuite(const char *title, int(*init)(), int(*teardown)(), struct test_testdef *tests)
void DusConditionString(char **query, const char *field, DQS_COMPARISON compare, const char *value, int clause)
Definition: du_string.c:176
void DusSetInt(char **sql, const char *field, int data, int clause)
Definition: du_string.c:97
void DusConditionInt(char **query, const char *field, DQS_COMPARISON compare, int value, int clause)
Definition: du_string.c:170
void DusEnd(char **sql)
Definition: du_string.c:202
char * DusInit(const char *table)
Definition: du_string.c:60
void DusSetString(char **sql, const char *field, const char *data, int clause)
Definition: du_string.c:113
int TestDus(void)