X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=test%2Fsocket_test.cc;h=bcdbcd7246f7cde15ffac522d3824260f13f90cb;hb=4d8f96e15edb4807cc9773cc7f9eb6aa56ac2dc8;hp=b9aaba9d25d388fed38a80bbf066a9442f92f5ca;hpb=dd9be86db6cde0afa5da0d1d1ac43b42e05dca26;p=dcpomatic.git diff --git a/test/socket_test.cc b/test/socket_test.cc index b9aaba9d2..bcdbcd724 100644 --- a/test/socket_test.cc +++ b/test/socket_test.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2020 Carl Hetherington + Copyright (C) 2020-2021 Carl Hetherington This file is part of DCP-o-matic. @@ -18,17 +18,19 @@ */ -#include "lib/server.h" + #include "lib/dcpomatic_socket.h" +#include "lib/server.h" #include -#include #include +#include #include #include -using std::string; +using std::make_shared; using std::shared_ptr; +using std::string; using boost::bind; @@ -41,7 +43,7 @@ class TestServer : public Server public: TestServer (bool digest) : Server (TEST_SERVER_PORT, 30) - , _buffer (new uint8_t[TEST_SERVER_BUFFER_LENGTH]) + , _buffer (TEST_SERVER_BUFFER_LENGTH) , _size (0) , _result (false) , _digest (digest) @@ -56,7 +58,6 @@ public: try { _thread.join (); } catch (...) {} - delete[] _buffer; } void expect (int size) @@ -66,7 +67,7 @@ public: } uint8_t const * buffer() const { - return _buffer; + return _buffer.data(); } void await () @@ -82,18 +83,18 @@ public: } private: - void handle (std::shared_ptr socket) + void handle (std::shared_ptr socket) override { boost::mutex::scoped_lock lm (_mutex); BOOST_REQUIRE (_size); if (_digest) { Socket::ReadDigestScope ds (socket); - socket->read (_buffer, _size); + socket->read (_buffer.data(), _size); _size = 0; _condition.notify_one (); _result = ds.check(); } else { - socket->read (_buffer, _size); + socket->read (_buffer.data(), _size); _size = 0; _condition.notify_one (); } @@ -102,7 +103,7 @@ private: boost::thread _thread; boost::mutex _mutex; boost::condition _condition; - uint8_t* _buffer; + std::vector _buffer; int _size; bool _result; bool _digest; @@ -115,6 +116,7 @@ send (shared_ptr socket, char const* message) socket->write (reinterpret_cast(message), strlen(message) + 1); } + /** Basic test to see if Socket can send and receive data */ BOOST_AUTO_TEST_CASE (socket_basic_test) { @@ -128,7 +130,7 @@ BOOST_AUTO_TEST_CASE (socket_basic_test) tcp::resolver::query query ("127.0.0.1", dcp::raw_convert(TEST_SERVER_PORT)); tcp::resolver::iterator endpoint_iterator = resolver.resolve (query); - shared_ptr socket (new Socket); + auto socket = make_shared(); socket->connect (*endpoint_iterator); send (socket, "Hello world!");