testMd5Sum.cc
Go to the documentation of this file.
1 
11 #include <utl/Md5Sum.h>
12 #include <string>
13 
14 #include <tst/Verify.h>
15 #include <cppunit/extensions/HelperMacros.h>
16 
17 using namespace utl;
18 using namespace std;
19 using namespace tst;
20 
21 
26 class testMd5Sum : public CppUnit::TestFixture {
27 
28  CPPUNIT_TEST_SUITE(testMd5Sum);
29  CPPUNIT_TEST(testMd5);
30  CPPUNIT_TEST_EXCEPTION(testUseFinalized, Md5Sum::WrongStageException);
31  CPPUNIT_TEST(testMd5OnFile);
32  CPPUNIT_TEST_SUITE_END();
33 
34 public:
35 
36  void setUp() { }
37 
38  void tearDown() { }
39 
40  string
41  MD5(const string& data)
42  {
43  return Md5Sum(data).GetHexDigest();
44  }
45 
46  void
48  {
49  const char* const pairs[][2] = {
50  { "", "d41d8cd98f00b204e9800998ecf8427e" },
51  { "a", "0cc175b9c0f1b6a831c399e269772661" },
52  { "abc", "900150983cd24fb0d6963f7d28e17f72" },
53  { "message digest", "f96b697d7cb7938d525a2f31aaf161d0" },
54  { "abcdefghijklmnopqrstuvwxyz", "c3fcd3d76192e4007dfb496cca67e13b" },
55  { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
56  "d174ab98d277d9f5a5611c2c9f419d9f" },
57  { "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
58  "57edf4a22be3c955ac49da2e2107b67a" },
59  // exactly 63
60  { "3.1415926535897932384626433832795028841971693993751058209749445",
61  "57c4db5f5fb1bf5f74ff805023238884" },
62  // exactly 64
63  { "3.14159265358979323846264338327950288419716939937510582097494459",
64  "a8faf9a61d25eafea6fdbfd06d05d71b" },
65  // exactly 65
66  { "3.141592653589793238462643383279502884197169399375105820974944592",
67  "f78333b1a39dc28bedda135ffeaf58d6" },
68  // exactly 500
69  { "3.14159265358979323846264338327950288419716939937510582097494459230781"
70  "6406286208998628034825342117067982148086513282306647093844609550582231"
71  "7253594081284811174502841027019385211055596446229489549303819644288109"
72  "7566593344612847564823378678316527120190914564856692346034861045432664"
73  "8213393607260249141273724587006606315588174881520920962829254091715364"
74  "3678925903600113305305488204665213841469519415116094330572703657595919"
75  "5309218611738193261179310511854807446237996274956735188575272489122793"
76  "8183011949129833673362440656643086021394946395224737190702179860943702"
77  "7705392171762931767523846748184676694051320005681271452635608277857713"
78  "4275778960917363717872146844090122495343014654958537105079227968925892"
79  "3542019956112129021960864034418159813629774771309960518707211349999998"
80  "3729780499510597317328160963185950244594553469083026425223082533446850"
81  "3526193118817101000313783875288658753320838142061717766914730359825349"
82  "0428755468731159562863882353787593751957781857780532171226806613001927"
83  "876611195909216420199", "02f9a22f0e8d910397c7cf9222a7a94b" }
84  };
85 
86  CPPUNIT_ASSERT(Verify<Equal>(Md5Sum().GetHexDigest(), string(pairs[0][1])));
87 
88  const int n = sizeof(pairs) / sizeof(pairs[0]);
89 
90  for (int i = 0; i < n; ++i)
91  CPPUNIT_ASSERT(Verify<Equal>(MD5(pairs[i][0]), string(pairs[i][1])));
92  }
93 
94  void
96  {
97  Md5Sum md5;
98  md5.GetRawDigest();
99  md5.AddData("");
100  }
101 
102  void
104  {
105  ifstream file(TEST_SOURCE_DIRECTORY "/md5sums.txt");
106  CPPUNIT_ASSERT(file.is_open());
107  int n = 0;
108  string line;
109  string text;
110  string checksum;
111  istringstream is;
112  cout << "testing length";
113  while (getline(file, line)) {
114  is.str(line);
115  is.clear();
116  if (!(is >> text >> checksum))
117  break;
118  cout << ' ' << text.length();
119  CPPUNIT_ASSERT(Verify<Equal>(MD5(text), checksum));
120  ++n;
121  }
122  cout << endl;
123  CPPUNIT_ASSERT(Verify<Equal>(n, 500));
124  }
125 
126 };
127 
128 
string MD5(const string &data)
Definition: testMd5Sum.cc:41
Class to compute MD5 checksum Based on the RSA C code, wrapped in an OO fashion.
Definition: Md5Sum.h:27
void setUp()
Definition: testMd5Sum.cc:36
bool is(const double a, const double b)
Definition: testlib.cc:113
void tearDown()
Definition: testMd5Sum.cc:38
CPPUNIT_TEST_SUITE_REGISTRATION(testAiresShowerFile)
void testUseFinalized()
Definition: testMd5Sum.cc:95
IteratorPair GetRawDigest()
Definition: Md5Sum.cc:309
void testMd5OnFile()
Definition: testMd5Sum.cc:103
void testMd5()
Definition: testMd5Sum.cc:47
const string file
uint16_t * data
Definition: dump1090.h:228
std::string GetHexDigest()
Definition: Md5Sum.cc:340
void AddData(const std::string &s)
Process the data in a string.
Definition: Md5Sum.h:56

, generated on Tue Sep 26 2023.