1 // Copyright 2017 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/socket/socket_tag.h"
6
7 #include "build/build_config.h"
8
9 #if BUILDFLAG(IS_ANDROID)
10 #include <netinet/in.h>
11 #include <sys/socket.h>
12 #include <sys/types.h>
13 #endif
14
15 #include <stdint.h>
16
17 #include "net/base/ip_address.h"
18 #include "net/base/ip_endpoint.h"
19 #include "net/base/sockaddr_storage.h"
20 #include "net/socket/socket_test_util.h"
21 #include "net/test/embedded_test_server/embedded_test_server.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23
24 namespace net {
25
26 // Test that SocketTag's comparison function work.
TEST(SocketTagTest,Compares)27 TEST(SocketTagTest, Compares) {
28 SocketTag unset1;
29 SocketTag unset2;
30
31 EXPECT_TRUE(unset1 == unset2);
32 EXPECT_FALSE(unset1 != unset2);
33 EXPECT_FALSE(unset1 < unset2);
34
35 #if BUILDFLAG(IS_ANDROID)
36 SocketTag s00(0, 0), s01(0, 1), s11(1, 1);
37
38 EXPECT_FALSE(s00 == unset1);
39 EXPECT_TRUE(s01 != unset2);
40 EXPECT_FALSE(unset1 < s00);
41 EXPECT_TRUE(s00 < unset2);
42
43 EXPECT_FALSE(s00 == s01);
44 EXPECT_FALSE(s01 == s11);
45 EXPECT_FALSE(s00 == s11);
46 EXPECT_TRUE(s00 < s01);
47 EXPECT_TRUE(s01 < s11);
48 EXPECT_TRUE(s00 < s11);
49 EXPECT_FALSE(s01 < s00);
50 EXPECT_FALSE(s11 < s01);
51 EXPECT_FALSE(s11 < s00);
52 #endif
53 }
54
55 // On Android, where socket tagging is supported, verify that SocketTag::Apply
56 // works as expected.
57 #if BUILDFLAG(IS_ANDROID)
TEST(SocketTagTest,Apply)58 TEST(SocketTagTest, Apply) {
59 if (!CanGetTaggedBytes()) {
60 DVLOG(0) << "Skipping test - GetTaggedBytes unsupported.";
61 return;
62 }
63
64 // Start test server.
65 EmbeddedTestServer test_server;
66 test_server.AddDefaultHandlers(base::FilePath());
67 ASSERT_TRUE(test_server.Start());
68
69 // Calculate sockaddr of test server.
70 AddressList addr_list;
71 ASSERT_TRUE(test_server.GetAddressList(&addr_list));
72 SockaddrStorage addr;
73 ASSERT_TRUE(addr_list[0].ToSockAddr(addr.addr, &addr.addr_len));
74
75 // Create socket.
76 int s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
77 ASSERT_NE(s, -1);
78
79 // Verify TCP connect packets are tagged and counted properly.
80 int32_t tag_val1 = 0x12345678;
81 uint64_t old_traffic = GetTaggedBytes(tag_val1);
82 SocketTag tag1(SocketTag::UNSET_UID, tag_val1);
83 tag1.Apply(s);
84 ASSERT_EQ(connect(s, addr.addr, addr.addr_len), 0);
85 EXPECT_GT(GetTaggedBytes(tag_val1), old_traffic);
86
87 // Verify socket can be retagged with a new value and the current process's
88 // UID.
89 int32_t tag_val2 = 0x87654321;
90 old_traffic = GetTaggedBytes(tag_val2);
91 SocketTag tag2(getuid(), tag_val2);
92 tag2.Apply(s);
93 const char kRequest1[] = "GET / HTTP/1.0";
94 ASSERT_EQ(send(s, kRequest1, strlen(kRequest1), 0),
95 static_cast<int>(strlen(kRequest1)));
96 EXPECT_GT(GetTaggedBytes(tag_val2), old_traffic);
97
98 // Verify socket can be retagged with a new value and the current process's
99 // UID.
100 old_traffic = GetTaggedBytes(tag_val1);
101 tag1.Apply(s);
102 const char kRequest2[] = "\n\n";
103 ASSERT_EQ(send(s, kRequest2, strlen(kRequest2), 0),
104 static_cast<int>(strlen(kRequest2)));
105 EXPECT_GT(GetTaggedBytes(tag_val1), old_traffic);
106
107 ASSERT_EQ(close(s), 0);
108 }
109 #endif
110
111 } // namespace net
112