/*******************************************************************

Copyright (c) 2004 Open Mobile Alliance, Ltd.  All Rights Reserved.

This software is made available under the usage and distribution terms 
covered by the Common Public License 1.0 and by using this software you
are bound by the terms and conditions of the same.

EXCEPT AS EXPRESSLY SET FORTH IN THE COMMON PUBLIC LICENSE,
THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, 
ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, 
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
   
Each recipient is solely responsible for determining the 
appropriateness of using and distributing this program and assumes 
all risks associated with its exercise of rights under the Common 
Public License, including but not limited to the risks and 
costs of program errors, compliance with applicable laws, 
damage to or loss of data, programs or equipment, and 
unavailability or interruption of operations.

********************************************************************/

#include "SctsDevManTestServer.h"
#include "SctsDevManDevice.h"
#include "SctsDevManSession.h"
#ifdef __SCTS_USING_SCTS__

/******************************************************************
SctsDMTestCase 1xxS Definition
******************************************************************/

void SctsDMTestCase101S::StateInitiatePreBuild()
{
	if (!testGroup->session->password)
	{
		testGroup->session->IsSuccessful(false);
		SctsErrorLog::OpenNode("Test Case 101S.","Pre-conditions");
		SctsErrorLog::AppendError("DMAccount in use does not have a password configured");
		SctsErrorLog::CloseNode();		
	}
	else
	{
		(*testGroup->session->password)++;
		if (!testGroup->session->outAuth) testGroup->session->outAuth=new SctsAuthBasic();
		state=TEST_FAILED;
	}
	doneSIPreB=true;
}

void SctsDMTestCase101S::StateInitiatePostBuild()
{
	if (state!=TEST_FAILED) return;
	(*testGroup->session->password)--;
	if (testGroup->session->outAuth)
	{
		delete testGroup->session->outAuth;
		testGroup->session->outAuth=0;		
		testGroup->device->SetAuthType(false,AUTH_NONE);
	}
	doneSIPostB=true;	
}

void SctsDMTestCase101S::StateInitiatePostProcess()
{
	if (state!=TEST_FAILED) return;
	state=TEST_PASSED;
	if (testGroup->session->recvSyncHdrStatus!=401)
	{
		char errorMsg[500];
		state=TEST_FAILED;
		SctsErrorLog::OpenNode("Test Case 101S failed.",0);
		sprintf(errorMsg,"Expecting a status of 401 on the SyncHdr, received a status of %d. Sync protocol (3.3) : the authentication of the server layer MUST be supported by the device complying with this specification. Representation protocol (5.4.1) : Invalid credentials. The requested command failed because the requestor MUST provide proper authentication.",testGroup->session->recvSyncHdrStatus);
		SctsErrorLog::AppendError(errorMsg);
		SctsErrorLog::CloseNode();
		testGroup->session->IsSuccessful(false);
	}
	doneSIPostP=true;
}

/******************************************************************
SctsDMTestCase 2xxS Definition
*****************************************************************/

void SctsDMTestCase201S::StateInitiatePreBuild()
{
	if (testGroup->session->outAuth)
	{
		delete testGroup->session->outAuth;
		testGroup->session->outAuth=0;
		testGroup->device->SetAuthType(false,AUTH_NONE);
	}
	doneSIPreB=true;
}

void SctsDMTestCase201S::StateInitiatePostProcess()
{
	if (testGroup->session->recvSyncHdrStatus==407)
		state=TEST_PASSED;
	else
	{
		char errorMsg[500];
		state=TEST_FAILED;
		testGroup->session->IsSuccessful(false);
		SctsErrorLog::OpenNode("Test Case 201S failed.",0);
		sprintf(errorMsg,"Expecting a status of 407 on the SyncHdr, received a status of %d. Sync protocol (3.3) : the authentication of the server layer MUST be supported by the device complying with this specification. Representation protocol (5.4.1) : Missing credentials. This response code is similar to 401 except that the response code indicates that the originator MUST first authenticate with the recipient.",testGroup->session->recvSyncHdrStatus);
		SctsErrorLog::AppendError(errorMsg);
		SctsErrorLog::CloseNode();		
	}
	doneSIPostP=true;
}

void SctsDMTestCase202S::StateInitiatePostProcess()
{
	if (testGroup->session->currentMsgID!=3) return;
	if (testGroup->session->recvSyncHdrStatus==200 || testGroup->session->recvSyncHdrStatus==212)
		state=TEST_PASSED;
	else
	{
		char errorMsg[500];
		state=TEST_FAILED;
		testGroup->session->IsSuccessful(false);
		SctsErrorLog::OpenNode("Test Case 202S failed.",0);
		sprintf(errorMsg,"Expecting a status of 200/212 on the SyncHdr, received a status of %d.",testGroup->session->recvSyncHdrStatus);
		SctsErrorLog::AppendError(errorMsg);
		SctsErrorLog::CloseNode();		
	}
	doneSIPostP=true;
}

