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