1
0

Mock siad

This commit is contained in:
Scott E. Graves
2017-02-22 08:47:15 -06:00
parent 4fcfb8517e
commit c109b75a8f
2 changed files with 45 additions and 17 deletions

View File

@@ -5,12 +5,9 @@ class CMockClient
{
public:
CMockClient(SOCKET socket, std::function<void(CMockClient* client)> removedCallback) :
_socket(socket)
_socket(socket),
_stopEvent(::CreateEvent(nullptr, FALSE, FALSE, nullptr))
{
_clientThread.reset(new std::thread([this]()
{
Run();
}));
}
public:
@@ -31,8 +28,8 @@ private:
void Run()
{
bool connected = true;
bool stopRequested;
while (connected && !((stopRequested = (::WaitForSingleObject(_stopEvent, 1) == WAIT_TIMEOUT))))
bool active;
while (connected && (::WaitForSingleObject(_stopEvent, 1) == WAIT_TIMEOUT))
{
timeval tv = { 1, 0 };
FD_SET readFds, exceptFds;
@@ -45,7 +42,28 @@ private:
{
if (FD_ISSET(_socket, &readFds))
{
std::vector<char> buffer(1024);
int recvd;
std::string response;
{
std::string tmp;
while (!response.length() && ((recvd = ::recv(_socket, &buffer[0], buffer.size(), 0))))
{
tmp += std::string(&buffer[0], recvd);
int position = tmp.find_first_of("\r\n\r\n");
if (position > -1)
{
std::string header = tmp.substr(0, position);
if (header == "GET /daemon/version HTTP/1.1")
{
response = "HTTP/1.1 404 Not Found\r\nConnection: close\r\n\r\n";
}
}
}
::send(_socket, &response[0], response.length(), 0);
}
}
else if (FD_ISSET(_socket, &exceptFds))
{
@@ -59,6 +77,14 @@ private:
}
public:
void Start()
{
_clientThread.reset(new std::thread([this]()
{
Run();
}));
}
void Close()
{
::SetEvent(_stopEvent);
@@ -72,7 +98,8 @@ CMockSiad::CMockSiad(const SiaHostConfig& hostConfig) :
_stopEvent(::CreateEvent(nullptr, FALSE, FALSE, nullptr))
{
const WORD ver = MAKEWORD(2, 2);
::WSAStartup(ver, nullptr);
WSADATA d = { 0 };
::WSAStartup(ver, &d);
}
CMockSiad::~CMockSiad()
@@ -88,7 +115,7 @@ void CMockSiad::Start(const SiadTestType& testType)
{
_serverThread.reset(new std::thread([this]()
{
bool stopRequested = false;
bool active;
std::mutex clientMutex;
std::vector<CMockClient*> connected;
std::vector<CMockClient*> disconnected;
@@ -109,10 +136,10 @@ void CMockSiad::Start(const SiadTestType& testType)
struct sockaddr_in sockAddr = { 0 };
sockAddr.sin_family = AF_INET;
sockAddr.sin_addr.s_addr = inet_addr("localhost");
sockAddr.sin_port = htons(5150);
bool listening = ((listenSocket != INVALID_SOCKET) && (::bind(listenSocket, reinterpret_cast<const struct sockaddr *>(&sockAddr), sizeof(sockAddr)) != SOCKET_ERROR));
while (listening && !(stopRequested = (::WaitForSingleObject(_stopEvent, 1) == WAIT_TIMEOUT)))
sockAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
sockAddr.sin_port = htons(19980);
bool listening = ((listenSocket != INVALID_SOCKET) && (::bind(listenSocket, reinterpret_cast<const struct sockaddr *>(&sockAddr), sizeof(sockAddr)) != SOCKET_ERROR)) && (::listen(listenSocket, 1) != SOCKET_ERROR);
while (listening && ((active = (::WaitForSingleObject(_stopEvent, 1) == WAIT_TIMEOUT))))
{
ClearDisconnected();
@@ -135,6 +162,7 @@ void CMockSiad::Start(const SiadTestType& testType)
connected.erase(std::remove_if(connected.begin(), connected.end(), [&](CMockClient* client) -> bool { return client == removedClient; }), connected.end());
disconnected.push_back(removedClient);
});
client->Start();
}
else if (FD_ISSET(listenSocket, &exceptFds))
{
@@ -160,7 +188,7 @@ void CMockSiad::Start(const SiadTestType& testType)
client->Close();
}
}
} while (!stopRequested);
} while (active);
ClearDisconnected();
}));

View File

@@ -30,10 +30,10 @@ namespace UnitTests
TEST_METHOD(TestMethod1)
{
siad->Start(SiadTestType::UploadFile);
Sleep(2000);
CSiaDriveConfig driveConfig;
CSiaCurl siaCurl(hostConfig);
siaCurl.GetServerVersion();
CUploadManager uploadManager(siaCurl, &driveConfig);
}
};