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