2 Copyright (C) 2020 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
21 #include "lib/server.h"
22 #include "lib/dcpomatic_socket.h"
23 #include <boost/thread.hpp>
24 #include <boost/test/unit_test.hpp>
25 #include <boost/shared_ptr.hpp>
29 using boost::shared_ptr;
32 #define TEST_SERVER_PORT 9142
33 #define TEST_SERVER_BUFFER_LENGTH 1024
36 class TestServer : public Server
39 TestServer (bool digest)
40 : Server (TEST_SERVER_PORT, 30)
41 , _buffer (new uint8_t[TEST_SERVER_BUFFER_LENGTH])
46 _thread = boost::thread(bind(&TestServer::run, this));
56 void expect (int size)
58 boost::mutex::scoped_lock lm (_mutex);
62 uint8_t const * buffer() const {
68 boost::mutex::scoped_lock lm (_mutex);
74 bool result () const {
79 void handle (boost::shared_ptr<Socket> socket)
81 boost::mutex::scoped_lock lm (_mutex);
82 BOOST_REQUIRE (_size);
84 Socket::ReadDigestScope ds (socket);
85 socket->read (_buffer, _size);
87 _condition.notify_one ();
90 socket->read (_buffer, _size);
92 _condition.notify_one ();
96 boost::thread _thread;
98 boost::condition _condition;
107 send (shared_ptr<Socket> socket, char const* message)
109 socket->write (reinterpret_cast<uint8_t const *>(message), strlen(message) + 1);
112 /** Basic test to see if Socket can send and receive data */
113 BOOST_AUTO_TEST_CASE (socket_basic_test)
115 TestServer server(false);
118 shared_ptr<Socket> socket (new Socket);
119 socket->connect (boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), TEST_SERVER_PORT));
120 send (socket, "Hello world!");
123 BOOST_CHECK_EQUAL(strcmp(reinterpret_cast<char const *>(server.buffer()), "Hello world!"), 0);
127 /** Check that the socket "auto-digest" creation works */
128 BOOST_AUTO_TEST_CASE (socket_digest_test1)
130 TestServer server(false);
131 server.expect (13 + 16);
133 shared_ptr<Socket> socket(new Socket);
134 socket->connect (boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), TEST_SERVER_PORT));
136 Socket::WriteDigestScope ds(socket);
137 send (socket, "Hello world!");
141 BOOST_CHECK_EQUAL(strcmp(reinterpret_cast<char const *>(server.buffer()), "Hello world!"), 0);
143 /* printf "%s\0" "Hello world!" | md5sum" in bash */
144 char ref[] = "\x59\x86\x88\xed\x18\xc8\x71\xdd\x57\xb9\xb7\x9f\x4b\x03\x14\xcf";
145 BOOST_CHECK_EQUAL (memcmp(server.buffer() + 13, ref, 16), 0);
149 /** Check that the socket "auto-digest" round-trip works */
150 BOOST_AUTO_TEST_CASE (socket_digest_test2)
152 TestServer server(true);
155 shared_ptr<Socket> socket(new Socket);
156 socket->connect (boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), TEST_SERVER_PORT));
158 Socket::WriteDigestScope ds(socket);
159 send (socket, "Hello world!");
163 BOOST_CHECK_EQUAL(strcmp(reinterpret_cast<char const *>(server.buffer()), "Hello world!"), 0);
165 BOOST_CHECK (server.result());