1/*
2 * Copyright (c) 2022 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#include <string>
18#include <cstdlib>
19
20#include "dfx_define.h"
21#include "rustc_demangle.h"
22
23using namespace testing;
24using namespace testing::ext;
25
26namespace OHOS {
27namespace HiviewDFX {
28static const int BUF_LEN = 100;
29
30class RustcDemangleTest : public testing::Test {
31public:
32    static void SetUpTestCase();
33    static void TearDownTestCase();
34    void SetUp();
35    void TearDown();
36};
37
38void RustcDemangleTest::SetUpTestCase()
39{}
40
41void RustcDemangleTest::TearDownTestCase()
42{}
43
44void RustcDemangleTest::SetUp()
45{}
46
47void RustcDemangleTest::TearDown()
48{}
49
50/**
51 * @tc.name: RustcDemangleTest001
52 * @tc.desc: test RustcDemangle ok
53 * @tc.type: FUNC
54 */
55HWTEST_F(RustcDemangleTest, RustcDemangleTest001, TestSize.Level2)
56{
57    GTEST_LOG_(INFO) << "RustcDemangleTest001: start.";
58    char buf[BUF_LEN] = "_RNvCs7FufEY4pgE0_21panic_handler_example4main\0";
59    char *res = reinterpret_cast<char*>(std::malloc(sizeof(char) * BUF_LEN));
60    res = rustc_demangle(buf);
61    int ret = -1;
62    if (res != nullptr) {
63        GTEST_LOG_(INFO) << "RustcDemangleTest001: res=" << std::string(res);
64        ret = strcmp(res, "panic_handler_example::main");
65        std::free(res);
66    }
67    EXPECT_EQ(true, ret == 0) << "RustcDemangleTest001 Failed";
68    GTEST_LOG_(INFO) << "RustcDemangleTest001: end.";
69}
70
71/**
72 * @tc.name: RustcDemangleTest002
73 * @tc.desc: test RustcDemangle not rust symbol
74 * @tc.type: FUNC
75 */
76HWTEST_F(RustcDemangleTest, RustcDemangleTest002, TestSize.Level2)
77{
78    GTEST_LOG_(INFO) << "RustcDemangleTest002: start.";
79    char buf[BUF_LEN] = "la la la\0";
80    char *res = reinterpret_cast<char*>(std::malloc(sizeof(char) * BUF_LEN));
81    res = rustc_demangle(buf);
82    EXPECT_EQ(true, res == nullptr) << "RustcDemangleTest002 Failed";
83    if (res != nullptr) {
84        free(res);
85    }
86    GTEST_LOG_(INFO) << "RustcDemangleTest002: end.";
87}
88
89/**
90 * @tc.name: RustcDemangleTest003
91 * @tc.desc: test RustcDemangle str null
92 * @tc.type: FUNC
93 */
94HWTEST_F(RustcDemangleTest, RustcDemangleTest003, TestSize.Level2)
95{
96    GTEST_LOG_(INFO) << "RustcDemangleTest003: start.";
97    char buf[BUF_LEN] = "\0";
98    char *res = reinterpret_cast<char*>(malloc(sizeof(char) * BUF_LEN));
99    res = rustc_demangle(buf);
100    EXPECT_EQ(true, res == nullptr) << "RustcDemangleTest003 Failed";
101    if (res != nullptr) {
102        std::free(res);
103    }
104    GTEST_LOG_(INFO) << "RustcDemangleTest003: end.";
105}
106
107/**
108 * @tc.name: RustcDemangleTest004
109 * @tc.desc: test RustcDemangle invalidid utf8
110 * @tc.type: FUNC
111 */
112HWTEST_F(RustcDemangleTest, RustcDemangleTest004, TestSize.Level2)
113{
114    GTEST_LOG_(INFO) << "RustcDemangleTest004: start.";
115    char buf[BUF_LEN] = "\xe7\x8a\xb6\xe6\x80\x81\0";
116    char *res = reinterpret_cast<char*>(std::malloc(sizeof(char) * BUF_LEN));
117    res = rustc_demangle(buf);
118    EXPECT_EQ(true, res == nullptr) << "RustcDemangleTest004 Failed";
119    if (res != nullptr) {
120        std::free(res);
121    }
122    GTEST_LOG_(INFO) << "RustcDemangleTest004: end.";
123}
124} // namespace HiviewDFX
125} // namepsace OHOS
126