blob: dde1788b99cd68ac7f52012de0d8a620c92038e7 [file] [log] [blame]
William A. Kennington III93ae9702023-12-22 16:41:17 -08001#include "util.hpp"
2
3#include <stdplus/exception.hpp>
4#include <stdplus/fd/gmock.hpp>
William A. Kennington IIIeac9d472020-08-03 13:57:14 -07005#include <stdplus/fd/ops.hpp>
William A. Kennington III93ae9702023-12-22 16:41:17 -08006#include <stdplus/numeric/endian.hpp>
William A. Kennington IIIeac9d472020-08-03 13:57:14 -07007
Patrick Williamsd1984dd2023-05-10 16:12:44 -05008#include <gtest/gtest.h>
9
William A. Kennington III93ae9702023-12-22 16:41:17 -080010namespace stdplus::fd
William A. Kennington IIIeac9d472020-08-03 13:57:14 -070011{
William A. Kennington III93ae9702023-12-22 16:41:17 -080012
13using testing::_;
14using testing::Ge;
15using testing::SizeIs;
16using std::literals::string_view_literals::operator""sv;
William A. Kennington IIIeac9d472020-08-03 13:57:14 -070017
18TEST(Flags, Flags)
19{
20 FdFlags f = FdFlags(0).set(FdFlag::CloseOnExec).unset(FdFlag::CloseOnExec);
21 f.set(FdFlag::CloseOnExec).unset(FdFlag::CloseOnExec);
22 EXPECT_EQ(0, static_cast<int>(f));
23}
24
William A. Kennington III1786e9a2023-12-17 23:23:33 -080025TEST(Connect, Success)
26{
27 testing::StrictMock<FdMock> fd;
28 SockAddrBuf buf;
29 EXPECT_CALL(fd, connect(_)).WillOnce([&](std::span<const std::byte> addr) {
30 std::copy(addr.begin(), addr.end(), reinterpret_cast<std::byte*>(&buf));
31 buf.len = addr.size();
32 });
33 Sock6Addr addr(In6Addr{0xff}, 365, 0);
34 connect(fd, addr);
35 EXPECT_EQ(Sock6Addr::fromBuf(buf), addr);
36}
37
William A. Kennington III93ae9702023-12-22 16:41:17 -080038TEST(ReadExact, Success)
39{
40 testing::StrictMock<FdMock> fd;
41 {
42 testing::InSequence seq;
43 EXPECT_CALL(fd, read(SizeIs(Ge(4)))).WillOnce(readSv("alph"));
44 EXPECT_CALL(fd, read(SizeIs(Ge(2)))).WillOnce(readSv("a "));
45 EXPECT_CALL(fd, read(SizeIs(Ge(3)))).WillOnce(readSv("one"));
46 }
47 char buf[9];
48 readExact(fd, buf);
49 EXPECT_EQ(std::string_view(buf, sizeof(buf)), "alpha one");
50}
51
52TEST(ReadExact, NotEnoughEof)
53{
54 testing::StrictMock<FdMock> fd;
55 {
56 testing::InSequence seq;
57 EXPECT_CALL(fd, read(_)).WillOnce(readSv("alph"));
58 EXPECT_CALL(fd, read(_)).WillOnce(readSv("a "));
59 EXPECT_CALL(fd, read(_))
60 .WillRepeatedly(testing::Throw(exception::Eof("test")));
61 }
62 char buf[9];
63 EXPECT_THROW(readExact(fd, buf), exception::Incomplete);
64 EXPECT_THROW(readExact(fd, buf), exception::Eof);
65}
66
67TEST(ReadExact, NotEnoughBlock)
68{
69 testing::StrictMock<FdMock> fd;
70 {
71 testing::InSequence seq;
72 EXPECT_CALL(fd, read(_)).WillOnce(readSv("alph"));
73 EXPECT_CALL(fd, read(_)).WillOnce(readSv("a "));
74 EXPECT_CALL(fd, read(_)).WillRepeatedly(readSv(""));
75 }
76 char buf[9];
77 EXPECT_THROW(readExact(fd, buf), exception::Incomplete);
78 EXPECT_THROW(readExact(fd, buf), exception::WouldBlock);
79}
80
81TEST(ReadExact, NotEnoughBlockInt)
82{
83 testing::StrictMock<FdMock> fd;
84 {
85 testing::InSequence seq;
86 EXPECT_CALL(fd, read(_)).WillOnce(readSv("\0\0\0"sv));
87 EXPECT_CALL(fd, read(_)).WillRepeatedly(readSv(""));
88 }
89 int32_t i;
90 EXPECT_THROW(readExact(fd, i), exception::Incomplete);
91 EXPECT_THROW(readExact(fd, i), exception::WouldBlock);
92}
93
94TEST(ReadExact, EnoughInt)
95{
96 testing::StrictMock<FdMock> fd;
97 {
98 testing::InSequence seq;
99 EXPECT_CALL(fd, read(_)).WillOnce(readSv("\0\0\0\0"sv));
100 EXPECT_CALL(fd, read(_)).WillOnce(readSv("\x01\x02"sv));
101 EXPECT_CALL(fd, read(_)).WillOnce(readSv("\x03\x04"sv));
102 }
103 int32_t i;
104 readExact(fd, i);
105 EXPECT_EQ(i, 0);
106 readExact(fd, i);
107 EXPECT_EQ(ntoh(i), 0x01020304);
108}
109
William A. Kennington IIIb88c4572023-12-22 16:43:42 -0800110TEST(Read, Success)
111{
112 testing::StrictMock<FdMock> fd;
113 {
114 testing::InSequence seq;
115 EXPECT_CALL(fd, read(_)).WillOnce(readSv("alph"));
116 EXPECT_CALL(fd, read(_)).WillOnce(readSv("one"));
117 EXPECT_CALL(fd, read(_)).WillOnce(readSv(""));
118 }
119 char buf[15];
120 auto res = read(fd, buf);
121 EXPECT_EQ(std::string_view(res.begin(), res.end()), "alph");
122 res = read(fd, buf);
123 EXPECT_EQ(std::string_view(res.begin(), res.end()), "one");
124 EXPECT_TRUE(read(fd, buf).empty());
125}
126
127TEST(Read, NotEnoughInt)
128{
129 testing::StrictMock<FdMock> fd;
130 {
131 testing::InSequence seq;
132 EXPECT_CALL(fd, read(_)).WillOnce(readSv("\0\0\0"sv));
133 EXPECT_CALL(fd, read(_)).WillRepeatedly(readSv(""));
134 }
135 std::array<int32_t, 1> i;
136 EXPECT_THROW(read(fd, i), exception::Incomplete);
137 EXPECT_TRUE(read(fd, i).empty());
138}
139
140TEST(Read, NotEnoughEofInt)
141{
142 testing::StrictMock<FdMock> fd;
143 {
144 testing::InSequence seq;
145 EXPECT_CALL(fd, read(_)).WillOnce(readSv("\0\0\0"sv));
146 EXPECT_CALL(fd, read(_))
147 .WillRepeatedly(testing::Throw(exception::Eof("test")));
148 }
149 std::array<int32_t, 1> i;
150 EXPECT_THROW(read(fd, i), exception::Incomplete);
151 EXPECT_THROW(read(fd, i), exception::Eof);
152}
153
154TEST(Read, EnoughInt)
155{
156 testing::StrictMock<FdMock> fd;
157 {
158 testing::InSequence seq;
159 EXPECT_CALL(fd, read(_)).WillOnce(readSv("\0\0\0\0"sv));
160 EXPECT_CALL(fd, read(_)).WillOnce(readSv("\x01\x02"sv));
161 EXPECT_CALL(fd, read(_)).WillOnce(readSv("\x03\x04"sv));
162 }
163 std::array<int32_t, 1> i;
164 read(fd, i);
165 EXPECT_EQ(i[0], 0);
166 read(fd, i);
167 EXPECT_EQ(stdplus::ntoh(i[0]), 0x01020304);
168}
169
William A. Kennington III93ae9702023-12-22 16:41:17 -0800170} // namespace stdplus::fd