1 // Copyright 2012 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/system/sys_info.h"
6
7 #include <stdint.h>
8
9 #include <optional>
10 #include <utility>
11 #include <vector>
12
13 #include "base/environment.h"
14 #include "base/files/file_util.h"
15 #include "base/functional/bind.h"
16 #include "base/numerics/safe_conversions.h"
17 #include "base/process/process_metrics.h"
18 #include "base/run_loop.h"
19 #include "base/strings/pattern.h"
20 #include "base/strings/string_number_conversions.h"
21 #include "base/strings/string_split.h"
22 #include "base/strings/string_util.h"
23 #include "base/strings/sys_string_conversions.h"
24 #include "base/strings/utf_string_conversions.h"
25 #include "base/test/scoped_chromeos_version_info.h"
26 #include "base/test/scoped_running_on_chromeos.h"
27 #include "base/test/task_environment.h"
28 #include "base/threading/platform_thread.h"
29 #include "base/threading/scoped_blocking_call.h"
30 #include "base/time/time.h"
31 #include "build/build_config.h"
32 #include "testing/gtest/include/gtest/gtest-death-test.h"
33 #include "testing/gtest/include/gtest/gtest.h"
34 #include "testing/platform_test.h"
35
36 #if BUILDFLAG(IS_WIN)
37 #include "base/win/com_init_util.h"
38 #include "base/win/scoped_bstr.h"
39 #include "base/win/scoped_com_initializer.h"
40 #include "base/win/scoped_variant.h"
41 #include "base/win/wmi.h"
42 #endif // BUILDFLAG(IS_WIN)
43
44 #if BUILDFLAG(IS_MAC)
45 #include "base/system/sys_info_internal.h"
46 #include "base/test/scoped_feature_list.h"
47 #endif // BUILDFLAG(IS_MAC)
48
49 namespace base {
50
51 #if BUILDFLAG(IS_ANDROID)
52 // Some Android (Cast) test devices have a large portion of physical memory
53 // reserved. During investigation, around 115-150 MB were seen reserved, so we
54 // track this here with a factory of safety of 2.
55 static constexpr int kReservedPhysicalMemory = 300 * 1024; // In _K_bytes.
56 #endif // BUILDFLAG(IS_ANDROID)
57
58 using SysInfoTest = PlatformTest;
59
TEST_F(SysInfoTest,NumProcs)60 TEST_F(SysInfoTest, NumProcs) {
61 // We aren't actually testing that it's correct, just that it's sane.
62 EXPECT_GE(SysInfo::NumberOfProcessors(), 1);
63
64 EXPECT_GE(SysInfo::NumberOfEfficientProcessors(), 0);
65 EXPECT_LT(SysInfo::NumberOfEfficientProcessors(),
66 SysInfo::NumberOfProcessors());
67 }
68
69 #if BUILDFLAG(IS_MAC)
TEST_F(SysInfoTest,NumProcsWithSecurityMitigationEnabled)70 TEST_F(SysInfoTest, NumProcsWithSecurityMitigationEnabled) {
71 // Reset state so that the call to SetCpuSecurityMitigationsEnabled() below
72 // succeeds even if SysInfo::NumberOfProcessors() was previously called.
73 SysInfo::ResetCpuSecurityMitigationsEnabledForTesting();
74
75 // Verify that the number of number of available processors available when CPU
76 // security mitigation is enabled is the number of available "physical"
77 // processors.
78 test::ScopedFeatureList feature_list_;
79 feature_list_.InitAndEnableFeature(kNumberOfCoresWithCpuSecurityMitigation);
80 SysInfo::SetCpuSecurityMitigationsEnabled();
81 EXPECT_EQ(SysInfo::NumberOfProcessors(),
82 internal::NumberOfPhysicalProcessors());
83
84 // Reset state set by this test.
85 SysInfo::ResetCpuSecurityMitigationsEnabledForTesting();
86 }
87 #endif // BUILDFLAG(IS_MAC)
88
TEST_F(SysInfoTest,AmountOfMem)89 TEST_F(SysInfoTest, AmountOfMem) {
90 // We aren't actually testing that it's correct, just that it's sane.
91 EXPECT_GT(SysInfo::AmountOfPhysicalMemory(), 0u);
92 EXPECT_GT(SysInfo::AmountOfPhysicalMemoryMB(), 0);
93 // The maxmimal amount of virtual memory can be zero which means unlimited.
94 EXPECT_GE(SysInfo::AmountOfVirtualMemory(), 0u);
95 }
96
97 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
98 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
99 #define MAYBE_AmountOfAvailablePhysicalMemory \
100 DISABLED_AmountOfAvailablePhysicalMemory
101 #else
102 #define MAYBE_AmountOfAvailablePhysicalMemory AmountOfAvailablePhysicalMemory
103 #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
TEST_F(SysInfoTest,MAYBE_AmountOfAvailablePhysicalMemory)104 TEST_F(SysInfoTest, MAYBE_AmountOfAvailablePhysicalMemory) {
105 // Note: info is in _K_bytes.
106 SystemMemoryInfoKB info;
107 ASSERT_TRUE(GetSystemMemoryInfo(&info));
108 EXPECT_GT(info.free, 0);
109 if (info.available != 0) {
110 // If there is MemAvailable from kernel.
111 EXPECT_LT(info.available, info.total);
112 const uint64_t amount = SysInfo::AmountOfAvailablePhysicalMemory(info);
113 // We aren't actually testing that it's correct, just that it's sane.
114 // Available memory is |free - reserved + reclaimable (inactive, non-free)|.
115 // On some android platforms, reserved is a substantial portion.
116 const int available =
117 #if BUILDFLAG(IS_ANDROID)
118 std::max(info.free - kReservedPhysicalMemory, 0);
119 #else
120 info.free;
121 #endif // BUILDFLAG(IS_ANDROID)
122 EXPECT_GT(amount, checked_cast<uint64_t>(available) * 1024);
123 EXPECT_LT(amount / 1024, checked_cast<uint64_t>(info.available));
124 // Simulate as if there is no MemAvailable.
125 info.available = 0;
126 }
127
128 // There is no MemAvailable. Check the fallback logic.
129 const uint64_t amount = SysInfo::AmountOfAvailablePhysicalMemory(info);
130 // We aren't actually testing that it's correct, just that it's sane.
131 EXPECT_GT(amount, checked_cast<uint64_t>(info.free) * 1024);
132 EXPECT_LT(amount / 1024, checked_cast<uint64_t>(info.total));
133 }
134 #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) ||
135 // BUILDFLAG(IS_ANDROID)
136
TEST_F(SysInfoTest,AmountOfFreeDiskSpace)137 TEST_F(SysInfoTest, AmountOfFreeDiskSpace) {
138 // We aren't actually testing that it's correct, just that it's sane.
139 FilePath tmp_path;
140 ASSERT_TRUE(GetTempDir(&tmp_path));
141 EXPECT_GE(SysInfo::AmountOfFreeDiskSpace(tmp_path), 0) << tmp_path;
142 }
143
TEST_F(SysInfoTest,AmountOfTotalDiskSpace)144 TEST_F(SysInfoTest, AmountOfTotalDiskSpace) {
145 // We aren't actually testing that it's correct, just that it's sane.
146 FilePath tmp_path;
147 ASSERT_TRUE(GetTempDir(&tmp_path));
148 EXPECT_GT(SysInfo::AmountOfTotalDiskSpace(tmp_path), 0) << tmp_path;
149 }
150
151 #if BUILDFLAG(IS_FUCHSIA)
152 // Verify that specifying total disk space for nested directories matches
153 // the deepest-nested.
TEST_F(SysInfoTest,NestedVolumesAmountOfTotalDiskSpace)154 TEST_F(SysInfoTest, NestedVolumesAmountOfTotalDiskSpace) {
155 constexpr int64_t kOuterVolumeQuota = 1024;
156 constexpr int64_t kInnerVolumeQuota = kOuterVolumeQuota / 2;
157
158 FilePath tmp_path;
159 ASSERT_TRUE(GetTempDir(&tmp_path));
160 SysInfo::SetAmountOfTotalDiskSpace(tmp_path, kOuterVolumeQuota);
161 const FilePath subdirectory_path = tmp_path.Append("subdirectory");
162 SysInfo::SetAmountOfTotalDiskSpace(subdirectory_path, kInnerVolumeQuota);
163
164 EXPECT_EQ(SysInfo::AmountOfTotalDiskSpace(tmp_path), kOuterVolumeQuota);
165 EXPECT_EQ(SysInfo::AmountOfTotalDiskSpace(subdirectory_path),
166 kInnerVolumeQuota);
167
168 // Remove the inner directory quota setting and check again.
169 SysInfo::SetAmountOfTotalDiskSpace(subdirectory_path, -1);
170 EXPECT_EQ(SysInfo::AmountOfTotalDiskSpace(subdirectory_path),
171 kOuterVolumeQuota);
172 }
173 #endif // BUILDFLAG(IS_FUCHSIA)
174
175 #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_LINUX) || \
176 BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_FUCHSIA)
177
TEST_F(SysInfoTest,OperatingSystemVersion)178 TEST_F(SysInfoTest, OperatingSystemVersion) {
179 std::string version = SysInfo::OperatingSystemVersion();
180 EXPECT_FALSE(version.empty());
181 }
182
TEST_F(SysInfoTest,OperatingSystemVersionNumbers)183 TEST_F(SysInfoTest, OperatingSystemVersionNumbers) {
184 int32_t os_major_version = -1;
185 int32_t os_minor_version = -1;
186 int32_t os_bugfix_version = -1;
187 SysInfo::OperatingSystemVersionNumbers(&os_major_version, &os_minor_version,
188 &os_bugfix_version);
189 EXPECT_GT(os_major_version, -1);
190 EXPECT_GT(os_minor_version, -1);
191 EXPECT_GT(os_bugfix_version, -1);
192 }
193 #endif
194
195 #if BUILDFLAG(IS_IOS)
TEST_F(SysInfoTest,GetIOSBuildNumber)196 TEST_F(SysInfoTest, GetIOSBuildNumber) {
197 std::string build_number(SysInfo::GetIOSBuildNumber());
198 EXPECT_GT(build_number.length(), 0U);
199 }
200 #endif // BUILDFLAG(IS_IOS)
201
TEST_F(SysInfoTest,Uptime)202 TEST_F(SysInfoTest, Uptime) {
203 TimeDelta up_time_1 = SysInfo::Uptime();
204 // UpTime() is implemented internally using TimeTicks::Now(), which documents
205 // system resolution as being 1-15ms. Sleep a little longer than that.
206 PlatformThread::Sleep(Milliseconds(20));
207 TimeDelta up_time_2 = SysInfo::Uptime();
208 EXPECT_GT(up_time_1.InMicroseconds(), 0);
209 EXPECT_GT(up_time_2.InMicroseconds(), up_time_1.InMicroseconds());
210 }
211
212 #if BUILDFLAG(IS_APPLE)
TEST_F(SysInfoTest,HardwareModelNameFormatMacAndiOS)213 TEST_F(SysInfoTest, HardwareModelNameFormatMacAndiOS) {
214 std::string hardware_model = SysInfo::HardwareModelName();
215 ASSERT_FALSE(hardware_model.empty());
216
217 // Check that the model is of the expected format, which is different on iOS
218 // simulators and real iOS / MacOS devices.
219 #if BUILDFLAG(IS_IOS) && TARGET_OS_SIMULATOR
220 // On iOS simulators, the device model looks like "iOS Simulator (Foo[,Bar])"
221 // where Foo is either "Unknown", "iPhone" or "iPad", and Bar, if present, is
222 // a number.
223 EXPECT_TRUE(base::MatchPattern(hardware_model, "iOS Simulator (*)"))
224 << hardware_model;
225 std::vector<StringPiece> mainPieces =
226 SplitStringPiece(hardware_model, "()", KEEP_WHITESPACE, SPLIT_WANT_ALL);
227 ASSERT_EQ(3u, mainPieces.size()) << hardware_model;
228 std::vector<StringPiece> modelPieces =
229 SplitStringPiece(mainPieces[1], ",", KEEP_WHITESPACE, SPLIT_WANT_ALL);
230 ASSERT_GE(modelPieces.size(), 1u) << hardware_model;
231 if (modelPieces.size() == 1u) {
232 EXPECT_TRUE(modelPieces[0] == "Unknown" || modelPieces[0] == "iPhone" ||
233 modelPieces[0] == "iPad")
234 << hardware_model;
235 } else {
236 int value;
237 EXPECT_TRUE(StringToInt(modelPieces[1], &value)) << hardware_model;
238 }
239 #else
240 // The expected format is "Foo,Bar" where Foo is "iPhone" or "iPad" and Bar is
241 // a number.
242 std::vector<StringPiece> pieces =
243 SplitStringPiece(hardware_model, ",", KEEP_WHITESPACE, SPLIT_WANT_ALL);
244 ASSERT_EQ(2u, pieces.size()) << hardware_model;
245 int value;
246 EXPECT_TRUE(StringToInt(pieces[1], &value)) << hardware_model;
247 #endif // BUILDFLAG(IS_IOS) && TARGET_OS_SIMULATOR
248 }
249 #endif // BUILDFLAG(IS_APPLE)
250
TEST_F(SysInfoTest,GetHardwareInfo)251 TEST_F(SysInfoTest, GetHardwareInfo) {
252 test::TaskEnvironment task_environment;
253 std::optional<SysInfo::HardwareInfo> hardware_info;
254
255 auto callback = base::BindOnce(
256 [](std::optional<SysInfo::HardwareInfo>* target_info,
257 SysInfo::HardwareInfo info) { *target_info = std::move(info); },
258 &hardware_info);
259 SysInfo::GetHardwareInfo(std::move(callback));
260 task_environment.RunUntilIdle();
261
262 ASSERT_TRUE(hardware_info.has_value());
263 EXPECT_TRUE(IsStringUTF8(hardware_info->manufacturer));
264 EXPECT_TRUE(IsStringUTF8(hardware_info->model));
265 bool empty_result_expected =
266 #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_WIN) || \
267 BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_FUCHSIA)
268 false;
269 #else
270 true;
271 #endif
272 EXPECT_EQ(hardware_info->manufacturer.empty(), empty_result_expected);
273 EXPECT_EQ(hardware_info->model.empty(), empty_result_expected);
274 }
275
276 #if BUILDFLAG(IS_WIN)
TEST_F(SysInfoTest,GetHardwareInfoWMIMatchRegistry)277 TEST_F(SysInfoTest, GetHardwareInfoWMIMatchRegistry) {
278 base::win::ScopedCOMInitializer com_initializer;
279 test::TaskEnvironment task_environment;
280 std::optional<SysInfo::HardwareInfo> hardware_info;
281
282 auto callback = base::BindOnce(
283 [](std::optional<SysInfo::HardwareInfo>* target_info,
284 SysInfo::HardwareInfo info) { *target_info = std::move(info); },
285 &hardware_info);
286 SysInfo::GetHardwareInfo(std::move(callback));
287 task_environment.RunUntilIdle();
288
289 ASSERT_TRUE(hardware_info.has_value());
290
291 Microsoft::WRL::ComPtr<IWbemServices> wmi_services;
292 EXPECT_TRUE(base::win::CreateLocalWmiConnection(true, &wmi_services));
293
294 static constexpr wchar_t query_computer_system[] =
295 L"SELECT Manufacturer,Model FROM Win32_ComputerSystem";
296
297 Microsoft::WRL::ComPtr<IEnumWbemClassObject> enumerator_computer_system;
298 HRESULT hr = wmi_services->ExecQuery(
299 base::win::ScopedBstr(L"WQL").Get(),
300 base::win::ScopedBstr(query_computer_system).Get(),
301 WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, nullptr,
302 &enumerator_computer_system);
303 EXPECT_FALSE(FAILED(hr) || !enumerator_computer_system.Get());
304
305 Microsoft::WRL::ComPtr<IWbemClassObject> class_object;
306 ULONG items_returned = 0;
307 hr = enumerator_computer_system->Next(WBEM_INFINITE, 1, &class_object,
308 &items_returned);
309 EXPECT_FALSE(FAILED(hr) || !items_returned);
310
311 base::win::ScopedVariant manufacturerVar;
312 std::wstring manufacturer;
313 hr = class_object->Get(L"Manufacturer", 0, manufacturerVar.Receive(), nullptr,
314 nullptr);
315 if (SUCCEEDED(hr) && manufacturerVar.type() == VT_BSTR) {
316 manufacturer.assign(V_BSTR(manufacturerVar.ptr()),
317 ::SysStringLen(V_BSTR(manufacturerVar.ptr())));
318 }
319 base::win::ScopedVariant modelVar;
320 std::wstring model;
321 hr = class_object->Get(L"Model", 0, modelVar.Receive(), nullptr, nullptr);
322 if (SUCCEEDED(hr) && modelVar.type() == VT_BSTR) {
323 model.assign(V_BSTR(modelVar.ptr()),
324 ::SysStringLen(V_BSTR(modelVar.ptr())));
325 }
326
327 EXPECT_TRUE(hardware_info->manufacturer == base::SysWideToUTF8(manufacturer));
328 EXPECT_TRUE(hardware_info->model == base::SysWideToUTF8(model));
329 }
330 #endif
331
332 #if BUILDFLAG(IS_CHROMEOS)
333
TEST_F(SysInfoTest,GoogleChromeOSVersionNumbers)334 TEST_F(SysInfoTest, GoogleChromeOSVersionNumbers) {
335 int32_t os_major_version = -1;
336 int32_t os_minor_version = -1;
337 int32_t os_bugfix_version = -1;
338 const char kLsbRelease[] =
339 "FOO=1234123.34.5\n"
340 "CHROMEOS_RELEASE_VERSION=1.2.3.4\n";
341 test::ScopedChromeOSVersionInfo version(kLsbRelease, Time());
342 SysInfo::OperatingSystemVersionNumbers(&os_major_version, &os_minor_version,
343 &os_bugfix_version);
344 EXPECT_EQ(1, os_major_version);
345 EXPECT_EQ(2, os_minor_version);
346 EXPECT_EQ(3, os_bugfix_version);
347 }
348
TEST_F(SysInfoTest,GoogleChromeOSVersionNumbersFirst)349 TEST_F(SysInfoTest, GoogleChromeOSVersionNumbersFirst) {
350 int32_t os_major_version = -1;
351 int32_t os_minor_version = -1;
352 int32_t os_bugfix_version = -1;
353 const char kLsbRelease[] =
354 "CHROMEOS_RELEASE_VERSION=1.2.3.4\n"
355 "FOO=1234123.34.5\n";
356 test::ScopedChromeOSVersionInfo version(kLsbRelease, Time());
357 SysInfo::OperatingSystemVersionNumbers(&os_major_version, &os_minor_version,
358 &os_bugfix_version);
359 EXPECT_EQ(1, os_major_version);
360 EXPECT_EQ(2, os_minor_version);
361 EXPECT_EQ(3, os_bugfix_version);
362 }
363
TEST_F(SysInfoTest,GoogleChromeOSNoVersionNumbers)364 TEST_F(SysInfoTest, GoogleChromeOSNoVersionNumbers) {
365 int32_t os_major_version = -1;
366 int32_t os_minor_version = -1;
367 int32_t os_bugfix_version = -1;
368 const char kLsbRelease[] = "FOO=1234123.34.5\n";
369 test::ScopedChromeOSVersionInfo version(kLsbRelease, Time());
370 SysInfo::OperatingSystemVersionNumbers(&os_major_version, &os_minor_version,
371 &os_bugfix_version);
372 EXPECT_EQ(0, os_major_version);
373 EXPECT_EQ(0, os_minor_version);
374 EXPECT_EQ(0, os_bugfix_version);
375 }
376
TEST_F(SysInfoTest,GoogleChromeOSLsbReleaseTime)377 TEST_F(SysInfoTest, GoogleChromeOSLsbReleaseTime) {
378 const char kLsbRelease[] = "CHROMEOS_RELEASE_VERSION=1.2.3.4";
379 // Use a fake time that can be safely displayed as a string.
380 const Time lsb_release_time(Time::FromSecondsSinceUnixEpoch(12345.6));
381 test::ScopedChromeOSVersionInfo version(kLsbRelease, lsb_release_time);
382 Time parsed_lsb_release_time = SysInfo::GetLsbReleaseTime();
383 EXPECT_DOUBLE_EQ(lsb_release_time.InSecondsFSinceUnixEpoch(),
384 parsed_lsb_release_time.InSecondsFSinceUnixEpoch());
385 }
386
TEST_F(SysInfoTest,IsRunningOnChromeOS)387 TEST_F(SysInfoTest, IsRunningOnChromeOS) {
388 {
389 const char kLsbRelease1[] =
390 "CHROMEOS_RELEASE_NAME=Non Chrome OS\n"
391 "CHROMEOS_RELEASE_VERSION=1.2.3.4\n";
392 test::ScopedChromeOSVersionInfo version(kLsbRelease1, Time());
393 EXPECT_FALSE(SysInfo::IsRunningOnChromeOS());
394 }
395 {
396 const char kLsbRelease2[] =
397 "CHROMEOS_RELEASE_NAME=Chrome OS\n"
398 "CHROMEOS_RELEASE_VERSION=1.2.3.4\n";
399 test::ScopedChromeOSVersionInfo version(kLsbRelease2, Time());
400 EXPECT_TRUE(SysInfo::IsRunningOnChromeOS());
401 }
402 {
403 const char kLsbRelease3[] = "CHROMEOS_RELEASE_NAME=Chromium OS\n";
404 test::ScopedChromeOSVersionInfo version(kLsbRelease3, Time());
405 EXPECT_TRUE(SysInfo::IsRunningOnChromeOS());
406 }
407 }
408
409 // Regression test for https://crbug.com/1148904.
TEST_F(SysInfoTest,ScopedChromeOSVersionInfoDoesNotChangeEnvironment)410 TEST_F(SysInfoTest, ScopedChromeOSVersionInfoDoesNotChangeEnvironment) {
411 std::unique_ptr<Environment> environment = Environment::Create();
412 ASSERT_FALSE(environment->HasVar("LSB_RELEASE"));
413 {
414 const char kLsbRelease[] =
415 "CHROMEOS_RELEASE_NAME=Chrome OS\n"
416 "CHROMEOS_RELEASE_VERSION=1.2.3.4\n";
417 test::ScopedChromeOSVersionInfo version(kLsbRelease, Time());
418 }
419 EXPECT_FALSE(environment->HasVar("LSB_RELEASE"));
420 }
421
TEST_F(SysInfoTest,CrashOnBaseImage)422 TEST_F(SysInfoTest, CrashOnBaseImage) {
423 const char kLsbRelease[] =
424 "CHROMEOS_RELEASE_NAME=Chrome OS\n"
425 "CHROMEOS_RELEASE_VERSION=1.2.3.4\n"
426 "CHROMEOS_RELEASE_TRACK=stable-channel\n";
427 test::ScopedChromeOSVersionInfo version(kLsbRelease, Time());
428 EXPECT_TRUE(SysInfo::IsRunningOnChromeOS());
429 EXPECT_DEATH_IF_SUPPORTED({ SysInfo::CrashIfChromeOSNonTestImage(); }, "");
430 }
431
TEST_F(SysInfoTest,NoCrashOnTestImage)432 TEST_F(SysInfoTest, NoCrashOnTestImage) {
433 const char kLsbRelease[] =
434 "CHROMEOS_RELEASE_NAME=Chrome OS\n"
435 "CHROMEOS_RELEASE_VERSION=1.2.3.4\n"
436 "CHROMEOS_RELEASE_TRACK=testimage-channel\n";
437 test::ScopedChromeOSVersionInfo version(kLsbRelease, Time());
438 EXPECT_TRUE(SysInfo::IsRunningOnChromeOS());
439 // Should not crash.
440 SysInfo::CrashIfChromeOSNonTestImage();
441 }
442
TEST_F(SysInfoTest,NoCrashOnLinuxBuild)443 TEST_F(SysInfoTest, NoCrashOnLinuxBuild) {
444 test::ScopedChromeOSVersionInfo version("", Time());
445 EXPECT_FALSE(SysInfo::IsRunningOnChromeOS());
446 // Should not crash.
447 SysInfo::CrashIfChromeOSNonTestImage();
448 }
449
TEST_F(SysInfoTest,ScopedRunningOnChromeOS)450 TEST_F(SysInfoTest, ScopedRunningOnChromeOS) {
451 // base_unittests run both on linux-chromeos and actual devices, so the
452 // initial state of IsRunningOnChromeOS may vary.
453 bool was_running = SysInfo::IsRunningOnChromeOS();
454 {
455 test::ScopedRunningOnChromeOS running_on_chromeos;
456 EXPECT_TRUE(SysInfo::IsRunningOnChromeOS());
457 }
458 // Previous value restored.
459 EXPECT_EQ(was_running, SysInfo::IsRunningOnChromeOS());
460 }
461
462 #endif // BUILDFLAG(IS_CHROMEOS)
463
464 } // namespace base
465