Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(437)

Side by Side Diff: base/sys_info_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698