014-configuration.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. // SPDX-FileCopyrightText: 2019-2025 Ivan Baidakou
  3. #include "test-utils.h"
  4. #include "config/utils.h"
  5. #include "utils/uri.h"
  6. #include "utils/location.h"
  7. #include <filesystem>
  8. #include <sstream>
  9. namespace syncspirit::config {
  10. bool operator==(const bep_config_t &lhs, const bep_config_t &rhs) noexcept {
  11. return lhs.rx_buff_size == rhs.rx_buff_size && lhs.tx_buff_limit == rhs.tx_buff_limit &&
  12. lhs.connect_timeout == rhs.connect_timeout && lhs.request_timeout == rhs.request_timeout &&
  13. lhs.tx_timeout == rhs.tx_timeout && lhs.rx_timeout == rhs.rx_timeout &&
  14. lhs.blocks_max_requested == rhs.blocks_max_requested &&
  15. lhs.blocks_simultaneous_write == rhs.blocks_simultaneous_write;
  16. }
  17. bool operator==(const dialer_config_t &lhs, const dialer_config_t &rhs) noexcept {
  18. return lhs.enabled == rhs.enabled && lhs.redial_timeout == rhs.redial_timeout &&
  19. lhs.skip_discovers == rhs.skip_discovers;
  20. }
  21. bool operator==(const fs_config_t &lhs, const fs_config_t &rhs) noexcept {
  22. return lhs.temporally_timeout == rhs.temporally_timeout && lhs.mru_size == rhs.mru_size &&
  23. lhs.bytes_scan_iteration_limit == rhs.bytes_scan_iteration_limit &&
  24. lhs.files_scan_iteration_limit == rhs.files_scan_iteration_limit;
  25. }
  26. bool operator==(const db_config_t &lhs, const db_config_t &rhs) noexcept {
  27. return lhs.upper_limit == rhs.upper_limit && lhs.uncommitted_threshold == rhs.uncommitted_threshold &&
  28. lhs.max_blocks_per_diff == rhs.max_blocks_per_diff && lhs.max_files_per_diff == rhs.max_files_per_diff;
  29. }
  30. bool operator==(const global_announce_config_t &lhs, const global_announce_config_t &rhs) noexcept {
  31. return lhs.enabled == rhs.enabled && lhs.debug == rhs.debug && *lhs.announce_url == *rhs.announce_url &&
  32. lhs.device_id == rhs.device_id && lhs.cert_file == rhs.cert_file && lhs.key_file == rhs.key_file &&
  33. lhs.rx_buff_size == rhs.rx_buff_size && lhs.timeout == rhs.timeout &&
  34. lhs.reannounce_after == rhs.reannounce_after;
  35. }
  36. bool operator==(const local_announce_config_t &lhs, const local_announce_config_t &rhs) noexcept {
  37. return lhs.enabled == rhs.enabled && lhs.port == rhs.port && lhs.frequency == rhs.frequency;
  38. }
  39. bool operator==(const log_config_t &lhs, const log_config_t &rhs) noexcept {
  40. return lhs.name == rhs.name && lhs.level == rhs.level && lhs.sinks == rhs.sinks;
  41. }
  42. bool operator==(const upnp_config_t &lhs, const upnp_config_t &rhs) noexcept {
  43. return lhs.enabled == rhs.enabled && lhs.max_wait == rhs.max_wait && lhs.external_port == rhs.external_port &&
  44. lhs.rx_buff_size == rhs.rx_buff_size && lhs.debug == rhs.debug;
  45. }
  46. bool operator==(const relay_config_t &lhs, const relay_config_t &rhs) noexcept {
  47. return lhs.enabled == rhs.enabled && lhs.debug == rhs.debug && *lhs.discovery_url == *rhs.discovery_url &&
  48. lhs.rx_buff_size == rhs.rx_buff_size;
  49. }
  50. bool operator==(const main_t &lhs, const main_t &rhs) noexcept {
  51. return lhs.local_announce_config == rhs.local_announce_config && lhs.upnp_config == rhs.upnp_config &&
  52. lhs.global_announce_config == rhs.global_announce_config && lhs.bep_config == rhs.bep_config &&
  53. lhs.db_config == rhs.db_config && lhs.timeout == rhs.timeout && lhs.device_name == rhs.device_name &&
  54. lhs.config_path == rhs.config_path && lhs.log_configs == rhs.log_configs &&
  55. lhs.hasher_threads == rhs.hasher_threads;
  56. }
  57. } // namespace syncspirit::config
  58. namespace sys = boost::system;
  59. namespace fs = std::filesystem;
  60. namespace st = syncspirit::test;
  61. using namespace syncspirit;
  62. TEST_CASE("expand_home", "[config]") {
  63. SECTION("valid home") {
  64. auto home = utils::home_option_t(fs::path("/user/home/.config/syncspirit_test"));
  65. REQUIRE(utils::expand_home("some/path", home) == "some/path");
  66. REQUIRE(utils::expand_home("~/some/path", home) == "/user/home/.config/syncspirit_test/some/path");
  67. }
  68. SECTION("invalid home") {
  69. auto ec = sys::error_code{1, sys::system_category()};
  70. auto home = utils::home_option_t(ec);
  71. REQUIRE(utils::expand_home("some/path", home) == "some/path");
  72. REQUIRE(utils::expand_home("~/some/path", home) == "~/some/path");
  73. }
  74. }
  75. TEST_CASE("default config is OK", "[config]") {
  76. auto dir = st::unique_path();
  77. fs::create_directory(dir);
  78. auto dir_guard = st::path_guard_t(dir);
  79. auto cfg_path = dir / "syncspirit.toml";
  80. auto cfg_opt = config::generate_config(cfg_path);
  81. REQUIRE(cfg_opt);
  82. auto &cfg = cfg_opt.value();
  83. std::stringstream out;
  84. SECTION("serialize default") {
  85. auto r = config::serialize(cfg, out);
  86. CHECK(r);
  87. INFO(out.str());
  88. CHECK(out.str().find("~") == std::string::npos);
  89. auto cfg_opt = config::get_config(out, cfg_path);
  90. CHECK(cfg_opt);
  91. auto cfg2 = cfg_opt.value();
  92. CHECK(cfg == cfg2);
  93. }
  94. }