diff options
Diffstat (limited to 'server/src/transactionparser.cc')
-rw-r--r-- | server/src/transactionparser.cc | 148 |
1 files changed, 63 insertions, 85 deletions
diff --git a/server/src/transactionparser.cc b/server/src/transactionparser.cc index f1f79c6..b33cac6 100644 --- a/server/src/transactionparser.cc +++ b/server/src/transactionparser.cc @@ -36,31 +36,6 @@ #include "debug.h" #include "exception.h" -static void error(const char* fmt, ...) -{ - PRACRO_ERR_LOG(transactionparser, "Error in TransactionParser: "); - - { - va_list argp; - va_start(argp, fmt); - PRACRO_ERR_LOG_VA(macro, fmt, argp); - va_end(argp); - - fprintf(stderr, "\n"); - } - - { - char *p; - va_list argp; - va_start(argp, fmt); - if(vasprintf(&p, fmt, argp) != -1) { - throw Exception("Error in TransactionParser: " + std::string(p)); - free(p); - } - va_end(argp); - } -} - TransactionParser::TransactionParser(Transaction *transaction) { this->transaction = transaction; @@ -68,8 +43,11 @@ TransactionParser::TransactionParser(Transaction *transaction) totalbytes = 0; } -void TransactionParser::startTag(std::string name, std::map< std::string, std::string> attributes) +void TransactionParser::startTag(std::string name, + std::map<std::string, std::string> attributes) { + PRACRO_DEBUG(transactionparser, "<%s>\n", name.c_str()); + if(name == "pracro") { transaction->user = attributes["user"]; transaction->cpr = attributes["cpr"]; @@ -92,29 +70,46 @@ void TransactionParser::startTag(std::string name, std::map< std::string, std::s } if(name == "field") { - if(!transaction->commits.size()) error("Field without a commit tag!"); - if(attributes.find("name") == attributes.end()) error("Field is missing 'name' attribute"); - if(attributes.find("value") == attributes.end()) error("Field is missing 'value' attribute"); - transaction->commits.back().fields[attributes["name"]] = attributes["value"]; + if(!transaction->commits.size()) { + PRACRO_ERR(transactionparser, "Field without a commit tag!"); + throw std::exception(); + } + + if(attributes.find("name") == attributes.end()) { + PRACRO_ERR(transactionparser, "Field is missing 'name' attribute"); + throw std::exception(); + } + + if(attributes.find("value") == attributes.end()) { + PRACRO_ERR(transactionparser, "Field is missing 'value' attribute"); + throw std::exception(); + } + + transaction->commits.back().fields[attributes["name"]] = + attributes["value"]; } } -void TransactionParser::parseError(const char *buf, size_t len, std::string error, int lineno) +void TransactionParser::parseError(const char *buf, size_t len, + std::string error, int lineno) { - PRACRO_ERR_LOG(transactionparser, "TransactionParser error at line %d: %s\n", - lineno, error.c_str()); - PRACRO_ERR_LOG(transactionparser, "\tBuffer %u bytes: [", len); - if(fwrite(buf, len, 1, stderr) != len) {} - PRACRO_ERR_LOG(transactionparser, "]\n"); - - char *slineno; - if(asprintf(&slineno, " at line %d\n", lineno) != -1) { - throw Exception(error + slineno); - free(slineno); - } + PRACRO_ERR(transactionparser, "TransactionParser error at line %d: %s\n", + lineno, error.c_str()); + + std::string xml; + xml.append(buf, len); + + PRACRO_ERR(transactionparser, "\tBuffer %u bytes: [%s]\n", + len, xml.c_str()); + + throw std::exception(); } #ifdef TEST_TRANSACTIONPARSER +//deps: saxparser.cc debug.cc exception.cc log.cc +//cflags: -I.. $(EXPAT_CFLAGS) +//libs: $(EXPAT_LIBS) +#include "test.h" static char xml_minimal[] = "<?xml version='1.0' encoding='UTF-8'?>\n" @@ -146,53 +141,36 @@ static char xml_fail[] = "</pracro>\n" ; -int main() -{ - // Test minimal - try { - Transaction transaction; - TransactionParser parser(&transaction); - parser.parse(xml_minimal, strlen(xml_minimal)); - } catch(Exception &e) { - printf("ERROR: %s\n", e.what()); - return 1; - } +TEST_BEGIN; - // Test request - try { - Transaction transaction; - TransactionParser parser(&transaction); - parser.parse(xml_request, strlen(xml_request)); - } catch(Exception &e) { - printf("ERROR: %s\n", e.what()); - return 1; - } - - // Test commit - try { - Transaction transaction; - TransactionParser parser(&transaction); - parser.parse(xml_commit, strlen(xml_commit)); - } catch(Exception &e) { - printf("ERROR: %s\n", e.what()); - return 1; - } +// Test minimal +{ + Transaction transaction; + TransactionParser parser(&transaction); + TEST_NOEXCEPTION(parser.parse(xml_minimal, sizeof(xml_minimal)-1), "minimal"); +} - // Test parse error (should throw an exception) - try { - Transaction transaction; - TransactionParser parser(&transaction); - parser.parse(xml_fail, strlen(xml_fail)); - } catch(Exception &e) { - printf("ERROR: %s\n", e.what()); - goto onandon; - } - printf("We should fail here...\n"); - return 1; +// Test request +{ + Transaction transaction; + TransactionParser parser(&transaction); + TEST_NOEXCEPTION(parser.parse(xml_request, sizeof(xml_request)-1), "request"); +} - onandon: +// Test commit +{ + Transaction transaction; + TransactionParser parser(&transaction); + TEST_NOEXCEPTION(parser.parse(xml_commit, sizeof(xml_commit)-1), "commit"); +} - return 0; +// Test parse error (should throw an exception) +{ + Transaction transaction; + TransactionParser parser(&transaction); + TEST_EXCEPTION(parser.parse(xml_fail, sizeof(xml_fail)-1), std::exception, "parse error"); } +TEST_END; + #endif/*TEST_TRANSACTIONPARSER*/ |