1 /* 2 * Copyright (c) 2023 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 "disassembler.h" 19 20 using namespace testing::ext; 21 22 namespace panda::disasm { 23 class DisassemblerModuleLiteralTest : public testing::Test { 24 public: SetUpTestCase(void)25 static void SetUpTestCase(void) {}; TearDownTestCase(void)26 static void TearDownTestCase(void) {}; SetUp()27 void SetUp() {}; TearDown()28 void TearDown() {}; 29 ValidateModuleLiteral(const std::vector<std::string> & module_literals,const std::vector<std::string> & expected_module_literals)30 bool ValidateModuleLiteral(const std::vector<std::string> &module_literals, 31 const std::vector<std::string> &expected_module_literals) 32 { 33 for (const auto &expected_module_literal : expected_module_literals) { 34 const auto module_iter = std::find(module_literals.begin(), module_literals.end(), expected_module_literal); 35 if (module_iter == module_literals.end()) { 36 return false; 37 } 38 } 39 40 return true; 41 } 42 }; 43 44 /** 45 * @tc.name: disassembler_module_literal_test_001 46 * @tc.desc: get module literal of abc file. 47 * @tc.type: FUNC 48 * @tc.require: file path and name 49 */ 50 HWTEST_F(DisassemblerModuleLiteralTest, disassembler_module_literal_test_001, TestSize.Level1) 51 { 52 const std::string file_name = GRAPH_TEST_ABC_DIR "module-regular-import.abc"; 53 std::vector<std::string> expected_module_literals = { 54 "\tMODULE_REQUEST_ARRAY: {\n\t\t0 : ./module-export-file.js,\n\t}", 55 "\tModuleTag: REGULAR_IMPORT, local_name: a, import_name: a, module_request: ./module-export-file.js" 56 }; 57 panda::disasm::Disassembler disasm {}; 58 disasm.Disassemble(file_name, false, false); 59 std::vector<std::string> module_literals = disasm.GetModuleLiterals(); 60 bool has_module_literal = ValidateModuleLiteral(module_literals, expected_module_literals); 61 EXPECT_TRUE(has_module_literal); 62 } 63 64 /** 65 * @tc.name: disassembler_module_literal_test_002 66 * @tc.desc: get module literal of abc file. 67 * @tc.type: FUNC 68 * @tc.require: file path and name 69 */ 70 HWTEST_F(DisassemblerModuleLiteralTest, disassembler_module_literal_test_002, TestSize.Level1) 71 { 72 const std::string file_name = GRAPH_TEST_ABC_DIR "module-namespace-import.abc"; 73 std::vector<std::string> expected_module_literals = { 74 "\tMODULE_REQUEST_ARRAY: {\n\t\t0 : ./module-export-file.js,\n\t}", 75 "\tModuleTag: NAMESPACE_IMPORT, local_name: ns, module_request: ./module-export-file.js" 76 }; 77 panda::disasm::Disassembler disasm {}; 78 disasm.Disassemble(file_name, false, false); 79 std::vector<std::string> module_literals = disasm.GetModuleLiterals(); 80 bool has_module_literal = ValidateModuleLiteral(module_literals, expected_module_literals); 81 EXPECT_TRUE(has_module_literal); 82 } 83 84 /** 85 * @tc.name: disassembler_module_literal_test_003 86 * @tc.desc: get module literal of abc file. 87 * @tc.type: FUNC 88 * @tc.require: file path and name 89 */ 90 HWTEST_F(DisassemblerModuleLiteralTest, disassembler_module_literal_test_003, TestSize.Level1) 91 { 92 const std::string file_name = GRAPH_TEST_ABC_DIR "module-local-export.abc"; 93 std::vector<std::string> expected_module_literals = { 94 "\tMODULE_REQUEST_ARRAY: {\n\t}", 95 "\tModuleTag: LOCAL_EXPORT, local_name: c, export_name: c" 96 }; 97 panda::disasm::Disassembler disasm {}; 98 disasm.Disassemble(file_name, false, false); 99 std::vector<std::string> module_literals = disasm.GetModuleLiterals(); 100 bool has_module_literal = ValidateModuleLiteral(module_literals, expected_module_literals); 101 EXPECT_TRUE(has_module_literal); 102 } 103 104 /** 105 * @tc.name: disassembler_module_literal_test_004 106 * @tc.desc: get module literal of abc file. 107 * @tc.type: FUNC 108 * @tc.require: file path and name 109 */ 110 HWTEST_F(DisassemblerModuleLiteralTest, disassembler_module_literal_test_004, TestSize.Level1) 111 { 112 const std::string file_name = GRAPH_TEST_ABC_DIR "module-indirect-export.abc"; 113 std::vector<std::string> expected_module_literals = { 114 "\tMODULE_REQUEST_ARRAY: {\n\t\t0 : ./module-import-file.js,\n\t}", 115 "\tModuleTag: INDIRECT_EXPORT, export_name: a, import_name: a, module_request: ./module-import-file.js" 116 }; 117 panda::disasm::Disassembler disasm {}; 118 disasm.Disassemble(file_name, false, false); 119 std::vector<std::string> module_literals = disasm.GetModuleLiterals(); 120 bool has_module_literal = ValidateModuleLiteral(module_literals, expected_module_literals); 121 EXPECT_TRUE(has_module_literal); 122 } 123 124 /** 125 * @tc.name: disassembler_module_literal_test_005 126 * @tc.desc: get module literal of abc file. 127 * @tc.type: FUNC 128 * @tc.require: file path and name 129 */ 130 HWTEST_F(DisassemblerModuleLiteralTest, disassembler_module_literal_test_005, TestSize.Level1) 131 { 132 const std::string file_name = GRAPH_TEST_ABC_DIR "module-start-export.abc"; 133 std::vector<std::string> expected_module_literals = { 134 "\tMODULE_REQUEST_ARRAY: {\n\t\t0 : ./module-import-file.js,\n\t}", 135 "\tModuleTag: STAR_EXPORT, module_request: ./module-import-file.js" 136 }; 137 panda::disasm::Disassembler disasm {}; 138 disasm.Disassemble(file_name, false, false); 139 std::vector<std::string> module_literals = disasm.GetModuleLiterals(); 140 bool has_module_literal = ValidateModuleLiteral(module_literals, expected_module_literals); 141 EXPECT_TRUE(has_module_literal); 142 } 143 144 /** 145 * @tc.name: disassembler_module_literal_test_005 146 * @tc.desc: get module literal of abc file. 147 * @tc.type: FUNC 148 * @tc.require: file path and name 149 */ 150 HWTEST_F(DisassemblerModuleLiteralTest, disassembler_module_literal_test_006, TestSize.Level1) 151 { 152 const std::string file_name = GRAPH_TEST_ABC_DIR "module-regular-import-local-export.abc"; 153 std::vector<std::string> expected_module_literals = { 154 "\tMODULE_REQUEST_ARRAY: {\n\t\t0 : ./module-indirect-export.js,\n\t}", 155 "\tModuleTag: REGULAR_IMPORT, local_name: a, import_name: a, module_request: ./module-indirect-export.js", 156 "\tModuleTag: LOCAL_EXPORT, local_name: b, export_name: b" 157 }; 158 panda::disasm::Disassembler disasm {}; 159 disasm.Disassemble(file_name, false, false); 160 std::vector<std::string> module_literals = disasm.GetModuleLiterals(); 161 bool has_module_literal = ValidateModuleLiteral(module_literals, expected_module_literals); 162 EXPECT_TRUE(has_module_literal); 163 } 164 } 165