OpenDNSSEC-enforcer  1.4.6
test_message.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_message.c - Test message Module
29  *
30  * Description:
31  * This is a short test module to check the functions in the message
32  * module.
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/message.h"
46 #include "test_routines.h"
47 
48 
49 /*+
50  * Output - Output Function
51  *
52  * Description:
53  * Used where a an output function is required, this just copies its
54  * output message into a global buffer for later examination.
55 -*/
56 
57 static char output_buffer[4096];
58 
59 static void Output(const char* text)
60 {
61  strncpy(output_buffer, text, sizeof(output_buffer));
62  output_buffer[sizeof(output_buffer) - 1] = '\0';
63 
64  return;
65 }
66 
67 
68 
69 /*+
70  * TestMsgInitRundown - Test MsgInit, MsgRundown (and MsgFindCodeBlock)
71  *
72  * Description:
73  * Registers a set of messages, checks that they are there, runs the
74  * module down, then checks that the messages can't be found.
75 -*/
76 
77 static void TestMsgInitRundown(void)
78 {
79  int BLOCK0_LOW = 10240;
80  int BLOCK0_HIGH = 10245;
81  const char* BLOCK0_MESSAGES[] = {
82  "ALPHA", "BETA", "GAMMA", "DELTA", "EPSILON", "ZETA"
83  };
84 
85  MsgInit();
86 
87  /* No match after initialization */
88 
89  CU_ASSERT_EQUAL(MsgFindCodeBlock(BLOCK0_LOW), -1);
90 
91  /* Register a message block and check again */
92 
93  MsgRegister(BLOCK0_LOW, BLOCK0_HIGH, BLOCK0_MESSAGES, MsgNoOutput);
94  CU_ASSERT_NOT_EQUAL(MsgFindCodeBlock(BLOCK0_LOW), -1);
95 
96  /* Rundown the module and check again */
97 
98  MsgRundown();
99  CU_ASSERT_EQUAL(MsgFindCodeBlock(BLOCK0_LOW), -1);
100 
101  return;
102 }
103 
104 
105 
106 /*+
107  * TestMsgRegisterText - Test MsgRegsiter and MsgText Functions
108  *
109  * Description:
110  * Registers multiple sets of messages and checks that the message can be
111  * retrieved.
112 -*/
113 
114 static void TestMsgRegisterText(void)
115 {
116  int i;
117 
118  int BLOCK1_LOW = 20480;
119  int BLOCK1_HIGH = 20485;
120  const char* BLOCK1_MESSAGES[] = {
121  "ALPHA", "BETA", "GAMMA", "DELTA", "EPSILON", "ZETA"
122  };
123 
124  int BLOCK2_LOW = 30720;
125  int BLOCK2_HIGH = 30725;
126  const char* BLOCK2_MESSAGES[] = {
127  "ALEPH", "BETH", "GIMMEL", "DALET", "HEY", "VAV"
128  };
129 
130  MsgInit();
131 
132  /* Register two blocks of messages with a null output function */
133 
134  MsgRegister(BLOCK1_LOW, BLOCK1_HIGH, BLOCK1_MESSAGES, MsgNoOutput);
135  MsgRegister(BLOCK2_LOW, BLOCK2_HIGH, BLOCK2_MESSAGES, MsgNoOutput);
136 
137  /* Now check the text */
138 
139  for (i = BLOCK1_LOW; i <= BLOCK1_HIGH; ++i) {
140  CU_ASSERT_STRING_EQUAL(MsgText(i), BLOCK1_MESSAGES[i - BLOCK1_LOW]);
141  }
142 
143  for (i = BLOCK2_LOW; i <= BLOCK2_HIGH; ++i) {
144  CU_ASSERT_STRING_EQUAL(MsgText(i), BLOCK2_MESSAGES[i - BLOCK2_LOW]);
145  }
146 
147  MsgRundown();
148 
149  return;
150 }
151 
152 
153 /*+
154  * TestMsgGetSetOutput - Test MsgGetOutput and MsgSetOutput
155  *
156  * Description:
157  * Sets and gets the output function for a block of messages.
158 -*/
159 
160 static void TestMsgGetSetOutput(void)
161 {
162  int BLOCK3_LOW = 40960;
163  int BLOCK3_HIGH = 40965;
164  const char* BLOCK3_MESSAGES[] = {
165  "A", "B", "C", "D", "E", "F"
166  };
167 
168  MsgInit();
169 
170  /*
171  * Register the above block of messages and check that we can obtain
172  * the output function. Note that MsgGetOutput only requires the number
173  * of a code in the range, so any value in the range will do.
174  */
175 
176  MsgRegister(BLOCK3_LOW, BLOCK3_HIGH, BLOCK3_MESSAGES, MsgNoOutput);
177  CU_ASSERT_PTR_EQUAL((void*) MsgGetOutput(BLOCK3_LOW), (void*) MsgNoOutput);
178 
179  /* Change the output function and check again */
180 
181  MsgSetOutput(BLOCK3_HIGH, MsgDefaultOutput);
182  CU_ASSERT_PTR_EQUAL((void*) MsgGetOutput(BLOCK3_LOW), (void*) MsgDefaultOutput);
183 
184  MsgRundown();
185 
186  return;
187 }
188 
189 
190 /*+
191  * TestMsgLog - Test MsgLog Function
192  *
193  * Description:
194  * Checks that MsgLog correctly handles the substitution of arguments.
195 -*/
196 
197 static void TestMsgLog(void)
198 {
199  int BLOCK4_LOW = 51200;
200  int BLOCK4_HIGH = 51201;
201  const char* BLOCK4_MESSAGES[] = {
202  "There are %d %ss in the store",
203  "%d %ss a %s"
204  };
205  int status; /* Status return */
206 
207  MsgInit();
208 
209  MsgRegister(BLOCK4_LOW, BLOCK4_HIGH, BLOCK4_MESSAGES, Output);
210 
211  status = MsgLog(BLOCK4_LOW, 15, "orange");
212  CU_ASSERT_EQUAL(status, BLOCK4_LOW);
213  CU_ASSERT_STRING_EQUAL(output_buffer, "There are 15 oranges in the store");
214 
215  status = MsgLog(BLOCK4_HIGH, 10, "lord", "leaping");
216  CU_ASSERT_EQUAL(status, BLOCK4_HIGH);
217  CU_ASSERT_STRING_EQUAL(output_buffer, "10 lords a leaping");
218 
219  MsgRundown();
220 
221  return;
222 }
223 
224 
225 /*+
226  * TestMessage - Create Test Suite
227  *
228  * Description:
229  * Adds the test suite to the CUnit test registry and adds all the tests
230  * to it.
231  *
232  * Arguments:
233  * None.
234  *
235  * Returns:
236  * int
237  * Return status. 0 => Success.
238  */
239 
240 int TestMsg(void); /* Declaration */
241 int TestMsg(void)
242 {
243  struct test_testdef tests[] = {
244  {"TestMsgInitRundown", TestMsgInitRundown},
245  {"TestMsgRegisterText", TestMsgRegisterText},
246  {"TestMsgGetSetOutput", TestMsgGetSetOutput},
247  {"TestMsgLog", TestMsgLog},
248  {NULL, NULL}
249  };
250 
251  return TcuCreateSuite("Msg", NULL, NULL, tests);
252 }
void MsgNoOutput(const char *text)
Definition: message.c:104
int TcuCreateSuite(const char *title, int(*init)(), int(*teardown)(), struct test_testdef *tests)
int MsgLog(int status,...)
Definition: message.c:335
const char * MsgText(int status)
Definition: message.c:223
MSG_OUTPUT_FUNCTION MsgGetOutput(int status)
Definition: message.c:258
int TestMsg(void)
Definition: test_message.c:241
int MsgFindCodeBlock(int status)
Definition: message.c:186
void MsgRegister(int min, int max, const char **message, MSG_OUTPUT_FUNCTION output)
Definition: message.c:141
void MsgRundown(void)
Definition: message.c:412
void MsgSetOutput(int code, MSG_OUTPUT_FUNCTION output)
Definition: message.c:290
void MsgDefaultOutput(const char *text)
Definition: message.c:84
void MsgInit(void)
Definition: message.c:63