1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3 #include "credential_pipe.hpp"
4
5 #include <boost/asio/buffer.hpp>
6 #include <boost/asio/io_context.hpp>
7 #include <boost/asio/read.hpp>
8 #include <boost/asio/readable_pipe.hpp>
9 #include <boost/system/error_code.hpp>
10
11 #include <array>
12 #include <cstddef>
13 #include <functional>
14 #include <string>
15
16 #include <gmock/gmock.h>
17 #include <gtest/gtest.h>
18
19 using ::testing::ElementsAre;
20
handler(boost::asio::io_context & io,const boost::system::error_code & ec,size_t sent)21 static void handler(boost::asio::io_context& io,
22 const boost::system::error_code& ec, size_t sent)
23 {
24 io.stop();
25 EXPECT_FALSE(ec);
26 EXPECT_EQ(sent, 18);
27 }
28
TEST(CredentialsPipe,BasicSend)29 TEST(CredentialsPipe, BasicSend)
30 {
31 boost::asio::io_context io;
32 boost::asio::readable_pipe testPipe(io);
33 {
34 CredentialsPipe pipe(io);
35 testPipe = boost::asio::readable_pipe(io, pipe.releaseFd());
36 ASSERT_GT(testPipe.native_handle(), 0);
37 pipe.asyncWrite("username", "password",
38 std::bind_front(handler, std::ref(io)));
39 }
40 io.run();
41 std::array<char, 18> buff{};
42 boost::system::error_code ec;
43 boost::asio::read(testPipe, boost::asio::buffer(buff), ec);
44 ASSERT_FALSE(ec);
45
46 EXPECT_THAT(buff,
47 ElementsAre('u', 's', 'e', 'r', 'n', 'a', 'm', 'e', '\0', 'p',
48 'a', 's', 's', 'w', 'o', 'r', 'd', '\0'));
49 }
50