1 /*
2 * Copyright (c) 2021 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 #include "init_cmds.h"
16 #include "init_group_manager.h"
17 #include "init_hashmap.h"
18 #include "init_param.h"
19 #include "init_plugin.h"
20 #include "init_plugin_manager.h"
21 #include "init_unittest.h"
22 #include "init_utils.h"
23 #include "securec.h"
24
25 using namespace testing::ext;
26 using namespace std;
27
28 namespace init_ut {
29 class PluginUnitTest : public testing::Test {
30 public:
SetUpTestCase(void)31 static void SetUpTestCase(void) {};
TearDownTestCase(void)32 static void TearDownTestCase(void) {};
SetUp(void)33 void SetUp(void) {};
TearDown(void)34 void TearDown(void) {};
35 };
36
37 int g_cmdExecId = 0;
TestCmdExecutor(int id,const char * name,int argc,const char ** argv)38 int TestCmdExecutor(int id, const char *name, int argc, const char **argv)
39 {
40 printf("TestCmdExecutor id %d, name %s \n", id, name);
41 g_cmdExecId = id;
42 return 0;
43 }
44
45 HWTEST_F(PluginUnitTest, PluginAddCmd, TestSize.Level1)
46 {
47 InitServiceSpace();
48 SetPluginInterface();
49 PluginManagerInit();
50 PluginInterface *pluginInterface = GetPluginInterface();
51 ASSERT_NE(pluginInterface, nullptr);
52 const char *testName = "testCmd1";
53 const char *cmdContent = "testCmd1 test1 test2 test3";
54 int cmdExecId1 = pluginInterface->addCmdExecutor(testName, TestCmdExecutor);
55 ASSERT_NE(cmdExecId1 > 0, 0);
56 int cmdExecId2 = pluginInterface->addCmdExecutor("testCmd2", TestCmdExecutor);
57 ASSERT_NE(cmdExecId2 > 0, 0);
58 cmdExecId2 = pluginInterface->addCmdExecutor("testCmd3", TestCmdExecutor);
59 ASSERT_NE(cmdExecId2 > 0, 0);
60 int cmdExecId4 = pluginInterface->addCmdExecutor("testCmd4", TestCmdExecutor);
61 ASSERT_NE(cmdExecId4 > 0, 0);
62
63 int cmdIndex = 0;
64 const char *cmdName = PluginGetCmdIndex(cmdContent, &cmdIndex);
65 ASSERT_EQ(strcmp(cmdName, testName), 0);
66 printf("TestCmdExecutor cmdIndex 0x%04x, name %s \n", cmdIndex, cmdName);
67
68 // exec
69 g_cmdExecId = -1;
70 PluginExecCmdByName(cmdName, cmdContent);
71 ASSERT_EQ(cmdExecId1, g_cmdExecId);
72 g_cmdExecId = -1;
73 PluginExecCmdByCmdIndex(cmdIndex, cmdContent);
74 ASSERT_EQ(cmdExecId1, g_cmdExecId);
75
76 // del
77 pluginInterface->removeCmdExecutor("testCmd4", cmdExecId4);
78 }
79
PluginTestInit(void)80 static int PluginTestInit(void)
81 {
82 PluginInterface *pluginInterface = GetPluginInterface();
83 if (pluginInterface != nullptr && pluginInterface->addCmdExecutor != nullptr) {
84 g_cmdExecId = pluginInterface->addCmdExecutor("testCmd4", TestCmdExecutor);
85 }
86 return 0;
87 }
88
PluginTestExit(void)89 static void PluginTestExit(void)
90 {
91 PluginInterface *pluginInterface = GetPluginInterface();
92 ASSERT_NE(pluginInterface, nullptr);
93 pluginInterface->removeCmdExecutor("testCmd4", g_cmdExecId);
94 }
95
96 HWTEST_F(PluginUnitTest, PluginInstallTest, TestSize.Level1)
97 {
98 PluginInterface *pluginInterface = GetPluginInterface();
99 ASSERT_NE(pluginInterface, nullptr);
100 const char *moduleName = "testplugin";
101 pluginInterface->pluginRegister(moduleName,
102 "/home/axw/init_ut/etc/init/plugin_param_test.cfg",
103 PluginTestInit, PluginTestExit);
104 PluginInstall(moduleName, NULL);
105 PluginUninstall(moduleName);
106 }
107 } // namespace init_ut
108