014-configuration.cpp 4.6 KB

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