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
16cc290419Sopenharmony_ci#include <HdcJdwpTest.h>
17cc290419Sopenharmony_ciusing namespace testing::ext;
18cc290419Sopenharmony_ciusing namespace std;
19cc290419Sopenharmony_cinamespace Hdc {
20cc290419Sopenharmony_civoid HdcJdwpTest::SetUpTestCase() {}
21cc290419Sopenharmony_ci
22cc290419Sopenharmony_civoid HdcJdwpTest::TearDownTestCase() {}
23cc290419Sopenharmony_ci
24cc290419Sopenharmony_civoid HdcJdwpTest::SetUp() {}
25cc290419Sopenharmony_ci
26cc290419Sopenharmony_civoid HdcJdwpTest::TearDown() {}
27cc290419Sopenharmony_ci
28cc290419Sopenharmony_cistd::unique_ptr<HdcJdwp> HdcJdwpTest::InstanceHdcJdwp()
29cc290419Sopenharmony_ci{
30cc290419Sopenharmony_ci    uv_loop_t loopMain;
31cc290419Sopenharmony_ci    uv_loop_init(&loopMain);
32cc290419Sopenharmony_ci    // Base::SetLogLevel(Hdc::LOG_FULL);
33cc290419Sopenharmony_ci    return std::make_unique<HdcJdwp>(&loopMain);
34cc290419Sopenharmony_ci}
35cc290419Sopenharmony_ci
36cc290419Sopenharmony_ci/*
37cc290419Sopenharmony_ci * @tc.name: TestCreateFdEventPoll
38cc290419Sopenharmony_ci * @tc.desc: Check the poll thread.
39cc290419Sopenharmony_ci * @tc.type: FUNC
40cc290419Sopenharmony_ci */
41cc290419Sopenharmony_ciHWTEST_F(HdcJdwpTest, TestCreateFdEventPoll, TestSize.Level1)
42cc290419Sopenharmony_ci{
43cc290419Sopenharmony_ci    std::unique_ptr<HdcJdwp> mJdwpTest = HdcJdwpTest::InstanceHdcJdwp();
44cc290419Sopenharmony_ci    ASSERT_NE(mJdwpTest, nullptr) << "Instanse HdcJdwp fail.";
45cc290419Sopenharmony_ci    ASSERT_EQ(mJdwpTest->Initial(), 0) << " Jdwp init instance fail.";
46cc290419Sopenharmony_ci    EXPECT_GE(mJdwpTest->pollNodeMap.size(), 0u) << " Jdwp pollNodeMap is null.";
47cc290419Sopenharmony_ci    mJdwpTest->Stop();
48cc290419Sopenharmony_ci}
49cc290419Sopenharmony_ci
50cc290419Sopenharmony_ci/*
51cc290419Sopenharmony_ci * @tc.name: CreateJdwpTracker
52cc290419Sopenharmony_ci * @tc.desc: Check the poll thread.
53cc290419Sopenharmony_ci * @tc.type: FUNC
54cc290419Sopenharmony_ci */
55cc290419Sopenharmony_ciHWTEST_F(HdcJdwpTest, TestCreateJdwpTracker, TestSize.Level1)
56cc290419Sopenharmony_ci{
57cc290419Sopenharmony_ci    std::unique_ptr<HdcJdwp> mJdwpTest = HdcJdwpTest::InstanceHdcJdwp();
58cc290419Sopenharmony_ci    ASSERT_NE(mJdwpTest, nullptr) << "Instanse HdcJdwp fail.";
59cc290419Sopenharmony_ci    ASSERT_EQ(mJdwpTest->Initial(), 0) << "Instanse HdcJdwp fail.";
60cc290419Sopenharmony_ci    ASSERT_EQ(mJdwpTest->CreateJdwpTracker(nullptr), 0) << "CreateJdwpTracker nullptr fail.";
61cc290419Sopenharmony_ci    HTaskInfo hTaskInfo = new TaskInformation();
62cc290419Sopenharmony_ci    HdcSessionBase HdcSessionTest(false);
63cc290419Sopenharmony_ci    hTaskInfo->ownerSessionClass = &HdcSessionTest;
64cc290419Sopenharmony_ci    ASSERT_EQ(mJdwpTest->CreateJdwpTracker(hTaskInfo), 1) << "CreateJdwpTracker valid param fail.";
65cc290419Sopenharmony_ci    delete hTaskInfo;
66cc290419Sopenharmony_ci    mJdwpTest->Stop();
67cc290419Sopenharmony_ci}
68cc290419Sopenharmony_ci
69cc290419Sopenharmony_ci/*
70cc290419Sopenharmony_ci * @tc.name: TestReadStream
71cc290419Sopenharmony_ci * @tc.desc: Check ReadStream interface of pipe connection.
72cc290419Sopenharmony_ci * @tc.type: FUNC
73cc290419Sopenharmony_ci */
74cc290419Sopenharmony_ciHWTEST_F(HdcJdwpTest, TestReadStream, TestSize.Level1)
75cc290419Sopenharmony_ci{
76cc290419Sopenharmony_ci    std::unique_ptr<HdcJdwp> mJdwpTest = HdcJdwpTest::InstanceHdcJdwp();
77cc290419Sopenharmony_ci    mJdwpTest->Initial();
78cc290419Sopenharmony_ci    uv_pipe_t pipe;
79cc290419Sopenharmony_ci    uv_stream_t *stream = (uv_stream_t *)&pipe;
80cc290419Sopenharmony_ci    HdcJdwp::HCtxJdwp ctxJdwp = (HdcJdwp::HCtxJdwp)mJdwpTest->MallocContext();
81cc290419Sopenharmony_ci    ctxJdwp->finish = true; // For skip HdcJdwp::FreeContext, because the pipe
82cc290419Sopenharmony_ci                            // connect hasn't actually established
83cc290419Sopenharmony_ci    pipe.data = ctxJdwp;
84cc290419Sopenharmony_ci    // invalid nread < min
85cc290419Sopenharmony_ci    mJdwpTest->ReadStream(stream, (HdcJdwp::JS_PKG_MIN_SIZE - 1), nullptr);
86cc290419Sopenharmony_ci    ASSERT_EQ(mJdwpTest->mapCtxJdwp.size(), 0u) << "Instanse HdcJdwp fail.";
87cc290419Sopenharmony_ci    // invalid nread > max
88cc290419Sopenharmony_ci    mJdwpTest->ReadStream(stream, (HdcJdwp::JS_PKG_MX_SIZE + 1), nullptr);
89cc290419Sopenharmony_ci    ASSERT_EQ(mJdwpTest->mapCtxJdwp.size(), 0u) << "Instanse HdcJdwp fail.";
90cc290419Sopenharmony_ci
91cc290419Sopenharmony_ci    // valid parameters
92cc290419Sopenharmony_ci    string pkgName = "JDWP_UTEST";
93cc290419Sopenharmony_ci    uint32_t pkgSize = pkgName.size() + sizeof(HdcJdwp::JsMsgHeader); // JsMsgHeader pkgName;
94cc290419Sopenharmony_ci    uint8_t *info = reinterpret_cast<uint8_t *>(ctxJdwp->buf);
95cc290419Sopenharmony_ci    ASSERT_NE(info, nullptr) << "TestReadStream info null.";
96cc290419Sopenharmony_ci    memset_s(info, sizeof(ctxJdwp->buf), 0, sizeof(ctxJdwp->buf));
97cc290419Sopenharmony_ci    HdcJdwp::JsMsgHeader *jsMsg = (HdcJdwp::JsMsgHeader *)info;
98cc290419Sopenharmony_ci    jsMsg->pid = 1212;
99cc290419Sopenharmony_ci    jsMsg->msgLen = pkgSize;
100cc290419Sopenharmony_ci    ASSERT_EQ(
101cc290419Sopenharmony_ci        memcpy_s(info + sizeof(HdcJdwp::JsMsgHeader), pkgName.size(), &pkgName[0], pkgName.size()),
102cc290419Sopenharmony_ci        0)
103cc290419Sopenharmony_ci        << "TestReadStream memcpy_s fail.";
104cc290419Sopenharmony_ci    mJdwpTest->ReadStream(stream, pkgSize, nullptr);
105cc290419Sopenharmony_ci    ASSERT_NE(mJdwpTest->AdminContext(OP_QUERY, 1212, nullptr), nullptr)
106cc290419Sopenharmony_ci        << "TestReadStream query fail.";
107cc290419Sopenharmony_ci    mJdwpTest->Stop();
108cc290419Sopenharmony_ci}
109cc290419Sopenharmony_ci} // namespace Hdc
110