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