file_server.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Pavel Kirienko <pavel.kirienko@gmail.com>
3  */
4 
5 #include <gtest/gtest.h>
7 #include "helpers.hpp"
8 
9 
11 {
12 public:
13  static const std::string file_name;
14  static const std::string file_data;
15 
16  virtual int16_t getInfo(const Path& path, uint64_t& out_size, EntryType& out_type)
17  {
18  if (path == file_name)
19  {
20  out_size = uint16_t(file_data.length());
21  out_type.flags |= EntryType::FLAG_FILE;
22  out_type.flags |= EntryType::FLAG_READABLE;
23  return 0;
24  }
25  else
26  {
27  return Error::NOT_FOUND;
28  }
29  }
30 
31  virtual int16_t read(const Path& path, const uint64_t offset, uint8_t* out_buffer, uint16_t& inout_size)
32  {
33  if (path == file_name)
34  {
35  if (offset < file_data.length())
36  {
37  inout_size = uint16_t(file_data.length() - offset);
38  std::memcpy(out_buffer, file_data.c_str() + offset, inout_size);
39  }
40  else
41  {
42  inout_size = 0;
43  }
44  return 0;
45  }
46  else
47  {
48  return Error::NOT_FOUND;
49  }
50  }
51 };
52 
53 const std::string TestFileServerBackend::file_name = "test";
54 const std::string TestFileServerBackend::file_data = "123456789";
55 
56 TEST(BasicFileServer, Basic)
57 {
58  using namespace uavcan::protocol::file;
59 
63 
65 
66  TestFileServerBackend backend;
67 
68  uavcan::BasicFileServer serv(nodes.a, backend);
69  std::cout << "sizeof(uavcan::BasicFileServer): " << sizeof(uavcan::BasicFileServer) << std::endl;
70 
71  ASSERT_LE(0, serv.start());
72 
73  /*
74  * GetInfo, existing file
75  */
76  {
77  ServiceClientWithCollector<GetInfo> get_info(nodes.b);
78 
79  GetInfo::Request get_info_req;
80  get_info_req.path.path = "test";
81 
82  ASSERT_LE(0, get_info.call(1, get_info_req));
84 
85  ASSERT_TRUE(get_info.collector.result.get());
86  ASSERT_TRUE(get_info.collector.result->isSuccessful());
87 
88  ASSERT_EQ(0, get_info.collector.result->getResponse().error.value);
89  ASSERT_EQ(9, get_info.collector.result->getResponse().size);
90  ASSERT_EQ(EntryType::FLAG_FILE | EntryType::FLAG_READABLE,
91  get_info.collector.result->getResponse().entry_type.flags);
92  }
93 
94  /*
95  * Read, existing file
96  */
97  {
99 
100  Read::Request read_req;
101  read_req.path.path = "test";
102 
103  ASSERT_LE(0, read.call(1, read_req));
105 
106  ASSERT_TRUE(read.collector.result.get());
107  ASSERT_TRUE(read.collector.result->isSuccessful());
108 
109  ASSERT_EQ("123456789", read.collector.result->getResponse().data);
110  ASSERT_EQ(0, read.collector.result->getResponse().error.value);
111  }
112 }
113 
114 
115 TEST(FileServer, Basic)
116 {
117  using namespace uavcan::protocol::file;
118 
125 
127 
128  TestFileServerBackend backend;
129 
130  uavcan::FileServer serv(nodes.a, backend);
131  std::cout << "sizeof(uavcan::FileServer): " << sizeof(uavcan::FileServer) << std::endl;
132 
133  ASSERT_LE(0, serv.start());
134 
135  // TODO TEST
136 }
uavcan::IFileServerBackend::EntryType
protocol::file::EntryType EntryType
Definition: file_server.hpp:30
uavcan::uint64_t
std::uint64_t uint64_t
Definition: std.hpp:27
uavcan::DefaultDataTypeRegistrator
Definition: global_data_type_registry.hpp:186
uavcan::FileServer
Definition: file_server.hpp:191
uavcan::FileServer::start
int start()
Definition: file_server.hpp:235
TestFileServerBackend::read
virtual int16_t read(const Path &path, const uint64_t offset, uint8_t *out_buffer, uint16_t &inout_size)
Definition: file_server.cpp:31
uavcan::DurationBase< MonotonicDuration >::fromMSec
static MonotonicDuration fromMSec(int64_t ms)
Definition: time.hpp:41
ServiceClientWithCollector::call
int call(uavcan::NodeID node_id, const typename DataType::Request &request)
Definition: libuavcan/libuavcan/test/protocol/helpers.hpp:111
TestFileServerBackend::file_name
static const std::string file_name
Definition: file_server.cpp:13
ServiceClientWithCollector
Definition: libuavcan/libuavcan/test/protocol/helpers.hpp:99
ServiceCallResultCollector::result
std::unique_ptr< Result > result
Definition: libuavcan/libuavcan/test/protocol/helpers.hpp:88
uavcan::uint16_t
std::uint16_t uint16_t
Definition: std.hpp:25
uavcan::int16_t
std::int16_t int16_t
Definition: std.hpp:30
uavcan::uint8_t
std::uint8_t uint8_t
Definition: std.hpp:24
uavcan::IFileServerBackend::Path
protocol::file::Path::FieldTypes::path Path
Definition: file_server.hpp:29
pyuavcan_v0.file
file
Definition: pyuavcan/pyuavcan_v0/__init__.py:42
helpers.hpp
InterlinkedTestNodes
Definition: test_node.hpp:149
TestFileServerBackend
Definition: file_server.cpp:10
uavcan::BasicFileServer::start
int start()
Definition: file_server.hpp:164
file_server.hpp
InterlinkedTestNodes::a
TestNode a
Definition: test_node.hpp:155
InterlinkedTestNodes::spinBoth
int spinBoth(uavcan::MonotonicDuration duration)
Definition: test_node.hpp:176
uavcan::BasicFileServer
Definition: file_server.hpp:112
uavcan::GlobalDataTypeRegistry::instance
static GlobalDataTypeRegistry & instance()
Definition: uc_global_data_type_registry.cpp:128
TestFileServerBackend::file_data
static const std::string file_data
Definition: file_server.cpp:14
InterlinkedTestNodes::b
TestNode b
Definition: test_node.hpp:156
TestFileServerBackend::getInfo
virtual int16_t getInfo(const Path &path, uint64_t &out_size, EntryType &out_type)
Definition: file_server.cpp:16
uavcan::IFileServerBackend
Definition: file_server.hpp:26
ServiceClientWithCollector::collector
Collector collector
Definition: libuavcan/libuavcan/test/protocol/helpers.hpp:104
TEST
TEST(BasicFileServer, Basic)
Definition: file_server.cpp:56


uavcan_communicator
Author(s):
autogenerated on Fri Dec 13 2024 03:10:02