test-main.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. int main(int argc, char *argv[]) {
  11. return Catch::Session().run(argc, argv);
  12. }
  13. namespace syncspirit::test {
  14. boost::filesystem::path file_path(const char* test_file) {
  15. auto self_file = __FILE__;
  16. bfs::path self(self_file);
  17. self.remove_filename();
  18. return self / test_file;
  19. }
  20. std::string read_file(const bfs::path& path) {
  21. sys::error_code ec;
  22. auto filesize = bfs::file_size(path, ec);
  23. auto file_path_c = path.c_str();
  24. auto in = fopen(file_path_c, "rb");
  25. if (!in) {
  26. auto ec = sys::error_code{errno, sys::generic_category()};
  27. std::cout << "can't open " << file_path_c << " : " << ec.message() << "\n";
  28. std::abort();
  29. }
  30. assert(in);
  31. std::vector<char> buffer(filesize, 0);
  32. auto r = fread(buffer.data(), filesize, 1, in);
  33. assert(r == 1);
  34. fclose(in);
  35. return std::string(buffer.data(), filesize);
  36. }
  37. std::string read_file(const char* test_file) {
  38. return read_file(file_path(test_file));
  39. }
  40. void write_file(const bfs::path& path, std::string_view content) {
  41. bfs::create_directories(path.parent_path());
  42. auto file_path_c = path.c_str();
  43. auto out = fopen(file_path_c, "wb");
  44. if (!out) {
  45. auto ec = sys::error_code{errno, sys::generic_category()};
  46. std::cout << "can't open " << file_path_c << " : " << ec.message() << "\n";
  47. std::abort();
  48. }
  49. if (content.size()) {
  50. auto r = fwrite(content.data(), content.size(), 1, out);
  51. assert(r);
  52. }
  53. fclose(out);
  54. }
  55. std::string device_id2sha256(const char* device_id) {
  56. return model::device_id_t::from_string(device_id).value().get_sha256();
  57. }
  58. }