OpenDNSSEC-enforcer  1.4.5
test_dq_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_dq_string.c - Test dq_string
29  *
30  * Description:
31  * This is a short test module to check the functions in the code that
32  * constructs a SELECT 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  * TestDqsBasic - Test Basic Dqs Routines
52  *
53  * Description:
54  * Constructs a database DELETE statement and checks the string so
55  * constructed.
56 -*/
57 
58 static void TestDqsBasic(void)
59 {
60  char* sql = NULL;
61 
62  sql = DqsInit("TEST");
63  DqsEnd(&sql);
64 
65  CU_ASSERT_STRING_EQUAL(sql, "SELECT * FROM TEST");
66  DqsFree(sql);
67 
68  sql = DqsCountInit("TEST");
69  DqsEnd(&sql);
70 
71  CU_ASSERT_STRING_EQUAL(sql, "SELECT COUNT(*) FROM TEST");
72  DqsFree(sql);
73 
74  return;
75 }
76 
77 /*+
78  * TestDqsConditionInt - Test Conditional
79  *
80  * Description:
81  * Checks that the deletion can be constrained by a WHERE clause comparing
82  * fields to integers.
83 -*/
84 
85 static void TestDqsConditionInt(void)
86 {
87  char* sql = NULL;
88  int clause = 0;
89 
90  sql = DqsCountInit("TEST");
91  DqsConditionInt(&sql, "ALPHA", DQS_COMPARE_LT, 1, clause++);
92  DqsConditionInt(&sql, "BETA", DQS_COMPARE_LE, 2, clause++);
93  DqsConditionInt(&sql, "GAMMA", DQS_COMPARE_EQ, 3, clause++);
94  DqsConditionInt(&sql, "DELTA", DQS_COMPARE_NE, 4, clause++);
95  DqsConditionInt(&sql, "EPSILON", DQS_COMPARE_GE, 5, clause++);
96  DqsConditionInt(&sql, "ZETA", DQS_COMPARE_GT, 6, clause++);
97  DqsEnd(&sql);
98 
99  CU_ASSERT_STRING_EQUAL(sql,
100  "SELECT COUNT(*) FROM TEST WHERE ALPHA < 1 AND BETA <= 2 AND GAMMA = 3 "
101  "AND DELTA != 4 AND EPSILON >= 5 AND ZETA > 6");
102  DqsFree(sql);
103 
104  return;
105 }
106 
107 /*+
108  * TestDqsConditionString - Test Conditional
109  *
110  * Description:
111  * Checks that the deletion can be constrained by a WHERE clause comparing
112  * fields to strings.
113 -*/
114 
115 static void TestDqsConditionString(void)
116 {
117  char* sql = NULL;
118  int clause = 0;
119  static const char* TEST =
120  "SELECT * FROM TEST WHERE ALPHA < 'PETER' AND BETA <= 'PIPER' "
121  "AND GAMMA = 'PICKED' AND DELTA != 'A' AND EPSILON >= 'PECK' "
122  "AND ZETA > 'OF'";
123 
124  sql = DqsInit("TEST");
125  DqsConditionString(&sql, "ALPHA", DQS_COMPARE_LT, "PETER", clause++);
126  DqsConditionString(&sql, "BETA", DQS_COMPARE_LE, "PIPER", clause++);
127  DqsConditionString(&sql, "GAMMA", DQS_COMPARE_EQ, "PICKED", clause++);
128  DqsConditionString(&sql, "DELTA", DQS_COMPARE_NE, "A", clause++);
129  DqsConditionString(&sql, "EPSILON", DQS_COMPARE_GE, "PECK", clause++);
130  DqsConditionString(&sql, "ZETA", DQS_COMPARE_GT, "OF", clause++);
131  DqsEnd(&sql);
132 
133  CU_ASSERT_STRING_EQUAL(sql, TEST);
134  DqsFree(sql);
135 
136  return;
137 }
138 
139 /*+
140  * TestDqsConditionKeyword - Test Conditional
141  *
142  * Description:
143  * Checks that the deletion can be constrained by a WHERE clause comprising
144  * an IN clause.
145 -*/
146 
147 
148 static void TestDqsConditionKeyword(void)
149 {
150  char* sql = NULL;
151  int clause = 0;
152  static const char* TEST =
153  "SELECT * FROM TEST WHERE ALPHA IN (1, 2, 3) "
154  "AND BETA IN (\"ALEPH\", \"BETH\")";
155 
156  sql = DqsInit("TEST");
157  DqsConditionKeyword(&sql, "ALPHA", DQS_COMPARE_IN, "(1, 2, 3)", clause++);
158  DqsConditionKeyword(&sql, "BETA", DQS_COMPARE_IN, "(\"ALEPH\", \"BETH\")",
159  clause++);
160  DqsEnd(&sql);
161 
162  CU_ASSERT_STRING_EQUAL(sql, TEST);
163  DqsFree(sql);
164 
165  return;
166 }
167 
168 /*+
169  * TestDqsOrderBy - Test ORDER BY Clause
170  *
171  * Description:
172  * Checks that the deletion can be constrained by a WHERE clause comprising
173  * an IN clause.
174 -*/
175 
176 
177 static void TestDqsOrderBy(void)
178 {
179  char* sql = NULL;
180  int clause = 0;
181  static const char* TEST =
182  "SELECT * FROM TEST WHERE ALPHA IN (1, 2, 3) ORDER BY BETA";
183 
184  sql = DqsInit("TEST");
185  DqsConditionKeyword(&sql, "ALPHA", DQS_COMPARE_IN, "(1, 2, 3)", clause++);
186  DqsOrderBy(&sql, "BETA");
187  DqsEnd(&sql);
188 
189  CU_ASSERT_STRING_EQUAL(sql, TEST);
190  DqsFree(sql);
191 
192  return;
193 }
194 
195 
196 /*+
197  * TestDqs - Create Test Suite
198  *
199  * Description:
200  * Adds the test suite to the CUnit test registry and adds all the tests
201  * to it.
202  *
203  * Arguments:
204  * None.
205  *
206  * Returns:
207  * int
208  * Return status. 0 => Success.
209  */
210 
211 int TestDqs(void); /* Declaration */
212 int TestDqs(void)
213 {
214  struct test_testdef tests[] = {
215  {"TestDqsBasic", TestDqsBasic},
216  {"TestDqsConditionInt", TestDqsConditionInt},
217  {"TestDqsConditionString", TestDqsConditionString},
218  {"TestDqsConditionKeyword", TestDqsConditionKeyword},
219  {"TestDqsOrderBy", TestDqsOrderBy},
220  {NULL, NULL}
221  };
222 
223  return TcuCreateSuite("Dqs", NULL, NULL, tests);
224 }