blob: 0e024c2d040034d2e584a30e893f71a28f66f646 [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
10namespace aos {
11namespace util {
12namespace testing {
13
14// Tests using uint32_t read/write methods on the ScopedPipe objects.
15TEST(ScopedPipeTest, IntegerPipe) {
James Kuszmauld42edb42022-01-07 18:00:16 -080016 ScopedPipe::PipePair pipe = ScopedPipe::MakePipe();
17 ASSERT_FALSE(pipe.read->Read().has_value())
James Kuszmaul731a05d2022-01-07 17:59:26 -080018 << "Shouldn't get anything on empty read.";
James Kuszmauld42edb42022-01-07 18:00:16 -080019 pipe.write->Write(971);
20 ASSERT_EQ(971, pipe.read->Read().value());
James Kuszmaul731a05d2022-01-07 17:59:26 -080021}
22
23// Tests using string read/write methods on the ScopedPipe objects.
24TEST(ScopedPipeTest, StringPipe) {
James Kuszmauld42edb42022-01-07 18:00:16 -080025 ScopedPipe::PipePair pipe = ScopedPipe::MakePipe();
James Kuszmaul731a05d2022-01-07 17:59:26 -080026 std::string buffer;
James Kuszmauld42edb42022-01-07 18:00:16 -080027 ASSERT_EQ(0u, pipe.read->Read(&buffer))
James Kuszmaul731a05d2022-01-07 17:59:26 -080028 << "Shouldn't get anything on empty read.";
29 ASSERT_TRUE(buffer.empty());
30
31 const char *const kAbc = "abcdef";
James Kuszmauld42edb42022-01-07 18:00:16 -080032 pipe.write->Write(
James Kuszmaul731a05d2022-01-07 17:59:26 -080033 absl::Span<const uint8_t>(reinterpret_cast<const uint8_t *>(kAbc), 6));
James Kuszmauld42edb42022-01-07 18:00:16 -080034 ASSERT_EQ(6u, pipe.read->Read(&buffer));
James Kuszmaul731a05d2022-01-07 17:59:26 -080035 ASSERT_EQ("abcdef", buffer);
36
37 std::array<uint8_t, 10000> large_buffer;
38 large_buffer.fill(99);
James Kuszmauld42edb42022-01-07 18:00:16 -080039 pipe.write->Write(
James Kuszmaul731a05d2022-01-07 17:59:26 -080040 absl::Span<const uint8_t>(large_buffer.data(), large_buffer.size()));
James Kuszmauld42edb42022-01-07 18:00:16 -080041 ASSERT_EQ(large_buffer.size(), pipe.read->Read(&buffer));
James Kuszmaul731a05d2022-01-07 17:59:26 -080042 for (size_t ii = 0; ii < large_buffer.size(); ++ii) {
43 ASSERT_EQ(large_buffer[ii], buffer[ii + 6]);
44 }
45}
46
Austin Schuh0d164112023-08-17 12:50:51 -070047// Tests using string read/write methods on the ScopedPipe objects.
48TEST(ScopedPipeTest, StringPipe2048) {
49 ScopedPipe::PipePair pipe = ScopedPipe::MakePipe();
50 std::string buffer;
51 ASSERT_EQ(0u, pipe.read->Read(&buffer))
52 << "Shouldn't get anything on empty read.";
53 ASSERT_TRUE(buffer.empty());
54
55 std::string a(2048, 'a');
56 pipe.write->Write(absl::Span<const uint8_t>(
57 reinterpret_cast<const uint8_t *>(a.data()), a.size()));
58 ASSERT_EQ(2048u, pipe.read->Read(&buffer));
59 ASSERT_EQ(a, buffer);
60}
61
James Kuszmauld42edb42022-01-07 18:00:16 -080062// Tests that calling SetCloexec succeeds and does indeed set FD_CLOEXEC.
63TEST(ScopedPipeTest, SetCloexec) {
64 ScopedPipe::PipePair pipe = ScopedPipe::MakePipe();
65 ASSERT_EQ(0, fcntl(pipe.read->fd(), F_GETFD) & FD_CLOEXEC);
66 pipe.read->SetCloexec();
67 ASSERT_NE(0, fcntl(pipe.read->fd(), F_GETFD) & FD_CLOEXEC);
68}
69
James Kuszmaul731a05d2022-01-07 17:59:26 -080070} // namespace testing
71} // namespace util
72} // namespace aos