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 <cstdlib> 17 #include <filesystem> 18 #include <gtest/gtest.h> 19 #include <iostream> 20 #include <string> 21 22 #include "disassembler.h" 23 24 using namespace testing::ext; 25 namespace panda::disasm { 26 27 static const std::string SOURCE_FILE_PATH = GRAPH_TEST_ABC_DIR "line-number1.abc"; 28 static const std::string FILE_NAME = "line-number1.abc"; 29 static const std::string CHECK_MESSAGE = "# source binary: " + FILE_NAME; 30 31 class DisassemblerGetFileNameTest : public testing::Test { 32 public: SetUpTestCase(void)33 static void SetUpTestCase(void) 34 { 35 std::filesystem::path file_name{ FILE_NAME }; 36 std::filesystem::path src_file_name{ SOURCE_FILE_PATH }; 37 if (std::filesystem::exists(file_name)) { 38 std::filesystem::remove(file_name); 39 } 40 std::filesystem::copy(src_file_name, file_name); 41 }; 42 TearDownTestCase(void)43 static void TearDownTestCase(void) 44 { 45 std::filesystem::path file_name{ FILE_NAME }; 46 std::filesystem::remove(file_name); 47 }; 48 SetUp()49 void SetUp() {}; TearDown()50 void TearDown() {}; 51 Find(std::stringstream & ss,const std::string & dst)52 bool Find(std::stringstream &ss, const std::string &dst) 53 { 54 return ss.str().find(dst) != std::string::npos; 55 } 56 }; 57 58 /** 59 * @tc.name: disassembler_getfilename_test_001 60 * @tc.desc: test disasm with relative path. 61 * @tc.type: FUNC 62 * @tc.require: file path and name 63 */ 64 HWTEST_F(DisassemblerGetFileNameTest, disassembler_getfilename_test_001, TestSize.Level1) 65 { 66 panda::disasm::Disassembler disasm {}; 67 disasm.Disassemble(SOURCE_FILE_PATH, false, false); 68 std::stringstream ss {}; 69 disasm.Serialize(ss); 70 EXPECT_TRUE(Find(ss, CHECK_MESSAGE)); 71 } 72 73 /** 74 * @tc.name: disassembler_getfilename_test_002 75 * @tc.desc: test disasm with file name. 76 * @tc.type: FUNC 77 * @tc.require: file path and name 78 */ 79 HWTEST_F(DisassemblerGetFileNameTest, disassembler_getfilename_test_002, TestSize.Level1) 80 { 81 panda::disasm::Disassembler disasm {}; 82 disasm.Disassemble(FILE_NAME, false, false); 83 std::stringstream ss {}; 84 disasm.Serialize(ss); 85 EXPECT_TRUE(Find(ss, CHECK_MESSAGE)); 86 } 87 88 /** 89 * @tc.name: disassembler_getfilename_test_003 90 * @tc.desc: test disasm with absolute path. 91 * @tc.type: FUNC 92 * @tc.require: file path and name 93 */ 94 HWTEST_F(DisassemblerGetFileNameTest, disassembler_getfilename_test_003, TestSize.Level1) 95 { 96 panda::disasm::Disassembler disasm {}; 97 disasm.Disassemble(std::filesystem::absolute(SOURCE_FILE_PATH).u8string(), false, false); 98 std::stringstream ss {}; 99 disasm.Serialize(ss); 100 EXPECT_TRUE(Find(ss, CHECK_MESSAGE)); 101 } 102 }; 103