test_control_unittest.cc 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright (C) 2012 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 <cstddef>
  15. #include <stdint.h>
  16. #include <string>
  17. #include <gtest/gtest.h>
  18. #include "../test_control.h"
  19. #include "../command_options.h"
  20. #include "exceptions/exceptions.h"
  21. using namespace std;
  22. using namespace isc;
  23. using namespace isc::perfdhcp;
  24. /// \brief Test Fixture Class
  25. ///
  26. /// This test fixture class is used to perform
  27. /// unit tests on perfdhcp TestControl class.
  28. class TestControlTest : public virtual ::testing::Test
  29. {
  30. public:
  31. /// \brief Default Constructor
  32. TestControlTest() { }
  33. protected:
  34. };
  35. TEST_F(TestControlTest, Run) {
  36. // Get the instance of TestControl object.
  37. TestControl& test_control = TestControl::instance();
  38. // Running test without parsing command line arguments is
  39. // expected to cause exception.
  40. EXPECT_THROW(test_control.run(), isc::InvalidOperation);
  41. // The command line is to run single test iteration and exit.
  42. // We have to declare argv as const walk around the problem
  43. // of deprecated conversion from string to char*.
  44. const char* argv[] = { "perfdhcp", "-l", "127.0.0.1", "-r", "10", "-n", "1" };
  45. const int argc = sizeof(argv) / sizeof(argv[0]);
  46. CommandOptions& options = CommandOptions::instance();
  47. // const_cast is odd but it seems to be the most straight forward way
  48. // to achive the goal. In the future we might think about creating
  49. // a tokenizing function that would dynamically produce non-const
  50. // argv value.
  51. ASSERT_NO_THROW(options.parse(argc, const_cast<char**>(argv)));
  52. EXPECT_NO_THROW(test_control.run());
  53. // This is ok to run the test again with the same parameters.
  54. // It may trigger exception if TestControl singleton is in
  55. // invalid state after test run. We want to make sure it is
  56. // safe to rerun the test.
  57. EXPECT_NO_THROW(test_control.run());
  58. }