blob: 6bd70481d78c61e5e477ab24d9df1bb3cc6ecfac [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) {
37 EXPECT_EQ(-ENODEV, hardwareAddressType("not_existing_if"));
38}
39
40TEST_F(ClatUtilsTest, HardwareAddressTypeOfLoopback) {
41 EXPECT_EQ(ARPHRD_LOOPBACK, hardwareAddressType("lo"));
42}
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
49 EXPECT_EQ(ARPHRD_ETHER, type);
50}
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
58 EXPECT_NE(ARPHRD_ETHER, type);
59
60 // ARPHRD_RAWIP is 530 on some pre-4.14 Qualcomm devices.
61 if (type == 530) return;
62
63 EXPECT_EQ(ARPHRD_RAWIP, type);
64}
65
Maciej Żenczykowski88d28ff2019-03-25 11:54:32 -070066TEST_F(ClatUtilsTest, GetClatMapFd) {
67 SKIP_IF_BPF_NOT_SUPPORTED;
68
69 base::unique_fd ufd(getClatMapFd());
70 EXPECT_LE(3, ufd); // 0,1,2 - stdin/out/err, thus 3 <= fd
71}
72
Maciej Żenczykowskib70da762019-01-28 15:20:48 -080073} // namespace net
74} // namespace android