1 // A basic unit test that runs on a BMC (qemu or hardware) 2 3 #include <iostream> 4 #include <systemd/sd-journal.h> 5 #include <sstream> 6 #include "elog.hpp" 7 #include "log.hpp" 8 #include "elog-gen.hpp" 9 10 using namespace phosphor; 11 using namespace logging; 12 13 // validate the journal metadata equals the input value 14 int validate_journal(const char *i_entry, const char *i_value) 15 { 16 sd_journal *journal; 17 const void *data; 18 size_t l; 19 int rc; 20 bool validated = false; 21 22 rc = sd_journal_open(&journal, SD_JOURNAL_LOCAL_ONLY); 23 if (rc < 0) { 24 std::cerr << "Failed to open journal: " << strerror(-rc) << "\n"; 25 return 1; 26 } 27 rc = sd_journal_query_unique(journal, i_entry); 28 if (rc < 0) { 29 std::cerr << "Failed to query journal: " << strerror(-rc) << "\n"; 30 return 1; 31 } 32 SD_JOURNAL_FOREACH_UNIQUE(journal, data, l) 33 { 34 std::string journ_entry((const char*)data); 35 std::cout << journ_entry << "\n"; 36 if(journ_entry.find(i_value) != std::string::npos) 37 { 38 std::cout << "We found it!\n"; 39 validated = true; 40 break; 41 } 42 } 43 44 sd_journal_close(journal); 45 46 rc = (validated) ? 0 : 1; 47 if(rc) 48 { 49 std::cerr << "Failed to find " << i_entry << " with value " << i_value 50 <<" in journal!" << "\n"; 51 } 52 53 return rc; 54 } 55 56 int main() 57 { 58 // TEST 1 - Basic log 59 log<level::DEBUG>("Basic phosphor logging test"); 60 61 // TEST 2 - Log with metadata field 62 const char *file_name = "phosphor_logging_test.txt"; 63 int number = 0xFEFE; 64 log<level::DEBUG>("phosphor logging test with attribute", 65 entry("FILE_NAME_WITH_NUM_TEST=%s_%x", file_name, number)); 66 67 // Now read back and verify our data made it into the journal 68 int rc = validate_journal("FILE_NAME_WITH_NUM_TEST", 69 "phosphor_logging_test.txt_fefe"); 70 if(rc) 71 return(rc); 72 73 // TEST 3 - Create error log with 2 meta data fields (rvalue and lvalue) 74 number = 0x1234; 75 const char *test_string = "/tmp/test_string/"; 76 try 77 { 78 elog<org::freedesktop::DBus::Error::FileNotFound>( 79 org::freedesktop::DBus::Error::FileNotFound:: 80 ERRNUM(number), 81 org::freedesktop::DBus::Error::FileNotFound:: 82 FILE_PATH(test_string), 83 org::freedesktop::DBus::Error::FileNotFound:: 84 FILE_NAME("elog_test_3.txt")); 85 } 86 catch (elogException<org::freedesktop::DBus::Error::FileNotFound>& e) 87 { 88 std::cout << "elog exception caught: " << e.what() << std::endl; 89 } 90 91 // Reduce our error namespaces 92 using namespace org::freedesktop::DBus::Error; 93 94 // Now read back and verify our data made it into the journal 95 std::stringstream stream; 96 stream << std::hex << number; 97 rc = validate_journal(FileNotFound::ERRNUM::str_short, 98 std::string(stream.str()).c_str()); 99 if(rc) 100 return(rc); 101 102 rc = validate_journal(FileNotFound::FILE_PATH::str_short, 103 test_string); 104 if(rc) 105 return(rc); 106 107 rc = validate_journal(FileNotFound::FILE_NAME::str_short, 108 "elog_test_3.txt"); 109 if(rc) 110 return(rc); 111 112 // TEST 4 - Create error log with previous entry use 113 number = 0x9876; 114 try 115 { 116 elog<FileNotFound>(FileNotFound::ERRNUM(number), 117 prev_entry<FileNotFound::FILE_PATH>(), 118 FileNotFound::FILE_NAME("elog_test_4.txt")); 119 } 120 catch (elogExceptionBase& e) 121 { 122 std::cout << "elog exception caught: " << e.what() << std::endl; 123 } 124 125 // Now read back and verify our data made it into the journal 126 stream.str(""); 127 stream << std::hex << number; 128 rc = validate_journal(FileNotFound::ERRNUM::str_short, 129 std::string(stream.str()).c_str()); 130 if(rc) 131 return(rc); 132 133 // This should just be equal to what we put in test 3 134 rc = validate_journal(FileNotFound::FILE_PATH::str_short, 135 test_string); 136 if(rc) 137 return(rc); 138 139 rc = validate_journal(FileNotFound::FILE_NAME::str_short, 140 "elog_test_4.txt"); 141 if(rc) 142 return(rc); 143 144 // Compile fail tests 145 146 // Simple test to prove we fail to compile due to missing param 147 //elog<FileNotFound>(FileNotFound::ERRNUM(1), 148 // FileNotFound::FILE_PATH("test")); 149 150 // Simple test to prove we fail to compile due to invalid param 151 //elog<FileNotFound>(FileNotFound::ERRNUM(1), 152 // FileNotFound::FILE_PATH("test"), 153 // FileNotFound::FILE_NAME(1)); 154 155 return 0; 156 } 157 158 159