File: unit_tester.cc

package info (click to toggle)
monotone 0.48-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 20,096 kB
  • ctags: 8,077
  • sloc: cpp: 81,000; sh: 6,402; perl: 1,241; lisp: 1,045; makefile: 655; python: 566; sql: 112; ansic: 52
file content (371 lines) | stat: -rw-r--r-- 10,214 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Copyright (C) 2002 Graydon Hoare <graydon@pobox.com>
//
// This program is made available under the GNU GPL version 2.0 or
// greater. See the accompanying file COPYING for details.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE.

#include "base.hh"
#include <map>
#include "vector.hh"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <cerrno>

#include <boost/function.hpp>
#include <botan/botan.h>

#include "option.hh"
#include "unit_tests.hh"
#include "sanity.hh"
#include "current_exception.hh"
#include "botan_pipe_cache.hh"

using std::map;
using std::pair;
using std::make_pair;
using std::vector;
using std::string;
using std::cout;
using std::cerr;
using std::clog;
using std::exit;

typedef unit_test::unit_test_case test_t;
typedef map<string const, test_t const *> test_list_t;
typedef map<string const, test_list_t> group_list_t;

// This is used by other global constructors, so initialize on demand.
static group_list_t & unit_tests()
{
  static group_list_t tests;
  return tests;
}

// this is effectively basename(), but has no dependencies on
// e.g. system_path being usable yet.  The special-case for 'fis'
// ensures that the unit tests of the unit tester itself will be
// run first (see below).
static string
extract_group(char const *filename, bool fis)
{
  string result(filename);
  string::size_type last_slash = result.find_last_of("/\\");
  if (last_slash != string::npos)
    result.erase(0, last_slash+1);
  string::size_type first_dot = result.find_first_of('.');
  if (first_dot != string::npos)
    result.erase(first_dot, string::npos);
  if (fis)
    result.insert(0U, 1, '_');
  return result;
}

unit_test::unit_test_case::unit_test_case(char const * g,
                                          char const * n,
                                          void (*f)(),
                                          bool fis)
  : group(extract_group(g, fis)), name(n), func(f), failure_is_success(fis)
{
  // All unit_test_case objects are statically allocated so this is safe.
  unit_tests()[group][name] = this;
}

// Test state.
static bool this_test_failed = false;

namespace { struct require_failed {}; }

static void log_state(char const * file, int line,
                      char const * kind, char const * msg)
{
  L(FL("%s:%s: %s: %s") % file % line % kind % msg);
}

// Report what we can about a fatal exception (caught in the outermost catch
// handlers) which is from the std::exception hierarchy.  In this case we
// can access the exception object.
static void log_exception(std::exception const & ex)
{
  using std::strcmp;
  using std::strncmp;
  char const * ex_name = typeid(ex).name();
  char const * ex_dem  = demangle_typename(ex_name);
  char const * ex_what = ex.what();

  if (ex_dem == 0)
    ex_dem = ex_name;

  // some demanglers stick "class" at the beginning of their output,
  // which looks dumb in this context
  if (!strncmp(ex_dem, "class ", 6))
    ex_dem += 6;

  // only print what() if it's interesting, i.e. nonempty and different
  // from the name (mangled or otherwise) of the exception type.
  if (ex_what == 0 || ex_what[0] == 0
      || !strcmp(ex_what, ex_name)
      || !strcmp(ex_what, ex_dem))
    L(FL("UNCAUGHT EXCEPTION: %s") % ex_dem);
  else
    L(FL("UNCAUGHT EXCEPTION: %s: %s") % ex_dem % ex_what);
}

// Report what we can about a fatal exception (caught in the outermost catch
// handlers) which is of unknown type.  If we have the <cxxabi.h> interfaces,
// we can at least get the type_info object.
static void
log_exception()
{
  std::type_info *ex_type = get_current_exception_type();
  if (ex_type)
    {
      char const * ex_name = ex_type->name();
      char const * ex_dem  = demangle_typename(ex_name);
      if (ex_dem == 0)
        ex_dem = ex_name;
      L(FL("UNCAUGHT EXCEPTION: %s") % ex_dem);
    }
  else
    L(FL("UNCAUGHT EXCEPTION: unknown type"));
}

void unit_test::do_check(bool checkval, char const * file,
                         int line, char const * message)
{
  if (!checkval)
    {
      this_test_failed = true;
      log_state(file, line, "CHECK FAILED", message);
    }
  else
    log_state(file, line, "CHECK OK", message);
}

void unit_test::do_require(bool checkval, char const * file,
                           int line, char const * message)
{
  if (!checkval)
    {
      this_test_failed = true;
      log_state(file, line, "REQUIRE FAILED", message);
      throw require_failed();
    }
  else
    log_state(file, line, "REQUIRE OK", message);
}

void unit_test::do_checkpoint(char const * file, int line,
                              char const * message)
{
  log_state(file, line, "CHECKPOINT", message);
}

// define the global objects needed by botan_pipe_cache.hh
pipe_cache_cleanup * global_pipe_cleanup_object;
Botan::Pipe * unfiltered_pipe;
static unsigned char unfiltered_pipe_cleanup_mem[sizeof(cached_botan_pipe)];

