2020#include " analyzerinfo.h"
2121#include " filesettings.h"
2222#include " fixture.h"
23+ #include " xml.h"
2324
2425#include < sstream>
26+ #include < tinyxml2.h>
2527
2628class TestAnalyzerInformation : public TestFixture , private AnalyzerInformation {
2729public:
@@ -34,6 +36,7 @@ class TestAnalyzerInformation : public TestFixture, private AnalyzerInformation
3436 TEST_CASE (duplicateFile);
3537 TEST_CASE (filesTextDuplicateFile);
3638 TEST_CASE (parse);
39+ TEST_CASE (skipAnalysis);
3740 }
3841
3942 void getAnalyzerInfoFile () const {
@@ -95,6 +98,116 @@ class TestAnalyzerInformation : public TestFixture, private AnalyzerInformation
9598 ASSERT_EQUALS (0 , info.fileIndex );
9699 ASSERT_EQUALS (" C:/dm/cppcheck-fix-13333/test/cli/whole-program/odr1.cpp" , info.sourceFile );
97100 }
101+
102+ void skipAnalysis () const {
103+ // Matching hash with license error (don't skip)
104+ {
105+ std::list<ErrorMessage> errorList;
106+ tinyxml2::XMLDocument doc;
107+
108+ const tinyxml2::XMLError xmlError = doc.Parse (
109+ " <?xml version=\" 1.0\" ?>"
110+ " <analyzerinfo hash=\" 100\" >"
111+ " <error id=\" premium-invalidLicense\" severity=\" error\" msg=\" Invalid license: No license file was found, contact sales@cppchecksolutions.com\" verbose=\" Invalid license: No license file was found, contact sales@cppchecksolutions.com\" file0=\" test.c\" >"
112+ " <location file=\" Cppcheck Premium\" line=\" 0\" column=\" 0\" />"
113+ " </error>"
114+ " </analyzerinfo>"
115+ );
116+ ASSERT_EQUALS (xmlError, tinyxml2::XML_SUCCESS);
117+
118+ ASSERT_EQUALS (AnalyzerInformation::skipAnalysis (doc, 100 , errorList), false );
119+ ASSERT_EQUALS (errorList.size (), 0 );
120+ }
121+
122+ // Matching hash with internal error (don't skip)
123+ {
124+ std::list<ErrorMessage> errorList;
125+ tinyxml2::XMLDocument doc;
126+
127+ const tinyxml2::XMLError xmlError = doc.Parse (
128+ " <?xml version=\" 1.0\" ?>"
129+ " <analyzerinfo hash=\" 100\" >"
130+ " <error id=\" internal\" severity=\" error\" msg=\" Something went wrong\" verbose=\" Something went wrong\" file0=\" test.c\" >"
131+ " <location file=\" Cppcheck\" line=\" 0\" column=\" 0\" />"
132+ " </error>"
133+ " </analyzerinfo>"
134+ );
135+ ASSERT_EQUALS (xmlError, tinyxml2::XML_SUCCESS);
136+
137+ ASSERT_EQUALS (AnalyzerInformation::skipAnalysis (doc, 100 , errorList), false );
138+ ASSERT_EQUALS (errorList.size (), 0 );
139+ }
140+
141+ // Matching hash with normal error (skip)
142+ {
143+ std::list<ErrorMessage> errorList;
144+ tinyxml2::XMLDocument doc;
145+
146+ const tinyxml2::XMLError xmlError = doc.Parse (
147+ " <?xml version=\" 1.0\" ?>"
148+ " <analyzerinfo hash=\" 100\" >"
149+ " <error id=\" nullPointer\" severity=\" error\" msg=\" Null pointer dereference: ptr\" verbose=\" Null pointer dereference: ptr\" cwe=\" 476\" file0=\" test.c\" >"
150+ " <location file=\" test.c\" line=\" 4\" column=\" 3\" info=\" Null pointer dereference\" />"
151+ " <location file=\" test.c\" line=\" 3\" column=\" 12\" info=\" Assignment 'ptr=NULL', assigned value is 0\" />"
152+ " <symbol>ptr</symbol>"
153+ " </error>"
154+ " </analyzerinfo>"
155+ );
156+ ASSERT_EQUALS (xmlError, tinyxml2::XML_SUCCESS);
157+
158+ ASSERT_EQUALS (AnalyzerInformation::skipAnalysis (doc, 100 , errorList), true );
159+ ASSERT_EQUALS (errorList.size (), 1 );
160+ }
161+
162+ // Matching hash with no error (skip)
163+ {
164+ std::list<ErrorMessage> errorList;
165+ tinyxml2::XMLDocument doc;
166+
167+ const tinyxml2::XMLError xmlError = doc.Parse (
168+ " <?xml version=\" 1.0\" ?>"
169+ " <analyzerinfo hash=\" 100\" >"
170+ " </analyzerinfo>"
171+ );
172+ ASSERT_EQUALS (xmlError, tinyxml2::XML_SUCCESS);
173+
174+ ASSERT_EQUALS (AnalyzerInformation::skipAnalysis (doc, 100 , errorList), true );
175+ ASSERT_EQUALS (errorList.size (), 0 );
176+ }
177+
178+ // Different hash with normal error (don't skip)
179+ {
180+ std::list<ErrorMessage> errorList;
181+ tinyxml2::XMLDocument doc;
182+
183+ const tinyxml2::XMLError xmlError = doc.Parse (
184+ " <?xml version=\" 1.0\" ?>"
185+ " <analyzerinfo hash=\" 100\" >"
186+ " <error id=\" nullPointer\" severity=\" error\" msg=\" Null pointer dereference: ptr\" verbose=\" Null pointer dereference: ptr\" cwe=\" 476\" file0=\" test.c\" >"
187+ " <location file=\" test.c\" line=\" 4\" column=\" 3\" info=\" Null pointer dereference\" />"
188+ " <location file=\" test.c\" line=\" 3\" column=\" 12\" info=\" Assignment 'ptr=NULL', assigned value is 0\" />"
189+ " <symbol>ptr</symbol>"
190+ " </error>"
191+ " </analyzerinfo>"
192+ );
193+ ASSERT_EQUALS (xmlError, tinyxml2::XML_SUCCESS);
194+
195+ ASSERT_EQUALS (AnalyzerInformation::skipAnalysis (doc, 99 , errorList), false );
196+ ASSERT_EQUALS (errorList.size (), 0 );
197+ }
198+
199+ // Empty document (don't skip)
200+ {
201+ std::list<ErrorMessage> errorList;
202+ tinyxml2::XMLDocument doc;
203+
204+ const tinyxml2::XMLError xmlError = doc.Parse (" " );
205+ ASSERT_EQUALS (xmlError, tinyxml2::XML_ERROR_EMPTY_DOCUMENT);
206+
207+ ASSERT_EQUALS (AnalyzerInformation::skipAnalysis (doc, 100 , errorList), false );
208+ ASSERT_EQUALS (errorList.size (), 0 );
209+ }
210+ }
98211};
99212
100213REGISTER_TEST (TestAnalyzerInformation)
0 commit comments