blob: 8d5d8bc0aebf385e0d2d7088f2a2c92b08e2946f [file] [log] [blame]
Pierre Imai904ce3a2016-02-18 13:13:12 +09001/*
2 * Copyright (C) 2016 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 *
Lorenzo Colitti93a0d642019-06-26 22:31:03 +090010 * Unless required by applicable law or agreed to in writing, software
Pierre Imai904ce3a2016-02-18 13:13:12 +090011 * 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 */
17
Maciej Żenczykowski9ce7e662020-05-29 00:41:22 -070018#include <arpa/inet.h>
Pierre Imai904ce3a2016-02-18 13:13:12 +090019#include <errno.h>
Maciej Żenczykowski9ce7e662020-05-29 00:41:22 -070020#include <linux/if_packet.h>
21#include <linux/if_tun.h>
22#include <net/if.h>
23#include <poll.h>
Maciej Żenczykowski7b242462020-05-13 20:53:10 -070024#include <sched.h>
Chenbo Feng66165472018-07-23 19:05:56 -070025#include <sys/capability.h>
Maciej Żenczykowski9ce7e662020-05-29 00:41:22 -070026#include <sys/ioctl.h>
27#include <sys/socket.h>
28#include <sys/types.h>
Pierre Imai904ce3a2016-02-18 13:13:12 +090029
30#include <gtest/gtest.h>
Pierre Imaibeedec32016-04-13 06:44:51 +090031
Maciej Żenczykowski9ce7e662020-05-29 00:41:22 -070032#include <android-base/unique_fd.h>
33
34#define LOG_TAG "NetdTest"
Maciej Żenczykowski9ce7e662020-05-29 00:41:22 -070035
Patrick Rohr70d42502021-10-12 18:24:10 +020036#include "TcUtils.h"
Maciej Żenczykowski9ce7e662020-05-29 00:41:22 -070037
38namespace android {
39namespace net {
40
41using base::unique_fd;
42
Chenbo Feng66165472018-07-23 19:05:56 -070043TEST(NetUtilsWrapperTest, TestFileCapabilities) {
44 errno = 0;
45 ASSERT_EQ(NULL, cap_get_file("/system/bin/netutils-wrapper-1.0"));
46 ASSERT_EQ(ENODATA, errno);
47}
Maciej Żenczykowski5a914822019-05-02 21:57:10 -070048
Maciej Żenczykowski02a8e172021-06-08 21:18:37 -070049// If this test fails most likely your device is lacking device/oem specific
50// selinux genfscon rules, something like .../vendor/.../genfs_contexts:
51// genfscon sysfs /devices/platform/.../net u:object_r:sysfs_net:s0
52// Easiest debugging is via:
53// adb root && sleep 1 && adb shell 'ls -Z /sys/class/net/*/mtu'
54// and look for the mislabeled item(s).
55// Everything should be 'u:object_r:sysfs_net:s0'
56//
57// Another useful command is:
58// adb root && sleep 1 && adb shell find /sys > dump.out
59// or in particular:
60// adb root && sleep 1 && adb shell find /sys | egrep '/net$'
61// which might (among other things) print out something like:
62// /sys/devices/platform/11110000.usb/11110000.dwc3/gadget/net
63// which means you need to add:
64// genfscon sysfs /devices/platform/11110000.usb/11110000.dwc3/gadget/net u:object_r:sysfs_net:s0
Maciej Żenczykowski5a914822019-05-02 21:57:10 -070065TEST(NetdSELinuxTest, CheckProperMTULabels) {
66 // Since we expect the egrep regexp to filter everything out,
67 // we thus expect no matches and thus a return code of 1
68 // NOLINTNEXTLINE(cert-env33-c)
69 ASSERT_EQ(W_EXITCODE(1, 0), system("ls -Z /sys/class/net/*/mtu | egrep -q -v "
70 "'^u:object_r:sysfs_net:s0 /sys/class/net/'"));
71}
Maciej Żenczykowski7b242462020-05-13 20:53:10 -070072
73// Trivial thread function that simply immediately terminates successfully.
74static int thread(void*) {
75 return 0;
76}
77
78typedef int (*thread_t)(void*);
79
80static void nsTest(int flags, bool success, thread_t newThread) {
81 // We need a minimal stack, but not clear if it will grow up or down,
82 // So allocate 2 pages and give a pointer to the middle.
83 static char stack[PAGE_SIZE * 2];
84 errno = 0;
85 // VFORK: if thread is successfully created, then kernel will wait for it
86 // to terminate before we resume -> hence static stack is safe to reuse.
87 int tid = clone(newThread, &stack[PAGE_SIZE], flags | CLONE_VFORK, NULL);
88 if (success) {
89 ASSERT_EQ(errno, 0);
90 ASSERT_GE(tid, 0);
91 } else {
92 ASSERT_EQ(errno, EINVAL);
93 ASSERT_EQ(tid, -1);
94 }
95}
96
97// Test kernel configuration option CONFIG_NAMESPACES=y
98TEST(NetdNamespaceTest, CheckMountNamespaceSupport) {
99 nsTest(CLONE_NEWNS, true, thread);
100}
101
102// Test kernel configuration option CONFIG_UTS_NS=y
103TEST(NetdNamespaceTest, CheckUTSNamespaceSupport) {
104 nsTest(CLONE_NEWUTS, true, thread);
105}
106
107// Test kernel configuration option CONFIG_NET_NS=y
108TEST(NetdNamespaceTest, CheckNetworkNamespaceSupport) {
109 nsTest(CLONE_NEWNET, true, thread);
110}
111
112// Test kernel configuration option CONFIG_USER_NS=n
113TEST(NetdNamespaceTest, /*DISABLED_*/ CheckNoUserNamespaceSupport) {
114 nsTest(CLONE_NEWUSER, false, thread);
115}
116
117// Test for all of the above
118TEST(NetdNamespaceTest, CheckFullNamespaceSupport) {
119 nsTest(CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWNET, true, thread);
120}
Maciej Żenczykowski9ce7e662020-05-29 00:41:22 -0700121
Maciej Żenczykowski9ce7e662020-05-29 00:41:22 -0700122} // namespace net
123} // namespace android