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 "KernelWakesParser.h"
18
19 #include "frameworks/base/core/proto/android/os/kernelwake.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
29 using namespace android::base;
30 using namespace android::os;
31 using namespace std;
32 using ::testing::StrEq;
33 using ::testing::Test;
34 using ::testing::internal::CaptureStderr;
35 using ::testing::internal::CaptureStdout;
36 using ::testing::internal::GetCapturedStderr;
37 using ::testing::internal::GetCapturedStdout;
38
39 class KernelWakesParserTest : public Test {
40 public:
SetUp()41 virtual void SetUp() override {
42 ASSERT_TRUE(tf.fd != -1);
43 }
44
45 protected:
46 TemporaryFile tf;
47
48 const string kTestPath = GetExecutableDirectory();
49 const string kTestDataPath = kTestPath + "/testdata/";
50 };
51
TEST_F(KernelWakesParserTest,Short)52 TEST_F(KernelWakesParserTest, Short) {
53 const string testFile = kTestDataPath + "kernel_wakeups_short.txt";
54 KernelWakesParser parser;
55 KernelWakeSourcesProto expected;
56
57 KernelWakeSourcesProto::WakeupSource* record1 = expected.add_wakeup_sources();
58 record1->set_name("ab");
59 record1->set_active_count(8);
60 record1->set_last_change(123456123456LL);
61
62 KernelWakeSourcesProto::WakeupSource* record2 = expected.add_wakeup_sources();
63 record2->set_name("df");
64 record2->set_active_count(143);
65 record2->set_last_change(0LL);
66
67 int fd = open(testFile.c_str(), O_RDONLY);
68 ASSERT_TRUE(fd != -1);
69
70 CaptureStdout();
71 ASSERT_EQ(NO_ERROR, parser.Parse(fd, STDOUT_FILENO));
72 EXPECT_EQ(GetCapturedStdout(), expected.SerializeAsString());
73 close(fd);
74 }
75
TEST_F(KernelWakesParserTest,Normal)76 TEST_F(KernelWakesParserTest, Normal) {
77 const string testFile = kTestDataPath + "kernel_wakeups.txt";
78 KernelWakesParser parser;
79 KernelWakeSourcesProto expected;
80
81 KernelWakeSourcesProto::WakeupSource* record1 = expected.add_wakeup_sources();
82 record1->set_name("ipc000000ab_ATFWD-daemon");
83 record1->set_active_count(8);
84 record1->set_event_count(8);
85 record1->set_wakeup_count(0);
86 record1->set_expire_count(0);
87 record1->set_active_since(0l);
88 record1->set_total_time(0l);
89 record1->set_max_time(0l);
90 record1->set_last_change(131348LL);
91 record1->set_prevent_suspend_time(0LL);
92
93 KernelWakeSourcesProto::WakeupSource* record2 = expected.add_wakeup_sources();
94 record2->set_name("ipc000000aa_ATFWD-daemon");
95 record2->set_active_count(143);
96 record2->set_event_count(143);
97 record2->set_wakeup_count(0);
98 record2->set_expire_count(0);
99 record2->set_active_since(0l);
100 record2->set_total_time(123l);
101 record2->set_max_time(3l);
102 record2->set_last_change(2067286206LL);
103 record2->set_prevent_suspend_time(0LL);
104
105 int fd = open(testFile.c_str(), O_RDONLY);
106 ASSERT_TRUE(fd != -1);
107
108 CaptureStdout();
109 ASSERT_EQ(NO_ERROR, parser.Parse(fd, STDOUT_FILENO));
110 EXPECT_EQ(GetCapturedStdout(), expected.SerializeAsString());
111 close(fd);
112 }
113