Fill test disk partitions with random noise to expose more bugs.
[dcpomatic.git] / test / socket_test.cc
index b9aaba9d25d388fed38a80bbf066a9442f92f5ca..bcdbcd7246f7cde15ffac522d3824260f13f90cb 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2020 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2020-2021 Carl Hetherington <cth@carlh.net>
 
     This file is part of DCP-o-matic.
 
 
 */
 
-#include "lib/server.h"
+
 #include "lib/dcpomatic_socket.h"
+#include "lib/server.h"
 #include <dcp/raw_convert.h>
-#include <boost/thread.hpp>
 #include <boost/test/unit_test.hpp>
+#include <boost/thread.hpp>
 #include <cstring>
 #include <iostream>
 
 
-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> socket)
+       void handle (std::shared_ptr<Socket> 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<uint8_t> _buffer;
        int _size;
        bool _result;
        bool _digest;
@@ -115,6 +116,7 @@ send (shared_ptr<Socket> socket, char const* message)
        socket->write (reinterpret_cast<uint8_t const *>(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<string>(TEST_SERVER_PORT));
        tcp::resolver::iterator endpoint_iterator = resolver.resolve (query);
 
-       shared_ptr<Socket> socket (new Socket);
+       auto socket = make_shared<Socket>();
        socket->connect (*endpoint_iterator);
        send (socket, "Hello world!");