• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "init_context.h"
16 
17 #include "param_stub.h"
18 #include "securec.h"
19 
20 using namespace std;
21 using namespace testing::ext;
22 
23 extern "C" {
24 struct ForkArgs {
25     int (*childFunc)(const SubInitForkArg *arg);
26     SubInitForkArg args;
27 };
28 static pid_t g_pid = 1000;
29 static pthread_t g_thread = 0;
30 
ThreadFunc(void * arg)31 static void *ThreadFunc(void *arg)
32 {
33     printf("Create thread %d \n", gettid());
34     struct ForkArgs *forkArg = static_cast<struct ForkArgs *>(arg);
35     forkArg->childFunc(&forkArg->args);
36     printf("Exit thread %d %d \n", forkArg->args.type, gettid());
37     free(forkArg);
38     g_thread = 0;
39     return nullptr;
40 }
41 
SubInitFork(int (* childFunc)(const SubInitForkArg * arg),const SubInitForkArg * args)42 pid_t SubInitFork(int (*childFunc)(const SubInitForkArg *arg), const SubInitForkArg *args)
43 {
44     if (g_pid >= 0) {
45         struct ForkArgs *forkArg = static_cast<struct ForkArgs *>(malloc(sizeof(struct ForkArgs)));
46         if (forkArg == nullptr) {
47             return -1;
48         }
49         forkArg->childFunc = childFunc;
50         forkArg->args.socket[0] = args->socket[0];
51         forkArg->args.socket[1] = args->socket[1];
52         forkArg->args.type = args->type;
53         int ret = pthread_create(&g_thread, nullptr, ThreadFunc, forkArg);
54         if (ret != 0) {
55             printf("Failed to create thread %d \n", errno);
56             return -1;
57         }
58         usleep(100); // 100 wait
59     }
60     g_pid++;
61     return g_pid;
62 }
63 }
64 
65 namespace init_ut {
66 class InitContextUnitTest : public testing::Test {
67 public:
SetUpTestCase(void)68     static void SetUpTestCase(void) {};
TearDownTestCase(void)69     static void TearDownTestCase(void) {};
SetUp()70     void SetUp() {};
TearDown()71     void TearDown() {};
72 };
73 
74 HWTEST_F(InitContextUnitTest, InitSubContextTest_01, TestSize.Level1)
75 {
76     g_pid = -3; // -3  test data
77     int ret = StartSubInit(INIT_CONTEXT_CHIPSET);
78     EXPECT_NE(ret, 0);
79     ret = StartSubInit(INIT_CONTEXT_MAIN);
80     EXPECT_NE(ret, 0);
81     g_pid = 100; // 100 test data
82 }
83 
84 HWTEST_F(InitContextUnitTest, InitSubContextTest_02, TestSize.Level1)
85 {
86     ConfigContext context = { INIT_CONTEXT_CHIPSET };
87     int ret = ExecuteCmdInSubInit(&context, "mkdir", STARTUP_INIT_UT_PATH"/testsubcontext");
88     EXPECT_EQ(ret, 0);
89     ret = ExecuteCmdInSubInit(&context, "mkdir", STARTUP_INIT_UT_PATH"/testsubcontext1");
90     EXPECT_EQ(ret, 0);
91     ret = ExecuteCmdInSubInit(&context, "mkdir", STARTUP_INIT_UT_PATH"/testsubcontext2");
92     EXPECT_EQ(ret, 0);
93     ret = ExecuteCmdInSubInit(&context, "mkdir", nullptr);
94     EXPECT_EQ(ret, 0);
95     context.type = INIT_CONTEXT_MAIN;
96     ret = ExecuteCmdInSubInit(&context, "mkdir", STARTUP_INIT_UT_PATH"/testsubcontext");
97     EXPECT_NE(ret, 0);
98 
99     // fail
100     ret = ExecuteCmdInSubInit(nullptr, "mkdir", STARTUP_INIT_UT_PATH"/testsubcontext");
101     EXPECT_EQ(ret, -1);
102     ret = ExecuteCmdInSubInit(&context, nullptr, STARTUP_INIT_UT_PATH"/testsubcontext");
103     EXPECT_EQ(ret, -1);
104 }
105 
106 HWTEST_F(InitContextUnitTest, InitSubContextTest_03, TestSize.Level1)
107 {
108     int ret = StartSubInit(INIT_CONTEXT_CHIPSET);
109     EXPECT_EQ(ret, 0);
110     if (g_thread != 0) {
111         pthread_join(g_thread, nullptr);
112         g_thread = 0;
113     }
114     SubInitInfo *subInfo = GetSubInitInfo(INIT_CONTEXT_CHIPSET);
115     if (subInfo == nullptr) {
116         EXPECT_EQ(1, 0);
117     } else {
118         EXPECT_EQ(2, subInfo->state);
119         StopSubInit(subInfo->subPid);
120     }
121     subInfo = GetSubInitInfo(INIT_CONTEXT_MAIN);
122     if (subInfo != nullptr) {
123         EXPECT_EQ(1, 0);
124     }
125 }
126 
127 HWTEST_F(InitContextUnitTest, InitSubContextTest_04, TestSize.Level1)
128 {
129     int ret = StartSubInit(INIT_CONTEXT_CHIPSET);
130     EXPECT_EQ(ret, 0);
131     if (g_thread != 0) {
132         pthread_join(g_thread, nullptr);
133         g_thread = 0;
134     }
135     SubInitInfo *subInfo = GetSubInitInfo(INIT_CONTEXT_CHIPSET);
136     if (subInfo != nullptr) {
137         EXPECT_EQ(2, subInfo->state);
138         StopSubInit(subInfo->subPid);
139     } else {
140         EXPECT_EQ(1, 0);
141     }
142     // close
143     subInfo = GetSubInitInfo(INIT_CONTEXT_CHIPSET);
144     if (subInfo != nullptr) {
145         EXPECT_EQ(0, subInfo->state);
146     }
147 
148     SubInitContext *subContext = GetSubInitContext(INIT_CONTEXT_CHIPSET);
149     if (subContext == nullptr) {
150         EXPECT_EQ(0, -1);
151         return;
152     }
153     ret = subContext->executeCmdInSubInit(INIT_CONTEXT_CHIPSET, "mkdir-2", STARTUP_INIT_UT_PATH"/testsubcontext");
154     EXPECT_NE(ret, 0);
155 }
156 
157 HWTEST_F(InitContextUnitTest, InitSubContextTest_05, TestSize.Level1)
158 {
159     ConfigContext context = { INIT_CONTEXT_CHIPSET };
160     int ret = ExecuteCmdInSubInit(&context, "mkdir-2", STARTUP_INIT_UT_PATH"/testsubcontext");
161     EXPECT_EQ(ret, 0);
162 }
163 
164 HWTEST_F(InitContextUnitTest, InitSubContextTest_06, TestSize.Level1)
165 {
166     ConfigContext context = { INIT_CONTEXT_CHIPSET };
167     int index = 0;
168     const char *cmd = GetMatchCmd("mkdir ", &index);
169     if (cmd == nullptr || strstr(cmd, "mkdir ") == nullptr) {
170         EXPECT_EQ(1, 0);
171         return;
172     }
173     DoCmdByIndex(index, STARTUP_INIT_UT_PATH"/testsubcontext", &context);
174 }
175 
176 HWTEST_F(InitContextUnitTest, InitSubContextTest_07, TestSize.Level1)
177 {
178     ConfigContext context = { INIT_CONTEXT_MAIN };
179     int index = 0;
180     const char *cmd = GetMatchCmd("mkdir ", &index);
181     if (cmd == nullptr || strstr(cmd, "mkdir ") == nullptr) {
182         EXPECT_EQ(1, 0);
183         return;
184     }
185     DoCmdByIndex(index, STARTUP_INIT_UT_PATH"/testsubcontext", &context);
186 }
187 
188 HWTEST_F(InitContextUnitTest, InitSubContextTest_09, TestSize.Level1)
189 {
190     int ret = SetSubInitContext(nullptr, "serviceName");
191     EXPECT_EQ(ret, -1);
192     ConfigContext context = { INIT_CONTEXT_MAIN };
193     ret = SetSubInitContext(&context, "serviceName");
194     EXPECT_EQ(ret, 0);
195     context = { INIT_CONTEXT_CHIPSET };
196     ret = SetSubInitContext(&context, "serviceName");
197     EXPECT_EQ(ret, 0);
198 }
199 
200 HWTEST_F(InitContextUnitTest, InitSubContextTest_10, TestSize.Level1)
201 {
202     int ret = InitSubInitContext(INIT_CONTEXT_MAIN, nullptr);
203     EXPECT_EQ(ret, -1);
204     ret = InitSubInitContext(INIT_CONTEXT_CHIPSET, nullptr);
205     EXPECT_EQ(ret, -1);
206 
207     SubInitContext *subContext = GetSubInitContext(INIT_CONTEXT_CHIPSET);
208     if (subContext == nullptr) {
209         EXPECT_EQ(0, -1);
210         return;
211     }
212     ret = subContext->startSubInit(INIT_CONTEXT_MAIN);
213     EXPECT_NE(ret, 0);
214     ret = subContext->executeCmdInSubInit(INIT_CONTEXT_CHIPSET, nullptr, nullptr);
215     EXPECT_NE(ret, 0);
216     ret = subContext->executeCmdInSubInit(INIT_CONTEXT_MAIN, nullptr, nullptr);
217     EXPECT_NE(ret, 0);
218 }
219 } // namespace init_ut
220