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 "PageTypeInfoParser.h"
18
19#include "frameworks/base/core/proto/android/os/pagetypeinfo.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 PageTypeInfoParserTest : 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(PageTypeInfoParserTest, Success) {
53    const string testFile = kTestDataPath + "pagetypeinfo.txt";
54    PageTypeInfoParser parser;
55    PageTypeInfoProto expected;
56
57    expected.set_page_block_order(10);
58    expected.set_pages_per_block(1024);
59
60    PageTypeInfoProto::MigrateType* mt1 = expected.add_migrate_types();
61    mt1->set_node(0);
62    mt1->set_zone("DMA");
63    mt1->set_type("Unmovable");
64    int arr1[] = { 426, 279, 226, 1, 1, 1, 0, 0, 2, 2, 0};
65    for (auto i=0; i<11; i++) {
66        mt1->add_free_pages_count(arr1[i]);
67    }
68
69    PageTypeInfoProto::MigrateType* mt2 = expected.add_migrate_types();
70    mt2->set_node(0);
71    mt2->set_zone("Normal");
72    mt2->set_type("Reclaimable");
73    int arr2[] = { 953, 773, 437, 154, 92, 26, 15, 14, 12, 7, 0};
74    for (auto i=0; i<11; i++) {
75        mt2->add_free_pages_count(arr2[i]);
76    }
77
78    PageTypeInfoProto::Block* block1 = expected.add_blocks();
79    block1->set_node(0);
80    block1->set_zone("DMA");
81    block1->set_unmovable(74);
82    block1->set_reclaimable(9);
83    block1->set_movable(337);
84    block1->set_cma(41);
85    block1->set_reserve(1);
86    block1->set_isolate(0);
87
88
89    PageTypeInfoProto::Block* block2 = expected.add_blocks();
90    block2->set_node(0);
91    block2->set_zone("Normal");
92    block2->set_unmovable(70);
93    block2->set_reclaimable(12);
94    block2->set_movable(423);
95    block2->set_cma(0);
96    block2->set_reserve(1);
97    block2->set_isolate(0);
98
99    int fd = open(testFile.c_str(), O_RDONLY);
100    ASSERT_TRUE(fd != -1);
101
102    CaptureStdout();
103    ASSERT_EQ(NO_ERROR, parser.Parse(fd, STDOUT_FILENO));
104    EXPECT_EQ(GetCapturedStdout(), expected.SerializeAsString());
105    close(fd);
106}
107