test-main.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // Copyright (c) 2019-2021 Ivan Baidakou (basiliscos) (the dot dmol at gmail dot com)
  3. //
  4. // Distributed under the MIT Software License
  5. //
  6. #define CATCH_CONFIG_RUNNER
  7. #include "catch.hpp"
  8. #include "test-utils.h"
  9. #include "model/device_id.h"
  10. #include "structs.pb.h"
  11. #include "db/prefix.h"
  12. int main(int argc, char *argv[]) {
  13. return Catch::Session().run(argc, argv);
  14. }
  15. namespace syncspirit::test {
  16. boost::filesystem::path file_path(const char* test_file) {
  17. auto self_file = __FILE__;
  18. bfs::path self(self_file);
  19. self.remove_filename();
  20. return self / test_file;
  21. }
  22. std::string read_file(const bfs::path& path) {
  23. sys::error_code ec;
  24. auto filesize = bfs::file_size(path, ec);
  25. auto file_path_c = path.c_str();
  26. auto in = fopen(file_path_c, "rb");
  27. if (!in) {
  28. auto ec = sys::error_code{errno, sys::generic_category()};
  29. std::cout << "can't open " << file_path_c << " : " << ec.message() << "\n";
  30. return "";
  31. }
  32. assert(in);
  33. std::vector<char> buffer(filesize, 0);
  34. auto r = fread(buffer.data(), filesize, 1, in);
  35. assert(r == 1);
  36. fclose(in);
  37. return std::string(buffer.data(), filesize);
  38. }
  39. std::string read_file(const char* test_file) {
  40. return read_file(file_path(test_file));
  41. }
  42. void write_file(const bfs::path& path, std::string_view content) {
  43. bfs::create_directories(path.parent_path());
  44. auto file_path_c = path.c_str();
  45. auto out = fopen(file_path_c, "wb");
  46. if (!out) {
  47. auto ec = sys::error_code{errno, sys::generic_category()};
  48. std::cout << "can't open " << file_path_c << " : " << ec.message() << "\n";
  49. std::abort();
  50. }
  51. if (content.size()) {
  52. auto r = fwrite(content.data(), content.size(), 1, out);
  53. assert(r);
  54. }
  55. fclose(out);
  56. }
  57. std::string device_id2sha256(std::string_view device_id) {
  58. return std::string(model::device_id_t::from_string(device_id).value().get_sha256());
  59. }
  60. model::device_ptr_t make_device(std::string_view device_id, std::string_view name) {
  61. auto id = model::device_id_t::from_string(device_id).value();
  62. return model::device_t::create(id, name).assume_value();
  63. }
  64. std::string hash_string(const std::string_view &hash) noexcept {
  65. auto r = std::string();
  66. r.reserve(hash.size() * 2);
  67. for (size_t i = 0; i < hash.size(); ++i) {
  68. char buff[3];
  69. sprintf(buff, "%02x", (unsigned char)hash[i]);
  70. r += std::string_view(buff, 2);
  71. }
  72. return r;
  73. }
  74. }