• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 <dlfcn.h>
17 #include "loader.h"
18 #include "config.h"
19 #include <string>
20 #include <gtest/gtest.h>
21 
22 using namespace testing;
23 using namespace testing::ext;
24 using namespace OHOS;
25 using namespace OHOS::MiscServices;
26 namespace {
27     const std::string LIB_STRING = "random_lib";
28     const std::string CONSTRUCTOR_STRING = "random_constructor";
29 }
30 
31 namespace OHOS {
32 namespace MiscServices {
Marshal(Serializable::json & node) const33 bool Config::Component::Marshal(Serializable::json &node) const
34 {
35     return true;
36 }
Unmarshal(const Serializable::json & node)37 bool Config::Component::Unmarshal(const Serializable::json &node)
38 {
39     return true;
40 }
41 
Marshal(Serializable::json & node) const42 bool Config::Marshal(Serializable::json &node) const
43 {
44     return true;
45 }
46 
47 static bool g_unmarshal = false;
48 static std::string g_description;
49 static std::string g_lib;
50 static std::string g_constructor;
51 static std::string g_destructor;
52 static std::string g_params;
Unmarshal(const DistributedData::Serializable::json & node)53 bool Config::Unmarshal(const DistributedData::Serializable::json &node)
54 {
55     if (g_unmarshal) {
56         Component component;
57         component.description = g_description;
58         component.lib = g_lib;
59         component.constructor = g_constructor;
60         component.destructor = g_destructor;
61         component.params = g_params;
62         this->components.push_back(component);
63     } else {
64         auto ret = GetValue(node, GET_NAME(processLabel), processLabel);
65         ret = GetValue(node, GET_NAME(version), version) && ret;
66         GetValue(node, GET_NAME(features), features);
67         GetValue(node, GET_NAME(plugins), plugins);
68         GetValue(node, GET_NAME(components), components);
69         GetValue(node, GET_NAME(uid), uid);
70     }
71     return true;
72 }
73 }
74 
75 class PasteboardLoadTest : public testing::Test {
76 public:
77     static void SetUpTestCase(void);
78     static void TearDownTestCase(void);
79     void SetUp();
80     void TearDown();
81 };
82 
SetUpTestCase(void)83 void PasteboardLoadTest::SetUpTestCase(void) { }
84 
TearDownTestCase(void)85 void PasteboardLoadTest::TearDownTestCase(void) { }
86 
SetUp(void)87 void PasteboardLoadTest::SetUp(void) { }
88 
TearDown(void)89 void PasteboardLoadTest::TearDown(void) { }
90 
91 namespace MiscServices {
92 /**
93  * @tc.name: LoadComponentsTest001
94  * @tc.desc: LoadComponents when component.lib == ""
95  * @tc.type: FUNC
96  */
97 HWTEST_F(PasteboardLoadTest, LoadComponentsTest001, TestSize.Level0)
98 {
99     Loader loader;
100     g_lib.clear();
101     g_unmarshal = true;
102     EXPECT_NO_FATAL_FAILURE(loader.LoadComponents());
103 }
104 /**
105  * @tc.name: LoadComponentsTest002
106  * @tc.desc: LoadComponents when Loader::ComponentIsExist(component.lib) return true
107  * @tc.type: FUNC
108  */
109 HWTEST_F(PasteboardLoadTest, LoadComponentsTest002, TestSize.Level0)
110 {
111     Loader loader;
112     std::string lib = LIB_STRING;
113     loader.handleMap[LIB_STRING] = &lib;
114     g_lib = lib;
115     g_unmarshal = true;
116     EXPECT_NO_FATAL_FAILURE(loader.LoadComponents());
117 }
118 /**
119  * @tc.name: LoadComponentsTest003
120  * @tc.desc: LoadComponents when dlopen return nullptr
121  * @tc.type: FUNC
122  */
123 HWTEST_F(PasteboardLoadTest, LoadComponentsTest003, TestSize.Level0)
124 {
125     Loader loader;
126     std::string lib = LIB_STRING;
127     loader.handleMap.clear();
128     g_lib = lib;
129     g_unmarshal = false;
130     EXPECT_NO_FATAL_FAILURE(loader.LoadComponents());
131 }
132 /**
133  * @tc.name: LoadComponentsTest004
134  * @tc.desc: LoadComponents when component.constructor.empty() return true
135  * @tc.type: FUNC
136  */
137 HWTEST_F(PasteboardLoadTest, LoadComponentsTest004, TestSize.Level0)
138 {
139     Loader loader;
140     std::string lib = LIB_STRING;
141     loader.handleMap.clear();
142     g_lib = lib;
143     g_constructor.clear();
144     g_unmarshal = false;
145     EXPECT_NO_FATAL_FAILURE(loader.LoadComponents());
146 }
147 /**
148  * @tc.name: LoadComponentsTest005
149  * @tc.desc: LoadComponents dlsym return false
150  * @tc.type: FUNC
151  */
152 HWTEST_F(PasteboardLoadTest, LoadComponentsTest005, TestSize.Level0)
153 {
154     Loader loader;
155     std::string lib = LIB_STRING;
156     loader.handleMap.clear();
157     g_lib = lib;
158     g_constructor = CONSTRUCTOR_STRING;
159     g_unmarshal = false;
160     EXPECT_NO_FATAL_FAILURE(loader.LoadComponents());
161 }
162 
163 /**
164  * @tc.name: LoadUidTest
165  * @tc.desc: LoadUid
166  * @tc.type: FUNC
167  */
168 HWTEST_F(PasteboardLoadTest, LoadUidTest, TestSize.Level0)
169 {
170     Loader loader;
171     EXPECT_NO_FATAL_FAILURE(loader.LoadUid());
172 }
173 
174 /**
175  * @tc.name: LoadConfigTest
176  * @tc.desc: LoadConfig
177  * @tc.type: FUNC
178  */
179 HWTEST_F(PasteboardLoadTest, LoadConfigTest, TestSize.Level0)
180 {
181     Loader loader;
182     EXPECT_NO_FATAL_FAILURE(loader.LoadConfig());
183 }
184 
185 /**
186  * @tc.name: ComponentIsExistTest
187  * @tc.desc: ComponentIsExist
188  * @tc.type: FUNC
189  */
190 HWTEST_F(PasteboardLoadTest, ComponentIsExistTest, TestSize.Level0)
191 {
192     Loader loader;
193     EXPECT_NO_FATAL_FAILURE(loader.ComponentIsExist(LIB_STRING));
194 }
195 }
196 } // namespace OHOS::MiscServices