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