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