blob: 94b5a8497167483ad74d3c841364c91be817c248 [file] [log] [blame]
nileshagrawal@chromium.orga049e952012-07-11 08:24:43 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
deanm@chromium.org42a34eb2008-09-17 19:09:39 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
avia6a6a682015-12-27 07:15:14 +09005#include <stdint.h>
6
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +09007#include "base/environment.h"
brettw@chromium.org01f3da42014-08-14 05:22:14 +09008#include "base/files/file_util.h"
mkolomaf6f3f82017-03-22 10:47:29 +09009#include "base/process/process_metrics.h"
deanm@chromium.org42a34eb2008-09-17 19:09:39 +090010#include "base/sys_info.h"
jeremy@chromium.orgde658ed2012-10-29 10:04:09 +090011#include "base/threading/platform_thread.h"
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +090012#include "base/time/time.h"
avia6a6a682015-12-27 07:15:14 +090013#include "build/build_config.h"
deanm@chromium.org42a34eb2008-09-17 19:09:39 +090014#include "testing/gtest/include/gtest/gtest.h"
jeremy@chromium.org0d8eba72008-12-03 04:20:15 +090015#include "testing/platform_test.h"
deanm@chromium.org42a34eb2008-09-17 19:09:39 +090016
mkolomaf6f3f82017-03-22 10:47:29 +090017namespace base {
18
19using SysInfoTest = PlatformTest;
mark@chromium.org3b2ec6f2008-09-26 05:33:42 +090020
21TEST_F(SysInfoTest, NumProcs) {
deanm@chromium.org42a34eb2008-09-17 19:09:39 +090022 // We aren't actually testing that it's correct, just that it's sane.
mkolomaf6f3f82017-03-22 10:47:29 +090023 EXPECT_GE(SysInfo::NumberOfProcessors(), 1);
deanm@chromium.org42a34eb2008-09-17 19:09:39 +090024}
deanm@chromium.org319f0442008-09-17 22:10:45 +090025
mark@chromium.org3b2ec6f2008-09-26 05:33:42 +090026TEST_F(SysInfoTest, AmountOfMem) {
deanm@chromium.org319f0442008-09-17 22:10:45 +090027 // We aren't actually testing that it's correct, just that it's sane.
mkolomaf6f3f82017-03-22 10:47:29 +090028 EXPECT_GT(SysInfo::AmountOfPhysicalMemory(), 0);
29 EXPECT_GT(SysInfo::AmountOfPhysicalMemoryMB(), 0);
jochen@chromium.org5ac0b172014-04-09 08:19:16 +090030 // The maxmimal amount of virtual memory can be zero which means unlimited.
mkolomaf6f3f82017-03-22 10:47:29 +090031 EXPECT_GE(SysInfo::AmountOfVirtualMemory(), 0);
deanm@chromium.orgee45e8f2008-09-18 21:34:24 +090032}
33
mkolomaf6f3f82017-03-22 10:47:29 +090034#if defined(OS_LINUX) || defined(OS_ANDROID)
35TEST_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.org3b2ec6f2008-09-26 05:33:42 +090060TEST_F(SysInfoTest, AmountOfFreeDiskSpace) {
deanm@chromium.orgee45e8f2008-09-18 21:34:24 +090061 // We aren't actually testing that it's correct, just that it's sane.
phajdan.jr@chromium.orgc8008582009-09-15 04:36:31 +090062 FilePath tmp_path;
mkolomaf6f3f82017-03-22 10:47:29 +090063 ASSERT_TRUE(GetTempDir(&tmp_path));
64 EXPECT_GE(SysInfo::AmountOfFreeDiskSpace(tmp_path), 0) << tmp_path.value();
fukino1892bc22016-06-11 04:35:55 +090065}
66
67TEST_F(SysInfoTest, AmountOfTotalDiskSpace) {
68 // We aren't actually testing that it's correct, just that it's sane.
69 FilePath tmp_path;
mkolomaf6f3f82017-03-22 10:47:29 +090070 ASSERT_TRUE(GetTempDir(&tmp_path));
71 EXPECT_GT(SysInfo::AmountOfTotalDiskSpace(tmp_path), 0) << tmp_path.value();
deanm@chromium.org319f0442008-09-17 22:10:45 +090072}
mark@chromium.orgb93c0542008-09-30 07:18:01 +090073
romax47a79712017-03-23 05:01:46 +090074#if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX)
jeremy@chromium.org7730edb2009-02-25 01:37:13 +090075TEST_F(SysInfoTest, OperatingSystemVersionNumbers) {
avi95a2f372015-12-09 09:44:49 +090076 int32_t os_major_version = -1;
77 int32_t os_minor_version = -1;
78 int32_t os_bugfix_version = -1;
mkolomaf6f3f82017-03-22 10:47:29 +090079 SysInfo::OperatingSystemVersionNumbers(&os_major_version,
80 &os_minor_version,
81 &os_bugfix_version);
jeremy@chromium.org7730edb2009-02-25 01:37:13 +090082 EXPECT_GT(os_major_version, -1);
83 EXPECT_GT(os_minor_version, -1);
84 EXPECT_GT(os_bugfix_version, -1);
85}
evan@chromium.org54b72102009-07-22 09:35:18 +090086#endif
87
jeremy@chromium.orgde658ed2012-10-29 10:04:09 +090088TEST_F(SysInfoTest, Uptime) {
mkolomaf6f3f82017-03-22 10:47:29 +090089 TimeDelta up_time_1 = SysInfo::Uptime();
jeremy@chromium.orgde658ed2012-10-29 10:04:09 +090090 // UpTime() is implemented internally using TimeTicks::Now(), which documents
91 // system resolution as being 1-15ms. Sleep a little longer than that.
mkolomaf6f3f82017-03-22 10:47:29 +090092 PlatformThread::Sleep(TimeDelta::FromMilliseconds(20));
93 TimeDelta up_time_2 = SysInfo::Uptime();
squebabf6732015-11-05 09:01:03 +090094 EXPECT_GT(up_time_1.InMicroseconds(), 0);
95 EXPECT_GT(up_time_2.InMicroseconds(), up_time_1.InMicroseconds());
jeremy@chromium.orgde658ed2012-10-29 10:04:09 +090096}
97
jeremyb43af092014-11-07 03:47:10 +090098#if defined(OS_MACOSX) && !defined(OS_IOS)
99TEST_F(SysInfoTest, HardwareModelName) {
mkolomaf6f3f82017-03-22 10:47:29 +0900100 std::string hardware_model = SysInfo::HardwareModelName();
jeremyb43af092014-11-07 03:47:10 +0900101 EXPECT_FALSE(hardware_model.empty());
102}
103#endif
104
evan@chromium.org54b72102009-07-22 09:35:18 +0900105#if defined(OS_CHROMEOS)
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +0900106
evan@chromium.org54b72102009-07-22 09:35:18 +0900107TEST_F(SysInfoTest, GoogleChromeOSVersionNumbers) {
avi95a2f372015-12-09 09:44:49 +0900108 int32_t os_major_version = -1;
109 int32_t os_minor_version = -1;
110 int32_t os_bugfix_version = -1;
thestigdd7aaa02014-10-21 12:11:21 +0900111 const char kLsbRelease[] =
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +0900112 "FOO=1234123.34.5\n"
113 "CHROMEOS_RELEASE_VERSION=1.2.3.4\n";
mkolomaf6f3f82017-03-22 10:47:29 +0900114 SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease, Time());
115 SysInfo::OperatingSystemVersionNumbers(&os_major_version,
116 &os_minor_version,
117 &os_bugfix_version);
rkc@chromium.orgd9ab6062011-10-03 15:22:12 +0900118 EXPECT_EQ(1, os_major_version);
119 EXPECT_EQ(2, os_minor_version);
120 EXPECT_EQ(3, os_bugfix_version);
evan@chromium.org54b72102009-07-22 09:35:18 +0900121}
122
123TEST_F(SysInfoTest, GoogleChromeOSVersionNumbersFirst) {
avi95a2f372015-12-09 09:44:49 +0900124 int32_t os_major_version = -1;
125 int32_t os_minor_version = -1;
126 int32_t os_bugfix_version = -1;
thestigdd7aaa02014-10-21 12:11:21 +0900127 const char kLsbRelease[] =
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +0900128 "CHROMEOS_RELEASE_VERSION=1.2.3.4\n"
129 "FOO=1234123.34.5\n";
mkolomaf6f3f82017-03-22 10:47:29 +0900130 SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease, Time());
131 SysInfo::OperatingSystemVersionNumbers(&os_major_version,
132 &os_minor_version,
133 &os_bugfix_version);
rkc@chromium.orgd9ab6062011-10-03 15:22:12 +0900134 EXPECT_EQ(1, os_major_version);
135 EXPECT_EQ(2, os_minor_version);
136 EXPECT_EQ(3, os_bugfix_version);
evan@chromium.org54b72102009-07-22 09:35:18 +0900137}
138
139TEST_F(SysInfoTest, GoogleChromeOSNoVersionNumbers) {
avi95a2f372015-12-09 09:44:49 +0900140 int32_t os_major_version = -1;
141 int32_t os_minor_version = -1;
142 int32_t os_bugfix_version = -1;
thestigdd7aaa02014-10-21 12:11:21 +0900143 const char kLsbRelease[] = "FOO=1234123.34.5\n";
mkolomaf6f3f82017-03-22 10:47:29 +0900144 SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease, Time());
145 SysInfo::OperatingSystemVersionNumbers(&os_major_version,
146 &os_minor_version,
147 &os_bugfix_version);
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +0900148 EXPECT_EQ(0, os_major_version);
149 EXPECT_EQ(0, os_minor_version);
150 EXPECT_EQ(0, os_bugfix_version);
151}
152
153TEST_F(SysInfoTest, GoogleChromeOSLsbReleaseTime) {
thestigdd7aaa02014-10-21 12:11:21 +0900154 const char kLsbRelease[] = "CHROMEOS_RELEASE_VERSION=1.2.3.4";
stevenjb@chromium.org3354cf12013-09-27 07:53:33 +0900155 // Use a fake time that can be safely displayed as a string.
mkolomaf6f3f82017-03-22 10:47:29 +0900156 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.org3354cf12013-09-27 07:53:33 +0900159 EXPECT_DOUBLE_EQ(lsb_release_time.ToDoubleT(),
160 parsed_lsb_release_time.ToDoubleT());
evan@chromium.org54b72102009-07-22 09:35:18 +0900161}
162
stevenjb@chromium.org861313b2013-09-28 04:28:24 +0900163TEST_F(SysInfoTest, IsRunningOnChromeOS) {
mkolomaf6f3f82017-03-22 10:47:29 +0900164 SysInfo::SetChromeOSVersionInfoForTest("", Time());
165 EXPECT_FALSE(SysInfo::IsRunningOnChromeOS());
stevenjb@chromium.org861313b2013-09-28 04:28:24 +0900166
thestigdd7aaa02014-10-21 12:11:21 +0900167 const char kLsbRelease1[] =
stevenjb@chromium.org861313b2013-09-28 04:28:24 +0900168 "CHROMEOS_RELEASE_NAME=Non Chrome OS\n"
169 "CHROMEOS_RELEASE_VERSION=1.2.3.4\n";
mkolomaf6f3f82017-03-22 10:47:29 +0900170 SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease1, Time());
171 EXPECT_FALSE(SysInfo::IsRunningOnChromeOS());
stevenjb@chromium.org861313b2013-09-28 04:28:24 +0900172
thestigdd7aaa02014-10-21 12:11:21 +0900173 const char kLsbRelease2[] =
stevenjb@chromium.org861313b2013-09-28 04:28:24 +0900174 "CHROMEOS_RELEASE_NAME=Chrome OS\n"
175 "CHROMEOS_RELEASE_VERSION=1.2.3.4\n";
mkolomaf6f3f82017-03-22 10:47:29 +0900176 SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease2, Time());
177 EXPECT_TRUE(SysInfo::IsRunningOnChromeOS());
stevenjb@chromium.org861313b2013-09-28 04:28:24 +0900178
thestigdd7aaa02014-10-21 12:11:21 +0900179 const char kLsbRelease3[] =
stevenjb@chromium.org861313b2013-09-28 04:28:24 +0900180 "CHROMEOS_RELEASE_NAME=Chromium OS\n";
mkolomaf6f3f82017-03-22 10:47:29 +0900181 SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease3, Time());
182 EXPECT_TRUE(SysInfo::IsRunningOnChromeOS());
stevenjb@chromium.org861313b2013-09-28 04:28:24 +0900183}
184
igorcov352c1342016-12-02 04:42:32 +0900185TEST_F(SysInfoTest, GetStrippedReleaseBoard) {
186 const char* kLsbRelease1 = "CHROMEOS_RELEASE_BOARD=Glimmer\n";
mkolomaf6f3f82017-03-22 10:47:29 +0900187 SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease1, Time());
188 EXPECT_EQ("glimmer", SysInfo::GetStrippedReleaseBoard());
igorcov352c1342016-12-02 04:42:32 +0900189
190 const char* kLsbRelease2 = "CHROMEOS_RELEASE_BOARD=glimmer-signed-mp-v4keys";
mkolomaf6f3f82017-03-22 10:47:29 +0900191 SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease2, Time());
192 EXPECT_EQ("glimmer", SysInfo::GetStrippedReleaseBoard());
igorcov352c1342016-12-02 04:42:32 +0900193}
194
evan@chromium.org54b72102009-07-22 09:35:18 +0900195#endif // OS_CHROMEOS
mkolomaf6f3f82017-03-22 10:47:29 +0900196
197} // namespace base