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