diff options
author | Ed Tanous <edtanous@google.com> | 2023-08-25 00:25:18 +0300 |
---|---|---|
committer | Ed Tanous <ed@tanous.net> | 2023-09-25 20:50:15 +0300 |
commit | 11e8f60df2fbe1ceafdf886132bd93d112a726bf (patch) | |
tree | a5a393b6ec81a04d7c021737ca71ba58af1482eb /test | |
parent | b25644a1ae3bbf00ddea91ad494be959cb2632c8 (diff) | |
download | bmcweb-11e8f60df2fbe1ceafdf886132bd93d112a726bf.tar.xz |
Clean up vm CredentialPipe
This code is needlessly complicated for what it does. Even with the
intent, which is secure buffer cleanup, it's trivial to encase all this
into a single class that accepts the strings by rvalue reference, then
cleans them up afterward.
Doing this also cleans up a potential lifetime problem, where if the
unix socket returned immediately, it would've invalidated the buffers
that were being sent. It also moves to async_write, instead of
async_write_some. The former could in theory fail if the socket blocks
(unlikely in this scenario) but it's good to handle anyway.
Tested: Need some help here. There's no backend for this, so we might
just have to rely on inspection.
Change-Id: I9032d458f8eb7a0689bee575aae611641bacee26
Signed-off-by: Ed Tanous <edtanous@google.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/include/credential_pipe_test.cpp | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/test/include/credential_pipe_test.cpp b/test/include/credential_pipe_test.cpp new file mode 100644 index 0000000000..c5b544bc0c --- /dev/null +++ b/test/include/credential_pipe_test.cpp @@ -0,0 +1,41 @@ +#include "credential_pipe.hpp" + +#include <boost/asio/io_context.hpp> +#include <boost/beast/core/file_posix.hpp> + +#include <string> + +#include <gmock/gmock.h> + +using ::testing::ElementsAre; + +static void handler(boost::asio::io_context& io, + const boost::system::error_code& ec, size_t sent) +{ + io.stop(); + EXPECT_FALSE(ec); + EXPECT_EQ(sent, 18); +} + +TEST(CredentialsPipe, BasicSend) +{ + boost::beast::file_posix file; + { + boost::asio::io_context io; + CredentialsPipe pipe(io); + file.native_handle(dup(pipe.fd())); + ASSERT_GT(file.native_handle(), 0); + pipe.asyncWrite("username", "password", + std::bind_front(handler, std::ref(io))); + io.run(); + } + std::array<char, 18> buff{}; + boost::system::error_code ec; + size_t r = file.read(buff.data(), buff.size(), ec); + ASSERT_FALSE(ec); + ASSERT_EQ(r, 18); + + EXPECT_THAT(buff, + ElementsAre('u', 's', 'e', 'r', 'n', 'a', 'm', 'e', '\0', 'p', + 'a', 's', 's', 'w', 'o', 'r', 'd', '\0')); +} |