<commit_before>/*
 * Copyright (c) 2005, <NAME> and IFS Institute for Software at HSR Rapperswil, Switzerland
 * All rights reserved.
 *
 * This library/application is free software; you can redistribute and/or modify it under the terms of
 * the license that is included with this library/application in the file license.txt.
 */

//--- interface include --------------------------------------------------------
#include "ConnectorDAImplTest.h"

//--- test modules used --------------------------------------------------------
#include "TestSuite.h"

//--- module under test --------------------------------------------------------
#include "ConnectorDAImpl.h"

//--- standard modules used ----------------------------------------------------
#include "SocketStream.h"
#include "Dbg.h"
#include "SysLog.h"
#include "System.h"
#include "DataAccess.h"

//--- c-modules used -----------------------------------------------------------
#if defined(WIN32)
#include <io.h>				// for SO_ERROR
#else
#include <sys/socket.h>		// for SO_ERROR
#endif

class AcceptorThread : public Thread
{
public:
	AcceptorThread(Acceptor *acceptor) : Thread("AcceptorThread"), fAcceptor(acceptor)
	{ }
	~AcceptorThread();
	void Run();

protected:
	bool DoStartRequestedHook(ROAnything args);
	void DoTerminationRequestHook(ROAnything);

private:
	Acceptor *fAcceptor;
};

class EchoMsgCallBack : public AcceptorCallBack
{
public:

	//!default constructor is empty since class provides only protocol no implementation
	EchoMsgCallBack() : fReplyMessage("Accepted") {};

	//!default destructor is empty since class provides only protocol no implementation
	virtual ~EchoMsgCallBack() {};

	//!call back functionality provides the socket object
	//! \param socket the Socket object resulting from the accepted connection
	virtual void CallBack(Socket *socket);

	//!I return the last message I read
	virtual Anything &GetLastRequest();

	//!Set the message that I shall return on subsequent requests
	void SetReplyMessage(Anything &reply);

protected:
	// no locking as only one thread initiates actions and the second thread
	// only accesses while the first one waits for I/O
	Anything	fLastRequest;
	Anything	fReplyMessage;
};

//---- ConnectorDAImplTest ----------------------------------------------------------------
ConnectorDAImplTest::ConnectorDAImplTest(TString strName)
	: TestCaseType(strName)
	, fCallBack(0)
	, fAcceptorThread(0)
{
	StartTrace(ConnectorDAImplTest.ConnectorDAImplTest);
}

TString ConnectorDAImplTest::getConfigFileName()
{
	return "ConnectorDAImplTestConfig";
}

ConnectorDAImplTest::~ConnectorDAImplTest()
{
	StartTrace(ConnectorDAImplTest.Dtor);
}

void ConnectorDAImplTest::setUp ()
{
	StartTrace(ConnectorDAImplTest.setUp);

	TraceAny(GetTestCaseConfig(), "TestCaseConfig");
	ROAnything roaConfig(GetTestCaseConfig());
	String strHost = roaConfig["Address"].AsString();
	long   lPort = roaConfig["Port"].AsLong();

	fCallBack = new EchoMsgCallBack();
	fAcceptorThread = new AcceptorThread(new Acceptor(strHost, lPort, 2, fCallBack));
	t_assert(fCallBack != 0);
	t_assert(fAcceptorThread != 0);
	if ( (fCallBack) && (fAcceptorThread) ) {
		bool startsuccesful = fAcceptorThread->Start();
		t_assertm(startsuccesful, "probably bind failed; since thread not started");
		bool running = fAcceptorThread->CheckState(Thread::eRunning, 3);
		if (!running) {
			t_assertm(running, "fAcceptorThread has to be running");
			fAcceptorThread->Terminate();
			fAcceptorThread->CheckState(Thread::eTerminated, 3);
		} else {
			return;
		}
	}
	// init failed or already terminated
	delete fAcceptorThread;
	fAcceptorThread = 0;
	t_assertm(fAcceptorThread != 0, "expected AcceptorThread to be ready; init or startup failed");
}

