xref: /aosp_15_r20/external/cronet/base/posix/sysctl_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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)18 TEST(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)30 TEST(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