• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved.
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 <gtest/gtest.h>
18 #include "stack_preprocess.h"
19 #include "native_hook_config.pb.h"
20 #include "plugin_module_api.h"
21 
22 using namespace std;
23 using namespace testing::ext;
24 using namespace OHOS::Developtools::NativeDaemon;
25 
26 namespace {
27 constexpr static uint32_t MAX_MATCH_INTERVAL = 3600;
28 constexpr static uint32_t MAX_MATCH_CNT = 1000;
29 
30 class StackPreprocessTest : public testing::Test {
31 public:
32 static void SetUpTestCase(void);
33 static void TearDownTestCase(void);
34 void SetUp();
35 void TearDown();
36 };
SetUpTestCase(void)37 void StackPreprocessTest::SetUpTestCase(void)
38 {
39 }
40 
TearDownTestCase(void)41 void StackPreprocessTest::TearDownTestCase(void)
42 {
43 }
44 
SetUp(void)45 void StackPreprocessTest::SetUp(void)
46 {
47 }
48 
TearDown(void)49 void StackPreprocessTest::TearDown(void)
50 {
51 }
52 
53 /*
54 @tc.name: StackPreprocessTest001
55 @tc.desc: test StackPreprocess with overcceeding max matching interval.
56 @tc.type: FUNC
57 */
58 HWTEST_F(StackPreprocessTest, StackPreprocessTest001, TestSize.Level1)
59 {
60     NativeHookConfig hookConfig;
61     hookConfig.set_malloc_free_matching_interval(MAX_MATCH_INTERVAL + 1);
62     StackPreprocess preprocess(nullptr, hookConfig, 0);
63     ASSERT_TRUE(preprocess.hookConfig_.malloc_free_matching_interval() == MAX_MATCH_INTERVAL);
64 }
65 
66 /*
67 @tc.name: StackPreprocessTest002
68 @tc.desc: test StackPreprocess with overcceeding max matching cnt.
69 @tc.type: FUNC
70 */
71 HWTEST_F(StackPreprocessTest, StackPreprocessTest002, TestSize.Level1)
72 {
73     NativeHookConfig hookConfig;
74     hookConfig.set_malloc_free_matching_cnt(MAX_MATCH_CNT + 1);
75     StackPreprocess preprocess(nullptr, hookConfig, 0);
76     ASSERT_TRUE(preprocess.hookConfig_.malloc_free_matching_cnt() == MAX_MATCH_CNT);
77 }
78 
79 /*
80 @tc.name: StackPreprocessTest003
81 @tc.desc: test StackPreprocess with save_file set to true but no file pointer provided.
82 @tc.type: FUNC
83 */
84 HWTEST_F(StackPreprocessTest, StackPreprocessTest003, TestSize.Level1)
85 {
86     NativeHookConfig hookConfig;
87     hookConfig.set_save_file(true);
88     FILE* fpHookData = nullptr;
89     StackPreprocess preprocess(nullptr, hookConfig, 0, fpHookData);
90     ASSERT_TRUE(hookConfig.save_file() && fpHookData == nullptr);
91 }
92 }