lease_file_io.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright (C) 2014-2015 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. #ifndef LEASE_FILE_IO_H
  15. #define LEASE_FILE_IO_H
  16. #include <string>
  17. namespace isc {
  18. namespace dhcp {
  19. namespace test {
  20. /// @brief This class contains functions to perform IO operations on files.
  21. ///
  22. /// This class is solely used by unit tests. Some tests often need files
  23. /// as an input. This class allows for easy creation of text files that can
  24. /// be later used by unit tests. It also provides method to read the contents
  25. /// of the existing file and remove existing file (cleanup after unit test).
  26. class LeaseFileIO {
  27. public:
  28. /// @brief Constructor
  29. ///
  30. /// @param filename Abolsute path to the file.
  31. /// @param recreate A boolean flag indicating if the new file should
  32. /// be created, even if one exists.
  33. LeaseFileIO(const std::string& filename, const bool recreate = true);
  34. /// @brief Destructor.
  35. ~LeaseFileIO();
  36. /// @brief Check if test file exists on disk.
  37. bool exists() const;
  38. /// @brief Reads whole lease file.
  39. ///
  40. /// @return Contents of the file.
  41. std::string readFile() const;
  42. /// @brief Removes existing file (if any).
  43. void removeFile() const;
  44. /// @brief Creates file with contents.
  45. ///
  46. /// @param contents Contents of the file.
  47. void writeFile(const std::string& contents) const;
  48. /// @brief Absolute path to the file used in the tests.
  49. std::string testfile_;
  50. /// @brief Indicates if the file should be recreated during object
  51. /// construction and removed during destruction.
  52. bool recreate_;
  53. };
  54. }
  55. }
  56. }
  57. #endif // LEASE_FILE_IO_H