File: Test_Digest.h

package info (click to toggle)
libcommoncpp2 1.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,272 kB
  • ctags: 4,532
  • sloc: cpp: 29,219; sh: 10,352; ansic: 1,134; makefile: 232; xml: 5
file content (341 lines) | stat: -rw-r--r-- 11,395 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Copyright (C) 2002-2003 Chad C. Yates cyates@uidaho.edu
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// As a special exception to the GNU General Public License, permission is
// granted for additional uses of the text contained in its release
// of Common C++.
//
// The exception is that, if you link the Common C++ library with other
// files to produce an executable, this does not by itself cause the
// resulting executable to be covered by the GNU General Public License.
// Your use of that executable is in no way restricted on account of
// linking the Common C++ library code into it.
//
// This exception does not however invalidate any other reasons why
// the executable file might be covered by the GNU General Public License.
//
// This exception applies only to the code released under the
// name Common C++.  If you copy code from other releases into a copy of
// Common C++, as the General Public License permits, the exception does
// not apply to the code that you add in this way.  To avoid misleading
// anyone as to the status of such modified files, you must delete
// this exception notice from them.
//
// If you write modifications of your own for Common C++, it is your choice
// whether to permit this exception to apply to your modifications.
// If you do not wish that, delete this exception notice.

#include <cppunit/extensions/HelperMacros.h>
#include <cc++/digest.h>
#include <fstream>

using namespace ost;

/**
 * Template class that provides common tests and data to excercise
 * the digest classes.  Provides common messages to run through the digests.
 * Sub-classes of this Fixture provide what tests to run and what the expected
 * digest strings are, as well as any other tests that are specific to that digest
 * type.  see the pre-defined subclasses for examples
 *
 * @author Chad C. Yates
 */
template<class DigestTypeClass>
class GenericDigestTests : public CppUnit::TestFixture
{
private:
  enum {BINARY_DATA1_SIZE = 256};  // constant trick
  unsigned char binaryData1[BINARY_DATA1_SIZE];

protected:
  DigestTypeClass digest;
  std::stringstream digestOutput;

  GenericDigestTests() {
	  // generate some binary data
	  for(unsigned int i = 0; i< BINARY_DATA1_SIZE; ++i)
		binaryData1[i] = i % 256; // repeating pattern of ascending bytes

	  // output it to a file in case it needs to be checked against an external program
	  std::ofstream file("BinaryData1.dat", std::ios::out|std::ios::binary);
	  file.write((char *) binaryData1, sizeof(binaryData1));
	  file.close();
	}

  virtual ~GenericDigestTests()
	{}

  std::string getEmptyMsg()
	{ return ""; }

  std::string getMsg1()
	{ return "pippo"; }

  std::string getLongMsg1()
	{ return "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah "
	  "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah "
	  "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah "
	  "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah "
	  "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah "
	  "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah "
	  "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah "
	  "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah "
	  "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah "
	  "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah "
	  "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah "
	  "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah "
	  "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah "
	  "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah "
	  "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah "
	  "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah "
	  "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah";
	}
  unsigned char* getBinaryData1()
  { return binaryData1; }

  // These define the textual digests that should be the result of processing
  // the corresponding message/data and exporting to a stream
  virtual std::string getEmptyMsg_Digest() { return ""; }
  virtual std::string getMsg1_Digest() { return ""; }
  virtual std::string getLongMsg1_Digest() { return ""; }
  virtual std::string getBinaryData1_Digest() { return ""; }

public:
  /**
   * Test case for Digest
   *
   * @author Chad C. Yates
   */
  void testTypicalUsage();

  /**
   * Test case for Digest
   *
   * @author Chad C. Yates
   */
  void testConstruction();

  /**
   * Test case for Digest
   *
   * @author Chad C. Yates
   */
  void testEmptyData();

  /**
   * Test case for Digest
   *
   * @author Chad C. Yates
   */
  void testSmallMessage();

