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