byte_buffer_unittest.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (C) 2016, The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <array>
  17. #include <cstring>
  18. #include <string>
  19. #include "gtest/gtest.h"
  20. #include "wifilogd/byte_buffer.h"
  21. namespace android {
  22. namespace wifilogd {
  23. namespace {
  24. constexpr size_t kBufferSizeBytes = 1024;
  25. constexpr std::array<uint8_t, 1> kSmallestMessage{};
  26. constexpr std::array<uint8_t, kBufferSizeBytes> kLargestMessage{};
  27. class ByteBufferTest : public ::testing::Test {
  28. public:
  29. ByteBufferTest() {}
  30. protected:
  31. ByteBuffer<kBufferSizeBytes> buffer_;
  32. };
  33. } // namespace
  34. TEST_F(ByteBufferTest, AppendMinimalOnEmptyBufferSucceeds) {
  35. buffer_.AppendOrDie(kSmallestMessage.data(), kSmallestMessage.size());
  36. }
  37. TEST_F(ByteBufferTest, AppendMaximalOnEmptyBufferSucceeds) {
  38. buffer_.AppendOrDie(kLargestMessage.data(), kLargestMessage.size());
  39. }
  40. TEST_F(ByteBufferTest, AppendStoresOurData) {
  41. const std::string message1{"hello"};
  42. const std::string message2{"world"};
  43. buffer_.AppendOrDie(message1.data(), message1.size());
  44. buffer_.AppendOrDie(message2.data(), message2.size());
  45. const std::string expected{"helloworld"};
  46. EXPECT_EQ(0, std::memcmp(buffer_.data(), expected.data(), expected.size()));
  47. }
  48. TEST_F(ByteBufferTest, AssigningWorks) {
  49. const std::string message1{"hello"};
  50. buffer_.AppendOrDie(message1.data(), message1.size());
  51. ByteBuffer<kBufferSizeBytes> copy;
  52. ASSERT_NE(buffer_.size(), copy.size());
  53. copy = buffer_;
  54. ASSERT_EQ(buffer_.size(), copy.size());
  55. EXPECT_EQ(0, std::memcmp(copy.data(), buffer_.data(), buffer_.size()));
  56. }
  57. TEST_F(ByteBufferTest, ChainingWorks) {
  58. const std::string message1{"hello"};
  59. const std::string message2{"world"};
  60. buffer_.AppendOrDie(message1.data(), message1.size())
  61. .AppendOrDie(message2.data(), message2.size());
  62. const std::string expected{"helloworld"};
  63. EXPECT_EQ(0, std::memcmp(buffer_.data(), expected.data(), expected.size()));
  64. }
  65. TEST_F(ByteBufferTest, CopyingWorks) {
  66. const std::string message1{"hello"};
  67. buffer_.AppendOrDie(message1.data(), message1.size());
  68. const ByteBuffer<kBufferSizeBytes> copy{buffer_};
  69. ASSERT_EQ(buffer_.size(), copy.size());
  70. EXPECT_EQ(0, std::memcmp(copy.data(), buffer_.data(), buffer_.size()));
  71. }
  72. TEST_F(ByteBufferTest, DataDoesNotReturnNullOnFreshBuffer) {
  73. EXPECT_NE(nullptr, buffer_.data());
  74. }
  75. TEST_F(ByteBufferTest, DataDoesNotReturnNullAfterLargeWrite) {
  76. buffer_.AppendOrDie(kLargestMessage.data(), kLargestMessage.size());
  77. EXPECT_NE(nullptr, buffer_.data());
  78. }
  79. TEST_F(ByteBufferTest, SizeReturnsZeroOnFreshBuffer) {
  80. EXPECT_EQ(0U, buffer_.size());
  81. }
  82. TEST_F(ByteBufferTest, SizeIsCorrectAfterSmallWrite) {
  83. buffer_.AppendOrDie(kSmallestMessage.data(), kSmallestMessage.size());
  84. EXPECT_EQ(kSmallestMessage.size(), buffer_.size());
  85. }
  86. TEST_F(ByteBufferTest, SizeIsCorrectAfterLargeWrite) {
  87. buffer_.AppendOrDie(kLargestMessage.data(), kLargestMessage.size());
  88. EXPECT_EQ(kLargestMessage.size(), buffer_.size());
  89. }
  90. TEST_F(ByteBufferTest, SizeIsCorrectAfterMultipleWrites) {
  91. buffer_.AppendOrDie(kSmallestMessage.data(), kSmallestMessage.size())
  92. .AppendOrDie(kSmallestMessage.data(), kSmallestMessage.size())
  93. .AppendOrDie(kSmallestMessage.data(), kSmallestMessage.size());
  94. EXPECT_EQ(3 * kSmallestMessage.size(), buffer_.size());
  95. }
  96. // Per
  97. // github.com/google/googletest/blob/master/googletest/docs/AdvancedGuide.md#death-tests,
  98. // death tests should be specially named.
  99. using ByteBufferDeathTest = ByteBufferTest;
  100. TEST_F(ByteBufferDeathTest, AppendLargerThanBufferCausesDeath) {
  101. constexpr std::array<uint8_t, kBufferSizeBytes + 1> oversized_message{};
  102. EXPECT_DEATH(
  103. buffer_.AppendOrDie(oversized_message.data(), oversized_message.size()),
  104. "Check failed");
  105. }
  106. TEST_F(ByteBufferDeathTest, AppendLargerThanFreeSpaceCausesDeath) {
  107. buffer_.AppendOrDie(kLargestMessage.data(), kLargestMessage.size());
  108. EXPECT_DEATH(
  109. buffer_.AppendOrDie(kSmallestMessage.data(), kSmallestMessage.size()),
  110. "Check failed");
  111. }
  112. } // namespace wifilogd
  113. } // namespace android