014-configuration.cpp 4.5 KB

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