Merge pull request !1648 from maosiping/master
This commit is contained in:
oh_ci 2024-10-30 03:21:50 +00:00 committed by Gitee
commit 59af54355e
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
7 changed files with 1656 additions and 0 deletions

View File

@ -0,0 +1,225 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "netstack_log.h"
#include "gtest/gtest.h"
#include <cstring>
#include <iostream>
#include "local_socket_context.h"
#include "local_socket_exec.h"
#include "local_socket_server_context.h"
#include "multicast_get_loopback_context.h"
#include "multicast_get_ttl_context.h"
#include "multicast_membership_context.h"
#include "multicast_set_loopback_context.h"
#include "multicast_set_ttl_context.h"
#include "socket_exec.h"
class SocketTest : public testing::Test {
public:
static void SetUpTestCase() {}
static void TearDownTestCase() {}
virtual void SetUp() {}
virtual void TearDown() {}
};
namespace {
using namespace testing::ext;
using namespace OHOS::NetStack::Socket;
HWTEST_F(SocketTest, MulticastTest001, TestSize.Level1)
{
napi_env env = nullptr;
OHOS::NetStack::EventManager eventManager;
MulticastMembershipContext context(env, &eventManager);
bool ret = SocketExec::ExecUdpAddMembership(&context);
EXPECT_EQ(ret, false);
}
HWTEST_F(SocketTest, MulticastTest002, TestSize.Level1)
{
napi_env env = nullptr;
OHOS::NetStack::EventManager eventManager;
MulticastMembershipContext context(env, &eventManager);
bool ret = SocketExec::ExecUdpDropMembership(&context);
EXPECT_EQ(ret, false);
}
HWTEST_F(SocketTest, MulticastTest003, TestSize.Level1)
{
napi_env env = nullptr;
OHOS::NetStack::EventManager eventManager;
MulticastSetTTLContext context(env, &eventManager);
bool ret = SocketExec::ExecSetMulticastTTL(&context);
EXPECT_EQ(ret, false);
}
HWTEST_F(SocketTest, MulticastTest004, TestSize.Level1)
{
napi_env env = nullptr;
OHOS::NetStack::EventManager eventManager;
MulticastGetTTLContext context(env, &eventManager);
bool ret = SocketExec::ExecGetMulticastTTL(&context);
EXPECT_EQ(ret, false);
}
HWTEST_F(SocketTest, MulticastTest005, TestSize.Level1)
{
napi_env env = nullptr;
OHOS::NetStack::EventManager eventManager;
MulticastSetLoopbackContext context(env, &eventManager);
bool ret = SocketExec::ExecSetLoopbackMode(&context);
EXPECT_EQ(ret, false);
}
HWTEST_F(SocketTest, MulticastTest006, TestSize.Level1)
{
napi_env env = nullptr;
OHOS::NetStack::EventManager eventManager;
MulticastGetLoopbackContext context(env, &eventManager);
bool ret = SocketExec::ExecGetLoopbackMode(&context);
EXPECT_EQ(ret, false);
}
HWTEST_F(SocketTest, LocalSocketTest001, TestSize.Level1)
{
napi_env env = nullptr;
OHOS::NetStack::EventManager eventManager;
LocalSocketBindContext context(env, &eventManager);
bool ret = LocalSocketExec::ExecLocalSocketBind(&context);
EXPECT_EQ(ret, false);
}
HWTEST_F(SocketTest, LocalSocketTest002, TestSize.Level1)
{
napi_env env = nullptr;
OHOS::NetStack::EventManager eventManager;
LocalSocketConnectContext context(env, &eventManager);
bool ret = LocalSocketExec::ExecLocalSocketConnect(&context);
EXPECT_EQ(ret, false);
}
HWTEST_F(SocketTest, LocalSocketTest003, TestSize.Level1)
{
napi_env env = nullptr;
OHOS::NetStack::EventManager eventManager;
LocalSocketSendContext context(env, &eventManager);
bool ret = LocalSocketExec::ExecLocalSocketSend(&context);
EXPECT_EQ(ret, false);
}
HWTEST_F(SocketTest, LocalSocketTest004, TestSize.Level1)
{
napi_env env = nullptr;
OHOS::NetStack::EventManager eventManager;
LocalSocketCloseContext context(env, &eventManager);
bool ret = LocalSocketExec::ExecLocalSocketClose(&context);
EXPECT_EQ(ret, false);
}
HWTEST_F(SocketTest, LocalSocketTest005, TestSize.Level1)
{
napi_env env = nullptr;
OHOS::NetStack::EventManager eventManager;
LocalSocketGetStateContext context(env, &eventManager);
bool ret = LocalSocketExec::ExecLocalSocketGetState(&context);
EXPECT_EQ(ret, true);
}
HWTEST_F(SocketTest, LocalSocketTest006, TestSize.Level1)
{
napi_env env = nullptr;
OHOS::NetStack::EventManager eventManager;
LocalSocketGetSocketFdContext context(env, &eventManager);
bool ret = LocalSocketExec::ExecLocalSocketGetSocketFd(&context);
EXPECT_EQ(ret, true);
}
HWTEST_F(SocketTest, LocalSocketTest007, TestSize.Level1)
{
napi_env env = nullptr;
OHOS::NetStack::EventManager eventManager;
LocalSocketSetExtraOptionsContext context(env, &eventManager);
bool ret = LocalSocketExec::ExecLocalSocketSetExtraOptions(&context);
EXPECT_EQ(ret, true);
}
HWTEST_F(SocketTest, LocalSocketTest008, TestSize.Level1)
{
napi_env env = nullptr;
OHOS::NetStack::EventManager eventManager;
LocalSocketGetExtraOptionsContext context(env, &eventManager);
bool ret = LocalSocketExec::ExecLocalSocketGetExtraOptions(&context);
EXPECT_EQ(ret, false);
}
HWTEST_F(SocketTest, LocalSocketServerTest001, TestSize.Level1)
{
napi_env env = nullptr;
OHOS::NetStack::EventManager eventManager;
LocalSocketServerListenContext context(env, &eventManager);
bool ret = LocalSocketExec::ExecLocalSocketServerListen(&context);
EXPECT_EQ(ret, false);
}
HWTEST_F(SocketTest, LocalSocketServerTest002, TestSize.Level1)
{
napi_env env = nullptr;
OHOS::NetStack::EventManager eventManager;
LocalSocketServerGetStateContext context(env, &eventManager);
bool ret = LocalSocketExec::ExecLocalSocketServerGetState(&context);
EXPECT_EQ(ret, true);
}
HWTEST_F(SocketTest, LocalSocketServerTest003, TestSize.Level1)
{
napi_env env = nullptr;
OHOS::NetStack::EventManager eventManager;
LocalSocketServerSetExtraOptionsContext context(env, &eventManager);
bool ret = LocalSocketExec::ExecLocalSocketServerSetExtraOptions(&context);
EXPECT_EQ(ret, false);
}
HWTEST_F(SocketTest, LocalSocketServerTest004, TestSize.Level1)
{
napi_env env = nullptr;
OHOS::NetStack::EventManager eventManager;
LocalSocketServerGetExtraOptionsContext context(env, &eventManager);
bool ret = LocalSocketExec::ExecLocalSocketServerGetExtraOptions(&context);
EXPECT_EQ(ret, false);
}
HWTEST_F(SocketTest, LocalSocketServerTest005, TestSize.Level1)
{
napi_env env = nullptr;
OHOS::NetStack::EventManager eventManager;
LocalSocketServerSendContext context(env, &eventManager);
bool ret = LocalSocketExec::ExecLocalSocketConnectionSend(&context);
EXPECT_EQ(ret, false);
}
HWTEST_F(SocketTest, LocalSocketServerTest006, TestSize.Level1)
{
napi_env env = nullptr;
OHOS::NetStack::EventManager eventManager;
LocalSocketServerCloseContext context(env, &eventManager);
bool ret = LocalSocketExec::ExecLocalSocketConnectionClose(&context);
EXPECT_EQ(ret, false);
}
} // namespace

