xref: /developtools/hdc/src/test/ut_command.cpp (revision cc290419)
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 "ut_command.h"
16using namespace Hdc;
17
18namespace HdcTest {
19void *TestBackgroundServerForClient(void *param)
20{
21    HdcServer server(true);
22    server.Initial("0.0.0.0:8710");
23    server.WorkerPendding();
24    WRITE_LOG(LOG_DEBUG, "Test ServerForClient free");
25    return nullptr;
26}
27
28void TestRunClient(const string &debugServerPort, const string &debugConnectKey, const string &cmd)
29{
30    uv_loop_t loopMain;
31    uv_loop_init(&loopMain);
32    HdcClient client(false, debugServerPort, &loopMain);
33    client.Initial(debugConnectKey);
34    client.ExecuteCommand(cmd);
35    uv_loop_close(&loopMain);
36}
37
38void PreConnectDaemon(const string &debugServerPort, const string &debugConnectKey)
39{
40    string bufString = "tconn ";
41    bufString += debugConnectKey;
42    WRITE_LOG(LOG_DEBUG, "------------Connect command------------");
43    TestRunClient(debugServerPort, "", bufString.c_str());
44}
45
46int TestRuntimeCommandSimple(bool bTCPorUSB, int method, bool bNeedConnectDaemon)
47{
48    // These two parameters are tested, not much change, manually modify by myself
49    string debugServerPort;
50    string debugConnectKey;
51    debugServerPort = DEBUG_ADDRESS;
52    if (bTCPorUSB) {
53        debugConnectKey = DEBUG_TCP_CONNECT_KEY;
54    } else {
55        debugConnectKey = DEBUG_USB_CONNECT_KEY;
56    }
57    if (bNeedConnectDaemon) {  // just tcp
58        PreConnectDaemon(debugServerPort, debugConnectKey);
59    }
60    WRITE_LOG(LOG_DEBUG, "Test Jump TestRuntimeCommand");
61    TestRuntimeCommand(method, debugServerPort, debugConnectKey);
62    return 0;
63}
64
65int TestTaskCommand(int method, const string &debugServerPort, const string &debugConnectKey)
66{
67    WRITE_LOG(LOG_DEBUG, "------------Operate command------------");
68    string bufString;
69    switch (method) {
70        case UT_SHELL_BASIC:  // Basic order test
71            TestRunClient(debugServerPort, debugConnectKey, "shell id");
72            break;
73        case UT_SHELL_LIGHT:  // Small pressure test
74            TestRunClient(debugServerPort, debugConnectKey, "shell cat /etc/passwd");
75            break;
76        case UT_SHELL_HEAVY:  // High pressure test (Long Time)
77            TestRunClient(debugServerPort, debugConnectKey, "shell cat /data/local/tmp/root.txt");
78            break;
79        case UT_SHELL_INTERACTIVE:  // Interactive shell test
80            TestRunClient(debugServerPort, debugConnectKey, CMDSTR_SHELL.c_str());
81            break;
82        case UT_FILE_SEND: {  // send files
83            bufString = Base::StringFormat("file send %s/file.local %s/file.remote", UT_TMP_PATH.c_str(),
84                                           UT_TMP_PATH.c_str());
85            TestRunClient(debugServerPort, debugConnectKey, bufString);
86            break;
87        }
88        case UT_FILE_RECV:  // recv files
89            TestRunClient(debugServerPort, debugConnectKey,
90                          "file recv /mnt/hgfs/vtmp/f.txt /mnt/hgfs/vtmp/f2.txt -z 1");
91            break;
92        case UT_FORWARD_TCP2TCP:  // TCP forward
93            TestRunClient(debugServerPort, debugConnectKey, "fport tcp:8081 tcp:8082");
94            break;
95        case UT_FORWARD_TCP2FILE:  // localfilesystem forward
96            TestRunClient(debugServerPort, debugConnectKey, "fport tcp:8081 localfilesystem:mysocket");
97            break;
98        case UT_FORWARD_TCP2DEV:
99            TestRunClient(debugServerPort, debugConnectKey, "fport tcp:8081 dev:/dev/urandom");
100            break;
101        case UT_FORWARD_TCP2JDWP:  // jdwp forward
102            TestRunClient(debugServerPort, debugConnectKey, "fport tcp:8081 jdwp:1234");
103            break;
104        case UT_APP_INSTALL:  // Single and multiple and multiple paths support
105            bufString = Base::StringFormat("install %s/app.hap", UT_TMP_PATH.c_str());
106            TestRunClient(debugServerPort, debugConnectKey, bufString);
107            break;
108        case UT_TEST_TMP:
109#ifdef DEF_NULL
110            while (true) {
111                uv_sleep(GLOBAL_TIMEOUT);
112                TestRunClient(debugServerPort, debugConnectKey, "list targets");
113                TestRunClient(debugServerPort, debugConnectKey, "shell id");
114                TestRunClient(debugServerPort, debugConnectKey, "shell bm dump -a");
115            }
116            TestRunClient(debugServerPort, debugConnectKey, "install /d/helloworld.hap");
117            TestRunClient(debugServerPort, debugConnectKey, "target mount");
118            TestRunClient(debugServerPort, debugConnectKey, "shell pwd");
119            TestRunClient(debugServerPort, debugConnectKey, "target mount");
120            TestRunClient(debugServerPort, debugConnectKey, "shell pwd");
121            TestRunClient(debugServerPort, debugConnectKey, "install /d -rt");
122            TestRunClient(debugServerPort, debugConnectKey, "fport tcp:8081 tcp:8082");
123            TestRunClient(debugServerPort, debugConnectKey, "fport tcp:8081 dev:/dev/urandom");
124            TestRunClient(debugServerPort, debugConnectKey, "shell hilog");
125            TestRunClient(debugServerPort, debugConnectKey, "file send /mnt/hgfs/vtmp/f.txt /tmp/f2.txt");
126            TestRunClient(debugServerPort, debugConnectKey, "file recv /tmp/f2.txt /mnt/hgfs/vtmp/f2.txt");
127            TestRunClient(debugServerPort, debugConnectKey, "shell find /proc");
128            TestRunClient(debugServerPort, debugConnectKey, "file send \"/d/a b/1.txt\" \"/d/a b/2.txt\"");
129            TestRunClient(debugServerPort, debugConnectKey, "file recv \"/d/a b/1.txt\" \"/d/a b/2.txt\"");
130#endif
131            break;
132        default:
133            break;
134    }
135    WRITE_LOG(LOG_DEBUG, "!!!Client finish");
136    return 0;
137}
138
139int TestRuntimeCommand(const int method, const string &debugServerPort, const string &debugConnectKey)
140{
141    switch (method) {
142        case UT_HELP:
143            TestRunClient(debugServerPort, "", CMDSTR_SOFTWARE_HELP.c_str());
144            TestRunClient(debugServerPort, "", CMDSTR_SOFTWARE_VERSION.c_str());
145            break;
146        case UT_DISCOVER:
147            TestRunClient(debugServerPort, "", CMDSTR_TARGET_DISCOVER.c_str());
148            break;
149        case UT_LIST_TARGETS:
150            TestRunClient(debugServerPort, "", CMDSTR_LIST_TARGETS.c_str());
151            break;
152        case UT_CONNECT_ANY:
153            TestRunClient(debugServerPort, "", CMDSTR_CONNECT_ANY.c_str());
154            break;
155        case UT_KILL_SERVER:
156            TestRunClient(debugServerPort, "", CMDSTR_SERVICE_KILL.c_str());
157            break;
158        case UT_KILL_DAEMON:
159            TestRunClient(debugServerPort, debugConnectKey, "kill daemon");
160            break;
161        default:
162            TestTaskCommand(method, debugServerPort, debugConnectKey);
163            break;
164    }
165    return 0;
166}
167}  // namespace HdcTest
168