void SctsDMTestCase203S::StateTerminate()
{
	if (((SctsDMClientSession *)testGroup->session)->sentSessionInitiationAlert==true)
		state=TEST_PASSED;
	else
	{
		state=TEST_FAILED;
		testGroup->session->IsSuccessful(false);
		SctsErrorLog::OpenNode("Test Case 203S failed.","Client Initiated Alert.");
		SctsErrorLog::AppendError("Unable to handle status for the Alert.");
		SctsErrorLog::CloseNode();
	}
}

void SctsDMTestCase204S::StateTerminate()
{
	if (((SctsDMClientSession *)testGroup->session)->sentDevInfo==true)
		state=TEST_PASSED;
	else
	{
		state=TEST_FAILED;
		testGroup->session->IsSuccessful(false);
		SctsErrorLog::OpenNode("Test Case 204S failed.","Device Information.");
		SctsErrorLog::AppendError("Unable to handle status for the Replace of Device Information.");
		SctsErrorLog::CloseNode();
	}	
}

/******************************************************************
SctsDMTestCase 3xxS Definition
*****************************************************************/

void SctsDMTestCase301S::StateTerminate()
{
	char errorMsg[500];
	SctsStatusRef *statusRef;
	std::list <SctsStatusRef *> getStatusList=((SctsDMClientSession *)testGroup->session)->sentStatusRefs[SCTS_GET];
	std::list <SctsStatusRef *>::iterator refListIt;

	state=TEST_PASSED;
	SctsErrorLog::OpenNode("Test Case 301S failed.","Get.");
	if (getStatusList.empty())
	{
		state=TEST_FAILED;
		SctsErrorLog::AppendError("Did not receive Get command from the server.");	
	}
	for (refListIt=getStatusList.begin();refListIt!=getStatusList.end();++refListIt)
	{
		statusRef=(*refListIt);
		if (statusRef->status!=200)
		{
			state=TEST_FAILED;
			sprintf(errorMsg,"Get sent by the Server resulted in status of %d. (Message ID : %d, Command ID : %s)",statusRef->status,statusRef->msgID,statusRef->cmdID);
			SctsErrorLog::AppendError(errorMsg);
		}
	}
	if (state==TEST_FAILED)
		testGroup->session->IsSuccessful(false);
	SctsErrorLog::CloseNode();
}
/******************************************************************
SctsDMTestCase 4xxS Definition
*****************************************************************/

void SctsDMTestCase401S::StateInitiatePreBuild()
{
	SctsDMNode *tempNode;
	if ((tempNode=((SctsDMClientDevice *)testGroup->device)->FindDMNode("./SCTSValue")))
		delete tempNode;
	doneSIPreB=true;
}
void SctsDMTestCase401S::StateTerminate()
{
	SctsDMNode *tempNode;
	if ((tempNode=((SctsDMClientDevice *)testGroup->device)->FindDMNode("./SCTSValue")) && tempNode->size && tempNode->data)
		state=TEST_PASSED;
	else
	{
		state=TEST_FAILED;
		testGroup->session->IsSuccessful(false);
		SctsErrorLog::OpenNode("Test Case 401S failed.","Add");		
		SctsErrorLog::AppendError("Did not receive valid Add on node ./SCTSValue.");
		SctsErrorLog::CloseNode();
	}
}
/******************************************************************
SctsDMTestCase 5xxS Definition
*****************************************************************/

void SctsDMTestCase501S::StateInitiatePreBuild()
{
	SctsDMNode *tempNode;
	char tempBuffer[100];
	if ((tempNode=((SctsDMClientDevice *)testGroup->device)->FindDMNode("./SCTSValue")))
	{
		if (tempNode->data) sctsFree(tempNode->data);
		tempNode->data=0;
		tempNode->size=0;
	}
	else
	{
		sprintf(tempBuffer,"./SCTSValue");
		((SctsDMClientDevice *)testGroup->device)->AddDMNode(tempBuffer,testGroup->device->serverID,"null",false,0,0,0,&tempNode);
	}
	cmdRef=new SctsDMCommand(SCTS_REPLACE,tempNode);
	doneSIPreB=true;
}
void SctsDMTestCase501S::StateTerminate()
{
	if (cmdRef->nodeRef && cmdRef->nodeRef->data && cmdRef->nodeRef->size && cmdRef->nodeRef->data && cmdRef->nodeRef->format!=DMFORMAT_NULL)
		state=TEST_PASSED;
	else
	{
		state=TEST_FAILED;
		testGroup->session->IsSuccessful(false);
		SctsErrorLog::OpenNode("Test Case 501S failed.","Replace on node ./SCTSValue.");		
		SctsErrorLog::AppendError("Did not receive valid Replace on node ./SCTSValue.");
		SctsErrorLog::CloseNode();
	}
}

