gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/syscalls/linux/uname.cc (about) 1 // Copyright 2018 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 <sched.h> 16 #include <sys/utsname.h> 17 #include <unistd.h> 18 19 #include "gtest/gtest.h" 20 #include "absl/strings/string_view.h" 21 #include "test/util/capability_util.h" 22 #include "test/util/test_util.h" 23 #include "test/util/thread_util.h" 24 25 namespace gvisor { 26 namespace testing { 27 28 namespace { 29 30 TEST(UnameTest, Sanity) { 31 struct utsname buf; 32 ASSERT_THAT(uname(&buf), SyscallSucceeds()); 33 EXPECT_NE(strlen(buf.release), 0); 34 EXPECT_NE(strlen(buf.version), 0); 35 EXPECT_NE(strlen(buf.machine), 0); 36 EXPECT_NE(strlen(buf.sysname), 0); 37 EXPECT_NE(strlen(buf.nodename), 0); 38 EXPECT_NE(strlen(buf.domainname), 0); 39 } 40 41 TEST(UnameTest, SetNames) { 42 SKIP_IF(!ASSERT_NO_ERRNO_AND_VALUE(HaveCapability(CAP_SYS_ADMIN))); 43 44 char hostname[65]; 45 ASSERT_THAT(sethostname("0123456789", 3), SyscallSucceeds()); 46 EXPECT_THAT(gethostname(hostname, sizeof(hostname)), SyscallSucceeds()); 47 EXPECT_EQ(absl::string_view(hostname), "012"); 48 49 ASSERT_THAT(sethostname("0123456789\0xxx", 11), SyscallSucceeds()); 50 EXPECT_THAT(gethostname(hostname, sizeof(hostname)), SyscallSucceeds()); 51 EXPECT_EQ(absl::string_view(hostname), "0123456789"); 52 53 ASSERT_THAT(sethostname("0123456789\0xxx", 12), SyscallSucceeds()); 54 EXPECT_THAT(gethostname(hostname, sizeof(hostname)), SyscallSucceeds()); 55 EXPECT_EQ(absl::string_view(hostname), "0123456789"); 56 57 constexpr char kHostname[] = "wubbalubba"; 58 ASSERT_THAT(sethostname(kHostname, sizeof(kHostname)), SyscallSucceeds()); 59 60 constexpr char kDomainname[] = "dubdub.com"; 61 ASSERT_THAT(setdomainname(kDomainname, sizeof(kDomainname)), 62 SyscallSucceeds()); 63 64 struct utsname buf; 65 EXPECT_THAT(uname(&buf), SyscallSucceeds()); 66 EXPECT_EQ(absl::string_view(buf.nodename), kHostname); 67 EXPECT_EQ(absl::string_view(buf.domainname), kDomainname); 68 69 // These should just be glibc wrappers that also call uname(2). 70 EXPECT_THAT(gethostname(hostname, sizeof(hostname)), SyscallSucceeds()); 71 EXPECT_EQ(absl::string_view(hostname), kHostname); 72 73 char domainname[65]; 74 EXPECT_THAT(getdomainname(domainname, sizeof(domainname)), SyscallSucceeds()); 75 EXPECT_EQ(absl::string_view(domainname), kDomainname); 76 } 77 78 TEST(UnameTest, UnprivilegedSetNames) { 79 AutoCapability cap(CAP_SYS_ADMIN, false); 80 81 EXPECT_THAT(sethostname("", 0), SyscallFailsWithErrno(EPERM)); 82 EXPECT_THAT(setdomainname("", 0), SyscallFailsWithErrno(EPERM)); 83 } 84 85 TEST(UnameTest, UnshareUTS) { 86 SKIP_IF(!ASSERT_NO_ERRNO_AND_VALUE(HaveCapability(CAP_SYS_ADMIN))); 87 88 struct utsname init; 89 ASSERT_THAT(uname(&init), SyscallSucceeds()); 90 91 ScopedThread thread = ScopedThread([&]() { 92 EXPECT_THAT(unshare(CLONE_NEWUTS), SyscallSucceeds()); 93 94 constexpr char kHostname[] = "wubbalubba"; 95 EXPECT_THAT(sethostname(kHostname, sizeof(kHostname)), SyscallSucceeds()); 96 97 char hostname[65]; 98 EXPECT_THAT(gethostname(hostname, sizeof(hostname)), SyscallSucceeds()); 99 }); 100 thread.Join(); 101 102 struct utsname after; 103 EXPECT_THAT(uname(&after), SyscallSucceeds()); 104 EXPECT_EQ(absl::string_view(after.nodename), init.nodename); 105 } 106 107 } // namespace 108 109 } // namespace testing 110 } // namespace gvisor