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 
17 #include <array>
18 #include <cstring>
19 #include <string>
20 
21 #include "gtest/gtest.h"
22 
23 #include "wifilogd/byte_buffer.h"
24 
25 namespace android {
26 namespace wifilogd {
27 namespace {
28 
29 constexpr size_t kBufferSizeBytes = 1024;
30 constexpr std::array<uint8_t, 1> kSmallestMessage{};
31 constexpr std::array<uint8_t, kBufferSizeBytes> kLargestMessage{};
32 
33 class ByteBufferTest : public ::testing::Test {
34  public:
ByteBufferTest()35   ByteBufferTest() {}
36 
37  protected:
38   ByteBuffer<kBufferSizeBytes> buffer_;
39 };
40 
41 }  // namespace
42 
TEST_F(ByteBufferTest,AppendMinimalOnEmptyBufferSucceeds)43 TEST_F(ByteBufferTest, AppendMinimalOnEmptyBufferSucceeds) {
44   buffer_.AppendOrDie(kSmallestMessage.data(), kSmallestMessage.size());
45 }
46 
TEST_F(ByteBufferTest,AppendMaximalOnEmptyBufferSucceeds)47 TEST_F(ByteBufferTest, AppendMaximalOnEmptyBufferSucceeds) {
48   buffer_.AppendOrDie(kLargestMessage.data(), kLargestMessage.size());
49 }
50 
TEST_F(ByteBufferTest,AppendStoresOurData)51 TEST_F(ByteBufferTest, AppendStoresOurData) {
52   const std::string message1{"hello"};
53   const std::string message2{"world"};
54   buffer_.AppendOrDie(message1.data(), message1.size());
55   buffer_.AppendOrDie(message2.data(), message2.size());
56 
57   const std::string expected{"helloworld"};
58   EXPECT_EQ(0, std::memcmp(buffer_.data(), expected.data(), expected.size()));
59 }
60 
TEST_F(ByteBufferTest,AssigningWorks)61 TEST_F(ByteBufferTest, AssigningWorks) {
62   const std::string message1{"hello"};
63   buffer_.AppendOrDie(message1.data(), message1.size());
64 
65   ByteBuffer<kBufferSizeBytes> copy;
66   ASSERT_NE(buffer_.size(), copy.size());
67 
68   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,ChainingWorks)73 TEST_F(ByteBufferTest, ChainingWorks) {
74   const std::string message1{"hello"};
75   const std::string message2{"world"};
76   buffer_.AppendOrDie(message1.data(), message1.size())
77       .AppendOrDie(message2.data(), message2.size());
78 
79   const std::string expected{"helloworld"};
80   EXPECT_EQ(0, std::memcmp(buffer_.data(), expected.data(), expected.size()));
81 }
82 
TEST_F(ByteBufferTest,CopyingWorks)83 TEST_F(ByteBufferTest, CopyingWorks) {
84   const std::string message1{"hello"};
85   buffer_.AppendOrDie(message1.data(), message1.size());
86 
87   const ByteBuffer<kBufferSizeBytes> copy{buffer_};
88   ASSERT_EQ(buffer_.size(), copy.size());
89   EXPECT_EQ(0, std::memcmp(copy.data(), buffer_.data(), buffer_.size()));
90 }
91 
TEST_F(ByteBufferTest,DataDoesNotReturnNullOnFreshBuffer)92 TEST_F(ByteBufferTest, DataDoesNotReturnNullOnFreshBuffer) {
93   EXPECT_NE(nullptr, buffer_.data());
94 }
95 
TEST_F(ByteBufferTest,DataDoesNotReturnNullAfterLargeWrite)96 TEST_F(ByteBufferTest, DataDoesNotReturnNullAfterLargeWrite) {
97   buffer_.AppendOrDie(kLargestMessage.data(), kLargestMessage.size());
98   EXPECT_NE(nullptr, buffer_.data());
99 }
100 
TEST_F(ByteBufferTest,SizeReturnsZeroOnFreshBuffer)101 TEST_F(ByteBufferTest, SizeReturnsZeroOnFreshBuffer) {
102   EXPECT_EQ(0U, buffer_.size());
103 }
104 
TEST_F(ByteBufferTest,SizeIsCorrectAfterSmallWrite)105 TEST_F(ByteBufferTest, SizeIsCorrectAfterSmallWrite) {
106   buffer_.AppendOrDie(kSmallestMessage.data(), kSmallestMessage.size());
107   EXPECT_EQ(kSmallestMessage.size(), buffer_.size());
108 }
109 
TEST_F(ByteBufferTest,SizeIsCorrectAfterLargeWrite)110 TEST_F(ByteBufferTest, SizeIsCorrectAfterLargeWrite) {
111   buffer_.AppendOrDie(kLargestMessage.data(), kLargestMessage.size());
112   EXPECT_EQ(kLargestMessage.size(), buffer_.size());
113 }
114 
TEST_F(ByteBufferTest,SizeIsCorrectAfterMultipleWrites)115 TEST_F(ByteBufferTest, SizeIsCorrectAfterMultipleWrites) {
116   buffer_.AppendOrDie(kSmallestMessage.data(), kSmallestMessage.size())
117       .AppendOrDie(kSmallestMessage.data(), kSmallestMessage.size())
118       .AppendOrDie(kSmallestMessage.data(), kSmallestMessage.size());
119   EXPECT_EQ(3 * kSmallestMessage.size(), buffer_.size());
120 }
121 
122 // Per
123 // github.com/google/googletest/blob/master/googletest/docs/AdvancedGuide.md#death-tests,
124 // death tests should be specially named.
125 using ByteBufferDeathTest = ByteBufferTest;
126 
TEST_F(ByteBufferDeathTest,AppendLargerThanBufferCausesDeath)127 TEST_F(ByteBufferDeathTest, AppendLargerThanBufferCausesDeath) {
128   constexpr std::array<uint8_t, kBufferSizeBytes + 1> oversized_message{};
129   EXPECT_DEATH(
130       buffer_.AppendOrDie(oversized_message.data(), oversized_message.size()),
131       "Check failed");
132 }
133 
TEST_F(ByteBufferDeathTest,AppendLargerThanFreeSpaceCausesDeath)134 TEST_F(ByteBufferDeathTest, AppendLargerThanFreeSpaceCausesDeath) {
135   buffer_.AppendOrDie(kLargestMessage.data(), kLargestMessage.size());
136   EXPECT_DEATH(
137       buffer_.AppendOrDie(kSmallestMessage.data(), kSmallestMessage.size()),
138       "Check failed");
139 }
140 
141 }  // namespace wifilogd
142 }  // namespace android
143