1/*
2 * Copyright (c) 2024 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 <gtest/gtest.h>
17#include <iostream>
18
19#if HAS_NETMANAGER_BASE
20#include "http_client_network_message.h"
21#include "http_network_message.h"
22#include "i_network_message.h"
23#include "netstack_network_profiler.h"
24
25namespace OHOS {
26namespace NetStack {
27namespace {
28using namespace testing::ext;
29static constexpr const char *REQUEST_ID = "123";
30static constexpr const char *HTTP_VERSION_2 = "2";
31static constexpr const char *REQUEST_URL = "https://127.0.0.1";
32static constexpr const char *REQUEST_IP_ADDRESS = "127.0.0.1";
33static constexpr const char *REQUEST_STRING = "unused";
34static constexpr const char *REQUEST_HEADERS = "HTTP/1.1 200 OK\r\nk:v";
35static constexpr const char *REQUEST_REASON_PARSE = "OK";
36static constexpr const uint64_t REQUEST_BEGIN_TIME = 100;
37static constexpr const double REQUEST_DNS_TIME = 10;
38
39class MockNetworkMessage : public INetworkMessage {
40public:
41    MockNetworkMessage() = delete;
42
43    MockNetworkMessage(std::string requestId, CURL *handle)
44    {
45        requestId_ = requestId;
46        requestBeginTime_ = 0;
47        handle_ = handle;
48    }
49
50    ~MockNetworkMessage() override;
51
52    DfxMessage Parse() override;
53
54private:
55    CURL *handle_ = nullptr;
56};
57
58MockNetworkMessage::~MockNetworkMessage() = default;
59
60DfxMessage MockNetworkMessage::Parse()
61{
62    DfxMessage msg{};
63    msg.requestId_ = requestId_;
64    msg.requestBeginTime_ = requestBeginTime_;
65    uint32_t ret = 0;
66    ret = GetIpAddressFromCurlHandle(msg.responseIpAddress_, handle_);
67    if (ret == 0) {
68        msg.responseIpAddress_ = REQUEST_IP_ADDRESS;
69    }
70    ret = GetEffectiveUrlFromCurlHandle(msg.responseEffectiveUrl_, handle_);
71    if (ret != 0) {
72        msg.responseEffectiveUrl_ = REQUEST_STRING;
73    }
74    ret = GetHttpVersionFromCurlHandle(msg.responseHttpVersion_, handle_);
75    if (ret == 0) {
76        msg.responseHttpVersion_ = HTTP_VERSION_2;
77    }
78    TimeInfo timeInfo{};
79    ret = GetTimeInfoFromCurlHandle(timeInfo, handle_);
80    if (ret == 0) {
81        msg.dnsEndTime_ = REQUEST_DNS_TIME;
82    }
83    msg.responseReasonPhrase_ = GetReasonParse(REQUEST_HEADERS);
84    return msg;
85}
86
87CURL *GetCurlHandle()
88{
89    CURL *handle = curl_easy_init();
90    curl_easy_setopt(handle, CURLOPT_URL, REQUEST_URL);
91    curl_easy_setopt(handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
92    return handle;
93}
94}
95
96class NetStackNetworkProfilerUtilsTest : public testing::Test {
97public:
98    static void SetUpTestCase() {}
99
100    static void TearDownTestCase() {}
101
102    virtual void SetUp() {}
103
104    virtual void TearDown() {}
105};
106
107HWTEST_F(NetStackNetworkProfilerUtilsTest, INetworkMessageTest001, TestSize.Level2) {
108    MockNetworkMessage mockInstance(REQUEST_ID, GetCurlHandle());
109    mockInstance.SetRequestBeginTime(REQUEST_BEGIN_TIME);
110    DfxMessage msg = mockInstance.Parse();
111    EXPECT_EQ(msg.requestId_, REQUEST_ID);
112    EXPECT_EQ(msg.requestBeginTime_, REQUEST_BEGIN_TIME);
113    EXPECT_EQ(msg.responseIpAddress_, REQUEST_IP_ADDRESS);
114    EXPECT_EQ(msg.responseHttpVersion_, HTTP_VERSION_2);
115    EXPECT_NE(msg.responseEffectiveUrl_, REQUEST_STRING);
116    EXPECT_EQ(msg.dnsEndTime_, REQUEST_DNS_TIME);
117    EXPECT_EQ(msg.responseReasonPhrase_, REQUEST_REASON_PARSE);
118}
119
120HWTEST_F(NetStackNetworkProfilerUtilsTest, HttpNetworkMessageTest001, TestSize.Level2) {
121    Http::HttpRequestOptions request{};
122    Http::HttpResponse response{};
123    {
124        HttpNetworkMessage httpMsg(REQUEST_ID, request, response, GetCurlHandle());
125        DfxMessage dfxMsg = httpMsg.Parse();
126        EXPECT_EQ(dfxMsg.requestId_, REQUEST_ID);
127    }
128}
129
130HWTEST_F(NetStackNetworkProfilerUtilsTest, HttpNetworkMessageTest002, TestSize.Level2) {
131    Http::HttpRequestOptions request{};
132    Http::HttpResponse response{};
133    HttpNetworkMessage httpMsg(REQUEST_ID, request, response, GetCurlHandle());
134    DfxMessage dfxMsg = httpMsg.Parse();
135    EXPECT_EQ(dfxMsg.requestId_, REQUEST_ID);
136    EXPECT_EQ(dfxMsg.requestBeginTime_, 0);
137}
138
139HWTEST_F(NetStackNetworkProfilerUtilsTest, HttpNetworkMessageTest003, TestSize.Level2) {
140    Http::HttpRequestOptions request{};
141    Http::HttpResponse response{};
142    HttpNetworkMessage httpMsg(REQUEST_ID, request, response, nullptr);
143    DfxMessage dfxMsg = httpMsg.Parse();
144    EXPECT_EQ(dfxMsg.requestId_, REQUEST_ID);
145    EXPECT_EQ(dfxMsg.requestBeginTime_, 0);
146    EXPECT_EQ(dfxMsg.dnsEndTime_, 0);
147}
148
149HWTEST_F(NetStackNetworkProfilerUtilsTest, HttpClientNetworkMessageTest001, TestSize.Level2) {
150    HttpClient::HttpClientRequest request{};
151    HttpClient::HttpClientResponse response{};
152    {
153        HttpClientNetworkMessage httpClientMsg(REQUEST_ID, request, response, GetCurlHandle());
154        DfxMessage dfxMsg = httpClientMsg.Parse();
155        EXPECT_EQ(dfxMsg.requestId_, REQUEST_ID);
156    }
157}
158
159HWTEST_F(NetStackNetworkProfilerUtilsTest, HttpClientNetworkMessageTest002, TestSize.Level2) {
160    HttpClient::HttpClientRequest request{};
161    HttpClient::HttpClientResponse response{};
162    HttpClientNetworkMessage httpClientMsg(REQUEST_ID, request, response, GetCurlHandle());
163    DfxMessage dfxMsg = httpClientMsg.Parse();
164    EXPECT_EQ(dfxMsg.requestId_, REQUEST_ID);
165    EXPECT_EQ(dfxMsg.requestBeginTime_, 0);
166}
167
168HWTEST_F(NetStackNetworkProfilerUtilsTest, HttpClientNetworkMessageTest003, TestSize.Level2) {
169    HttpClient::HttpClientRequest request{};
170    HttpClient::HttpClientResponse response{};
171    HttpClientNetworkMessage httpClientMsg(REQUEST_ID, request, response, nullptr);
172    DfxMessage dfxMsg = httpClientMsg.Parse();
173    EXPECT_EQ(dfxMsg.requestId_, REQUEST_ID);
174    EXPECT_EQ(dfxMsg.requestBeginTime_, 0);
175    EXPECT_EQ(dfxMsg.dnsEndTime_, 0);
176}
177}
178}
179#endif
180