Simple test for POSIX storage backend

This commit is contained in:
Pavel Kirienko
2015-05-18 17:08:49 +03:00
parent 09a96061ad
commit 3c073ac9d4
2 changed files with 26 additions and 1 deletions
@@ -40,6 +40,29 @@ int main(int argc, const char** argv)
ENFORCE(0 == std::system(("cat " + event_log_file).c_str()));
}
/*
* Storage backend test
*/
{
using namespace uavcan::dynamic_node_id_server;
uavcan_posix::dynamic_node_id_server::FileStorageBackend backend;
ENFORCE(0 <= backend.init("/tmp/uavcan_posix/dynamic_node_id_server/storage"));
auto print_key = [&](const char* key) {
std::cout << static_cast<IStorageBackend&>(backend).get(key).c_str() << std::endl;
};
print_key("foobar");
static_cast<IStorageBackend&>(backend).set("foobar", "0123456789abcdef0123456789abcdef");
static_cast<IStorageBackend&>(backend).set("the_answer", "42");
print_key("foobar");
print_key("the_answer");
print_key("nonexistent");
}
return 0;
}
catch (const std::exception& ex)
@@ -34,6 +34,8 @@ class FileStorageBackend : public uavcan::dynamic_node_id_server::IStorageBacken
*/
enum { MaxPathLength = 128 };
enum { FilePermissions = 438 }; ///< 0o666
/**
* This type is used for the path
*/
@@ -76,7 +78,7 @@ class FileStorageBackend : public uavcan::dynamic_node_id_server::IStorageBacken
using namespace std;
PathString path = base_path.c_str();
path += key;
int fd = open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC);
int fd = open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, FilePermissions);
if (fd >= 0)
{
write(fd, value.c_str(), value.size());