X-Git-Url: https://git.teslayout.com/public/public/public/?a=blobdiff_plain;f=example%2Fdiameter%2Flauncher%2FEventOperation.cpp;h=a3ee7baec4c4ab91cabe5a5a74ef8f15b9fa9d93;hb=8a597c7ccbe2986f505fd70258e4b59ecef4166f;hp=935940db56d8a0f6c18db729db1c026a496f981e;hpb=c56124ff93e8bceec159748dfe5ba8d56c62e3de;p=anna.git diff --git a/example/diameter/launcher/EventOperation.cpp b/example/diameter/launcher/EventOperation.cpp index 935940d..a3ee7ba 100644 --- a/example/diameter/launcher/EventOperation.cpp +++ b/example/diameter/launcher/EventOperation.cpp @@ -12,46 +12,24 @@ // Process #include #include +#include +#include +#include +#include + +// Standard +#include +#include // chdir // Project #include - - -//// Standard -//#include // std::istringstream -//#include // std::cout -//#include // ceil -//#include -#include // chdir -//#include -// -//// Project #include #include -//#include -//#include -//#include -//#include -//#include -//#include -//#include #include -//#include -//#include -//#include +#include +#include #include -//#include -//#include #include -// -//// Process -//#include -//#include -//#include -#include -#include -//#include -//#include ///////////////////// @@ -577,8 +555,17 @@ bool EventOperation::answermsg_action_2c(std::string &response, const std::strin bool EventOperation::test_id__description(std::string &response, unsigned int id, const std::string & description) { Launcher& my_app = static_cast (anna::app::functions::getApp()); + anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate(); - + try { + testManager.getTestCase(id)->setDescription(description); // creates / reuses + response = "Done"; + } + catch(anna::RuntimeException &ex) { + ex.trace(); + response += "invalid ip-limit"; + return false; + } return true; // OK } @@ -586,8 +573,17 @@ bool EventOperation::test_id__description(std::string &response, unsigned int id bool EventOperation::test_id__ip_limit(std::string &response, unsigned int id, int amount) { Launcher& my_app = static_cast (anna::app::functions::getApp()); + anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate(); - + try { + testManager.getTestCase(id)->addIpLimit(amount); // creates / reuses + response = "Done"; + } + catch(anna::RuntimeException &ex) { + ex.trace(); + response += "invalid ip-limit"; + return false; + } return true; // OK } @@ -595,26 +591,55 @@ bool EventOperation::test_id__ip_limit(std::string &response, unsigned int id, i bool EventOperation::test_id__timeout(std::string &response, unsigned int id, int msecs) { Launcher& my_app = static_cast (anna::app::functions::getApp()); + anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate(); - + try { + anna::Millisecond timeout = my_app.checkTimeMeasure("Test case timeout", anna::functions::asString(msecs)); + testManager.getTestCase(id)->addTimeout(timeout); // creates / reuses + response = "Done"; + } + catch(anna::RuntimeException &ex) { + ex.trace(); + response += "invalid timeout"; + return false; + } return true; // OK } -bool EventOperation::test_id__sendmsg2e(std::string &response, unsigned int id, const std::string & diameterJson, int stepNumber) { +bool EventOperation::test_id__sendmsg2e_2c(std::string &response, unsigned int id, bool _2e_or_2c, const std::string & diameterJson, int stepNumber) { Launcher& my_app = static_cast (anna::app::functions::getApp()); + anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate(); + bool success; + std::string diameterXml = anna::json::functions::json2xml(diameterJson, success); + if (!success) { + response += "json to xml failed, unable to load message !"; + return false; + } + anna::diameter::codec::Message codecMsg; // auxiliary codec message + codecMsg.loadXMLString(diameterXml); + LOGWARNING( + if (!codecMsg.isRequest() && (stepNumber == -1)) + anna::Logger::warning("Step number has not been provided. Take into account that this answer message will be sent 'as is' and sequence information could be wrong at the remote peer", ANNA_FILE_LOCATION); + ); - return true; // OK -} - -bool EventOperation::test_id__sendmsg2c(std::string &response, unsigned int id, const std::string & diameterJson, int stepNumber) { - - Launcher& my_app = static_cast (anna::app::functions::getApp()); - + try { + my_app.updateOperatedOriginHostWithMessage(codecMsg); + if (_2e_or_2c) + testManager.getTestCase(id)->addSendDiameterXml2e(codecMsg.code(), my_app.getOperatedHost(), stepNumber); // creates / reuses + else + testManager.getTestCase(id)->addSendDiameterXml2c(codecMsg.code(), my_app.getOperatedHost(), stepNumber); // creates / reuses + response = "Done"; + } + catch(anna::RuntimeException &ex) { + ex.trace(); + response += "failed"; + return false; + } return true; // OK } @@ -622,8 +647,18 @@ bool EventOperation::test_id__sendmsg2c(std::string &response, unsigned int id, bool EventOperation::test_id__delay(std::string &response, unsigned int id, int msecs) { Launcher& my_app = static_cast (anna::app::functions::getApp()); + anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate(); - + try { + anna::Millisecond delay = ((msecs == 0 /* special case */) ? (anna::Millisecond)0 : my_app.checkTimeMeasure("Test case delay step", anna::functions::asString(msecs))); + testManager.getTestCase(id)->addDelay(delay); // creates / reuses + response = "Done"; + } + catch(anna::RuntimeException &ex) { + ex.trace(); + response += "invalid delay"; + return false; + } return true; // OK } @@ -631,62 +666,164 @@ bool EventOperation::test_id__delay(std::string &response, unsigned int id, int bool EventOperation::test_id__sh_command(std::string &response, unsigned int id, const std::string & script) { Launcher& my_app = static_cast (anna::app::functions::getApp()); + anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate(); - - - return true; // OK -} - -bool EventOperation::test_id__waitfe_hex(std::string &response, unsigned int id, const std::string & hex, bool strict) { - - Launcher& my_app = static_cast (anna::app::functions::getApp()); - - + try { + testManager.getTestCase(id)->addCommand(script); // creates / reuses + response = "Done"; + } + catch(anna::RuntimeException &ex) { + ex.trace(); + response += "failed"; + return false; + } return true; // OK } -bool EventOperation::test_id__waitfc_hex(std::string &response, unsigned int id, const std::string & hex, bool strict) { +bool EventOperation::test_id__waitfefc_hex(std::string &response, unsigned int id, bool fe_or_fc, const std::string & hex, bool strict) { Launcher& my_app = static_cast (anna::app::functions::getApp()); + anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate(); + // Get DataBlock from hex content: + anna::DataBlock db_aux(true); + std::string hexString = hex; + hexString.erase(std::remove(hexString.begin(), hexString.end(), ':'), hexString.end()); + LOGDEBUG( + std::string msg = "Hex string (remove colons if exists): "; + msg += hexString; + anna::Logger::debug(msg, ANNA_FILE_LOCATION); + ); + + std::string regexp; + try { + anna::functions::fromHexString(hexString, db_aux); // could launch exception + regexp = anna::functions::asHexString(db_aux); + } + catch(anna::RuntimeException &ex) { + ex.trace(); + response += "failed"; + return false; + } + // optional 'full': + if(!strict) { + //// If request, we will ignore sequence data: + //if (anna::diameter::codec::functions::requestBit(db_aux)) + regexp.replace (24, 16, "[0-9A-Fa-f]{16}"); - return true; // OK -} - -bool EventOperation::test_id__waitfe_msg(std::string &response, unsigned int id, const std::string & diameterJson, bool strict) { - - Launcher& my_app = static_cast (anna::app::functions::getApp()); - + regexp.insert(0, "^"); + regexp += "$"; + } + try { + testManager.getTestCase(id)->addWaitDiameterRegexpHex(fe_or_fc, regexp); + response = "Done"; + } + catch(anna::RuntimeException &ex) { + ex.trace(); + response += "failed"; + return false; + } return true; // OK } -bool EventOperation::test_id__waitfc_msg(std::string &response, unsigned int id, const std::string & diameterJson, bool strict) { +bool EventOperation::test_id__waitfefc_msg(std::string &response, unsigned int id, bool fe_or_fc, const std::string & diameterJson, bool strict) { Launcher& my_app = static_cast (anna::app::functions::getApp()); + anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate(); + bool success; + std::string diameterXml = anna::json::functions::json2xml(diameterJson, success); + if (!success) { + response += "json to xml failed, unable to load message !"; + return false; + } + try { + anna::diameter::codec::Message codecMsg; // auxiliary codec message + codecMsg.loadXMLString(diameterXml); + std::string regexp = codecMsg.asXMLString(true /* normalization */); + + // Now we must insert regular expressions in hop-by-hop, end-to-end and Origin-State-Id: + + // optional 'full': + if(!strict) { + std::string::size_type pos, pos_1, pos_2; + + pos = regexp.find("end-to-end-id=", 0u); + pos = regexp.find("\"", pos); + pos_1 = pos; + pos = regexp.find("\"", pos+1); + pos_2 = pos; + regexp.replace(pos_1 + 1, pos_2 - pos_1 - 1, "[0-9]+"); + + pos = regexp.find("hop-by-hop-id=", 0u); + pos = regexp.find("\"", pos); + pos_1 = pos; + pos = regexp.find("\"", pos+1); + pos_2 = pos; + regexp.replace(pos_1 + 1, pos_2 - pos_1 - 1, "[0-9]+"); + + // For this representation: + //pos = regexp.find("Origin-State-Id", 0u); + //pos = regexp.find("\"", pos); + //pos = regexp.find("\"", pos+1); + //pos_1 = pos; + //pos = regexp.find("\"", pos+1); + //pos_2 = pos; + //regexp.replace(pos_1 + 1, pos_2 - pos_1 - 1, "[0-9]+"); + // But we have this one: + pos = regexp.find("Origin-State-Id", 0u); + pos = regexp.rfind("\"", pos); + pos = regexp.rfind("\"", pos-1); + pos = regexp.rfind("\"", pos-1); + pos_1 = pos; + pos = regexp.find("\"", pos+1); + pos_2 = pos; + regexp.replace(pos_1 + 1, pos_2 - pos_1 - 1, "[0-9]+"); + + //regexp.insert(0, "^"); + //regexp += "$"; + } - return true; // OK -} - -bool EventOperation::test_id__waitfe(std::string &response, unsigned int id, const std::string & condition) { - - Launcher& my_app = static_cast (anna::app::functions::getApp()); - - + testManager.getTestCase(id)->addWaitDiameterRegexpXml(fe_or_fc, regexp); + response = "Done"; + } + catch(anna::RuntimeException &ex) { + ex.trace(); + response += "failed"; + return false; + } return true; // OK } -bool EventOperation::test_id__waitfc(std::string &response, unsigned int id, const std::string & condition) { +bool EventOperation::test_id__waitfefc(std::string &response, unsigned int id, bool fe_or_fc, + const std::string & code, + const std::string & bitR, + const std::string & hopByHop, + const std::string & applicationId, + const std::string & sessionId, + const std::string & resultCode, + const std::string & msisdn, + const std::string & imsi, + const std::string & serviceContextId) { Launcher& my_app = static_cast (anna::app::functions::getApp()); + anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate(); - + try { // [code]|[bitR]|[hopByHop]|[applicationId]|[sessionId]|[resultCode]|[msisdn]|[imsi]|[serviceContextId] + testManager.getTestCase(id)->addWaitDiameter(fe_or_fc, code, bitR, hopByHop, applicationId, sessionId, resultCode, msisdn, imsi, serviceContextId); + response = "Done"; + } + catch(anna::RuntimeException &ex) { + ex.trace(); + response += "failed"; + return false; + } return true; // OK } @@ -697,26 +834,59 @@ bool EventOperation::test_id__waitfc(std::string &response, unsigned int id, con bool EventOperation::test__ttps(std::string &response, int amount) { Launcher& my_app = static_cast (anna::app::functions::getApp()); + anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate(); + bool success = testManager.configureTTPS(amount); + if (success) { + response = "Assigned new test launch rate to "; + response += anna::functions::asString(amount); + response += " events per second"; + } + else { + response += "unable to configure the test rate provided"; + } - - return true; // OK + return success; // OK } bool EventOperation::test__next(std::string &response, int syncAmount) { Launcher& my_app = static_cast (anna::app::functions::getApp()); + anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate(); + if (syncAmount < 1) { + response += "the parameter 'sync-amount' must be a positive integer value"; + return false; + } + bool success = testManager.execTestCases(syncAmount); - return true; // OK + response = (success ? "P" : "Not completely p" /* completed cycle and no repeats, rare case */); + response += "rocessed "; + response += anna::functions::asString(syncAmount); + response += ((syncAmount > 1) ? " test cases synchronously" : " test case"); + + return success; } bool EventOperation::test__ip_limit(std::string &response, int amount) { Launcher& my_app = static_cast (anna::app::functions::getApp()); + anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate(); - + if (amount > -2) { + testManager.setInProgressLimit(amount); + response = "New in-progress limit: "; + response += (amount != -1) ? anna::functions::asString(amount) : ""; + } + else { + response = "In-progress limit amount: "; + int limit = testManager.getInProgressLimit(); + response += (limit != -1) ? anna::functions::asString(limit) : ""; + response += "; currently there are "; + response += anna::functions::asString(testManager.getInProgressCount()); + response += " test cases running"; + } return true; // OK } @@ -724,26 +894,64 @@ bool EventOperation::test__ip_limit(std::string &response, int amount) { bool EventOperation::test__goto(std::string &response, int id) { Launcher& my_app = static_cast (anna::app::functions::getApp()); + anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate(); + bool success = testManager.gotoTestCase(id); + if (success) { + response = "Position updated for id provided ("; + } + else { + response += "cannot found test id ("; + } + response += anna::functions::asString(id); + response += ")"; - return true; // OK + return success; } bool EventOperation::test__run(std::string &response, int id) { Launcher& my_app = static_cast (anna::app::functions::getApp()); + anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate(); + bool success = testManager.runTestCase(id); + if (success) { + response = "Test executed for id provided ("; + } + else { + response += "cannot found test id ("; + } + response += anna::functions::asString(id); + response += ")"; - return true; // OK + return success; } bool EventOperation::test__look(std::string &response, int id) { Launcher& my_app = static_cast (anna::app::functions::getApp()); + anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate(); + anna::testing::TestCase *testCase = testManager.findTestCase(id); + if (!testCase) { + if (id == -1) { + response += "no current test case detected (testing started ?)"; + } + else { + response += "cannot found test id ("; + response += anna::functions::asString(id); + response += ")"; + } + return false; + } + + if (a_http) + response = anna::functions::encodeBase64(testCase->asXMLString()); + else + response = testCase->asXMLString(); return true; // OK } @@ -751,26 +959,89 @@ bool EventOperation::test__look(std::string &response, int id) { bool EventOperation::test__state(std::string &response, int id) { Launcher& my_app = static_cast (anna::app::functions::getApp()); + anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate(); + anna::testing::TestCase *testCase = testManager.findTestCase(id); + if (!testCase) { + if (id == -1) { + response += "no current test case detected (testing started ?)"; + } + else { + response += "cannot found test id ("; + response += anna::functions::asString(id); + response += ")"; + } + return false; + } - return true; // OK + response = anna::testing::TestCase::asText(testCase->getState()); + return testCase->isSuccess(); } bool EventOperation::test__interact(std::string &response, int amount, unsigned int id) { Launcher& my_app = static_cast (anna::app::functions::getApp()); + anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate(); + if (amount < -1) { + response += "interactive amount must be -1 (to disable interactive mode) or a positive number."; + return false; + } + anna::testing::TestCase *testCase = testManager.findTestCase(id); + if (testCase) { + if (amount == -1) { + testCase->makeInteractive(false); + response = "Interactive mode disabled"; + } + else { + testCase->addInteractiveAmount(amount); + response = "Added interactive amount of "; + response += anna::functions::asString(amount); + response += " units"; + if (amount == 0) response += " (0: freezing a non-interactive testcase, no effect on already interactive)"; + } + response += " for test case id "; + response += anna::functions::asString(id); + } + else { + response += "cannot found test id ("; + response += anna::functions::asString(id); + response += ")"; + return false; + } return true; // OK } -bool EventOperation::test__reset(std::string &response, bool soft_hard, unsigned int id) { +bool EventOperation::test__reset(std::string &response, bool soft_hard, int id) { Launcher& my_app = static_cast (anna::app::functions::getApp()); - - + anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate(); + + anna::testing::TestCase *testCase = ((id != -1) ? testManager.findTestCase(id) : NULL); + if (testCase) { + bool done = testCase->reset(!soft_hard); + response = "Test "; + response += (soft_hard ? "soft":"hard"); + response += " reset for id "; + response += anna::functions::asString(id); + response += done ? ": done": ": not done"; + } + else { + if (id == -1) { + bool anyReset = testManager.resetPool(!soft_hard); + response = (soft_hard ? "Soft":"Hard"); + response += " reset have been sent to all programmed tests: "; response += anyReset ? "some/all have been reset" : "nothing was reset"; + } + else { + response += "cannot found test id ("; + response += anna::functions::asString(id); + response += ")"; + return false; + } + } return true; // OK } @@ -778,8 +1049,12 @@ bool EventOperation::test__reset(std::string &response, bool soft_hard, unsigned bool EventOperation::test__repeats(std::string &response, int amount) { Launcher& my_app = static_cast (anna::app::functions::getApp()); + anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate(); - + if (amount < 0) amount = -1; + testManager.setPoolRepeats(amount); + std::string nolimit = (amount != -1) ? "":" [no limit]"; + response = anna::functions::asString("Pool repeats: %d%s (current cycle: %d)", amount, nolimit.c_str(), testManager.getPoolCycle()); return true; // OK } @@ -787,8 +1062,10 @@ bool EventOperation::test__repeats(std::string &response, int amount) { bool EventOperation::test__auto_reset(std::string &response, bool soft_hard) { Launcher& my_app = static_cast (anna::app::functions::getApp()); + anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate(); - + testManager.setAutoResetHard(!soft_hard); + response = anna::functions::asString("Auto-reset configured to '%s'", (soft_hard ? "soft":"hard")); return true; // OK } @@ -796,8 +1073,9 @@ bool EventOperation::test__auto_reset(std::string &response, bool soft_hard) { bool EventOperation::test__initialized(std::string &response) { Launcher& my_app = static_cast (anna::app::functions::getApp()); + anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate(); - + response = anna::functions::asString("%lu", testManager.getInitializedCount()); return true; // OK } @@ -805,8 +1083,9 @@ bool EventOperation::test__initialized(std::string &response) { bool EventOperation::test__finished(std::string &response) { Launcher& my_app = static_cast (anna::app::functions::getApp()); + anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate(); - + response = anna::functions::asString("%lu", testManager.getFinishedCount()); return true; // OK } @@ -814,17 +1093,32 @@ bool EventOperation::test__finished(std::string &response) { bool EventOperation::test__clear(std::string &response) { Launcher& my_app = static_cast (anna::app::functions::getApp()); + anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate(); - - - return true; // OK + return testManager.clearPool(response); } bool EventOperation::test__junit(std::string &response, const std::string & targetFile) { Launcher& my_app = static_cast (anna::app::functions::getApp()); + anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate(); + std::ofstream out; + out.open(targetFile.c_str()); + if(out.is_open() == false) { + response += "error opening '"; + response += targetFile; + response += "'"; + return false; + } + + out << testManager.junitAsXMLString() << std::endl; + out.close(); + + response = "Junit report written on '"; + response += targetFile; + response += "'"; return true; // OK } @@ -832,8 +1126,9 @@ bool EventOperation::test__junit(std::string &response, const std::string & targ bool EventOperation::test__summary_counts(std::string &response) { Launcher& my_app = static_cast (anna::app::functions::getApp()); + anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate(); - + response = anna::functions::encodeBase64(testManager.summaryCounts()); return true; // OK } @@ -841,8 +1136,9 @@ bool EventOperation::test__summary_counts(std::string &response) { bool EventOperation::test__summary_states(std::string &response) { Launcher& my_app = static_cast (anna::app::functions::getApp()); + anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate(); - + response = anna::functions::encodeBase64(testManager.summaryStates()); return true; // OK } @@ -850,8 +1146,9 @@ bool EventOperation::test__summary_states(std::string &response) { bool EventOperation::test__summary(std::string &response) { Launcher& my_app = static_cast (anna::app::functions::getApp()); + anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate(); - + response = anna::functions::encodeBase64(testManager.asXMLString()); return true; // OK } @@ -859,8 +1156,36 @@ bool EventOperation::test__summary(std::string &response) { bool EventOperation::test__report(std::string &response, const std::string & state, bool enable) { Launcher& my_app = static_cast (anna::app::functions::getApp()); + anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate(); + + std::string _state = state; + + if(_state == "initialized") + testManager.setDumpInitializedReports(enable); + else if(_state == "in-progress") + testManager.setDumpInProgressReports(enable); + else if(_state == "failed") + testManager.setDumpFailedReports(enable); + else if(_state == "success") + testManager.setDumpSuccessReports(enable); + else if(_state == "all") { + _state = "any"; + testManager.setDumpAllReports(enable); + } + else if(_state == "none") { + enable = !enable; + _state = "any"; + testManager.setDumpAllReports(enable); + } + else { + response += "invalid state (allowed: initialized|in-progress|failed|success|[all]|none)"; + return false; + } - + response = (enable ? "Report enabled " : "Report disabled "); + response += "for tests in '"; + response += _state; + response += "' state"; return true; // OK } @@ -868,8 +1193,10 @@ bool EventOperation::test__report(std::string &response, const std::string & sta bool EventOperation::test__report_hex(std::string &response, bool enable) { Launcher& my_app = static_cast (anna::app::functions::getApp()); + anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate(); - + testManager.setDumpHex(enable); + response = (testManager.getDumpHex() ? "Report includes hexadecimal messages" : "Report excludes hexadecimal messages"); return true; // OK } @@ -877,8 +1204,27 @@ bool EventOperation::test__report_hex(std::string &response, bool enable) { bool EventOperation::test__dump_stdout(std::string &response, bool enable) { Launcher& my_app = static_cast (anna::app::functions::getApp()); + anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate(); + + testManager.setDumpStdout(enable); + response = (testManager.getDumpStdout() ? "Test manager dumps progress into stdout" : "Test manager does not dump progress into stdout"); + return true; // OK +} + +bool EventOperation::test__dynamic(std::string &response, const nlohmann::json &arguments) { + + Launcher& my_app = static_cast (anna::app::functions::getApp()); + Procedure p(&my_app); + try { + p.execute(arguments, response); + } + catch(anna::RuntimeException &ex) { + ex.trace(); + response += ex.asString(); + return false; + } return true; // OK }