/******************************************************************
SctsDMTestCase 6xxS Definition
*****************************************************************/

void SctsDMTestCase601S::StateInitiatePreBuild()
{
	char tempBuffer[100];
	if (!((SctsDMClientDevice *)testGroup->device)->FindDMNode("./SCTSValue"))
	{
		sprintf(tempBuffer,"./SCTSValue");
		((SctsDMClientDevice *)testGroup->device)->AddDMNode(tempBuffer,testGroup->device->serverID,"null",false,0,0,0);
	}
	doneSIPreB=true;
}

void SctsDMTestCase601S::StateTerminate()
{
	if(((SctsDMClientDevice *)testGroup->device)->FindDMNode("./SCTSValue"))
	{
		state=TEST_FAILED;
		testGroup->session->IsSuccessful(false);
		SctsErrorLog::OpenNode("Test Case 601S failed.","Delete on node ./SCTSValue.");		
		SctsErrorLog::AppendError("Did not receive valid Delete on node ./SCTSValue.");
		SctsErrorLog::CloseNode();
	}
	else
		state=TEST_PASSED;
}

/******************************************************************
SctsDMTestCase 7xxS Definition
*****************************************************************/

void SctsDMTestCase701S::StateInitiatePreBuild()
{
	((SctsDMClientSession *)testGroup->session)->sentDevInfo=true;
	doneSIPreB=true;
}

void SctsDMTestCase701S::StateInitiatePostBuild()
{
	if (testGroup->session->outgoingMessage->IsFinal()) 
	{
		testGroup->session->outgoingMessage->IsFinal(false);
		testGroup->session->outgoingMessage->InsertCommand(0);
	}
	((SctsDMClientSession *)testGroup->session)->sentDevInfo=false;
	doneSIPostB=true;
}

void SctsDMTestCase701S::StateTerminate()
{
	if(((SctsDMClientSession *)testGroup->session)->sentDevInfo)
		state=TEST_PASSED;
}

/******************************************************************
SctsDMTestCase 8xxS Definition
*****************************************************************/

void SctsDMTestCase801S::StateTerminate()
{
	char errorMsg[500];
	SctsStatusRef *statusRef;
	std::list <SctsStatusRef *>::iterator refListIt;
	std::list <SctsStatusRef *> sequenceStatusList=((SctsDMClientSession *)testGroup->session)->sentStatusRefs[SCTS_SEQUENCE_START];
	
	state=TEST_PASSED;
	SctsErrorLog::OpenNode("Test Case 801S failed.","Status sent by SCTS on Sequence MUST be 200.");		
	if (sequenceStatusList.empty())
	{
		state=TEST_FAILED;
		SctsErrorLog::AppendError("Did not receive Sequence command from the server.");	
	}
	for (refListIt=sequenceStatusList.begin();refListIt!=sequenceStatusList.end();++refListIt)
	{
		statusRef=(*refListIt);
		if (statusRef->status!=200)
		{
			state=TEST_FAILED;
			sprintf(errorMsg,"Sequence sent by the server resulted in status of %d.(Message ID : %d, Command ID : %s)",statusRef->status,statusRef->msgID,statusRef->cmdID);
			SctsErrorLog::AppendError(errorMsg);
		}
	}
	if (state==TEST_FAILED)
		testGroup->session->IsSuccessful(false);
	SctsErrorLog::CloseNode();
}

/******************************************************************
SctsDMTestCase 9xxS Definition
*****************************************************************/

void SctsDMTestCase901S::StateTerminate()
{
	char errorMsg[500];
	SctsStatusRef *statusRef;
	state=TEST_PASSED;
	std::list <SctsStatusRef *> atomicStatusList=((SctsDMClientSession *)testGroup->session)->sentStatusRefs[SCTS_ATOMIC_START];
	std::list <SctsStatusRef *>::iterator refListIt;
	SctsErrorLog::OpenNode("Test Case 901S failed.","Status sent by SCTS on Atomic MUST be 200.");
	if (atomicStatusList.empty())
	{
		state=TEST_FAILED;
		SctsErrorLog::AppendError("Did not receive Atomic command from the server.");	
	}
	for (refListIt=atomicStatusList.begin();refListIt!=atomicStatusList.end();++refListIt)
	{
		statusRef=(*refListIt);
		if (statusRef->status!=200)
		{
			state=TEST_FAILED;
			sprintf(errorMsg,"Atomic sent by the server resulted in status of %d. (Message ID : %d, Command ID : %s)",statusRef->status,statusRef->msgID,statusRef->cmdID);
			SctsErrorLog::AppendError(errorMsg);
		}
	}
	if (state==TEST_FAILED)
		testGroup->session->IsSuccessful(false);
	SctsErrorLog::CloseNode();
}


