test_posix.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (C) 2015 Pavel Kirienko <pavel.kirienko@gmail.com>
  3. */
  4. #include <uavcan_posix/dynamic_node_id_server/file_event_tracer.hpp>
  5. #include <uavcan_posix/dynamic_node_id_server/file_storage_backend.hpp>
  6. #include <uavcan_linux/uavcan_linux.hpp>
  7. #include <iostream>
  8. #include <iomanip>
  9. #include "debug.hpp"
  10. int main(int argc, const char** argv)
  11. {
  12. (void)argc;
  13. (void)argv;
  14. try
  15. {
  16. ENFORCE(0 == std::system("mkdir -p /tmp/uavcan_posix/dynamic_node_id_server"));
  17. /*
  18. * Event tracer test
  19. */
  20. {
  21. using namespace uavcan::dynamic_node_id_server;
  22. const std::string event_log_file("/tmp/uavcan_posix/dynamic_node_id_server/event.log");
  23. uavcan_posix::dynamic_node_id_server::FileEventTracer tracer;
  24. ENFORCE(0 <= tracer.init(event_log_file.c_str()));
  25. // Adding a line
  26. static_cast<IEventTracer&>(tracer).onEvent(TraceError, 123456);
  27. ENFORCE(0 == std::system(("cat " + event_log_file).c_str()));
  28. // Removing the log file
  29. ENFORCE(0 == std::system(("rm -f " + event_log_file).c_str()));
  30. // Adding another line
  31. static_cast<IEventTracer&>(tracer).onEvent(TraceError, 789123);
  32. ENFORCE(0 == std::system(("cat " + event_log_file).c_str()));
  33. }
  34. /*
  35. * Storage backend test
  36. */
  37. {
  38. using namespace uavcan::dynamic_node_id_server;
  39. uavcan_posix::dynamic_node_id_server::FileStorageBackend backend;
  40. ENFORCE(0 <= backend.init("/tmp/uavcan_posix/dynamic_node_id_server/storage"));
  41. auto print_key = [&](const char* key) {
  42. std::cout << static_cast<IStorageBackend&>(backend).get(key).c_str() << std::endl;
  43. };
  44. print_key("foobar");
  45. static_cast<IStorageBackend&>(backend).set("foobar", "0123456789abcdef0123456789abcdef");
  46. static_cast<IStorageBackend&>(backend).set("the_answer", "42");
  47. print_key("foobar");
  48. print_key("the_answer");
  49. print_key("nonexistent");
  50. }
  51. return 0;
  52. }
  53. catch (const std::exception& ex)
  54. {
  55. std::cerr << "Exception: " << ex.what() << std::endl;
  56. return 1;
  57. }
  58. }