blob: 08f304274106a7067776d4a479a5af74b75d8029 [file] [log] [blame]
Hungming Chen8ebdb6f2022-01-16 14:44:11 +08001/*
2 * Copyright 2019 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 * TcUtilsTest.cpp - unit tests for TcUtils.cpp
17 */
18
19#include <gtest/gtest.h>
20
21#include "libclat/TcUtils.h"
22
23#include <linux/if_arp.h>
24#include <stdlib.h>
25#include <sys/wait.h>
26
27#include "bpf/BpfUtils.h"
28#include "bpf_shared.h"
29
30namespace android {
31namespace net {
32
33class TcUtilsTest : public ::testing::Test {
34 public:
35 void SetUp() {}
36};
37
38TEST_F(TcUtilsTest, HardwareAddressTypeOfNonExistingIf) {
39 ASSERT_EQ(-ENODEV, hardwareAddressType("not_existing_if"));
40}
41
42TEST_F(TcUtilsTest, HardwareAddressTypeOfLoopback) {
43 ASSERT_EQ(ARPHRD_LOOPBACK, hardwareAddressType("lo"));
44}
45
46// If wireless 'wlan0' interface exists it should be Ethernet.
47TEST_F(TcUtilsTest, HardwareAddressTypeOfWireless) {
48 int type = hardwareAddressType("wlan0");
49 if (type == -ENODEV) return;
50
51 ASSERT_EQ(ARPHRD_ETHER, type);
52}
53
54// If cellular 'rmnet_data0' interface exists it should
55// *probably* not be Ethernet and instead be RawIp.
56TEST_F(TcUtilsTest, HardwareAddressTypeOfCellular) {
57 int type = hardwareAddressType("rmnet_data0");
58 if (type == -ENODEV) return;
59
60 ASSERT_NE(ARPHRD_ETHER, type);
61
62 // ARPHRD_RAWIP is 530 on some pre-4.14 Qualcomm devices.
63 if (type == 530) return;
64
65 ASSERT_EQ(ARPHRD_RAWIP, type);
66}
67
68TEST_F(TcUtilsTest, IsEthernetOfNonExistingIf) {
69 auto res = isEthernet("not_existing_if");
70 ASSERT_FALSE(res.ok());
71 ASSERT_EQ(ENODEV, res.error().code());
72}
73
74TEST_F(TcUtilsTest, IsEthernetOfLoopback) {
75 auto res = isEthernet("lo");
76 ASSERT_FALSE(res.ok());
77 ASSERT_EQ(EAFNOSUPPORT, res.error().code());
78}
79
80// If wireless 'wlan0' interface exists it should be Ethernet.
81// See also HardwareAddressTypeOfWireless.
82TEST_F(TcUtilsTest, IsEthernetOfWireless) {
83 auto res = isEthernet("wlan0");
84 if (!res.ok() && res.error().code() == ENODEV) return;
85
86 ASSERT_RESULT_OK(res);
87 ASSERT_TRUE(res.value());
88}
89
90// If cellular 'rmnet_data0' interface exists it should
91// *probably* not be Ethernet and instead be RawIp.
92// See also HardwareAddressTypeOfCellular.
93TEST_F(TcUtilsTest, IsEthernetOfCellular) {
94 auto res = isEthernet("rmnet_data0");
95 if (!res.ok() && res.error().code() == ENODEV) return;
96
97 ASSERT_RESULT_OK(res);
98 ASSERT_FALSE(res.value());
99}
100
101TEST_F(TcUtilsTest, DeviceMTUOfNonExistingIf) {
102 ASSERT_EQ(-ENODEV, deviceMTU("not_existing_if"));
103}
104
105TEST_F(TcUtilsTest, DeviceMTUofLoopback) {
106 ASSERT_EQ(65536, deviceMTU("lo"));
107}
108
109TEST_F(TcUtilsTest, GetClatEgress4MapFd) {
110 int fd = getClatEgress4MapFd();
111 ASSERT_GE(fd, 3); // 0,1,2 - stdin/out/err, thus fd >= 3
112 EXPECT_EQ(FD_CLOEXEC, fcntl(fd, F_GETFD));
113 close(fd);
114}
115
116TEST_F(TcUtilsTest, GetClatEgress4RawIpProgFd) {
117 int fd = getClatEgress4ProgFd(RAWIP);
118 ASSERT_GE(fd, 3);
119 EXPECT_EQ(FD_CLOEXEC, fcntl(fd, F_GETFD));
120 close(fd);
121}
122
123TEST_F(TcUtilsTest, GetClatEgress4EtherProgFd) {
124 int fd = getClatEgress4ProgFd(ETHER);
125 ASSERT_GE(fd, 3);
126 EXPECT_EQ(FD_CLOEXEC, fcntl(fd, F_GETFD));
127 close(fd);
128}
129
130TEST_F(TcUtilsTest, GetClatIngress6MapFd) {
131 int fd = getClatIngress6MapFd();
132 ASSERT_GE(fd, 3); // 0,1,2 - stdin/out/err, thus fd >= 3
133 EXPECT_EQ(FD_CLOEXEC, fcntl(fd, F_GETFD));
134 close(fd);
135}
136
137TEST_F(TcUtilsTest, GetClatIngress6RawIpProgFd) {
138 int fd = getClatIngress6ProgFd(RAWIP);
139 ASSERT_GE(fd, 3);
140 EXPECT_EQ(FD_CLOEXEC, fcntl(fd, F_GETFD));
141 close(fd);
142}
143
144TEST_F(TcUtilsTest, GetClatIngress6EtherProgFd) {
145 int fd = getClatIngress6ProgFd(ETHER);
146 ASSERT_GE(fd, 3);
147 EXPECT_EQ(FD_CLOEXEC, fcntl(fd, F_GETFD));
148 close(fd);
149}
150
151// See Linux kernel source in include/net/flow.h
152#define LOOPBACK_IFINDEX 1
153
154TEST_F(TcUtilsTest, AttachReplaceDetachClsactLo) {
155 // This attaches and detaches a configuration-less and thus no-op clsact
156 // qdisc to loopback interface (and it takes fractions of a second)
157 EXPECT_EQ(0, tcQdiscAddDevClsact(LOOPBACK_IFINDEX));
158 EXPECT_EQ(0, tcQdiscReplaceDevClsact(LOOPBACK_IFINDEX));
159 EXPECT_EQ(0, tcQdiscDelDevClsact(LOOPBACK_IFINDEX));
160 EXPECT_EQ(-EINVAL, tcQdiscDelDevClsact(LOOPBACK_IFINDEX));
161}
162
163static void checkAttachDetachBpfFilterClsactLo(const bool ingress, const bool ethernet) {
164 // Older kernels return EINVAL instead of ENOENT due to lacking proper error propagation...
165 const int errNOENT = android::bpf::isAtLeastKernelVersion(4, 19, 0) ? ENOENT : EINVAL;
166
167 int clatBpfFd = ingress ? getClatIngress6ProgFd(ethernet) : getClatEgress4ProgFd(ethernet);
168 ASSERT_GE(clatBpfFd, 3);
169
170 // This attaches and detaches a clsact plus ebpf program to loopback
171 // interface, but it should not affect traffic by virtue of us not
172 // actually populating the ebpf control map.
173 // Furthermore: it only takes fractions of a second.
174 EXPECT_EQ(-EINVAL, tcFilterDelDevIngressClatIpv6(LOOPBACK_IFINDEX));
175 EXPECT_EQ(-EINVAL, tcFilterDelDevEgressClatIpv4(LOOPBACK_IFINDEX));
176 EXPECT_EQ(0, tcQdiscAddDevClsact(LOOPBACK_IFINDEX));
177 EXPECT_EQ(-errNOENT, tcFilterDelDevIngressClatIpv6(LOOPBACK_IFINDEX));
178 EXPECT_EQ(-errNOENT, tcFilterDelDevEgressClatIpv4(LOOPBACK_IFINDEX));
179 if (ingress) {
180 EXPECT_EQ(0, tcFilterAddDevIngressClatIpv6(LOOPBACK_IFINDEX, clatBpfFd, ethernet));
181 EXPECT_EQ(0, tcFilterDelDevIngressClatIpv6(LOOPBACK_IFINDEX));
182 } else {
183 EXPECT_EQ(0, tcFilterAddDevEgressClatIpv4(LOOPBACK_IFINDEX, clatBpfFd, ethernet));
184 EXPECT_EQ(0, tcFilterDelDevEgressClatIpv4(LOOPBACK_IFINDEX));
185 }
186 EXPECT_EQ(-errNOENT, tcFilterDelDevIngressClatIpv6(LOOPBACK_IFINDEX));
187 EXPECT_EQ(-errNOENT, tcFilterDelDevEgressClatIpv4(LOOPBACK_IFINDEX));
188 EXPECT_EQ(0, tcQdiscDelDevClsact(LOOPBACK_IFINDEX));
189 EXPECT_EQ(-EINVAL, tcFilterDelDevIngressClatIpv6(LOOPBACK_IFINDEX));
190 EXPECT_EQ(-EINVAL, tcFilterDelDevEgressClatIpv4(LOOPBACK_IFINDEX));
191
192 close(clatBpfFd);
193}
194
195TEST_F(TcUtilsTest, CheckAttachBpfFilterRawIpClsactEgressLo) {
196 checkAttachDetachBpfFilterClsactLo(EGRESS, RAWIP);
197}
198
199TEST_F(TcUtilsTest, CheckAttachBpfFilterEthernetClsactEgressLo) {
200 checkAttachDetachBpfFilterClsactLo(EGRESS, ETHER);
201}
202
203TEST_F(TcUtilsTest, CheckAttachBpfFilterRawIpClsactIngressLo) {
204 checkAttachDetachBpfFilterClsactLo(INGRESS, RAWIP);
205}
206
207TEST_F(TcUtilsTest, CheckAttachBpfFilterEthernetClsactIngressLo) {
208 checkAttachDetachBpfFilterClsactLo(INGRESS, ETHER);
209}
210
211} // namespace net
212} // namespace android