test-utils.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. // SPDX-FileCopyrightText: 2019-2025 Ivan Baidakou
  3. #include "test-utils.h"
  4. #include "model/device_id.h"
  5. #include "utils/base32.h"
  6. #include "utils/log-setup.h"
  7. #include <random>
  8. #include <cstdint>
  9. #include <spdlog/sinks/stdout_color_sinks.h>
  10. int main(int argc, char *argv[]) { return Catch::Session().run(argc, argv); }
  11. namespace syncspirit::test {
  12. bfs::path locate_path(const char *test_file) {
  13. auto path = bfs::path(test_file);
  14. if (bfs::exists(path)) {
  15. return path;
  16. }
  17. path = bfs::path("../") / path;
  18. if (bfs::exists(path)) {
  19. return path;
  20. }
  21. std::string err = "path not found: ";
  22. err += test_file;
  23. throw std::runtime_error(err);
  24. }
  25. std::string read_file(const bfs::path &path) {
  26. sys::error_code ec;
  27. auto file_path = path.string();
  28. auto file_path_c = file_path.c_str();
  29. auto in = fopen(file_path_c, "rb");
  30. if (!in) {
  31. auto ec = sys::error_code{errno, sys::generic_category()};
  32. std::cout << "can't open " << file_path_c << " : " << ec.message() << "\n";
  33. return "";
  34. }
  35. fseek(in, 0L, SEEK_END);
  36. auto filesize = ftell(in);
  37. fseek(in, 0L, SEEK_SET);
  38. std::vector<char> buffer(filesize, 0);
  39. auto r = fread(buffer.data(), filesize, 1, in);
  40. assert(r == 1);
  41. (void)r;
  42. fclose(in);
  43. return std::string(buffer.data(), filesize);
  44. }
  45. void write_file(const bfs::path &path, std::string_view content) {
  46. bfs::create_directories(path.parent_path());
  47. auto file_path = path.string();
  48. auto out = fopen(file_path.c_str(), "wb");
  49. if (!out) {
  50. auto ec = sys::error_code{errno, sys::generic_category()};
  51. std::cout << "can't open " << file_path << " : " << ec.message() << "\n";
  52. std::abort();
  53. }
  54. if (content.size()) {
  55. auto r = fwrite(content.data(), content.size(), 1, out);
  56. assert(r);
  57. (void)r;
  58. }
  59. fclose(out);
  60. }
  61. std::string device_id2sha256(std::string_view device_id) {
  62. return std::string(model::device_id_t::from_string(device_id).value().get_sha256());
  63. }
  64. model::device_ptr_t make_device(std::string_view device_id, std::string_view name) {
  65. auto id = model::device_id_t::from_string(device_id).value();
  66. return model::device_t::create(id, name).assume_value();
  67. }
  68. std::string hash_string(const std::string_view &hash) noexcept {
  69. auto r = std::string();
  70. r.reserve(hash.size() * 2);
  71. for (size_t i = 0; i < hash.size(); ++i) {
  72. r += fmt::format("{:02x}", (unsigned char)hash[i]);
  73. }
  74. return r;
  75. }
  76. static model::diff::apply_controller_t apply_controller;
  77. model::diff::apply_controller_t &get_apply_controller() { return apply_controller; }
  78. void init_logging() {
  79. auto dist_sink = utils::create_root_logger();
  80. auto console_sink = std::make_shared<spdlog::sinks::stderr_color_sink_mt>();
  81. dist_sink->add_sink(console_sink);
  82. }
  83. static std::random_device rd;
  84. static std::uniform_int_distribution<std::uint64_t> dist;
  85. bfs::path unique_path() {
  86. auto n = dist(rd);
  87. auto view = std::string_view(reinterpret_cast<const char *>(&n), sizeof(n));
  88. auto random_name = utils::base32::encode(view);
  89. std::transform(random_name.begin(), random_name.end(), random_name.begin(),
  90. [](unsigned char c) { return std::tolower(c); });
  91. auto name = std::string("tmp-") + random_name;
  92. return bfs::path(name);
  93. }
  94. } // namespace syncspirit::test