OpenDNSSEC-enforcer 1.3.0
|
00001 /* 00002 * $Id: test_message.c 5320 2011-07-12 10:42:26Z 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_message.c - Test message Module 00031 * 00032 * Description: 00033 * This is a short test module to check the functions in the message 00034 * module. 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/message.h" 00048 #include "test_routines.h" 00049 00050 00051 /*+ 00052 * Output - Output Function 00053 * 00054 * Description: 00055 * Used where a an output function is required, this just copies its 00056 * output message into a global buffer for later examination. 00057 -*/ 00058 00059 static char output_buffer[4096]; 00060 00061 static void Output(const char* text) 00062 { 00063 strncpy(output_buffer, text, sizeof(output_buffer)); 00064 output_buffer[sizeof(output_buffer) - 1] = '\0'; 00065 00066 return; 00067 } 00068 00069 00070 00071 /*+ 00072 * TestMsgInitRundown - Test MsgInit, MsgRundown (and MsgFindCodeBlock) 00073 * 00074 * Description: 00075 * Registers a set of messages, checks that they are there, runs the 00076 * module down, then checks that the messages can't be found. 00077 -*/ 00078 00079 static void TestMsgInitRundown(void) 00080 { 00081 int BLOCK0_LOW = 10240; 00082 int BLOCK0_HIGH = 10245; 00083 const char* BLOCK0_MESSAGES[] = { 00084 "ALPHA", "BETA", "GAMMA", "DELTA", "EPSILON", "ZETA" 00085 }; 00086 00087 MsgInit(); 00088 00089 /* No match after initialization */ 00090 00091 CU_ASSERT_EQUAL(MsgFindCodeBlock(BLOCK0_LOW), -1); 00092 00093 /* Register a message block and check again */ 00094 00095 MsgRegister(BLOCK0_LOW, BLOCK0_HIGH, BLOCK0_MESSAGES, MsgNoOutput); 00096 CU_ASSERT_NOT_EQUAL(MsgFindCodeBlock(BLOCK0_LOW), -1); 00097 00098 /* Rundown the module and check again */ 00099 00100 MsgRundown(); 00101 CU_ASSERT_EQUAL(MsgFindCodeBlock(BLOCK0_LOW), -1); 00102 00103 return; 00104 } 00105 00106 00107 00108 /*+ 00109 * TestMsgRegisterText - Test MsgRegsiter and MsgText Functions 00110 * 00111 * Description: 00112 * Registers multiple sets of messages and checks that the message can be 00113 * retrieved. 00114 -*/ 00115 00116 static void TestMsgRegisterText(void) 00117 { 00118 int i; 00119 00120 int BLOCK1_LOW = 20480; 00121 int BLOCK1_HIGH = 20485; 00122 const char* BLOCK1_MESSAGES[] = { 00123 "ALPHA", "BETA", "GAMMA", "DELTA", "EPSILON", "ZETA" 00124 }; 00125 00126 int BLOCK2_LOW = 30720; 00127 int BLOCK2_HIGH = 30725; 00128 const char* BLOCK2_MESSAGES[] = { 00129 "ALEPH", "BETH", "GIMMEL", "DALET", "HEY", "VAV" 00130 }; 00131 00132 MsgInit(); 00133 00134 /* Register two blocks of messages with a null output function */ 00135 00136 MsgRegister(BLOCK1_LOW, BLOCK1_HIGH, BLOCK1_MESSAGES, MsgNoOutput); 00137 MsgRegister(BLOCK2_LOW, BLOCK2_HIGH, BLOCK2_MESSAGES, MsgNoOutput); 00138 00139 /* Now check the text */ 00140 00141 for (i = BLOCK1_LOW; i <= BLOCK1_HIGH; ++i) { 00142 CU_ASSERT_STRING_EQUAL(MsgText(i), BLOCK1_MESSAGES[i - BLOCK1_LOW]); 00143 } 00144 00145 for (i = BLOCK2_LOW; i <= BLOCK2_HIGH; ++i) { 00146 CU_ASSERT_STRING_EQUAL(MsgText(i), BLOCK2_MESSAGES[i - BLOCK2_LOW]); 00147 } 00148 00149 MsgRundown(); 00150 00151 return; 00152 } 00153 00154 00155 /*+ 00156 * TestMsgGetSetOutput - Test MsgGetOutput and MsgSetOutput 00157 * 00158 * Description: 00159 * Sets and gets the output function for a block of messages. 00160 -*/ 00161 00162 static void TestMsgGetSetOutput(void) 00163 { 00164 int BLOCK3_LOW = 40960; 00165 int BLOCK3_HIGH = 40965; 00166 const char* BLOCK3_MESSAGES[] = { 00167 "A", "B", "C", "D", "E", "F" 00168 }; 00169 00170 MsgInit(); 00171 00172 /* 00173 * Register the above block of messages and check that we can obtain 00174 * the output function. Note that MsgGetOutput only requires the number 00175 * of a code in the range, so any value in the range will do. 00176 */ 00177 00178 MsgRegister(BLOCK3_LOW, BLOCK3_HIGH, BLOCK3_MESSAGES, MsgNoOutput); 00179 CU_ASSERT_PTR_EQUAL((void*) MsgGetOutput(BLOCK3_LOW), (void*) MsgNoOutput); 00180 00181 /* Change the output function and check again */ 00182 00183 MsgSetOutput(BLOCK3_HIGH, MsgDefaultOutput); 00184 CU_ASSERT_PTR_EQUAL((void*) MsgGetOutput(BLOCK3_LOW), (void*) MsgDefaultOutput); 00185 00186 MsgRundown(); 00187 00188 return; 00189 } 00190 00191 00192 /*+ 00193 * TestMsgLog - Test MsgLog Function 00194 * 00195 * Description: 00196 * Checks that MsgLog correctly handles the substitution of arguments. 00197 -*/ 00198 00199 static void TestMsgLog(void) 00200 { 00201 int BLOCK4_LOW = 51200; 00202 int BLOCK4_HIGH = 51201; 00203 const char* BLOCK4_MESSAGES[] = { 00204 "There are %d %ss in the store", 00205 "%d %ss a %s" 00206 }; 00207 int status; /* Status return */ 00208 00209 MsgInit(); 00210 00211 MsgRegister(BLOCK4_LOW, BLOCK4_HIGH, BLOCK4_MESSAGES, Output); 00212 00213 status = MsgLog(BLOCK4_LOW, 15, "orange"); 00214 CU_ASSERT_EQUAL(status, BLOCK4_LOW); 00215 CU_ASSERT_STRING_EQUAL(output_buffer, "There are 15 oranges in the store"); 00216 00217 status = MsgLog(BLOCK4_HIGH, 10, "lord", "leaping"); 00218 CU_ASSERT_EQUAL(status, BLOCK4_HIGH); 00219 CU_ASSERT_STRING_EQUAL(output_buffer, "10 lords a leaping"); 00220 00221 MsgRundown(); 00222 00223 return; 00224 } 00225 00226 00227 /*+ 00228 * TestMessage - Create Test Suite 00229 * 00230 * Description: 00231 * Adds the test suite to the CUnit test registry and adds all the tests 00232 * to it. 00233 * 00234 * Arguments: 00235 * None. 00236 * 00237 * Returns: 00238 * int 00239 * Return status. 0 => Success. 00240 */ 00241 00242 int TestMsg(void); /* Declaration */ 00243 int TestMsg(void) 00244 { 00245 struct test_testdef tests[] = { 00246 {"TestMsgInitRundown", TestMsgInitRundown}, 00247 {"TestMsgRegisterText", TestMsgRegisterText}, 00248 {"TestMsgGetSetOutput", TestMsgGetSetOutput}, 00249 {"TestMsgLog", TestMsgLog}, 00250 {NULL, NULL} 00251 }; 00252 00253 return TcuCreateSuite("Msg", NULL, NULL, tests); 00254 }