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

randombit / botan / 16809535423

07 Aug 2025 11:50AM UTC coverage: 90.675% (-0.002%) from 90.677%
16809535423

push

github

web-flow
Merge pull request #4255 from solemnwarning/clang-cl

Add support for building with clang-cl

99981 of 110263 relevant lines covered (90.68%)

12119499.5 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() {
1✔
30
   #if defined(__VERSION__)
31
   return __VERSION__;
1✔
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() {
1✔
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";
1✔
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) {
2,892✔
71
   auto seconds_since_epoch = std::chrono::system_clock::to_time_t(tp);
2,892✔
72
   #if defined(BOTAN_HAS_OS_UTILS)
73
   return Botan::OS::format_time(seconds_since_epoch, "%FT%T%z");
5,784✔
74
   #else
75
   return std::to_string(seconds_since_epoch);
76
   #endif
77
}
78

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

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

88
}  // namespace
89

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

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

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

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

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

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

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

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

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

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

142
namespace {
143

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

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

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

163
std::string format_cdata(std::string str) {
2,760✔
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[]>");
5,520✔
173
   //            ^^^ -> ^~~~~~~~~~~~~^^
174

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

181
}  // namespace
182

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

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

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

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

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

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

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

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

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

238
      for(const auto& result : suite.results()) {
2,891✔
239
         render_testcase(out, result);
2,481✔
240
      }
241

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

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

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

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

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

270
void XmlReporter::render_failures_and_stdout(std::ostream& out, const TestSummary& test) const {
123✔
271
   for(const auto& failure : test.failures()) {
123✔
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()) {
123✔
280
      out << "<system-out>\n";
123✔
281
      for(const auto& note : test.notes()) {
2,883✔
282
         out << format_cdata(note) << '\n';
8,280✔
283
      }
284
      out << "</system-out>\n";
123✔
285
   }
286
}
123✔
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