sys_info_unittest.cc 5.6 KB
Newer Older
gejun's avatar
gejun committed
1 2 3 4
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6 7 8 9
#include "butil/environment.h"
#include "butil/file_util.h"
#include "butil/sys_info.h"
#include "butil/threading/platform_thread.h"
#include "butil/time/time.h"
gejun's avatar
gejun committed
10 11 12 13
#include <gtest/gtest.h>
#include <gtest/gtest.h>

typedef testing::Test SysInfoTest;
14
using butil::FilePath;
gejun's avatar
gejun committed
15 16 17 18

#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
TEST_F(SysInfoTest, MaxSharedMemorySize) {
  // We aren't actually testing that it's correct, just that it's sane.
19
  EXPECT_GT(butil::SysInfo::MaxSharedMemorySize(), 0u);
gejun's avatar
gejun committed
20 21 22 23 24
}
#endif

TEST_F(SysInfoTest, NumProcs) {
  // We aren't actually testing that it's correct, just that it's sane.
25
  EXPECT_GE(butil::SysInfo::NumberOfProcessors(), 1);
gejun's avatar
gejun committed
26 27 28 29
}

TEST_F(SysInfoTest, AmountOfMem) {
  // We aren't actually testing that it's correct, just that it's sane.
30 31
  EXPECT_GT(butil::SysInfo::AmountOfPhysicalMemory(), 0);
  EXPECT_GT(butil::SysInfo::AmountOfPhysicalMemoryMB(), 0);
gejun's avatar
gejun committed
32
  // The maxmimal amount of virtual memory can be zero which means unlimited.
33
  EXPECT_GE(butil::SysInfo::AmountOfVirtualMemory(), 0);
gejun's avatar
gejun committed
34 35 36 37 38
}

TEST_F(SysInfoTest, AmountOfFreeDiskSpace) {
  // We aren't actually testing that it's correct, just that it's sane.
  FilePath tmp_path;
39 40
  ASSERT_TRUE(butil::GetTempDir(&tmp_path));
  EXPECT_GT(butil::SysInfo::AmountOfFreeDiskSpace(tmp_path), 0)
gejun's avatar
gejun committed
41 42 43 44 45 46 47 48
            << tmp_path.value();
}

#if defined(OS_WIN) || defined(OS_MACOSX)
TEST_F(SysInfoTest, OperatingSystemVersionNumbers) {
  int32_t os_major_version = -1;
  int32_t os_minor_version = -1;
  int32_t os_bugfix_version = -1;
49
  butil::SysInfo::OperatingSystemVersionNumbers(&os_major_version,
gejun's avatar
gejun committed
50 51 52 53 54 55 56 57 58
                                               &os_minor_version,
                                               &os_bugfix_version);
  EXPECT_GT(os_major_version, -1);
  EXPECT_GT(os_minor_version, -1);
  EXPECT_GT(os_bugfix_version, -1);
}
#endif

TEST_F(SysInfoTest, Uptime) {
59
  int64_t up_time_1 = butil::SysInfo::Uptime();
gejun's avatar
gejun committed
60 61
  // UpTime() is implemented internally using TimeTicks::Now(), which documents
  // system resolution as being 1-15ms. Sleep a little longer than that.
62 63
  butil::PlatformThread::Sleep(butil::TimeDelta::FromMilliseconds(20));
  int64_t up_time_2 = butil::SysInfo::Uptime();
gejun's avatar
gejun committed
64 65 66 67 68 69 70 71 72 73 74 75 76
  EXPECT_GT(up_time_1, 0);
  EXPECT_GT(up_time_2, up_time_1);
}

#if defined(OS_CHROMEOS)

TEST_F(SysInfoTest, GoogleChromeOSVersionNumbers) {
  int32_t os_major_version = -1;
  int32_t os_minor_version = -1;
  int32_t os_bugfix_version = -1;
  const char* kLsbRelease =
      "FOO=1234123.34.5\n"
      "CHROMEOS_RELEASE_VERSION=1.2.3.4\n";
77 78
  butil::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease, butil::Time());
  butil::SysInfo::OperatingSystemVersionNumbers(&os_major_version,
gejun's avatar
gejun committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92
                                               &os_minor_version,
                                               &os_bugfix_version);
  EXPECT_EQ(1, os_major_version);
  EXPECT_EQ(2, os_minor_version);
  EXPECT_EQ(3, os_bugfix_version);
}

