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