1800b99b8Sopenharmony_ci/*
2800b99b8Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License.
5800b99b8Sopenharmony_ci * You may obtain a copy of the License at
6800b99b8Sopenharmony_ci *
7800b99b8Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8800b99b8Sopenharmony_ci *
9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and
13800b99b8Sopenharmony_ci * limitations under the License.
14800b99b8Sopenharmony_ci */
15800b99b8Sopenharmony_ci
16800b99b8Sopenharmony_ci#include <gtest/gtest.h>
17800b99b8Sopenharmony_ci#include <string>
18800b99b8Sopenharmony_ci#include <cstdlib>
19800b99b8Sopenharmony_ci
20800b99b8Sopenharmony_ci#include "dfx_define.h"
21800b99b8Sopenharmony_ci#include "rustc_demangle.h"
22800b99b8Sopenharmony_ci
23800b99b8Sopenharmony_ciusing namespace testing;
24800b99b8Sopenharmony_ciusing namespace testing::ext;
25800b99b8Sopenharmony_ci
26800b99b8Sopenharmony_cinamespace OHOS {
27800b99b8Sopenharmony_cinamespace HiviewDFX {
28800b99b8Sopenharmony_cistatic const int BUF_LEN = 100;
29800b99b8Sopenharmony_ci
30800b99b8Sopenharmony_ciclass RustcDemangleTest : public testing::Test {
31800b99b8Sopenharmony_cipublic:
32800b99b8Sopenharmony_ci    static void SetUpTestCase();
33800b99b8Sopenharmony_ci    static void TearDownTestCase();
34800b99b8Sopenharmony_ci    void SetUp();
35800b99b8Sopenharmony_ci    void TearDown();
36800b99b8Sopenharmony_ci};
37800b99b8Sopenharmony_ci
38800b99b8Sopenharmony_civoid RustcDemangleTest::SetUpTestCase()
39800b99b8Sopenharmony_ci{}
40800b99b8Sopenharmony_ci
41800b99b8Sopenharmony_civoid RustcDemangleTest::TearDownTestCase()
42800b99b8Sopenharmony_ci{}
43800b99b8Sopenharmony_ci
44800b99b8Sopenharmony_civoid RustcDemangleTest::SetUp()
45800b99b8Sopenharmony_ci{}
46800b99b8Sopenharmony_ci
47800b99b8Sopenharmony_civoid RustcDemangleTest::TearDown()
48800b99b8Sopenharmony_ci{}
49800b99b8Sopenharmony_ci
50800b99b8Sopenharmony_ci/**
51800b99b8Sopenharmony_ci * @tc.name: RustcDemangleTest001
52800b99b8Sopenharmony_ci * @tc.desc: test RustcDemangle ok
53800b99b8Sopenharmony_ci * @tc.type: FUNC
54800b99b8Sopenharmony_ci */
55800b99b8Sopenharmony_ciHWTEST_F(RustcDemangleTest, RustcDemangleTest001, TestSize.Level2)
56800b99b8Sopenharmony_ci{
57800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "RustcDemangleTest001: start.";
58800b99b8Sopenharmony_ci    char buf[BUF_LEN] = "_RNvCs7FufEY4pgE0_21panic_handler_example4main\0";
59800b99b8Sopenharmony_ci    char *res = reinterpret_cast<char*>(std::malloc(sizeof(char) * BUF_LEN));
60800b99b8Sopenharmony_ci    res = rustc_demangle(buf);
61800b99b8Sopenharmony_ci    int ret = -1;
62800b99b8Sopenharmony_ci    if (res != nullptr) {
63800b99b8Sopenharmony_ci        GTEST_LOG_(INFO) << "RustcDemangleTest001: res=" << std::string(res);
64800b99b8Sopenharmony_ci        ret = strcmp(res, "panic_handler_example::main");
65800b99b8Sopenharmony_ci        std::free(res);
66800b99b8Sopenharmony_ci    }
67800b99b8Sopenharmony_ci    EXPECT_EQ(true, ret == 0) << "RustcDemangleTest001 Failed";
68800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "RustcDemangleTest001: end.";
69800b99b8Sopenharmony_ci}
70800b99b8Sopenharmony_ci
71800b99b8Sopenharmony_ci/**
72800b99b8Sopenharmony_ci * @tc.name: RustcDemangleTest002
73800b99b8Sopenharmony_ci * @tc.desc: test RustcDemangle not rust symbol
74800b99b8Sopenharmony_ci * @tc.type: FUNC
75800b99b8Sopenharmony_ci */
76800b99b8Sopenharmony_ciHWTEST_F(RustcDemangleTest, RustcDemangleTest002, TestSize.Level2)
77800b99b8Sopenharmony_ci{
78800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "RustcDemangleTest002: start.";
79800b99b8Sopenharmony_ci    char buf[BUF_LEN] = "la la la\0";
80800b99b8Sopenharmony_ci    char *res = reinterpret_cast<char*>(std::malloc(sizeof(char) * BUF_LEN));
81800b99b8Sopenharmony_ci    res = rustc_demangle(buf);
82800b99b8Sopenharmony_ci    EXPECT_EQ(true, res == nullptr) << "RustcDemangleTest002 Failed";
83800b99b8Sopenharmony_ci    if (res != nullptr) {
84800b99b8Sopenharmony_ci        free(res);
85800b99b8Sopenharmony_ci    }
86800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "RustcDemangleTest002: end.";
87800b99b8Sopenharmony_ci}
88800b99b8Sopenharmony_ci
89800b99b8Sopenharmony_ci/**
90800b99b8Sopenharmony_ci * @tc.name: RustcDemangleTest003
91800b99b8Sopenharmony_ci * @tc.desc: test RustcDemangle str null
92800b99b8Sopenharmony_ci * @tc.type: FUNC
93800b99b8Sopenharmony_ci */
94800b99b8Sopenharmony_ciHWTEST_F(RustcDemangleTest, RustcDemangleTest003, TestSize.Level2)
95800b99b8Sopenharmony_ci{
96800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "RustcDemangleTest003: start.";
97800b99b8Sopenharmony_ci    char buf[BUF_LEN] = "\0";
98800b99b8Sopenharmony_ci    char *res = reinterpret_cast<char*>(malloc(sizeof(char) * BUF_LEN));
99800b99b8Sopenharmony_ci    res = rustc_demangle(buf);
100800b99b8Sopenharmony_ci    EXPECT_EQ(true, res == nullptr) << "RustcDemangleTest003 Failed";
101800b99b8Sopenharmony_ci    if (res != nullptr) {
102800b99b8Sopenharmony_ci        std::free(res);
103800b99b8Sopenharmony_ci    }
104800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "RustcDemangleTest003: end.";
105800b99b8Sopenharmony_ci}
106800b99b8Sopenharmony_ci
107800b99b8Sopenharmony_ci/**
108800b99b8Sopenharmony_ci * @tc.name: RustcDemangleTest004
109800b99b8Sopenharmony_ci * @tc.desc: test RustcDemangle invalidid utf8
110800b99b8Sopenharmony_ci * @tc.type: FUNC
111800b99b8Sopenharmony_ci */
112800b99b8Sopenharmony_ciHWTEST_F(RustcDemangleTest, RustcDemangleTest004, TestSize.Level2)
113800b99b8Sopenharmony_ci{
114800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "RustcDemangleTest004: start.";
115800b99b8Sopenharmony_ci    char buf[BUF_LEN] = "\xe7\x8a\xb6\xe6\x80\x81\0";
116800b99b8Sopenharmony_ci    char *res = reinterpret_cast<char*>(std::malloc(sizeof(char) * BUF_LEN));
117800b99b8Sopenharmony_ci    res = rustc_demangle(buf);
118800b99b8Sopenharmony_ci    EXPECT_EQ(true, res == nullptr) << "RustcDemangleTest004 Failed";
119800b99b8Sopenharmony_ci    if (res != nullptr) {
120800b99b8Sopenharmony_ci        std::free(res);
121800b99b8Sopenharmony_ci    }
122800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "RustcDemangleTest004: end.";
123800b99b8Sopenharmony_ci}
124800b99b8Sopenharmony_ci} // namespace HiviewDFX
125800b99b8Sopenharmony_ci} // namepsace OHOS
126