file_server.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (C) 2015 Pavel Kirienko <pavel.kirienko@gmail.com>
  3. */
  4. #include <gtest/gtest.h>
  5. #include <uavcan/protocol/file_server.hpp>
  6. #include "helpers.hpp"
  7. class TestFileServerBackend : public uavcan::IFileServerBackend
  8. {
  9. public:
  10. static const std::string file_name;
  11. static const std::string file_data;
  12. virtual int16_t getInfo(const Path& path, uint64_t& out_size, EntryType& out_type)
  13. {
  14. if (path == file_name)
  15. {
  16. out_size = uint16_t(file_data.length());
  17. out_type.flags |= EntryType::FLAG_FILE;
  18. out_type.flags |= EntryType::FLAG_READABLE;
  19. return 0;
  20. }
  21. else
  22. {
  23. return Error::NOT_FOUND;
  24. }
  25. }
  26. virtual int16_t read(const Path& path, const uint64_t offset, uint8_t* out_buffer, uint16_t& inout_size)
  27. {
  28. if (path == file_name)
  29. {
  30. if (offset < file_data.length())
  31. {
  32. inout_size = uint16_t(file_data.length() - offset);
  33. std::memcpy(out_buffer, file_data.c_str() + offset, inout_size);
  34. }
  35. else
  36. {
  37. inout_size = 0;
  38. }
  39. return 0;
  40. }
  41. else
  42. {
  43. return Error::NOT_FOUND;
  44. }
  45. }
  46. };
  47. const std::string TestFileServerBackend::file_name = "test";
  48. const std::string TestFileServerBackend::file_data = "123456789";
  49. TEST(BasicFileServer, Basic)
  50. {
  51. using namespace uavcan::protocol::file;
  52. uavcan::GlobalDataTypeRegistry::instance().reset();
  53. uavcan::DefaultDataTypeRegistrator<GetInfo> _reg1;
  54. uavcan::DefaultDataTypeRegistrator<Read> _reg2;
  55. InterlinkedTestNodesWithSysClock nodes;
  56. TestFileServerBackend backend;
  57. uavcan::BasicFileServer serv(nodes.a, backend);
  58. std::cout << "sizeof(uavcan::BasicFileServer): " << sizeof(uavcan::BasicFileServer) << std::endl;
  59. ASSERT_LE(0, serv.start());
  60. /*
  61. * GetInfo, existing file
  62. */
  63. {
  64. ServiceClientWithCollector<GetInfo> get_info(nodes.b);
  65. GetInfo::Request get_info_req;
  66. get_info_req.path.path = "test";
  67. ASSERT_LE(0, get_info.call(1, get_info_req));
  68. nodes.spinBoth(uavcan::MonotonicDuration::fromMSec(10));
  69. ASSERT_TRUE(get_info.collector.result.get());
  70. ASSERT_TRUE(get_info.collector.result->isSuccessful());
  71. ASSERT_EQ(0, get_info.collector.result->getResponse().error.value);
  72. ASSERT_EQ(9, get_info.collector.result->getResponse().size);
  73. ASSERT_EQ(EntryType::FLAG_FILE | EntryType::FLAG_READABLE,
  74. get_info.collector.result->getResponse().entry_type.flags);
  75. }
  76. /*
  77. * Read, existing file
  78. */
  79. {
  80. ServiceClientWithCollector<Read> read(nodes.b);
  81. Read::Request read_req;
  82. read_req.path.path = "test";
  83. ASSERT_LE(0, read.call(1, read_req));
  84. nodes.spinBoth(uavcan::MonotonicDuration::fromMSec(10));
  85. ASSERT_TRUE(read.collector.result.get());
  86. ASSERT_TRUE(read.collector.result->isSuccessful());
  87. ASSERT_EQ("123456789", read.collector.result->getResponse().data);
  88. ASSERT_EQ(0, read.collector.result->getResponse().error.value);
  89. }
  90. }
  91. TEST(FileServer, Basic)
  92. {
  93. using namespace uavcan::protocol::file;
  94. uavcan::GlobalDataTypeRegistry::instance().reset();
  95. uavcan::DefaultDataTypeRegistrator<GetInfo> _reg1;
  96. uavcan::DefaultDataTypeRegistrator<Read> _reg2;
  97. uavcan::DefaultDataTypeRegistrator<Write> _reg3;
  98. uavcan::DefaultDataTypeRegistrator<Delete> _reg4;
  99. uavcan::DefaultDataTypeRegistrator<GetDirectoryEntryInfo> _reg5;
  100. InterlinkedTestNodesWithSysClock nodes;
  101. TestFileServerBackend backend;
  102. uavcan::FileServer serv(nodes.a, backend);
  103. std::cout << "sizeof(uavcan::FileServer): " << sizeof(uavcan::FileServer) << std::endl;
  104. ASSERT_LE(0, serv.start());
  105. // TODO TEST
  106. }