/******************************************************************
SctsDMTestCase 10xxS Definition
*****************************************************************/

void SctsDMTestCase1001S::StateInitiatePreBuild()
{
	SctsDMNode *tempNode;
	if ((tempNode=((SctsDMClientDevice *)testGroup->device)->FindDMNode("./SCTSLrgObjText")))
		delete tempNode;
	doneSIPreB=true;
	state=TEST_FAILED;
}

void SctsDMTestCase1001S::StateTerminate()
{
	SctsDMNode *tempNode=((SctsDMClientDevice *)testGroup->device)->FindDMNode("./SCTSLrgObjText");
	if (!tempNode)
	{
		SctsErrorLog::OpenNode("Test Case 1001S failed.","Add");		
		SctsErrorLog::AppendError("Did not receive valid Add on node ./SCTSLrgObjText.");
		SctsErrorLog::CloseNode();
		return;
	}
	if (tempNode->format!=DMFORMAT_CHR)
	{
		SctsErrorLog::OpenNode("Test Case 1001S failed.","Add");		
		SctsErrorLog::AppendError("The Format of ./SCTSLrgObjText node is not 'chr'.");
		SctsErrorLog::CloseNode();
		return;
	}
	if (tempNode->mimeType && sctsStrcmp(tempNode->mimeType,"text/plain"))
	{
		SctsErrorLog::OpenNode("Test Case 1001S failed.","Add");		
		SctsErrorLog::AppendError("The Type of ./SCTSLrgObjText node is not 'text/plain'.");
		SctsErrorLog::CloseNode();
		return;
	}
	if(!tempNode->size || tempNode->size<((SctsDMClientSession *)testGroup->session)->maxMsgSize) 
	{
		char buffer[300];
		SctsErrorLog::OpenNode("Test Case 1001S failed.","Add");		
		sprintf(buffer,"The Size of ./SCTSLrgObjText is %d, which is less the MaxMsgSize of %d.",tempNode->size,((SctsDMClientSession *)testGroup->session)->maxMsgSize);
		SctsErrorLog::AppendError(buffer);
		SctsErrorLog::CloseNode();
		return;
	}	
	state=TEST_PASSED;
}

void SctsDMTestCase1002S::StateTerminate()
{
	char errorMsg[500];
	SctsStatusRef *statusRef;
	state=TEST_PASSED;
	std::list <SctsStatusRef *> getStatusList=((SctsDMClientSession *)testGroup->session)->sentStatusRefs[SCTS_GET];
	std::list <SctsStatusRef *>::iterator refListIt;
	SctsErrorLog::OpenNode("Test Case 1001S failed.","Status sent by SCTS on Get MUST be 200.");
	while (true)
	{
		if (!testGroup->session->IsSuccessful())
		{
			state=TEST_FAILED;
			SctsErrorLog::AppendError("The session did not complete successfully.");	
			break;
		}
		if (getStatusList.empty())
		{
			state=TEST_FAILED;
			SctsErrorLog::AppendError("Did not receive Get command from the server.");	
			break;
		}
		for (refListIt=getStatusList.begin();refListIt!=getStatusList.end();++refListIt)
		{
			statusRef=(*refListIt);
			if (statusRef->data && !sctsStrcmp("./SCTSLrgObjText",(char *)statusRef->data)) break;		
		}
		if (refListIt==getStatusList.end())
		{
			state=TEST_FAILED;
			SctsErrorLog::AppendError("Did not receive Get on ./SCTSLrgObjText node from the server.");	
			break;
		}
		if (statusRef && statusRef->status!=200)
		{
			state=TEST_FAILED;
			sprintf(errorMsg,"Get sent by the server resulted in status of %d. (Message ID : %d, Command ID : %s)",statusRef->status,statusRef->msgID,statusRef->cmdID);
			SctsErrorLog::AppendError(errorMsg);
			break;
		}
		if (!testGroup->session->IsSuccessful())
		{
			state=TEST_FAILED;
			SctsErrorLog::AppendError("The session did not complete successfully.");	
			break;
		}
		break;
	}
	if (state==TEST_FAILED)	testGroup->session->IsSuccessful(false);
	SctsErrorLog::CloseNode();
}

/******************************************************************
SctsDMTestCase 11xxS Definition
*****************************************************************/