  /**
   * Test case for Digest
   *
   * @author Chad C. Yates
   */
  void testLargeMessage();

  /**
   * Test case for Digest
   *
   * @author Chad C. Yates
   */
  void testBinaryData();

  /**
   * Test case for Digest
   *
   * @author Chad C. Yates
   */
  void testReInitialization();

  /**
   * Test case for Digest
   * attempts to excercise the putDigest method by giving it a long string a piece at a time.
   *
   * @author Chad C. Yates
   */
  void testPiecewise();
};

/**
 * Subclass of the generic digest tests that specifies the expected digest
 * strings and tests to run for the Checksum digest class.
 */
class ChecksumDigestTest : public GenericDigestTests<ChecksumDigest>
{
  CPPUNIT_TEST_SUITE(ChecksumDigestTest);
  CPPUNIT_TEST(testTypicalUsage);
  CPPUNIT_TEST(testConstruction);
  CPPUNIT_TEST(testEmptyData);
  CPPUNIT_TEST(testSmallMessage);
  CPPUNIT_TEST(testLargeMessage);
  CPPUNIT_TEST(testBinaryData);
  CPPUNIT_TEST(testReInitialization);
  CPPUNIT_TEST(testPiecewise);
  CPPUNIT_TEST_SUITE_END();

public:
  std::string getEmptyMsg_Digest()
	{ return "00"; }
  std::string getMsg1_Digest()
	{ return "28"; }
  std::string getLongMsg1_Digest()
	{ return "29"; }
  std::string getBinaryData1_Digest()
	{ return "80"; }
};

/**
 * Subclass of the generic digest tests that specifies the expected digest
 * strings and tests to run for the CRC16 digest class.
 */
class CRC16DigestTest : public GenericDigestTests<CRC16Digest>
{
  CPPUNIT_TEST_SUITE(CRC16DigestTest);
  CPPUNIT_TEST(testTypicalUsage);
  CPPUNIT_TEST(testConstruction);
  CPPUNIT_TEST(testEmptyData);
  CPPUNIT_TEST(testSmallMessage);
  CPPUNIT_TEST(testLargeMessage);
  CPPUNIT_TEST(testBinaryData);
  CPPUNIT_TEST(testReInitialization);
  CPPUNIT_TEST(testPiecewise);
  CPPUNIT_TEST_SUITE_END();

public:
  std::string getEmptyMsg_Digest()
	{ return "0000"; }
  std::string getMsg1_Digest()
	{ return "fa3b"; }
  std::string getLongMsg1_Digest()
	{ return "54cf"; }
  std::string getBinaryData1_Digest()
	{ return "7e55"; }
};

/**
 * Subclass of the generic digest tests that specifies the expected digest
 * strings and tests to run for the CRC32 digest class.
 */
class CRC32DigestTest : public GenericDigestTests<CRC32Digest>
{
  CPPUNIT_TEST_SUITE(CRC32DigestTest);
  CPPUNIT_TEST(testTypicalUsage);
  CPPUNIT_TEST(testConstruction);
  CPPUNIT_TEST(testEmptyData);
  CPPUNIT_TEST(testSmallMessage);
  CPPUNIT_TEST(testLargeMessage);
  CPPUNIT_TEST(testBinaryData);
  CPPUNIT_TEST(testReInitialization);
  CPPUNIT_TEST(testPiecewise);
  CPPUNIT_TEST_SUITE_END();

public:
  virtual std::string getEmptyMsg_Digest()
	{ return "00000000"; }
  virtual std::string getMsg1_Digest()
	{ return "df1dc234"; }
  virtual std::string getLongMsg1_Digest()
	{ return "2d69085d"; }
  virtual std::string getBinaryData1_Digest()
	{ return "29058c73"; }
};

/**
 * Subclass of the generic digest tests that specifies the expected digest
 * strings and tests to run for the MD5 digest class.
 */