View File

@ -0,0 +1,94 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <iostream>
#include <cstring>
#include "gtest/gtest.h"
#include "http_client_constant.h"
#include "netstack_log.h"
#define private public
#include "http_client_error.h"
using namespace OHOS::NetStack::HttpClient;
class HttpClientErrorTest : public testing::Test {
public:
static void SetUpTestCase() {}
static void TearDownTestCase() {}
virtual void SetUp() {}
virtual void TearDown() {}
};
namespace {
using namespace std;
using namespace testing::ext;
HWTEST_F(HttpClientErrorTest, GetErrorCodeTest001, TestSize.Level1)
{
HttpClientError req;
int errorCode = req.GetErrorCode();
EXPECT_EQ(errorCode, HttpErrorCode::HTTP_NONE_ERR);
}
HWTEST_F(HttpClientErrorTest, SetErrorCodeTest001, TestSize.Level1)
{
HttpClientError req;
req.SetErrorCode(HttpErrorCode::HTTP_PERMISSION_DENIED_CODE);
int errorCode = req.GetErrorCode();
EXPECT_EQ(errorCode, HttpErrorCode::HTTP_PERMISSION_DENIED_CODE);
}
HWTEST_F(HttpClientErrorTest, GetErrorMessageTest001, TestSize.Level1)
{
HttpClientError req;
string errorMsg = req.GetErrorMessage();
EXPECT_EQ(errorMsg, "No errors occurred");
}
HWTEST_F(HttpClientErrorTest, GetErrorMessageTest002, TestSize.Level1)
{
HttpClientError req;
req.SetErrorCode(HttpErrorCode::HTTP_PERMISSION_DENIED_CODE);
string errorMsg = req.GetErrorMessage();
EXPECT_EQ(errorMsg, "Permission denied");
}
HWTEST_F(HttpClientErrorTest, SetCURLResultTest001, TestSize.Level1)
{
HttpClientError req;
req.SetCURLResult(CURLE_OK);
int errorCode = req.GetErrorCode();
EXPECT_EQ(errorCode, HttpErrorCode::HTTP_NONE_ERR);
}
HWTEST_F(HttpClientErrorTest, SetCURLResultTest002, TestSize.Level1)
{
HttpClientError req;
req.SetCURLResult(CURLE_UNSUPPORTED_PROTOCOL);
int errorCode = req.GetErrorCode();
EXPECT_EQ(errorCode, HttpErrorCode::HTTP_UNSUPPORTED_PROTOCOL);
}
} // namespace

View File

