2010-12-05 21:23:36 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2023-01-28 10:16:34 +01:00
|
|
|
* Copyright (C) 2007-2023 Cppcheck team.
|
2010-12-05 21:23:36 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "config.h"
|
2011-02-03 08:44:23 +01:00
|
|
|
#include "cppcheck.h"
|
2010-12-05 21:23:36 +01:00
|
|
|
#include "errorlogger.h"
|
2022-01-27 19:03:20 +01:00
|
|
|
#include "errortypes.h"
|
2023-01-27 08:18:32 +01:00
|
|
|
#include "fixture.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
|
|
|
|
#include <list>
|
|
|
|
#include <string>
|
2010-12-05 21:23:36 +01:00
|
|
|
|
2023-11-26 14:04:35 +01:00
|
|
|
#include "xml.h"
|
2015-03-11 22:26:33 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
class TestErrorLogger : public TestFixture {
|
2010-12-05 21:23:36 +01:00
|
|
|
public:
|
2023-05-02 11:48:24 +02:00
|
|
|
TestErrorLogger() : TestFixture("TestErrorLogger") {}
|
2010-12-05 21:23:36 +01:00
|
|
|
|
|
|
|
private:
|
2023-05-02 11:48:24 +02:00
|
|
|
const ErrorMessage::FileLocation fooCpp5{"foo.cpp", 5, 1};
|
|
|
|
const ErrorMessage::FileLocation barCpp8{"bar.cpp", 8, 1};
|
2010-12-05 21:23:36 +01:00
|
|
|
|
2022-02-10 23:02:24 +01:00
|
|
|
void run() override {
|
2015-08-31 16:44:33 +02:00
|
|
|
TEST_CASE(PatternSearchReplace);
|
2010-12-05 21:23:36 +01:00
|
|
|
TEST_CASE(FileLocationDefaults);
|
|
|
|
TEST_CASE(FileLocationSetFile);
|
|
|
|
TEST_CASE(ErrorMessageConstruct);
|
2011-07-16 12:41:31 +02:00
|
|
|
TEST_CASE(ErrorMessageConstructLocations);
|
2010-12-05 21:23:36 +01:00
|
|
|
TEST_CASE(ErrorMessageVerbose);
|
2011-07-16 12:41:31 +02:00
|
|
|
TEST_CASE(ErrorMessageVerboseLocations);
|
2023-09-20 10:40:57 +02:00
|
|
|
TEST_CASE(ErrorMessageFromInternalError);
|
2010-12-05 21:23:36 +01:00
|
|
|
TEST_CASE(CustomFormat);
|
|
|
|
TEST_CASE(CustomFormat2);
|
2011-07-16 12:41:31 +02:00
|
|
|
TEST_CASE(CustomFormatLocations);
|
2011-02-03 08:44:23 +01:00
|
|
|
TEST_CASE(ToXmlV2);
|
2011-07-16 12:41:31 +02:00
|
|
|
TEST_CASE(ToXmlV2Locations);
|
2014-04-13 09:50:57 +02:00
|
|
|
TEST_CASE(ToXmlV2Encoding);
|
2021-04-22 14:28:33 +02:00
|
|
|
TEST_CASE(FromXmlV2);
|
2011-04-10 21:51:27 +02:00
|
|
|
|
|
|
|
// Inconclusive results in xml reports..
|
|
|
|
TEST_CASE(InconclusiveXml);
|
2011-04-16 13:42:48 +02:00
|
|
|
|
|
|
|
// Serialize / Deserialize inconclusive message
|
|
|
|
TEST_CASE(SerializeInconclusiveMessage);
|
2015-01-17 15:56:09 +01:00
|
|
|
TEST_CASE(DeserializeInvalidInput);
|
2015-01-17 15:58:36 +01:00
|
|
|
TEST_CASE(SerializeSanitize);
|
2019-08-18 09:06:40 +02:00
|
|
|
TEST_CASE(SerializeFileLocation);
|
2012-07-13 08:29:49 +02:00
|
|
|
|
2023-04-08 22:19:52 +02:00
|
|
|
TEST_CASE(substituteTemplateFormatStatic);
|
|
|
|
TEST_CASE(substituteTemplateLocationStatic);
|
2010-12-05 21:23:36 +01:00
|
|
|
}
|
|
|
|
|
2015-08-31 16:44:33 +02:00
|
|
|
void TestPatternSearchReplace(const std::string& idPlaceholder, const std::string& id) const {
|
|
|
|
const std::string plainText = "text";
|
|
|
|
|
2020-05-23 07:16:49 +02:00
|
|
|
ErrorMessage message;
|
2019-08-18 12:19:05 +02:00
|
|
|
message.id = id;
|
2015-08-31 16:44:33 +02:00
|
|
|
|
|
|
|
std::string serialized = message.toString(true, idPlaceholder + plainText + idPlaceholder);
|
|
|
|
ASSERT_EQUALS(id + plainText + id, serialized);
|
|
|
|
|
|
|
|
serialized = message.toString(true, idPlaceholder + idPlaceholder);
|
|
|
|
ASSERT_EQUALS(id + id, serialized);
|
|
|
|
|
|
|
|
serialized = message.toString(true, plainText + idPlaceholder + plainText);
|
|
|
|
ASSERT_EQUALS(plainText + id + plainText, serialized);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PatternSearchReplace() const {
|
|
|
|
const std::string idPlaceholder = "{id}";
|
|
|
|
|
|
|
|
const std::string empty;
|
|
|
|
TestPatternSearchReplace(idPlaceholder, empty);
|
|
|
|
|
|
|
|
const std::string shortIdValue = "ID";
|
|
|
|
ASSERT_EQUALS(true, shortIdValue.length() < idPlaceholder.length());
|
|
|
|
TestPatternSearchReplace(idPlaceholder, shortIdValue);
|
|
|
|
|
|
|
|
const std::string mediumIdValue = "_ID_";
|
|
|
|
ASSERT_EQUALS(mediumIdValue.length(), idPlaceholder.length());
|
|
|
|
TestPatternSearchReplace(idPlaceholder, mediumIdValue);
|
|
|
|
|
|
|
|
const std::string longIdValue = "longId";
|
|
|
|
ASSERT_EQUALS(true, longIdValue.length() > idPlaceholder.length());
|
|
|
|
TestPatternSearchReplace(idPlaceholder, longIdValue);
|
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void FileLocationDefaults() const {
|
2020-05-23 07:16:49 +02:00
|
|
|
ErrorMessage::FileLocation loc;
|
2010-12-05 21:23:36 +01:00
|
|
|
ASSERT_EQUALS("", loc.getfile());
|
|
|
|
ASSERT_EQUALS(0, loc.line);
|
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void FileLocationSetFile() const {
|
2020-05-23 07:16:49 +02:00
|
|
|
ErrorMessage::FileLocation loc;
|
2010-12-05 21:23:36 +01:00
|
|
|
loc.setfile("foo.cpp");
|
|
|
|
ASSERT_EQUALS("foo.cpp", loc.getfile());
|
|
|
|
ASSERT_EQUALS(0, loc.line);
|
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void ErrorMessageConstruct() const {
|
2020-05-23 07:16:49 +02:00
|
|
|
std::list<ErrorMessage::FileLocation> locs(1, fooCpp5);
|
2021-02-24 22:00:06 +01:00
|
|
|
ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.", "errorId", Certainty::normal);
|
2019-08-18 12:19:05 +02:00
|
|
|
ASSERT_EQUALS(1, msg.callStack.size());
|
2010-12-05 21:23:36 +01:00
|
|
|
ASSERT_EQUALS("Programming error.", msg.shortMessage());
|
|
|
|
ASSERT_EQUALS("Programming error.", msg.verboseMessage());
|
|
|
|
ASSERT_EQUALS("[foo.cpp:5]: (error) Programming error.", msg.toString(false));
|
|
|
|
ASSERT_EQUALS("[foo.cpp:5]: (error) Programming error.", msg.toString(true));
|
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void ErrorMessageConstructLocations() const {
|
2020-05-23 07:16:49 +02:00
|
|
|
std::list<ErrorMessage::FileLocation> locs = { fooCpp5, barCpp8 };
|
2021-02-24 22:00:06 +01:00
|
|
|
ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.", "errorId", Certainty::normal);
|
2019-08-18 12:19:05 +02:00
|
|
|
ASSERT_EQUALS(2, msg.callStack.size());
|
2011-07-16 12:41:31 +02:00
|
|
|
ASSERT_EQUALS("Programming error.", msg.shortMessage());
|
|
|
|
ASSERT_EQUALS("Programming error.", msg.verboseMessage());
|
|
|
|
ASSERT_EQUALS("[foo.cpp:5] -> [bar.cpp:8]: (error) Programming error.", msg.toString(false));
|
|
|
|
ASSERT_EQUALS("[foo.cpp:5] -> [bar.cpp:8]: (error) Programming error.", msg.toString(true));
|
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void ErrorMessageVerbose() const {
|
2020-05-23 07:16:49 +02:00
|
|
|
std::list<ErrorMessage::FileLocation> locs(1, fooCpp5);
|
2021-02-24 22:00:06 +01:00
|
|
|
ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", Certainty::normal);
|
2019-08-18 12:19:05 +02:00
|
|
|
ASSERT_EQUALS(1, msg.callStack.size());
|
2010-12-05 21:23:36 +01:00
|
|
|
ASSERT_EQUALS("Programming error.", msg.shortMessage());
|
|
|
|
ASSERT_EQUALS("Verbose error", msg.verboseMessage());
|
|
|
|
ASSERT_EQUALS("[foo.cpp:5]: (error) Programming error.", msg.toString(false));
|
|
|
|
ASSERT_EQUALS("[foo.cpp:5]: (error) Verbose error", msg.toString(true));
|
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void ErrorMessageVerboseLocations() const {
|
2020-05-23 07:16:49 +02:00
|
|
|
std::list<ErrorMessage::FileLocation> locs = { fooCpp5, barCpp8 };
|
2021-02-24 22:00:06 +01:00
|
|
|
ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", Certainty::normal);
|
2019-08-18 12:19:05 +02:00
|
|
|
ASSERT_EQUALS(2, msg.callStack.size());
|
2011-07-16 12:41:31 +02:00
|
|
|
ASSERT_EQUALS("Programming error.", msg.shortMessage());
|
|
|
|
ASSERT_EQUALS("Verbose error", msg.verboseMessage());
|
|
|
|
ASSERT_EQUALS("[foo.cpp:5] -> [bar.cpp:8]: (error) Programming error.", msg.toString(false));
|
|
|
|
ASSERT_EQUALS("[foo.cpp:5] -> [bar.cpp:8]: (error) Verbose error", msg.toString(true));
|
|
|
|
}
|
|
|
|
|
2023-09-20 10:40:57 +02:00
|
|
|
void ErrorMessageFromInternalError() const {
|
|
|
|
// TODO: test with token
|
|
|
|
{
|
|
|
|
InternalError internalError(nullptr, "message", InternalError::INTERNAL);
|
|
|
|
const auto msg = ErrorMessage::fromInternalError(internalError, nullptr, "file.c");
|
|
|
|
ASSERT_EQUALS(1, msg.callStack.size());
|
|
|
|
const auto &loc = *msg.callStack.cbegin();
|
|
|
|
ASSERT_EQUALS(0, loc.fileIndex);
|
|
|
|
ASSERT_EQUALS(0, loc.line);
|
|
|
|
ASSERT_EQUALS(0, loc.column);
|
|
|
|
ASSERT_EQUALS("message", msg.shortMessage());
|
|
|
|
ASSERT_EQUALS("message", msg.verboseMessage());
|
|
|
|
ASSERT_EQUALS("[file.c:0]: (error) message", msg.toString(false));
|
|
|
|
ASSERT_EQUALS("[file.c:0]: (error) message", msg.toString(true));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
InternalError internalError(nullptr, "message", "details", InternalError::INTERNAL);
|
|
|
|
const auto msg = ErrorMessage::fromInternalError(internalError, nullptr, "file.cpp", "msg");
|
|
|
|
ASSERT_EQUALS(1, msg.callStack.size());
|
|
|
|
const auto &loc = *msg.callStack.cbegin();
|
|
|
|
ASSERT_EQUALS(0, loc.fileIndex);
|
|
|
|
ASSERT_EQUALS(0, loc.line);
|
|
|
|
ASSERT_EQUALS(0, loc.column);
|
|
|
|
ASSERT_EQUALS("msg: message", msg.shortMessage());
|
|
|
|
ASSERT_EQUALS("msg: message: details", msg.verboseMessage());
|
|
|
|
ASSERT_EQUALS("[file.cpp:0]: (error) msg: message", msg.toString(false));
|
|
|
|
ASSERT_EQUALS("[file.cpp:0]: (error) msg: message: details", msg.toString(true));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void CustomFormat() const {
|
2020-05-23 07:16:49 +02:00
|
|
|
std::list<ErrorMessage::FileLocation> locs(1, fooCpp5);
|
2021-02-24 22:00:06 +01:00
|
|
|
ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", Certainty::normal);
|
2019-08-18 12:19:05 +02:00
|
|
|
ASSERT_EQUALS(1, msg.callStack.size());
|
2010-12-05 21:23:36 +01:00
|
|
|
ASSERT_EQUALS("Programming error.", msg.shortMessage());
|
|
|
|
ASSERT_EQUALS("Verbose error", msg.verboseMessage());
|
|
|
|
ASSERT_EQUALS("foo.cpp:5,error,errorId,Programming error.", msg.toString(false, "{file}:{line},{severity},{id},{message}"));
|
|
|
|
ASSERT_EQUALS("foo.cpp:5,error,errorId,Verbose error", msg.toString(true, "{file}:{line},{severity},{id},{message}"));
|
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void CustomFormat2() const {
|
2020-05-23 07:16:49 +02:00
|
|
|
std::list<ErrorMessage::FileLocation> locs(1, fooCpp5);
|
2021-02-24 22:00:06 +01:00
|
|
|
ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", Certainty::normal);
|
2019-08-18 12:19:05 +02:00
|
|
|
ASSERT_EQUALS(1, msg.callStack.size());
|
2010-12-05 21:23:36 +01:00
|
|
|
ASSERT_EQUALS("Programming error.", msg.shortMessage());
|
|
|
|
ASSERT_EQUALS("Verbose error", msg.verboseMessage());
|
|
|
|
ASSERT_EQUALS("Programming error. - foo.cpp(5):(error,errorId)", msg.toString(false, "{message} - {file}({line}):({severity},{id})"));
|
|
|
|
ASSERT_EQUALS("Verbose error - foo.cpp(5):(error,errorId)", msg.toString(true, "{message} - {file}({line}):({severity},{id})"));
|
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void CustomFormatLocations() const {
|
2011-07-16 12:41:31 +02:00
|
|
|
// Check that first location from location stack is used in template
|
2020-05-23 07:16:49 +02:00
|
|
|
std::list<ErrorMessage::FileLocation> locs = { fooCpp5, barCpp8 };
|
2021-02-24 22:00:06 +01:00
|
|
|
ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", Certainty::normal);
|
2019-08-18 12:19:05 +02:00
|
|
|
ASSERT_EQUALS(2, msg.callStack.size());
|
2011-07-16 12:41:31 +02:00
|
|
|
ASSERT_EQUALS("Programming error.", msg.shortMessage());
|
|
|
|
ASSERT_EQUALS("Verbose error", msg.verboseMessage());
|
|
|
|
ASSERT_EQUALS("Programming error. - bar.cpp(8):(error,errorId)", msg.toString(false, "{message} - {file}({line}):({severity},{id})"));
|
|
|
|
ASSERT_EQUALS("Verbose error - bar.cpp(8):(error,errorId)", msg.toString(true, "{message} - {file}({line}):({severity},{id})"));
|
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void ToXmlV2() const {
|
2020-05-23 07:16:49 +02:00
|
|
|
std::list<ErrorMessage::FileLocation> locs(1, fooCpp5);
|
2021-02-24 22:00:06 +01:00
|
|
|
ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", Certainty::normal);
|
2011-02-04 20:14:19 +01:00
|
|
|
std::string header("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<results version=\"2\">\n");
|
2013-08-07 16:30:55 +02:00
|
|
|
header += " <cppcheck version=\"";
|
2011-02-03 08:44:23 +01:00
|
|
|
header += CppCheck::version();
|
2013-08-07 16:30:55 +02:00
|
|
|
header += "\"/>\n <errors>";
|
2022-07-07 17:35:13 +02:00
|
|
|
ASSERT_EQUALS(header, ErrorMessage::getXMLHeader(""));
|
2020-05-23 07:16:49 +02:00
|
|
|
ASSERT_EQUALS(" </errors>\n</results>", ErrorMessage::getXMLFooter());
|
2013-08-07 16:30:55 +02:00
|
|
|
std::string message(" <error id=\"errorId\" severity=\"error\"");
|
2011-02-03 08:44:23 +01:00
|
|
|
message += " msg=\"Programming error.\" verbose=\"Verbose error\">\n";
|
2019-08-18 12:19:05 +02:00
|
|
|
message += " <location file=\"foo.cpp\" line=\"5\" column=\"1\"/>\n </error>";
|
2017-07-29 18:56:22 +02:00
|
|
|
ASSERT_EQUALS(message, msg.toXML());
|
2011-07-16 12:41:31 +02:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void ToXmlV2Locations() const {
|
2020-05-23 07:16:49 +02:00
|
|
|
std::list<ErrorMessage::FileLocation> locs = { fooCpp5, barCpp8 };
|
2019-01-02 18:22:12 +01:00
|
|
|
locs.back().setinfo("ä");
|
2021-02-24 22:00:06 +01:00
|
|
|
ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", Certainty::normal);
|
2011-07-16 12:41:31 +02:00
|
|
|
std::string header("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<results version=\"2\">\n");
|
2013-08-07 16:30:55 +02:00
|
|
|
header += " <cppcheck version=\"";
|
2011-07-16 12:41:31 +02:00
|
|
|
header += CppCheck::version();
|
2013-08-07 16:30:55 +02:00
|
|
|
header += "\"/>\n <errors>";
|
2022-07-07 17:35:13 +02:00
|
|
|
ASSERT_EQUALS(header, ErrorMessage::getXMLHeader(""));
|
2020-05-23 07:16:49 +02:00
|
|
|
ASSERT_EQUALS(" </errors>\n</results>", ErrorMessage::getXMLFooter());
|
2013-08-07 16:30:55 +02:00
|
|
|
std::string message(" <error id=\"errorId\" severity=\"error\"");
|
2011-07-16 12:41:31 +02:00
|
|
|
message += " msg=\"Programming error.\" verbose=\"Verbose error\">\n";
|
2019-08-18 12:19:05 +02:00
|
|
|
message += " <location file=\"bar.cpp\" line=\"8\" column=\"1\" info=\"\\303\\244\"/>\n";
|
|
|
|
message += " <location file=\"foo.cpp\" line=\"5\" column=\"1\"/>\n </error>";
|
2017-07-29 18:56:22 +02:00
|
|
|
ASSERT_EQUALS(message, msg.toXML());
|
2011-02-03 08:44:23 +01:00
|
|
|
}
|
2011-04-14 03:12:35 +02:00
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void ToXmlV2Encoding() const {
|
2015-06-18 21:16:25 +02:00
|
|
|
{
|
2020-05-23 07:16:49 +02:00
|
|
|
std::list<ErrorMessage::FileLocation> locs;
|
2021-02-24 22:00:06 +01:00
|
|
|
ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nComparing \"\203\" with \"\003\"", "errorId", Certainty::normal);
|
2017-07-29 18:56:22 +02:00
|
|
|
const std::string expected(" <error id=\"errorId\" severity=\"error\" msg=\"Programming error.\" verbose=\"Comparing "\\203" with "\\003"\"/>");
|
|
|
|
ASSERT_EQUALS(expected, msg.toXML());
|
2015-06-18 21:16:25 +02:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const char code1[]="äöü";
|
|
|
|
const char code2[]="\x12\x00\x00\x01";
|
2020-05-23 07:16:49 +02:00
|
|
|
std::list<ErrorMessage::FileLocation> locs;
|
2021-02-24 22:00:06 +01:00
|
|
|
ErrorMessage msg1(locs, emptyString, Severity::error, std::string("Programming error.\nReading \"")+code1+"\"", "errorId", Certainty::normal);
|
2017-07-29 18:56:22 +02:00
|
|
|
ASSERT_EQUALS(" <error id=\"errorId\" severity=\"error\" msg=\"Programming error.\" verbose=\"Reading "\\303\\244\\303\\266\\303\\274"\"/>", msg1.toXML());
|
2021-02-24 22:00:06 +01:00
|
|
|
ErrorMessage msg2(locs, emptyString, Severity::error, std::string("Programming error.\nReading \"")+code2+"\"", "errorId", Certainty::normal);
|
2017-07-29 18:56:22 +02:00
|
|
|
ASSERT_EQUALS(" <error id=\"errorId\" severity=\"error\" msg=\"Programming error.\" verbose=\"Reading "\\022"\"/>", msg2.toXML());
|
2015-06-18 21:16:25 +02:00
|
|
|
}
|
2014-04-13 09:50:57 +02:00
|
|
|
}
|
|
|
|
|
2021-04-22 14:28:33 +02:00
|
|
|
void FromXmlV2() const {
|
|
|
|
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
|
2021-04-22 19:08:24 +02:00
|
|
|
"<error id=\"errorId\""
|
|
|
|
" severity=\"error\""
|
|
|
|
" cwe=\"123\""
|
|
|
|
" inconclusive=\"true\""
|
|
|
|
" msg=\"Programming error.\""
|
|
|
|
" verbose=\"Verbose error\""
|
|
|
|
" hash=\"456\""
|
|
|
|
">\n"
|
|
|
|
" <location file=\"bar.cpp\" line=\"8\" column=\"1\"/>\n"
|
|
|
|
" <location file=\"foo.cpp\" line=\"5\" column=\"2\"/>\n"
|
|
|
|
"</error>";
|
2021-04-22 14:28:33 +02:00
|
|
|
tinyxml2::XMLDocument doc;
|
2023-03-02 21:10:51 +01:00
|
|
|
ASSERT(doc.Parse(xmldata, sizeof(xmldata)) == tinyxml2::XML_SUCCESS);
|
2021-04-22 14:28:33 +02:00
|
|
|
ErrorMessage msg(doc.FirstChildElement());
|
|
|
|
ASSERT_EQUALS("errorId", msg.id);
|
2023-10-12 11:58:39 +02:00
|
|
|
ASSERT_EQUALS(static_cast<int>(Severity::error), static_cast<int>(msg.severity));
|
2021-04-22 14:28:33 +02:00
|
|
|
ASSERT_EQUALS(123u, msg.cwe.id);
|
2023-01-08 19:31:54 +01:00
|
|
|
ASSERT_EQUALS(static_cast<int>(Certainty::inconclusive), static_cast<int>(msg.certainty));
|
2021-04-22 14:28:33 +02:00
|
|
|
ASSERT_EQUALS("Programming error.", msg.shortMessage());
|
|
|
|
ASSERT_EQUALS("Verbose error", msg.verboseMessage());
|
|
|
|
ASSERT_EQUALS(456u, msg.hash);
|
|
|
|
ASSERT_EQUALS(2u, msg.callStack.size());
|
|
|
|
ASSERT_EQUALS("foo.cpp", msg.callStack.front().getfile());
|
|
|
|
ASSERT_EQUALS(5, msg.callStack.front().line);
|
|
|
|
ASSERT_EQUALS(2u, msg.callStack.front().column);
|
|
|
|
ASSERT_EQUALS("bar.cpp", msg.callStack.back().getfile());
|
|
|
|
ASSERT_EQUALS(8, msg.callStack.back().line);
|
|
|
|
ASSERT_EQUALS(1u, msg.callStack.back().column);
|
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void InconclusiveXml() const {
|
2011-04-10 21:51:27 +02:00
|
|
|
// Location
|
2020-05-23 07:16:49 +02:00
|
|
|
std::list<ErrorMessage::FileLocation> locs(1, fooCpp5);
|
2011-04-10 21:51:27 +02:00
|
|
|
|
2011-04-14 18:02:01 +02:00
|
|
|
// Inconclusive error message
|
2021-02-24 22:00:06 +01:00
|
|
|
ErrorMessage msg(locs, emptyString, Severity::error, "Programming error", "errorId", Certainty::inconclusive);
|
2011-04-10 21:51:27 +02:00
|
|
|
|
2012-03-25 12:51:10 +02:00
|
|
|
// xml version 2 error message
|
2013-08-07 16:30:55 +02:00
|
|
|
ASSERT_EQUALS(" <error id=\"errorId\" severity=\"error\" msg=\"Programming error\" verbose=\"Programming error\" inconclusive=\"true\">\n"
|
2019-08-18 12:19:05 +02:00
|
|
|
" <location file=\"foo.cpp\" line=\"5\" column=\"1\"/>\n"
|
2013-08-07 16:30:55 +02:00
|
|
|
" </error>",
|
2017-07-29 18:56:22 +02:00
|
|
|
msg.toXML());
|
2011-04-10 21:51:27 +02:00
|
|
|
}
|
2011-04-16 13:42:48 +02:00
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void SerializeInconclusiveMessage() const {
|
2011-04-16 13:42:48 +02:00
|
|
|
// Inconclusive error message
|
2020-05-23 07:16:49 +02:00
|
|
|
std::list<ErrorMessage::FileLocation> locs;
|
2021-02-24 22:00:06 +01:00
|
|
|
ErrorMessage msg(locs, emptyString, Severity::error, "Programming error", "errorId", Certainty::inconclusive);
|
2021-04-05 13:11:23 +02:00
|
|
|
msg.file0 = "test.cpp";
|
2022-12-07 18:00:45 +01:00
|
|
|
|
|
|
|
const std::string msg_str = msg.serialize();
|
2011-04-16 13:42:48 +02:00
|
|
|
ASSERT_EQUALS("7 errorId"
|
|
|
|
"5 error"
|
2015-04-25 17:48:11 +02:00
|
|
|
"1 0"
|
2020-07-14 22:30:42 +02:00
|
|
|
"1 0"
|
2021-04-05 13:11:23 +02:00
|
|
|
"8 test.cpp"
|
2011-04-16 13:42:48 +02:00
|
|
|
"12 inconclusive"
|
|
|
|
"17 Programming error"
|
|
|
|
"17 Programming error"
|
2022-12-07 18:00:45 +01:00
|
|
|
"0 ", msg_str);
|
2011-04-16 13:42:48 +02:00
|
|
|
|
|
|
|
ErrorMessage msg2;
|
2022-12-07 18:00:45 +01:00
|
|
|
ASSERT_NO_THROW(msg2.deserialize(msg_str));
|
2019-08-18 12:19:05 +02:00
|
|
|
ASSERT_EQUALS("errorId", msg2.id);
|
2023-10-12 11:58:39 +02:00
|
|
|
ASSERT_EQUALS(static_cast<int>(Severity::error), static_cast<int>(msg2.severity));
|
2021-04-05 13:11:23 +02:00
|
|
|
ASSERT_EQUALS("test.cpp", msg2.file0);
|
2023-01-08 19:31:54 +01:00
|
|
|
ASSERT_EQUALS(static_cast<int>(Certainty::inconclusive), static_cast<int>(msg2.certainty));
|
2011-04-16 13:42:48 +02:00
|
|
|
ASSERT_EQUALS("Programming error", msg2.shortMessage());
|
|
|
|
ASSERT_EQUALS("Programming error", msg2.verboseMessage());
|
|
|
|
}
|
2012-07-13 08:29:49 +02:00
|
|
|
|
2015-01-17 15:56:09 +01:00
|
|
|
void DeserializeInvalidInput() const {
|
2022-12-03 15:44:33 +01:00
|
|
|
{
|
|
|
|
// missing/invalid length
|
|
|
|
// missing separator
|
|
|
|
ErrorMessage msg;
|
2022-12-07 18:00:45 +01:00
|
|
|
ASSERT_THROW_EQUALS(msg.deserialize("500foobar"), InternalError, "Internal Error: Deserialization of error message failed - invalid separator");
|
2022-12-03 15:44:33 +01:00
|
|
|
}
|
|
|
|
{
|
|
|
|
// invalid length
|
|
|
|
ErrorMessage msg;
|
2022-12-07 18:00:45 +01:00
|
|
|
ASSERT_THROW_EQUALS(msg.deserialize("foo foobar"), InternalError, "Internal Error: Deserialization of error message failed - invalid length");
|
2022-12-03 15:44:33 +01:00
|
|
|
}
|
|
|
|
{
|
|
|
|
// mismatching length
|
|
|
|
ErrorMessage msg;
|
2022-12-07 18:00:45 +01:00
|
|
|
ASSERT_THROW_EQUALS(msg.deserialize("8 errorId"), InternalError, "Internal Error: Deserialization of error message failed - premature end of data");
|
2022-12-03 15:44:33 +01:00
|
|
|
}
|
|
|
|
{
|
|
|
|
// incomplete message
|
|
|
|
ErrorMessage msg;
|
2022-12-07 18:00:45 +01:00
|
|
|
ASSERT_THROW_EQUALS(msg.deserialize("7 errorId"), InternalError, "Internal Error: Deserialization of error message failed - invalid length");
|
2022-12-03 15:44:33 +01:00
|
|
|
}
|
|
|
|
{
|
|
|
|
// invalid CWE ID
|
|
|
|
const char str[] = "7 errorId"
|
|
|
|
"5 error"
|
2023-04-08 22:29:09 +02:00
|
|
|
"7 invalid" // cwe
|
2022-12-03 15:44:33 +01:00
|
|
|
"1 0"
|
|
|
|
"8 test.cpp"
|
|
|
|
"17 Programming error"
|
|
|
|
"17 Programming error"
|
|
|
|
"0 ";
|
|
|
|
ErrorMessage msg;
|
2023-04-08 22:29:09 +02:00
|
|
|
ASSERT_THROW_EQUALS(msg.deserialize(str), InternalError, "Internal Error: Deserialization of error message failed - invalid CWE ID - not an integer");
|
2022-12-03 15:44:33 +01:00
|
|
|
}
|
|
|
|
{
|
|
|
|
// invalid hash
|
|
|
|
const char str[] = "7 errorId"
|
|
|
|
"5 error"
|
|
|
|
"1 0"
|
2023-04-08 22:29:09 +02:00
|
|
|
"7 invalid" // hash
|
2022-12-03 15:44:33 +01:00
|
|
|
"8 test.cpp"
|
|
|
|
"17 Programming error"
|
|
|
|
"17 Programming error"
|
|
|
|
"0 ";
|
|
|
|
ErrorMessage msg;
|
2023-04-08 22:29:09 +02:00
|
|
|
ASSERT_THROW_EQUALS(msg.deserialize(str), InternalError, "Internal Error: Deserialization of error message failed - invalid hash - not an integer");
|
2022-12-03 15:44:33 +01:00
|
|
|
}
|
2022-12-09 19:34:51 +01:00
|
|
|
{
|
|
|
|
// out-of-range CWE ID
|
|
|
|
const char str[] = "7 errorId"
|
|
|
|
"5 error"
|
|
|
|
"5 65536" // max +1
|
|
|
|
"1 0"
|
|
|
|
"8 test.cpp"
|
|
|
|
"17 Programming error"
|
|
|
|
"17 Programming error"
|
|
|
|
"0 ";
|
|
|
|
ErrorMessage msg;
|
|
|
|
ASSERT_THROW(msg.deserialize(str), InternalError);
|
|
|
|
}
|
2015-01-17 15:56:09 +01:00
|
|
|
}
|
|
|
|
|
2015-01-17 15:58:36 +01:00
|
|
|
void SerializeSanitize() const {
|
2020-05-23 07:16:49 +02:00
|
|
|
std::list<ErrorMessage::FileLocation> locs;
|
2021-02-24 22:00:06 +01:00
|
|
|
ErrorMessage msg(locs, emptyString, Severity::error, std::string("Illegal character in \"foo\001bar\""), "errorId", Certainty::normal);
|
2021-04-05 13:11:23 +02:00
|
|
|
msg.file0 = "1.c";
|
2015-01-17 15:58:36 +01:00
|
|
|
|
2022-12-07 18:00:45 +01:00
|
|
|
const std::string msg_str = msg.serialize();
|
2015-04-25 17:48:11 +02:00
|
|
|
ASSERT_EQUALS("7 errorId"
|
|
|
|
"5 error"
|
|
|
|
"1 0"
|
2020-07-14 22:30:42 +02:00
|
|
|
"1 0"
|
2021-04-05 13:11:23 +02:00
|
|
|
"3 1.c"
|
2015-04-25 17:48:11 +02:00
|
|
|
"33 Illegal character in \"foo\\001bar\""
|
|
|
|
"33 Illegal character in \"foo\\001bar\""
|
2022-12-07 18:00:45 +01:00
|
|
|
"0 ", msg_str);
|
2015-01-17 15:58:36 +01:00
|
|
|
|
|
|
|
ErrorMessage msg2;
|
2022-12-07 18:00:45 +01:00
|
|
|
ASSERT_NO_THROW(msg2.deserialize(msg_str));
|
2019-08-18 12:19:05 +02:00
|
|
|
ASSERT_EQUALS("errorId", msg2.id);
|
2023-10-12 11:58:39 +02:00
|
|
|
ASSERT_EQUALS(static_cast<int>(Severity::error), static_cast<int>(msg2.severity));
|
2021-04-05 13:11:23 +02:00
|
|
|
ASSERT_EQUALS("1.c", msg2.file0);
|
2015-01-17 15:58:36 +01:00
|
|
|
ASSERT_EQUALS("Illegal character in \"foo\\001bar\"", msg2.shortMessage());
|
|
|
|
ASSERT_EQUALS("Illegal character in \"foo\\001bar\"", msg2.verboseMessage());
|
|
|
|
}
|
|
|
|
|
2019-08-18 09:06:40 +02:00
|
|
|
void SerializeFileLocation() const {
|
2020-05-23 07:16:49 +02:00
|
|
|
ErrorMessage::FileLocation loc1(":/,;", 654, 33);
|
2019-08-18 16:33:32 +02:00
|
|
|
loc1.setfile("[]:;,()");
|
2019-08-18 09:06:40 +02:00
|
|
|
loc1.setinfo("abcd:/,");
|
|
|
|
|
2020-05-23 07:16:49 +02:00
|
|
|
std::list<ErrorMessage::FileLocation> locs{loc1};
|
2019-08-18 09:07:57 +02:00
|
|
|
|
2021-02-24 22:00:06 +01:00
|
|
|
ErrorMessage msg(locs, emptyString, Severity::error, "Programming error", "errorId", Certainty::inconclusive);
|
2019-08-18 09:06:40 +02:00
|
|
|
|
2022-12-07 18:00:45 +01:00
|
|
|
const std::string msg_str = msg.serialize();
|
|
|
|
ASSERT_EQUALS("7 errorId"
|
|
|
|
"5 error"
|
|
|
|
"1 0"
|
|
|
|
"1 0"
|
|
|
|
"0 "
|
|
|
|
"12 inconclusive"
|
|
|
|
"17 Programming error"
|
|
|
|
"17 Programming error"
|
|
|
|
"1 "
|
|
|
|
"27 654\t33\t[]:;,()\t:/,;\tabcd:/,", msg_str);
|
|
|
|
|
2019-08-18 09:06:40 +02:00
|
|
|
ErrorMessage msg2;
|
2022-12-07 18:00:45 +01:00
|
|
|
ASSERT_NO_THROW(msg2.deserialize(msg_str));
|
2019-08-18 16:33:32 +02:00
|
|
|
ASSERT_EQUALS("[]:;,()", msg2.callStack.front().getfile(false));
|
|
|
|
ASSERT_EQUALS(":/,;", msg2.callStack.front().getOrigFile(false));
|
|
|
|
ASSERT_EQUALS(654, msg2.callStack.front().line);
|
2019-08-18 12:19:05 +02:00
|
|
|
ASSERT_EQUALS(33, msg2.callStack.front().column);
|
|
|
|
ASSERT_EQUALS("abcd:/,", msg2.callStack.front().getinfo());
|
2019-08-18 09:06:40 +02:00
|
|
|
}
|
|
|
|
|
2023-04-08 22:19:52 +02:00
|
|
|
void substituteTemplateFormatStatic() const
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::string s;
|
|
|
|
::substituteTemplateFormatStatic(s);
|
|
|
|
ASSERT_EQUALS("", s);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
std::string s = "template{black}\\z";
|
|
|
|
::substituteTemplateFormatStatic(s);
|
|
|
|
ASSERT_EQUALS("template{black}\\z", s);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
std::string s = "{reset}{bold}{dim}{red}{blue}{magenta}{default}\\b\\n\\r\\t";
|
|
|
|
::substituteTemplateFormatStatic(s);
|
|
|
|
ASSERT_EQUALS("\b\n\r\t", s);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
std::string s = "\\\\n";
|
|
|
|
::substituteTemplateFormatStatic(s);
|
|
|
|
ASSERT_EQUALS("\\\n", s);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
std::string s = "{{red}";
|
|
|
|
::substituteTemplateFormatStatic(s);
|
|
|
|
ASSERT_EQUALS("{", s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void substituteTemplateLocationStatic() const
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::string s;
|
|
|
|
::substituteTemplateLocationStatic(s);
|
|
|
|
ASSERT_EQUALS("", s);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
std::string s = "template";
|
|
|
|
::substituteTemplateLocationStatic(s);
|
|
|
|
ASSERT_EQUALS("template", s);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
std::string s = "template{black}\\z";
|
|
|
|
::substituteTemplateFormatStatic(s);
|
|
|
|
ASSERT_EQUALS("template{black}\\z", s);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
std::string s = "{reset}{bold}{dim}{red}{blue}{magenta}{default}\\b\\n\\r\\t";
|
|
|
|
::substituteTemplateFormatStatic(s);
|
|
|
|
ASSERT_EQUALS("\b\n\r\t", s);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
std::string s = "\\\\n";
|
|
|
|
::substituteTemplateFormatStatic(s);
|
|
|
|
ASSERT_EQUALS("\\\n", s);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
std::string s = "{{red}";
|
|
|
|
::substituteTemplateFormatStatic(s);
|
|
|
|
ASSERT_EQUALS("{", s);
|
|
|
|
}
|
|
|
|
}
|
2010-12-05 21:23:36 +01:00
|
|
|
};
|
2015-10-18 20:34:38 +02:00
|
|
|
|
2010-12-05 21:23:36 +01:00
|
|
|
REGISTER_TEST(TestErrorLogger)
|