int main(int argc, char * argv[])
{
  bool help(false);
  string test_to_run;

  global_sanity.initialize(argc, argv, "C");  // we didn't call setlocale

  try
    {
      option::concrete_option_set os;
      os("help,h", "display help message", option::setter(help))
        ("--", "", option::setter(test_to_run));

      os.from_command_line(argc, argv);

      if (help)
        {
          cout << (FL("Usage: %s [-h|--help] [test]\n"
                      "  With no arguments, lists all test cases.\n"
                      "  With the name of a test case, runs that test.\n"
                      "  -h or --help prints this message.\n") % argv[0]);
          return 0;
        }
    }
  catch (option::option_error const & e)
    {
      cerr << argv[0] << ": " << e.what() << '\n';
      return 2;
    }

  if (test_to_run == "")
    {
      for (group_list_t::const_iterator i = unit_tests().begin();
           i != unit_tests().end(); i++)
        for (test_list_t::const_iterator j = i->second.begin();
             j != i->second.end(); ++j)
          cout << i->first << ":" << j->first << "\n";

      return 0;
    }


  // set up some global state before running the tests
  // keep this in sync with monotone.cc, except for selftest=1 here, =0 there
  Botan::LibraryInitializer acquire_botan("thread_safe=0 selftest=1 "
                                          "seed_rng=1 use_engines=0 "
                                          "secure_memory=1 fips140=0");
  // and caching for botan pipes
  pipe_cache_cleanup acquire_botan_pipe_caching;
  unfiltered_pipe = new Botan::Pipe;
  new (unfiltered_pipe_cleanup_mem) cached_botan_pipe(unfiltered_pipe);

  // Make clog and cout use the same streambuf as cerr; this ensures
  // that all messages will appear in the order written, no matter what
  // stream each one is written to.
  clog.rdbuf(cerr.rdbuf());
  cout.rdbuf(cerr.rdbuf());

  global_sanity.set_debug();

  string::size_type sep = test_to_run.find(":");

  if (sep == string::npos) // it's a group name
    {
      cerr << argv[0] << ": must specify a test, not a group, to run\n";
      return 2;
    }

  string group, test;
  group = test_to_run.substr(0, sep);
  test = test_to_run.substr(sep+1, string::npos);

  group_list_t::const_iterator g = unit_tests().find(group);

  if (g == unit_tests().end())
    {
      cerr << argv[0] << ": unrecognized test group: "
           << group << '\n';
      return 2;
    }

  test_list_t::const_iterator t = g->second.find(test);
  if (t == g->second.end())
    {
      cerr << argv[0] << ": unrecognized test: "
           << group << ':' << test << '\n';
      return 2;
    }

  L(FL("Beginning test %s:%s") % group % test);

  try
    {
      t->second->func();
    }
  catch(require_failed &)
    {
      // no action required
    }
  catch(std::exception & e)
    {
      log_exception(e);
      this_test_failed = true;
    }
  catch(...)
    {
      log_exception();
      this_test_failed = true;
    }

  if (this_test_failed && !t->second->failure_is_success)
    {
      L(FL("Test %s:%s failed.\n") % group % test);
      return 1;
    }
  else
    {
      L(FL("Test %s:%s succeeded.\n") % group % test);
      return 0;
    }
}

// Global sanity object.  We don't want to depend on ui.
struct unit_tester_sanity : public sanity
{
  void inform_log(std::string const &msg)
  { cout << msg << "\n"; }
  void inform_message(std::string const &msg)
  { cout << msg << "\n"; }
  void inform_warning(std::string const &msg)
  { cerr << "warning: " << msg << "\n"; }
  void inform_error(std::string const &msg)
  { cerr << "error: " << msg << "\n"; }
};
unit_tester_sanity real_sanity;
sanity & global_sanity = real_sanity;

// These are tests of the unit testing mechanism itself.  They would all
// fail, but we make use of a special mechanism to convert that failure
// into a success.  Since we don't want that mechanism used elsewhere,
// the necessary definition macro is defined here and not in unit_test.hh.

#define NEGATIVE_UNIT_TEST(TEST)                \
  namespace unit_test {                         \
      static void t_##TEST();                   \
      static const unit_test_case r_##TEST      \
      (__FILE__, #TEST, t_##TEST, true);        \
  }                                             \
  static void unit_test::t_##TEST()

#include <stdexcept>

NEGATIVE_UNIT_TEST(fail_check)
{
  UNIT_TEST_CHECKPOINT("checkpoint");
  UNIT_TEST_CHECK(false);
  UNIT_TEST_CHECK(false);
}

NEGATIVE_UNIT_TEST(fail_require)
{
  UNIT_TEST_CHECKPOINT("checkpoint");
  UNIT_TEST_REQUIRE(false);
  UNIT_TEST_CHECK(false);
}

NEGATIVE_UNIT_TEST(fail_throw)
{
  UNIT_TEST_CHECK_THROW(string().size(), int);
}

NEGATIVE_UNIT_TEST(fail_nothrow)
{
  UNIT_TEST_CHECK_NOT_THROW(throw int(), int);
}

NEGATIVE_UNIT_TEST(uncaught)
{
  throw int();
}

NEGATIVE_UNIT_TEST(uncaught_std)
{
  throw std::bad_exception();
}

NEGATIVE_UNIT_TEST(uncaught_std_what)
{
  throw std::runtime_error("There is no spoon.");
}

// Local Variables:
// mode: C++
// fill-column: 76
// c-file-style: "gnu"
// indent-tabs-mode: nil
// End:
// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s: