OpenDNSSEC-enforcer  1.4.1
test_message.c
Go to the documentation of this file.
1 /*
2  * $Id: test_message.c 3811 2010-08-26 15:05:19Z jakob $
3  *
4  * Copyright (c) 2008-2009 Nominet UK. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
29 /*+
30  * Filename: test_message.c - Test message Module
31  *
32  * Description:
33  * This is a short test module to check the functions in the message
34  * module.
35  *
36  * The test program makes use of the CUnit framework, as described in
37  * http://cunit.sourceforge.net
38 -*/
39 
40 #include <stdlib.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <time.h>
44 
45 #include "CUnit/Basic.h"
46 
47 #include "ksm/message.h"
48 #include "test_routines.h"
49 
50 
51 /*+
52  * Output - Output Function
53  *
54  * Description:
55  * Used where a an output function is required, this just copies its
56  * output message into a global buffer for later examination.
57 -*/
58 
59 static char output_buffer[4096];
60 
61 static void Output(const char* text)
62 {
63  strncpy(output_buffer, text, sizeof(output_buffer));
64  output_buffer[sizeof(output_buffer) - 1] = '\0';
65 
66  return;
67 }
68 
69 
70 
71 /*+
72  * TestMsgInitRundown - Test MsgInit, MsgRundown (and MsgFindCodeBlock)
73  *
74  * Description:
75  * Registers a set of messages, checks that they are there, runs the
76  * module down, then checks that the messages can't be found.
77 -*/
78 
79 static void TestMsgInitRundown(void)
80 {
81  int BLOCK0_LOW = 10240;
82  int BLOCK0_HIGH = 10245;
83  const char* BLOCK0_MESSAGES[] = {
84  "ALPHA", "BETA", "GAMMA", "DELTA", "EPSILON", "ZETA"
85  };
86 
87  MsgInit();
88 
89  /* No match after initialization */
90 
91  CU_ASSERT_EQUAL(MsgFindCodeBlock(BLOCK0_LOW), -1);
92 
93  /* Register a message block and check again */
94 
95  MsgRegister(BLOCK0_LOW, BLOCK0_HIGH, BLOCK0_MESSAGES, MsgNoOutput);
96  CU_ASSERT_NOT_EQUAL(MsgFindCodeBlock(BLOCK0_LOW), -1);
97 
98  /* Rundown the module and check again */
99 
100  MsgRundown();
101  CU_ASSERT_EQUAL(MsgFindCodeBlock(BLOCK0_LOW), -1);
102 
103  return;
104 }
105 
106 
107 
108 /*+
109  * TestMsgRegisterText - Test MsgRegsiter and MsgText Functions
110  *
111  * Description:
112  * Registers multiple sets of messages and checks that the message can be
113  * retrieved.
114 -*/
115 
116 static void TestMsgRegisterText(void)
117 {
118  int i;
119 
120  int BLOCK1_LOW = 20480;
121  int BLOCK1_HIGH = 20485;
122  const char* BLOCK1_MESSAGES[] = {
123  "ALPHA", "BETA", "GAMMA", "DELTA", "EPSILON", "ZETA"
124  };
125 
126  int BLOCK2_LOW = 30720;
127  int BLOCK2_HIGH = 30725;
128  const char* BLOCK2_MESSAGES[] = {
129  "ALEPH", "BETH", "GIMMEL", "DALET", "HEY", "VAV"
130  };
131 
132  MsgInit();
133 
134  /* Register two blocks of messages with a null output function */
135 
136  MsgRegister(BLOCK1_LOW, BLOCK1_HIGH, BLOCK1_MESSAGES, MsgNoOutput);
137  MsgRegister(BLOCK2_LOW, BLOCK2_HIGH, BLOCK2_MESSAGES, MsgNoOutput);
138 
139  /* Now check the text */
140 
141  for (i = BLOCK1_LOW; i <= BLOCK1_HIGH; ++i) {
142  CU_ASSERT_STRING_EQUAL(MsgText(i), BLOCK1_MESSAGES[i - BLOCK1_LOW]);
143  }
144 
145  for (i = BLOCK2_LOW; i <= BLOCK2_HIGH; ++i) {
146  CU_ASSERT_STRING_EQUAL(MsgText(i), BLOCK2_MESSAGES[i - BLOCK2_LOW]);
147  }
148 
149  MsgRundown();
150 
151  return;
152 }
153 
154 
155 /*+
156  * TestMsgGetSetOutput - Test MsgGetOutput and MsgSetOutput
157  *
158  * Description:
159  * Sets and gets the output function for a block of messages.
160 -*/
161 
162 static void TestMsgGetSetOutput(void)
163 {
164  int BLOCK3_LOW = 40960;
165  int BLOCK3_HIGH = 40965;
166  const char* BLOCK3_MESSAGES[] = {
167  "A", "B", "C", "D", "E", "F"
168  };
169 
170  MsgInit();
171 
172  /*
173  * Register the above block of messages and check that we can obtain
174  * the output function. Note that MsgGetOutput only requires the number
175  * of a code in the range, so any value in the range will do.
176  */
177 
178  MsgRegister(BLOCK3_LOW, BLOCK3_HIGH, BLOCK3_MESSAGES, MsgNoOutput);
179  CU_ASSERT_PTR_EQUAL((void*) MsgGetOutput(BLOCK3_LOW), (void*) MsgNoOutput);
180 
181  /* Change the output function and check again */
182 
183  MsgSetOutput(BLOCK3_HIGH, MsgDefaultOutput);
184  CU_ASSERT_PTR_EQUAL((void*) MsgGetOutput(BLOCK3_LOW), (void*) MsgDefaultOutput);
185 
186  MsgRundown();
187 
188  return;
189 }
190 
191 
192 /*+
193  * TestMsgLog - Test MsgLog Function
194  *
195  * Description:
196  * Checks that MsgLog correctly handles the substitution of arguments.
197 -*/
198 
199 static void TestMsgLog(void)
200 {
201  int BLOCK4_LOW = 51200;
202  int BLOCK4_HIGH = 51201;
203  const char* BLOCK4_MESSAGES[] = {
204  "There are %d %ss in the store",
205  "%d %ss a %s"
206  };
207  int status; /* Status return */
208 
209  MsgInit();
210 
211  MsgRegister(BLOCK4_LOW, BLOCK4_HIGH, BLOCK4_MESSAGES, Output);
212 
213  status = MsgLog(BLOCK4_LOW, 15, "orange");
214  CU_ASSERT_EQUAL(status, BLOCK4_LOW);
215  CU_ASSERT_STRING_EQUAL(output_buffer, "There are 15 oranges in the store");
216 
217  status = MsgLog(BLOCK4_HIGH, 10, "lord", "leaping");
218  CU_ASSERT_EQUAL(status, BLOCK4_HIGH);
219  CU_ASSERT_STRING_EQUAL(output_buffer, "10 lords a leaping");
220 
221  MsgRundown();
222 
223  return;
224 }
225 
226 
227 /*+
228  * TestMessage - Create Test Suite
229  *
230  * Description:
231  * Adds the test suite to the CUnit test registry and adds all the tests
232  * to it.
233  *
234  * Arguments:
235  * None.
236  *
237  * Returns:
238  * int
239  * Return status. 0 => Success.
240  */
241 
242 int TestMsg(void); /* Declaration */
243 int TestMsg(void)
244 {
245  struct test_testdef tests[] = {
246  {"TestMsgInitRundown", TestMsgInitRundown},
247  {"TestMsgRegisterText", TestMsgRegisterText},
248  {"TestMsgGetSetOutput", TestMsgGetSetOutput},
249  {"TestMsgLog", TestMsgLog},
250  {NULL, NULL}
251  };
252 
253  return TcuCreateSuite("Msg", NULL, NULL, tests);
254 }
int TestMsg(void)
Definition: test_message.c:243
void MsgNoOutput(const char *text)
Definition: message.c:106
int TcuCreateSuite(const char *title, int(*init)(), int(*teardown)(), struct test_testdef *tests)
int MsgLog(int status,...)
Definition: message.c:337
const char * MsgText(int status)
Definition: message.c:225
MSG_OUTPUT_FUNCTION MsgGetOutput(int status)
Definition: message.c:260
int MsgFindCodeBlock(int status)
Definition: message.c:188
void MsgRegister(int min, int max, const char **message, MSG_OUTPUT_FUNCTION output)
Definition: message.c:143
void MsgRundown(void)
Definition: message.c:414
void MsgSetOutput(int code, MSG_OUTPUT_FUNCTION output)
Definition: message.c:292
void MsgDefaultOutput(const char *text)
Definition: message.c:86
void MsgInit(void)
Definition: message.c:65