1
0

Checksum_test.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "util/Checksum.h"
  16. #include "util/Bits.h"
  17. #include "util/Endian.h"
  18. #include "util/Hex.h"
  19. #include "util/Assert.h"
  20. #include <stdio.h>
  21. #define UDP_PACKET_HEX /* vvvv --- IPv4 header checksum */\
  22. "4500""0033""7a95""4000""4011""f877""c0a8""0102""0402""0201" \
  23. /* UDP checksum -- vvvv */\
  24. "9e05""0035""001f""c7dd" \
  25. /* content */\
  26. "221a""0100""0001""0000""0000""0000""0564""6562""6f38""0000""1c00""01"
  27. static const uint8_t* udpPacketHex = (uint8_t*) UDP_PACKET_HEX;
  28. #define UDP_PACKET_SIZE ((sizeof(UDP_PACKET_HEX)-1)/2)
  29. static void checksumAlgorithmTest()
  30. {
  31. uint8_t packetBuff[UDP_PACKET_SIZE + 1];
  32. uint8_t* packet = packetBuff + ((uintptr_t)packetBuff % 2);
  33. Hex_decode(packet, UDP_PACKET_SIZE, udpPacketHex, UDP_PACKET_SIZE * 2);
  34. //operating on the ip checksum which is easy to compute.
  35. uint16_t checksum;
  36. Bits_memcpy(&checksum, &packet[8], 2);
  37. packet[8] = 0;
  38. packet[9] = 0;
  39. uint16_t calcatedSum = Checksum_engine(packet, 20);
  40. //printf("%2x == %2x", checksum, calcatedSum);
  41. Assert_true(checksum == calcatedSum);
  42. }
  43. #define UDP6_PACKET_HEX \
  44. /* Packet type --- vv */ \
  45. "6000""0000""0019""1140" \
  46. /* Source addr. */ \
  47. "fce5""de17""cbde""c87b""5289""0556""8b83""c9c8" \
  48. /* Dest addr. */ \
  49. "fc00""0000""0000""0000""0000""0000""0000""0001" \
  50. /* UDP checksum -- vvvv */ \
  51. "b4a9""0035""0019""4972" \
  52. /* Content */ \
  53. "e4e4""0100""0001""0000""0000""0000""0000""0200" \
  54. "01"
  55. static const uint8_t* udp6PacketHex = (uint8_t*) UDP6_PACKET_HEX;
  56. #define UDP6_PACKET_SIZE ((sizeof(UDP6_PACKET_HEX)-1)/2)
  57. static void udp6ChecksumTest()
  58. {
  59. uint8_t packet[UDP6_PACKET_SIZE + 3];
  60. Hex_decode(packet, UDP6_PACKET_SIZE, udp6PacketHex, UDP6_PACKET_SIZE * 2);
  61. // add some evil at the end to check for buffer overrun
  62. packet[UDP6_PACKET_SIZE] = 0x00;
  63. packet[UDP6_PACKET_SIZE + 1] = 0x00;
  64. packet[UDP6_PACKET_SIZE + 2] = 0x00;
  65. uint16_t udp6Checksum;
  66. Bits_memcpy(&udp6Checksum, &packet[46], 2);
  67. packet[46] = 0;
  68. packet[47] = 0;
  69. uint16_t calcatedSum = Checksum_udpIp6(&packet[8], &packet[40], 25);
  70. printf("%2x == %2x", udp6Checksum, calcatedSum);
  71. Assert_true(udp6Checksum == calcatedSum);
  72. }
  73. #define PING6_PACKET_HEX \
  74. /* Packet type --- vv */ \
  75. "6000""0000""0040""3a40" \
  76. /* Source Addr. */\
  77. "fce5""de17""cbde""c87b""5289""0556""8b83""c9c8" \
  78. /* Dest Addr. */\
  79. "fc00""0000""0000""0000""0000""0000""0000""0001" \
  80. /* vvvv -- ICMP6 Checksum */\
  81. "8000""1d00" \
  82. /* Content */\
  83. "792e""0020""11d3""a04f""0000""0000""57bb""0800" \
  84. "0000""0000""1011""1213""1415""1617""1819""1a1b" \
  85. "1c1d""1e1f""2021""2223""2425""2627""2829""2a2b" \
  86. "2c2d""2e2f""3031""3233""3435""3637"
  87. static const uint8_t* ping6PacketHex = (uint8_t*) PING6_PACKET_HEX;
  88. #define PING6_PACKET_SIZE ((sizeof(PING6_PACKET_HEX)-1)/2)
  89. static void icmp6ChecksumTest()
  90. {
  91. uint8_t packet[PING6_PACKET_SIZE];
  92. Hex_decode(packet, PING6_PACKET_SIZE, ping6PacketHex, PING6_PACKET_SIZE * 2);
  93. uint16_t checksum;
  94. Bits_memcpy(&checksum, &packet[42], 2);
  95. packet[42] = 0;
  96. packet[43] = 0;
  97. uint16_t calcatedSum = Checksum_icmp6(&packet[8], &packet[40], 64);
  98. //printf("%2x == %2x", checksum, calcatedSum);
  99. Assert_true(checksum == calcatedSum);
  100. }
  101. int main()
  102. {
  103. checksumAlgorithmTest();
  104. udp6ChecksumTest();
  105. icmp6ChecksumTest();
  106. return 0;
  107. }