Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:29:22

0001 #include "FWCore/Utilities/interface/Digest.h"
0002 #include "FWCore/Utilities/interface/Exception.h"
0003 
0004 #include <cassert>
0005 #include <iostream>
0006 
0007 using cms::Digest;
0008 using cms::MD5Result;
0009 
0010 void testGivenString(std::string const& s) {
0011   Digest dig1(s);
0012   MD5Result r1 = dig1.digest();
0013 
0014   Digest dig2;
0015   dig2.append(s);
0016   MD5Result r2 = dig2.digest();
0017   assert(r1 == r2);
0018 
0019   // The result should be valid *iff* s is non-empty.
0020   assert(r1.isValid() == !s.empty());
0021   assert(r1.toString().size() == 32);
0022   assert(r1.compactForm().size() == 16);
0023 }
0024 
0025 void testConversions() {
0026   std::string data("aldjfakl\tsdjf34234 \najdf");
0027   Digest dig(data);
0028   MD5Result r1 = dig.digest();
0029   assert(r1.isValid());
0030   std::string hexy = r1.toString();
0031   assert(hexy.size() == 32);
0032   MD5Result r2;
0033   r2.fromHexifiedString(hexy);
0034   assert(r1 == r2);
0035   assert(r1.toString() == r2.toString());
0036   assert(r1.compactForm() == r2.compactForm());
0037 
0038   //check the MD5Result lookup table
0039   MD5Result lookup;
0040   MD5Result fromHex;
0041   for (unsigned int i = 0; i < 256; ++i) {
0042     for (unsigned int j = 0; j < 16; ++j) {
0043       lookup.bytes[j] = static_cast<char>(i);
0044       fromHex.fromHexifiedString(lookup.toString());
0045       assert(lookup == fromHex);
0046       assert(lookup.toString() == fromHex.toString());
0047       assert(lookup.compactForm() == fromHex.compactForm());
0048     }
0049   }
0050 }
0051 
0052 void testEmptyString() {
0053   std::string e;
0054   testGivenString(e);
0055 
0056   Digest dig1;
0057   MD5Result r1 = dig1.digest();
0058 
0059   MD5Result r2;
0060   assert(r1 == r2);
0061 
0062   assert(!r1.isValid());
0063 }
0064 
0065 int main() {
0066   Digest dig1;
0067   dig1.append("hello");
0068   Digest dig2("hello");
0069 
0070   MD5Result r1 = dig1.digest();
0071   MD5Result r2 = dig2.digest();
0072 
0073   assert(r1 == r2);
0074   assert(!(r1 < r2));
0075   assert(!(r2 < r1));
0076 
0077   assert(r1.toString().size() == 32);
0078 
0079   testGivenString("a");
0080   testGivenString("{ }");
0081   testGivenString("abc 123 abc");
0082   testEmptyString();
0083   try {
0084     testConversions();
0085   } catch (cms::Exception const& e) {
0086     std::cerr << e.explainSelf() << std::endl;
0087     return 1;
0088   }
0089   return 0;
0090 }