1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
|
// NOLINTBEGIN(*)
#include "compare_test.h"
#include <stdio.h>
#include <algorithm>
#ifdef WIN32
#include <process.h>
using pid_t = int;
#else
#include <unistd.h>
#endif
#include "logging.h"
using namespace std;
using namespace CmpTst;
#define TMP_SUFFIX ".tmp"
namespace
{
const std::string LOG_PREFIX = "<log4j:message><![CDATA[";
const std::string LOG_SUFFIX = "]]></log4j:message>";
void strip_cr_lf(std::string &line)
{
while(line.size() > 0 && (line.back() == '\r' || line.back() == '\n'))
{
line.pop_back();
}
}
bool is_log_message(const std::string &line)
{
return line.compare(0, LOG_PREFIX.size(), LOG_PREFIX) == 0 &&
line.compare(line.size() - LOG_SUFFIX.size(), LOG_SUFFIX.size(), LOG_SUFFIX) == 0;
}
auto strip_log_tags(const std::string &line)
{
return line.substr(LOG_PREFIX.size(), line.size() - LOG_PREFIX.size() - LOG_SUFFIX.size());
}
bool is_matching_log_message(const std::string &ref_line, const std::string &out_line)
{
// For log messages, we only check that emitted message contains expected
// substring. There may be extra information included (like line numbers).
return is_log_message(ref_line) && is_log_message(out_line) &&
out_line.find(strip_log_tags(ref_line)) != std::string::npos;
}
bool is_line_equal(const std::string &ref_line, const std::string &out_line)
{
return ref_line.compare(out_line) == 0;
}
bool compare_lines(const std::string &ref_line, const std::string &out_line)
{
return is_line_equal(ref_line, out_line) || is_matching_log_message(ref_line, out_line);
}
} // namespace
//+-------------------------------------------------------------------------
//
// method : make_filename_unique
//
// description : Returns a filename from `base` which is guaranteed to be
// unique among all the running copies of this program.
// argument : in : - base : basename for the file
std::string CmpTst::CompareTest::make_filename_unique(std::string base)
{
pid_t pid = getpid();
std::stringstream unique_filename;
unique_filename << base << "." << pid;
return unique_filename.str();
}
//+-------------------------------------------------------------------------
//
// method : out_set_event_properties
//
// description : Scans the output file seeking log4j:event tags and replaces
// values of the indicated properties in the found tags. E.g.:
// Original log4j:event tag:
// <log4j:event logger="DSERVER" timestamp="10" level="DEBUG" thread="1">
// After out_set_event_properties("outfile", "thread", "20") call:
// <log4j:event logger="DSERVER" timestamp="10" level="DEBUG" thread="20">
// argument : in : - file : output file to be modified
// - prop : property to be changed the value of
// - value : new value
//
//--------------------------------------------------------------------------
void CmpTst::CompareTest::out_set_event_properties(string file, string prop, string val)
{
map<string, string> prop_val_map;
prop_val_map[prop] = val;
out_set_event_properties(file, prop_val_map);
}
//+-------------------------------------------------------------------------
//
// method : out_set_event_properties
//
// description : Scans the output file seeking log4j:event tags and replaces
// values of the properties indicated by pop_val_map in the tags found.
// E.g.:
// Original log4j:event tag:
// <log4j:event logger="DSERVER" timestamp="10" level="DEBUG" thread="1">
// Map declaration:
// map<std::string,std::string> pop_val_map;
// prop_val_map["timestamp"] = "5";
// prop_val_map["thread"] = "20";
// The tag after out_set_event_properties("outfile", pop_val_map) call:
// <log4j:event logger="DSERVER" timestamp="5" level="DEBUG" thread="20">
// argument : in : - file : output file to be modified
// - prop_val_map : keys - properties to be modified,
// values - new values of the properties
//
//--------------------------------------------------------------------------
void CmpTst::CompareTest::out_set_event_properties(string file, map<string, string> prop_val_map)
{
if(prop_val_map.size() > 0)
{
ifstream infile;
ofstream outfile;
infile.open(file.c_str()); // file to be modified
if(!infile)
{
throw CmpTst::CompareTestException("[CmpTst::CompareTest::out_set_event_properties] Cannot open file: " +
file);
}
string outfile_name = file + TMP_SUFFIX; // temporary file
outfile.open(string(outfile_name).c_str());
if(!outfile)
{
throw CmpTst::CompareTestException(
"[CmpTst::CompareTest::out_set_event_properties] Cannot open file for writing: " + outfile_name);
}
while(infile)
{
string line;
getline(infile, line);
// modify only properties of the 'event' tags
if(line.find("<log4j:event ") != string::npos)
{
// for each line checks presence of each property
for(map<string, string>::iterator it = prop_val_map.begin(); it != prop_val_map.end(); ++it)
{
string property_str = (*it).first + "=\"";
// replaces property with value each time the keyword is found in the line
size_t begin = line.find(property_str);
while(begin != string::npos)
{
begin += property_str.length();
size_t end =
line.find("\"", begin); // looking for the property value closing double quotation mark
line.replace(begin, end - begin, (*it).second); // replacing with new value
begin = line.find(property_str, begin + (*it).second.size());
}
}
}
outfile << line; // writing line to the tmp file
if(!infile.eof())
{
outfile << endl; // no endl after the last line in the file
}
}
infile.close();
outfile.close();
if(infile.bad())
{
throw CmpTst::CompareTestException("[CmpTst::CompareTest::out_set_event_properties] Cannot close file: " +
file);
}
if(outfile.bad())
{
throw CmpTst::CompareTestException("[CmpTst::CompareTest::out_set_event_properties] Cannot close file: " +
outfile_name);
}
// remove file to swap its content with the tmp file
if(remove(file.c_str()) != 0)
{
throw CmpTst::CompareTestException("[CmpTst::CompareTest::out_set_event_properties] Cannot remove file: " +
file);
}
// rename the tmp file to the name of its original
if(rename(string(outfile_name).c_str(), file.c_str()) != 0)
{
throw CmpTst::CompareTestException("[CmpTst::CompareTest::out_set_event_properties] Cannot rename file: " +
outfile_name);
}
}
}
//+-------------------------------------------------------------------------
//
// method : out_set_replace_numbers
//
// description : Scans the output file seeking the occurrences of the
// prefix and replaces the following number with the one declared.
// The prefix can be any string directly preceding the number
// to be replaced.
// E.g.:
// Original tag in the output file:
// <![CDATA[In rel_monitor() DSERVER, ctr = 123, thread = 2]]>
// The tag after out_set_replace_numbers("outfile", "ctr = ", "4") call:
// <![CDATA[In rel_monitor() DSERVER, ctr = 4, thread = 2]]>
// argument : in : - file : output file to be modified
// - prefix : string directly preceding the number
// to be replaced
// - num : new number
//
//--------------------------------------------------------------------------
void CmpTst::CompareTest::out_set_replace_numbers(string file, string prefix, string num)
{
map<string, string> prefix_num_map;
prefix_num_map[prefix] = num;
out_set_replace_numbers(file, prefix_num_map);
}
//+-------------------------------------------------------------------------
//
// method : out_set_replace_numbers
//
// description : Scans the output file seeking the occurrences of the
// prefixes indicated by the prefix_num_map and replaces the
// following numbers with the ones declared in the map.
// The prefix can be any string directly preceding the number
// to be replaced.
// E.g.:
// Original tag in the output file:
// <![CDATA[In rel_monitor() DSERVER, ctr = 123, thread = 2]]>
// Map declaration:
// map<std::string,std::string> prefix_num_map;
// prefix_num_map["ctr = "] = "4";
// prefix_num_map["thread = "] = "567";
// The tag after out_set_replace_numbers("outfile", prefix_num_map) call:
// <![CDATA[In rel_monitor() DSERVER, ctr = 4, thread = 567]]>
// argument : in : - file : output file to be modified
// - prefix_num_map : keys - strings preceding the numbers
// to be modified,
// values - new numbers
//
//--------------------------------------------------------------------------
void CmpTst::CompareTest::out_set_replace_numbers(string file, map<string, string> prefix_num_map)
{
if(prefix_num_map.size() > 0)
{
ifstream infile;
ofstream outfile;
infile.open(file.c_str()); // file to be modified
if(!infile)
{
throw CmpTst::CompareTestException("[CmpTst::CompareTest::out_set_event_properties] Cannot open file: " +
file);
}
string outfile_name = file + TMP_SUFFIX; // temporary file
outfile.open(string(outfile_name).c_str());
if(!outfile)
{
throw CmpTst::CompareTestException(
"[CmpTst::CompareTest::out_set_event_properties] Cannot open file for writing: " + outfile_name);
}
while(infile)
{
string line;
getline(infile, line);
// for each line checks presence of each property
for(map<string, string>::iterator it = prefix_num_map.begin(); it != prefix_num_map.end(); ++it)
{
string prefix = (*it).first;
// replaces property with value each time the prefix is found in the line
size_t begin = line.find(prefix);
while(begin != string::npos)
{
begin += prefix.size();
size_t i = begin;
// checks if the next character is a number
while(i < line.size() && line[i] >= '0' && line[i] <= '9')
{
i++;
}
size_t end = i;
line.replace(begin, end - begin, (*it).second);
begin = line.find(prefix, begin + (*it).second.size());
}
}
outfile << line; // writing line to the tmp file
if(!infile.eof())
{
outfile << endl; // no endl after the last line in the file
}
}
infile.close();
outfile.close();
if(infile.bad())
{
throw CmpTst::CompareTestException("[CmpTst::CompareTest::out_set_event_properties] Cannot close file: " +
file);
}
if(outfile.bad())
{
throw CmpTst::CompareTestException("[CmpTst::CompareTest::out_set_event_properties] Cannot close file: " +
outfile_name);
}
// remove file to swap its content with the tmp file
if(remove(file.c_str()) != 0)
{
throw CmpTst::CompareTestException("[CmpTst::CompareTest::out_set_event_properties] Cannot remove file: " +
file);
}
// rename the tmp file to the name of its original
if(rename(string(outfile_name).c_str(), file.c_str()) != 0)
{
throw CmpTst::CompareTestException("[CmpTst::CompareTest::out_set_event_properties] Cannot rename file: " +
outfile_name);
}
}
}
//+-------------------------------------------------------------------------
//
// method : ref_replace_keywords
//
// description : Scans the output file seeking the occurrences of the
// keyword and replaces them with the declared value.
// E.g.:
// Original tag in the output file:
// <log4j:event logger="DSERVER" timestamp="10" level="DEBUG" thread="1">
// The tag after ref_replace_keywords("outfile", "DSERVER", "dserver/devTest/test") call:
// <log4j:event logger="dserver/devTest/test" timestamp="10" level="DEBUG" thread="1">
// argument : in : - file : output file to be modified
// - key : keyword to be replaced
// - value : new string
//
//--------------------------------------------------------------------------
void CmpTst::CompareTest::ref_replace_keywords(string file, string key, string val)
{
map<string, string> key_val_map;
key_val_map[key] = val;
ref_replace_keywords(file, key_val_map);
}
//+-------------------------------------------------------------------------
//
// method : ref_replace_keywords
//
// description : Scans the output file seeking the occurrences of the
// keywords indicated by the key_val_map and replaces them with
// the declared values.
// E.g.:
// Original tag in the output file:
// <log4j:event logger="DSERVER" timestamp="10" level="DEBUG" thread="1">
// Map declaration:
// map<std::string,std::string> key_val_map;
// key_val_map["DSERVER"] = "dserver/devTest/test";
// key_val_map["DEBUG"] = "INFO";
// The tag after ref_replace_keywords("outfile", key_val_map) call:
// <log4j:event logger="dserver/devTest/test" timestamp="10" level="INFO" thread="1">
// argument : in : - file : output file to be modified
// - key_val_map : keys - keywords to be replaced,
// values - new replacement strings
//
//--------------------------------------------------------------------------
void CmpTst::CompareTest::ref_replace_keywords(string file, map<string, string> key_val_map)
{
if(key_val_map.size() > 0)
{
ifstream infile;
ofstream outfile;
infile.open(file.c_str());
if(!infile)
{
throw CmpTst::CompareTestException("[CmpTst::CompareTest::ref_replace_keywords] Cannot open file: " + file);
}
string outfile_name = make_filename_unique(file + TMP_SUFFIX); // temporary file
outfile.open(string(outfile_name).c_str());
if(!outfile)
{
throw CmpTst::CompareTestException(
"[CmpTst::CompareTest::ref_replace_keywords] Cannot open file for writing: " + outfile_name);
}
while(infile)
{
string line;
getline(infile, line);
// for each line checks presence of each keyword
for(map<string, string>::iterator it = key_val_map.begin(); it != key_val_map.end(); ++it)
{
// replaces keyword with value each time the keyword is found in the line
size_t begin = line.find((*it).first);
while(begin != string::npos)
{
line.replace(begin, (*it).first.size(), (*it).second);
begin = line.find((*it).first, begin + (*it).second.size());
}
}
outfile << line; // writing line to the tmp file
if(!infile.eof())
{
outfile << endl; // no endl after the last line in the file
}
}
infile.close();
outfile.close();
if(infile.bad())
{
throw CmpTst::CompareTestException("[CmpTst::CompareTest::ref_replace_keywords] Cannot close file: " +
file);
}
if(outfile.bad())
{
throw CmpTst::CompareTestException("[CmpTst::CompareTest::ref_replace_keywords] Cannot close file: " +
outfile_name);
}
}
}
//+-------------------------------------------------------------------------
//
// method : out_remove_entries
//
// description :
// argument : in : - file : output file to be modified
// - prop_val_map : keys - properties to be modified,
// values - new values of the properties
//
//--------------------------------------------------------------------------
void CmpTst::CompareTest::out_remove_entries(string file, vector<string> &v_entries)
{
if(v_entries.empty() == false)
{
ifstream infile;
ofstream outfile;
infile.open(file.c_str()); // file to be modified
if(!infile)
{
throw CmpTst::CompareTestException("[CmpTst::CompareTest::out_remove_entries] Cannot open file: " + file);
}
string outfile_name = file + TMP_SUFFIX; // temporary file
outfile.open(string(outfile_name).c_str());
if(!outfile)
{
throw CmpTst::CompareTestException(
"[CmpTst::CompareTest::out_remove_entries] Cannot open file for writing: " + outfile_name);
}
while(infile)
{
string line1;
getline(infile, line1);
if(line1.find("<log4j:event ") == string::npos)
{
outfile << line1;
continue;
}
string line2, line3, line4, line5;
getline(infile, line2);
getline(infile, line3);
getline(infile, line4);
getline(infile, line5);
// for each line checks presence of each property
bool found = false;
for(vector<string>::iterator it = v_entries.begin(); it != v_entries.end(); ++it)
{
if(line2.find(*it) != string::npos)
{
found = true;
break;
}
}
if(found == true)
{
continue;
}
outfile << line1; // writing lines to the tmp file
outfile << endl;
outfile << line2;
outfile << endl;
outfile << line3;
outfile << endl;
outfile << line4;
outfile << endl;
outfile << line5;
if(!infile.eof())
{
outfile << endl; // no endl after the last line in the file
}
}
infile.close();
outfile.close();
if(infile.bad())
{
throw CmpTst::CompareTestException("[CmpTst::CompareTest::out_remove_entries] Cannot close file: " + file);
}
if(outfile.bad())
{
throw CmpTst::CompareTestException("[CmpTst::CompareTest::out_remove_entries] Cannot close file: " +
outfile_name);
}
// remove file to swap its content with the tmp file
if(remove(file.c_str()) != 0)
{
throw CmpTst::CompareTestException("[CmpTst::CompareTest::out_reemove_entries] Cannot remove file: " +
file);
}
// rename the tmp file to the name of its original
if(rename(string(outfile_name).c_str(), file.c_str()) != 0)
{
throw CmpTst::CompareTestException("[CmpTst::CompareTest::out_remove_entries] Cannot rename file: " +
outfile_name);
}
}
}
//+-------------------------------------------------------------------------
//
// method : compare
//
// description : Compares the output file with the reference file line
// by line. Throws an exception at the first occurrence of any
// discrepancy. No exception thrown means the files are identical.
// The exception indicates the number of the inconsistent line and
// displays it as well as the corresponding line from the reference
// file.
// argument : in : - ref : reference file to be compared with
// - out : output file to be compared with the
// reference file
//
//--------------------------------------------------------------------------
void CmpTst::CompareTest::compare(string ref, string out)
{
ifstream refstream, outstream;
string ref_tmp = make_filename_unique(ref + TMP_SUFFIX);
refstream.open(ref_tmp.c_str());
if(!refstream)
{
refstream.open(ref.c_str());
if(!refstream)
{
throw CmpTst::CompareTestException("[CmpTst::CompareTest::compare] Cannot open reference file: " + ref);
}
}
outstream.open(out.c_str());
if(!outstream)
{
throw CmpTst::CompareTestException("[CmpTst::CompareTest::compare] Cannot open output file: " + out);
}
unsigned int line_number = 0;
while(refstream && outstream)
{
line_number++;
string ref_line, out_line;
getline(refstream, ref_line);
getline(outstream, out_line);
strip_cr_lf(ref_line);
strip_cr_lf(out_line);
if(!compare_lines(ref_line, out_line))
{
refstream.close();
outstream.close();
stringstream ss;
ss << line_number;
throw CmpTst::CompareTestException("[CmpTst::CompareTest::compare] FAILED in file: " + out + ":" +
ss.str() + "\nEXPECTED:\t" + ref_line + "\n WAS:\t" + out_line);
}
}
refstream.close();
outstream.close();
if(refstream.bad())
{
throw CmpTst::CompareTestException("[CmpTst::CompareTest::compare] Cannot close file: " + ref);
}
if(outstream.bad())
{
throw CmpTst::CompareTestException("[CmpTst::CompareTest::compare] Cannot close file: " + out);
}
}
//+-------------------------------------------------------------------------
//
// method : clean_on_startup
//
// description : Removes the temporary files created while running
// the CompareTest which were left undeleted on sudden termination
// of a test suite.
// argument : in : - ref : reference file
// - out : output file
//
//--------------------------------------------------------------------------
void CmpTst::CompareTest::clean_on_startup(string ref, string out)
{
string ref_tmp = make_filename_unique(ref + TMP_SUFFIX);
remove(ref_tmp.c_str());
remove(out.c_str());
}
//+-------------------------------------------------------------------------
//
// method : clean_up
//
// description : Removes the output file and the temporary files
// created while running the CompareTest (ref_tmp).
// argument : in : - ref : reference file
// - out : output file
//
//--------------------------------------------------------------------------
void CmpTst::CompareTest::clean_up(string ref, string out)
{
string ref_tmp = make_filename_unique(ref + TMP_SUFFIX);
bool ref_err = false, out_err = false;
if(remove(ref_tmp.c_str()) != 0)
{
ref_err = true;
}
if(remove(out.c_str()) != 0)
{
out_err = true;
}
if(ref_err)
{
throw CmpTst::CompareTestException("[CmpTst::CompareTest::clean_up] Cannot remove file: " + ref_tmp);
}
if(out_err)
{
throw CmpTst::CompareTestException("[CmpTst::CompareTest::clean_up] Cannot remove file: " + out);
}
}
//+-------------------------------------------------------------------------
//
// method : leave_output
//
// description : Removes the temporary files created while running
// the CompareTest (ref_tmp) and leaves the output file.
// argument : in : - ref : reference file
//
//--------------------------------------------------------------------------
void CmpTst::CompareTest::leave_output(string ref)
{
string ref_tmp = make_filename_unique(ref + TMP_SUFFIX);
if(remove(ref_tmp.c_str()) != 0)
{
throw CmpTst::CompareTestException("[CmpTst::CompareTest::clean_up] Cannot remove file: " + ref_tmp);
}
}
//+-------------------------------------------------------------------------
//
// method : print_output
//
// description : Displays the output file.
// argument : in : - out : output file to be displayed
//
//--------------------------------------------------------------------------
void CmpTst::CompareTest::print_output(string out)
{
print_output(out, false);
}
//+-------------------------------------------------------------------------
//
// method : print_output
//
// description : Displays the output file and shows the line numbers.
// argument : in : - out : output file to be displayed
// - show_line_numbers : if true displays line numbers
//
//--------------------------------------------------------------------------
void CmpTst::CompareTest::print_output(string out, bool show_line_numbers)
{
ifstream outstream;
outstream.open(out.c_str());
if(!outstream)
{
throw CmpTst::CompareTestException("[CmpTst::CompareTest::print_output] Cannot open output file: " + out);
}
TEST_LOG << out << ":" << endl;
unsigned int line_number = 0;
while(outstream)
{
line_number++;
string ref_line, out_line, ref_line_orig, out_line_orig;
getline(outstream, out_line);
if(show_line_numbers)
{
TEST_LOG << line_number << "\t" << out_line << endl;
}
else
{
TEST_LOG << out_line << endl;
}
}
outstream.close();
if(outstream.bad())
{
throw CmpTst::CompareTestException("[CmpTst::CompareTest::print_output] Cannot close file: " + out);
}
}
#undef TMP_SUFFIX
// NOLINTEND(*)
|