@ -0,0 +1,182 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <iostream>
#include <cstring>
#include "gtest/gtest.h"
#include "http_client_constant.h"
#include "netstack_log.h"
#define private public
#include "http_client_response.h"
using namespace OHOS::NetStack::HttpClient;
class HttpClientResponseTest : public testing::Test {
public:
static void SetUpTestCase() {}
static void TearDownTestCase() {}
virtual void SetUp() {}
virtual void TearDown() {}
};
namespace {
using namespace std;
using namespace testing::ext;
HWTEST_F(HttpClientResponseTest, GetResponseCodeTest001, TestSize.Level1)
{
HttpClientResponse req;
int responseTest = req.GetResponseCode();
EXPECT_EQ(responseTest, ResponseCode::NONE);
}
HWTEST_F(HttpClientResponseTest, GetHeaderTest001, TestSize.Level1)
{
HttpClientResponse req;
string header = req.GetHeader();
EXPECT_EQ(header, "");
}
HWTEST_F(HttpClientResponseTest, GetRequestTimeTest001, TestSize.Level1)
{
HttpClientResponse req;
string requestTime = req.GetRequestTime();
EXPECT_EQ(requestTime, "");
}
HWTEST_F(HttpClientResponseTest, GetResponseTimeTest001, TestSize.Level1)
{
HttpClientResponse req;
string responseTime = req.GetResponseTime();
EXPECT_EQ(responseTime, "");
}
HWTEST_F(HttpClientResponseTest, SetRequestTimeTest001, TestSize.Level1)
{
HttpClientResponse req;
req.SetRequestTime("10");
string requestTime = req.GetRequestTime();
EXPECT_EQ(requestTime, "10");
}
HWTEST_F(HttpClientResponseTest, SetResponseTimeTest001, TestSize.Level1)
{
HttpClientResponse req;
req.SetResponseTime("10");
string responseTime = req.GetResponseTime();
EXPECT_EQ(responseTime, "10");
}
HWTEST_F(HttpClientResponseTest, AppendHeaderTest001, TestSize.Level1)
{
HttpClientResponse req;
std::string add = "test";
req.AppendHeader(add.data(), add.length());
string header = req.GetHeader();
EXPECT_EQ(header, "test");
}
HWTEST_F(HttpClientResponseTest, SetResponseCodeTest001, TestSize.Level1)
{
HttpClientResponse req;
req.SetResponseCode(ResponseCode::MULT_CHOICE);
int responseTest = req.GetResponseCode();
EXPECT_EQ(responseTest, ResponseCode::MULT_CHOICE);
}
HWTEST_F(HttpClientResponseTest, ResponseParseHeader001, TestSize.Level1)
{
HttpClientResponse req;
const char *emptyHead = " \r\n";
const char *errHead = "test1 data1\r\n";
const char *realHead = "test:data\r\n";
req.AppendHeader(emptyHead, strlen(emptyHead));
req.AppendHeader(errHead, strlen(errHead));
req.AppendHeader(realHead, strlen(realHead));
req.ParseHeaders();
auto headers = req.GetHeaders();
std::string ret;
std::for_each(headers.begin(), headers.end(), [&ret](const auto &item) {
if (!item.first.empty() && !item.second.empty()) {
ret += item.first + ":" + item.second + "\r\n";
}
});
EXPECT_EQ(realHead, ret);
}
HWTEST_F(HttpClientResponseTest, ResponseAppendCookie001, TestSize.Level1)
{
HttpClientResponse req;
const char *emptyHead = " \r\n";
const char *errHead = "test data\r\n";
const char *realHead = "test:data\r\n";
string cookies = "";
cookies.append(emptyHead);
cookies.append(errHead);
cookies.append(realHead);
req.AppendCookies(emptyHead, strlen(emptyHead));
req.AppendCookies(errHead, strlen(errHead));
req.AppendCookies(realHead, strlen(realHead));
auto ret = req.GetCookies();
EXPECT_EQ(cookies, ret);
}
HWTEST_F(HttpClientResponseTest, ResponseSetCookie001, TestSize.Level1)
{
HttpClientResponse req;
const char *realHead = "test:data\r\n";
req.SetCookies(realHead);
auto result = req.GetCookies();
EXPECT_EQ(realHead, result);
}
HWTEST_F(HttpClientResponseTest, ResponseSetWarning001, TestSize.Level1)
{
HttpClientResponse req;
const char *realHead = "test:data";
const char *warningText = "Warning";
req.SetWarning(realHead);
auto headers = req.GetHeaders();
for (auto &item: headers) {
auto key = item.first.c_str();
if (strcmp(warningText, key) == 0) {
EXPECT_EQ(realHead, item.second);
return;
}
}
EXPECT_FALSE(true);
}
HWTEST_F(HttpClientResponseTest, ResponseSetRawHeader001, TestSize.Level1)
{
HttpClientResponse req;
const char *realHead = "test:data\r\n";
req.SetRawHeader(realHead);
auto header = req.GetHeader();
EXPECT_EQ(realHead, header);
}
} // namespace

View File

