11e934351Sopenharmony_ci/*
21e934351Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
31e934351Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
41e934351Sopenharmony_ci * you may not use this file except in compliance with the License.
51e934351Sopenharmony_ci * You may obtain a copy of the License at
61e934351Sopenharmony_ci *
71e934351Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
81e934351Sopenharmony_ci *
91e934351Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
101e934351Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
111e934351Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
121e934351Sopenharmony_ci * See the License for the specific language governing permissions and
131e934351Sopenharmony_ci * limitations under the License.
141e934351Sopenharmony_ci */
151e934351Sopenharmony_ci
161e934351Sopenharmony_ci#include <gtest/gtest.h>
171e934351Sopenharmony_ci#include <iostream>
181e934351Sopenharmony_ci
191e934351Sopenharmony_ci#if HAS_NETMANAGER_BASE
201e934351Sopenharmony_ci#include "http_client_network_message.h"
211e934351Sopenharmony_ci#include "http_network_message.h"
221e934351Sopenharmony_ci#include "i_network_message.h"
231e934351Sopenharmony_ci#include "netstack_network_profiler.h"
241e934351Sopenharmony_ci
251e934351Sopenharmony_cinamespace OHOS {
261e934351Sopenharmony_cinamespace NetStack {
271e934351Sopenharmony_cinamespace {
281e934351Sopenharmony_ciusing namespace testing::ext;
291e934351Sopenharmony_cistatic constexpr const char *REQUEST_ID = "123";
301e934351Sopenharmony_cistatic constexpr const char *HTTP_VERSION_2 = "2";
311e934351Sopenharmony_cistatic constexpr const char *REQUEST_URL = "https://127.0.0.1";
321e934351Sopenharmony_cistatic constexpr const char *REQUEST_IP_ADDRESS = "127.0.0.1";
331e934351Sopenharmony_cistatic constexpr const char *REQUEST_STRING = "unused";
341e934351Sopenharmony_cistatic constexpr const char *REQUEST_HEADERS = "HTTP/1.1 200 OK\r\nk:v";
351e934351Sopenharmony_cistatic constexpr const char *REQUEST_REASON_PARSE = "OK";
361e934351Sopenharmony_cistatic constexpr const uint64_t REQUEST_BEGIN_TIME = 100;
371e934351Sopenharmony_cistatic constexpr const double REQUEST_DNS_TIME = 10;
381e934351Sopenharmony_ci
391e934351Sopenharmony_ciclass MockNetworkMessage : public INetworkMessage {
401e934351Sopenharmony_cipublic:
411e934351Sopenharmony_ci    MockNetworkMessage() = delete;
421e934351Sopenharmony_ci
431e934351Sopenharmony_ci    MockNetworkMessage(std::string requestId, CURL *handle)
441e934351Sopenharmony_ci    {
451e934351Sopenharmony_ci        requestId_ = requestId;
461e934351Sopenharmony_ci        requestBeginTime_ = 0;
471e934351Sopenharmony_ci        handle_ = handle;
481e934351Sopenharmony_ci    }
491e934351Sopenharmony_ci
501e934351Sopenharmony_ci    ~MockNetworkMessage() override;
511e934351Sopenharmony_ci
521e934351Sopenharmony_ci    DfxMessage Parse() override;
531e934351Sopenharmony_ci
541e934351Sopenharmony_ciprivate:
551e934351Sopenharmony_ci    CURL *handle_ = nullptr;
561e934351Sopenharmony_ci};
571e934351Sopenharmony_ci
581e934351Sopenharmony_ciMockNetworkMessage::~MockNetworkMessage() = default;
591e934351Sopenharmony_ci
601e934351Sopenharmony_ciDfxMessage MockNetworkMessage::Parse()
611e934351Sopenharmony_ci{
621e934351Sopenharmony_ci    DfxMessage msg{};
631e934351Sopenharmony_ci    msg.requestId_ = requestId_;
641e934351Sopenharmony_ci    msg.requestBeginTime_ = requestBeginTime_;
651e934351Sopenharmony_ci    uint32_t ret = 0;
661e934351Sopenharmony_ci    ret = GetIpAddressFromCurlHandle(msg.responseIpAddress_, handle_);
671e934351Sopenharmony_ci    if (ret == 0) {
681e934351Sopenharmony_ci        msg.responseIpAddress_ = REQUEST_IP_ADDRESS;
691e934351Sopenharmony_ci    }
701e934351Sopenharmony_ci    ret = GetEffectiveUrlFromCurlHandle(msg.responseEffectiveUrl_, handle_);
711e934351Sopenharmony_ci    if (ret != 0) {
721e934351Sopenharmony_ci        msg.responseEffectiveUrl_ = REQUEST_STRING;
731e934351Sopenharmony_ci    }
741e934351Sopenharmony_ci    ret = GetHttpVersionFromCurlHandle(msg.responseHttpVersion_, handle_);
751e934351Sopenharmony_ci    if (ret == 0) {
761e934351Sopenharmony_ci        msg.responseHttpVersion_ = HTTP_VERSION_2;
771e934351Sopenharmony_ci    }
781e934351Sopenharmony_ci    TimeInfo timeInfo{};
791e934351Sopenharmony_ci    ret = GetTimeInfoFromCurlHandle(timeInfo, handle_);
801e934351Sopenharmony_ci    if (ret == 0) {
811e934351Sopenharmony_ci        msg.dnsEndTime_ = REQUEST_DNS_TIME;
821e934351Sopenharmony_ci    }
831e934351Sopenharmony_ci    msg.responseReasonPhrase_ = GetReasonParse(REQUEST_HEADERS);
841e934351Sopenharmony_ci    return msg;
851e934351Sopenharmony_ci}
861e934351Sopenharmony_ci
871e934351Sopenharmony_ciCURL *GetCurlHandle()
881e934351Sopenharmony_ci{
891e934351Sopenharmony_ci    CURL *handle = curl_easy_init();
901e934351Sopenharmony_ci    curl_easy_setopt(handle, CURLOPT_URL, REQUEST_URL);
911e934351Sopenharmony_ci    curl_easy_setopt(handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
921e934351Sopenharmony_ci    return handle;
931e934351Sopenharmony_ci}
941e934351Sopenharmony_ci}
951e934351Sopenharmony_ci
961e934351Sopenharmony_ciclass NetStackNetworkProfilerUtilsTest : public testing::Test {
971e934351Sopenharmony_cipublic:
981e934351Sopenharmony_ci    static void SetUpTestCase() {}
991e934351Sopenharmony_ci
1001e934351Sopenharmony_ci    static void TearDownTestCase() {}
1011e934351Sopenharmony_ci
1021e934351Sopenharmony_ci    virtual void SetUp() {}
1031e934351Sopenharmony_ci
1041e934351Sopenharmony_ci    virtual void TearDown() {}
1051e934351Sopenharmony_ci};
1061e934351Sopenharmony_ci
1071e934351Sopenharmony_ciHWTEST_F(NetStackNetworkProfilerUtilsTest, INetworkMessageTest001, TestSize.Level2) {
1081e934351Sopenharmony_ci    MockNetworkMessage mockInstance(REQUEST_ID, GetCurlHandle());
1091e934351Sopenharmony_ci    mockInstance.SetRequestBeginTime(REQUEST_BEGIN_TIME);
1101e934351Sopenharmony_ci    DfxMessage msg = mockInstance.Parse();
1111e934351Sopenharmony_ci    EXPECT_EQ(msg.requestId_, REQUEST_ID);
1121e934351Sopenharmony_ci    EXPECT_EQ(msg.requestBeginTime_, REQUEST_BEGIN_TIME);
1131e934351Sopenharmony_ci    EXPECT_EQ(msg.responseIpAddress_, REQUEST_IP_ADDRESS);
1141e934351Sopenharmony_ci    EXPECT_EQ(msg.responseHttpVersion_, HTTP_VERSION_2);
1151e934351Sopenharmony_ci    EXPECT_NE(msg.responseEffectiveUrl_, REQUEST_STRING);
1161e934351Sopenharmony_ci    EXPECT_EQ(msg.dnsEndTime_, REQUEST_DNS_TIME);
1171e934351Sopenharmony_ci    EXPECT_EQ(msg.responseReasonPhrase_, REQUEST_REASON_PARSE);
1181e934351Sopenharmony_ci}
1191e934351Sopenharmony_ci
1201e934351Sopenharmony_ciHWTEST_F(NetStackNetworkProfilerUtilsTest, HttpNetworkMessageTest001, TestSize.Level2) {
1211e934351Sopenharmony_ci    Http::HttpRequestOptions request{};
1221e934351Sopenharmony_ci    Http::HttpResponse response{};
1231e934351Sopenharmony_ci    {
1241e934351Sopenharmony_ci        HttpNetworkMessage httpMsg(REQUEST_ID, request, response, GetCurlHandle());
1251e934351Sopenharmony_ci        DfxMessage dfxMsg = httpMsg.Parse();
1261e934351Sopenharmony_ci        EXPECT_EQ(dfxMsg.requestId_, REQUEST_ID);
1271e934351Sopenharmony_ci    }
1281e934351Sopenharmony_ci}
1291e934351Sopenharmony_ci
1301e934351Sopenharmony_ciHWTEST_F(NetStackNetworkProfilerUtilsTest, HttpNetworkMessageTest002, TestSize.Level2) {
1311e934351Sopenharmony_ci    Http::HttpRequestOptions request{};
1321e934351Sopenharmony_ci    Http::HttpResponse response{};
1331e934351Sopenharmony_ci    HttpNetworkMessage httpMsg(REQUEST_ID, request, response, GetCurlHandle());
1341e934351Sopenharmony_ci    DfxMessage dfxMsg = httpMsg.Parse();
1351e934351Sopenharmony_ci    EXPECT_EQ(dfxMsg.requestId_, REQUEST_ID);
1361e934351Sopenharmony_ci    EXPECT_EQ(dfxMsg.requestBeginTime_, 0);
1371e934351Sopenharmony_ci}
1381e934351Sopenharmony_ci
1391e934351Sopenharmony_ciHWTEST_F(NetStackNetworkProfilerUtilsTest, HttpNetworkMessageTest003, TestSize.Level2) {
1401e934351Sopenharmony_ci    Http::HttpRequestOptions request{};
1411e934351Sopenharmony_ci    Http::HttpResponse response{};
1421e934351Sopenharmony_ci    HttpNetworkMessage httpMsg(REQUEST_ID, request, response, nullptr);
1431e934351Sopenharmony_ci    DfxMessage dfxMsg = httpMsg.Parse();
1441e934351Sopenharmony_ci    EXPECT_EQ(dfxMsg.requestId_, REQUEST_ID);
1451e934351Sopenharmony_ci    EXPECT_EQ(dfxMsg.requestBeginTime_, 0);
1461e934351Sopenharmony_ci    EXPECT_EQ(dfxMsg.dnsEndTime_, 0);
1471e934351Sopenharmony_ci}
1481e934351Sopenharmony_ci
1491e934351Sopenharmony_ciHWTEST_F(NetStackNetworkProfilerUtilsTest, HttpClientNetworkMessageTest001, TestSize.Level2) {
1501e934351Sopenharmony_ci    HttpClient::HttpClientRequest request{};
1511e934351Sopenharmony_ci    HttpClient::HttpClientResponse response{};
1521e934351Sopenharmony_ci    {
1531e934351Sopenharmony_ci        HttpClientNetworkMessage httpClientMsg(REQUEST_ID, request, response, GetCurlHandle());
1541e934351Sopenharmony_ci        DfxMessage dfxMsg = httpClientMsg.Parse();
1551e934351Sopenharmony_ci        EXPECT_EQ(dfxMsg.requestId_, REQUEST_ID);
1561e934351Sopenharmony_ci    }
1571e934351Sopenharmony_ci}
1581e934351Sopenharmony_ci
1591e934351Sopenharmony_ciHWTEST_F(NetStackNetworkProfilerUtilsTest, HttpClientNetworkMessageTest002, TestSize.Level2) {
1601e934351Sopenharmony_ci    HttpClient::HttpClientRequest request{};
1611e934351Sopenharmony_ci    HttpClient::HttpClientResponse response{};
1621e934351Sopenharmony_ci    HttpClientNetworkMessage httpClientMsg(REQUEST_ID, request, response, GetCurlHandle());
1631e934351Sopenharmony_ci    DfxMessage dfxMsg = httpClientMsg.Parse();
1641e934351Sopenharmony_ci    EXPECT_EQ(dfxMsg.requestId_, REQUEST_ID);
1651e934351Sopenharmony_ci    EXPECT_EQ(dfxMsg.requestBeginTime_, 0);
1661e934351Sopenharmony_ci}
1671e934351Sopenharmony_ci
1681e934351Sopenharmony_ciHWTEST_F(NetStackNetworkProfilerUtilsTest, HttpClientNetworkMessageTest003, TestSize.Level2) {
1691e934351Sopenharmony_ci    HttpClient::HttpClientRequest request{};
1701e934351Sopenharmony_ci    HttpClient::HttpClientResponse response{};
1711e934351Sopenharmony_ci    HttpClientNetworkMessage httpClientMsg(REQUEST_ID, request, response, nullptr);
1721e934351Sopenharmony_ci    DfxMessage dfxMsg = httpClientMsg.Parse();
1731e934351Sopenharmony_ci    EXPECT_EQ(dfxMsg.requestId_, REQUEST_ID);
1741e934351Sopenharmony_ci    EXPECT_EQ(dfxMsg.requestBeginTime_, 0);
1751e934351Sopenharmony_ci    EXPECT_EQ(dfxMsg.dnsEndTime_, 0);
1761e934351Sopenharmony_ci}
1771e934351Sopenharmony_ci}
1781e934351Sopenharmony_ci}
1791e934351Sopenharmony_ci#endif
180