class MD5DigestTest : public GenericDigestTests<MD5Digest>
{
  CPPUNIT_TEST_SUITE(MD5DigestTest);
  CPPUNIT_TEST(testTypicalUsage);
  CPPUNIT_TEST(testConstruction);
  CPPUNIT_TEST(testEmptyData);
  CPPUNIT_TEST(testSmallMessage);
  CPPUNIT_TEST(testLargeMessage);
  CPPUNIT_TEST(testBinaryData);
  CPPUNIT_TEST(testReInitialization);
  CPPUNIT_TEST(testPiecewise);
  CPPUNIT_TEST_SUITE_END();

public:
  virtual std::string getEmptyMsg_Digest()
	{ return "d41d8cd9" "8f00b204" "e9800998" "ecf8427e"; }
  virtual std::string getMsg1_Digest()
	{ return "0c88028b" "f3aa6a6a" "143ed846" "f2be1ea4"; }
  virtual std::string getLongMsg1_Digest()
	{ return "7588d0eb" "c99997f3" "a4284b11" "6ac117b5"; }
  virtual std::string getBinaryData1_Digest()
	{ return "e2c865db" "4162bed9" "63bfaa9e" "f6ac18f0"; }
};

/**
 * Subclass of the generic digest tests that specifies the expected digest
 * strings and tests to run for the SHA1 digest class.
 */
class SHA1DigestTest : public GenericDigestTests<SHA1Digest>
{
  CPPUNIT_TEST_SUITE(SHA1DigestTest);
  //CPPUNIT_TEST(testTypicalUsage);
  //CPPUNIT_TEST(testConstruction);
  //CPPUNIT_TEST(testEmptyData);
  //CPPUNIT_TEST(testSmallMessage);
  //CPPUNIT_TEST(testLargeMessage);
  //CPPUNIT_TEST(testBinaryData);
  //CPPUNIT_TEST(testReInitialization);
  //CPPUNIT_TEST(testPiecewise);
  CPPUNIT_TEST_SUITE_END();

public:
  virtual std::string getEmptyMsg_Digest()
	{ return ""; }
  virtual std::string getMsg1_Digest()
	{ return "d012f681" "44ed0f12" "1d3cc330" "a17eec52" "8c2e7d59"; }
  virtual std::string getLongMsg1_Digest()
	{ return "c2cce34c" "96d1ab91" "ce5cb15d" "7542a37a" "a7c57fae"; }
  virtual std::string getBinaryData1_Digest()
	{ return "4916d6bd" "b7f78e68" "03698cab" "32d1586e" "a457dfc8"; }
};

/**
 * Subclass of the generic digest tests that specifies the expected digest
 * strings and tests to run for the SHA256 digest class.
 */
class SHA256DigestTest : public GenericDigestTests<SHA256Digest>
{
  CPPUNIT_TEST_SUITE(SHA256DigestTest);
  //CPPUNIT_TEST(testTypicalUsage);
  //CPPUNIT_TEST(testConstruction);
  //CPPUNIT_TEST(testEmptyData);
  //CPPUNIT_TEST(testSmallMessage);
  //CPPUNIT_TEST(testLargeMessage);
  //CPPUNIT_TEST(testBinaryData);
  //CPPUNIT_TEST(testReInitialization);
  //CPPUNIT_TEST(testPiecewise);
  CPPUNIT_TEST_SUITE_END();

public:
  virtual std::string getEmptyMsg_Digest()
	{ return ""; }
  virtual std::string getMsg1_Digest()
	{ return "a2242ead" "55c94c3d" "eb7cf234" "0bfef9d5" "bcaca22d" "fe66e646" "745ee437" "1c633fc8"; }
  virtual std::string getLongMsg1_Digest()
	{ return "24703531" "a4557f53" "d6da5faa" "c96566e4" "d8b1b8ec" "9655757f" "53778d62" "63257943"; }
  virtual std::string getBinaryData1_Digest()
	{ return "40aff2e9" "d2d8922e" "47afd464" "8e696749" "7158785f" "bd1da870" "e7110266" "bf944880"; }
};