test-crc.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2011 Intel Corporation
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #include "monitor/crc.h"
  14. #include "src/shared/tester.h"
  15. #include <glib.h>
  16. struct crc_data {
  17. const void *packet;
  18. size_t size;
  19. uint32_t crc_init;
  20. };
  21. static const unsigned char packet_1[] = {
  22. 0xd6, 0xbe, 0x89, 0x8e, 0x00, 0x17, 0x7e, 0x01,
  23. 0x00, 0xd0, 0x22, 0x00, 0x02, 0x01, 0x06, 0x03,
  24. 0x02, 0x0d, 0x18, 0x06, 0xff, 0x6b, 0x00, 0x03,
  25. 0x16, 0x52, 0x02, 0x0a, 0x00, 0xf4, 0x09, 0x92,
  26. };
  27. static const struct crc_data crc_1 = {
  28. .packet = packet_1,
  29. .size = sizeof(packet_1),
  30. };
  31. static const unsigned char packet_2[] = {
  32. 0xd6, 0xbe, 0x89, 0x8e, 0x00, 0x17, 0x7e, 0x01,
  33. 0x00, 0xd0, 0x22, 0x00, 0x02, 0x01, 0x06, 0x03,
  34. 0x02, 0x0d, 0x18, 0x06, 0xff, 0x6b, 0x00, 0x03,
  35. 0x16, 0x54, 0x02, 0x0a, 0x00, 0x95, 0x5f, 0x14,
  36. };
  37. static const struct crc_data crc_2 = {
  38. .packet = packet_2,
  39. .size = sizeof(packet_2),
  40. };
  41. static const unsigned char packet_3[] = {
  42. 0xd6, 0xbe, 0x89, 0x8e, 0x00, 0x17, 0x7e, 0x01,
  43. 0x00, 0xd0, 0x22, 0x00, 0x02, 0x01, 0x06, 0x03,
  44. 0x02, 0x0d, 0x18, 0x06, 0xff, 0x6b, 0x00, 0x03,
  45. 0x16, 0x55, 0x02, 0x0a, 0x00, 0x85, 0x66, 0x63,
  46. };
  47. static const struct crc_data crc_3 = {
  48. .packet = packet_3,
  49. .size = sizeof(packet_3),
  50. };
  51. static const unsigned char packet_4[] = {
  52. 0xd6, 0xbe, 0x89, 0x8e, 0x00, 0x17, 0x7e, 0x01,
  53. 0x00, 0xd0, 0x22, 0x00, 0x02, 0x01, 0x06, 0x03,
  54. 0x02, 0x0d, 0x18, 0x06, 0xff, 0x6b, 0x00, 0x03,
  55. 0x16, 0x53, 0x02, 0x0a, 0x00, 0xe4, 0x30, 0xe5,
  56. };
  57. static const struct crc_data crc_4 = {
  58. .packet = packet_4,
  59. .size = sizeof(packet_4),
  60. };
  61. static const unsigned char packet_5[] = {
  62. 0xd6, 0xbe, 0x89, 0x8e, 0x03, 0x0c, 0x46, 0x1c,
  63. 0xda, 0x72, 0x02, 0x00, 0x7e, 0x01, 0x00, 0xd0,
  64. 0x22, 0x00, 0x6e, 0xf4, 0x6f,
  65. };
  66. static const struct crc_data crc_5 = {
  67. .packet = packet_5,
  68. .size = sizeof(packet_5),
  69. };
  70. static const unsigned char packet_6[] = {
  71. 0xd6, 0xbe, 0x89, 0x8e, 0x04, 0x17, 0x7e, 0x01,
  72. 0x00, 0xd0, 0x22, 0x00, 0x10, 0x09, 0x50, 0x6f,
  73. 0x6c, 0x61, 0x72, 0x20, 0x48, 0x37, 0x20, 0x30,
  74. 0x30, 0x30, 0x31, 0x37, 0x45, 0x0f, 0x8a, 0x65,
  75. };
  76. static const struct crc_data crc_6 = {
  77. .packet = packet_6,
  78. .size = sizeof(packet_6),
  79. };
  80. static const unsigned char packet_7[] = {
  81. 0xd6, 0xbe, 0x89, 0x8e, 0x05, 0x22, 0x46, 0x1c,
  82. 0xda, 0x72, 0x02, 0x00, 0x7e, 0x01, 0x00, 0xd0,
  83. 0x22, 0x00, 0x96, 0x83, 0x9a, 0xaf, 0xbe, 0x1d,
  84. 0x16, 0x03, 0x05, 0x00, 0x36, 0x00, 0x00, 0x00,
  85. 0x2a, 0x00, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xa5,
  86. 0x77, 0x2d, 0x95,
  87. };
  88. static const struct crc_data crc_7 = {
  89. .packet = packet_7,
  90. .size = sizeof(packet_7),
  91. };
  92. static const unsigned char packet_8[] = {
  93. 0x96, 0x83, 0x9a, 0xaf, 0x01, 0x00, 0xc7, 0x15,
  94. 0x4d,
  95. };
  96. static const struct crc_data crc_8 = {
  97. .packet = packet_8,
  98. .size = sizeof(packet_8),
  99. .crc_init = 0x161dbe, /* from packet_7 = 0xbe 0x1d 0x16 */
  100. };
  101. static const unsigned char packet_9[] = {
  102. 0x96, 0x83, 0x9a, 0xaf, 0x06, 0x14, 0x10, 0x00,
  103. 0x04, 0x00, 0x09, 0x07, 0x10, 0x00, 0x10, 0x11,
  104. 0x00, 0x37, 0x2a, 0x13, 0x00, 0x02, 0x14, 0x00,
  105. 0x38, 0x2a, 0x73, 0x2a, 0xa3,
  106. };
  107. static const struct crc_data crc_9 = {
  108. .packet = packet_9,
  109. .size = sizeof(packet_9),
  110. .crc_init = 0x161dbe, /* from packet_7 = 0xbe 0x1d 0x16 */
  111. };
  112. static void test_crc(gconstpointer data)
  113. {
  114. const struct crc_data *test_data = data;
  115. const uint8_t *buf = test_data->packet + test_data->size - 3;
  116. uint32_t crc_init, crc_value, crc, rev;
  117. if (test_data->crc_init)
  118. crc_init = crc24_bit_reverse(test_data->crc_init);
  119. else
  120. crc_init = crc24_bit_reverse(0x555555);
  121. crc_value = buf[0] | buf[1] << 8 | buf[2] << 16;
  122. crc = crc24_calculate(crc_init, test_data->packet + 4,
  123. test_data->size - 7);
  124. tester_debug("CRC: 0x%6.6x, Calculated: 0x%6.6x", crc_value, crc);
  125. g_assert(crc_value == crc);
  126. rev = crc24_reverse(crc_value, test_data->packet + 4,
  127. test_data->size - 7);
  128. tester_debug("Preset: 0x%6.6x, Calculated: 0x%6.6x", crc_init, rev);
  129. g_assert(crc_init == rev);
  130. tester_test_passed();
  131. }
  132. int main(int argc, char *argv[])
  133. {
  134. tester_init(&argc, &argv);
  135. tester_add("/crc/1", &crc_1, NULL, test_crc, NULL);
  136. tester_add("/crc/2", &crc_2, NULL, test_crc, NULL);
  137. tester_add("/crc/3", &crc_3, NULL, test_crc, NULL);
  138. tester_add("/crc/4", &crc_4, NULL, test_crc, NULL);
  139. tester_add("/crc/5", &crc_5, NULL, test_crc, NULL);
  140. tester_add("/crc/6", &crc_6, NULL, test_crc, NULL);
  141. tester_add("/crc/7", &crc_7, NULL, test_crc, NULL);
  142. tester_add("/crc/8", &crc_8, NULL, test_crc, NULL);
  143. tester_add("/crc/9", &crc_9, NULL, test_crc, NULL);
  144. return tester_run();
  145. }