1cc290419Sopenharmony_ci/*
2cc290419Sopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd.
3cc290419Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4cc290419Sopenharmony_ci * you may not use this file except in compliance with the License.
5cc290419Sopenharmony_ci * You may obtain a copy of the License at
6cc290419Sopenharmony_ci *
7cc290419Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8cc290419Sopenharmony_ci *
9cc290419Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10cc290419Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11cc290419Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12cc290419Sopenharmony_ci * See the License for the specific language governing permissions and
13cc290419Sopenharmony_ci * limitations under the License.
14cc290419Sopenharmony_ci */
15cc290419Sopenharmony_ci#include "ut_command.h"
16cc290419Sopenharmony_ciusing namespace Hdc;
17cc290419Sopenharmony_ci
18cc290419Sopenharmony_cinamespace HdcTest {
19cc290419Sopenharmony_civoid *TestBackgroundServerForClient(void *param)
20cc290419Sopenharmony_ci{
21cc290419Sopenharmony_ci    HdcServer server(true);
22cc290419Sopenharmony_ci    server.Initial("0.0.0.0:8710");
23cc290419Sopenharmony_ci    server.WorkerPendding();
24cc290419Sopenharmony_ci    WRITE_LOG(LOG_DEBUG, "Test ServerForClient free");
25cc290419Sopenharmony_ci    return nullptr;
26cc290419Sopenharmony_ci}
27cc290419Sopenharmony_ci
28cc290419Sopenharmony_civoid TestRunClient(const string &debugServerPort, const string &debugConnectKey, const string &cmd)
29cc290419Sopenharmony_ci{
30cc290419Sopenharmony_ci    uv_loop_t loopMain;
31cc290419Sopenharmony_ci    uv_loop_init(&loopMain);
32cc290419Sopenharmony_ci    HdcClient client(false, debugServerPort, &loopMain);
33cc290419Sopenharmony_ci    client.Initial(debugConnectKey);
34cc290419Sopenharmony_ci    client.ExecuteCommand(cmd);
35cc290419Sopenharmony_ci    uv_loop_close(&loopMain);
36cc290419Sopenharmony_ci}
37cc290419Sopenharmony_ci
38cc290419Sopenharmony_civoid PreConnectDaemon(const string &debugServerPort, const string &debugConnectKey)
39cc290419Sopenharmony_ci{
40cc290419Sopenharmony_ci    string bufString = "tconn ";
41cc290419Sopenharmony_ci    bufString += debugConnectKey;
42cc290419Sopenharmony_ci    WRITE_LOG(LOG_DEBUG, "------------Connect command------------");
43cc290419Sopenharmony_ci    TestRunClient(debugServerPort, "", bufString.c_str());
44cc290419Sopenharmony_ci}
45cc290419Sopenharmony_ci
46cc290419Sopenharmony_ciint TestRuntimeCommandSimple(bool bTCPorUSB, int method, bool bNeedConnectDaemon)
47cc290419Sopenharmony_ci{
48cc290419Sopenharmony_ci    // These two parameters are tested, not much change, manually modify by myself
49cc290419Sopenharmony_ci    string debugServerPort;
50cc290419Sopenharmony_ci    string debugConnectKey;
51cc290419Sopenharmony_ci    debugServerPort = DEBUG_ADDRESS;
52cc290419Sopenharmony_ci    if (bTCPorUSB) {
53cc290419Sopenharmony_ci        debugConnectKey = DEBUG_TCP_CONNECT_KEY;
54cc290419Sopenharmony_ci    } else {
55cc290419Sopenharmony_ci        debugConnectKey = DEBUG_USB_CONNECT_KEY;
56cc290419Sopenharmony_ci    }
57cc290419Sopenharmony_ci    if (bNeedConnectDaemon) {  // just tcp
58cc290419Sopenharmony_ci        PreConnectDaemon(debugServerPort, debugConnectKey);
59cc290419Sopenharmony_ci    }
60cc290419Sopenharmony_ci    WRITE_LOG(LOG_DEBUG, "Test Jump TestRuntimeCommand");
61cc290419Sopenharmony_ci    TestRuntimeCommand(method, debugServerPort, debugConnectKey);
62cc290419Sopenharmony_ci    return 0;
63cc290419Sopenharmony_ci}
64cc290419Sopenharmony_ci
65cc290419Sopenharmony_ciint TestTaskCommand(int method, const string &debugServerPort, const string &debugConnectKey)
66cc290419Sopenharmony_ci{
67cc290419Sopenharmony_ci    WRITE_LOG(LOG_DEBUG, "------------Operate command------------");
68cc290419Sopenharmony_ci    string bufString;
69cc290419Sopenharmony_ci    switch (method) {
70cc290419Sopenharmony_ci        case UT_SHELL_BASIC:  // Basic order test
71cc290419Sopenharmony_ci            TestRunClient(debugServerPort, debugConnectKey, "shell id");
72cc290419Sopenharmony_ci            break;
73cc290419Sopenharmony_ci        case UT_SHELL_LIGHT:  // Small pressure test
74cc290419Sopenharmony_ci            TestRunClient(debugServerPort, debugConnectKey, "shell cat /etc/passwd");
75cc290419Sopenharmony_ci            break;
76cc290419Sopenharmony_ci        case UT_SHELL_HEAVY:  // High pressure test (Long Time)
77cc290419Sopenharmony_ci            TestRunClient(debugServerPort, debugConnectKey, "shell cat /data/local/tmp/root.txt");
78cc290419Sopenharmony_ci            break;
79cc290419Sopenharmony_ci        case UT_SHELL_INTERACTIVE:  // Interactive shell test
80cc290419Sopenharmony_ci            TestRunClient(debugServerPort, debugConnectKey, CMDSTR_SHELL.c_str());
81cc290419Sopenharmony_ci            break;
82cc290419Sopenharmony_ci        case UT_FILE_SEND: {  // send files
83cc290419Sopenharmony_ci            bufString = Base::StringFormat("file send %s/file.local %s/file.remote", UT_TMP_PATH.c_str(),
84cc290419Sopenharmony_ci                                           UT_TMP_PATH.c_str());
85cc290419Sopenharmony_ci            TestRunClient(debugServerPort, debugConnectKey, bufString);
86cc290419Sopenharmony_ci            break;
87cc290419Sopenharmony_ci        }
88cc290419Sopenharmony_ci        case UT_FILE_RECV:  // recv files
89cc290419Sopenharmony_ci            TestRunClient(debugServerPort, debugConnectKey,
90cc290419Sopenharmony_ci                          "file recv /mnt/hgfs/vtmp/f.txt /mnt/hgfs/vtmp/f2.txt -z 1");
91cc290419Sopenharmony_ci            break;
92cc290419Sopenharmony_ci        case UT_FORWARD_TCP2TCP:  // TCP forward
93cc290419Sopenharmony_ci            TestRunClient(debugServerPort, debugConnectKey, "fport tcp:8081 tcp:8082");
94cc290419Sopenharmony_ci            break;
95cc290419Sopenharmony_ci        case UT_FORWARD_TCP2FILE:  // localfilesystem forward
96cc290419Sopenharmony_ci            TestRunClient(debugServerPort, debugConnectKey, "fport tcp:8081 localfilesystem:mysocket");
97cc290419Sopenharmony_ci            break;
98cc290419Sopenharmony_ci        case UT_FORWARD_TCP2DEV:
99cc290419Sopenharmony_ci            TestRunClient(debugServerPort, debugConnectKey, "fport tcp:8081 dev:/dev/urandom");
100cc290419Sopenharmony_ci            break;
101cc290419Sopenharmony_ci        case UT_FORWARD_TCP2JDWP:  // jdwp forward
102cc290419Sopenharmony_ci            TestRunClient(debugServerPort, debugConnectKey, "fport tcp:8081 jdwp:1234");
103cc290419Sopenharmony_ci            break;
104cc290419Sopenharmony_ci        case UT_APP_INSTALL:  // Single and multiple and multiple paths support
105cc290419Sopenharmony_ci            bufString = Base::StringFormat("install %s/app.hap", UT_TMP_PATH.c_str());
106cc290419Sopenharmony_ci            TestRunClient(debugServerPort, debugConnectKey, bufString);
107cc290419Sopenharmony_ci            break;
108cc290419Sopenharmony_ci        case UT_TEST_TMP:
109cc290419Sopenharmony_ci#ifdef DEF_NULL
110cc290419Sopenharmony_ci            while (true) {
111cc290419Sopenharmony_ci                uv_sleep(GLOBAL_TIMEOUT);
112cc290419Sopenharmony_ci                TestRunClient(debugServerPort, debugConnectKey, "list targets");
113cc290419Sopenharmony_ci                TestRunClient(debugServerPort, debugConnectKey, "shell id");
114cc290419Sopenharmony_ci                TestRunClient(debugServerPort, debugConnectKey, "shell bm dump -a");
115cc290419Sopenharmony_ci            }
116cc290419Sopenharmony_ci            TestRunClient(debugServerPort, debugConnectKey, "install /d/helloworld.hap");
117cc290419Sopenharmony_ci            TestRunClient(debugServerPort, debugConnectKey, "target mount");
118cc290419Sopenharmony_ci            TestRunClient(debugServerPort, debugConnectKey, "shell pwd");
119cc290419Sopenharmony_ci            TestRunClient(debugServerPort, debugConnectKey, "target mount");
120cc290419Sopenharmony_ci            TestRunClient(debugServerPort, debugConnectKey, "shell pwd");
121cc290419Sopenharmony_ci            TestRunClient(debugServerPort, debugConnectKey, "install /d -rt");
122cc290419Sopenharmony_ci            TestRunClient(debugServerPort, debugConnectKey, "fport tcp:8081 tcp:8082");
123cc290419Sopenharmony_ci            TestRunClient(debugServerPort, debugConnectKey, "fport tcp:8081 dev:/dev/urandom");
124cc290419Sopenharmony_ci            TestRunClient(debugServerPort, debugConnectKey, "shell hilog");
125cc290419Sopenharmony_ci            TestRunClient(debugServerPort, debugConnectKey, "file send /mnt/hgfs/vtmp/f.txt /tmp/f2.txt");
126cc290419Sopenharmony_ci            TestRunClient(debugServerPort, debugConnectKey, "file recv /tmp/f2.txt /mnt/hgfs/vtmp/f2.txt");
127cc290419Sopenharmony_ci            TestRunClient(debugServerPort, debugConnectKey, "shell find /proc");
128cc290419Sopenharmony_ci            TestRunClient(debugServerPort, debugConnectKey, "file send \"/d/a b/1.txt\" \"/d/a b/2.txt\"");
129cc290419Sopenharmony_ci            TestRunClient(debugServerPort, debugConnectKey, "file recv \"/d/a b/1.txt\" \"/d/a b/2.txt\"");
130cc290419Sopenharmony_ci#endif
131cc290419Sopenharmony_ci            break;
132cc290419Sopenharmony_ci        default:
133cc290419Sopenharmony_ci            break;
134cc290419Sopenharmony_ci    }
135cc290419Sopenharmony_ci    WRITE_LOG(LOG_DEBUG, "!!!Client finish");
136cc290419Sopenharmony_ci    return 0;
137cc290419Sopenharmony_ci}
138cc290419Sopenharmony_ci
139cc290419Sopenharmony_ciint TestRuntimeCommand(const int method, const string &debugServerPort, const string &debugConnectKey)
140cc290419Sopenharmony_ci{
141cc290419Sopenharmony_ci    switch (method) {
142cc290419Sopenharmony_ci        case UT_HELP:
143cc290419Sopenharmony_ci            TestRunClient(debugServerPort, "", CMDSTR_SOFTWARE_HELP.c_str());
144cc290419Sopenharmony_ci            TestRunClient(debugServerPort, "", CMDSTR_SOFTWARE_VERSION.c_str());
145cc290419Sopenharmony_ci            break;
146cc290419Sopenharmony_ci        case UT_DISCOVER:
147cc290419Sopenharmony_ci            TestRunClient(debugServerPort, "", CMDSTR_TARGET_DISCOVER.c_str());
148cc290419Sopenharmony_ci            break;
149cc290419Sopenharmony_ci        case UT_LIST_TARGETS:
150cc290419Sopenharmony_ci            TestRunClient(debugServerPort, "", CMDSTR_LIST_TARGETS.c_str());
151cc290419Sopenharmony_ci            break;
152cc290419Sopenharmony_ci        case UT_CONNECT_ANY:
153cc290419Sopenharmony_ci            TestRunClient(debugServerPort, "", CMDSTR_CONNECT_ANY.c_str());
154cc290419Sopenharmony_ci            break;
155cc290419Sopenharmony_ci        case UT_KILL_SERVER:
156cc290419Sopenharmony_ci            TestRunClient(debugServerPort, "", CMDSTR_SERVICE_KILL.c_str());
157cc290419Sopenharmony_ci            break;
158cc290419Sopenharmony_ci        case UT_KILL_DAEMON:
159cc290419Sopenharmony_ci            TestRunClient(debugServerPort, debugConnectKey, "kill daemon");
160cc290419Sopenharmony_ci            break;
161cc290419Sopenharmony_ci        default:
162cc290419Sopenharmony_ci            TestTaskCommand(method, debugServerPort, debugConnectKey);
163cc290419Sopenharmony_ci            break;
164cc290419Sopenharmony_ci    }
165cc290419Sopenharmony_ci    return 0;
166cc290419Sopenharmony_ci}
167cc290419Sopenharmony_ci}  // namespace HdcTest
168