• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 16788529396

06 Aug 2025 09:04PM UTC coverage: 90.677%. Remained the same
16788529396

Pull #4255

github

web-flow
Merge 09544fbc3 into 1f9987555
Pull Request #4255: Add support for building with clang-cl

99983 of 110263 relevant lines covered (90.68%)

24310874.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.48
/src/tests/runner/test_xml_reporter.cpp
1
/*
2
* (C) 2022 Jack Lloyd
3
* (C) 2022 René Meusel, Rohde & Schwarz Cybersecurity
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include "test_xml_reporter.h"
9

10
#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
11

12
   #include <botan/build.h>
13
   #include <botan/version.h>
14
   #include <botan/internal/loadstor.h>
15
   #include <botan/internal/target_info.h>
16

17
   #if defined(BOTAN_HAS_OS_UTILS)
18
      #include <botan/internal/os_utils.h>
19
   #endif
20

21
   #include <iomanip>
22
   #include <numeric>
23
   #include <sstream>
24

25
namespace Botan_Tests {
26

27
namespace {
28

29
std::string full_compiler_version_string() {
2✔
30
   #if defined(__VERSION__)
31
   return __VERSION__;
2✔
32

33
   #elif defined(BOTAN_BUILD_COMPILER_IS_MSVC)
34
   // See https://learn.microsoft.com/en-us/cpp/preprocessor/predefined-macros
35
   //    If the version number of the Microsoft C/C++ compiler is 15.00.20706.01,
36
   //    the _MSC_FULL_VER macro evaluates to 150020706.
37
   constexpr int major = _MSC_FULL_VER / 10000000;
38
   constexpr int minor = (_MSC_FULL_VER % 10000000) / 100000;
39
   constexpr int patch = _MSC_FULL_VER % 100000;
40
   constexpr int build = _MSC_BUILD;
41

42
   std::ostringstream oss;
43

44
   oss << std::setfill('0') << std::setw(2) << major << "." << std::setw(2) << minor << "." << std::setw(5) << patch
45
       << "." << std::setw(2) << build << "\n";
46

47
   return oss.str();
48
   #else
49
   return "unknown";
50
   #endif
51
}
52

53
std::string full_compiler_name_string() {
2✔
54
   #if defined(BOTAN_BUILD_COMPILER_IS_XCODE)
55
   return "xcode";
56
   #elif defined(BOTAN_BUILD_COMPILER_IS_CLANG)
57
   return "clang";
58
   #elif defined(BOTAN_BUILD_COMPILER_IS_CLANGCL)
59
   return "clangcl";
60
   #elif defined(BOTAN_BUILD_COMPILER_IS_GCC)
61
   return "gcc";
2✔
62
   #elif defined(BOTAN_BUILD_COMPILER_IS_MSVC)
63
   return "Microsoft Visual C++";
64
   #else
65
   return "unknown";
66
   #endif
67
}
68

69
/// formats a given time point in ISO 8601 format (with time zone)
70
std::string format(const std::chrono::system_clock::time_point& tp) {
5,784✔
71
   auto seconds_since_epoch = std::chrono::system_clock::to_time_t(tp);
5,784✔
72
   #if defined(BOTAN_HAS_OS_UTILS)
73
   return Botan::OS::format_time(seconds_since_epoch, "%FT%T%z");
11,568✔
74
   #else
75
   return std::to_string(seconds_since_epoch);
76
   #endif
77
}
78

79
std::string format(const std::chrono::nanoseconds& dur) {
3,458✔
80
   const double secs = static_cast<double>(dur.count()) / 1000000000.0;
3,458✔
81

82
   std::ostringstream out;
3,458✔
83
   out.precision(3);
3,458✔
84
   out << std::fixed << secs;
3,458✔
85
   return out.str();
6,916✔
86
}
3,458✔
87

88
}  // namespace
89

90
XmlReporter::XmlReporter(const Test_Options& opts, std::string output_dir) :
2✔
91
      Reporter(opts), m_output_dir(std::move(output_dir)) {
2✔
92
   set_property("architecture", BOTAN_TARGET_ARCH);
4✔
93
   set_property("compiler", full_compiler_name_string());
4✔
94
   set_property("compiler_version", full_compiler_version_string());
4✔
95
   set_property("timestamp", format(std::chrono::system_clock::now()));
4✔
96
   auto custom_props = opts.report_properties();
2✔
97
   for(const auto& prop : custom_props) {
6✔
98
      set_property(prop.first, prop.second);
4✔
99
   }
100
}
2✔
101

102
void XmlReporter::render() const {
2✔
103
   BOTAN_STATE_CHECK(m_outfile.has_value() && m_outfile->good());
2✔
104

105
   render_preamble(m_outfile.value());
2✔
106
   render_testsuites(m_outfile.value());
2✔
107
}
2✔
108

109
std::string XmlReporter::get_unique_output_filename() const {
2✔
110
   const uint64_t ts = Botan_Tests::Test::timestamp();
2✔
111
   std::vector<uint8_t> seed(8);
2✔
112
   Botan::store_be(ts, seed.data());
2✔
113

114
   std::stringstream ss;
2✔
115
   ss << m_output_dir << "/"
2✔
116
      << "Botan-" << Botan::short_version_string() << "-tests-" << Botan::hex_encode(seed, false) << ".xml";
6✔
117

118
   return ss.str();
2✔
119
}
4✔
120

121
void XmlReporter::next_run() {
2✔
122
   if(m_outfile.has_value()) {
2✔
123
      m_outfile.reset();
×
124
   }
125

126
   set_property("current test run", std::to_string(current_test_run()));
4✔
127
   set_property("total test runs", std::to_string(total_test_runs()));
4✔
128
   const auto file = get_unique_output_filename();
2✔
129
   m_outfile = std::ofstream(file, std::ofstream::out | std::ofstream::trunc);
2✔
130

131
   if(!m_outfile->good()) {
2✔
132
      std::stringstream ss;
×
133
      ss << "Failed to open '" << file << "' for writing JUnit report.";
×
134
      throw Botan::System_Error(ss.str());
×
135
   }
×
136
}
2✔
137

138
// == == == == == == == == == == == == == == == == == == == == == == == == == ==
139
// XML Rendering
140
// == == == == == == == == == == == == == == == == == == == == == == == == == ==
141

142
namespace {
143

144
void replace(std::string& str, const std::string& from, const std::string& to) {
59,460✔
145
   if(from.empty()) {
59,460✔
146
      return;
147
   }
148

149
   for(size_t offset = 0, pos = 0; (pos = str.find(from, offset)) != std::string::npos; offset = pos + to.size()) {
59,578✔
150
      str.replace(pos, from.size(), to);
118✔
151
   }
152
}
153

154
std::string escape(std::string str) {
10,788✔
155
   replace(str, "&", "&amp;");
21,576✔
156
   replace(str, "<", "&lt;");
21,576✔
157
   replace(str, ">", "&gt;");
21,576✔
158
   replace(str, "\"", "&quot;");
21,576✔
159
   replace(str, "'", "&apos;");
21,576✔
160
   return str;
10,788✔
161
}
162

163
std::string format_cdata(std::string str) {
5,520✔
164
   // XML CDATA payloads are not evaluated, hence no special character encoding
165
   // is needed.
166
   // Though the termination sequence (i.e. ']]>') must not appear in
167
   // a CDATA payload frame. The only way to escape it is to terminate the CDATA
168
   // sequence and break the payload's termination sequence into the adjacent
169
   // CDATA frames.
170
   //
171
   //   See: https://stackoverflow.com/a/223782
172
   replace(str, "]]>", "]]]><![CDATA[]>");
11,040✔
173
   //            ^^^ -> ^~~~~~~~~~~~~^^
174

175
   // wrap the (escaped) payload into a CDATA frame
176
   std::ostringstream out;
5,520✔
177
   out << "<![CDATA[" << str << "]]>";
5,520✔
178
   return out.str();
11,040✔
179
}
5,520✔
180

181
}  // namespace
182

183
void XmlReporter::render_preamble(std::ostream& out) const {
2✔
184
   out << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
2✔
185
}
×
186

187
void XmlReporter::render_properties(std::ostream& out) const {
2✔
188
   if(properties().empty()) {
2✔
189
      return;
190
   }
191

192
   out << "<properties>\n";
2✔
193
   for(const auto& prop : properties()) {
24✔
194
      out << "<property"
22✔
195
          << " name=\"" << escape(prop.first) << "\""
44✔
196
          << " value=\"" << escape(prop.second) << "\""
44✔
197
          << " />\n";
66✔
198
   }
199
   out << "</properties>\n";
2✔
200
}
201

202
void XmlReporter::render_testsuites(std::ostream& out) const {
2✔
203
   // render an empty testsuites tag even if no tests were run
204
   out << "<testsuites"
2✔
205
       << " tests=\"" << tests_run() << "\""
2✔
206
       << " failures=\"" << tests_failed() << "\""
2✔
207
       << " time=\"" << format(elapsed_time()) << "\">\n";
4✔
208

209
   // Note: In the JUnit .xsd spec, <properties> appear only in individual
210
   //       test cases. This deviation from the spec allows us to embed
211
   //       specific platform information about this particular test run.
212
   render_properties(out);
2✔
213

214
   for(const auto& suite : testsuites()) {
822✔
215
      render_testsuite(out, suite.second);
820✔
216
   }
217

218
   out << "</testsuites>\n";
2✔
219
}
2✔
220

221
void XmlReporter::render_testsuite(std::ostream& out, const Testsuite& suite) const {
820✔
222
   out << "<testsuite"
820✔
223
       << " name=\"" << escape(suite.name()) << "\""
1,640✔
224
       << " tests=\"" << suite.tests_run() << "\""
1,640✔
225
       << " failures=\"" << suite.tests_failed() << "\""
820✔
226
       << " timestamp=\"" << format(suite.timestamp()) << "\"";
2,460✔
227

228
   const auto elapsed = suite.elapsed_time();
820✔
229
   if(elapsed.has_value()) {
820✔
230
      out << " time=\"" << format(elapsed.value()) << "\"";
1,614✔
231
   }
232

233
   if(suite.results().empty()) {
820✔
234
      out << " />\n";
×
235
   } else {
236
      out << ">\n";
820✔
237

238
      for(const auto& result : suite.results()) {
5,782✔
239
         render_testcase(out, result);
4,962✔
240
      }
241

242
      out << "</testsuite>\n";
820✔
243
   }
244
}
820✔
245

246
void XmlReporter::render_testcase(std::ostream& out, const TestSummary& test) const {
4,962✔
247
   out << "<testcase"
4,962✔
248
       << " name=\"" << escape(test.name()) << "\""
9,924✔
249
       << " assertions=\"" << test.assertions() << "\""
9,924✔
250
       << " timestamp=\"" << format(test.timestamp()) << "\"";
14,886✔
251

252
   if(test.elapsed_time().has_value()) {
4,962✔
253
      out << " time=\"" << format(test.elapsed_time().value()) << "\"";
8,754✔
254
   }
255

256
   if(test.code_location().has_value()) {
4,962✔
257
      out << " file=\"" << escape(test.code_location()->path) << "\""
9,924✔
258
          << " line=\"" << test.code_location()->line << "\"";
9,924✔
259
   }
260

261
   if(test.passed() && test.notes().empty()) {
4,962✔
262
      out << " />\n";
4,716✔
263
   } else {
264
      out << ">\n";
246✔
265
      render_failures_and_stdout(out, test);
246✔
266
      out << "</testcase>\n";
246✔
267
   }
268
}
4,962✔
269

270
void XmlReporter::render_failures_and_stdout(std::ostream& out, const TestSummary& test) const {
246✔
271
   for(const auto& failure : test.failures()) {
246✔
272
      out << "<failure>\n"
×
273
          << format_cdata(failure) << "\n"
×
274
          << "</failure>\n";
×
275
   }
276

277
   // xUnit format does not have a special tag for test notes, hence we
278
   // render it into the freetext 'system-out'
279
   if(!test.notes().empty()) {
246✔
280
      out << "<system-out>\n";
246✔
281
      for(const auto& note : test.notes()) {
5,766✔
282
         out << format_cdata(note) << '\n';
16,560✔
283
      }
284
      out << "</system-out>\n";
246✔
285
   }
286
}
246✔
287

288
}  // namespace Botan_Tests
289

290
#endif  // defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc