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 
19 #include "gtest/gtest.h"
20 
21 #include "wifilogd/byte_buffer.h"
22 #include "wifilogd/local_utils.h"
23 #include "wifilogd/memory_reader.h"
24 
25 namespace android {
26 namespace wifilogd {
27 
28 using local_utils::GetMaxVal;
29 
TEST(MemoryReaderTest,BoolConversionNonEmptyBufferYieldsTrue)30 TEST(MemoryReaderTest, BoolConversionNonEmptyBufferYieldsTrue) {
31   constexpr std::array<uint8_t, 1> buffer{};
32   MemoryReader memory_reader(buffer.data(), buffer.size());
33   EXPECT_TRUE(memory_reader);
34 }
35 
TEST(MemoryReaderTest,BoolConversionEmptyBufferYieldsFalse)36 TEST(MemoryReaderTest, BoolConversionEmptyBufferYieldsFalse) {
37   constexpr std::array<uint8_t, 0> buffer{};
38   MemoryReader memory_reader(buffer.data(), buffer.size());
39   EXPECT_FALSE(memory_reader);
40 }
41 
TEST(MemoryReaderTest,BoolConversionNullBufferYieldsFalse)42 TEST(MemoryReaderTest, BoolConversionNullBufferYieldsFalse) {
43   MemoryReader memory_reader(nullptr, 10);
44   EXPECT_FALSE(memory_reader);
45 }
46 
TEST(MemoryReaderTest,CopyAssignmentWorks)47 TEST(MemoryReaderTest, CopyAssignmentWorks) {
48   constexpr std::array<uint8_t, 3> buffer{{1, 2, 3}};
49 
50   MemoryReader reader1{buffer.data(), buffer.size()};
51   EXPECT_EQ(buffer[0], *reader1.GetBytesOrDie(sizeof(uint8_t)));
52 
53   // A copy of reader1 should reflect the byte already read.
54   MemoryReader reader2 = reader1;
55   EXPECT_EQ(buffer[1], *reader2.GetBytesOrDie(sizeof(uint8_t)));
56 
57   // reader1 should _not_ reflect the additional read on reader2.
58   EXPECT_GT(reader1.size(), reader2.size());
59 }
60 
TEST(MemoryReaderTest,CopyConstructionWorks)61 TEST(MemoryReaderTest, CopyConstructionWorks) {
62   constexpr std::array<uint8_t, 3> buffer{{1, 2, 3}};
63 
64   MemoryReader reader1{buffer.data(), buffer.size()};
65   EXPECT_EQ(buffer[0], *reader1.GetBytesOrDie(sizeof(uint8_t)));
66 
67   // A copy of reader1 should reflect the byte already read.
68   MemoryReader reader2(reader1);
69   EXPECT_EQ(buffer[1], *reader2.GetBytesOrDie(sizeof(uint8_t)));
70 
71   // reader1 should _not_ reflect the additional read on reader2.
72   EXPECT_GT(reader1.size(), reader2.size());
73 }
74 
TEST(MemoryReaderTest,CopyOutOrDieCopiesData)75 TEST(MemoryReaderTest, CopyOutOrDieCopiesData) {
76   constexpr struct Message {
77     int a;
78     char b;
79   } original_message{5, 'c'};
80   const auto& duplicate_message =
81       MemoryReader(&original_message, sizeof(original_message))
82           .CopyOutOrDie<Message>();
83   EXPECT_EQ(original_message.a, duplicate_message.a);
84   EXPECT_EQ(original_message.b, duplicate_message.b);
85 }
86 
TEST(MemoryReaderTest,CopyOutOrDieWorksForMultipleObjects)87 TEST(MemoryReaderTest, CopyOutOrDieWorksForMultipleObjects) {
88   constexpr struct M1 {
89     int a;
90     char b;
91   } first_message{1, 'a'};
92   constexpr struct M2 {
93     uint64_t a;
94     uint64_t b;
95   } second_message{0, GetMaxVal(second_message.b)};
96   const auto& buf = ByteBuffer<sizeof(first_message) + sizeof(second_message)>()
97                         .AppendOrDie(&first_message, sizeof(first_message))
98                         .AppendOrDie(&second_message, sizeof(second_message));
99 
100   MemoryReader memory_reader(buf.data(), buf.size());
101   const auto& copy_of_first_message = memory_reader.CopyOutOrDie<M1>();
102   const auto& copy_of_second_message = memory_reader.CopyOutOrDie<M2>();
103   EXPECT_EQ(first_message.a, copy_of_first_message.a);
104   EXPECT_EQ(first_message.b, copy_of_first_message.b);
105   EXPECT_EQ(second_message.a, copy_of_second_message.a);
106   EXPECT_EQ(second_message.b, copy_of_second_message.b);
107 }
108 
TEST(MemoryReaderTest,GetBytesOrDieSucceedsOnSmallRead)109 TEST(MemoryReaderTest, GetBytesOrDieSucceedsOnSmallRead) {
110   constexpr std::array<uint8_t, 1024> buffer{};
111   MemoryReader memory_reader(buffer.data(), buffer.size());
112   EXPECT_EQ(buffer.data(), memory_reader.GetBytesOrDie(1));
113 }
114 
TEST(MemoryReaderTest,GetBytesOrDieSucceedsOnFullRead)115 TEST(MemoryReaderTest, GetBytesOrDieSucceedsOnFullRead) {
116   constexpr std::array<uint8_t, 1024> buffer{};
117   MemoryReader memory_reader(buffer.data(), buffer.size());
118   EXPECT_EQ(buffer.data(), memory_reader.GetBytesOrDie(buffer.size()));
119 }
120 
TEST(MemoryReaderTest,GetBytesOrDieCanConsumeFullBufferInParts)121 TEST(MemoryReaderTest, GetBytesOrDieCanConsumeFullBufferInParts) {
122   constexpr std::array<uint8_t, 1024> buffer{};
123   static_assert(buffer.size() % 2 == 0, "Test will not consume whole buffer");
124   MemoryReader memory_reader(buffer.data(), buffer.size());
125   EXPECT_EQ(buffer.data(), memory_reader.GetBytesOrDie(buffer.size() / 2));
126   EXPECT_EQ(buffer.data() + buffer.size() / 2,
127             memory_reader.GetBytesOrDie(buffer.size() / 2));
128 }
129 
TEST(MemoryReaderTest,SizeIsCorrectForNonEmptyBuffer)130 TEST(MemoryReaderTest, SizeIsCorrectForNonEmptyBuffer) {
131   constexpr std::array<uint8_t, 1> buffer{};
132   MemoryReader memory_reader(buffer.data(), buffer.size());
133   EXPECT_EQ(buffer.size(), memory_reader.size());
134 }
135 
TEST(MemoryReaderTest,SizeIsCorrectForEmptyBuffer)136 TEST(MemoryReaderTest, SizeIsCorrectForEmptyBuffer) {
137   constexpr std::array<uint8_t, 0> buffer{};
138   MemoryReader memory_reader(buffer.data(), buffer.size());
139   EXPECT_EQ(buffer.size(), memory_reader.size());
140 }
141 
TEST(MemoryReaderTest,SizeIsCorrectAfterSmallRead)142 TEST(MemoryReaderTest, SizeIsCorrectAfterSmallRead) {
143   constexpr std::array<uint8_t, 1024> buffer{};
144   MemoryReader memory_reader(buffer.data(), buffer.size());
145   memory_reader.GetBytesOrDie(1);
146   EXPECT_EQ(buffer.size() - 1, memory_reader.size());
147 }
148 
TEST(MemoryReaderTest,SizeIsCorrectAfterFullRead)149 TEST(MemoryReaderTest, SizeIsCorrectAfterFullRead) {
150   constexpr std::array<uint8_t, 1024> buffer{};
151   MemoryReader memory_reader(buffer.data(), buffer.size());
152   memory_reader.GetBytesOrDie(buffer.size());
153   EXPECT_EQ(0U, memory_reader.size());
154 }
155 
TEST(MemoryReaderTest,SizeIsZeroForNullBuffer)156 TEST(MemoryReaderTest, SizeIsZeroForNullBuffer) {
157   MemoryReader memory_reader(nullptr, 0);
158   EXPECT_EQ(0U, memory_reader.size());
159 }
160 
161 // Per
162 // github.com/google/googletest/blob/master/googletest/docs/AdvancedGuide.md#death-tests,
163 // death tests should be specially named.
164 
TEST(MemoryReaderDeathTest,CopyOutOrDieWithNullBufferCausesDeath)165 TEST(MemoryReaderDeathTest, CopyOutOrDieWithNullBufferCausesDeath) {
166   constexpr struct Message {
167     int a;
168     char b;
169   } original{5, 'c'};
170   EXPECT_DEATH(MemoryReader(nullptr, sizeof(original)).CopyOutOrDie<Message>(),
171                "Check failed");
172 }
173 
TEST(MemoryReaderDeathTest,CopyOutOrDieWithShortBufferCausesDeath)174 TEST(MemoryReaderDeathTest, CopyOutOrDieWithShortBufferCausesDeath) {
175   constexpr struct Message {
176     int a;
177     char b;
178   } original{5, 'c'};
179   EXPECT_DEATH(
180       MemoryReader(&original, sizeof(original) - 1).CopyOutOrDie<Message>(),
181       "Check failed");
182 }
183 
TEST(MemoryReaderDeathTest,CopyOutOrDieOverrunWithMultipleReadsCausesDeath)184 TEST(MemoryReaderDeathTest, CopyOutOrDieOverrunWithMultipleReadsCausesDeath) {
185   constexpr struct Message {
186     int a;
187     char b;
188   } message{5, 'c'};
189   MemoryReader memory_reader(&message, sizeof(message));
190   memory_reader.CopyOutOrDie<Message>();
191   EXPECT_DEATH(memory_reader.CopyOutOrDie<Message>(), "Check failed");
192 }
193 
TEST(MemoryReaderTest,GetBytesOrDieWithNullBufferCausesDeath)194 TEST(MemoryReaderTest, GetBytesOrDieWithNullBufferCausesDeath) {
195   constexpr std::array<uint8_t, 1024> buffer{};
196   MemoryReader memory_reader(nullptr, buffer.size());
197   EXPECT_DEATH(memory_reader.GetBytesOrDie(buffer.size()), "Check failed");
198 }
199 
TEST(MemoryReaderTest,GetBytesOrDieWithShortBufferCausesDeath)200 TEST(MemoryReaderTest, GetBytesOrDieWithShortBufferCausesDeath) {
201   constexpr std::array<uint8_t, 1024> buffer{};
202   MemoryReader memory_reader(buffer.data(), buffer.size());
203   EXPECT_DEATH(memory_reader.GetBytesOrDie(buffer.size() + 1), "Check failed");
204 }
205 
TEST(MemoryReaderTest,GetBytesOrDieOverrunWithMultipleReadsCausesDeath)206 TEST(MemoryReaderTest, GetBytesOrDieOverrunWithMultipleReadsCausesDeath) {
207   constexpr std::array<uint8_t, 1024> buffer{};
208   MemoryReader memory_reader(buffer.data(), buffer.size());
209   EXPECT_EQ(buffer.data(), memory_reader.GetBytesOrDie(buffer.size()));
210   EXPECT_DEATH(memory_reader.GetBytesOrDie(1), "Check failed");
211 }
212 
213 }  // namespace wifilogd
214 }  // namespace android
215