• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
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 <stdint.h>
6 
7 #include "base/environment.h"
8 #include "base/files/file_util.h"
9 #include "base/process/process_metrics.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_split.h"
12 #include "base/sys_info.h"
13 #include "base/threading/platform_thread.h"
14 #include "base/time/time.h"
15 #include "build/build_config.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "testing/platform_test.h"
18 
19 namespace base {
20 
21 using SysInfoTest = PlatformTest;
22 
TEST_F(SysInfoTest,NumProcs)23 TEST_F(SysInfoTest, NumProcs) {
24   // We aren't actually testing that it's correct, just that it's sane.
25   EXPECT_GE(SysInfo::NumberOfProcessors(), 1);
26 }
27 
TEST_F(SysInfoTest,AmountOfMem)28 TEST_F(SysInfoTest, AmountOfMem) {
29   // We aren't actually testing that it's correct, just that it's sane.
30   EXPECT_GT(SysInfo::AmountOfPhysicalMemory(), 0);
31   EXPECT_GT(SysInfo::AmountOfPhysicalMemoryMB(), 0);
32   // The maxmimal amount of virtual memory can be zero which means unlimited.
33   EXPECT_GE(SysInfo::AmountOfVirtualMemory(), 0);
34 }
35 
36 #if defined(OS_LINUX) || defined(OS_ANDROID)
37 #if defined(OS_LINUX)
38 #define MAYBE_AmountOfAvailablePhysicalMemory \
39   DISABLED_AmountOfAvailablePhysicalMemory
40 #else
41 #define MAYBE_AmountOfAvailablePhysicalMemory AmountOfAvailablePhysicalMemory
42 #endif  // defined(OS_LINUX)
TEST_F(SysInfoTest,MAYBE_AmountOfAvailablePhysicalMemory)43 TEST_F(SysInfoTest, MAYBE_AmountOfAvailablePhysicalMemory) {
44   // Note: info is in _K_bytes.
45   SystemMemoryInfoKB info;
46   ASSERT_TRUE(GetSystemMemoryInfo(&info));
47   EXPECT_GT(info.free, 0);
48 
49   if (info.available != 0) {
50     // If there is MemAvailable from kernel.
51     EXPECT_LT(info.available, info.total);
52     const int64_t amount = SysInfo::AmountOfAvailablePhysicalMemory(info);
53     // We aren't actually testing that it's correct, just that it's sane.
54     EXPECT_GT(amount, static_cast<int64_t>(info.free) * 1024);
55     EXPECT_LT(amount / 1024, info.available);
56     // Simulate as if there is no MemAvailable.
57     info.available = 0;
58   }
59 
60   // There is no MemAvailable. Check the fallback logic.
61   const int64_t amount = SysInfo::AmountOfAvailablePhysicalMemory(info);
62   // We aren't actually testing that it's correct, just that it's sane.
63   EXPECT_GT(amount, static_cast<int64_t>(info.free) * 1024);
64   EXPECT_LT(amount / 1024, info.total);
65 }
66 #endif  // defined(OS_LINUX) || defined(OS_ANDROID)
67 
68 #if defined(OS_FUCHSIA)
69 // TODO(crbug.com/851734): Implementation depends on statvfs, which is not
70 // implemented on Fuchsia
71 #define MAYBE_AmountOfFreeDiskSpace DISABLED_AmountOfFreeDiskSpace
72 #else
73 #define MAYBE_AmountOfFreeDiskSpace AmountOfFreeDiskSpace
74 #endif
TEST_F(SysInfoTest,MAYBE_AmountOfFreeDiskSpace)75 TEST_F(SysInfoTest, MAYBE_AmountOfFreeDiskSpace) {
76   // We aren't actually testing that it's correct, just that it's sane.
77   FilePath tmp_path;
78   ASSERT_TRUE(GetTempDir(&tmp_path));
79   EXPECT_GE(SysInfo::AmountOfFreeDiskSpace(tmp_path), 0) << tmp_path.value();
80 }
81 
82 #if defined(OS_FUCHSIA)
83 // TODO(crbug.com/851734): Implementation depends on statvfs, which is not
84 // implemented on Fuchsia
85 #define MAYBE_AmountOfTotalDiskSpace DISABLED_AmountOfTotalDiskSpace
86 #else
87 #define MAYBE_AmountOfTotalDiskSpace AmountOfTotalDiskSpace
88 #endif
TEST_F(SysInfoTest,MAYBE_AmountOfTotalDiskSpace)89 TEST_F(SysInfoTest, MAYBE_AmountOfTotalDiskSpace) {
90   // We aren't actually testing that it's correct, just that it's sane.
91   FilePath tmp_path;
92   ASSERT_TRUE(GetTempDir(&tmp_path));
93   EXPECT_GT(SysInfo::AmountOfTotalDiskSpace(tmp_path), 0) << tmp_path.value();
94 }
95 
96 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX)
TEST_F(SysInfoTest,OperatingSystemVersionNumbers)97 TEST_F(SysInfoTest, OperatingSystemVersionNumbers) {
98   int32_t os_major_version = -1;
99   int32_t os_minor_version = -1;
100   int32_t os_bugfix_version = -1;
101   SysInfo::OperatingSystemVersionNumbers(&os_major_version,
102                                          &os_minor_version,
103                                          &os_bugfix_version);
104   EXPECT_GT(os_major_version, -1);
105   EXPECT_GT(os_minor_version, -1);
106   EXPECT_GT(os_bugfix_version, -1);
107 }
108 #endif
109 
TEST_F(SysInfoTest,Uptime)110 TEST_F(SysInfoTest, Uptime) {
111   TimeDelta up_time_1 = SysInfo::Uptime();
112   // UpTime() is implemented internally using TimeTicks::Now(), which documents
113   // system resolution as being 1-15ms. Sleep a little longer than that.
114   PlatformThread::Sleep(TimeDelta::FromMilliseconds(20));
115   TimeDelta up_time_2 = SysInfo::Uptime();
116   EXPECT_GT(up_time_1.InMicroseconds(), 0);
117   EXPECT_GT(up_time_2.InMicroseconds(), up_time_1.InMicroseconds());
118 }
119 
120 #if defined(OS_MACOSX)
TEST_F(SysInfoTest,HardwareModelNameFormatMacAndiOS)121 TEST_F(SysInfoTest, HardwareModelNameFormatMacAndiOS) {
122   std::string hardware_model = SysInfo::HardwareModelName();
123   ASSERT_FALSE(hardware_model.empty());
124   // Check that the model is of the expected format "Foo,Bar" where "Bar" is
125   // a number.
126   std::vector<StringPiece> pieces =
127       SplitStringPiece(hardware_model, ",", KEEP_WHITESPACE, SPLIT_WANT_ALL);
128   ASSERT_EQ(2u, pieces.size()) << hardware_model;
129   int value;
130   EXPECT_TRUE(StringToInt(pieces[1], &value)) << hardware_model;
131 }
132 #endif
133 
134 #if defined(OS_CHROMEOS)
135 
TEST_F(SysInfoTest,GoogleChromeOSVersionNumbers)136 TEST_F(SysInfoTest, GoogleChromeOSVersionNumbers) {
137   int32_t os_major_version = -1;
138   int32_t os_minor_version = -1;
139   int32_t os_bugfix_version = -1;
140   const char kLsbRelease[] =
141       "FOO=1234123.34.5\n"
142       "CHROMEOS_RELEASE_VERSION=1.2.3.4\n";
143   SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease, Time());
144   SysInfo::OperatingSystemVersionNumbers(&os_major_version,
145                                          &os_minor_version,
146                                          &os_bugfix_version);
147   EXPECT_EQ(1, os_major_version);
148   EXPECT_EQ(2, os_minor_version);
149   EXPECT_EQ(3, os_bugfix_version);
150 }
151 
TEST_F(SysInfoTest,GoogleChromeOSVersionNumbersFirst)152 TEST_F(SysInfoTest, GoogleChromeOSVersionNumbersFirst) {
153   int32_t os_major_version = -1;
154   int32_t os_minor_version = -1;
155   int32_t os_bugfix_version = -1;
156   const char kLsbRelease[] =
157       "CHROMEOS_RELEASE_VERSION=1.2.3.4\n"
158       "FOO=1234123.34.5\n";
159   SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease, Time());
160   SysInfo::OperatingSystemVersionNumbers(&os_major_version,
161                                          &os_minor_version,
162                                          &os_bugfix_version);
163   EXPECT_EQ(1, os_major_version);
164   EXPECT_EQ(2, os_minor_version);
165   EXPECT_EQ(3, os_bugfix_version);
166 }
167 
TEST_F(SysInfoTest,GoogleChromeOSNoVersionNumbers)168 TEST_F(SysInfoTest, GoogleChromeOSNoVersionNumbers) {
169   int32_t os_major_version = -1;
170   int32_t os_minor_version = -1;
171   int32_t os_bugfix_version = -1;
172   const char kLsbRelease[] = "FOO=1234123.34.5\n";
173   SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease, Time());
174   SysInfo::OperatingSystemVersionNumbers(&os_major_version,
175                                          &os_minor_version,
176                                          &os_bugfix_version);
177   EXPECT_EQ(0, os_major_version);
178   EXPECT_EQ(0, os_minor_version);
179   EXPECT_EQ(0, os_bugfix_version);
180 }
181 
TEST_F(SysInfoTest,GoogleChromeOSLsbReleaseTime)182 TEST_F(SysInfoTest, GoogleChromeOSLsbReleaseTime) {
183   const char kLsbRelease[] = "CHROMEOS_RELEASE_VERSION=1.2.3.4";
184   // Use a fake time that can be safely displayed as a string.
185   const Time lsb_release_time(Time::FromDoubleT(12345.6));
186   SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease, lsb_release_time);
187   Time parsed_lsb_release_time = SysInfo::GetLsbReleaseTime();
188   EXPECT_DOUBLE_EQ(lsb_release_time.ToDoubleT(),
189                    parsed_lsb_release_time.ToDoubleT());
190 }
191 
TEST_F(SysInfoTest,IsRunningOnChromeOS)192 TEST_F(SysInfoTest, IsRunningOnChromeOS) {
193   SysInfo::SetChromeOSVersionInfoForTest("", Time());
194   EXPECT_FALSE(SysInfo::IsRunningOnChromeOS());
195 
196   const char kLsbRelease1[] =
197       "CHROMEOS_RELEASE_NAME=Non Chrome OS\n"
198       "CHROMEOS_RELEASE_VERSION=1.2.3.4\n";
199   SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease1, Time());
200   EXPECT_FALSE(SysInfo::IsRunningOnChromeOS());
201 
202   const char kLsbRelease2[] =
203       "CHROMEOS_RELEASE_NAME=Chrome OS\n"
204       "CHROMEOS_RELEASE_VERSION=1.2.3.4\n";
205   SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease2, Time());
206   EXPECT_TRUE(SysInfo::IsRunningOnChromeOS());
207 
208   const char kLsbRelease3[] =
209       "CHROMEOS_RELEASE_NAME=Chromium OS\n";
210   SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease3, Time());
211   EXPECT_TRUE(SysInfo::IsRunningOnChromeOS());
212 }
213 
214 #endif  // OS_CHROMEOS
215 
216 }  // namespace base
217