logcheck.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <gtest/gtest.h>
  15. #include <acl/acl.h>
  16. #include <cassert>
  17. // This is not a public header, it is used only inside the tests. Therefore
  18. // we lower the standards a bit and use anonymous namespace in the header
  19. // and "using", just for convenience. This is just to share little bit of code
  20. // between multiple tests.
  21. using namespace isc::acl;
  22. using boost::shared_ptr;
  23. namespace {
  24. // This is arbitrary guess of size for the log. If it's too small for your
  25. // test, just make it bigger.
  26. const size_t LOG_SIZE = 10;
  27. // This will remember which checks did run already.
  28. struct Log {
  29. // The actual log cells, if i-th check did run
  30. mutable bool run[LOG_SIZE];
  31. Log() {
  32. // Nothing run yet
  33. for (size_t i(0); i < LOG_SIZE; ++ i) {
  34. run[i] = false;
  35. }
  36. }
  37. // Checks that the first amount of checks did run and the rest didn't.
  38. void checkFirst(size_t amount) const {
  39. ASSERT_LE(amount, LOG_SIZE) << "Wrong test: amount bigger than size "
  40. "of log";
  41. {
  42. SCOPED_TRACE("Checking that the first amount of checks did run");
  43. for (size_t i(0); i < amount; ++ i) {
  44. EXPECT_TRUE(run[i]) << "Check #" << i << " did not run.";
  45. }
  46. }
  47. {
  48. SCOPED_TRACE("Checking that the rest did not run");
  49. for (size_t i(amount); i < LOG_SIZE; ++ i) {
  50. EXPECT_FALSE(run[i]) << "Check #" << i << "did run.";
  51. }
  52. }
  53. }
  54. };
  55. // This returns true or false every time, no matter what is passed to it.
  56. // But it logs that it did run.
  57. class ConstCheck : public Check<Log> {
  58. public:
  59. ConstCheck(bool accepts, size_t logNum) :
  60. logNum_(logNum),
  61. accepts_(accepts)
  62. {
  63. assert(logNum < LOG_SIZE); // If this fails, the LOG_SIZE is too small
  64. }
  65. virtual bool matches(const Log& log) const {
  66. /*
  67. * This is abuse of the context. It is designed to carry the
  68. * information to check, not to modify it. However, this is the
  69. * easiest way to do the test, so we go against the design.
  70. */
  71. log.run[logNum_] = true;
  72. return (accepts_);
  73. }
  74. private:
  75. size_t logNum_;
  76. bool accepts_;
  77. };
  78. }