• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <ctime>
18 #include <securec.h>
19 #include <string>
20 #include <vector>
21 
22 #include "dfx_elf.h"
23 #include "dfx_memory_file.h"
24 
25 using namespace OHOS::HiviewDFX;
26 using namespace testing::ext;
27 using namespace std;
28 
29 static const char DUMPCATCHER_ELF_FILE[] = "/system/bin/dumpcatcher";
30 
31 namespace OHOS {
32 namespace HiviewDFX {
33 class DfxElfTest : public testing::Test {
34 public:
SetUpTestCase(void)35     static void SetUpTestCase(void) {}
TearDownTestCase(void)36     static void TearDownTestCase(void) {}
SetUp()37     void SetUp() {}
TearDown()38     void TearDown() {}
39 };
40 
41 namespace {
42 /**
43  * @tc.name: DfxElfTest001
44  * @tc.desc: test DfxElf class functions
45  * @tc.type: FUNC
46  */
47 HWTEST_F(DfxElfTest, DfxElfTest001, TestSize.Level2)
48 {
49     GTEST_LOG_(INFO) << "DfxElfTest001: start.";
50     auto memory = DfxMemoryFile::CreateFileMemory(DUMPCATCHER_ELF_FILE, 0);
51     if (memory == nullptr) {
52         FAIL();
53     }
54     auto elf = std::make_shared<DfxElf>(memory);
55     EXPECT_EQ(true, elf != nullptr) << "DfxElfTest001: failed";
56     if (!elf->Init() || !elf->IsValid()) {
57         GTEST_LOG_(INFO) << "DfxElfTest001: not a valid elf file.";
58         FAIL();
59     }
60     std::string buildIdHex = elf->GetBuildID();
61     if (!buildIdHex.empty()) {
62         printf("Build ID hex: ");
63         for (size_t i = 0; i < buildIdHex.size(); ++i) {
64             printf("%02hhx", buildIdHex[i]);
65         }
66         printf("\n");
67         std::string buildId = DfxElf::GetReadableBuildID(buildIdHex);
68         printf("Build ID: %s\n", buildId.c_str());
69     }
70     GTEST_LOG_(INFO) << "DfxElfTest001: end.";
71 }
72 }
73 } // namespace HiviewDFX
74 } // namespace OHOS
75 
76