1 // Copyright (C) 2017 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #define DEBUG false
15 #include "Log.h"
16
17 #include "FdBuffer.h"
18 #include "PrivacyBuffer.h"
19
20 #include <android-base/file.h>
21 #include <android-base/test_utils.h>
22 #include <android/os/IncidentReportArgs.h>
23 #include <gmock/gmock.h>
24 #include <gtest/gtest.h>
25 #include <string.h>
26
27 using namespace android;
28 using namespace android::base;
29 using namespace android::os;
30 using namespace android::os::incidentd;
31 using ::testing::StrEq;
32 using ::testing::Test;
33 using ::testing::internal::CaptureStdout;
34 using ::testing::internal::GetCapturedStdout;
35
36 const uint8_t OTHER_TYPE = 1;
37 const uint8_t STRING_TYPE = 9;
38 const uint8_t MESSAGE_TYPE = 11;
39 const std::string STRING_FIELD_0 = "\x02\viamtestdata";
40 const std::string VARINT_FIELD_1 = "\x08\x96\x01"; // 150
41 const std::string STRING_FIELD_2 = "\x12\vandroidwins";
42 const std::string FIX64_FIELD_3 = "\x19\xff\xff\xff\xff\xff\xff\xff\xff"; // -1
43 const std::string FIX32_FIELD_4 = "\x25\xff\xff\xff\xff"; // -1
44 const std::string MESSAGE_FIELD_5 = "\x2a\x10" + VARINT_FIELD_1 + STRING_FIELD_2;
45 const std::string NEGATIVE_VARINT_FIELD_6 = "\x30\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01"; // -1
46
47 class PrivacyBufferTest : public Test {
48 public:
~PrivacyBufferTest()49 virtual ~PrivacyBufferTest() {
50 // Delete in reverse order of construction, to be consistent with
51 // regular allocation/deallocation.
52 while (!privacies.empty()) {
53 delete privacies.back();
54 privacies.pop_back();
55 }
56 }
57
SetUp()58 virtual void SetUp() override { ASSERT_NE(tf.fd, -1); }
59
writeToFdBuffer(std::string str)60 void writeToFdBuffer(std::string str) {
61 ASSERT_TRUE(WriteStringToFile(str, tf.path));
62 ASSERT_EQ(NO_ERROR, buffer.read(tf.fd, 10000));
63 ASSERT_EQ(str.size(), buffer.size());
64 }
65
assertBuffer(PrivacyBuffer & buf,std::string expected)66 void assertBuffer(PrivacyBuffer& buf, std::string expected) {
67 ASSERT_EQ(buf.size(), expected.size());
68 CaptureStdout();
69 ASSERT_EQ(buf.flush(STDOUT_FILENO), NO_ERROR);
70 ASSERT_THAT(GetCapturedStdout(), StrEq(expected));
71 }
72
assertStrip(uint8_t dest,std::string expected,Privacy * policy)73 void assertStrip(uint8_t dest, std::string expected, Privacy* policy) {
74 PrivacySpec spec = PrivacySpec::new_spec(dest);
75 EncodedBuffer::iterator bufData = buffer.data();
76 PrivacyBuffer privacyBuf(policy, bufData);
77 ASSERT_EQ(privacyBuf.strip(spec), NO_ERROR);
78 assertBuffer(privacyBuf, expected);
79 }
80
assertStripByFields(uint8_t dest,std::string expected,int size,Privacy * privacy,...)81 void assertStripByFields(uint8_t dest, std::string expected, int size, Privacy* privacy, ...) {
82 Privacy* list[size + 1];
83 list[0] = privacy;
84 va_list args;
85 va_start(args, privacy);
86 for (int i = 1; i < size; i++) {
87 Privacy* p = va_arg(args, Privacy*);
88 list[i] = p;
89 }
90 va_end(args);
91 list[size] = NULL;
92 assertStrip(dest, expected, create_message_privacy(300, list));
93 }
94
create_privacy(uint32_t field_id,uint8_t type,uint8_t dest)95 Privacy* create_privacy(uint32_t field_id, uint8_t type, uint8_t dest) {
96 Privacy* p = new_uninit_privacy();
97 p->field_id = field_id;
98 p->type = type;
99 p->children = NULL;
100 p->dest = dest;
101 p->patterns = NULL;
102 return p;
103 }
104
create_message_privacy(uint32_t field_id,Privacy ** children)105 Privacy* create_message_privacy(uint32_t field_id, Privacy** children) {
106 Privacy* p = new_uninit_privacy();
107 p->field_id = field_id;
108 p->type = MESSAGE_TYPE;
109 p->children = children;
110 p->dest = DEST_DEFAULT_VALUE;
111 p->patterns = NULL;
112 return p;
113 }
114
115 FdBuffer buffer;
116
117 private:
118 TemporaryFile tf;
119 // Littering this code with unique_ptr (or similar) is ugly, so we just
120 // mass-free everything after the test completes.
121 std::vector<Privacy*> privacies;
122
new_uninit_privacy()123 Privacy* new_uninit_privacy() {
124 Privacy* p = new Privacy;
125 privacies.push_back(p);
126 return p;
127 }
128 };
129
TEST_F(PrivacyBufferTest,NullPolicy)130 TEST_F(PrivacyBufferTest, NullPolicy) {
131 writeToFdBuffer(STRING_FIELD_0);
132 assertStrip(DEST_EXPLICIT, STRING_FIELD_0, NULL);
133 }
134
TEST_F(PrivacyBufferTest,StripUnsetField)135 TEST_F(PrivacyBufferTest, StripUnsetField) {
136 writeToFdBuffer(STRING_FIELD_0);
137 assertStripByFields(DEST_AUTOMATIC, "", 1, create_privacy(0, STRING_TYPE, DEST_UNSET));
138 }
139
TEST_F(PrivacyBufferTest,StripVarintField)140 TEST_F(PrivacyBufferTest, StripVarintField) {
141 writeToFdBuffer(VARINT_FIELD_1);
142 assertStripByFields(DEST_EXPLICIT, "", 1, create_privacy(1, OTHER_TYPE, DEST_LOCAL));
143 }
144
TEST_F(PrivacyBufferTest,StripLengthDelimitedField_String)145 TEST_F(PrivacyBufferTest, StripLengthDelimitedField_String) {
146 writeToFdBuffer(STRING_FIELD_2);
147 assertStripByFields(DEST_EXPLICIT, "", 1, create_privacy(2, STRING_TYPE, DEST_LOCAL));
148 }
149
TEST_F(PrivacyBufferTest,StripFixed64Field)150 TEST_F(PrivacyBufferTest, StripFixed64Field) {
151 writeToFdBuffer(FIX64_FIELD_3);
152 assertStripByFields(DEST_EXPLICIT, "", 1, create_privacy(3, OTHER_TYPE, DEST_LOCAL));
153 }
154
TEST_F(PrivacyBufferTest,StripFixed32Field)155 TEST_F(PrivacyBufferTest, StripFixed32Field) {
156 writeToFdBuffer(FIX32_FIELD_4);
157 assertStripByFields(DEST_EXPLICIT, "", 1, create_privacy(4, OTHER_TYPE, DEST_LOCAL));
158 }
159
TEST_F(PrivacyBufferTest,StripLengthDelimitedField_Message)160 TEST_F(PrivacyBufferTest, StripLengthDelimitedField_Message) {
161 writeToFdBuffer(MESSAGE_FIELD_5);
162 assertStripByFields(DEST_EXPLICIT, "", 1, create_privacy(5, MESSAGE_TYPE, DEST_LOCAL));
163 }
164
TEST_F(PrivacyBufferTest,StripNegativeVarint)165 TEST_F(PrivacyBufferTest, StripNegativeVarint) {
166 writeToFdBuffer(NEGATIVE_VARINT_FIELD_6);
167 assertStripByFields(DEST_EXPLICIT, "", 1, create_privacy(6, OTHER_TYPE, DEST_LOCAL));
168 }
169
TEST_F(PrivacyBufferTest,NoStripVarintField)170 TEST_F(PrivacyBufferTest, NoStripVarintField) {
171 writeToFdBuffer(VARINT_FIELD_1);
172 assertStripByFields(DEST_EXPLICIT, VARINT_FIELD_1, 1,
173 create_privacy(1, OTHER_TYPE, DEST_AUTOMATIC));
174 }
175
TEST_F(PrivacyBufferTest,NoStripLengthDelimitedField_String)176 TEST_F(PrivacyBufferTest, NoStripLengthDelimitedField_String) {
177 writeToFdBuffer(STRING_FIELD_2);
178 assertStripByFields(DEST_EXPLICIT, STRING_FIELD_2, 1,
179 create_privacy(2, STRING_TYPE, DEST_AUTOMATIC));
180 }
181
TEST_F(PrivacyBufferTest,NoStripFixed64Field)182 TEST_F(PrivacyBufferTest, NoStripFixed64Field) {
183 writeToFdBuffer(FIX64_FIELD_3);
184 assertStripByFields(DEST_EXPLICIT, FIX64_FIELD_3, 1,
185 create_privacy(3, OTHER_TYPE, DEST_AUTOMATIC));
186 }
187
TEST_F(PrivacyBufferTest,NoStripFixed32Field)188 TEST_F(PrivacyBufferTest, NoStripFixed32Field) {
189 writeToFdBuffer(FIX32_FIELD_4);
190 assertStripByFields(DEST_EXPLICIT, FIX32_FIELD_4, 1,
191 create_privacy(4, OTHER_TYPE, DEST_AUTOMATIC));
192 }
193
TEST_F(PrivacyBufferTest,NoStripLengthDelimitedField_Message)194 TEST_F(PrivacyBufferTest, NoStripLengthDelimitedField_Message) {
195 writeToFdBuffer(MESSAGE_FIELD_5);
196 assertStripByFields(DEST_EXPLICIT, MESSAGE_FIELD_5, 1,
197 create_privacy(5, MESSAGE_TYPE, DEST_AUTOMATIC));
198 }
199
TEST_F(PrivacyBufferTest,NoStripNegativeVarintField)200 TEST_F(PrivacyBufferTest, NoStripNegativeVarintField) {
201 writeToFdBuffer(NEGATIVE_VARINT_FIELD_6);
202 assertStripByFields(DEST_EXPLICIT, NEGATIVE_VARINT_FIELD_6, 1,
203 create_privacy(6, OTHER_TYPE, DEST_AUTOMATIC));
204 }
205
TEST_F(PrivacyBufferTest,StripVarintAndString)206 TEST_F(PrivacyBufferTest, StripVarintAndString) {
207 writeToFdBuffer(STRING_FIELD_0 + VARINT_FIELD_1 + STRING_FIELD_2 + FIX64_FIELD_3 +
208 FIX32_FIELD_4);
209 std::string expected = STRING_FIELD_0 + FIX64_FIELD_3 + FIX32_FIELD_4;
210 assertStripByFields(DEST_EXPLICIT, expected, 2, create_privacy(1, OTHER_TYPE, DEST_LOCAL),
211 create_privacy(2, STRING_TYPE, DEST_LOCAL));
212 }
213
TEST_F(PrivacyBufferTest,StripVarintAndFixed64)214 TEST_F(PrivacyBufferTest, StripVarintAndFixed64) {
215 writeToFdBuffer(STRING_FIELD_0 + VARINT_FIELD_1 + STRING_FIELD_2 + FIX64_FIELD_3 +
216 FIX32_FIELD_4);
217 std::string expected = STRING_FIELD_0 + STRING_FIELD_2 + FIX32_FIELD_4;
218 assertStripByFields(DEST_EXPLICIT, expected, 2, create_privacy(1, OTHER_TYPE, DEST_LOCAL),
219 create_privacy(3, OTHER_TYPE, DEST_LOCAL));
220 }
221
TEST_F(PrivacyBufferTest,StripVarintInNestedMessage)222 TEST_F(PrivacyBufferTest, StripVarintInNestedMessage) {
223 writeToFdBuffer(STRING_FIELD_0 + MESSAGE_FIELD_5);
224 Privacy* list[] = {create_privacy(1, OTHER_TYPE, DEST_LOCAL), NULL};
225 std::string expected = STRING_FIELD_0 + "\x2a\xd" + STRING_FIELD_2;
226 assertStripByFields(DEST_EXPLICIT, expected, 1, create_message_privacy(5, list));
227 }
228
TEST_F(PrivacyBufferTest,StripFix64AndVarintInNestedMessage)229 TEST_F(PrivacyBufferTest, StripFix64AndVarintInNestedMessage) {
230 writeToFdBuffer(STRING_FIELD_0 + FIX64_FIELD_3 + MESSAGE_FIELD_5);
231 Privacy* list[] = {create_privacy(1, OTHER_TYPE, DEST_LOCAL), NULL};
232 std::string expected = STRING_FIELD_0 + "\x2a\xd" + STRING_FIELD_2;
233 assertStripByFields(DEST_EXPLICIT, expected, 2, create_privacy(3, OTHER_TYPE, DEST_LOCAL),
234 create_message_privacy(5, list));
235 }
236
TEST_F(PrivacyBufferTest,ClearAndStrip)237 TEST_F(PrivacyBufferTest, ClearAndStrip) {
238 string data = STRING_FIELD_0 + VARINT_FIELD_1;
239 writeToFdBuffer(data);
240 Privacy* list[] = {create_privacy(1, OTHER_TYPE, DEST_LOCAL), NULL};
241 EncodedBuffer::iterator bufData = buffer.data();
242 PrivacyBuffer privacyBuf(create_message_privacy(300, list), bufData);
243 PrivacySpec spec1 = PrivacySpec::new_spec(DEST_EXPLICIT);
244 PrivacySpec spec2 = PrivacySpec::new_spec(DEST_LOCAL);
245
246 ASSERT_EQ(privacyBuf.strip(spec1), NO_ERROR);
247 assertBuffer(privacyBuf, STRING_FIELD_0);
248 ASSERT_EQ(privacyBuf.strip(spec2), NO_ERROR);
249 assertBuffer(privacyBuf, data);
250 }
251
TEST_F(PrivacyBufferTest,BadDataInFdBuffer)252 TEST_F(PrivacyBufferTest, BadDataInFdBuffer) {
253 writeToFdBuffer("iambaddata");
254 Privacy* list[] = {create_privacy(4, OTHER_TYPE, DEST_AUTOMATIC), NULL};
255 EncodedBuffer::iterator bufData = buffer.data();
256 PrivacyBuffer privacyBuf(create_message_privacy(300, list), bufData);
257 PrivacySpec spec;
258 ASSERT_EQ(privacyBuf.strip(spec), BAD_VALUE);
259 }
260
TEST_F(PrivacyBufferTest,BadDataInNestedMessage)261 TEST_F(PrivacyBufferTest, BadDataInNestedMessage) {
262 writeToFdBuffer(STRING_FIELD_0 + MESSAGE_FIELD_5 + "aoeoe");
263 Privacy* list[] = {create_privacy(1, OTHER_TYPE, DEST_LOCAL), NULL};
264 Privacy* field5[] = {create_message_privacy(5, list), NULL};
265 EncodedBuffer::iterator bufData = buffer.data();
266 PrivacyBuffer privacyBuf(create_message_privacy(300, field5), bufData);
267 PrivacySpec spec;
268 ASSERT_EQ(privacyBuf.strip(spec), BAD_VALUE);
269 }
270
TEST_F(PrivacyBufferTest,SelfRecursionMessage)271 TEST_F(PrivacyBufferTest, SelfRecursionMessage) {
272 string input = "\x2a\"" + VARINT_FIELD_1 + STRING_FIELD_2 + MESSAGE_FIELD_5;
273 writeToFdBuffer(input);
274 Privacy* field5 = create_message_privacy(5, NULL);
275 Privacy* list[] = {create_privacy(1, OTHER_TYPE, DEST_LOCAL), field5, NULL};
276 field5->children = list;
277 std::string expected = "\x2a\x1c" + STRING_FIELD_2 + "\x2a\xd" + STRING_FIELD_2;
278 assertStrip(DEST_EXPLICIT, expected, field5);
279 }
280
TEST_F(PrivacyBufferTest,AutoMessage)281 TEST_F(PrivacyBufferTest, AutoMessage) {
282 writeToFdBuffer(STRING_FIELD_2 + MESSAGE_FIELD_5);
283 Privacy* list[] = {create_privacy(1, OTHER_TYPE, DEST_LOCAL), NULL};
284 Privacy* autoMsg = create_privacy(5, MESSAGE_TYPE, DEST_AUTOMATIC);
285 autoMsg->children = list;
286 std::string expected = "\x2a\xd" + STRING_FIELD_2;
287 assertStripByFields(DEST_AUTOMATIC, expected, 1, autoMsg);
288 }
289