• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <dlfcn.h>
17 #include <gtest/gtest.h>
18 #include "address_handler.h"
19 #include "init_param.h"
20 
21 using namespace testing::ext;
22 
23 namespace {
24 
25 constexpr uint64_t TEST_ADDR = 64;
26 class AddressHandlerTest : public ::testing::Test {
27 public:
SetUpTestCase()28     static void SetUpTestCase() {}
TearDownTestCase()29     static void TearDownTestCase() {}
SetUp()30     void SetUp() {}
TearDown()31     void TearDown() {}
32 };
33 
34 /**
35  * @tc.name: LowAddrHandlerTest
36  * @tc.desc: Test LowAddrHandler.
37  * @tc.type: FUNC
38  */
39 HWTEST_F(AddressHandlerTest, LowAddrHandlerTest, TestSize.Level1)
40 {
41     std::unique_ptr<LowAddrHandler> handler = std::make_unique<LowAddrHandler>();
42     uint64_t noExistaddr = TEST_ADDR - 1;
43     handler->AddAllocAddr(TEST_ADDR);
44     ASSERT_TRUE(handler->CheckAddr(TEST_ADDR));
45     ASSERT_FALSE(handler->CheckAddr(noExistaddr));
46     handler = nullptr;
47 }
48 
49 /**
50  * @tc.name: MidAddrHandlerTest
51  * @tc.desc: Test MidAddrHandler.
52  * @tc.type: FUNC
53  */
54 #ifdef __aarch64__
55 HWTEST_F(AddressHandlerTest, MidAddrHandlerTest, TestSize.Level1)
56 {
57     std::unique_ptr<MidAddrHandler> handler = std::make_unique<MidAddrHandler>();
58     uint64_t addr = 0xFF00000;
59     uint64_t noExistaddr = addr - 1;
60     handler->AddAllocAddr(addr);
61     ASSERT_TRUE(handler->CheckAddr(addr));
62     ASSERT_FALSE(handler->CheckAddr(noExistaddr));
63     handler = nullptr;
64 }
65 #endif
66 
67 /**
68  * @tc.name: WholeAddrHandlerTest
69  * @tc.desc: Test WholeAddrHandler.
70  * @tc.type: FUNC
71  */
72 HWTEST_F(AddressHandlerTest, WholeAddrHandlerTest, TestSize.Level1)
73 {
74     std::unique_ptr<WholeAddrHandler> handler = std::make_unique<WholeAddrHandler>();
75     uint64_t noExistaddr = TEST_ADDR - 1;
76     handler->AddAllocAddr(TEST_ADDR);
77     ASSERT_TRUE(handler->CheckAddr(TEST_ADDR));
78     ASSERT_FALSE(handler->CheckAddr(noExistaddr));
79     handler = nullptr;
80 }
81 
82 /**
83  * @tc.name: LowMidAddrHandlerTest
84  * @tc.desc: Test LowAddrHandler and MidAddrHandler.
85  * @tc.type: FUNC
86  */
87 HWTEST_F(AddressHandlerTest, LowMidAddrHandlerTest, TestSize.Level1)
88 {
89     std::unique_ptr<LowAddrHandler> handlerLow = std::make_unique<LowAddrHandler>();
90     std::unique_ptr<MidAddrHandler> handlerMid = std::make_unique<MidAddrHandler>();
91     uint64_t addr = 0xFF000000000;
92     uint64_t noExistaddr = 0xFE000000000;
93     handlerLow->AddAllocAddr(addr);
94     handlerMid->AddAllocAddr(addr);
95     handlerLow->SetSuccessor(std::move(handlerMid));
96     ASSERT_TRUE(handlerLow->CheckAddr(addr));
97     ASSERT_FALSE(handlerLow->CheckAddr(noExistaddr));
98     handlerLow = nullptr;
99     handlerMid = nullptr;
100 }
101 
102 /**
103  * @tc.name: LowWholeAddrHandlerTest
104  * @tc.desc: Test LowAddrHandler and WholeAddrHandler.
105  * @tc.type: FUNC
106  */
107 #ifdef __aarch64__
108 HWTEST_F(AddressHandlerTest, LowWholeAddrHandlerTest, TestSize.Level1)
109 {
110     std::unique_ptr<LowAddrHandler> handlerLow = std::make_unique<LowAddrHandler>();
111     std::unique_ptr<WholeAddrHandler> handlerWhole = std::make_unique<WholeAddrHandler>();
112     uint64_t addr = 0xFF000000000;
113     uint64_t noExistaddr = 0xFE000000000;
114     handlerLow->AddAllocAddr(addr);
115     handlerWhole->AddAllocAddr(addr);
116     handlerLow->SetSuccessor(std::move(handlerWhole));
117     ASSERT_TRUE(handlerLow->CheckAddr(addr));
118     ASSERT_FALSE(handlerLow->CheckAddr(noExistaddr));
119     handlerLow = nullptr;
120     handlerWhole = nullptr;
121 }
122 #endif
123 
124 /**
125  * @tc.name: MidWholeAddrHandlerTest
126  * @tc.desc: Test MidAddrHandler and WholeAddrHandler.
127  * @tc.type: FUNC
128  */
129 HWTEST_F(AddressHandlerTest, MidWholeAddrHandlerTest, TestSize.Level1)
130 {
131     std::unique_ptr<MidAddrHandler> handlerMid = std::make_unique<MidAddrHandler>();
132     std::unique_ptr<WholeAddrHandler> handlerWhole = std::make_unique<WholeAddrHandler>();
133     uint64_t addr = TEST_ADDR;
134     uint64_t noExistaddr = addr - 1;
135     handlerMid->AddAllocAddr(addr);
136     handlerWhole->AddAllocAddr(addr);
137     handlerMid->SetSuccessor(std::move(handlerWhole));
138     ASSERT_TRUE(handlerMid->CheckAddr(addr));
139     ASSERT_FALSE(handlerMid->CheckAddr(noExistaddr));
140     handlerMid = nullptr;
141     handlerWhole = nullptr;
142 }
143 
144 /**
145  * @tc.name: AllAddrHandlerTest
146  * @tc.desc: Test LowAddrHandler, MidAddrHandler and WholeAddrHandler.
147  * @tc.type: FUNC
148  */
149 HWTEST_F(AddressHandlerTest, AllAddrHandlerTest, TestSize.Level1)
150 {
151     std::unique_ptr<LowAddrHandler> handlerLow = std::make_unique<LowAddrHandler>();
152     std::unique_ptr<MidAddrHandler> handlerMid = std::make_unique<MidAddrHandler>();
153     std::unique_ptr<WholeAddrHandler> handlerWhole = std::make_unique<WholeAddrHandler>();
154     uint64_t addr = 0xFF0000000000000;
155     uint64_t noExistaddr = 0xEF0000000000000;
156     handlerLow->AddAllocAddr(addr);
157     handlerWhole->AddAllocAddr(addr);
158     handlerMid->AddAllocAddr(addr);
159     ASSERT_TRUE(handlerLow->CheckAddr(addr));
160     handlerMid->SetSuccessor(std::move(handlerWhole));
161     handlerLow->SetSuccessor(std::move(handlerMid));
162     ASSERT_TRUE(handlerLow->CheckAddr(addr));
163     ASSERT_FALSE(handlerLow->CheckAddr(noExistaddr));
164     handlerLow = nullptr;
165     handlerMid = nullptr;
166     handlerWhole = nullptr;
167 }
168 }