1 /*
2 * Copyright (C) 2017 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 requied 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
18 /*
19 * This socket tagging test is to ensure that the
20 * netfilter/xt_qtaguid kernel module somewhat behaves as expected
21 * with respect to tagging sockets.
22 */
23 #define LOG_TAG "SocketTagUsrSpaceTest"
24 #include "SocketTagUserSpace.h"
25 #include <arpa/inet.h>
26 #include <assert.h>
27 #include <cutils/qtaguid.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <inttypes.h>
31 #include <netinet/in.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <sys/socket.h>
35 #include <sys/types.h>
36 #include <sys/utsname.h>
37 #include <string>
38
39 #include <fstream>
40
41 #include <android-base/stringprintf.h>
42 #include <gtest/gtest.h>
43 #include <qtaguid/qtaguid.h>
44 #include <utils/Log.h>
45
46 static const int kMaxCounterSet = 2;
47
48 namespace android {
49
50 #define SKIP_IF_QTAGUID_NOT_SUPPORTED() \
51 do { \
52 bool hasQtaguidSupport; \
53 EXPECT_EQ(checkKernelSupport(&hasQtaguidSupport), 0) \
54 << "kernel support check failed"; \
55 if (!hasQtaguidSupport) { \
56 GTEST_LOG_(INFO) << "skipped since kernel version is not compatible.\n"; \
57 return; \
58 } \
59 } while (0);
60 /* A helper program to generate some traffic between two socket. */
server_download(SockInfo sock_server,SockInfo sock_client)61 int server_download(SockInfo sock_server, SockInfo sock_client) {
62 struct sockaddr_in server, client;
63 server.sin_addr.s_addr = inet_addr("127.0.0.1");
64 server.sin_family = AF_INET;
65 server.sin_port = htons(8765);
66 if (bind(sock_server.fd, (struct sockaddr *)&server, sizeof(server)) < 0) {
67 std::cerr << "bind failed" << std::endl;
68 return -1;
69 }
70 listen(sock_server.fd, 3);
71 int sock_addr_length;
72 sock_addr_length = sizeof(struct sockaddr_in);
73 if (connect(sock_client.fd, (struct sockaddr *)&server, sizeof(server)), 0) {
74 return -1;
75 }
76 int new_socket;
77 new_socket = accept(sock_server.fd, (struct sockaddr *)&client,
78 reinterpret_cast<socklen_t *>(&sock_addr_length));
79 if (new_socket < 0) {
80 return -1;
81 }
82 int packet_Count = 1024;
83 char byte_buffer[1024];
84 snprintf(byte_buffer, sizeof(byte_buffer), "%d", packet_Count);
85 send(sock_client.fd, "start", 5, 0);
86 if (recv(new_socket, byte_buffer, strlen(byte_buffer), 0) < 0) {
87 close(new_socket);
88 return -1;
89 }
90 memset(byte_buffer, 'x', 1023);
91 byte_buffer[1023] = '\0';
92 if (send(new_socket, byte_buffer, strlen(byte_buffer), 0) < 0) {
93 close(new_socket);
94 return -1;
95 }
96 EXPECT_GE(recv(sock_client.fd, byte_buffer, 1024, 0), 0);
97 close(new_socket);
98 return 0;
99 }
100
checkKernelSupport(bool * qtaguidSupport)101 int checkKernelSupport(bool *qtaguidSupport) {
102 int ret;
103 struct utsname buf;
104 int kernel_version_major;
105 int kernel_version_minor;
106
107 ret = uname(&buf);
108 if (ret) {
109 ret = -errno;
110 std::cout << "Get system information failed: %s\n"
111 << strerror(errno) << std::endl;
112 return ret;
113 }
114 char dummy;
115 ret = sscanf(buf.release, "%d.%d%c", &kernel_version_major,
116 &kernel_version_minor, &dummy);
117 // For device running kernel 4.9 or above and running Android P, it should use
118 // the eBPF cgroup filter to monitoring networking stats instead. So it may
119 // not have xt_qtaguid module on device. But for devices that still have
120 // xt_qtaguid, this test is still useful to make sure it behaves properly.
121 // b/30950746
122 if (ret >= 2 && ((kernel_version_major == 4 && kernel_version_minor >= 9) ||
123 (kernel_version_major > 4))) {
124 *qtaguidSupport = false;
125 } else {
126 *qtaguidSupport = true;
127 }
128 return 0;
129 }
130
131 /* socket setup, initial the socket and try to validate the socket. */
setup(int tag)132 int SockInfo::setup(int tag) {
133 fd = socket(AF_INET, SOCK_STREAM, 0);
134 if (fd < 0) {
135 std::cout << "socket creation failed: %s" << strerror(errno) << std::endl;
136 return -1;
137 }
138 if (legacy_tagSocket(fd, tag, getuid()) < 0) {
139 std::cout << "socket setup: failed to tag" << std::endl;
140 close(fd);
141 return -1;
142 }
143
144 if (legacy_untagSocket(fd) < 0) {
145 std::cout << "socket setup: Unexpected results" << std::endl;
146 close(fd);
147 return -1;
148 }
149 return 0;
150 }
151
152 /* Check if the socket is properly tagged by read through the proc file.*/
checkTag(uint64_t acct_tag,uid_t uid)153 bool SockInfo::checkTag(uint64_t acct_tag, uid_t uid) {
154
155 std::ifstream fctrl("/proc/net/xt_qtaguid/ctrl", std::fstream::in);
156 if (!fctrl.is_open()) {
157 std::cout << "qtaguid ctrl open failed!" << std::endl;
158 }
159
160 uint64_t full_tag = (acct_tag << 32) | uid;
161 std::string buff =
162 android::base::StringPrintf(" tag=0x%" PRIx64 " (uid=%u)", full_tag, uid);
163
164 std::string ctrl_data;
165 std::size_t pos = std::string::npos;
166 while (std::getline(fctrl, ctrl_data)) {
167 pos = ctrl_data.find(buff);
168 if (pos != std::string::npos) break;
169 }
170 return pos != std::string::npos;
171 }
172
173 /*
174 * Check if the socket traffic statistics is properly recorded by reading the
175 * corresponding proc file.
176 */
checkStats(uint64_t acct_tag,uid_t uid,int counterSet,uint32_t * stats_result)177 bool SockInfo::checkStats(uint64_t acct_tag, uid_t uid, int counterSet,
178 uint32_t *stats_result) {
179 uint64_t kTag = (uint64_t)acct_tag << 32;
180
181 std::ifstream fstats("/proc/net/xt_qtaguid/stats", std::fstream::in);
182 if (!fstats.is_open()) {
183 std::cout << "qtaguid ctrl open failed!" << std::endl;
184 }
185 std::string buff =
186 android::base::StringPrintf("0x%" PRIx64 " %u %d", kTag, uid, counterSet);
187 std::string stats_data;
188 std::size_t pos = std::string::npos;
189 while (std::getline(fstats, stats_data)) {
190 pos = stats_data.find(buff);
191 if (pos != std::string::npos) {
192 std::string match_data = stats_data.substr(pos);
193 sscanf(match_data.c_str(), "0x%" PRIx64 " %u %d %d %d", &kTag, &uid,
194 &counterSet, stats_result, stats_result + 1);
195 return pos != std::string::npos;
196 }
197 }
198 return pos != std::string::npos;
199 }
200
201 class SocketTagUsrSpaceTest : public ::testing::Test {
202 protected:
203 uint32_t stats_result_[2];
204 SockInfo sock_0;
205 SockInfo sock_1;
206 uid_t fake_uid;
207 uid_t fake_uid2;
208 uid_t inet_uid;
209 uid_t my_uid;
210 pid_t my_pid;
211 int valid_tag1;
212 int valid_tag2;
213 uint64_t max_uint_tag;
214
SetUp()215 virtual void SetUp() {
216 SKIP_IF_QTAGUID_NOT_SUPPORTED();
217
218 my_uid = getuid();
219 my_pid = getpid();
220 srand48(my_pid * my_uid);
221 // Adjust fake UIDs and tags so that multiple instances can run
222 // in parallel.
223 fake_uid = rand() & 0x7FFFFFFF;
224 fake_uid2 = rand() & 0x7FFFFFFF;
225 inet_uid = 1024;
226 valid_tag1 = (my_pid << 12) | (rand());
227 valid_tag2 = (my_pid << 12) | (rand());
228 max_uint_tag = 0xffffffff00000000LLU;
229 max_uint_tag = 1LLU << 63 | (((uint64_t)my_pid << 48) ^ max_uint_tag);
230 // Check the node /dev/xt_qtaguid exist before start.
231 struct stat nodeStat;
232 EXPECT_GE(stat("/dev/xt_qtaguid", &nodeStat), 0)
233 << "fail to check /dev/xt_qtaguid";
234 // We want to clean up any previous faulty test runs.
235 EXPECT_GE(legacy_deleteTagData(0, fake_uid), 0)
236 << "Failed to delete fake_uid";
237 EXPECT_GE(legacy_deleteTagData(0, fake_uid2), 0)
238 << "Failed to delete fake_uid2";
239 EXPECT_GE(legacy_deleteTagData(0, my_uid), 0) << "Failed to delete my_uid";
240 EXPECT_GE(legacy_deleteTagData(0, inet_uid), 0)
241 << "Failed to delete inet_uid";
242 ASSERT_FALSE(sock_0.setup(valid_tag1)) << "socket0 setup failed";
243 ASSERT_FALSE(sock_1.setup(valid_tag1)) << "socket1 setup failed";
244 }
245
TearDown()246 virtual void TearDown() {
247 if (sock_0.fd >= 0) {
248 close(sock_0.fd);
249 }
250 if (sock_1.fd >= 0) {
251 close(sock_1.fd);
252 }
253 }
254 };
255
256 /* Tag to a invalid socket fd, should fail */
TEST_F(SocketTagUsrSpaceTest,invalidSockfdFail)257 TEST_F(SocketTagUsrSpaceTest, invalidSockfdFail) {
258 SKIP_IF_QTAGUID_NOT_SUPPORTED();
259
260 EXPECT_LT(legacy_tagSocket(-1, valid_tag1, my_uid), 0)
261 << "Invalid socketfd case 1, should fail.";
262 }
263
264 /* Check the stats of a invalid socket, should fail. */
TEST_F(SocketTagUsrSpaceTest,CheckStatsInvalidSocketFail)265 TEST_F(SocketTagUsrSpaceTest, CheckStatsInvalidSocketFail) {
266 SKIP_IF_QTAGUID_NOT_SUPPORTED();
267
268 memset(stats_result_, 0, 2);
269 EXPECT_FALSE(sock_0.checkStats(valid_tag1, fake_uid, 0, stats_result_))
270 << "No stats should be here";
271 }
272
273 /* Untag invalid socket fd, should fail */
TEST_F(SocketTagUsrSpaceTest,UntagInvalidSocketFail)274 TEST_F(SocketTagUsrSpaceTest, UntagInvalidSocketFail) {
275 SKIP_IF_QTAGUID_NOT_SUPPORTED();
276
277 EXPECT_LT(legacy_untagSocket(-1), 0) << "invalid socket fd, should fail";
278 EXPECT_LT(legacy_untagSocket(sock_0.fd), 0)
279 << "no tags on sock0, should fail";
280 }
281
282 /*
283 * Set the counter to a number larger then max counter avalaible
284 * should fail
285 */
TEST_F(SocketTagUsrSpaceTest,CounterSetNumExceedFail)286 TEST_F(SocketTagUsrSpaceTest, CounterSetNumExceedFail) {
287 SKIP_IF_QTAGUID_NOT_SUPPORTED();
288
289 int wrongCounterNum = kMaxCounterSet + 1;
290 EXPECT_LT(legacy_setCounterSet(wrongCounterNum, my_uid), 0)
291 << "Invalid counter set number, should fail.";
292 }
293
294 /* Tag without valid uid, should be tagged with my_uid */
TEST_F(SocketTagUsrSpaceTest,NoUidTag)295 TEST_F(SocketTagUsrSpaceTest, NoUidTag) {
296 SKIP_IF_QTAGUID_NOT_SUPPORTED();
297
298 EXPECT_GE(legacy_tagSocket(sock_0.fd, valid_tag1, 0), 0)
299 << "tag failed without uid";
300 EXPECT_TRUE(sock_0.checkTag(valid_tag1, my_uid)) << "Tag not found";
301 }
302
303 /*
304 * Tag without tag and uid number, should be tagged with tag 0 and
305 * my_uid
306 */
TEST_F(SocketTagUsrSpaceTest,NoTagNoUid)307 TEST_F(SocketTagUsrSpaceTest, NoTagNoUid) {
308 SKIP_IF_QTAGUID_NOT_SUPPORTED();
309
310 EXPECT_GE(legacy_tagSocket(sock_0.fd, 0, 0), 0)
311 << "no tag and uid infomation";
312 ASSERT_TRUE(sock_0.checkTag(0, my_uid)) << "Tag not found";
313 }
314
315 /* Untag from a tagged socket */
TEST_F(SocketTagUsrSpaceTest,ValidUntag)316 TEST_F(SocketTagUsrSpaceTest, ValidUntag) {
317 SKIP_IF_QTAGUID_NOT_SUPPORTED();
318
319 EXPECT_GE(legacy_tagSocket(sock_0.fd, valid_tag1, my_uid), 0);
320 EXPECT_TRUE(sock_0.checkTag(valid_tag1, my_uid)) << "Tag not found";
321 EXPECT_GE(legacy_untagSocket(sock_0.fd), 0);
322 EXPECT_FALSE(sock_0.checkTag(valid_tag1, my_uid)) << "Tag should be removed";
323 }
324
325 /* Tag a socket for the first time */
TEST_F(SocketTagUsrSpaceTest,ValidFirsttag)326 TEST_F(SocketTagUsrSpaceTest, ValidFirsttag) {
327 SKIP_IF_QTAGUID_NOT_SUPPORTED();
328
329 EXPECT_GE(legacy_tagSocket(sock_0.fd, valid_tag2, fake_uid), 0);
330 EXPECT_TRUE(sock_0.checkTag(valid_tag2, fake_uid)) << "Tag not found.";
331 }
332
333 /* ReTag a already tagged socket with the same tag and uid */
TEST_F(SocketTagUsrSpaceTest,ValidReTag)334 TEST_F(SocketTagUsrSpaceTest, ValidReTag) {
335 SKIP_IF_QTAGUID_NOT_SUPPORTED();
336
337 EXPECT_GE(legacy_tagSocket(sock_0.fd, valid_tag2, fake_uid), 0);
338 EXPECT_GE(legacy_tagSocket(sock_0.fd, valid_tag2, fake_uid), 0);
339 EXPECT_TRUE(sock_0.checkTag(valid_tag2, fake_uid)) << "Tag not found.";
340 }
341
342 /*
343 * Retag a already tagged socket with the same uid but different tag
344 * Should keep the second one and untag the original one
345 */
TEST_F(SocketTagUsrSpaceTest,ValidReTagWithAcctTagChange)346 TEST_F(SocketTagUsrSpaceTest, ValidReTagWithAcctTagChange) {
347 SKIP_IF_QTAGUID_NOT_SUPPORTED();
348
349 EXPECT_GE(legacy_tagSocket(sock_0.fd, valid_tag2, fake_uid), 0);
350 EXPECT_GE(legacy_tagSocket(sock_0.fd, valid_tag1, fake_uid), 0);
351 EXPECT_TRUE(sock_0.checkTag(valid_tag1, fake_uid)) << "Tag not found.";
352 EXPECT_FALSE(sock_0.checkTag(valid_tag2, fake_uid))
353 << "Tag should not be here";
354 }
355
356 /*
357 * Retag a already tagged socket with different uid and different tag.
358 * Should keep both
359 */
TEST_F(SocketTagUsrSpaceTest,ReTagWithUidChange)360 TEST_F(SocketTagUsrSpaceTest, ReTagWithUidChange) {
361 SKIP_IF_QTAGUID_NOT_SUPPORTED();
362
363 EXPECT_GE(legacy_tagSocket(sock_0.fd, valid_tag2, fake_uid), 0);
364 EXPECT_GE(legacy_tagSocket(sock_0.fd, valid_tag1, fake_uid2), 0);
365 }
366
367 /*
368 * Retag a already tagged socket with different uid but the same tag.
369 * The original one should be replaced by the new one.
370 */
TEST_F(SocketTagUsrSpaceTest,ReTagWithUidChange2)371 TEST_F(SocketTagUsrSpaceTest, ReTagWithUidChange2) {
372 SKIP_IF_QTAGUID_NOT_SUPPORTED();
373
374 EXPECT_GE(legacy_tagSocket(sock_0.fd, valid_tag2, fake_uid), 0);
375 EXPECT_GE(legacy_tagSocket(sock_0.fd, valid_tag2, fake_uid2), 0);
376 EXPECT_TRUE(sock_0.checkTag(valid_tag2, fake_uid2)) << "Tag not found.";
377 EXPECT_FALSE(sock_0.checkTag(valid_tag2, fake_uid))
378 << "Tag should not be here";
379 }
380
381 /* Tag two sockets with two uids and two tags. */
TEST_F(SocketTagUsrSpaceTest,TagAnotherSocket)382 TEST_F(SocketTagUsrSpaceTest, TagAnotherSocket) {
383 SKIP_IF_QTAGUID_NOT_SUPPORTED();
384
385 EXPECT_GE(legacy_tagSocket(sock_0.fd, max_uint_tag, my_uid), 0);
386 EXPECT_GE(legacy_tagSocket(sock_1.fd, valid_tag1, fake_uid2), 0);
387 EXPECT_TRUE(sock_1.checkTag(valid_tag1, fake_uid2)) << "Tag not found.";
388 EXPECT_GE(legacy_untagSocket(sock_0.fd), 0);
389 EXPECT_FALSE(sock_0.checkTag(max_uint_tag, fake_uid))
390 << "Tag should not be there";
391 EXPECT_TRUE(sock_1.checkTag(valid_tag1, fake_uid2)) << "Tag not found";
392 EXPECT_GE(legacy_untagSocket(sock_1.fd), 0);
393 EXPECT_FALSE(sock_1.checkTag(valid_tag1, fake_uid2))
394 << "Tag should not be there";
395 }
396
397 /* Tag two sockets with the same uid but different acct_tags. */
TEST_F(SocketTagUsrSpaceTest,SameUidTwoSocket)398 TEST_F(SocketTagUsrSpaceTest, SameUidTwoSocket) {
399 SKIP_IF_QTAGUID_NOT_SUPPORTED();
400
401 EXPECT_GE(legacy_tagSocket(sock_0.fd, valid_tag1, my_uid), 0);
402 EXPECT_GE(legacy_tagSocket(sock_1.fd, valid_tag2, my_uid), 0);
403 EXPECT_TRUE(sock_1.checkTag(valid_tag2, my_uid)) << "Tag not found.";
404 EXPECT_TRUE(sock_0.checkTag(valid_tag1, my_uid)) << "Tag not found.";
405 EXPECT_GE(legacy_untagSocket(sock_0.fd), 0);
406 EXPECT_FALSE(sock_0.checkTag(valid_tag1, my_uid))
407 << "Tag should not be there";
408 EXPECT_TRUE(sock_1.checkTag(valid_tag2, my_uid)) << "Tag not found";
409 EXPECT_GE(legacy_untagSocket(sock_1.fd), 0);
410 EXPECT_FALSE(sock_1.checkTag(valid_tag2, my_uid))
411 << "Tag should not be there";
412 }
413
414 /* Tag two sockets with the same acct_tag but different uids */
TEST_F(SocketTagUsrSpaceTest,SameTagTwoSocket)415 TEST_F(SocketTagUsrSpaceTest, SameTagTwoSocket) {
416 SKIP_IF_QTAGUID_NOT_SUPPORTED();
417
418 EXPECT_GE(legacy_tagSocket(sock_0.fd, valid_tag1, fake_uid), 0);
419 EXPECT_GE(legacy_tagSocket(sock_1.fd, valid_tag1, fake_uid2), 0);
420 EXPECT_TRUE(sock_1.checkTag(valid_tag1, fake_uid)) << "Tag not found.";
421 EXPECT_TRUE(sock_0.checkTag(valid_tag1, fake_uid2)) << "Tag not found.";
422 EXPECT_GE(legacy_untagSocket(sock_0.fd), 0);
423 EXPECT_FALSE(sock_0.checkTag(valid_tag1, fake_uid))
424 << "Tag should not be there";
425 EXPECT_TRUE(sock_1.checkTag(valid_tag1, fake_uid2)) << "Tag not found";
426 EXPECT_GE(legacy_untagSocket(sock_1.fd), 0);
427 EXPECT_FALSE(sock_1.checkTag(valid_tag1, fake_uid2))
428 << "Tag should not be there";
429 }
430
431 /* Tag a closed socket, should fail. */
TEST_F(SocketTagUsrSpaceTest,TagInvalidSocketFail)432 TEST_F(SocketTagUsrSpaceTest, TagInvalidSocketFail) {
433 SKIP_IF_QTAGUID_NOT_SUPPORTED();
434
435 close(sock_0.fd);
436 EXPECT_LT(legacy_tagSocket(sock_0.fd, valid_tag1, my_uid), 0);
437 EXPECT_FALSE(sock_0.checkTag(valid_tag1, my_uid))
438 << "Tag should not be there";
439 }
440
441 /* untag from a closed socket, should fail. */
TEST_F(SocketTagUsrSpaceTest,UntagClosedSocketFail)442 TEST_F(SocketTagUsrSpaceTest, UntagClosedSocketFail) {
443 SKIP_IF_QTAGUID_NOT_SUPPORTED();
444
445 EXPECT_GE(legacy_tagSocket(sock_1.fd, valid_tag1, my_uid), 0);
446 EXPECT_TRUE(sock_1.checkTag(valid_tag1, my_uid));
447 close(sock_1.fd);
448 EXPECT_LT(legacy_untagSocket(sock_1.fd), 0) << "no tag attached, should fail";
449 }
450
451 /*
452 * try to connect with google.com in order to generate some
453 * tranffic through the socket, the traffic statistics should
454 * be stored in the stats file and will be returned.
455 */
TEST_F(SocketTagUsrSpaceTest,dataTransmitTest)456 TEST_F(SocketTagUsrSpaceTest, dataTransmitTest) {
457 SKIP_IF_QTAGUID_NOT_SUPPORTED();
458
459 memset(stats_result_, 0, 2);
460 EXPECT_GE(legacy_tagSocket(sock_0.fd, valid_tag1, inet_uid), 0);
461 EXPECT_TRUE(sock_0.checkTag(valid_tag1, inet_uid)) << "tag not found";
462 EXPECT_GE(server_download(sock_0, sock_1), 0);
463 EXPECT_GE(legacy_untagSocket(sock_0.fd), 0);
464 close(sock_0.fd);
465 EXPECT_TRUE(sock_0.checkStats(valid_tag1, inet_uid, 0, stats_result_))
466 << "failed to retreive data";
467 EXPECT_GT(*stats_result_, (uint32_t)0) << "no stats found for this socket";
468 EXPECT_GT(*(stats_result_ + 1), (uint32_t)0)
469 << "no stats stored for this socket";
470 }
471
472 /* Generate some traffic first and then delete the
473 * tag and uid from stats. All the stats related should
474 * be deleted. checkStats() should return false.
475 */
TEST_F(SocketTagUsrSpaceTest,dataStatsDeleteTest)476 TEST_F(SocketTagUsrSpaceTest, dataStatsDeleteTest) {
477 SKIP_IF_QTAGUID_NOT_SUPPORTED();
478
479 memset(stats_result_, 0, 2);
480 EXPECT_GE(legacy_tagSocket(sock_0.fd, valid_tag1, fake_uid), 0);
481 EXPECT_TRUE(sock_0.checkTag(valid_tag1, fake_uid)) << "tag not found";
482 EXPECT_GE(server_download(sock_0, sock_1), 0);
483 EXPECT_GE(legacy_untagSocket(sock_0.fd), 0);
484 EXPECT_TRUE(sock_0.checkStats(valid_tag1, fake_uid, 0, stats_result_))
485 << "failed to retreive data";
486 EXPECT_GT(*stats_result_, (uint32_t)0) << "no stats found for this socket";
487 EXPECT_GT(*(stats_result_ + 1), (uint32_t)0)
488 << "no stats stored for this socket";
489 EXPECT_GE(legacy_deleteTagData(0, fake_uid), 0)
490 << "Failed to delete fake_uid";
491 EXPECT_FALSE(sock_0.checkStats(valid_tag1, fake_uid, 0, stats_result_))
492 << "NO DATA should be stored";
493 }
494
495 /*
496 * try to store the traffic stats in the secound counter
497 * insdead of the first. All the stats should be stored
498 * in the secound counter.
499 */
TEST_F(SocketTagUsrSpaceTest,CounterSetTest)500 TEST_F(SocketTagUsrSpaceTest, CounterSetTest) {
501 SKIP_IF_QTAGUID_NOT_SUPPORTED();
502
503 memset(stats_result_, 0, 2);
504 EXPECT_GE(legacy_tagSocket(sock_1.fd, valid_tag1, inet_uid), 0);
505 EXPECT_GE(legacy_setCounterSet(1, inet_uid), 0);
506 EXPECT_TRUE(sock_1.checkTag(valid_tag1, inet_uid)) << "tag not found";
507 EXPECT_GE(server_download(sock_0, sock_1), 0);
508 EXPECT_GE(legacy_untagSocket(sock_1.fd), 0);
509 close(sock_1.fd);
510 EXPECT_TRUE(sock_1.checkStats(valid_tag1, inet_uid, 1, stats_result_))
511 << "failed to retreive data";
512 uint32_t packet_count = 1;
513 uint32_t total_byte = 1024;
514 EXPECT_GT(*stats_result_, total_byte) << "no stats found for this socket";
515 EXPECT_GT(*(stats_result_ + 1), packet_count)
516 << "wrong stats stored for this socket";
517 uint32_t stats_foreground[2] = {0, 0};
518 EXPECT_TRUE(sock_0.checkStats(valid_tag1, inet_uid, 0, stats_foreground))
519 << "fail to retrieve data";
520 EXPECT_LE(*stats_foreground, (uint32_t)0) << "stats data is not zero";
521 }
522 } // namespace android
523