void SctsDMTestCase1101S::StateInitiatePreBuild()
{
	SctsDMNode *tempNode;
	if ((tempNode=((SctsDMClientDevice *)testGroup->device)->FindDMNode("./SCTSLrgObjBin")))
		delete tempNode;
	doneSIPreB=true;
	state=TEST_FAILED;
}

void SctsDMTestCase1101S::StateTerminate()
{
	SctsDMNode *tempNode=((SctsDMClientDevice *)testGroup->device)->FindDMNode("./SCTSLrgObjBin");
	if (!tempNode)
	{
		SctsErrorLog::OpenNode("Test Case 1101S failed.","Add");		
		SctsErrorLog::AppendError("Did not receive valid Add on node ./SCTSLrgObjBin.");
		SctsErrorLog::CloseNode();
		return;
	}
	if (tempNode->format!=DMFORMAT_BIN && tempNode->format!=DMFORMAT_B64)
	{
		SctsErrorLog::OpenNode("Test Case 1101S failed.","Add");		
		SctsErrorLog::AppendError("The Format of ./SCTSLrgObjBin node is not 'bin' or 'b64'.");
		SctsErrorLog::CloseNode();
		return;
	}
	if(!tempNode->size || tempNode->size<((SctsDMClientSession *)testGroup->session)->maxMsgSize) 
	{
		char buffer[300];
		SctsErrorLog::OpenNode("Test Case 1101S failed.","Add");		
		sprintf(buffer,"The Size of ./SCTSLrgObjBin is %d, which is less the MaxMsgSize of %d.",tempNode->size,((SctsDMClientSession *)testGroup->session)->maxMsgSize);
		SctsErrorLog::AppendError(buffer);
		SctsErrorLog::CloseNode();
		return;
	}	
	state=TEST_PASSED;
}


void SctsDMTestCase1102S::StateTerminate()
{
	char errorMsg[500];
	SctsStatusRef *statusRef;
	state=TEST_PASSED;
	std::list <SctsStatusRef *> getStatusList=((SctsDMClientSession *)testGroup->session)->sentStatusRefs[SCTS_GET];
	std::list <SctsStatusRef *>::iterator refListIt;
	SctsErrorLog::OpenNode("Test Case 1101S failed.","Status sent by SCTS on Get MUST be 200.");
	while (true)
	{
		if (!testGroup->session->IsSuccessful())
		{
			state=TEST_FAILED;
			SctsErrorLog::AppendError("The session did not complete successfully.");	
			break;
		}
		if (getStatusList.empty())
		{
			state=TEST_FAILED;
			SctsErrorLog::AppendError("Did not receive Get command from the server.");	
			break;
		}
		for (refListIt=getStatusList.begin();refListIt!=getStatusList.end();++refListIt)
		{
			statusRef=(*refListIt);
			if (statusRef->data && !sctsStrcmp("./SCTSLrgObjBin",(char *)statusRef->data)) break;		
		}
		if (refListIt==getStatusList.end())
		{
			state=TEST_FAILED;
			SctsErrorLog::AppendError("Did not receive Get on ./SCTSLrgObjBin node from the server.");	
			break;
		}
		if (statusRef && statusRef->status!=200)
		{
			state=TEST_FAILED;
			sprintf(errorMsg,"Get sent by the server resulted in status of %d. (Message ID : %d, Command ID : %s)",statusRef->status,statusRef->msgID,statusRef->cmdID);
			SctsErrorLog::AppendError(errorMsg);
			break;
		}
		if (!testGroup->session->IsSuccessful())
		{
			state=TEST_FAILED;
			SctsErrorLog::AppendError("The session did not complete successfully.");	
			break;
		}
		break;
	}
	if (state==TEST_FAILED)	testGroup->session->IsSuccessful(false);
	SctsErrorLog::CloseNode();
}

/******************************************************************
SctsDMTestCase 12xxS Definition
*****************************************************************/

void SctsDMTestCase1201S::StateInitiatePostBuild()
{
	SctsAlert *alert=new SctsAlert(testGroup->session->currentCmdID,SctsItemList(),1223);
	testGroup->session->outgoingMessage->InsertCommand(alert);
	++testGroup->session->currentCmdID;
	state=TEST_FAILED;
}

void SctsDMTestCase1201S::StateTerminate()
{
	if (!testGroup->session->IsSuccessful())
	{
		char buffer[300];
		SctsErrorLog::OpenNode("Test Case 1201S failed.","Session Abort Alert");		
		sprintf(buffer,"SCTS did not receive a status of 200 to the session abort alert.");
		SctsErrorLog::AppendError(buffer);
		SctsErrorLog::CloseNode();
		return;
	}	
	state=TEST_PASSED;
}

