blob: 80059f801ae3deecb268a5bee8c907f729a6ffa5 [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
James Kuszmaul731a05d2022-01-07 17:59:26 -08005#include <array>
6#include <string>
7
8#include "gtest/gtest.h"
9
Stephan Pleinesf63bde82024-01-13 15:59:33 -080010namespace aos::util::testing {
James Kuszmaul731a05d2022-01-07 17:59:26 -080011
12// Tests using uint32_t read/write methods on the ScopedPipe objects.
13TEST(ScopedPipeTest, IntegerPipe) {
James Kuszmauld42edb42022-01-07 18:00:16 -080014 ScopedPipe::PipePair pipe = ScopedPipe::MakePipe();
15 ASSERT_FALSE(pipe.read->Read().has_value())
James Kuszmaul731a05d2022-01-07 17:59:26 -080016 << "Shouldn't get anything on empty read.";
James Kuszmauld42edb42022-01-07 18:00:16 -080017 pipe.write->Write(971);
18 ASSERT_EQ(971, pipe.read->Read().value());
James Kuszmaul731a05d2022-01-07 17:59:26 -080019}
20
21// Tests using string read/write methods on the ScopedPipe objects.
22TEST(ScopedPipeTest, StringPipe) {
James Kuszmauld42edb42022-01-07 18:00:16 -080023 ScopedPipe::PipePair pipe = ScopedPipe::MakePipe();
James Kuszmaul731a05d2022-01-07 17:59:26 -080024 std::string buffer;
James Kuszmauld42edb42022-01-07 18:00:16 -080025 ASSERT_EQ(0u, pipe.read->Read(&buffer))
James Kuszmaul731a05d2022-01-07 17:59:26 -080026 << "Shouldn't get anything on empty read.";
27 ASSERT_TRUE(buffer.empty());
28
29 const char *const kAbc = "abcdef";
James Kuszmauld42edb42022-01-07 18:00:16 -080030 pipe.write->Write(
James Kuszmaul731a05d2022-01-07 17:59:26 -080031 absl::Span<const uint8_t>(reinterpret_cast<const uint8_t *>(kAbc), 6));
James Kuszmauld42edb42022-01-07 18:00:16 -080032 ASSERT_EQ(6u, pipe.read->Read(&buffer));
James Kuszmaul731a05d2022-01-07 17:59:26 -080033 ASSERT_EQ("abcdef", buffer);
34
35 std::array<uint8_t, 10000> large_buffer;
36 large_buffer.fill(99);
James Kuszmauld42edb42022-01-07 18:00:16 -080037 pipe.write->Write(
James Kuszmaul731a05d2022-01-07 17:59:26 -080038 absl::Span<const uint8_t>(large_buffer.data(), large_buffer.size()));
James Kuszmauld42edb42022-01-07 18:00:16 -080039 ASSERT_EQ(large_buffer.size(), pipe.read->Read(&buffer));
James Kuszmaul731a05d2022-01-07 17:59:26 -080040 for (size_t ii = 0; ii < large_buffer.size(); ++ii) {
41 ASSERT_EQ(large_buffer[ii], buffer[ii + 6]);
42 }
43}
44
Austin Schuh0d164112023-08-17 12:50:51 -070045// Tests using string read/write methods on the ScopedPipe objects.
46TEST(ScopedPipeTest, StringPipe2048) {
47 ScopedPipe::PipePair pipe = ScopedPipe::MakePipe();
48 std::string buffer;
49 ASSERT_EQ(0u, pipe.read->Read(&buffer))
50 << "Shouldn't get anything on empty read.";
51 ASSERT_TRUE(buffer.empty());
52
53 std::string a(2048, 'a');
54 pipe.write->Write(absl::Span<const uint8_t>(
55 reinterpret_cast<const uint8_t *>(a.data()), a.size()));
56 ASSERT_EQ(2048u, pipe.read->Read(&buffer));
57 ASSERT_EQ(a, buffer);
58}
59
James Kuszmauld42edb42022-01-07 18:00:16 -080060// Tests that calling SetCloexec succeeds and does indeed set FD_CLOEXEC.
61TEST(ScopedPipeTest, SetCloexec) {
62 ScopedPipe::PipePair pipe = ScopedPipe::MakePipe();
63 ASSERT_EQ(0, fcntl(pipe.read->fd(), F_GETFD) & FD_CLOEXEC);
64 pipe.read->SetCloexec();
65 ASSERT_NE(0, fcntl(pipe.read->fd(), F_GETFD) & FD_CLOEXEC);
66}
67
Stephan Pleinesf63bde82024-01-13 15:59:33 -080068} // namespace aos::util::testing