github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/syscalls/linux/network_namespace.cc (about) 1 // Copyright 2020 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include <net/if.h> 16 #include <sched.h> 17 #include <sys/ioctl.h> 18 #include <sys/socket.h> 19 #include <sys/types.h> 20 21 #include "gmock/gmock.h" 22 #include "gtest/gtest.h" 23 #include "test/syscalls/linux/socket_test_util.h" 24 #include "test/util/capability_util.h" 25 #include "test/util/posix_error.h" 26 #include "test/util/test_util.h" 27 #include "test/util/thread_util.h" 28 29 namespace gvisor { 30 namespace testing { 31 namespace { 32 33 TEST(NetworkNamespaceTest, LoopbackExists) { 34 SKIP_IF(!ASSERT_NO_ERRNO_AND_VALUE(HaveCapability(CAP_NET_ADMIN))); 35 36 ScopedThread t([&] { 37 ASSERT_THAT(unshare(CLONE_NEWNET), SyscallSucceedsWithValue(0)); 38 39 // TODO(gvisor.dev/issue/1833): Update this to test that only "lo" exists. 40 // Check loopback device exists. 41 int sock = socket(AF_INET, SOCK_DGRAM, 0); 42 ASSERT_THAT(sock, SyscallSucceeds()); 43 struct ifreq ifr; 44 strncpy(ifr.ifr_name, "lo", IFNAMSIZ); 45 EXPECT_THAT(ioctl(sock, SIOCGIFINDEX, &ifr), SyscallSucceeds()) 46 << "lo cannot be found"; 47 }); 48 } 49 50 } // namespace 51 } // namespace testing 52 } // namespace gvisor