1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "CpuFreqParser.h"
18
19#include "frameworks/base/core/proto/android/os/cpufreq.pb.h"
20
21#include <android-base/file.h>
22#include <android-base/test_utils.h>
23#include <gmock/gmock.h>
24#include <google/protobuf/message_lite.h>
25#include <gtest/gtest.h>
26#include <string.h>
27#include <fcntl.h>
28
29using namespace android::base;
30using namespace android::os;
31using namespace std;
32using ::testing::StrEq;
33using ::testing::Test;
34using ::testing::internal::CaptureStderr;
35using ::testing::internal::CaptureStdout;
36using ::testing::internal::GetCapturedStderr;
37using ::testing::internal::GetCapturedStdout;
38
39class CpuFreqParserTest : public Test {
40public:
41    virtual void SetUp() override {
42        ASSERT_TRUE(tf.fd != -1);
43    }
44
45protected:
46    TemporaryFile tf;
47
48    const string kTestPath = GetExecutableDirectory();
49    const string kTestDataPath = kTestPath + "/testdata/";
50};
51
52TEST_F(CpuFreqParserTest, Success) {
53    const string testFile = kTestDataPath + "cpufreq.txt";
54    CpuFreqParser parser;
55    CpuFreqProto expected;
56
57    long jiffyHz = sysconf(_SC_CLK_TCK);
58    expected.set_jiffy_hz(jiffyHz);
59
60    CpuFreqProto::Stats::TimeInState* state;
61
62    CpuFreqProto::Stats* cpu0 = expected.add_cpu_freqs();
63    cpu0->set_cpu_name("cpu0");
64    state = cpu0->add_times();
65    state->set_state_khz(307200);
66    state->set_time_jiffy(23860761);
67    state = cpu0->add_times();
68    state->set_state_khz(384000);
69    state->set_time_jiffy(83124);
70    state = cpu0->add_times();
71    state->set_state_khz(768000);
72    state->set_time_jiffy(22652);
73
74    CpuFreqProto::Stats* cpu1 = expected.add_cpu_freqs();
75    cpu1->set_cpu_name("cpu1");
76    state = cpu1->add_times();
77    state->set_state_khz(307200);
78    state->set_time_jiffy(23860761);
79    state = cpu1->add_times();
80    state->set_state_khz(384000);
81    state->set_time_jiffy(83124);
82    state = cpu1->add_times();
83    state->set_state_khz(768000);
84    state->set_time_jiffy(22652);
85
86    CpuFreqProto::Stats* cpu2 = expected.add_cpu_freqs();
87    cpu2->set_cpu_name("cpu2");
88    state = cpu2->add_times();
89    state->set_state_khz(307200);
90    state->set_time_jiffy(23890935);
91    state = cpu2->add_times();
92    state->set_state_khz(384000);
93    state->set_time_jiffy(29383);
94    state = cpu2->add_times();
95    state->set_state_khz(748800);
96    state->set_time_jiffy(10547);
97    state = cpu2->add_times();
98    state->set_state_khz(825600);
99    state->set_time_jiffy(13173);
100
101    CpuFreqProto::Stats* cpu3 = expected.add_cpu_freqs();
102    cpu3->set_cpu_name("cpu3");
103    state = cpu3->add_times();
104    state->set_state_khz(307200);
105    state->set_time_jiffy(23890935);
106    state = cpu3->add_times();
107    state->set_state_khz(384000);
108    state->set_time_jiffy(29383);
109    state = cpu3->add_times();
110    state->set_state_khz(748800);
111    state->set_time_jiffy(10547);
112    state = cpu3->add_times();
113    state->set_state_khz(825600);
114    state->set_time_jiffy(13173);
115
116    int fd = open(testFile.c_str(), O_RDONLY);
117    ASSERT_TRUE(fd != -1);
118
119    CaptureStdout();
120    ASSERT_EQ(NO_ERROR, parser.Parse(fd, STDOUT_FILENO));
121    EXPECT_EQ(GetCapturedStdout(), expected.SerializeAsString());
122    close(fd);
123}
124