nileshagrawal@chromium.org | a049e95 | 2012-07-11 08:24:43 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
deanm@chromium.org | 42a34eb | 2008-09-17 19:09:39 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 5 | #include <stdint.h> |
| 6 | |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 7 | #include "base/environment.h" |
brettw@chromium.org | 01f3da4 | 2014-08-14 05:22:14 +0900 | [diff] [blame] | 8 | #include "base/files/file_util.h" |
mkolom | af6f3f8 | 2017-03-22 10:47:29 +0900 | [diff] [blame] | 9 | #include "base/process/process_metrics.h" |
deanm@chromium.org | 42a34eb | 2008-09-17 19:09:39 +0900 | [diff] [blame] | 10 | #include "base/sys_info.h" |
jeremy@chromium.org | de658ed | 2012-10-29 10:04:09 +0900 | [diff] [blame] | 11 | #include "base/threading/platform_thread.h" |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 12 | #include "base/time/time.h" |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 13 | #include "build/build_config.h" |
deanm@chromium.org | 42a34eb | 2008-09-17 19:09:39 +0900 | [diff] [blame] | 14 | #include "testing/gtest/include/gtest/gtest.h" |
jeremy@chromium.org | 0d8eba7 | 2008-12-03 04:20:15 +0900 | [diff] [blame] | 15 | #include "testing/platform_test.h" |
deanm@chromium.org | 42a34eb | 2008-09-17 19:09:39 +0900 | [diff] [blame] | 16 | |
mkolom | af6f3f8 | 2017-03-22 10:47:29 +0900 | [diff] [blame] | 17 | namespace base { |
| 18 | |
| 19 | using SysInfoTest = PlatformTest; |
mark@chromium.org | 3b2ec6f | 2008-09-26 05:33:42 +0900 | [diff] [blame] | 20 | |
| 21 | TEST_F(SysInfoTest, NumProcs) { |
deanm@chromium.org | 42a34eb | 2008-09-17 19:09:39 +0900 | [diff] [blame] | 22 | // We aren't actually testing that it's correct, just that it's sane. |
mkolom | af6f3f8 | 2017-03-22 10:47:29 +0900 | [diff] [blame] | 23 | EXPECT_GE(SysInfo::NumberOfProcessors(), 1); |
deanm@chromium.org | 42a34eb | 2008-09-17 19:09:39 +0900 | [diff] [blame] | 24 | } |
deanm@chromium.org | 319f044 | 2008-09-17 22:10:45 +0900 | [diff] [blame] | 25 | |
mark@chromium.org | 3b2ec6f | 2008-09-26 05:33:42 +0900 | [diff] [blame] | 26 | TEST_F(SysInfoTest, AmountOfMem) { |
deanm@chromium.org | 319f044 | 2008-09-17 22:10:45 +0900 | [diff] [blame] | 27 | // We aren't actually testing that it's correct, just that it's sane. |
mkolom | af6f3f8 | 2017-03-22 10:47:29 +0900 | [diff] [blame] | 28 | EXPECT_GT(SysInfo::AmountOfPhysicalMemory(), 0); |
| 29 | EXPECT_GT(SysInfo::AmountOfPhysicalMemoryMB(), 0); |
jochen@chromium.org | 5ac0b17 | 2014-04-09 08:19:16 +0900 | [diff] [blame] | 30 | // The maxmimal amount of virtual memory can be zero which means unlimited. |
mkolom | af6f3f8 | 2017-03-22 10:47:29 +0900 | [diff] [blame] | 31 | EXPECT_GE(SysInfo::AmountOfVirtualMemory(), 0); |
deanm@chromium.org | ee45e8f | 2008-09-18 21:34:24 +0900 | [diff] [blame] | 32 | } |
| 33 | |
mkolom | af6f3f8 | 2017-03-22 10:47:29 +0900 | [diff] [blame] | 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 | |
mark@chromium.org | 3b2ec6f | 2008-09-26 05:33:42 +0900 | [diff] [blame] | 60 | TEST_F(SysInfoTest, AmountOfFreeDiskSpace) { |
deanm@chromium.org | ee45e8f | 2008-09-18 21:34:24 +0900 | [diff] [blame] | 61 | // We aren't actually testing that it's correct, just that it's sane. |
phajdan.jr@chromium.org | c800858 | 2009-09-15 04:36:31 +0900 | [diff] [blame] | 62 | FilePath tmp_path; |
mkolom | af6f3f8 | 2017-03-22 10:47:29 +0900 | [diff] [blame] | 63 | ASSERT_TRUE(GetTempDir(&tmp_path)); |
| 64 | EXPECT_GE(SysInfo::AmountOfFreeDiskSpace(tmp_path), 0) << tmp_path.value(); |
fukino | 1892bc2 | 2016-06-11 04:35:55 +0900 | [diff] [blame] | 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; |
mkolom | af6f3f8 | 2017-03-22 10:47:29 +0900 | [diff] [blame] | 70 | ASSERT_TRUE(GetTempDir(&tmp_path)); |
| 71 | EXPECT_GT(SysInfo::AmountOfTotalDiskSpace(tmp_path), 0) << tmp_path.value(); |
deanm@chromium.org | 319f044 | 2008-09-17 22:10:45 +0900 | [diff] [blame] | 72 | } |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 73 | |
romax | 47a7971 | 2017-03-23 05:01:46 +0900 | [diff] [blame] | 74 | #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) |
jeremy@chromium.org | 7730edb | 2009-02-25 01:37:13 +0900 | [diff] [blame] | 75 | TEST_F(SysInfoTest, OperatingSystemVersionNumbers) { |
avi | 95a2f37 | 2015-12-09 09:44:49 +0900 | [diff] [blame] | 76 | int32_t os_major_version = -1; |
| 77 | int32_t os_minor_version = -1; |
| 78 | int32_t os_bugfix_version = -1; |
mkolom | af6f3f8 | 2017-03-22 10:47:29 +0900 | [diff] [blame] | 79 | SysInfo::OperatingSystemVersionNumbers(&os_major_version, |
| 80 | &os_minor_version, |
| 81 | &os_bugfix_version); |
jeremy@chromium.org | 7730edb | 2009-02-25 01:37:13 +0900 | [diff] [blame] | 82 | EXPECT_GT(os_major_version, -1); |
| 83 | EXPECT_GT(os_minor_version, -1); |
| 84 | EXPECT_GT(os_bugfix_version, -1); |
| 85 | } |
evan@chromium.org | 54b7210 | 2009-07-22 09:35:18 +0900 | [diff] [blame] | 86 | #endif |
| 87 | |
jeremy@chromium.org | de658ed | 2012-10-29 10:04:09 +0900 | [diff] [blame] | 88 | TEST_F(SysInfoTest, Uptime) { |
mkolom | af6f3f8 | 2017-03-22 10:47:29 +0900 | [diff] [blame] | 89 | TimeDelta up_time_1 = SysInfo::Uptime(); |
jeremy@chromium.org | de658ed | 2012-10-29 10:04:09 +0900 | [diff] [blame] | 90 | // UpTime() is implemented internally using TimeTicks::Now(), which documents |
| 91 | // system resolution as being 1-15ms. Sleep a little longer than that. |
mkolom | af6f3f8 | 2017-03-22 10:47:29 +0900 | [diff] [blame] | 92 | PlatformThread::Sleep(TimeDelta::FromMilliseconds(20)); |
| 93 | TimeDelta up_time_2 = SysInfo::Uptime(); |
sque | babf673 | 2015-11-05 09:01:03 +0900 | [diff] [blame] | 94 | EXPECT_GT(up_time_1.InMicroseconds(), 0); |
| 95 | EXPECT_GT(up_time_2.InMicroseconds(), up_time_1.InMicroseconds()); |
jeremy@chromium.org | de658ed | 2012-10-29 10:04:09 +0900 | [diff] [blame] | 96 | } |
| 97 | |
jeremy | b43af09 | 2014-11-07 03:47:10 +0900 | [diff] [blame] | 98 | #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 99 | TEST_F(SysInfoTest, HardwareModelName) { |
mkolom | af6f3f8 | 2017-03-22 10:47:29 +0900 | [diff] [blame] | 100 | std::string hardware_model = SysInfo::HardwareModelName(); |
jeremy | b43af09 | 2014-11-07 03:47:10 +0900 | [diff] [blame] | 101 | EXPECT_FALSE(hardware_model.empty()); |
| 102 | } |
| 103 | #endif |
| 104 | |
evan@chromium.org | 54b7210 | 2009-07-22 09:35:18 +0900 | [diff] [blame] | 105 | #if defined(OS_CHROMEOS) |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 106 | |
evan@chromium.org | 54b7210 | 2009-07-22 09:35:18 +0900 | [diff] [blame] | 107 | TEST_F(SysInfoTest, GoogleChromeOSVersionNumbers) { |
avi | 95a2f37 | 2015-12-09 09:44:49 +0900 | [diff] [blame] | 108 | int32_t os_major_version = -1; |
| 109 | int32_t os_minor_version = -1; |
| 110 | int32_t os_bugfix_version = -1; |
thestig | dd7aaa0 | 2014-10-21 12:11:21 +0900 | [diff] [blame] | 111 | const char kLsbRelease[] = |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 112 | "FOO=1234123.34.5\n" |
| 113 | "CHROMEOS_RELEASE_VERSION=1.2.3.4\n"; |
mkolom | af6f3f8 | 2017-03-22 10:47:29 +0900 | [diff] [blame] | 114 | SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease, Time()); |
| 115 | SysInfo::OperatingSystemVersionNumbers(&os_major_version, |
| 116 | &os_minor_version, |
| 117 | &os_bugfix_version); |
rkc@chromium.org | d9ab606 | 2011-10-03 15:22:12 +0900 | [diff] [blame] | 118 | EXPECT_EQ(1, os_major_version); |
| 119 | EXPECT_EQ(2, os_minor_version); |
| 120 | EXPECT_EQ(3, os_bugfix_version); |
evan@chromium.org | 54b7210 | 2009-07-22 09:35:18 +0900 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | TEST_F(SysInfoTest, GoogleChromeOSVersionNumbersFirst) { |
avi | 95a2f37 | 2015-12-09 09:44:49 +0900 | [diff] [blame] | 124 | int32_t os_major_version = -1; |
| 125 | int32_t os_minor_version = -1; |
| 126 | int32_t os_bugfix_version = -1; |
thestig | dd7aaa0 | 2014-10-21 12:11:21 +0900 | [diff] [blame] | 127 | const char kLsbRelease[] = |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 128 | "CHROMEOS_RELEASE_VERSION=1.2.3.4\n" |
| 129 | "FOO=1234123.34.5\n"; |
mkolom | af6f3f8 | 2017-03-22 10:47:29 +0900 | [diff] [blame] | 130 | SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease, Time()); |
| 131 | SysInfo::OperatingSystemVersionNumbers(&os_major_version, |
| 132 | &os_minor_version, |
| 133 | &os_bugfix_version); |
rkc@chromium.org | d9ab606 | 2011-10-03 15:22:12 +0900 | [diff] [blame] | 134 | EXPECT_EQ(1, os_major_version); |
| 135 | EXPECT_EQ(2, os_minor_version); |
| 136 | EXPECT_EQ(3, os_bugfix_version); |
evan@chromium.org | 54b7210 | 2009-07-22 09:35:18 +0900 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | TEST_F(SysInfoTest, GoogleChromeOSNoVersionNumbers) { |
avi | 95a2f37 | 2015-12-09 09:44:49 +0900 | [diff] [blame] | 140 | int32_t os_major_version = -1; |
| 141 | int32_t os_minor_version = -1; |
| 142 | int32_t os_bugfix_version = -1; |
thestig | dd7aaa0 | 2014-10-21 12:11:21 +0900 | [diff] [blame] | 143 | const char kLsbRelease[] = "FOO=1234123.34.5\n"; |
mkolom | af6f3f8 | 2017-03-22 10:47:29 +0900 | [diff] [blame] | 144 | SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease, Time()); |
| 145 | SysInfo::OperatingSystemVersionNumbers(&os_major_version, |
| 146 | &os_minor_version, |
| 147 | &os_bugfix_version); |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 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) { |
thestig | dd7aaa0 | 2014-10-21 12:11:21 +0900 | [diff] [blame] | 154 | const char kLsbRelease[] = "CHROMEOS_RELEASE_VERSION=1.2.3.4"; |
stevenjb@chromium.org | 3354cf1 | 2013-09-27 07:53:33 +0900 | [diff] [blame] | 155 | // Use a fake time that can be safely displayed as a string. |
mkolom | af6f3f8 | 2017-03-22 10:47:29 +0900 | [diff] [blame] | 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(); |
stevenjb@chromium.org | 3354cf1 | 2013-09-27 07:53:33 +0900 | [diff] [blame] | 159 | EXPECT_DOUBLE_EQ(lsb_release_time.ToDoubleT(), |
| 160 | parsed_lsb_release_time.ToDoubleT()); |
evan@chromium.org | 54b7210 | 2009-07-22 09:35:18 +0900 | [diff] [blame] | 161 | } |
| 162 | |
stevenjb@chromium.org | 861313b | 2013-09-28 04:28:24 +0900 | [diff] [blame] | 163 | TEST_F(SysInfoTest, IsRunningOnChromeOS) { |
mkolom | af6f3f8 | 2017-03-22 10:47:29 +0900 | [diff] [blame] | 164 | SysInfo::SetChromeOSVersionInfoForTest("", Time()); |
| 165 | EXPECT_FALSE(SysInfo::IsRunningOnChromeOS()); |
stevenjb@chromium.org | 861313b | 2013-09-28 04:28:24 +0900 | [diff] [blame] | 166 | |
thestig | dd7aaa0 | 2014-10-21 12:11:21 +0900 | [diff] [blame] | 167 | const char kLsbRelease1[] = |
stevenjb@chromium.org | 861313b | 2013-09-28 04:28:24 +0900 | [diff] [blame] | 168 | "CHROMEOS_RELEASE_NAME=Non Chrome OS\n" |
| 169 | "CHROMEOS_RELEASE_VERSION=1.2.3.4\n"; |
mkolom | af6f3f8 | 2017-03-22 10:47:29 +0900 | [diff] [blame] | 170 | SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease1, Time()); |
| 171 | EXPECT_FALSE(SysInfo::IsRunningOnChromeOS()); |
stevenjb@chromium.org | 861313b | 2013-09-28 04:28:24 +0900 | [diff] [blame] | 172 | |
thestig | dd7aaa0 | 2014-10-21 12:11:21 +0900 | [diff] [blame] | 173 | const char kLsbRelease2[] = |
stevenjb@chromium.org | 861313b | 2013-09-28 04:28:24 +0900 | [diff] [blame] | 174 | "CHROMEOS_RELEASE_NAME=Chrome OS\n" |
| 175 | "CHROMEOS_RELEASE_VERSION=1.2.3.4\n"; |
mkolom | af6f3f8 | 2017-03-22 10:47:29 +0900 | [diff] [blame] | 176 | SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease2, Time()); |
| 177 | EXPECT_TRUE(SysInfo::IsRunningOnChromeOS()); |
stevenjb@chromium.org | 861313b | 2013-09-28 04:28:24 +0900 | [diff] [blame] | 178 | |
thestig | dd7aaa0 | 2014-10-21 12:11:21 +0900 | [diff] [blame] | 179 | const char kLsbRelease3[] = |
stevenjb@chromium.org | 861313b | 2013-09-28 04:28:24 +0900 | [diff] [blame] | 180 | "CHROMEOS_RELEASE_NAME=Chromium OS\n"; |
mkolom | af6f3f8 | 2017-03-22 10:47:29 +0900 | [diff] [blame] | 181 | SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease3, Time()); |
| 182 | EXPECT_TRUE(SysInfo::IsRunningOnChromeOS()); |
stevenjb@chromium.org | 861313b | 2013-09-28 04:28:24 +0900 | [diff] [blame] | 183 | } |
| 184 | |
igorcov | 352c134 | 2016-12-02 04:42:32 +0900 | [diff] [blame] | 185 | TEST_F(SysInfoTest, GetStrippedReleaseBoard) { |
| 186 | const char* kLsbRelease1 = "CHROMEOS_RELEASE_BOARD=Glimmer\n"; |
mkolom | af6f3f8 | 2017-03-22 10:47:29 +0900 | [diff] [blame] | 187 | SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease1, Time()); |
| 188 | EXPECT_EQ("glimmer", SysInfo::GetStrippedReleaseBoard()); |
igorcov | 352c134 | 2016-12-02 04:42:32 +0900 | [diff] [blame] | 189 | |
| 190 | const char* kLsbRelease2 = "CHROMEOS_RELEASE_BOARD=glimmer-signed-mp-v4keys"; |
mkolom | af6f3f8 | 2017-03-22 10:47:29 +0900 | [diff] [blame] | 191 | SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease2, Time()); |
| 192 | EXPECT_EQ("glimmer", SysInfo::GetStrippedReleaseBoard()); |
igorcov | 352c134 | 2016-12-02 04:42:32 +0900 | [diff] [blame] | 193 | } |
| 194 | |
evan@chromium.org | 54b7210 | 2009-07-22 09:35:18 +0900 | [diff] [blame] | 195 | #endif // OS_CHROMEOS |
mkolom | af6f3f8 | 2017-03-22 10:47:29 +0900 | [diff] [blame] | 196 | |
| 197 | } // namespace base |