Add tests for libtcutils
Add tests for isEthernet(). Before I can add tests for the other
functions, I need to move tcQdiscAddDevClsact into libtcutils.
Bug: 202086915
Bug: 157552970
Test: atest LibTcUtilsTest
Change-Id: Id51e87bb76bc42a055f71027c94c51fcb76be209
diff --git a/staticlibs/native/tcutils/Android.bp b/staticlibs/native/tcutils/Android.bp
index c26fca6..e819e4c 100644
--- a/staticlibs/native/tcutils/Android.bp
+++ b/staticlibs/native/tcutils/Android.bp
@@ -42,3 +42,27 @@
"//system/netd/server",
],
}
+
+cc_test {
+ name: "libtcutils_test",
+ srcs: [
+ "tests/tcutils_test.cpp",
+ ],
+ cflags: [
+ "-Wall",
+ "-Werror",
+ "-Wno-error=unused-variable",
+ ],
+ header_libs: ["bpf_syscall_wrappers"],
+ static_libs: [
+ "libgmock",
+ "libtcutils",
+ ],
+ shared_libs: [
+ "libbase",
+ "liblog",
+ ],
+ min_sdk_version: "30",
+ require_root: true,
+ test_suites: ["general-tests"],
+}
diff --git a/staticlibs/native/tcutils/tcutils.cpp b/staticlibs/native/tcutils/tcutils.cpp
index 00f3c56..3f24178 100644
--- a/staticlibs/native/tcutils/tcutils.cpp
+++ b/staticlibs/native/tcutils/tcutils.cpp
@@ -175,7 +175,7 @@
if (rv < 0) {
logError("Get hardware address type of interface %s failed: %s", iface,
strerror(-rv));
- return -rv;
+ return rv;
}
// Backwards compatibility with pre-GKI kernels that use various custom
@@ -208,7 +208,7 @@
return 0;
default:
logError("Unknown hardware address type %d on interface %s", rv, iface);
- return -ENOENT;
+ return -EAFNOSUPPORT;
}
}
diff --git a/staticlibs/native/tcutils/tests/tcutils_test.cpp b/staticlibs/native/tcutils/tests/tcutils_test.cpp
new file mode 100644
index 0000000..bab2d3e
--- /dev/null
+++ b/staticlibs/native/tcutils/tests/tcutils_test.cpp
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * TcUtilsTest.cpp - unit tests for TcUtils.cpp
+ */
+
+#include <gtest/gtest.h>
+
+#include <tcutils/tcutils.h>
+
+#include <errno.h>
+
+namespace android {
+
+TEST(LibTcUtilsTest, IsEthernetOfNonExistingIf) {
+ bool result = false;
+ int error = isEthernet("not_existing_if", result);
+ ASSERT_FALSE(result);
+ ASSERT_EQ(-ENODEV, error);
+}
+
+TEST(LibTcUtilsTest, IsEthernetOfLoopback) {
+ bool result = false;
+ int error = isEthernet("lo", result);
+ ASSERT_FALSE(result);
+ ASSERT_EQ(-EAFNOSUPPORT, error);
+}
+
+// If wireless 'wlan0' interface exists it should be Ethernet.
+// See also HardwareAddressTypeOfWireless.
+TEST(LibTcUtilsTest, IsEthernetOfWireless) {
+ bool result = false;
+ int error = isEthernet("wlan0", result);
+ if (!result && error == -ENODEV)
+ return;
+
+ ASSERT_EQ(0, error);
+ ASSERT_TRUE(result);
+}
+
+// If cellular 'rmnet_data0' interface exists it should
+// *probably* not be Ethernet and instead be RawIp.
+// See also HardwareAddressTypeOfCellular.
+TEST(LibTcUtilsTest, IsEthernetOfCellular) {
+ bool result = false;
+ int error = isEthernet("rmnet_data0", result);
+ if (!result && error == -ENODEV)
+ return;
+
+ ASSERT_EQ(0, error);
+ ASSERT_FALSE(result);
+}
+
+} // namespace android