@ -0,0 +1,75 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cstring>
#include <iostream>
#include "cert_context.h"
#include "gtest/gtest.h"
#include "net_ssl.h"
#include "net_ssl_async_work.h"
#include "net_ssl_exec.h"
#include "net_ssl_module.h"
#include "net_ssl_verify_cert.h"
#include "netstack_log.h"
class NetsslTest : public testing::Test {
public:
static void SetUpTestCase() {}
static void TearDownTestCase() {}
virtual void SetUp() {}
virtual void TearDown() {}
};
namespace {
using namespace testing::ext;
using namespace OHOS::NetStack::Ssl;
HWTEST_F(NetsslTest, CertVerifyTest001, TestSize.Level1)
{
napi_env env = nullptr;
CertContext context(env, nullptr);
bool ret = SslExec::ExecVerify(&context);
EXPECT_EQ(ret, false);
}
HWTEST_F(NetsslTest, NetStackVerifyCertificationTest001, TestSize.Level1)
{
CertBlob *cert = nullptr;
CertBlob *caCert = nullptr;
uint32_t ret = NetStackVerifyCertification(cert);
EXPECT_EQ(ret, SSL_X509_V_ERR_UNSPECIFIED);
ret = NetStackVerifyCertification(cert, caCert);
EXPECT_EQ(ret, SSL_X509_V_ERR_UNSPECIFIED);
}
HWTEST_F(NetsslTest, NetStackVerifyCertificationTest002, TestSize.Level1)
{
CertBlob cert;
CertBlob caCert;
uint32_t ret = NetStackVerifyCertification(&cert);
EXPECT_EQ(ret, SSL_X509_V_ERR_UNSPECIFIED);
ret = NetStackVerifyCertification(&cert, &caCert);
EXPECT_EQ(ret, SSL_X509_V_ERR_UNSPECIFIED);
}
} // namespace

View File

@ -0,0 +1,60 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <iostream>
#include <cstring>
#include "gtest/gtest.h"
#include "http_client_constant.h"
#include "netstack_log.h"
#define private public
#include "http_client.h"
#include "http_client_task.h"
#include <curl/curl.h>
using namespace OHOS::NetStack::HttpClient;
class HttpClientTest : public testing::Test {
public:
static void SetUpTestCase() {}
static void TearDownTestCase() {}
virtual void SetUp() {}
virtual void TearDown() {}
};
namespace {
using namespace std;
using namespace testing::ext;
HWTEST_F(HttpClientTest, AddRequestInfoTest001, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "http://www.baidu.com";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
task->Start();
while (task->GetStatus() != TaskStatus::IDLE) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
EXPECT_EQ(task->GetStatus(), IDLE);
}
} // namespace

View File

