blob: 73f725acbc1b6c79c259e5ba0ee0a7819d22e92f [file] [log] [blame]
Emeric Vigier2f625822012-08-06 11:09:52 -04001// Copyright (C) 2002-2003 Chad C. Yates cyates@uidaho.edu
2//
3// This program is free software; you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation; either version 2 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program; if not, write to the Free Software
15// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16//
17// As a special exception to the GNU General Public License, permission is
18// granted for additional uses of the text contained in its release
19// of Common C++.
20//
21// The exception is that, if you link the Common C++ library with other
22// files to produce an executable, this does not by itself cause the
23// resulting executable to be covered by the GNU General Public License.
24// Your use of that executable is in no way restricted on account of
25// linking the Common C++ library code into it.
26//
27// This exception does not however invalidate any other reasons why
28// the executable file might be covered by the GNU General Public License.
29//
30// This exception applies only to the code released under the
31// name Common C++. If you copy code from other releases into a copy of
32// Common C++, as the General Public License permits, the exception does
33// not apply to the code that you add in this way. To avoid misleading
34// anyone as to the status of such modified files, you must delete
35// this exception notice from them.
36//
37// If you write modifications of your own for Common C++, it is your choice
38// whether to permit this exception to apply to your modifications.
39// If you do not wish that, delete this exception notice.
40
41#include <cppunit/extensions/HelperMacros.h>
42#include <cc++/digest.h>
43#include <fstream>
44
45using namespace ost;
46
47/**
48 * Template class that provides common tests and data to excercise
49 * the digest classes. Provides common messages to run through the digests.
50 * Sub-classes of this Fixture provide what tests to run and what the expected
51 * digest strings are, as well as any other tests that are specific to that digest
52 * type. see the pre-defined subclasses for examples
53 *
54 * @author Chad C. Yates
55 */
56template<class DigestTypeClass>
57class GenericDigestTests : public CppUnit::TestFixture
58{
59private:
60 enum {BINARY_DATA1_SIZE = 256}; // constant trick
61 unsigned char binaryData1[BINARY_DATA1_SIZE];
62
63protected:
64 DigestTypeClass digest;
65 std::stringstream digestOutput;
66
67 GenericDigestTests() {
68 // generate some binary data
69 for(unsigned int i = 0; i< BINARY_DATA1_SIZE; ++i)
70 binaryData1[i] = i % 256; // repeating pattern of ascending bytes
71
72 // output it to a file in case it needs to be checked against an external program
73 std::ofstream file("BinaryData1.dat", std::ios::out|std::ios::binary);
74 file.write((char *) binaryData1, sizeof(binaryData1));
75 file.close();
76 }
77
78 virtual ~GenericDigestTests()
79 {}
80
81 std::string getEmptyMsg()
82 { return ""; }
83
84 std::string getMsg1()
85 { return "pippo"; }
86
87 std::string getLongMsg1()
88 { return "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah "
89 "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah "
90 "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah "
91 "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah "
92 "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah "
93 "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah "
94 "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah "
95 "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah "
96 "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah "
97 "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah "
98 "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah "
99 "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah "
100 "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah "
101 "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah "
102 "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah "
103 "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah "
104 "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah";
105 }
106 unsigned char* getBinaryData1()
107 { return binaryData1; }
108
109 // These define the textual digests that should be the result of processing
110 // the corresponding message/data and exporting to a stream
111 virtual std::string getEmptyMsg_Digest() { return ""; }
112 virtual std::string getMsg1_Digest() { return ""; }
113 virtual std::string getLongMsg1_Digest() { return ""; }
114 virtual std::string getBinaryData1_Digest() { return ""; }
115
116public:
117 /**
118 * Test case for Digest
119 *
120 * @author Chad C. Yates
121 */
122 void testTypicalUsage();
123
124 /**
125 * Test case for Digest
126 *
127 * @author Chad C. Yates
128 */
129 void testConstruction();
130
131 /**
132 * Test case for Digest
133 *
134 * @author Chad C. Yates
135 */
136 void testEmptyData();
137
138 /**
139 * Test case for Digest
140 *
141 * @author Chad C. Yates
142 */
143 void testSmallMessage();
144
145 /**
146 * Test case for Digest
147 *
148 * @author Chad C. Yates
149 */
150 void testLargeMessage();
151
152 /**
153 * Test case for Digest
154 *
155 * @author Chad C. Yates
156 */
157 void testBinaryData();
158
159 /**
160 * Test case for Digest
161 *
162 * @author Chad C. Yates
163 */
164 void testReInitialization();
165
166 /**
167 * Test case for Digest
168 * attempts to excercise the putDigest method by giving it a long string a piece at a time.
169 *
170 * @author Chad C. Yates
171 */
172 void testPiecewise();
173};
174
175/**
176 * Subclass of the generic digest tests that specifies the expected digest
177 * strings and tests to run for the Checksum digest class.
178 */
179class ChecksumDigestTest : public GenericDigestTests<ChecksumDigest>
180{
181 CPPUNIT_TEST_SUITE(ChecksumDigestTest);
182 CPPUNIT_TEST(testTypicalUsage);
183 CPPUNIT_TEST(testConstruction);
184 CPPUNIT_TEST(testEmptyData);
185 CPPUNIT_TEST(testSmallMessage);
186 CPPUNIT_TEST(testLargeMessage);
187 CPPUNIT_TEST(testBinaryData);
188 CPPUNIT_TEST(testReInitialization);
189 CPPUNIT_TEST(testPiecewise);
190 CPPUNIT_TEST_SUITE_END();
191
192public:
193 std::string getEmptyMsg_Digest()
194 { return "00"; }
195 std::string getMsg1_Digest()
196 { return "28"; }
197 std::string getLongMsg1_Digest()
198 { return "29"; }
199 std::string getBinaryData1_Digest()
200 { return "80"; }
201};
202
203/**
204 * Subclass of the generic digest tests that specifies the expected digest
205 * strings and tests to run for the CRC16 digest class.
206 */
207class CRC16DigestTest : public GenericDigestTests<CRC16Digest>
208{
209 CPPUNIT_TEST_SUITE(CRC16DigestTest);
210 CPPUNIT_TEST(testTypicalUsage);
211 CPPUNIT_TEST(testConstruction);
212 CPPUNIT_TEST(testEmptyData);
213 CPPUNIT_TEST(testSmallMessage);
214 CPPUNIT_TEST(testLargeMessage);
215 CPPUNIT_TEST(testBinaryData);
216 CPPUNIT_TEST(testReInitialization);
217 CPPUNIT_TEST(testPiecewise);
218 CPPUNIT_TEST_SUITE_END();
219
220public:
221 std::string getEmptyMsg_Digest()
222 { return "0000"; }
223 std::string getMsg1_Digest()
224 { return "fa3b"; }
225 std::string getLongMsg1_Digest()
226 { return "54cf"; }
227 std::string getBinaryData1_Digest()
228 { return "7e55"; }
229};
230
231/**
232 * Subclass of the generic digest tests that specifies the expected digest
233 * strings and tests to run for the CRC32 digest class.
234 */
235class CRC32DigestTest : public GenericDigestTests<CRC32Digest>
236{
237 CPPUNIT_TEST_SUITE(CRC32DigestTest);
238 CPPUNIT_TEST(testTypicalUsage);
239 CPPUNIT_TEST(testConstruction);
240 CPPUNIT_TEST(testEmptyData);
241 CPPUNIT_TEST(testSmallMessage);
242 CPPUNIT_TEST(testLargeMessage);
243 CPPUNIT_TEST(testBinaryData);
244 CPPUNIT_TEST(testReInitialization);
245 CPPUNIT_TEST(testPiecewise);
246 CPPUNIT_TEST_SUITE_END();
247
248public:
249 virtual std::string getEmptyMsg_Digest()
250 { return "00000000"; }
251 virtual std::string getMsg1_Digest()
252 { return "df1dc234"; }
253 virtual std::string getLongMsg1_Digest()
254 { return "2d69085d"; }
255 virtual std::string getBinaryData1_Digest()
256 { return "29058c73"; }
257};
258
259/**
260 * Subclass of the generic digest tests that specifies the expected digest
261 * strings and tests to run for the MD5 digest class.
262 */
263class MD5DigestTest : public GenericDigestTests<MD5Digest>
264{
265 CPPUNIT_TEST_SUITE(MD5DigestTest);
266 CPPUNIT_TEST(testTypicalUsage);
267 CPPUNIT_TEST(testConstruction);
268 CPPUNIT_TEST(testEmptyData);
269 CPPUNIT_TEST(testSmallMessage);
270 CPPUNIT_TEST(testLargeMessage);
271 CPPUNIT_TEST(testBinaryData);
272 CPPUNIT_TEST(testReInitialization);
273 CPPUNIT_TEST(testPiecewise);
274 CPPUNIT_TEST_SUITE_END();
275
276public:
277 virtual std::string getEmptyMsg_Digest()
278 { return "d41d8cd9" "8f00b204" "e9800998" "ecf8427e"; }
279 virtual std::string getMsg1_Digest()
280 { return "0c88028b" "f3aa6a6a" "143ed846" "f2be1ea4"; }
281 virtual std::string getLongMsg1_Digest()
282 { return "7588d0eb" "c99997f3" "a4284b11" "6ac117b5"; }
283 virtual std::string getBinaryData1_Digest()
284 { return "e2c865db" "4162bed9" "63bfaa9e" "f6ac18f0"; }
285};
286
287/**
288 * Subclass of the generic digest tests that specifies the expected digest
289 * strings and tests to run for the SHA1 digest class.
290 */
291class SHA1DigestTest : public GenericDigestTests<SHA1Digest>
292{
293 CPPUNIT_TEST_SUITE(SHA1DigestTest);
294 //CPPUNIT_TEST(testTypicalUsage);
295 //CPPUNIT_TEST(testConstruction);
296 //CPPUNIT_TEST(testEmptyData);
297 //CPPUNIT_TEST(testSmallMessage);
298 //CPPUNIT_TEST(testLargeMessage);
299 //CPPUNIT_TEST(testBinaryData);
300 //CPPUNIT_TEST(testReInitialization);
301 //CPPUNIT_TEST(testPiecewise);
302 CPPUNIT_TEST_SUITE_END();
303
304public:
305 virtual std::string getEmptyMsg_Digest()
306 { return ""; }
307 virtual std::string getMsg1_Digest()
308 { return "d012f681" "44ed0f12" "1d3cc330" "a17eec52" "8c2e7d59"; }
309 virtual std::string getLongMsg1_Digest()
310 { return "c2cce34c" "96d1ab91" "ce5cb15d" "7542a37a" "a7c57fae"; }
311 virtual std::string getBinaryData1_Digest()
312 { return "4916d6bd" "b7f78e68" "03698cab" "32d1586e" "a457dfc8"; }
313};
314
315/**
316 * Subclass of the generic digest tests that specifies the expected digest
317 * strings and tests to run for the SHA256 digest class.
318 */
319class SHA256DigestTest : public GenericDigestTests<SHA256Digest>
320{
321 CPPUNIT_TEST_SUITE(SHA256DigestTest);
322 //CPPUNIT_TEST(testTypicalUsage);
323 //CPPUNIT_TEST(testConstruction);
324 //CPPUNIT_TEST(testEmptyData);
325 //CPPUNIT_TEST(testSmallMessage);
326 //CPPUNIT_TEST(testLargeMessage);
327 //CPPUNIT_TEST(testBinaryData);
328 //CPPUNIT_TEST(testReInitialization);
329 //CPPUNIT_TEST(testPiecewise);
330 CPPUNIT_TEST_SUITE_END();
331
332public:
333 virtual std::string getEmptyMsg_Digest()
334 { return ""; }
335 virtual std::string getMsg1_Digest()
336 { return "a2242ead" "55c94c3d" "eb7cf234" "0bfef9d5" "bcaca22d" "fe66e646" "745ee437" "1c633fc8"; }
337 virtual std::string getLongMsg1_Digest()
338 { return "24703531" "a4557f53" "d6da5faa" "c96566e4" "d8b1b8ec" "9655757f" "53778d62" "63257943"; }
339 virtual std::string getBinaryData1_Digest()
340 { return "40aff2e9" "d2d8922e" "47afd464" "8e696749" "7158785f" "bd1da870" "e7110266" "bf944880"; }
341};