blob: 6f28fbec0b8642897e8d3367c0f65e04ebb3bd0a [file] [log] [blame]
Patrick Rohr27846ff2022-01-17 12:22:51 +01001/*
2 * Copyright 2022 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 <tcutils/tcutils.h>
22
23#include <errno.h>
24
25namespace android {
26
27TEST(LibTcUtilsTest, IsEthernetOfNonExistingIf) {
28 bool result = false;
29 int error = isEthernet("not_existing_if", result);
30 ASSERT_FALSE(result);
31 ASSERT_EQ(-ENODEV, error);
32}
33
34TEST(LibTcUtilsTest, IsEthernetOfLoopback) {
35 bool result = false;
36 int error = isEthernet("lo", result);
37 ASSERT_FALSE(result);
38 ASSERT_EQ(-EAFNOSUPPORT, error);
39}
40
41// If wireless 'wlan0' interface exists it should be Ethernet.
42// See also HardwareAddressTypeOfWireless.
43TEST(LibTcUtilsTest, IsEthernetOfWireless) {
44 bool result = false;
45 int error = isEthernet("wlan0", result);
46 if (!result && error == -ENODEV)
47 return;
48
49 ASSERT_EQ(0, error);
50 ASSERT_TRUE(result);
51}
52
53// If cellular 'rmnet_data0' interface exists it should
54// *probably* not be Ethernet and instead be RawIp.
55// See also HardwareAddressTypeOfCellular.
56TEST(LibTcUtilsTest, IsEthernetOfCellular) {
57 bool result = false;
58 int error = isEthernet("rmnet_data0", result);
59 if (!result && error == -ENODEV)
60 return;
61
62 ASSERT_EQ(0, error);
63 ASSERT_FALSE(result);
64}
65
Patrick Rohr42b58ae2022-01-17 13:09:12 +010066// See Linux kernel source in include/net/flow.h
67static constexpr int LOOPBACK_IFINDEX = 1;
68
69TEST(LibTcUtilsTest, AttachReplaceDetachClsactLo) {
70 // This attaches and detaches a configuration-less and thus no-op clsact
71 // qdisc to loopback interface (and it takes fractions of a second)
72 EXPECT_EQ(0, tcAddQdiscClsact(LOOPBACK_IFINDEX));
73 EXPECT_EQ(0, tcReplaceQdiscClsact(LOOPBACK_IFINDEX));
74 EXPECT_EQ(0, tcDeleteQdiscClsact(LOOPBACK_IFINDEX));
75 EXPECT_EQ(-EINVAL, tcDeleteQdiscClsact(LOOPBACK_IFINDEX));
76}
77
Patrick Rohr27846ff2022-01-17 12:22:51 +010078} // namespace android