/******************************************************************
SctsDMTestCase 13xxS Definition
*****************************************************************/

void SctsDMTestCase1301S::StateTerminate()
{
	char errorMsg[500];
	SctsStatusRef *statusRef;
	std::list <SctsStatusRef *>::iterator refListIt;
	std::list <SctsStatusRef *> getStatusList=((SctsDMClientSession *)testGroup->session)->sentStatusRefs[SCTS_GET];
	
	state=TEST_PASSED;
	SctsErrorLog::OpenNode("Test Case 1301S failed.","Get (.?list=Struct)");		
	while (true)
	{
		if (getStatusList.empty())
		{
			state=TEST_FAILED;
			SctsErrorLog::AppendError("Did not receive the Get command from the server.");	
			break;
		}
		for (refListIt=getStatusList.begin();refListIt!=getStatusList.end();++refListIt)
		{
			statusRef=(*refListIt);
			if (statusRef->prop==DMPROP_STRUCT && statusRef->data && !sctsStrcmp((char *)statusRef->data,".?list=Struct")) break;
		}
		if (refListIt==getStatusList.end())
		{
			state=TEST_FAILED;
			sprintf(errorMsg,"Did not receive a Get with '.?list=Struct' from the server.");
			SctsErrorLog::AppendError(errorMsg);
			break;
		}
		if (state!=TEST_FAILED && statusRef->status!=200)
		{
			state=TEST_FAILED;
			sprintf(errorMsg,"Get sent by the server resulted in status of %d.(Message ID : %d, Command ID : %s)",statusRef->status,statusRef->msgID,statusRef->cmdID);
			SctsErrorLog::AppendError(errorMsg);
			break;
		}
		if (!((SctsDMClientSession *)testGroup->session)->pendingResults.empty())
		{
			state=TEST_FAILED;
			sprintf(errorMsg,"SCTS did not finish sending results.");
			SctsErrorLog::AppendError(errorMsg);
			break;
		}
		if (!testGroup->session->IsSuccessful())
		{
			state=TEST_FAILED;
			sprintf(errorMsg,"Session did not complete successfully.");
			SctsErrorLog::AppendError(errorMsg);
			break;
		}
		break;
	}
	if (state==TEST_FAILED)
		testGroup->session->IsSuccessful(false);
	SctsErrorLog::CloseNode();
}


/******************************************************************
SctsDMTestCase 14xxS Definition
*****************************************************************/

void SctsDMTestCase1401S::StateTerminate()
{
	char errorMsg[500];
	SctsStatusRef *statusRef;
	std::list <SctsStatusRef *>::iterator refListIt;
	std::list <SctsStatusRef *> getStatusList=((SctsDMClientSession *)testGroup->session)->sentStatusRefs[SCTS_GET];
	
	state=TEST_PASSED;
	SctsErrorLog::OpenNode("Test Case 1401S failed.","Get (.?list=StructData)");		
	while (true)
	{
		if (getStatusList.empty())
		{
			state=TEST_FAILED;
			SctsErrorLog::AppendError("Did not receive the Get command from the server.");	
			break;
		}
		for (refListIt=getStatusList.begin();refListIt!=getStatusList.end();++refListIt)
		{
			statusRef=(*refListIt);
			if (statusRef->prop==DMPROP_STRUCTDATA && statusRef->data && !sctsStrcmp((char *)statusRef->data,".?list=StructData")) break;
		}
		if (refListIt==getStatusList.end())
		{
			state=TEST_FAILED;
			sprintf(errorMsg,"Did not receive a Get with '.?list=StructData' from the server.");
			SctsErrorLog::AppendError(errorMsg);
			break;
		}

		if (state!=TEST_FAILED && statusRef->status!=200)
		{
			state=TEST_FAILED;
			sprintf(errorMsg,"Get sent by the server resulted in status of %d.(Message ID : %d, Command ID : %s)",statusRef->status,statusRef->msgID,statusRef->cmdID);
			SctsErrorLog::AppendError(errorMsg);
			break;
		}
		if (!((SctsDMClientSession *)testGroup->session)->pendingResults.empty())
		{
			state=TEST_FAILED;
			sprintf(errorMsg,"SCTS did not finish sending results.");
			SctsErrorLog::AppendError(errorMsg);
			break;
		}
		if (!testGroup->session->IsSuccessful())
		{
			state=TEST_FAILED;
			sprintf(errorMsg,"Session did not complete successfully.");
			SctsErrorLog::AppendError(errorMsg);
			break;
		}
		break;
	}
	if (state==TEST_FAILED)
		testGroup->session->IsSuccessful(false);
	SctsErrorLog::CloseNode();
}

/******************************************************************
SctsDMTestCase 15xxS Definition
*****************************************************************/

