• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "file_ut.h"
17 
18 using namespace testing::ext;
19 using namespace testing;
20 
21 namespace Hdc {
22 
23 class HdcFileTest : public Test {
24 public:
25     static void SetUpTestCase(void);
26     static void TearDownTestCase(void);
27     void SetUp();
28     void TearDown();
29 
30     class MockHdcFile : public HdcFile {
31     public:
MockHdcFile(HTaskInfo hTaskInfo)32         explicit MockHdcFile(HTaskInfo hTaskInfo) : HdcFile(hTaskInfo) {}
33     };
34 
35 private:
BuildPTask()36     HTaskInfo BuildPTask()
37     {
38         HTaskInfo hTaskInfo = new(std::nothrow) TaskInformation();
39         if (hTaskInfo == nullptr) {
40             return nullptr;
41         }
42         uv_loop_t looptest;
43         uv_loop_init(&looptest);
44         LoopStatus ls(&looptest, "not support");
45         HdcSessionBase *unittest = new (std::nothrow) HdcSessionBase(false);
46         if (unittest == nullptr) {
47             delete hTaskInfo;
48             return nullptr;
49         }
50         hTaskInfo->ownerSessionClass = unittest;
51         hTaskInfo->channelId = 0;
52         hTaskInfo->sessionId = 0;
53         hTaskInfo->runLoop = &looptest;
54         hTaskInfo->runLoopStatus = &ls;
55         hTaskInfo->serverOrDaemon = false;
56         hTaskInfo->masterSlave = false;
57         hTaskInfo->closeRetryCount = 0;
58         hTaskInfo->channelTask = false;
59         hTaskInfo->isCleared = false;
60         hTaskInfo->taskType = TASK_FILE;
61         hTaskInfo->hasInitial = true;
62         HdcFile *ptrTask = nullptr;
63         ptrTask = new (std::nothrow) HdcFile(hTaskInfo);
64         if (ptrTask == nullptr) {
65             delete hTaskInfo;
66             delete unittest;
67             return nullptr;
68         }
69         hTaskInfo->taskClass = ptrTask;
70         return hTaskInfo;
71     }
72 
73     HTaskInfo hTaskInfo;
74     MockHdcFile *mockHdcdFile;
75 };
76 
SetUpTestCase()77 void HdcFileTest::SetUpTestCase()
78 {
79 #ifdef UT_DEBUG
80     Hdc::Base::SetLogLevel(LOG_ALL);
81 #else
82     Hdc::Base::SetLogLevel(LOG_OFF);
83 #endif
84 }
TearDownTestCase()85 void HdcFileTest::TearDownTestCase() {}
SetUp()86 void HdcFileTest::SetUp()
87 {
88     hTaskInfo = BuildPTask();
89     mockHdcdFile = new (std::nothrow) MockHdcFile(hTaskInfo);
90 }
TearDown()91 void HdcFileTest::TearDown()
92 {
93     delete mockHdcdFile;
94     mockHdcdFile = nullptr;
95 }
96 
97 HWTEST_F(HdcFileTest, TestStopTask, TestSize.Level0)
98 {
99     mockHdcdFile->StopTask();
100     ASSERT_EQ(mockHdcdFile->singalStop, true);
101 }
102 
103 HWTEST_F(HdcFileTest, TestBegintarnsfer, TestSize.Level0)
104 {
105     HdcFile::CtxFile context;
106     // case 1: argc < CMD_ARG1_COUNT
107     std::string command = "-cwd";
108     bool result = mockHdcdFile->BeginTransfer(&context, command);
109     EXPECT_FALSE(result);
110     // case 2: argv == nullptr
111     command = "";
112     result = mockHdcdFile->BeginTransfer(&context, command);
113     EXPECT_FALSE(result);
114 }
115 
116 HWTEST_F(HdcFileTest, TestCheckBlacklistPath, TestSize.Level0)
117 {
118     HdcFile::CtxFile context;
119     // case 1: path not in balcklist
120     context.localPath = "/data/service/el1/public/validpath";
121     ASSERT_EQ(mockHdcdFile->CheckBlacklistPath(&context), true);
122 
123     // case 2: path in balcklist
124     context.localPath = "/data/service/el1/public/hdc";
125     ASSERT_EQ(mockHdcdFile->CheckBlacklistPath(&context), false);
126 }
127 
128 HWTEST_F(HdcFileTest, TestParseMasterParameters, TestSize.Level0)
129 {
130     HdcFile::CtxFile context;
131     int argc = 0, srcArgvIndex = 0;
132     char arg0[] = "-b";
133     char arg1[] = "-z";
134     char arg2[] = "-sync";
135     char arg3[] = "-a";
136     char arg4[] = "-m";
137     char *argBase[] = {arg0, arg1, arg2, arg3, arg4};
138     char argFirst[] = "-cwd";
139     char argRemotePath[] = "D:\\";
140     char argBundleName[] = "com.example.myapplication";
141     char argLocalPath[] = "data/local/tmp/test.txt";
142     // case 1: -b Parameter ok
143     argc = 5;
144     char *argvB[] = {argFirst, argRemotePath, argBase[0], argBundleName, argLocalPath};
145     bool result = mockHdcdFile->ParseMasterParameters(&context, argc, argvB, srcArgvIndex);
146     EXPECT_TRUE(result);
147     EXPECT_EQ(context.bundleName, "com.example.myapplication");
148     EXPECT_EQ(srcArgvIndex, argc - 1);
149     // case 2: -b MissingParameter
150     argc = 4;
151     srcArgvIndex = 0;
152     char *argvBInvalid[] = {argFirst, argRemotePath, argBase[0], argLocalPath};
153     result = mockHdcdFile->ParseMasterParameters(&context, argc, argvBInvalid, srcArgvIndex);
154     EXPECT_FALSE(result);
155     EXPECT_EQ(srcArgvIndex, argc);
156     int i = 1;
157     int numArgs = sizeof(argBase) / sizeof(argBase[0]);
158     for (i = 1; i < numArgs; i++)
159     {
160         argc = 4;
161         srcArgvIndex = 0;
162         // case: argBase Parameter ok
163         char *argv[] = {argFirst, argRemotePath, argBase[i], argLocalPath};
164         result = mockHdcdFile->ParseMasterParameters(&context, argc, argv, srcArgvIndex);
165         EXPECT_TRUE(result);
166         EXPECT_EQ(srcArgvIndex, argc - 1);
167 
168         argc = 3;
169         srcArgvIndex = 0;
170         // case: argBase MissingParameter
171         char *argvInvalid[] = {argFirst, argRemotePath, argBase[i]};
172         result = mockHdcdFile->ParseMasterParameters(&context, argc, argvInvalid, srcArgvIndex);
173         EXPECT_FALSE(result);
174         EXPECT_EQ(srcArgvIndex, argc);
175     }
176 }
177 
178 HWTEST_F(HdcFileTest, TestCheckSandboxSubPath, TestSize.Level0)
179 {
180     HdcFile::CtxFile context;
181     // case 1: application path
182     string resolvedPath;
183     context.bundleName = "com.example.myapplication";
184     context.inputLocalPath = "data/storage/el2/base/";
185     ASSERT_EQ(mockHdcdFile->CheckSandboxSubPath(&context, resolvedPath), true);
186     std::stringstream ss;
187     ss << "/mnt/debug/100/debug_hap/com.example.myapplication/data/storage/el2/";
188     ASSERT_EQ(resolvedPath, ss.str());
189 }
190 
191 HWTEST_F(HdcFileTest, TestSetMasterParameters, TestSize.Level0)
192 {
193     HdcFile::CtxFile context;
194     char arg0[] = "-b";
195     char arg1[] = "-z";
196     char arg2[] = "-sync";
197     char arg3[] = "-a";
198     char arg4[] = "-m";
199     char *argBase[] = {arg0, arg1, arg2, arg3, arg4};
200     char argFirst[] = "-cwd";
201     char argRemotePath[] = "D:\\";
202     char argBundleName[] = "com.example.myapplication";
203     char argLocalPath[] = "data/local/tmp/test.txt";
204     char argInvalidBundlePath[] = "../../../../test.txt";
205     std::filesystem::path testPath = "/mnt/debug/100/debug_hap/com.example.myapplication/data/local/tmp/";
206     if (!std::filesystem::exists(testPath))
207     {
208         std::filesystem::create_directories(testPath);
209     }
210     ASSERT_TRUE(std::filesystem::exists(testPath));
211     // case 1: -b Parameter ok
212     int argc = 5;
213     context.transferConfig.clientCwd = argRemotePath;
214     char *argvB[] = {argFirst, argRemotePath, argBase[0], argBundleName, argLocalPath};
215     std::stringstream ss;
216     ss << "/mnt/debug/100/debug_hap/com.example.myapplication/data/local/tmp//test.txt";
217     EXPECT_EQ(mockHdcdFile->SetMasterParameters(&context, argc, argvB), true);
218     EXPECT_EQ(context.bundleName, "com.example.myapplication");
219     EXPECT_EQ(context.localPath, ss.str());
220     EXPECT_EQ(context.localName, "test.txt");
221     // case 2: -b serverOrDaemon == true
222     hTaskInfo->serverOrDaemon = true;
223     EXPECT_EQ(mockHdcdFile->SetMasterParameters(&context, argc, argvB), false);
224     // case 3: -b InvalidBundlePath
225     hTaskInfo->serverOrDaemon = false;
226     char *argvBInvalidBundlePath[] = {argFirst, argRemotePath, argBase[0], argBundleName, argInvalidBundlePath};
227     EXPECT_EQ(mockHdcdFile->SetMasterParameters(&context, argc, argvBInvalidBundlePath), false);
228     // case 4: -b MissingParameter
229     argc = 4;
230     char *argvBInvalid[] = {argFirst, argRemotePath, argBase[0], argLocalPath};
231     EXPECT_EQ(mockHdcdFile->SetMasterParameters(&context, argc, argvBInvalid), false);
232     context.bundleName = "";
233     context.transferConfig.reserve1 = "";
234 
235     int i = 1;
236     int numArgs = sizeof(argBase) / sizeof(argBase[0]);
237     for (i = 1; i < numArgs; i++)
238     {
239         argc = 4;
240         // case: argBase Parameter ok
241         char *argv[] = {argFirst, argRemotePath, argBase[i], argLocalPath};
242         EXPECT_EQ(mockHdcdFile->SetMasterParameters(&context, argc, argv), true);
243         argc = 3;
244         // case: argBase MissingParameter
245         char *argvInvalid[] = {argFirst, argRemotePath, argBase[i]};
246         EXPECT_EQ(mockHdcdFile->SetMasterParameters(&context, argc, argvInvalid), false);
247     }
248 }
249 }