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