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