OpenDNSSEC-enforcer  1.3.8
/build/buildd/opendnssec-1.3.8/enforcer/test/cunit/test_du_string.c
Go to the documentation of this file.
00001 /*
00002  * $Id: test_du_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_dd_string.c - Test dd_string
00031  *
00032  * Description:
00033  *      This is a short test module to check the functions in the code that
00034  *      constructs a DELETE 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  * TestDusSetInt - Test Basic Dus SET With Integer
00054  *
00055  * Description:
00056  *      Constructs a database UPDATE statement setting an integer attribute and
00057  *      checks the string so constructed.
00058 -*/
00059 
00060 static void TestDusSetInt(void)
00061 {
00062         char*   sql = NULL;
00063         int             set = 0;
00064 
00065         /* Check a single integer update */
00066 
00067         set = 0;
00068         sql = DusInit("TEST");
00069         DusSetInt(&sql, "ALPHA", 1, set++);
00070         DusEnd(&sql);
00071 
00072         CU_ASSERT_STRING_EQUAL(sql, "UPDATE TEST SET ALPHA = 1");
00073         DusFree(sql);
00074 
00075         /* Check multiple updates */
00076 
00077         set = 0;
00078         sql = DusInit("TEST");
00079         DusSetInt(&sql, "ALPHA", 1, set++);
00080         DusSetInt(&sql, "BETA",  2, set++);
00081         DusEnd(&sql);
00082 
00083         CU_ASSERT_STRING_EQUAL(sql, "UPDATE TEST SET ALPHA = 1, BETA = 2");
00084         DusFree(sql);
00085 
00086         return;
00087 }
00088 
00089 
00090 
00091 /*+
00092  * TestDusSetString - Test Basic Dus SET With String
00093  *
00094  * Description:
00095  *      Constructs a database UPDATE statement setting  a string attribute and
00096  *      checks the string so constructed.
00097 -*/
00098 
00099 static void TestDusSetString(void)
00100 {
00101         char*   sql = NULL;
00102         int             set = 0;
00103 
00104         /* Check a single string update */
00105 
00106         set = 0;
00107         sql = DusInit("TEST");
00108         DusSetString(&sql, "ALPHA", "XYZZY", set++);
00109         DusEnd(&sql);
00110 
00111         CU_ASSERT_STRING_EQUAL(sql, "UPDATE TEST SET ALPHA = \"XYZZY\"");
00112         DusFree(sql);
00113 
00114         /* Check a single string update of a NULL value */
00115 
00116         set = 0;
00117         sql = DusInit("TEST");
00118         DusSetString(&sql, "BETA", NULL, set++);
00119         DusEnd(&sql);
00120 
00121         CU_ASSERT_STRING_EQUAL(sql, "UPDATE TEST SET BETA = NULL");
00122         DusFree(sql);
00123 
00124         /* Check a combination */
00125 
00126         set = 0;
00127         sql = DusInit("TEST");
00128         DusSetString(&sql, "ALPHA", "XYZZY", set++);
00129         DusSetString(&sql, "BETA", NULL, set++);
00130         DusEnd(&sql);
00131 
00132         CU_ASSERT_STRING_EQUAL(sql,
00133                 "UPDATE TEST SET ALPHA = \"XYZZY\", BETA = NULL");
00134         DusFree(sql);
00135 
00136         return;
00137 }
00138 
00139 /*+
00140  * TestDusConditionInt - Test Conditional
00141  *
00142  * Description:
00143  *              Checks that the deletion can be constrained by a WHERE clause comparing
00144  *              fields to integers.
00145 -*/
00146 
00147 static void TestDusConditionInt(void)
00148 {
00149         char*   sql = NULL;
00150         int             set = 0;
00151         int             where = 0;
00152         static const char* TEST = 
00153                 "UPDATE TEST SET ALPHA = 0 WHERE ALPHA < 1 AND BETA <= 2 AND GAMMA = 3 "
00154                 "AND DELTA != 4 AND EPSILON >= 5 AND ZETA > 6";
00155 
00156         sql = DusInit("TEST");
00157         DusSetInt(&sql, "ALPHA", 0, set++);
00158         DusConditionInt(&sql, "ALPHA", DQS_COMPARE_LT, 1, where++);
00159         DusConditionInt(&sql, "BETA", DQS_COMPARE_LE, 2, where++);
00160         DusConditionInt(&sql, "GAMMA", DQS_COMPARE_EQ, 3, where++);
00161         DusConditionInt(&sql, "DELTA", DQS_COMPARE_NE, 4, where++);
00162         DusConditionInt(&sql, "EPSILON", DQS_COMPARE_GE, 5, where++);
00163         DusConditionInt(&sql, "ZETA", DQS_COMPARE_GT, 6, where++);
00164         DusEnd(&sql);
00165 
00166         CU_ASSERT_STRING_EQUAL(sql, TEST);
00167         DusFree(sql);
00168 
00169         return;
00170 }
00171 
00172 /*+
00173  * TestDusConditionString - Test Conditional
00174  *
00175  * Description:
00176  *              Checks that the deletion can be constrained by a WHERE clause comparing
00177  *              fields to strings.
00178 -*/
00179 
00180 static void TestDusConditionString(void)
00181 {
00182         char*   sql = NULL;
00183         int             set = 0;
00184         int             where = 0;
00185         static const char* TEST = 
00186                 "UPDATE TEST SET ALPHA = 0 "
00187                 "WHERE ALPHA < \"PETER\" AND BETA <= \"PIPER\" "
00188                 "AND GAMMA = \"PICKED\" AND DELTA != \"A\" AND EPSILON >= \"PECK\" "
00189                 "AND ZETA > \"OF\"";
00190 
00191         sql = DusInit("TEST");
00192         DusSetInt(&sql, "ALPHA", 0, set++);
00193         DusConditionString(&sql, "ALPHA", DQS_COMPARE_LT, "PETER", where++);
00194         DusConditionString(&sql, "BETA", DQS_COMPARE_LE, "PIPER", where++);
00195         DusConditionString(&sql, "GAMMA", DQS_COMPARE_EQ, "PICKED", where++);
00196         DusConditionString(&sql, "DELTA", DQS_COMPARE_NE, "A", where++);
00197         DusConditionString(&sql, "EPSILON", DQS_COMPARE_GE, "PECK", where++);
00198         DusConditionString(&sql, "ZETA", DQS_COMPARE_GT, "OF", where++);
00199         DusEnd(&sql);
00200 
00201         CU_ASSERT_STRING_EQUAL(sql, TEST);
00202         DusFree(sql);
00203 
00204         return;
00205 }
00206 
00207 /*+
00208  * TestDusConditionKeyword - Test Conditional
00209  *
00210  * Description:
00211  *              Checks that the deletion can be constrained by a WHERE clause comprising
00212  *              an IN clause.
00213 -*/
00214 
00215 
00216 static void TestDusConditionKeyword(void)
00217 {
00218         char*   sql = NULL;
00219         int             set = 0;
00220         int             where = 0;
00221         static const char* TEST = 
00222                 "UPDATE TEST SET ALPHA = 0, BETA = \"GIMMEL\" WHERE ALPHA IN (1, 2, 3) "
00223                 "AND BETA IN (\"ALEPH\", \"BETH\")";
00224 
00225         sql = DusInit("TEST");
00226         DusSetInt(&sql, "ALPHA", 0, set++);
00227         DusSetString(&sql, "BETA", "GIMMEL", set++);
00228         DusConditionKeyword(&sql, "ALPHA", DQS_COMPARE_IN, "(1, 2, 3)", where++);
00229         DusConditionKeyword(&sql, "BETA", DQS_COMPARE_IN, "(\"ALEPH\", \"BETH\")",
00230                 where++);
00231         DusEnd(&sql);
00232 
00233         CU_ASSERT_STRING_EQUAL(sql, TEST);
00234         DusFree(sql);
00235 
00236         return;
00237 }
00238 
00239 
00240 /*+
00241  * TestDus  - Create Test Suite
00242  *
00243  * Description:
00244  *      Adds the test suite to the CUnit test registry and adds all the tests
00245  *      to it.
00246  *
00247  * Arguments:
00248  *      None.
00249  *
00250  * Returns:
00251  *      int
00252  *          Return status.  0 => Success.
00253  */
00254 
00255 int TestDus(void);      /* Declaration */
00256 int TestDus(void)
00257 {
00258     struct test_testdef tests[] = {
00259         {"TestDusSetInt",                       TestDusSetInt},
00260         {"TestDusSetString",            TestDusSetString},
00261         {"TestDusConditionInt",         TestDusConditionInt},
00262         {"TestDusConditionString",      TestDusConditionString},
00263         {"TestDusConditionKeyword",     TestDusConditionKeyword},
00264         {NULL,                      NULL}
00265     };
00266 
00267     return TcuCreateSuite("Dus", NULL, NULL, tests);
00268 }