mirror of
https://github.com/bitcoin/bitcoin.git
synced 2024-11-20 10:38:42 +01:00
test: add a test to ensure RecvUntilTerminator() limit works
This commit is contained in:
parent
80a5a8ea2b
commit
7059e6d822
@ -4,11 +4,13 @@
|
|||||||
|
|
||||||
#include <compat.h>
|
#include <compat.h>
|
||||||
#include <test/util/setup_common.h>
|
#include <test/util/setup_common.h>
|
||||||
|
#include <threadinterrupt.h>
|
||||||
#include <util/sock.h>
|
#include <util/sock.h>
|
||||||
#include <util/system.h>
|
#include <util/system.h>
|
||||||
|
|
||||||
#include <boost/test/unit_test.hpp>
|
#include <boost/test/unit_test.hpp>
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
using namespace std::chrono_literals;
|
using namespace std::chrono_literals;
|
||||||
@ -144,6 +146,35 @@ BOOST_AUTO_TEST_CASE(wait)
|
|||||||
waiter.join();
|
waiter.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(recv_until_terminator_limit)
|
||||||
|
{
|
||||||
|
constexpr auto timeout = 1min; // High enough so that it is never hit.
|
||||||
|
CThreadInterrupt interrupt;
|
||||||
|
int s[2];
|
||||||
|
CreateSocketPair(s);
|
||||||
|
|
||||||
|
Sock sock_send(s[0]);
|
||||||
|
Sock sock_recv(s[1]);
|
||||||
|
|
||||||
|
std::thread receiver([&sock_recv, &timeout, &interrupt]() {
|
||||||
|
constexpr size_t max_data{10};
|
||||||
|
bool threw_as_expected{false};
|
||||||
|
// BOOST_CHECK_EXCEPTION() writes to some variables shared with the main thread which
|
||||||
|
// creates a data race. So mimic it manually.
|
||||||
|
try {
|
||||||
|
sock_recv.RecvUntilTerminator('\n', timeout, interrupt, max_data);
|
||||||
|
} catch (const std::runtime_error& e) {
|
||||||
|
threw_as_expected = HasReason("too many bytes without a terminator")(e);
|
||||||
|
}
|
||||||
|
assert(threw_as_expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
BOOST_REQUIRE_NO_THROW(sock_send.SendComplete("1234567", timeout, interrupt));
|
||||||
|
BOOST_REQUIRE_NO_THROW(sock_send.SendComplete("89a\n", timeout, interrupt));
|
||||||
|
|
||||||
|
receiver.join();
|
||||||
|
}
|
||||||
|
|
||||||
#endif /* WIN32 */
|
#endif /* WIN32 */
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE_END()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
Loading…
Reference in New Issue
Block a user