@ -0,0 +1,887 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <iostream>
#include <cstring>
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "http_client_constant.h"
#include "netstack_log.h"
#include "netstack_common_utils.h"
#define private public
#include "http_client_task.h"
#include "http_client.h"
#include "http_client_error.h"
#include <curl/curl.h>
#include "http_client_request.h"
#include "http_client_response.h"
#if HAS_NETMANAGER_BASE
#include "net_conn_client.h"
#include "network_security_config.h"
#endif
using namespace OHOS::NetStack::HttpClient;
using namespace testing;
using namespace testing::ext;
class HttpClientTaskTest : public testing::Test {
public:
static void SetUpTestCase() {}
static void TearDownTestCase() {}
virtual void SetUp() {}
virtual void TearDown() {}
void ProcesslTaskCb(std::shared_ptr<HttpClientTask> task);
};
namespace {
using namespace std;
using namespace testing::ext;
HWTEST_F(HttpClientTaskTest, GetHttpVersionTest001, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "https://www.baidu.com";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
uint32_t httpVersionTest = task->GetHttpVersion(HttpProtocol::HTTP_NONE);
EXPECT_EQ(httpVersionTest, CURL_HTTP_VERSION_NONE);
}
HWTEST_F(HttpClientTaskTest, GetHttpVersionTest002, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "https://www.baidu.com";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
uint32_t httpVersionTest = task->GetHttpVersion(HttpProtocol::HTTP1_1);
EXPECT_EQ(httpVersionTest, CURL_HTTP_VERSION_1_1);
}
HWTEST_F(HttpClientTaskTest, GetHttpVersionTest003, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "https://www.baidu.com";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
uint32_t httpVersionTest = task->GetHttpVersion(HttpProtocol::HTTP2);
EXPECT_EQ(httpVersionTest, CURL_HTTP_VERSION_2_0);
httpVersionTest = task->GetHttpVersion(HttpProtocol::HTTP3);
EXPECT_EQ(httpVersionTest, CURL_HTTP_VERSION_3);
}
HWTEST_F(HttpClientTaskTest, SetOtherCurlOptionTest001, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "http://www.httpbin.org/get";
httpReq.SetURL(url);
httpReq.SetHttpProxyType(NOT_USE);
HttpProxy proxy;
proxy.host = "192.168.147.60";
proxy.port = 8888;
proxy.exclusions = "www.httpbin.org";
proxy.tunnel = false;
httpReq.SetHttpProxy(proxy);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
bool result = task->SetOtherCurlOption(task->curlHandle_);
EXPECT_TRUE(result);
}
HWTEST_F(HttpClientTaskTest, SetOtherCurlOptionTest002, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "http://www.httpbin.org/get";
httpReq.SetURL(url);
httpReq.SetHttpProxyType(NOT_USE);
HttpProxy proxy;
proxy.host = "192.168.147.60";
proxy.port = 8888;
proxy.tunnel = false;
httpReq.SetHttpProxy(proxy);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
EXPECT_TRUE(task->SetOtherCurlOption(task->curlHandle_));
}
HWTEST_F(HttpClientTaskTest, SetOtherCurlOptionTest003, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "http://www.httpbin.org/get";
httpReq.SetURL(url);
httpReq.SetHttpProxyType(NOT_USE);
HttpProxy proxy;
proxy.host = "192.168.147.60";
proxy.port = 8888;
proxy.tunnel = false;
httpReq.SetHttpProxy(proxy);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
EXPECT_TRUE(task->SetOtherCurlOption(task->curlHandle_));
}
HWTEST_F(HttpClientTaskTest, SetOtherCurlOptionTest004, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "http://www.httpbin.org/get";
httpReq.SetURL(url);
httpReq.SetHttpProxyType(NOT_USE);
HttpProxy proxy;
proxy.host = "192.168.147.60";
proxy.port = 8888;
proxy.exclusions = "www.httpbin.org";
proxy.tunnel = true;
httpReq.SetHttpProxy(proxy);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
EXPECT_TRUE(task->SetOtherCurlOption(task->curlHandle_));
curl_easy_cleanup(task->curlHandle_);
task->curlHandle_ = nullptr;
}
HWTEST_F(HttpClientTaskTest, SetOtherCurlOptionTest005, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "http://www.httpbin.org/get";
httpReq.SetURL(url);
httpReq.SetHttpProxyType(USE_SPECIFIED);
HttpProxy proxy;
proxy.host = "192.168.147.60";
proxy.port = 8888;
proxy.exclusions = "www.test.org";
proxy.tunnel = true;
httpReq.SetHttpProxy(proxy);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
EXPECT_TRUE(task->SetOtherCurlOption(task->curlHandle_));
curl_easy_cleanup(task->curlHandle_);
task->curlHandle_ = nullptr;
}
HWTEST_F(HttpClientTaskTest, SetUploadOptionsTest001, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "http://www.httpbin.org/put";
httpReq.SetURL(url);
std::string method = "PUT";
httpReq.SetMethod(method);
HttpSession &session = HttpSession::GetInstance();
std::string filePath = "/bin/who";
auto task = session.CreateTask(httpReq, UPLOAD, filePath);
EXPECT_TRUE(task->SetUploadOptions(task->curlHandle_));
}
HWTEST_F(HttpClientTaskTest, SetUploadOptionsTest002, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "http://www.httpbin.org/put";
httpReq.SetURL(url);
std::string method = "PUT";
httpReq.SetMethod(method);
HttpSession &session = HttpSession::GetInstance();
std::string filePath = "";
auto task = session.CreateTask(httpReq, UPLOAD, filePath);
EXPECT_FALSE(task->SetUploadOptions(task->curlHandle_));
}
HWTEST_F(HttpClientTaskTest, SetUploadOptionsTest003, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "http://www.httpbin.org/put";
httpReq.SetURL(url);
std::string method = "PUT";
httpReq.SetMethod(method);
HttpSession &session = HttpSession::GetInstance();
std::string filePath = "unavailable";
auto task = session.CreateTask(httpReq, UPLOAD, filePath);
EXPECT_FALSE(task->SetUploadOptions(task->curlHandle_));
}
HWTEST_F(HttpClientTaskTest, SetCurlOptionsTest001, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "http://www.httpbin.org/get";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
EXPECT_TRUE(task->SetCurlOptions());
}
HWTEST_F(HttpClientTaskTest, SetCurlOptionsTest002, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "http://www.httpbin.org/get";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
task->request_.SetMethod(HttpConstant::HTTP_METHOD_HEAD);
EXPECT_TRUE(task->SetCurlOptions());
}
HWTEST_F(HttpClientTaskTest, SetCurlOptionsTest003, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "http://www.httpbin.org/put";
httpReq.SetURL(url);
std::string method = "PUT";
httpReq.SetMethod(method);
HttpSession &session = HttpSession::GetInstance();
std::string filePath = "/bin/who";
auto task = session.CreateTask(httpReq, UPLOAD, filePath);
task->curlHandle_ = nullptr;
EXPECT_FALSE(task->SetCurlOptions());
}
HWTEST_F(HttpClientTaskTest, SetCurlOptionsTest004, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "http://www.httpbin.org/get";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
task->request_.SetMethod(HttpConstant::HTTP_METHOD_POST);
EXPECT_TRUE(task->SetCurlOptions());
}
HWTEST_F(HttpClientTaskTest, SetCurlOptionsTest005, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "http://www.httpbin.org/get";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
task->curlHandle_ = nullptr;
EXPECT_FALSE(task->SetCurlOptions());
}
HWTEST_F(HttpClientTaskTest, SetCurlOptionsTest006, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "http://www.httpbin.org/get";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
task->curlHandle_ = nullptr;
std::string headerStr = "Connection:keep-alive";
task->curlHeaderList_ = curl_slist_append(task->curlHeaderList_, headerStr.c_str());
EXPECT_FALSE(task->SetCurlOptions());
}
HWTEST_F(HttpClientTaskTest, GetType001, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "https://www.baidu.com";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
EXPECT_EQ(TaskType::DEFAULT, task->GetType());
}
HWTEST_F(HttpClientTaskTest, GetFilePathTest001, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "http://www.httpbin.org/put";
httpReq.SetURL(url);
std::string method = "PUT";
httpReq.SetMethod(method);
HttpSession &session = HttpSession::GetInstance();
std::string filePath = "/bin/who";
auto task = session.CreateTask(httpReq, UPLOAD, filePath);
EXPECT_EQ(task->GetFilePath(), "/bin/who");
}
HWTEST_F(HttpClientTaskTest, GetTaskIdTest001, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "http://www.httpbin.org/get";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
unsigned int taskId = task->GetTaskId();
EXPECT_TRUE(taskId >= 0);
}
HWTEST_F(HttpClientTaskTest, OnSuccessTest001, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "https://www.baidu.com";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
task->OnSuccess([task](const HttpClientRequest &request, const HttpClientResponse &response) {});
EXPECT_TRUE(task->onSucceeded_ != nullptr);
}
HWTEST_F(HttpClientTaskTest, OnCancelTest001, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "http://www.httpbin.org/get";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
task->OnCancel([](const HttpClientRequest &request, const HttpClientResponse &response) {});
EXPECT_TRUE(task->onCanceled_ != nullptr);
}
HWTEST_F(HttpClientTaskTest, OnFailTest001, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "http://www.httpbin.org/get";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
task->OnFail(
[](const HttpClientRequest &request, const HttpClientResponse &response, const HttpClientError &error) {});
EXPECT_TRUE(task->onFailed_ != nullptr);
}
HWTEST_F(HttpClientTaskTest, OnDataReceiveTest001, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "http://www.httpbin.org/get";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
task->OnDataReceive([](const HttpClientRequest &request, const uint8_t *data, size_t length) {});
EXPECT_TRUE(task->onDataReceive_ != nullptr);
}
HWTEST_F(HttpClientTaskTest, OnProgressTest001, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "http://www.httpbin.org/get";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
task->OnProgress(
[](const HttpClientRequest &request, u_long dltotal, u_long dlnow, u_long ultotal, u_long ulnow) {});
EXPECT_TRUE(task->onProgress_ != nullptr);
}
HWTEST_F(HttpClientTaskTest, DataReceiveCallbackTest001, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "https://www.baidu.com";
const char *data = "https://www.baidu.com";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
auto *userData = task.get();
size_t size = 10;
size_t memBytes = 1;
task->OnDataReceive([](const HttpClientRequest &request, const uint8_t *data, size_t length) {});
size_t result = task->DataReceiveCallback(data, size, memBytes, userData);
EXPECT_EQ(result, size * memBytes);
}
HWTEST_F(HttpClientTaskTest, DataReceiveCallbackTest002, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "https://www.baidu.com";
const char *data = "https://www.baidu.com";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
auto *userData = task.get();
size_t size = 10;
size_t memBytes = 1;
size_t result = task->DataReceiveCallback(data, size, memBytes, userData);
EXPECT_EQ(result, size * memBytes);
}
HWTEST_F(HttpClientTaskTest, DataReceiveCallbackTest003, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "http://www.httpbin.org/get";
const char *data = "http://www.httpbin.org/get";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
auto *userData = task.get();
size_t size = 10;
size_t memBytes = 1;
task->canceled_ = true;
size_t result = task->DataReceiveCallback(data, size, memBytes, userData);
EXPECT_EQ(result, 0);
task->canceled_ = false;
}
HWTEST_F(HttpClientTaskTest, ProgressCallbackTest001, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "https://www.baidu.com";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
auto *userData = task.get();
curl_off_t dltotal = 100;
curl_off_t dlnow = 50;
curl_off_t ultotal = 200;
curl_off_t ulnow = 100;
int result;
result = task->ProgressCallback(userData, dltotal, dlnow, ultotal, ulnow);
EXPECT_EQ(result, 0);
}
HWTEST_F(HttpClientTaskTest, ProgressCallbackTest002, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "https://www.baidu.com";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
auto *userData = task.get();
curl_off_t dltotal = 100;
curl_off_t dlnow = 50;
curl_off_t ultotal = 200;
curl_off_t ulnow = 100;
int result;
task->Cancel();
result = task->ProgressCallback(userData, dltotal, dlnow, ultotal, ulnow);
EXPECT_EQ(result, CURLE_ABORTED_BY_CALLBACK);
}
HWTEST_F(HttpClientTaskTest, ProgressCallbackTest003, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "https://www.baidu.com";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
auto *userData = task.get();
curl_off_t dltotal = 100;
curl_off_t dlnow = 50;
curl_off_t ultotal = 200;
curl_off_t ulnow = 100;
int result = task->ProgressCallback(userData, dltotal, dlnow, ultotal, ulnow);
EXPECT_EQ(result, 0);
}
HWTEST_F(HttpClientTaskTest, HeaderReceiveCallbackTest001, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "https://www.baidu.com";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
auto *userData = task.get();
const char *data = "Test Header";
size_t size = 5;
size_t memBytes = 2;
size_t result = task->HeaderReceiveCallback(data, size, memBytes, userData);
EXPECT_EQ(result, size * memBytes);
}
HWTEST_F(HttpClientTaskTest, HeaderReceiveCallbackTest002, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "https://www.baidu.com";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
auto *userData = task.get();
const char *data = "Test Header";
size_t size = HttpConstant::MAX_DATA_LIMIT + 1;
size_t memBytes = 1;
size_t result = task->HeaderReceiveCallback(data, size, memBytes, userData);
EXPECT_EQ(result, 0);
}
HWTEST_F(HttpClientTaskTest, HeaderReceiveCallbackTest003, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "http://www.httpbin.org/get";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
auto *userData = task.get();
const char *data = "Test Header";
size_t size = 5;
size_t memBytes = 2;
size_t result = task->HeaderReceiveCallback(data, size, memBytes, userData);
EXPECT_EQ(result, size * memBytes);
}
HWTEST_F(HttpClientTaskTest, ProcessResponseCodeTest001, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "https://www.baidu.com";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
task->Start();
while (task->GetStatus() != TaskStatus::IDLE) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
EXPECT_TRUE(task->ProcessResponseCode());
}
HWTEST_F(HttpClientTaskTest, ProcessResponseTest001, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "https://www.baidu.com";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
CURLMsg msg;
msg.data.result = CURLE_ABORTED_BY_CALLBACK;
task->OnCancel([](const HttpClientRequest &request, const HttpClientResponse &response) {});
task->ProcessResponse(&msg);
EXPECT_TRUE(task->onCanceled_);
msg.data.result = CURLE_FAILED_INIT;
task->OnFail(
[](const HttpClientRequest &request, const HttpClientResponse &response, const HttpClientError &error) {});
task->ProcessResponse(&msg);
EXPECT_TRUE(task->onFailed_);
msg.data.result = CURLE_OK;
task->response_.SetResponseCode(ResponseCode::NOT_MODIFIED);
task->OnSuccess([task](const HttpClientRequest &request, const HttpClientResponse &response) {});
task->ProcessResponse(&msg);
EXPECT_TRUE(task->onSucceeded_);
task->curlHandle_ = nullptr;
task->OnFail(
[](const HttpClientRequest &request, const HttpClientResponse &response, const HttpClientError &error) {});
task->ProcessResponse(&msg);
EXPECT_TRUE(task->onFailed_);
}
HWTEST_F(HttpClientTaskTest, SetResponseTest001, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "https://www.baidu.com";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
HttpClientResponse resp;
resp.result_ = "result1";
task->SetResponse(resp);
EXPECT_EQ(task->response_.result_, "result1");
}
HWTEST_F(HttpClientTaskTest, GetHttpProxyInfoTest001, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "http://www.httpbin.org/get";
httpReq.SetURL(url);
httpReq.SetHttpProxyType(USE_SPECIFIED);
HttpProxy proxy;
proxy.host = "192.168.147.60";
proxy.port = 8888;
proxy.exclusions = "www.httpbin.org";
proxy.tunnel = false;
httpReq.SetHttpProxy(proxy);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
std::string host;
std::string exclusions;
int32_t port = 0;
bool tunnel = false;
task->GetHttpProxyInfo(host, port, exclusions, tunnel);
EXPECT_EQ(host, "192.168.147.60");
EXPECT_EQ(port, 8888);
EXPECT_EQ(exclusions, "www.httpbin.org");
EXPECT_FALSE(tunnel);
}
HWTEST_F(HttpClientTaskTest, GetHttpProxyInfoTest002, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "http://www.httpbin.org/get";
httpReq.SetURL(url);
httpReq.SetHttpProxyType(NOT_USE);
HttpProxy proxy;
proxy.host = "192.168.147.60";
proxy.port = 8888;
proxy.exclusions = "www.httpbin.org";
proxy.tunnel = false;
httpReq.SetHttpProxy(proxy);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
std::string host;
std::string exclusions;
int32_t port = 0;
bool tunnel = false;
task->GetHttpProxyInfo(host, port, exclusions, tunnel);
EXPECT_EQ(host, "");
EXPECT_EQ(port, 0);
EXPECT_EQ(exclusions, "");
EXPECT_FALSE(tunnel);
}
HWTEST_F(HttpClientTaskTest, SetStatus001, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "http://www.httpbin.org/get";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
task->SetStatus(RUNNING);
EXPECT_EQ(RUNNING, task->GetStatus());
}
HWTEST_F(HttpClientTaskTest, GetStatusTest001, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "http://www.httpbin.org/get";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
EXPECT_EQ(IDLE, task->GetStatus());
}
HWTEST_F(HttpClientTaskTest, GetStatusTest002, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "https://www.baidu.com";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
task->SetStatus(TaskStatus::RUNNING);
EXPECT_EQ(task->GetStatus(), RUNNING);
}
HWTEST_F(HttpClientTaskTest, StartTest001, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "http://www.baidu.com";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
task->SetStatus(TaskStatus::RUNNING);
bool result = task->Start();
EXPECT_FALSE(result);
}
HWTEST_F(HttpClientTaskTest, StartTest002, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "http://www.baidu.com";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
task->error_.SetErrorCode(HttpErrorCode::HTTP_UNSUPPORTED_PROTOCOL);
bool result = task->Start();
EXPECT_FALSE(result);
}
HWTEST_F(HttpClientTaskTest, ProcessCookieTest001, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "http://www.httpbin.org/cookies/set/name1/value1";
httpReq.SetURL(url);
httpReq.SetHeader("content-type", "text/plain");
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
task->Start();
while (task->GetStatus() != TaskStatus::IDLE) {
std::this_thread::sleep_for(std::chrono::milliseconds(5));
}
EXPECT_EQ(task->GetResponse().GetResponseCode(), ResponseCode::OK);
}
HWTEST_F(HttpClientTaskTest, CancelTest001, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "https://www.baidu.com";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
task->Cancel();
EXPECT_TRUE(task->canceled_);
}
HWTEST_F(HttpClientTaskTest, SetServerSSLCertOption001, TestSize.Level1)
{
HttpClientRequest httpReq;
std::string url = "https://www.baidu.com";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
bool result = task->SetServerSSLCertOption(task->curlHandle_);
EXPECT_TRUE(result);
}
#if HAS_NETMANAGER_BASE
HWTEST_F(HttpClientTaskTest, SetServerSSLCertOption_ShouldReturnTrue_WhenGetPinSetForHostNameReturnsZeroAndPinsIsEmpty,
TestSize.Level2)
{
auto configInstance = OHOS::NetManagerStandard::NetworkSecurityConfig::GetInstance();
OHOS::NetManagerStandard::DomainConfig config = {};
OHOS::NetManagerStandard::Domain domain;
domain.domainName_ = "https://www.example.com";
domain.includeSubDomains_ = false;
config.domains_.push_back(domain);
OHOS::NetManagerStandard::Pin pin;
pin.digestAlgorithm_ = "TEST";
pin.digest_ = "TEST";
config.pinSet_.pins_.push_back(pin);
configInstance.domainConfigs_.push_back(config);
HttpClientRequest httpReq;
std::string url = "https://www.example.com";
httpReq.SetURL(url);
HttpSession &session = HttpSession::GetInstance();
auto task = session.CreateTask(httpReq);
EXPECT_TRUE(task->SetServerSSLCertOption(task->curlHandle_));
}
#endif
class MockCurl {
public:
MOCK_METHOD0(easy_init, CURL *());
};
HWTEST_F(HttpClientTaskTest, HttpClientTask_ShouldNotCreate_WhenCurlInitFails, TestSize.Level0)
{
MockCurl mockCurl;
ON_CALL(mockCurl, easy_init).WillByDefault(Return(nullptr));
HttpClientRequest request;
HttpClientTask httpClientTask(request);
ASSERT_EQ(httpClientTask.GetStatus(), IDLE);
ASSERT_EQ(httpClientTask.GetType(), DEFAULT);
ASSERT_EQ(httpClientTask.canceled_, false);
HttpSession &session = HttpSession::GetInstance();
auto httpClientTask2 = session.CreateTask(request, UPLOAD, "testFakePath");
ASSERT_EQ(httpClientTask2->GetType(), UPLOAD);
}
} // namespace

