1 // Copyright 2023 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "base/posix/sysctl.h" 6 7 #include <sys/sysctl.h> 8 9 #include "build/build_config.h" 10 #include "testing/gtest/include/gtest/gtest.h" 11 12 namespace base { 13 14 namespace { 15 16 using SysctlTest = testing::Test; 17 TEST(SysctlTest,MibSuccess)18TEST(SysctlTest, MibSuccess) { 19 std::optional<std::string> result1 = StringSysctl({CTL_HW, HW_MACHINE}); 20 EXPECT_TRUE(result1); 21 22 #if !BUILDFLAG(IS_OPENBSD) 23 std::optional<std::string> result2 = StringSysctlByName("hw.machine"); 24 EXPECT_TRUE(result2); 25 26 EXPECT_EQ(result1, result2); 27 #endif 28 } 29 TEST(SysctlTest,MibFailure)30TEST(SysctlTest, MibFailure) { 31 std::optional<std::string> result = StringSysctl({-1}); 32 EXPECT_FALSE(result); 33 34 #if !BUILDFLAG(IS_OPENBSD) 35 result = StringSysctlByName("banananananananana"); 36 EXPECT_FALSE(result); 37 #endif 38 } 39 40 } // namespace 41 42 } // namespace base 43