io_utilities_unittest.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. /// \brief Test of asiolink utilities
  15. ///
  16. /// Tests the functionality of the asiolink utilities code by comparing them
  17. /// with the equivalent methods in isc::dns::[Input/Output]Buffer.
  18. #include <cstddef>
  19. #include <arpa/inet.h>
  20. #include <gtest/gtest.h>
  21. #include <util/buffer.h>
  22. #include <util/io_utilities.h>
  23. using namespace isc::util;
  24. TEST(asioutil, readUint16) {
  25. // Reference buffer
  26. uint8_t data[2];
  27. InputBuffer buffer(data, sizeof(data));
  28. // Avoid possible compiler warnings by only setting uint8_t variables to
  29. // uint8_t values.
  30. uint8_t i8 = 0;
  31. uint8_t j8 = 0;
  32. for (int i = 0; i < (2 << 8); ++i, ++i8) {
  33. for (int j = 0; j < (2 << 8); ++j, ++j8) {
  34. data[0] = i8;
  35. data[1] = j8;
  36. buffer.setPosition(0);
  37. EXPECT_EQ(buffer.readUint16(), readUint16(data, sizeof(data)));
  38. }
  39. }
  40. }
  41. TEST(asioutil, readUint16OutOfRange) {
  42. uint8_t data;
  43. EXPECT_THROW(readUint16(&data, sizeof(data)), isc::OutOfRange);
  44. }
  45. TEST(asioutil, writeUint16) {
  46. // Reference buffer
  47. OutputBuffer buffer(2);
  48. uint8_t test[2];
  49. // Avoid possible compiler warnings by only setting uint16_t variables to
  50. // uint16_t values.
  51. uint16_t i16 = 0;
  52. for (uint32_t i = 0; i < (2 << 16); ++i, ++i16) {
  53. // Write the reference data
  54. buffer.clear();
  55. buffer.writeUint16(i16);
  56. // ... and the test data
  57. writeUint16(i16, test, sizeof(test));
  58. // ... and compare
  59. const uint8_t* ref = static_cast<const uint8_t*>(buffer.getData());
  60. EXPECT_EQ(ref[0], test[0]);
  61. EXPECT_EQ(ref[1], test[1]);
  62. }
  63. }
  64. TEST(asioutil, writeUint16OutOfRange) {
  65. uint16_t i16 = 42;
  66. uint8_t data;
  67. EXPECT_THROW(writeUint16(i16, &data, sizeof(data)), isc::OutOfRange);
  68. }
  69. // test data shared amount readUint32 and writeUint32 tests
  70. const static uint32_t test32[] = {
  71. 0,
  72. 1,
  73. 2000,
  74. 0x80000000,
  75. 0xffffffff
  76. };
  77. TEST(asioutil, readUint32) {
  78. uint8_t data[8];
  79. // make sure that we can read data, regardless of
  80. // the memory alignment. That' why we need to repeat
  81. // it 4 times.
  82. for (int offset=0; offset < 4; offset++) {
  83. for (int i=0; i < sizeof(test32)/sizeof(uint32_t); i++) {
  84. uint32_t tmp = htonl(test32[i]);
  85. memcpy(&data[offset], &tmp, sizeof(uint32_t));
  86. EXPECT_EQ(test32[i], readUint32(&data[offset], sizeof(uint32_t)));
  87. }
  88. }
  89. }
  90. TEST(asioutil, readUint32OutOfRange) {
  91. uint8_t data[3];
  92. EXPECT_THROW(readUint32(data, sizeof(data)), isc::OutOfRange);
  93. }
  94. TEST(asioutil, writeUint32) {
  95. uint8_t data[8];
  96. // make sure that we can write data, regardless of
  97. // the memory alignment. That's why we need to repeat
  98. // it 4 times.
  99. for (int offset=0; offset < 4; offset++) {
  100. for (int i=0; i < sizeof(test32)/sizeof(uint32_t); i++) {
  101. uint8_t* ptr = writeUint32(test32[i], &data[offset],
  102. sizeof(uint32_t));
  103. EXPECT_EQ(&data[offset]+sizeof(uint32_t), ptr);
  104. uint32_t tmp = htonl(test32[i]);
  105. EXPECT_EQ(0, memcmp(&tmp, &data[offset], sizeof(uint32_t)));
  106. }
  107. }
  108. }
  109. TEST(asioutil, writeUint32OutOfRange) {
  110. uint32_t i32 = 28;
  111. uint8_t data[3];
  112. EXPECT_THROW(writeUint32(i32, data, sizeof(data)), isc::OutOfRange);
  113. }