DRDE/AusRegCliever/server/mdJSON.cpp

265 lines
8.5 KiB
C++
Raw Normal View History

2014-01-28 18:16:48 +00:00
/*
* mdJSON.cpp
*
* Created on: Jan 28, 2014
* Author: jdaugherty
*/
2014-01-29 00:02:24 +00:00
#define MD_JSON
2014-01-28 18:16:48 +00:00
#include "cliever-md.h"
2014-01-28 19:23:02 +00:00
#include <algorithm> // sort
#include <json/json.h>
#include <stdio.h>
2014-02-04 19:58:44 +00:00
#include <execinfo.h>
2014-01-29 18:10:26 +00:00
#include "mdJSON.hpp"
2014-01-28 19:23:02 +00:00
2014-02-04 19:58:44 +00:00
extern bool JSONBatchInProgress,bindDone;
extern const char *parms[MAX_OTE_CASE];
2014-01-31 23:38:13 +00:00
2014-01-30 23:27:18 +00:00
namespace ACPRODINOTE {
2014-02-04 19:58:44 +00:00
extern string thisDesc, thisRegistry, thisAccount, thatAccount;
extern testDescs theseSpecs;
2014-01-31 23:38:13 +00:00
extern testCases theseCases;
extern testFuncs theseFuncs;
extern int cmd,thisCase;
2014-02-04 19:58:44 +00:00
extern bool bindError,dryRun;
extern Json::Value theseParms;
2014-01-29 00:02:24 +00:00
2014-01-30 23:27:18 +00:00
}
2014-01-29 00:02:24 +00:00
using namespace std;
2014-01-28 19:23:02 +00:00
static std::string
readInputTestFile( const char *path )
{
FILE *file = fopen( path, "rb" );
if ( !file )
return std::string("");
fseek( file, 0, SEEK_END );
long size = ftell( file );
fseek( file, 0, SEEK_SET );
std::string text;
char *buffer = new char[size+1];
buffer[size] = 0;
if ( fread( buffer, 1, size, file ) == (unsigned long)size )
text = buffer;
fclose( file );
delete[] buffer;
return text;
}
2014-01-29 18:10:26 +00:00
bool mdJSON::run()
{
2014-02-04 19:58:44 +00:00
bool lastMemberWasString=false,parseError=false,done=false,parseOnly=false;;
int i=0, mCases=0, nCases =0, debug=100;
2014-01-29 18:10:26 +00:00
2014-01-31 23:38:13 +00:00
const Json::Value suite = root["testSuiteAC"];
2014-01-29 18:10:26 +00:00
if (!suite) {
2014-01-31 23:38:13 +00:00
theseLogs->logN(0,"No 'testSuiteAC' suite root, can't run.");
2014-01-29 18:10:26 +00:00
return true;
}
2014-01-29 23:57:01 +00:00
if (!suite.isObject()) {
2014-01-31 23:38:13 +00:00
theseLogs->logN(0,"'testSuiteAC' isn't an object, can't run.");
2014-01-29 23:57:01 +00:00
return true;
}
if (suite.empty()) {
2014-01-31 23:38:13 +00:00
theseLogs->logN(0,"'testSuiteAC' contains nothing, can't run.");
2014-01-29 23:57:01 +00:00
return true;
}
2014-02-04 19:58:44 +00:00
theseLogs->logN(1,"'testSuiteAC' - parse and bind %d fields and case objects.",suite.size());
2014-01-29 23:57:01 +00:00
Json::Value::Members itemNames = suite.getMemberNames();
2014-01-31 23:38:13 +00:00
string thisTestCase(""), thisTestCaseDesc("");
ACPRODINOTE::thisRegistry = string("");
ACPRODINOTE::thisAccount = string("");
ACPRODINOTE::thatAccount = string("");
2014-02-04 19:58:44 +00:00
ACPRODINOTE::dryRun = false;
2014-01-29 23:57:01 +00:00
for ( i = 0; i < itemNames.size(); ++i ) { const char *thisItem;
2014-01-30 02:30:49 +00:00
try {
2014-01-29 23:57:01 +00:00
thisItem = itemNames[i].c_str();
2014-01-31 23:38:13 +00:00
Json::Value thisMember = suite.get(thisItem,root);
2014-01-30 15:25:00 +00:00
if (debug > 100000)
2014-01-29 23:57:01 +00:00
theseLogs->logN(1,"item %s.",thisItem);
2014-02-04 19:58:44 +00:00
if (thisMember.isBool()) {
if (!stricmp(thisItem,"dryrun")) {
theseLogs->logN(0,"The script has toggled transactioning.");
ACPRODINOTE::dryRun = thisMember.asBool();
continue;
}
if (!stricmp(thisItem,"parseonly")) {
theseLogs->logN(0,"The script has toggled execution.");
parseOnly = thisMember.asBool();
continue;
}
}
if (thisMember.isString()) {
if (!stricmp(thisItem,"registry")) {
2014-01-31 23:38:13 +00:00
theseLogs->logN(1,"The primary name provider is '%s'",thisMember.asString().c_str());
ACPRODINOTE::thisRegistry = thisMember.asString();
continue;
2014-02-04 19:58:44 +00:00
}
if (!stricmp(thisItem,"accounta")) {
2014-01-31 23:38:13 +00:00
theseLogs->logN(1,"The first OTE account is '%s'",thisMember.asString().c_str());
ACPRODINOTE::thisAccount = thisMember.asString();
continue;
2014-02-04 19:58:44 +00:00
}
if (!stricmp(thisItem,"accountb")) {
2014-01-31 23:38:13 +00:00
theseLogs->logN(1,"The second OTE account is '%s'",thisMember.asString().c_str());
ACPRODINOTE::thatAccount = thisMember.asString();
continue;
2014-02-04 19:58:44 +00:00
}
if (strncmp(thisItem,"Case",4)) continue;
if (strlen(thisItem) < 6) continue;
if (mCases >= 1) { int k;
for (k=0;k < mCases;k++)
if (!strnicmp(ACPRODINOTE::theseSpecs[k].case_name.c_str(),thisItem,6)) {
parseError = true;
theseLogs->logN(1,"multiple '%s', not supported, use multiple files.",thisItem);
}
}
if (debug > 10000)
theseLogs->logN(1,"case desc: %s.",thisMember.asString().c_str());
ACPRODINOTE::theseSpecs[mCases].case_name = string(thisItem);
ACPRODINOTE::theseSpecs[mCases++].case_desc = thisMember.asString();
2014-01-30 15:25:00 +00:00
}
2014-02-04 19:58:44 +00:00
else { char w0[16];
if (strncmp(thisItem,"case",4)) continue;
if (strlen(thisItem) < 6) continue;
memcpy(w0,thisItem,6); w0[6] =0;
if (thisMember != root && thisMember.isObject() && !parseError) {
if (!ACPRODINOTE::theseFuncs[w0]) {
theseLogs->logN(1,"No logic to bind to '%s', need it.",thisItem);
return true;
}
parms[nCases] = itemNames[i].c_str();
ACPRODINOTE::theseCases[nCases].fBody = ACPRODINOTE::theseFuncs[w0];
ACPRODINOTE::theseCases[nCases++].caseName = thisItem;
}
}
}
catch (exception e)
{
//void *array[20];
//size_t size;
// get void*'s for all entries on the stack
//size = backtrace(array, 20);
theseLogs->logN(2,"Item %s fault: %s ",thisItem, e.what());
//backtrace_symbols_fd(array, size, thisConfig->);
AC_ASSERTIONP(false)
2014-01-30 02:30:49 +00:00
}
catch (...)
{
2014-02-04 19:58:44 +00:00
theseLogs->logN(1,"Test case parse exception: %s ",thisItem );
AC_ASSERTIONP(false)
}
}// for
if (mCases != nCases ) {
theseLogs->logN(2,"spec - case body mismatch (%d:%d).", mCases, nCases); parseError = true; }
2014-01-29 18:10:26 +00:00
2014-01-31 23:38:13 +00:00
if (ACPRODINOTE::thisRegistry.empty()) {
theseLogs->logN(0,"No primary name provider."); parseError = true; }
if (ACPRODINOTE::thisAccount.empty() && ACPRODINOTE::thatAccount.empty()) {
theseLogs->logN(0,"At least one account must be specified."); parseError = true; }
2014-01-29 18:10:26 +00:00
2014-01-31 23:38:13 +00:00
if (parseError) {
theseLogs->logN(0,"parse errors, script cannot be run");
return true;
}
2014-02-04 19:58:44 +00:00
bindDone = false;
2014-01-31 23:38:13 +00:00
ACPRODINOTE::bindError = false;
2014-02-04 19:58:44 +00:00
if (parseOnly) {
theseLogs->logN(0,"If you see no errors above, script is valid syntactically.");
JSONBatchInProgress = false;
goto endBind;
}
theseLogs->logN(1,"%d case(s) parsed, bind and queueing begins.",nCases);
2014-01-31 23:38:13 +00:00
for (i=0;i<ACPRODINOTE::theseCases.size();i++) { ACPRODINOTE::thisCase = i;
2014-02-04 19:58:44 +00:00
ACPRODINOTE::theseParms = suite.get(parms[i],root);
2014-01-29 23:57:01 +00:00
try{
2014-01-31 23:38:13 +00:00
theseLogs->logN(2,"%d Setup %s ...",i+1,ACPRODINOTE::theseCases[i].caseName );
2014-01-30 23:27:18 +00:00
ACPRODINOTE::theseCases[i].fBody();
2014-02-04 19:58:44 +00:00
theseLogs->logN(3,"%d ... %s %d parameter(s) ",i+1,ACPRODINOTE::theseCases[i].caseName,ACPRODINOTE::theseParms.size() );
2014-01-29 18:10:26 +00:00
}
2014-01-30 23:27:18 +00:00
catch (exception e)
{
theseLogs->logN(2,"Case %s fault: %s ",ACPRODINOTE::theseCases[i].caseName, e.what());
}
2014-01-29 23:57:01 +00:00
catch (...)
{
2014-01-30 23:27:18 +00:00
theseLogs->logN(1,"Unknown test case fault in %s ",ACPRODINOTE::theseCases[i].caseName );
2014-01-31 23:38:13 +00:00
}
2014-01-29 23:57:01 +00:00
}
2014-01-31 23:38:13 +00:00
if (ACPRODINOTE::bindError) {
theseLogs->logN(0,"binding errors, script cannot be run");
2014-02-04 19:58:44 +00:00
done = true;
goto endBind;
2014-01-31 23:38:13 +00:00
}
else
2014-02-04 19:58:44 +00:00
theseLogs->logN(0,"Suite 'testSuiteAC' bound and and queued for execution.");
endBind:
bindDone = true;
2014-01-31 23:38:13 +00:00
return done; // Should be false if no error.
}
2014-01-28 19:23:02 +00:00
static bool
2014-01-29 00:02:24 +00:00
parseValueTree( const std::string &input, const std::string &kind, Json::Value &root, const Json::Features &features)
2014-01-28 19:23:02 +00:00
{
Json::Reader reader( features );
bool parsingSuccessful = reader.parse( input, root );
if ( !parsingSuccessful )
{
2014-01-29 23:57:01 +00:00
theseLogs->logN(2, "Failed to parse %s file: %s",
2014-01-28 19:23:02 +00:00
kind.c_str(),
reader.getFormattedErrorMessages().c_str() );
return true;
}
return false;
2014-01-28 18:16:48 +00:00
}
2014-01-29 00:02:24 +00:00
bool mdJSON::parse()
2014-01-28 19:23:02 +00:00
{
bool value = false;
Json::Features features;
try
{
std::string input = readInputTestFile( path.c_str() );
if ( input.empty() )
{
2014-01-29 23:57:01 +00:00
theseLogs->logN(1, "Failed to read input or empty input: %s", path.c_str() );
2014-01-28 19:23:02 +00:00
return 3;
}
2014-01-29 00:02:24 +00:00
return parseValueTree( input, "input", root, features );
2014-01-28 19:23:02 +00:00
}
catch ( const std::exception &e )
{
2014-01-29 23:57:01 +00:00
theseLogs->logN(1, "Unhandled exception: %s", e.what() );
2014-01-28 19:23:02 +00:00
value = true;
}
return value;
2014-01-28 18:16:48 +00:00
}
2014-01-29 18:10:26 +00:00
void mdJSON::setPath(char *fileName) {
2014-01-28 19:23:02 +00:00
2014-01-29 18:10:26 +00:00
path = string(fileName);
2014-01-28 18:16:48 +00:00
}