forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcpu_temperature_reader_unittest.cc
154 lines (120 loc) · 5.49 KB
/
cpu_temperature_reader_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/system/cpu_temperature_reader.h"
#include <algorithm>
#include <string>
#include "base/files/file.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
namespace system {
class CPUTemperatureReaderTest : public ::testing::Test {
public:
CPUTemperatureReaderTest() {
CHECK(dir_.CreateUniqueTempDir());
hwmon_path_ = dir_.GetPath();
reader_.set_hwmon_dir_for_test(hwmon_path_);
}
~CPUTemperatureReaderTest() override = default;
protected:
using CPUTemperatureInfo = CPUTemperatureReader::CPUTemperatureInfo;
// Creates a subdirectory in |hwmon_path_| with name |name|. Returns the full
// path of the new subdirectory.
base::FilePath CreateHwmonSubdir(const std::string& name) {
base::FilePath subdir_path = hwmon_path_.Append(name);
CHECK(base::CreateDirectory(subdir_path));
return subdir_path;
}
// Creates a file at |path| containing data |contents|.
void CreateFileWithContents(const base::FilePath& path,
const std::string& contents) {
CHECK_EQ(WriteFile(path, contents.data(), contents.size()),
static_cast<int>(contents.size()));
}
// Creates a temporary dir to act as the hwmon directory passed to |reader_|.
base::ScopedTempDir dir_;
// Path of the temporary dir created by |dir_|.
base::FilePath hwmon_path_;
// Instance of the class under test
CPUTemperatureReader reader_;
};
TEST_F(CPUTemperatureReaderTest, EmptyDir) {
base::FilePath subdir_empty = CreateHwmonSubdir("hwmon0");
base::FilePath subdir_not_temp = CreateHwmonSubdir("hwmon1");
CreateFileWithContents(subdir_not_temp.Append("not_cpu_temp"), "garbage");
EXPECT_EQ(0U, reader_.GetCPUTemperatures().size());
}
TEST_F(CPUTemperatureReaderTest, SingleDir) {
base::FilePath subdir = CreateHwmonSubdir("hwmon0");
CreateFileWithContents(subdir.Append("temp1_input"), "10000\n");
std::vector<CPUTemperatureInfo> cpu_temp_readings =
reader_.GetCPUTemperatures();
ASSERT_EQ(1U, cpu_temp_readings.size());
EXPECT_EQ(10.0f, cpu_temp_readings[0].temp_celsius);
EXPECT_EQ(subdir.Append("temp1_label").value(), cpu_temp_readings[0].label);
}
TEST_F(CPUTemperatureReaderTest, SingleDirWithLabel) {
base::FilePath subdir = CreateHwmonSubdir("hwmon0");
CreateFileWithContents(subdir.Append("temp2_input"), "20000\n");
CreateFileWithContents(subdir.Append("temp2_label"), "t2\n");
std::vector<CPUTemperatureInfo> cpu_temp_readings =
reader_.GetCPUTemperatures();
ASSERT_EQ(1U, cpu_temp_readings.size());
EXPECT_EQ(20.0f, cpu_temp_readings[0].temp_celsius);
EXPECT_EQ("t2", cpu_temp_readings[0].label);
}
TEST_F(CPUTemperatureReaderTest, SingleDirWithName) {
base::FilePath subdir = CreateHwmonSubdir("hwmon0");
CreateFileWithContents(subdir.Append("temp3_input"), "30000\n");
CreateFileWithContents(subdir.Append("temp3_label"), "\n");
CreateFileWithContents(subdir.Append("name"), "t3\n");
std::vector<CPUTemperatureInfo> cpu_temp_readings =
reader_.GetCPUTemperatures();
ASSERT_EQ(1U, cpu_temp_readings.size());
EXPECT_EQ(30.0f, cpu_temp_readings[0].temp_celsius);
EXPECT_EQ("t3", cpu_temp_readings[0].label);
}
TEST_F(CPUTemperatureReaderTest, SingleDirWithDeviceSubdir) {
base::FilePath subdir = CreateHwmonSubdir("hwmon0");
CreateFileWithContents(subdir.Append("temp1_input"), "10000\n");
base::FilePath device_subdir = subdir.Append("device");
base::CreateDirectory(device_subdir);
CreateFileWithContents(device_subdir.Append("temp1_input"), "20000\n");
std::vector<CPUTemperatureInfo> cpu_temp_readings =
reader_.GetCPUTemperatures();
ASSERT_EQ(2U, cpu_temp_readings.size());
EXPECT_EQ(10.0f, cpu_temp_readings[0].temp_celsius);
EXPECT_EQ(subdir.Append("temp1_label").value(), cpu_temp_readings[0].label);
EXPECT_EQ(20.0f, cpu_temp_readings[1].temp_celsius);
EXPECT_EQ(device_subdir.Append("temp1_label").value(),
cpu_temp_readings[1].label);
}
TEST_F(CPUTemperatureReaderTest, MultipleDirs) {
base::FilePath subdir0 = CreateHwmonSubdir("hwmon0");
CreateFileWithContents(subdir0.Append("temp1_input"), "10000\n");
base::FilePath subdir1 = CreateHwmonSubdir("hwmon1");
CreateFileWithContents(subdir1.Append("temp2_input"), "20000\n");
CreateFileWithContents(subdir1.Append("temp2_label"), "t2\n");
// This should not result in a CPU temperature reading.
base::FilePath subdir2 = CreateHwmonSubdir("hwmon2");
CreateFileWithContents(subdir2.Append("not_cpu_temp"), "garbage");
base::FilePath subdir3 = CreateHwmonSubdir("hwmon3");
CreateFileWithContents(subdir3.Append("temp3_input"), "30000\n");
CreateFileWithContents(subdir3.Append("temp3_label"), "t3\n");
std::vector<CPUTemperatureInfo> cpu_temp_readings =
reader_.GetCPUTemperatures();
// The order in which these directories were read is not guaranteed. Sort them
// first.
std::sort(cpu_temp_readings.begin(), cpu_temp_readings.end());
ASSERT_EQ(3U, cpu_temp_readings.size());
EXPECT_EQ(10.0f, cpu_temp_readings[0].temp_celsius);
EXPECT_EQ(subdir0.Append("temp1_label").value(), cpu_temp_readings[0].label);
EXPECT_EQ(20.0f, cpu_temp_readings[1].temp_celsius);
EXPECT_EQ("t2", cpu_temp_readings[1].label);
EXPECT_EQ(30.0f, cpu_temp_readings[2].temp_celsius);
EXPECT_EQ("t3", cpu_temp_readings[2].label);
}
} // namespace system
} // namespace chromeos