testEncode.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*============================================================================
  2. KWSys - Kitware System Library
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "kwsysPrivate.h"
  11. #include KWSYS_HEADER(MD5.h)
  12. /* Work-around CMake dependency scanning limitation. This must
  13. duplicate the above list of headers. */
  14. #if 0
  15. # include "MD5.h.in"
  16. #endif
  17. #include <stdio.h>
  18. #include <string.h>
  19. static const unsigned char testMD5input1[] =
  20. " A quick brown fox jumps over the lazy dog.\n"
  21. " This is sample text for MD5 sum input.\n";
  22. static const char testMD5output1[] = "8f146af46ed4f267921bb937d4d3500c";
  23. static const int testMD5input2len = 28;
  24. static const unsigned char testMD5input2[] = "the cow jumped over the moon";
  25. static const char testMD5output2[] = "a2ad137b746138fae4e5adca9c85d3ae";
  26. static int testMD5_1(kwsysMD5* md5)
  27. {
  28. char md5out[33];
  29. kwsysMD5_Initialize(md5);
  30. kwsysMD5_Append(md5, testMD5input1, -1);
  31. kwsysMD5_FinalizeHex(md5, md5out);
  32. md5out[32] = 0;
  33. printf("md5sum 1: expected [%s]\n"
  34. " got [%s]\n",
  35. testMD5output1, md5out);
  36. return (strcmp(md5out, testMD5output1) != 0)? 1:0;
  37. }
  38. static int testMD5_2(kwsysMD5* md5)
  39. {
  40. unsigned char digest[16];
  41. char md5out[33];
  42. kwsysMD5_Initialize(md5);
  43. kwsysMD5_Append(md5, testMD5input2, testMD5input2len);
  44. kwsysMD5_Finalize(md5, digest);
  45. kwsysMD5_DigestToHex(digest, md5out);
  46. md5out[32] = 0;
  47. printf("md5sum 2: expected [%s]\n"
  48. " got [%s]\n",
  49. testMD5output2, md5out);
  50. return (strcmp(md5out, testMD5output2) != 0)? 1:0;
  51. }
  52. int testEncode(int argc, char* argv[])
  53. {
  54. int result = 0;
  55. (void)argc;
  56. (void)argv;
  57. /* Test MD5 digest. */
  58. {
  59. kwsysMD5* md5 = kwsysMD5_New();
  60. result |= testMD5_1(md5);
  61. result |= testMD5_2(md5);
  62. kwsysMD5_Delete(md5);
  63. }
  64. return result;
  65. }