cpu_temperature_reader_unittest.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2017 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 "chromeos/system/cpu_temperature_reader.h"
  5. #include <algorithm>
  6. #include <string>
  7. #include "base/files/file.h"
  8. #include "base/files/file_util.h"
  9. #include "base/files/scoped_temp_dir.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. namespace chromeos {
  12. namespace system {
  13. class CPUTemperatureReaderTest : public ::testing::Test {
  14. public:
  15. CPUTemperatureReaderTest() {
  16. CHECK(dir_.CreateUniqueTempDir());
  17. hwmon_path_ = dir_.GetPath();
  18. reader_.set_hwmon_dir_for_test(hwmon_path_);
  19. }
  20. ~CPUTemperatureReaderTest() override = default;
  21. protected:
  22. using CPUTemperatureInfo = CPUTemperatureReader::CPUTemperatureInfo;
  23. // Creates a subdirectory in |hwmon_path_| with name |name|. Returns the full
  24. // path of the new subdirectory.
  25. base::FilePath CreateHwmonSubdir(const std::string& name) {
  26. base::FilePath subdir_path = hwmon_path_.Append(name);
  27. CHECK(base::CreateDirectory(subdir_path));
  28. return subdir_path;
  29. }
  30. // Creates a file at |path| containing data |contents|.
  31. void CreateFileWithContents(const base::FilePath& path,
  32. const std::string& contents) {
  33. CHECK_EQ(WriteFile(path, contents.data(), contents.size()),
  34. static_cast<int>(contents.size()));
  35. }
  36. // Creates a temporary dir to act as the hwmon directory passed to |reader_|.
  37. base::ScopedTempDir dir_;
  38. // Path of the temporary dir created by |dir_|.
  39. base::FilePath hwmon_path_;
  40. // Instance of the class under test
  41. CPUTemperatureReader reader_;
  42. };
  43. TEST_F(CPUTemperatureReaderTest, EmptyDir) {
  44. base::FilePath subdir_empty = CreateHwmonSubdir("hwmon0");
  45. base::FilePath subdir_not_temp = CreateHwmonSubdir("hwmon1");
  46. CreateFileWithContents(subdir_not_temp.Append("not_cpu_temp"), "garbage");
  47. EXPECT_EQ(0U, reader_.GetCPUTemperatures().size());
  48. }
  49. TEST_F(CPUTemperatureReaderTest, SingleDir) {
  50. base::FilePath subdir = CreateHwmonSubdir("hwmon0");
  51. CreateFileWithContents(subdir.Append("temp1_input"), "10000\n");
  52. std::vector<CPUTemperatureInfo> cpu_temp_readings =
  53. reader_.GetCPUTemperatures();
  54. ASSERT_EQ(1U, cpu_temp_readings.size());
  55. EXPECT_EQ(10.0f, cpu_temp_readings[0].temp_celsius);
  56. EXPECT_EQ(subdir.Append("temp1_label").value(), cpu_temp_readings[0].label);
  57. }
  58. TEST_F(CPUTemperatureReaderTest, SingleDirWithLabel) {
  59. base::FilePath subdir = CreateHwmonSubdir("hwmon0");
  60. CreateFileWithContents(subdir.Append("temp2_input"), "20000\n");
  61. CreateFileWithContents(subdir.Append("temp2_label"), "t2\n");
  62. std::vector<CPUTemperatureInfo> cpu_temp_readings =
  63. reader_.GetCPUTemperatures();
  64. ASSERT_EQ(1U, cpu_temp_readings.size());
  65. EXPECT_EQ(20.0f, cpu_temp_readings[0].temp_celsius);
  66. EXPECT_EQ("t2", cpu_temp_readings[0].label);
  67. }
  68. TEST_F(CPUTemperatureReaderTest, SingleDirWithName) {
  69. base::FilePath subdir = CreateHwmonSubdir("hwmon0");
  70. CreateFileWithContents(subdir.Append("temp3_input"), "30000\n");
  71. CreateFileWithContents(subdir.Append("temp3_label"), "\n");
  72. CreateFileWithContents(subdir.Append("name"), "t3\n");
  73. std::vector<CPUTemperatureInfo> cpu_temp_readings =
  74. reader_.GetCPUTemperatures();
  75. ASSERT_EQ(1U, cpu_temp_readings.size());
  76. EXPECT_EQ(30.0f, cpu_temp_readings[0].temp_celsius);
  77. EXPECT_EQ("t3", cpu_temp_readings[0].label);
  78. }
  79. TEST_F(CPUTemperatureReaderTest, SingleDirWithDeviceSubdir) {
  80. base::FilePath subdir = CreateHwmonSubdir("hwmon0");
  81. CreateFileWithContents(subdir.Append("temp1_input"), "10000\n");
  82. base::FilePath device_subdir = subdir.Append("device");
  83. base::CreateDirectory(device_subdir);
  84. CreateFileWithContents(device_subdir.Append("temp1_input"), "20000\n");
  85. std::vector<CPUTemperatureInfo> cpu_temp_readings =
  86. reader_.GetCPUTemperatures();
  87. ASSERT_EQ(2U, cpu_temp_readings.size());
  88. EXPECT_EQ(10.0f, cpu_temp_readings[0].temp_celsius);
  89. EXPECT_EQ(subdir.Append("temp1_label").value(), cpu_temp_readings[0].label);
  90. EXPECT_EQ(20.0f, cpu_temp_readings[1].temp_celsius);
  91. EXPECT_EQ(device_subdir.Append("temp1_label").value(),
  92. cpu_temp_readings[1].label);
  93. }
  94. TEST_F(CPUTemperatureReaderTest, MultipleDirs) {
  95. base::FilePath subdir0 = CreateHwmonSubdir("hwmon0");
  96. CreateFileWithContents(subdir0.Append("temp1_input"), "10000\n");
  97. base::FilePath subdir1 = CreateHwmonSubdir("hwmon1");
  98. CreateFileWithContents(subdir1.Append("temp2_input"), "20000\n");
  99. CreateFileWithContents(subdir1.Append("temp2_label"), "t2\n");
  100. // This should not result in a CPU temperature reading.
  101. base::FilePath subdir2 = CreateHwmonSubdir("hwmon2");
  102. CreateFileWithContents(subdir2.Append("not_cpu_temp"), "garbage");
  103. base::FilePath subdir3 = CreateHwmonSubdir("hwmon3");
  104. CreateFileWithContents(subdir3.Append("temp3_input"), "30000\n");
  105. CreateFileWithContents(subdir3.Append("temp3_label"), "t3\n");
  106. std::vector<CPUTemperatureInfo> cpu_temp_readings =
  107. reader_.GetCPUTemperatures();
  108. // The order in which these directories were read is not guaranteed. Sort them
  109. // first.
  110. std::sort(cpu_temp_readings.begin(), cpu_temp_readings.end());
  111. ASSERT_EQ(3U, cpu_temp_readings.size());
  112. EXPECT_EQ(10.0f, cpu_temp_readings[0].temp_celsius);
  113. EXPECT_EQ(subdir0.Append("temp1_label").value(), cpu_temp_readings[0].label);
  114. EXPECT_EQ(20.0f, cpu_temp_readings[1].temp_celsius);
  115. EXPECT_EQ("t2", cpu_temp_readings[1].label);
  116. EXPECT_EQ(30.0f, cpu_temp_readings[2].temp_celsius);
  117. EXPECT_EQ("t3", cpu_temp_readings[2].label);
  118. }
  119. } // namespace system
  120. } // namespace chromeos