testEncode.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*=========================================================================
  2. Program: KWSys - Kitware System Library
  3. Module: $RCSfile$
  4. Copyright (c) Kitware, Inc., Insight Consortium. All rights reserved.
  5. See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even
  7. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  8. PURPOSE. See the above copyright notices 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. " Copyright (c) Kitware, Inc., Insight Consortium. All rights reserved.\n"
  21. " See Copyright.txt or http://www.kitware.com/Copyright.htm for details.\n";
  22. static const char testMD5output1[] = "04051e509e81ef0b1612ddf0e52ca89e";
  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. }