OpenDNSSEC-enforcer  1.3.8
/build/buildd/opendnssec-1.3.8/enforcer/test/cunit/test_dq_string.c
Go to the documentation of this file.
00001 /*
00002  * $Id: test_dq_string.c 3811 2010-08-26 15:05:19Z jakob $
00003  *
00004  * Copyright (c) 2008-2009 Nominet UK. All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  *
00015  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00016  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00017  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00018  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00019  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00020  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00021  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
00023  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00024  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
00025  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  *
00027  */
00028 
00029 /*+
00030  * Filename: test_dq_string.c - Test dq_string
00031  *
00032  * Description:
00033  *      This is a short test module to check the functions in the code that
00034  *      constructs a SELECT statement.
00035  *      
00036  *      The test program makes use of the CUnit framework, as described in
00037  *      http://cunit.sourceforge.net
00038 -*/
00039 
00040 #include <stdlib.h>
00041 #include <stdio.h>
00042 #include <string.h>
00043 #include <time.h>
00044 
00045 #include "CUnit/Basic.h"
00046 
00047 #include "ksm/database_statement.h"
00048 #include "test_routines.h"
00049 
00050 
00051 
00052 /*+
00053  * TestDqsBasic - Test Basic Dqs Routines
00054  *
00055  * Description:
00056  *      Constructs a database DELETE statement and checks the string so
00057  *      constructed.
00058 -*/
00059 
00060 static void TestDqsBasic(void)
00061 {
00062         char*   sql = NULL;
00063 
00064         sql = DqsInit("TEST");
00065         DqsEnd(&sql);
00066 
00067         CU_ASSERT_STRING_EQUAL(sql, "SELECT * FROM TEST");
00068         DqsFree(sql);
00069 
00070         sql = DqsCountInit("TEST");
00071         DqsEnd(&sql);
00072 
00073         CU_ASSERT_STRING_EQUAL(sql, "SELECT COUNT(*) FROM TEST");
00074         DqsFree(sql);
00075 
00076         return;
00077 }
00078 
00079 /*+
00080  * TestDqsConditionInt - Test Conditional
00081  *
00082  * Description:
00083  *              Checks that the deletion can be constrained by a WHERE clause comparing
00084  *              fields to integers.
00085 -*/
00086 
00087 static void TestDqsConditionInt(void)
00088 {
00089         char*   sql = NULL;
00090         int             clause = 0;
00091 
00092         sql = DqsCountInit("TEST");
00093         DqsConditionInt(&sql, "ALPHA", DQS_COMPARE_LT, 1, clause++);
00094         DqsConditionInt(&sql, "BETA", DQS_COMPARE_LE, 2, clause++);
00095         DqsConditionInt(&sql, "GAMMA", DQS_COMPARE_EQ, 3, clause++);
00096         DqsConditionInt(&sql, "DELTA", DQS_COMPARE_NE, 4, clause++);
00097         DqsConditionInt(&sql, "EPSILON", DQS_COMPARE_GE, 5, clause++);
00098         DqsConditionInt(&sql, "ZETA", DQS_COMPARE_GT, 6, clause++);
00099         DqsEnd(&sql);
00100 
00101         CU_ASSERT_STRING_EQUAL(sql,
00102                 "SELECT COUNT(*) FROM TEST WHERE ALPHA < 1 AND BETA <= 2 AND GAMMA = 3 "
00103                 "AND DELTA != 4 AND EPSILON >= 5 AND ZETA > 6");
00104         DqsFree(sql);
00105 
00106         return;
00107 }
00108 
00109 /*+
00110  * TestDqsConditionString - Test Conditional
00111  *
00112  * Description:
00113  *              Checks that the deletion can be constrained by a WHERE clause comparing
00114  *              fields to strings.
00115 -*/
00116 
00117 static void TestDqsConditionString(void)
00118 {
00119         char*   sql = NULL;
00120         int             clause = 0;
00121         static const char* TEST = 
00122                 "SELECT * FROM TEST WHERE ALPHA < \"PETER\" AND BETA <= \"PIPER\" "
00123                 "AND GAMMA = \"PICKED\" AND DELTA != \"A\" AND EPSILON >= \"PECK\" "
00124                 "AND ZETA > \"OF\"";
00125 
00126         sql = DqsInit("TEST");
00127         DqsConditionString(&sql, "ALPHA", DQS_COMPARE_LT, "PETER", clause++);
00128         DqsConditionString(&sql, "BETA", DQS_COMPARE_LE, "PIPER", clause++);
00129         DqsConditionString(&sql, "GAMMA", DQS_COMPARE_EQ, "PICKED", clause++);
00130         DqsConditionString(&sql, "DELTA", DQS_COMPARE_NE, "A", clause++);
00131         DqsConditionString(&sql, "EPSILON", DQS_COMPARE_GE, "PECK", clause++);
00132         DqsConditionString(&sql, "ZETA", DQS_COMPARE_GT, "OF", clause++);
00133         DqsEnd(&sql);
00134 
00135         CU_ASSERT_STRING_EQUAL(sql, TEST);
00136         DqsFree(sql);
00137 
00138         return;
00139 }
00140 
00141 /*+
00142  * TestDqsConditionKeyword - Test Conditional
00143  *
00144  * Description:
00145  *              Checks that the deletion can be constrained by a WHERE clause comprising
00146  *              an IN clause.
00147 -*/
00148 
00149 
00150 static void TestDqsConditionKeyword(void)
00151 {
00152         char*   sql = NULL;
00153         int             clause = 0;
00154         static const char* TEST = 
00155                 "SELECT * FROM TEST WHERE ALPHA IN (1, 2, 3) "
00156                 "AND BETA IN (\"ALEPH\", \"BETH\")";
00157 
00158         sql = DqsInit("TEST");
00159         DqsConditionKeyword(&sql, "ALPHA", DQS_COMPARE_IN, "(1, 2, 3)", clause++);
00160         DqsConditionKeyword(&sql, "BETA", DQS_COMPARE_IN, "(\"ALEPH\", \"BETH\")",
00161                 clause++);
00162         DqsEnd(&sql);
00163 
00164         CU_ASSERT_STRING_EQUAL(sql, TEST);
00165         DqsFree(sql);
00166 
00167         return;
00168 }
00169 
00170 /*+
00171  * TestDqsOrderBy - Test ORDER BY Clause
00172  *
00173  * Description:
00174  *              Checks that the deletion can be constrained by a WHERE clause comprising
00175  *              an IN clause.
00176 -*/
00177 
00178 
00179 static void TestDqsOrderBy(void)
00180 {
00181         char*   sql = NULL;
00182         int             clause = 0;
00183         static const char* TEST = 
00184                 "SELECT * FROM TEST WHERE ALPHA IN (1, 2, 3) ORDER BY BETA";
00185 
00186         sql = DqsInit("TEST");
00187         DqsConditionKeyword(&sql, "ALPHA", DQS_COMPARE_IN, "(1, 2, 3)", clause++);
00188         DqsOrderBy(&sql, "BETA");
00189         DqsEnd(&sql);
00190 
00191         CU_ASSERT_STRING_EQUAL(sql, TEST);
00192         DqsFree(sql);
00193 
00194         return;
00195 }
00196 
00197 
00198 /*+
00199  * TestDqs  - Create Test Suite
00200  *
00201  * Description:
00202  *      Adds the test suite to the CUnit test registry and adds all the tests
00203  *      to it.
00204  *
00205  * Arguments:
00206  *      None.
00207  *
00208  * Returns:
00209  *      int
00210  *          Return status.  0 => Success.
00211  */
00212 
00213 int TestDqs(void);      /* Declaration */
00214 int TestDqs(void)
00215 {
00216     struct test_testdef tests[] = {
00217         {"TestDqsBasic",                        TestDqsBasic},
00218         {"TestDqsConditionInt",         TestDqsConditionInt},
00219         {"TestDqsConditionString",      TestDqsConditionString},
00220         {"TestDqsConditionKeyword",     TestDqsConditionKeyword},
00221         {"TestDqsOrderBy",                      TestDqsOrderBy},
00222         {NULL,                      NULL}
00223     };
00224 
00225     return TcuCreateSuite("Dqs", NULL, NULL, tests);
00226 }