• 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 <gtest/gtest.h>
17 
18 #include "libabckit/include/c/abckit.h"
19 #include "helpers/helpers.h"
20 
21 namespace libabckit::test {
22 
23 static auto g_impl = AbckitGetApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
24 static auto g_implI = AbckitGetInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
25 static auto g_implM = AbckitGetModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
26 static auto g_implG = AbckitGetGraphApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
27 
28 class LibAbcKitAbcStuff : public ::testing::Test {};
29 
30 struct ClassData {
31     const char *className = nullptr;
32     std::vector<const char *> classMethods = {};
33 };
34 
35 struct ModuleData {
36     const char *moduleName = nullptr;
37     std::vector<const char *> moduleMethods = {};
38 };
39 
40 // Test: test-kind=api, api=ApiImpl::openAbc, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitAbcStuff,OpenAbcStatic)41 TEST_F(LibAbcKitAbcStuff, OpenAbcStatic)
42 {
43     constexpr auto INPUT_PATH = ABCKIT_ABC_DIR "internal/implementation_api/abc_static.abc";
44     auto *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
45     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
46 
47     std::vector<struct ClassData> userData = {{"ClassA", {"foo", "bar"}}, {"ClassB", {"baz", "func"}}};
48 
49     g_implI->fileEnumerateModules(file, &userData, [](AbckitCoreModule *m, void *data) {
50         auto *userData = reinterpret_cast<std::vector<struct ClassData> *>(data);
51 
52         for (const auto &classData : *userData) {
53             helpers::ClassByNameContext classCtxFinder = {nullptr, classData.className};
54             g_implI->moduleEnumerateClasses(m, &classCtxFinder, helpers::ClassByNameFinder);
55             EXPECT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
56             EXPECT_NE(classCtxFinder.klass, nullptr);
57 
58             for (auto *method : classData.classMethods) {
59                 helpers::MethodByNameContext methodCtxFinder = {nullptr, method};
60                 g_implI->classEnumerateMethods(classCtxFinder.klass, &methodCtxFinder, helpers::MethodByNameFinder);
61                 EXPECT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
62                 EXPECT_NE(methodCtxFinder.method, nullptr);
63             }
64         }
65 
66         return true;
67     });
68     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
69 
70     g_impl->closeFile(file);
71     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
72 }
73 
74 // Test: test-kind=api, api=ApiImpl::writeAbc, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitAbcStuff,WriteAbcStatic)75 TEST_F(LibAbcKitAbcStuff, WriteAbcStatic)
76 {
77     constexpr auto INPUT_PATH = ABCKIT_ABC_DIR "internal/implementation_api/abc_static.abc";
78     auto *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
79     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
80     g_impl->writeAbc(file, INPUT_PATH, strlen(INPUT_PATH));
81     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
82     g_impl->closeFile(file);
83     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
84     file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
85     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
86 
87     std::vector<struct ClassData> userData = {{"ClassA", {"foo", "bar"}}, {"ClassB", {"baz", "func"}}};
88 
89     g_implI->fileEnumerateModules(file, &userData, [](AbckitCoreModule *m, void *data) {
90         auto *userData = reinterpret_cast<std::vector<struct ClassData> *>(data);
91 
92         for (const auto &classData : *userData) {
93             helpers::ClassByNameContext classCtxFinder = {nullptr, classData.className};
94             g_implI->moduleEnumerateClasses(m, &classCtxFinder, helpers::ClassByNameFinder);
95             EXPECT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
96             EXPECT_NE(classCtxFinder.klass, nullptr);
97 
98             for (auto *method : classData.classMethods) {
99                 helpers::MethodByNameContext methodCtxFinder = {nullptr, method};
100                 g_implI->classEnumerateMethods(classCtxFinder.klass, &methodCtxFinder, helpers::MethodByNameFinder);
101                 EXPECT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
102                 EXPECT_NE(methodCtxFinder.method, nullptr);
103             }
104         }
105 
106         return true;
107     });
108     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
109 
110     g_impl->closeFile(file);
111 }
112 
113 // Test: test-kind=api, api=ApiImpl::closeFile, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitAbcStuff,CloseFileStatic)114 TEST_F(LibAbcKitAbcStuff, CloseFileStatic)
115 {
116     constexpr auto INPUT_PATH = ABCKIT_ABC_DIR "internal/implementation_api/abc_static.abc";
117     auto *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
118     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
119     g_impl->closeFile(file);
120     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
121 }
122 
123 // Test: test-kind=api, api=ApiImpl::openAbc, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitAbcStuff,OpenAbcDynamic)124 TEST_F(LibAbcKitAbcStuff, OpenAbcDynamic)
125 {
126     constexpr auto INPUT_PATH = ABCKIT_ABC_DIR "internal/implementation_api/abc_dynamic.abc";
127     auto *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
128     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
129 
130     struct ModuleData userData = {"abc_dynamic", {"foo", "bar", "baz", "func"}};
131 
132     helpers::ModuleByNameContext moduleCtxFinder = {nullptr, userData.moduleName};
133     g_implI->fileEnumerateModules(file, &moduleCtxFinder, helpers::ModuleByNameFinder);
134     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
135     ASSERT_NE(moduleCtxFinder.module, nullptr);
136 
137     for (auto method : userData.moduleMethods) {
138         auto *found = helpers::FindMethodByName(file, method);
139         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
140         ASSERT_NE(found, nullptr);
141     }
142 
143     g_impl->closeFile(file);
144     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
145 }
146 
147 // Test: test-kind=api, api=ApiImpl::writeAbc, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitAbcStuff,WriteAbcDynamic)148 TEST_F(LibAbcKitAbcStuff, WriteAbcDynamic)
149 {
150     constexpr auto INPUT_PATH = ABCKIT_ABC_DIR "internal/implementation_api/abc_dynamic.abc";
151     auto *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
152     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
153     g_impl->writeAbc(file, INPUT_PATH, strlen(INPUT_PATH));
154     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
155     g_impl->closeFile(file);
156     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
157     file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
158     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
159 
160     struct ModuleData userData = {"abc_dynamic", {"foo", "bar", "baz", "func"}};
161 
162     helpers::ModuleByNameContext moduleCtxFinder = {nullptr, userData.moduleName};
163     g_implI->fileEnumerateModules(file, &moduleCtxFinder, helpers::ModuleByNameFinder);
164     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
165     ASSERT_NE(moduleCtxFinder.module, nullptr);
166 
167     for (auto method : userData.moduleMethods) {
168         auto *found = helpers::FindMethodByName(file, method);
169         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
170         ASSERT_NE(found, nullptr);
171     }
172 
173     g_impl->closeFile(file);
174     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
175 }
176 
177 // Test: test-kind=api, api=ApiImpl::closeFile, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitAbcStuff,CloseFileDynamic)178 TEST_F(LibAbcKitAbcStuff, CloseFileDynamic)
179 {
180     constexpr auto INPUT_PATH = ABCKIT_ABC_DIR "internal/implementation_api/abc_dynamic.abc";
181     auto *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
182     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
183     g_impl->closeFile(file);
184     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
185 }
186 
187 }  // namespace libabckit::test
188