void ConnectorDAImplTest::tearDown ()
{
	StartTrace(ConnectorDAImplTest.tearDown);

	if (fAcceptorThread) {
		fAcceptorThread->Terminate();
		t_assert(fAcceptorThread->CheckState(Thread::eTerminated, 3));
		delete fAcceptorThread;
		fAcceptorThread = 0;
	}
}

bool ConnectorDAImplTest::IsSocketValid(int socketFd)
{
	StartTrace(ConnectorDAImplTest.IsSocketValid);
	int error = 0;
	bool bSuccess = Socket::GetSockOptInt(socketFd, SO_ERROR, error);
	if ( !bSuccess ) {
		SYSWARNING("socketFd " << (long)socketFd << " not valid anymore, error:" << (long)error);
		return false;
	}
	return true;
}

void ConnectorDAImplTest::SendReceiveOnceTest()
{
	StartTrace(ConnectorDAImplTest.SendReceiveOnceTest);
	TraceAny(GetTestCaseConfig(), "TestCaseConfig");
	Anything anyEnv = GetTestCaseConfig().DeepClone();
	Context ctx(anyEnv);
	DataAccess aDA("SendReceiveOnce");
	t_assert(aDA.StdExec(ctx));
	Anything anyExpected;
	StringStream stream(GetTestCaseConfig()["Input"].AsString());
	anyExpected.Import(stream);
	assertAnyEqual(anyExpected, fCallBack->GetLastRequest());
	TraceAny(ctx.GetTmpStore(), "TmpStore");
}

void ConnectorDAImplTest::RecreateSocketTest()
{
	StartTrace(ConnectorDAImplTest.RecreateSocketTest);
	TraceAny(GetTestCaseConfig(), "TestCaseConfig");
	Anything anyEnv = GetTestCaseConfig().DeepClone();
	Context ctx(anyEnv);
	{
		DataAccess aDA("RecreateSocket");
		ctx.GetTmpStore()["Input"] = "{ \"Kurt hat recht\" }";
		t_assert(aDA.StdExec(ctx));
		Anything anyExpected;
		StringStream stream(ctx.GetTmpStore()["Input"].AsString());
		anyExpected.Import(stream);
		assertAnyEqual(anyExpected, fCallBack->GetLastRequest());
	}
	// test if socketFd is still valid
	int socketFd = ctx.GetTmpStore()["SocketParams"]["SocketFd"].AsLong(0L);
	if ( t_assert(socketFd != 0L) ) {
		t_assertm(IsSocketValid(socketFd), "expected socket to be valid");
		{
			DataAccess aDA("SendReceiveOnce");
			ctx.GetTmpStore()["Input"] = "{ \"<NAME>\" }";
			t_assert(aDA.StdExec(ctx));
			Anything anyExpected;
			StringStream stream(ctx.GetTmpStore()["Input"].AsString());
			anyExpected.Import(stream);
			assertAnyEqual(anyExpected, fCallBack->GetLastRequest());
		}
	}
	// test if socketFd is not valid any longer
	t_assertm(!IsSocketValid(socketFd), "expected socket to be invalid");
}

Anything &EchoMsgCallBack::GetLastRequest()
{
	StartTrace(EchoMsgCallBack.GetLastRequest);
	return fLastRequest;
}

void EchoMsgCallBack::SetReplyMessage( Anything &reply )
{
	StartTrace(EchoMsgCallBack.SetReplyMessage);
	fReplyMessage = reply;
}

