HelloAurora/Tests/Public/Net. TLS Bing/Main.cpp

190 lines
4.7 KiB
C++
Raw Normal View History

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: Main.cpp
Date: 2022-2-18
Author: Reece
***/
#include <AuroraRuntime.hpp>
#include <Aurora/IO/IOExperimental.hpp>
#include <Aurora/IO/Net/NetExperimental.hpp>
#include <Aurora/IO/TLS/TLS.hpp>
#include <gtest/gtest.h>
static const auto kHTTP11Address = "204.79.197.200"; // bing.com
struct DriverStat
{
AuUInt uPreestablish {};
AuUInt uEstablish {};
AuUInt uStreamUpdate {};
AuUInt uError {};
AuUInt uEnd {};
AuUInt uFinalize {};
};
struct TestSocketDriver : AuNet::ISocketDriver
{
AuSPtr<DriverStat> pStats;
AuSPtr<AuNet::ISocketDriver> pProxy;
bool OnPreestablish(const AuSPtr<AuNet::ISocket> &pInforming) override;
void OnEstablish() override;
void OnStreamUpdated() override;
void OnFatalErrorReported(const AuNet::NetError &error) override;
void OnEnd() override;
void OnFinalize() override;
};
bool TestSocketDriver::OnPreestablish(const AuSPtr<AuNet::ISocket> &pInforming)
{
this->pStats->uPreestablish++;
if (this->pProxy)
{
this->pProxy->OnPreestablish(pInforming);
}
return true;
}
void TestSocketDriver::OnEstablish()
{
this->pStats->uEstablish++;
if (this->pProxy)
{
this->pProxy->OnEstablish();
}
}
void TestSocketDriver::OnStreamUpdated()
{
this->pStats->uStreamUpdate++;
if (this->pProxy)
{
this->pProxy->OnStreamUpdated();
}
}
void TestSocketDriver::OnFatalErrorReported(const AuNet::NetError &error)
{
this->pStats->uError++;
if (this->pProxy)
{
this->pProxy->OnFatalErrorReported(error);
}
}
void TestSocketDriver::OnEnd()
{
this->pStats->uEnd++;
if (this->pProxy)
{
this->pProxy->OnEnd();
}
}
void TestSocketDriver::OnFinalize()
{
this->pStats->uFinalize++;
if (this->pProxy)
{
this->pProxy->OnFinalize();
}
}
static AuSPtr<TestSocketDriver> NewTestSocketDriver(const AuSPtr<DriverStat> &pStats)
{
auto pDriver = AuMakeShared<TestSocketDriver>();
SysAssert(pDriver);
pDriver->pStats = pStats;
return pDriver;
}
TEST(Net, TLSGoogle)
{
auto pIoProcessor = AuIO::NewIOProcessorNoQueue(false);
ASSERT_TRUE(bool(pIoProcessor));
auto pNetProcessor = AuNet::NewNetworkInterface();
ASSERT_TRUE(bool(pNetProcessor));
auto pNetWorker = pNetProcessor->GetWorkersService()->Attach(pIoProcessor);
ASSERT_TRUE(bool(pNetWorker));
auto pStats = AuMakeShared<DriverStat>();
ASSERT_TRUE(bool(pStats));
auto pDriver = NewTestSocketDriver(pStats);
ASSERT_TRUE(bool(pDriver));
auto pRealDriver = AuMakeShared<AuNet::ISocketDriverFunctional>();
static AuSPtr<AuNet::ISocket> gDriverSocket;
pRealDriver->OnPreestablishFunctional = [&](const AuSPtr<AuNet::ISocket> &socket) -> bool
{
#define ENABLE_TLS
#if defined(ENABLE_TLS)
Aurora::IO::TLS::TLSMeta tlsMeta;
//tlsMeta.sSNIServerName = "google.com";
auto pContext = Aurora::IO::TLS::NewTLSContextEx(socket->ToChannel()->NewProtocolSendStack(),
socket->ToChannel()->NewProtocolRecvStack(),
tlsMeta);
pContext->Attach(socket);
pContext->StartHandshake();
#endif
gDriverSocket = socket;
return true;
};
pRealDriver->OnEstablishFunctional = [&]()
{
auto pChannel = gDriverSocket->ToChannel();
pChannel->AsStreamWriter()->Write(AuMemoryViewRead("GET / HTTP/1.1\r\nConnection: close\r\nHost: www.bing.com\r\n\r\n", 57));
};
pRealDriver->OnFatalErrorReportedFunctional = [&](const AuNet::NetError &error)
{
AuLogDbg("[3] On error: {}", NetErrorToExtendedString(error));
};
pRealDriver->OnStreamUpdatedFunctional = [&]()
{
auto pChannel = gDriverSocket->ToChannel();
auto pReader = pChannel->AsReadableByteBuffer();
AuString message;
message.resize(pReader->RemainingBytes());
pReader->Read(message.data(), message.size());
AuLogDbg("Stream updated: {}", message);
};
pRealDriver->OnFinalizeFunctional = [&]()
{
AuLogDbg("[3] On finalize");
};
pDriver->pProxy = pRealDriver;
AuNet::NetSocketConnect nsConnect;
nsConnect.pDriver = pDriver;
nsConnect.endpoint = AuNet::NetEndpoint(AuNet::ETransportProtocol::eProtocolTCP, kHTTP11Address, 443);
auto pSocket = pNetProcessor->GetSocketService()->Connect(nsConnect);
ASSERT_TRUE(bool(pSocket));
while (pIoProcessor->HasItems())
{
pIoProcessor->RunTick();
}
}
void RunTests()
{
Aurora::RuntimeStartInfo info;
2023-07-28 11:58:08 +00:00
info.console.fio.bEnableLogging = false;
Aurora::RuntimeStart(info);
}