• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cassert>
17 #include <cstring>
18 #include <gtest/gtest.h>
19 
20 #include "libabckit/include/c/abckit.h"
21 #include "helpers/helpers.h"
22 #include "libabckit/include/c/metadata_core.h"
23 #include "logger.h"
24 
25 namespace libabckit::test {
26 
27 static auto g_impl = AbckitGetApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
28 static auto g_implI = AbckitGetInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
29 
30 class LibAbcKitInspectApiFilesTest : public ::testing::Test {};
31 
32 // Test: test-kind=api, api=InspectApiImpl::fileGetVersion, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitInspectApiFilesTest,StaticFileGetVersion)33 TEST_F(LibAbcKitInspectApiFilesTest, StaticFileGetVersion)
34 {
35     LIBABCKIT_LOG(DEBUG) << "StaticFileGetVersion source: " << ABCKIT_TEST_DIR "ut/metadata_core/inspect_api/files\n";
36 
37     AbckitFile *file = nullptr;
38     helpers::AssertOpenAbc(ABCKIT_ABC_DIR "ut/metadata_core/inspect_api/files/file_static.abc", &file);
39 
40     AbckitFileVersion version = g_implI->fileGetVersion(file);
41     std::array<uint8_t, ABCKIT_VERSION_SIZE> expectedVersion = {0, 0, 0, 5};
42 
43     const auto versionsEquality = std::memcmp(expectedVersion.data(), version, sizeof(uint8_t) * ABCKIT_VERSION_SIZE);
44     ASSERT_EQ(versionsEquality, 0);
45 
46     g_impl->closeFile(file);
47     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
48 }
49 
50 // Test: test-kind=api, api=InspectApiImpl::fileGetVersion, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitInspectApiFilesTest,DynamicFileGetVersion)51 TEST_F(LibAbcKitInspectApiFilesTest, DynamicFileGetVersion)
52 {
53     LIBABCKIT_LOG(DEBUG) << "StaticFileGetVersion source: " << ABCKIT_TEST_DIR "ut/metadata_core/inspect_api/files\n";
54 
55     AbckitFile *file = nullptr;
56     helpers::AssertOpenAbc(ABCKIT_ABC_DIR "ut/metadata_core/inspect_api/files/file_dynamic.abc", &file);
57 
58     AbckitFileVersion version = g_implI->fileGetVersion(file);
59     // NOLINTNEXTLINE(readability-magic-numbers)
60     std::array<uint8_t, ABCKIT_VERSION_SIZE> expectedVersion = {13, 0, 1, 0};
61     const auto versionsEquality = std::memcmp(expectedVersion.data(), version, sizeof(uint8_t) * ABCKIT_VERSION_SIZE);
62     ASSERT_EQ(versionsEquality, 0);
63 
64     g_impl->closeFile(file);
65     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
66 }
67 
68 // Test: test-kind=api, api=InspectApiImpl::fileEnumerateModules, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitInspectApiFilesTest,DynamicFileEnumerateModules)69 TEST_F(LibAbcKitInspectApiFilesTest, DynamicFileEnumerateModules)
70 {
71     AbckitFile *file = nullptr;
72     helpers::AssertOpenAbc(ABCKIT_ABC_DIR "ut/metadata_core/inspect_api/modules/modules_dynamic.abc", &file);
73 
74     std::unordered_map<std::string, AbckitCoreModule *> gotModulesMap;
75 
76     g_implI->fileEnumerateModules(file, &gotModulesMap, helpers::NameToModuleCollector);
77     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
78 
79     std::set<std::string> expectedNames = {"modules_dynamic", "modules/module1", "modules/module2", "modules/module3"};
80     ASSERT_EQ(expectedNames.size(), gotModulesMap.size());
81     for (auto &expectedName : expectedNames) {
82         ASSERT_NE(gotModulesMap.find(expectedName), gotModulesMap.end());
83     }
84 
85     constexpr auto OUTPUT_PATH = ABCKIT_ABC_DIR "ut/metadata_core/inspect_api/modules/modules_dynamic_modified.abc";
86     g_impl->writeAbc(file, OUTPUT_PATH, strlen(OUTPUT_PATH));
87     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
88     g_impl->closeFile(file);
89     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
90 }
91 
92 // NOTE: fix after ets2bundle integrated into abckit unit tests
93 // Test: test-kind=api, api=InspectApiImpl::fileEnumerateExternalModules, abc-kind=ArkTS1, category=positive,
94 // extension=c
TEST_F(LibAbcKitInspectApiFilesTest,DISABLED_DynamicFileEnumerateExternalModules)95 TEST_F(LibAbcKitInspectApiFilesTest, DISABLED_DynamicFileEnumerateExternalModules)
96 {
97     AbckitFile *file = nullptr;
98     helpers::AssertOpenAbc(ABCKIT_ABC_DIR "ut/metadata_core/inspect_api/modules/modules_dynamic.abc", &file);
99 
100     std::unordered_map<std::string, AbckitCoreModule *> gotModulesMap;
101 
102     g_implI->fileEnumerateExternalModules(file, &gotModulesMap, helpers::NameToModuleCollector);
103     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
104 
105     std::set<std::string> expectedNames = {"modules/module1", "modules/module2", "modules/module3"};
106     ASSERT_EQ(expectedNames.size(), gotModulesMap.size());
107     for (auto &expectedName : expectedNames) {
108         ASSERT_NE(gotModulesMap.find(expectedName), gotModulesMap.end());
109     }
110 
111     constexpr auto OUTPUT_PATH = ABCKIT_ABC_DIR "ut/metadata_core/inspect_api/modules/modules_dynamic_modified.abc";
112     g_impl->writeAbc(file, OUTPUT_PATH, strlen(OUTPUT_PATH));
113     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
114     g_impl->closeFile(file);
115     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
116 }
117 
118 }  // namespace libabckit::test
119