sys_info_unittest.cc 5.6 KB

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