void SctsDMTestCase1501S::StateTerminate()
{
	char errorMsg[500];
	SctsStatusRef *statusRef;
	std::list <SctsStatusRef *>::iterator refListIt;
	std::list <SctsStatusRef *> alertStatusList=((SctsDMClientSession *)testGroup->session)->sentStatusRefs[SCTS_ALERT];
	
	state=TEST_PASSED;
	SctsErrorLog::OpenNode("Test Case 1501S failed.","UI Display Alert");		
	while (true)
	{
		if (alertStatusList.empty())
		{
			state=TEST_FAILED;
			SctsErrorLog::AppendError("Did not receive the UI Display Alert from the server.");	
			break;
		}
		for (refListIt=alertStatusList.begin();refListIt!=alertStatusList.end();++refListIt)
		{
			statusRef=(*refListIt);
			if (statusRef->data)
			{
				int alertCode=*((int *)statusRef->data);
				if (alertCode==1100) break;
			}
		}
		if (refListIt==alertStatusList.end())
		{
			state=TEST_FAILED;
			sprintf(errorMsg,"Did not receive UI Display alert from the server.");
			SctsErrorLog::AppendError(errorMsg);
			break;
		}

		if (state!=TEST_FAILED && statusRef->status!=200)
		{
			state=TEST_FAILED;
			sprintf(errorMsg,"UI Alert sent by the server resulted in status of %d.(Message ID : %d, Command ID : %s)",statusRef->status,statusRef->msgID,statusRef->cmdID);
			SctsErrorLog::AppendError(errorMsg);
			break;
		}
		if (!testGroup->session->IsSuccessful())
		{
			state=TEST_FAILED;
			sprintf(errorMsg,"Session did not complete successfully.");
			SctsErrorLog::AppendError(errorMsg);
			break;
		}
		break;
	}
	if (state==TEST_FAILED)
		testGroup->session->IsSuccessful(false);
	SctsErrorLog::CloseNode();
}

/******************************************************************
SctsDMTestCase 16xxS Definition
*****************************************************************/

void SctsDMTestCase1601S::StateTerminate()
{
	char errorMsg[500];
	SctsStatusRef *statusRef;
	std::list <SctsStatusRef *>::iterator refListIt;
	std::list <SctsStatusRef *> alertStatusList=((SctsDMClientSession *)testGroup->session)->sentStatusRefs[SCTS_ALERT];
	
	state=TEST_PASSED;
	SctsErrorLog::OpenNode("Test Case 1601S failed.","UI Confirmation Alert");		
	while (true)
	{
		if (alertStatusList.empty())
		{
			state=TEST_FAILED;
			SctsErrorLog::AppendError("Did not receive the UI Confirmation Alert from the server.");	
			break;
		}
		for (refListIt=alertStatusList.begin();refListIt!=alertStatusList.end();++refListIt)
		{
			statusRef=(*refListIt);
			if (statusRef->data)
			{
				int alertCode=*((int *)statusRef->data);
				if (alertCode==1101) break;
			}
		}
		if (refListIt==alertStatusList.end())
		{
			state=TEST_FAILED;
			sprintf(errorMsg,"Did not receive UI Confirmation alert from the server.");
			SctsErrorLog::AppendError(errorMsg);
			break;
		}

		if (state!=TEST_FAILED && statusRef->status!=200 && statusRef->status!=304)
		{
			state=TEST_FAILED;
			sprintf(errorMsg,"UI Alert sent by the server resulted in status of %d.(Message ID : %d, Command ID : %s)",statusRef->status,statusRef->msgID,statusRef->cmdID);
			SctsErrorLog::AppendError(errorMsg);
			break;
		}
		if (!testGroup->session->IsSuccessful())
		{
			state=TEST_FAILED;
			sprintf(errorMsg,"Session did not complete successfully.");
			SctsErrorLog::AppendError(errorMsg);
			break;
		}
		break;
	}
	if (state==TEST_FAILED)
		testGroup->session->IsSuccessful(false);
	SctsErrorLog::CloseNode();
}


/******************************************************************
SctsDMTestCase 17xxS Definition
*****************************************************************/