void EchoMsgCallBack::CallBack(Socket *pSocket)
{
	StartTrace(EchoMsgCallBack.CallBack);
	if (pSocket != 0) {	// might be 0 if we run out of memory, not likely<commit_msg>[IO]StringStream interface adjustments<commit_after>/*
 * Copyright (c) 2005, <NAME> and IFS Institute for Software at HSR Rapperswil, Switzerland
 * All rights reserved.
 *
 * This library/application is free software; you can redistribute and/or modify it under the terms of
 * the license that is included with this library/application in the file license.txt.
 */

//--- interface include --------------------------------------------------------
#include "ConnectorDAImplTest.h"

//--- test modules used --------------------------------------------------------
#include "TestSuite.h"

//--- module under test --------------------------------------------------------
#include "ConnectorDAImpl.h"

//--- standard modules used ----------------------------------------------------
#include "SocketStream.h"
#include "Dbg.h"
#include "SysLog.h"
#include "System.h"
#include "DataAccess.h"

//--- c-modules used -----------------------------------------------------------
#if defined(WIN32)
#include <io.h>				// for SO_ERROR
#else
#include <sys/socket.h>		// for SO_ERROR
#endif

class AcceptorThread : public Thread
{
public:
	AcceptorThread(Acceptor *acceptor) : Thread("AcceptorThread"), fAcceptor(acceptor)
	{ }
	~AcceptorThread();
	void Run();

protected:
	bool DoStartRequestedHook(ROAnything args);
	void DoTerminationRequestHook(ROAnything);

private:
	Acceptor *fAcceptor;
};

class EchoMsgCallBack : public AcceptorCallBack
{
public:

	//!default constructor is empty since class provides only protocol no implementation
	EchoMsgCallBack() : fReplyMessage("Accepted") {};

	//!default destructor is empty since class provides only protocol no implementation
	virtual ~EchoMsgCallBack() {};

	//!call back functionality provides the socket object
	//! \param socket the Socket object resulting from the accepted connection
	virtual void CallBack(Socket *socket);

	//!I return the last message I read
	virtual Anything &GetLastRequest();

	//!Set the message that I shall return on subsequent requests
	void SetReplyMessage(Anything &reply);

protected:
	// no locking as only one thread initiates actions and the second thread
	// only accesses while the first one waits for I/O
	Anything	fLastRequest;
	Anything	fReplyMessage;
};

//---- ConnectorDAImplTest ----------------------------------------------------------------
ConnectorDAImplTest::ConnectorDAImplTest(TString strName)
	: TestCaseType(strName)
	, fCallBack(0)
	, fAcceptorThread(0)
{
	StartTrace(ConnectorDAImplTest.ConnectorDAImplTest);
}

TString ConnectorDAImplTest::getConfigFileName()
{
	return "ConnectorDAImplTestConfig";
}

ConnectorDAImplTest::~ConnectorDAImplTest()
{
	StartTrace(ConnectorDAImplTest.Dtor);
}

void ConnectorDAImplTest::setUp ()
{
	StartTrace(ConnectorDAImplTest.setUp);

	TraceAny(GetTestCaseConfig(), "TestCaseConfig");
	ROAnything roaConfig(GetTestCaseConfig());
	String strHost = roaConfig["Address"].AsString();
	long   lPort = roaConfig["Port"].AsLong();

	fCallBack = new EchoMsgCallBack();
	fAcceptorThread = new AcceptorThread(new Acceptor(strHost, lPort, 2, fCallBack));
	t_assert(fCallBack != 0);
	t_assert(fAcceptorThread != 0);
	if ( (fCallBack) && (fAcceptorThread) ) {
		bool startsuccesful = fAcceptorThread->Start();
		t_assertm(startsuccesful, "probably bind failed; since thread not started");
		bool running = fAcceptorThread->CheckState(Thread::eRunning, 3);
		if (!running) {
			t_assertm(running, "fAcceptorThread has to be running");
			fAcceptorThread->Terminate();
			fAcceptorThread->CheckState(Thread::eTerminated, 3);
		} else {
			return;
		}
	}
	// init failed or already terminated
	delete fAcceptorThread;
	fAcceptorThread = 0;
	t_assertm(fAcceptorThread != 0, "expected AcceptorThread to be ready; init or startup failed");
}

void ConnectorDAImplTest::tearDown ()
{
	StartTrace(ConnectorDAImplTest.tearDown);

	if (fAcceptorThread) {
		fAcceptorThread->Terminate();
		t_assert(fAcceptorThread->CheckState(Thread::eTerminated, 3));
		delete fAcceptorThread;
		fAcceptorThread = 0;
	}
}

bool ConnectorDAImplTest::IsSocketValid(int socketFd)
{
	StartTrace(ConnectorDAImplTest.IsSocketValid);
	int error = 0;
	bool bSuccess = Socket::GetSockOptInt(socketFd, SO_ERROR, error);
	if ( !bSuccess ) {
		SYSWARNING("socketFd " << (long)socketFd << " not valid anymore, error:" << (long)error);
		return false;
	}
	return true;
}

void ConnectorDAImplTest::SendReceiveOnceTest()
{
	StartTrace(ConnectorDAImplTest.SendReceiveOnceTest);
	TraceAny(GetTestCaseConfig(), "TestCaseConfig");
	Anything anyEnv = GetTestCaseConfig().DeepClone();
	Context ctx(anyEnv);
	DataAccess aDA("SendReceiveOnce");
	t_assert(aDA.StdExec(ctx));
	Anything anyExpected;
	String strIn = GetTestCaseConfig()["Input"].AsString();
	IStringStream stream(strIn);
	anyExpected.Import(stream);
	assertAnyEqual(anyExpected, fCallBack->GetLastRequest());
	TraceAny(ctx.GetTmpStore(), "TmpStore");
}

void ConnectorDAImplTest::RecreateSocketTest()
{
	StartTrace(ConnectorDAImplTest.RecreateSocketTest);
	TraceAny(GetTestCaseConfig(), "TestCaseConfig");
	Anything anyEnv = GetTestCaseConfig().DeepClone();
	Context ctx(anyEnv);
	{
		DataAccess aDA("RecreateSocket");
		ctx.GetTmpStore()["Input"] = "{ \"Kurt hat recht\" }";
		t_assert(aDA.StdExec(ctx));
		Anything anyExpected;
		String strIn = ctx.GetTmpStore()["Input"].AsString();
		IStringStream stream(strIn);
		anyExpected.Import(stream);
		assertAnyEqual(anyExpected, fCallBack->GetLastRequest());
	}
	// test if socketFd is still valid
	int socketFd = ctx.GetTmpStore()["SocketParams"]["SocketFd"].AsLong(0L);
	if ( t_assert(socketFd != 0L) ) {
		t_assertm(IsSocketValid(socketFd), "expected socket to be valid");
		{
			DataAccess aDA("SendReceiveOnce");
			ctx.GetTmpStore()["Input"] = "{ \"<NAME>\" }";
			t_assert(aDA.StdExec(ctx));
			Anything anyExpected;
			String strIn(ctx.GetTmpStore()["Input"].AsString());
			IStringStream stream(strIn);
			anyExpected.Import(stream);
			assertAnyEqual(anyExpected, fCallBack->GetLastRequest());
		}
	}
	// test if socketFd is not valid any longer
	t_assertm(!IsSocketValid(socketFd), "expected socket to be invalid");
}

Anything &EchoMsgCallBack::GetLastRequest()
{
	StartTrace(EchoMsgCallBack.GetLastRequest);
	return fLastRequest;
}

void EchoMsgCallBack::SetReplyMessage( Anything &reply )
{
	StartTrace(EchoMsgCallBack.SetReplyMessage);
	fReplyMessage = reply;
}

void EchoMsgCallBack::CallBack(Socket *pSocket)
{
	StartTrace(EchoMsgCallBack.CallBack);
	if (pSocket != 0) {	// might be 0 if we run out of memory, not likely
{'fraction_non_alphanumeric': 0.17098045275438462, 'fraction_numerical': 0.0033995209765896625, 'mean_word_length': 4.179671868747499, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3398581', 'n_tokens_mistral': 4111, 'n_tokens_neox': 3935, 'n_words': 919}