1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
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 <gtest/gtest.h>
17 
18 #include <cstdio>
19 #include <cstdlib>
20 #include <memory>
21 #include <mutex>
22 #include <thread>
23 
24 #include <dlfcn.h>
25 #include <fcntl.h>
26 #include <securec.h>
27 #include <sys/wait.h>
28 #include <unistd.h>
29 
30 #include "async_stack.h"
31 #include "dfx_test_util.h"
32 #include "elapsed_time.h"
33 #include "fp_unwinder.h"
34 
35 using namespace testing;
36 using namespace testing::ext;
37 
38 namespace OHOS {
39 namespace HiviewDFX {
40 #undef LOG_DOMAIN
41 #undef LOG_TAG
42 #define LOG_TAG "AsyncStackTest"
43 #define LOG_DOMAIN 0xD002D11
44 
45 class AsyncStackTest : public testing::Test {
46 public:
47     static void SetUpTestCase();
48     static void TearDownTestCase();
49     void SetUp();
50     void TearDown();
51 };
52 
SetUpTestCase()53 void AsyncStackTest::SetUpTestCase()
54 {}
55 
TearDownTestCase()56 void AsyncStackTest::TearDownTestCase()
57 {}
58 
SetUp()59 void AsyncStackTest::SetUp()
60 {}
61 
TearDown()62 void AsyncStackTest::TearDown()
63 {}
64 
65 /**
66  * @tc.name: AsyncStackTest001
67  * @tc.desc: test GetStackId()
68  * @tc.type: FUNC
69  */
70 HWTEST_F(AsyncStackTest, AsyncStackTest001, TestSize.Level2)
71 {
72     GTEST_LOG_(INFO) << "AsyncStackTest001: start.";
73     SetStackId(1);
74     auto res = GetStackId();
75     GTEST_LOG_(INFO) << "res: " << res;
76 #if defined(__arm__)
77     ASSERT_EQ(0, res);
78 #elif defined(__aarch64__)
79     ASSERT_NE(0, res);
80 #endif
81 
82     GTEST_LOG_(INFO) << "AsyncStackTest001: end.";
83 }
84 
85 /**
86  * @tc.name: AsyncStackTest002
87  * @tc.desc: test CollectAsyncStack()
88  * @tc.type: FUNC
89  */
90 HWTEST_F(AsyncStackTest, AsyncStackTest002, TestSize.Level2)
91 {
92     GTEST_LOG_(INFO) << "AsyncStackTest002: start.";
93     auto ret = CollectAsyncStack();
94 #if defined(__aarch64__)
95     ASSERT_NE(0, ret);
96 #else
97     ASSERT_EQ(0, ret);
98 #endif
99     GTEST_LOG_(INFO) << "AsyncStackTest002: end.";
100 }
101 
102 /**
103  * @tc.name: AsyncStackTest003
104  * @tc.desc: test UnwindFallback Function
105  * @tc.type: FUNC
106  */
107 HWTEST_F(AsyncStackTest, AsyncStackTest003, TestSize.Level2)
108 {
109     GTEST_LOG_(INFO) << "AsyncStackTest003: start.";
110     const int32_t maxSize = 16;
111     uintptr_t pcs[maxSize] = {0};
112     int32_t skipFrameNum = 2;
113     std::thread (FpUnwinder::Unwind, pcs, maxSize, skipFrameNum).join();
114     int32_t ret = FpUnwinder::UnwindFallback(pcs, maxSize, skipFrameNum);
115 #if defined(__arm__)
116     ASSERT_EQ(0, ret);
117 #elif defined(__aarch64__)
118     ASSERT_NE(0, ret);
119 #endif
120     GTEST_LOG_(INFO) << "AsyncStackTest003: end.";
121 }
122 } // namespace HiviewDFX
123 } // namepsace OHOS
124