void SctsDMTestCase1701S::StateTerminate()
{
	char errorMsg[500];
	SctsStatusRef *statusRef;
	std::list <SctsStatusRef *>::iterator refListIt;
	std::list <SctsStatusRef *> alertStatusList=((SctsDMClientSession *)testGroup->session)->sentStatusRefs[SCTS_ALERT];
	
	state=TEST_PASSED;
	SctsErrorLog::OpenNode("Test Case 1701S failed.","UI User Input Alert");		
	while (true)
	{
		if (alertStatusList.empty())
		{
			state=TEST_FAILED;
			SctsErrorLog::AppendError("Did not receive the UI User Input Alert from the server.");	
			break;
		}
		for (refListIt=alertStatusList.begin();refListIt!=alertStatusList.end();++refListIt)
		{
			statusRef=(*refListIt);
			if (statusRef->data)
			{
				int alertCode=*((int *)statusRef->data);
				if (alertCode==1102) break;
			}
		}
		if (refListIt==alertStatusList.end())
		{
			state=TEST_FAILED;
			sprintf(errorMsg,"Did not receive UI User Input alert from the server.");
			SctsErrorLog::AppendError(errorMsg);
			break;
		}

		if (state!=TEST_FAILED && statusRef->status!=200)
		{
			state=TEST_FAILED;
			sprintf(errorMsg,"UI Alert sent by the server resulted in status of %d.(Message ID : %d, Command ID : %s)",statusRef->status,statusRef->msgID,statusRef->cmdID);
			SctsErrorLog::AppendError(errorMsg);
			break;
		}
		if (!testGroup->session->IsSuccessful())
		{
			state=TEST_FAILED;
			sprintf(errorMsg,"Session did not complete successfully.");
			SctsErrorLog::AppendError(errorMsg);
			break;
		}
		break;
	}
	if (state==TEST_FAILED)
		testGroup->session->IsSuccessful(false);
	SctsErrorLog::CloseNode();
}

/******************************************************************
SctsDMTestCase 18xxS Definition
*****************************************************************/

bool SctsDMTestCase1801S::StatePreReq()
{
	extern void sctsTestDeviceExecuteStageTerminate(SctsDMSession *session);
	SctsDMClientDevice::setNotificationDeviceParams((SctsDMClientDevice *)testGroup->device,sctsTestDeviceExecuteStageTerminate);		
	state=TEST_PASSED;
	return true;
}

/******************************************************************
SctsDMTestCase 19xxS Definition
*****************************************************************/

void SctsDMTestCase1901S::StateInitiatePostBuild()
{
	SctsAlert *alert=new SctsAlert(testGroup->session->currentCmdID,SctsItemList(),1226);
	testGroup->session->outgoingMessage->InsertCommand(alert);
	++testGroup->session->currentCmdID;
	state=TEST_FAILED;
}

void SctsDMTestCase1901S::StateTerminate()
{
	if (!testGroup->session->IsSuccessful())
	{
		char buffer[300];
		SctsErrorLog::OpenNode("Test Case 1901S failed.","Support of Generic alert");		
		sprintf(buffer,"SCTS did not receive a status of 200 or 202 to the generic alert.");
		SctsErrorLog::AppendError(buffer);
		SctsErrorLog::CloseNode();
		return;
	}	
	state=TEST_PASSED;
}

/******************************************************************
SctsDMTestCase 20xxS Definition
*****************************************************************/

void SctsDMTestCase2001S::StateTerminate()
{
	char errorMsg[500];
	SctsStatusRef *statusRef;
	std::list <SctsStatusRef *>::iterator refListIt;
	std::list <SctsStatusRef *> execStatusList=((SctsDMClientSession *)testGroup->session)->sentStatusRefs[SCTS_EXEC];
	
	state=TEST_PASSED;
	SctsErrorLog::OpenNode("Test Case 2001S failed.","Support for sending and receiving Correlator");		
	while (true)
	{
		if (execStatusList.empty())
		{
			state=TEST_FAILED;
			SctsErrorLog::AppendError("Did not receive the Exec command from the server.");
			break;
		}
		for (refListIt=execStatusList.begin();refListIt!=execStatusList.end();++refListIt)
		{
			statusRef=(*refListIt);
			if (statusRef->data)
			{
				break;
			}
		}
		if (refListIt==execStatusList.end())
		{
			state=TEST_FAILED;
			sprintf(errorMsg,"Did not receive Exec command with Correlator from the server.");
			SctsErrorLog::AppendError(errorMsg);
			break;
		}

		if (state!=TEST_FAILED && statusRef->status!=200)
		{
			state=TEST_FAILED;
			sprintf(errorMsg,"Exec command sent by the server resulted in status of %d.(Message ID : %d, Command ID : %s)",statusRef->status,statusRef->msgID,statusRef->cmdID);
			SctsErrorLog::AppendError(errorMsg);
			break;
		}
		if (!testGroup->session->IsSuccessful())
		{
			state=TEST_FAILED;
			sprintf(errorMsg,"Session did not complete successfully.");
			SctsErrorLog::AppendError(errorMsg);
			break;
		}
		break;
	}
	if (state==TEST_FAILED)
		testGroup->session->IsSuccessful(false);
	SctsErrorLog::CloseNode();
}

#endif