hex_test.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. *******************************************************************************
  3. \file hex_test.c
  4. \brief Tests for hexadecimal strings
  5. \project bee2/test
  6. \author (C) Sergey Agievich [agievich@{bsu.by|gmail.com}]
  7. \created 2016.06.17
  8. \version 2016.06.17
  9. \license This program is released under the GNU General Public License
  10. version 3. See Copyright Notices in bee2/info.h.
  11. *******************************************************************************
  12. */
  13. #include <bee2/core/hex.h>
  14. #include <bee2/core/mem.h>
  15. #include <bee2/core/str.h>
  16. #include <bee2/crypto/belt.h>
  17. /*
  18. *******************************************************************************
  19. Тестирование
  20. *******************************************************************************
  21. */
  22. bool_t hexTest()
  23. {
  24. octet buf[256];
  25. char hex[512 + 1];
  26. char hex1[512 + 1];
  27. size_t count;
  28. // валидация
  29. if (!hexIsValid("1234") ||
  30. hexIsValid("12345") ||
  31. !hexIsValid("ABCDEFabcdef") ||
  32. hexIsValid("abcdefgh"))
  33. return FALSE;
  34. // кодировать / декодировать
  35. for (count = 0; count <= 256; ++count)
  36. {
  37. hexFrom(hex, beltH(), count);
  38. if (!hexEq(beltH(), hex))
  39. return FALSE;
  40. hexTo(buf, hex);
  41. if (!memEq(buf, beltH(), count))
  42. return FALSE;
  43. hexFromRev(hex, beltH(), count);
  44. if (!hexEqRev(beltH(), hex))
  45. return FALSE;
  46. hexToRev(buf, hex);
  47. if (!memEq(buf, beltH(), count))
  48. return FALSE;
  49. memCopy(hex1, hex, sizeof(hex));
  50. hexLower(hex1);
  51. hexUpper(hex1);
  52. if (!strEq(hex, hex1))
  53. return FALSE;
  54. }
  55. // все нормально
  56. return TRUE;
  57. }