blob: 55a8f6266c7eb8ac544966ce1c77849d57317df7 [file] [log] [blame]
James Kuszmaul731a05d2022-01-07 17:59:26 -08001#include "aos/util/scoped_pipe.h"
2
James Kuszmauld42edb42022-01-07 18:00:16 -08003#include <fcntl.h>
4
Stephan Pleinesb1177672024-05-27 17:48:32 -07005#include <algorithm>
James Kuszmaul731a05d2022-01-07 17:59:26 -08006#include <array>
7#include <string>
8
9#include "gtest/gtest.h"
10
Stephan Pleinesf63bde82024-01-13 15:59:33 -080011namespace aos::util::testing {
James Kuszmaul731a05d2022-01-07 17:59:26 -080012
13// Tests using uint32_t read/write methods on the ScopedPipe objects.
14TEST(ScopedPipeTest, IntegerPipe) {
James Kuszmauld42edb42022-01-07 18:00:16 -080015 ScopedPipe::PipePair pipe = ScopedPipe::MakePipe();
16 ASSERT_FALSE(pipe.read->Read().has_value())
James Kuszmaul731a05d2022-01-07 17:59:26 -080017 << "Shouldn't get anything on empty read.";
James Kuszmauld42edb42022-01-07 18:00:16 -080018 pipe.write->Write(971);
19 ASSERT_EQ(971, pipe.read->Read().value());
James Kuszmaul731a05d2022-01-07 17:59:26 -080020}
21
22// Tests using string read/write methods on the ScopedPipe objects.
23TEST(ScopedPipeTest, StringPipe) {
James Kuszmauld42edb42022-01-07 18:00:16 -080024 ScopedPipe::PipePair pipe = ScopedPipe::MakePipe();
James Kuszmaul731a05d2022-01-07 17:59:26 -080025 std::string buffer;
James Kuszmauld42edb42022-01-07 18:00:16 -080026 ASSERT_EQ(0u, pipe.read->Read(&buffer))
James Kuszmaul731a05d2022-01-07 17:59:26 -080027 << "Shouldn't get anything on empty read.";
28 ASSERT_TRUE(buffer.empty());
29
30 const char *const kAbc = "abcdef";
James Kuszmauld42edb42022-01-07 18:00:16 -080031 pipe.write->Write(
James Kuszmaul731a05d2022-01-07 17:59:26 -080032 absl::Span<const uint8_t>(reinterpret_cast<const uint8_t *>(kAbc), 6));
James Kuszmauld42edb42022-01-07 18:00:16 -080033 ASSERT_EQ(6u, pipe.read->Read(&buffer));
James Kuszmaul731a05d2022-01-07 17:59:26 -080034 ASSERT_EQ("abcdef", buffer);
35
36 std::array<uint8_t, 10000> large_buffer;
37 large_buffer.fill(99);
James Kuszmauld42edb42022-01-07 18:00:16 -080038 pipe.write->Write(
James Kuszmaul731a05d2022-01-07 17:59:26 -080039 absl::Span<const uint8_t>(large_buffer.data(), large_buffer.size()));
James Kuszmauld42edb42022-01-07 18:00:16 -080040 ASSERT_EQ(large_buffer.size(), pipe.read->Read(&buffer));
James Kuszmaul731a05d2022-01-07 17:59:26 -080041 for (size_t ii = 0; ii < large_buffer.size(); ++ii) {
42 ASSERT_EQ(large_buffer[ii], buffer[ii + 6]);
43 }
44}
45
Austin Schuh0d164112023-08-17 12:50:51 -070046// Tests using string read/write methods on the ScopedPipe objects.
47TEST(ScopedPipeTest, StringPipe2048) {
48 ScopedPipe::PipePair pipe = ScopedPipe::MakePipe();
49 std::string buffer;
50 ASSERT_EQ(0u, pipe.read->Read(&buffer))
51 << "Shouldn't get anything on empty read.";
52 ASSERT_TRUE(buffer.empty());
53
54 std::string a(2048, 'a');
55 pipe.write->Write(absl::Span<const uint8_t>(
56 reinterpret_cast<const uint8_t *>(a.data()), a.size()));
57 ASSERT_EQ(2048u, pipe.read->Read(&buffer));
58 ASSERT_EQ(a, buffer);
59}
60
James Kuszmauld42edb42022-01-07 18:00:16 -080061// Tests that calling SetCloexec succeeds and does indeed set FD_CLOEXEC.
62TEST(ScopedPipeTest, SetCloexec) {
63 ScopedPipe::PipePair pipe = ScopedPipe::MakePipe();
64 ASSERT_EQ(0, fcntl(pipe.read->fd(), F_GETFD) & FD_CLOEXEC);
65 pipe.read->SetCloexec();
66 ASSERT_NE(0, fcntl(pipe.read->fd(), F_GETFD) & FD_CLOEXEC);
67}
68
Stephan Pleinesf63bde82024-01-13 15:59:33 -080069} // namespace aos::util::testing