util.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "tests.h"
  3. #include "util/blake2s.h"
  4. #include "util/debug.h"
  5. #include <linux/compiler.h>
  6. #include <stdlib.h>
  7. #include <string2.h>
  8. static int test_strreplace(char needle, const char *haystack,
  9. const char *replace, const char *expected)
  10. {
  11. char *new = strreplace_chars(needle, haystack, replace);
  12. int ret = strcmp(new, expected);
  13. free(new);
  14. return ret == 0;
  15. }
  16. /* Maximum data length tested by test_blake2s() */
  17. #define MAX_DATA_LEN 512
  18. /*
  19. * Hash length tested by test_blake2s(). BLAKE2s supports variable-length
  20. * hashes. However, the only user of BLAKE2s in 'perf' uses 20-byte hashes,
  21. * matching the length of the ELF build ID field. So that's the length we test.
  22. */
  23. #define HASH_LEN 20
  24. /* Test the implementation of the BLAKE2s hash algorithm. */
  25. static int test_blake2s(void)
  26. {
  27. u8 data[MAX_DATA_LEN];
  28. u8 hash[HASH_LEN];
  29. u8 hash2[HASH_LEN];
  30. struct blake2s_ctx main_ctx;
  31. /*
  32. * This value was generated by the following Python code:
  33. *
  34. * import hashlib
  35. *
  36. * data = bytes(i % 256 for i in range(513))
  37. * h = hashlib.blake2s(digest_size=20)
  38. * for i in range(513):
  39. * h.update(hashlib.blake2s(data=data[:i], digest_size=20).digest())
  40. * print(h.hexdigest())
  41. */
  42. static const u8 expected_hash_of_hashes[20] = {
  43. 0xef, 0x9b, 0x13, 0x98, 0x78, 0x8e, 0x74, 0x59, 0x9c, 0xd5,
  44. 0x0c, 0xf0, 0x33, 0x97, 0x79, 0x3d, 0x3e, 0xd0, 0x95, 0xa6
  45. };
  46. size_t i;
  47. /* Generate MAX_DATA_LEN bytes of data. */
  48. for (i = 0; i < MAX_DATA_LEN; i++)
  49. data[i] = i;
  50. blake2s_init(&main_ctx, sizeof(hash));
  51. for (i = 0; i <= MAX_DATA_LEN; i++) {
  52. struct blake2s_ctx ctx;
  53. /* Compute the BLAKE2s hash of 'i' data bytes. */
  54. blake2s_init(&ctx, HASH_LEN);
  55. blake2s_update(&ctx, data, i);
  56. blake2s_final(&ctx, hash);
  57. /* Verify that multiple updates produce the same result. */
  58. blake2s_init(&ctx, HASH_LEN);
  59. blake2s_update(&ctx, data, i / 2);
  60. blake2s_update(&ctx, &data[i / 2], i - (i / 2));
  61. blake2s_final(&ctx, hash2);
  62. TEST_ASSERT_VAL("inconsistent BLAKE2s hashes",
  63. memcmp(hash, hash2, HASH_LEN) == 0);
  64. /*
  65. * Pass the hash to another BLAKE2s context, so that we
  66. * incrementally compute the hash of all the hashes.
  67. */
  68. blake2s_update(&main_ctx, hash, HASH_LEN);
  69. }
  70. /* Verify the hash of all the hashes. */
  71. blake2s_final(&main_ctx, hash);
  72. TEST_ASSERT_VAL("wrong BLAKE2s hashes",
  73. memcmp(hash, expected_hash_of_hashes, HASH_LEN) == 0);
  74. return 0;
  75. }
  76. static int test__util(struct test_suite *t __maybe_unused, int subtest __maybe_unused)
  77. {
  78. TEST_ASSERT_VAL("empty string", test_strreplace(' ', "", "123", ""));
  79. TEST_ASSERT_VAL("no match", test_strreplace('5', "123", "4", "123"));
  80. TEST_ASSERT_VAL("replace 1", test_strreplace('3', "123", "4", "124"));
  81. TEST_ASSERT_VAL("replace 2", test_strreplace('a', "abcabc", "ef", "efbcefbc"));
  82. TEST_ASSERT_VAL("replace long", test_strreplace('a', "abcabc", "longlong",
  83. "longlongbclonglongbc"));
  84. return test_blake2s();
  85. }
  86. DEFINE_SUITE("util", util);