TEST_F(SysInfoTest, GoogleChromeOSVersionNumbersFirst) {
  int32_t os_major_version = -1;
  int32_t os_minor_version = -1;
  int32_t os_bugfix_version = -1;
  const char* kLsbRelease =
      "CHROMEOS_RELEASE_VERSION=1.2.3.4\n"
      "FOO=1234123.34.5\n";
93 94
  butil::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease, butil::Time());
  butil::SysInfo::OperatingSystemVersionNumbers(&os_major_version,
gejun's avatar
gejun committed
95 96 97 98 99 100 101 102 103 104 105 106
                                               &os_minor_version,
                                               &os_bugfix_version);
  EXPECT_EQ(1, os_major_version);
  EXPECT_EQ(2, os_minor_version);
  EXPECT_EQ(3, os_bugfix_version);
}

TEST_F(SysInfoTest, GoogleChromeOSNoVersionNumbers) {
  int32_t os_major_version = -1;
  int32_t os_minor_version = -1;
  int32_t os_bugfix_version = -1;
  const char* kLsbRelease = "FOO=1234123.34.5\n";
107 108
  butil::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease, butil::Time());
  butil::SysInfo::OperatingSystemVersionNumbers(&os_major_version,
gejun's avatar
gejun committed
109 110 111 112 113 114 115 116 117 118
                                               &os_minor_version,
                                               &os_bugfix_version);
  EXPECT_EQ(0, os_major_version);
  EXPECT_EQ(0, os_minor_version);
  EXPECT_EQ(0, os_bugfix_version);
}

TEST_F(SysInfoTest, GoogleChromeOSLsbReleaseTime) {
  const char* kLsbRelease = "CHROMEOS_RELEASE_VERSION=1.2.3.4";
  // Use a fake time that can be safely displayed as a string.
119 120 121
  const butil::Time lsb_release_time(butil::Time::FromDoubleT(12345.6));
  butil::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease, lsb_release_time);
  butil::Time parsed_lsb_release_time = butil::SysInfo::GetLsbReleaseTime();
gejun's avatar
gejun committed
122 123 124 125 126
  EXPECT_DOUBLE_EQ(lsb_release_time.ToDoubleT(),
                   parsed_lsb_release_time.ToDoubleT());
}

TEST_F(SysInfoTest, IsRunningOnChromeOS) {
127 128
  butil::SysInfo::SetChromeOSVersionInfoForTest("", butil::Time());
  EXPECT_FALSE(butil::SysInfo::IsRunningOnChromeOS());
gejun's avatar
gejun committed
129 130 131 132

  const char* kLsbRelease1 =
      "CHROMEOS_RELEASE_NAME=Non Chrome OS\n"
      "CHROMEOS_RELEASE_VERSION=1.2.3.4\n";
133 134
  butil::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease1, butil::Time());
  EXPECT_FALSE(butil::SysInfo::IsRunningOnChromeOS());
gejun's avatar
gejun committed
135 136 137 138

  const char* kLsbRelease2 =
      "CHROMEOS_RELEASE_NAME=Chrome OS\n"
      "CHROMEOS_RELEASE_VERSION=1.2.3.4\n";
139 140
  butil::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease2, butil::Time());
  EXPECT_TRUE(butil::SysInfo::IsRunningOnChromeOS());
gejun's avatar
gejun committed
141 142 143

  const char* kLsbRelease3 =
      "CHROMEOS_RELEASE_NAME=Chromium OS\n";
144 145
  butil::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease3, butil::Time());
  EXPECT_TRUE(butil::SysInfo::IsRunningOnChromeOS());
gejun's avatar
gejun committed
146 147 148
}

#endif  // OS_CHROMEOS