utils.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*************************************************************************
  2. *
  3. * Copyright (C) 2018-2023 Ruilin Peng (Nick) <[email protected]>.
  4. *
  5. * smartdns is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * smartdns is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef _SMARTDNS_TEST_UTILS_
  19. #define _SMARTDNS_TEST_UTILS_
  20. #include <fstream>
  21. #include <functional>
  22. #include <string>
  23. #include <vector>
  24. namespace smartdns
  25. {
  26. class DeferGuard
  27. {
  28. public:
  29. template <class Callable>
  30. DeferGuard(Callable &&fn) noexcept : fn_(std::forward<Callable>(fn))
  31. {
  32. }
  33. DeferGuard(DeferGuard &&other) noexcept
  34. {
  35. fn_ = std::move(other.fn_);
  36. other.fn_ = nullptr;
  37. }
  38. virtual ~DeferGuard()
  39. {
  40. if (fn_) {
  41. fn_();
  42. }
  43. };
  44. DeferGuard(const DeferGuard &) = delete;
  45. void operator=(const DeferGuard &) = delete;
  46. private:
  47. std::function<void()> fn_;
  48. };
  49. #define SMARTDNS_CONCAT_(a, b) a##b
  50. #define SMARTDNS_CONCAT(a, b) SMARTDNS_CONCAT_(a, b)
  51. #define Defer ::smartdns::DeferGuard SMARTDNS_CONCAT(__defer__, __LINE__) = [&]()
  52. class TempFile
  53. {
  54. public:
  55. TempFile();
  56. TempFile(const std::string &line);
  57. virtual ~TempFile();
  58. bool Write(const std::string &line);
  59. std::string GetPath();
  60. void SetPattern(const std::string &pattern);
  61. private:
  62. bool NewTempFile();
  63. std::string path_;
  64. std::ofstream ofs_;
  65. std::string pattern_;
  66. };
  67. class Commander
  68. {
  69. public:
  70. Commander();
  71. virtual ~Commander();
  72. bool Run(const std::vector<std::string> &cmds);
  73. bool Run(const std::string &cmd);
  74. void Kill();
  75. void Terminate();
  76. int ExitCode();
  77. int GetPid();
  78. private:
  79. pid_t pid_{-1};
  80. int exit_code_ = {-1};
  81. };
  82. bool IsCommandExists(const std::string &cmd);
  83. std::string GenerateRandomString(int len);
  84. int ParserArg(const std::string &cmd, std::vector<std::string> &args);
  85. } // namespace smartdns
  86. #endif // _SMARTDNS_TEST_UTILS_