blob: 8c2152897508e570cbb0bc83742006da49e007c3 [file] [log] [blame]
Maciej Żenczykowskib70da762019-01-28 15:20:48 -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 * ClatUtilsTest.cpp - unit tests for ClatUtils.cpp
17 */
18
19#include <gtest/gtest.h>
20
21#include "ClatUtils.h"
22
Maciej Żenczykowski0a7dce82019-01-28 15:31:55 -080023#include <linux/if_arp.h>
24
Maciej Żenczykowski88d28ff2019-03-25 11:54:32 -070025#include "bpf/BpfUtils.h"
26#include "netdbpf/bpf_shared.h"
27
Maciej Żenczykowskib70da762019-01-28 15:20:48 -080028namespace android {
29namespace net {
30
31class ClatUtilsTest : public ::testing::Test {
32 public:
33 void SetUp() {}
34};
35
Maciej Żenczykowski0a7dce82019-01-28 15:31:55 -080036TEST_F(ClatUtilsTest, HardwareAddressTypeOfNonExistingIf) {
Maciej Żenczykowski10a2ed52019-03-27 15:46:26 -070037 ASSERT_EQ(-ENODEV, hardwareAddressType("not_existing_if"));
Maciej Żenczykowski0a7dce82019-01-28 15:31:55 -080038}
39
40TEST_F(ClatUtilsTest, HardwareAddressTypeOfLoopback) {
Maciej Żenczykowski10a2ed52019-03-27 15:46:26 -070041 ASSERT_EQ(ARPHRD_LOOPBACK, hardwareAddressType("lo"));
Maciej Żenczykowski0a7dce82019-01-28 15:31:55 -080042}
43
44// If wireless 'wlan0' interface exists it should be Ethernet.
45TEST_F(ClatUtilsTest, HardwareAddressTypeOfWireless) {
46 int type = hardwareAddressType("wlan0");
47 if (type == -ENODEV) return;
48
Maciej Żenczykowski10a2ed52019-03-27 15:46:26 -070049 ASSERT_EQ(ARPHRD_ETHER, type);
Maciej Żenczykowski0a7dce82019-01-28 15:31:55 -080050}
51
52// If cellular 'rmnet_data0' interface exists it should
53// *probably* not be Ethernet and instead be RawIp.
54TEST_F(ClatUtilsTest, HardwareAddressTypeOfCellular) {
55 int type = hardwareAddressType("rmnet_data0");
56 if (type == -ENODEV) return;
57
Maciej Żenczykowski10a2ed52019-03-27 15:46:26 -070058 ASSERT_NE(ARPHRD_ETHER, type);
Maciej Żenczykowski0a7dce82019-01-28 15:31:55 -080059
60 // ARPHRD_RAWIP is 530 on some pre-4.14 Qualcomm devices.
61 if (type == 530) return;
62
Maciej Żenczykowski10a2ed52019-03-27 15:46:26 -070063 ASSERT_EQ(ARPHRD_RAWIP, type);
Maciej Żenczykowski0a7dce82019-01-28 15:31:55 -080064}
65
Maciej Żenczykowski88d28ff2019-03-25 11:54:32 -070066TEST_F(ClatUtilsTest, GetClatMapFd) {
67 SKIP_IF_BPF_NOT_SUPPORTED;
68
Maciej Żenczykowski10a2ed52019-03-27 15:46:26 -070069 int fd = getClatMapFd();
70 ASSERT_LE(3, fd); // 0,1,2 - stdin/out/err, thus 3 <= fd
71 close(fd);
Maciej Żenczykowski88d28ff2019-03-25 11:54:32 -070072}
73
Maciej Żenczykowski949d84a2019-01-28 17:22:30 -080074TEST_F(ClatUtilsTest, GetClatRawIpProgFd) {
75 SKIP_IF_BPF_NOT_SUPPORTED;
76
Maciej Żenczykowski10a2ed52019-03-27 15:46:26 -070077 int fd = getClatProgFd(false);
78 ASSERT_LE(3, fd);
79 close(fd);
Maciej Żenczykowski949d84a2019-01-28 17:22:30 -080080}
81
82TEST_F(ClatUtilsTest, GetClatEtherProgFd) {
83 SKIP_IF_BPF_NOT_SUPPORTED;
84
Maciej Żenczykowski10a2ed52019-03-27 15:46:26 -070085 int fd = getClatProgFd(true);
86 ASSERT_LE(3, fd);
87 close(fd);
Maciej Żenczykowski949d84a2019-01-28 17:22:30 -080088}
89
Maciej Żenczykowski7330b022019-01-28 17:30:24 -080090TEST_F(ClatUtilsTest, TryOpeningNetlinkSocket) {
91 int fd = openNetlinkSocket();
92 ASSERT_LE(3, fd);
93 close(fd);
94}
95
Maciej Żenczykowskiff3308d2019-02-12 19:10:55 -080096// See Linux kernel source in include/net/flow.h
97#define LOOPBACK_IFINDEX 1
98
99TEST_F(ClatUtilsTest, AttachReplaceDetachClsactLo) {
100 // Technically does not depend on ebpf, but does depend on clsact,
101 // and we do not really care if it works on pre-4.9-Q anyway.
102 SKIP_IF_BPF_NOT_SUPPORTED;
103
104 int fd = openNetlinkSocket();
105 ASSERT_LE(3, fd);
106
107 // This attaches and detaches a configuration-less and thus no-op clsact
108 // qdisc to loopback interface (and it takes fractions of a second)
109 EXPECT_EQ(0, tcQdiscAddDevClsact(fd, LOOPBACK_IFINDEX));
110 EXPECT_EQ(0, tcQdiscReplaceDevClsact(fd, LOOPBACK_IFINDEX));
111 EXPECT_EQ(0, tcQdiscDelDevClsact(fd, LOOPBACK_IFINDEX));
112 close(fd);
113}
114
Maciej Żenczykowskib70da762019-01-28 15:20:48 -0800115} // namespace net
116} // namespace android