View File

@ -0,0 +1,133 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "netstack_log.h"
#include "gtest/gtest.h"
#include <cstring>
#include <iostream>
#include "close_context.h"
#include "connect_context.h"
#include "send_context.h"
#include "websocket_async_work.h"
#include "websocket_exec.h"
#include "websocket_module.h"
class WebSocketTest : public testing::Test {
public:
static void SetUpTestCase() {}
static void TearDownTestCase() {}
virtual void SetUp() {}
virtual void TearDown() {}
};
namespace {
using namespace testing::ext;
using namespace OHOS::NetStack::Websocket;
HWTEST_F(WebSocketTest, WebSocketTest001, TestSize.Level1)
{
napi_env env = nullptr;
OHOS::NetStack::EventManager eventManager;
ConnectContext context(env, &eventManager);
bool ret = WebSocketExec::ExecConnect(&context);
EXPECT_EQ(ret, false);
}
HWTEST_F(WebSocketTest, WebSocketTest002, TestSize.Level1)
{
napi_env env = nullptr;
OHOS::NetStack::EventManager eventManager;
SendContext context(env, &eventManager);
bool ret = WebSocketExec::ExecSend(&context);
EXPECT_EQ(ret, false);
}
HWTEST_F(WebSocketTest, WebSocketTest003, TestSize.Level1)
{
napi_env env = nullptr;
OHOS::NetStack::EventManager eventManager;
CloseContext context(env, &eventManager);
bool ret = WebSocketExec::ExecClose(&context);
EXPECT_EQ(ret, false);
}
HWTEST_F(WebSocketTest, WebSocketTest004, TestSize.Level1)
{
bool ret = WebSocketExec::ExecConnect(nullptr);
EXPECT_EQ(ret, false);
}
HWTEST_F(WebSocketTest, WebSocketTest005, TestSize.Level1)
{
napi_env env = nullptr;
OHOS::NetStack::EventManager eventManager;
ConnectContext context(env, &eventManager);
context.caPath_ = "/etc/ssl/certs/test_ca.crt";
bool ret = WebSocketExec::ExecConnect(&context);
EXPECT_EQ(ret, false);
}
HWTEST_F(WebSocketTest, WebSocketTest006, TestSize.Level1)
{
napi_env env = nullptr;
OHOS::NetStack::EventManager eventManager;
ConnectContext context(env, &eventManager);
context.caPath_ = "";
bool ret = WebSocketExec::ExecConnect(&context);
EXPECT_EQ(ret, false);
}
HWTEST_F(WebSocketTest, WebSocketTest007, TestSize.Level1)
{
napi_env env = nullptr;
OHOS::NetStack::EventManager eventManager;
ConnectContext context(env, &eventManager);
context.url = "ws://123.123.123.123:9000";
std::string myProtocol = "my-protocol";
context.SetProtocol(myProtocol);
std::string getMyProtocol = context.GetProtocol();
bool ret = WebSocketExec::ExecConnect(&context);
EXPECT_EQ(getMyProtocol, "my-protocol");
EXPECT_EQ(ret, false);
}
HWTEST_F(WebSocketTest, WebSocketTest008, TestSize.Level1)
{
napi_env env = nullptr;
OHOS::NetStack::EventManager eventManager;
ConnectContext context(env, &eventManager);
context.url = "ws://123.123.123.123:9000";
context.SetWebsocketProxyType(WebsocketProxyType::USE_SPECIFIED);
std::string host = "192.168.147.60";
int32_t port = 8888;
std::string exclusions = "www.httpbin.org";
context.SetSpecifiedWebsocketProxy(host, port, exclusions);
std::string getHost;
uint32_t getPort;
std::string getExclusions;
context.GetSpecifiedWebsocketProxy(getHost, getPort, getExclusions);
bool ret = WebSocketExec::ExecConnect(&context);
EXPECT_EQ(getHost, "192.168.147.60");
EXPECT_EQ(getPort, 8888);
EXPECT_EQ(getExclusions, "www.httpbin.org");
EXPECT_EQ(ret, false);
}
} // namespace