2 Copyright (C) 2020-2021 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/>.
22 #include "lib/dcpomatic_socket.h"
23 #include "lib/server.h"
24 #include <dcp/raw_convert.h>
25 #include <boost/test/unit_test.hpp>
26 #include <boost/thread.hpp>
31 using std::make_shared;
32 using std::shared_ptr;
37 #define TEST_SERVER_PORT 9142
38 #define TEST_SERVER_BUFFER_LENGTH 1024
41 class TestServer : public Server
44 TestServer (bool digest)
45 : Server (TEST_SERVER_PORT, 30)
46 , _buffer (TEST_SERVER_BUFFER_LENGTH)
51 _thread = boost::thread(bind(&TestServer::run, this));
56 boost::this_thread::disable_interruption dis;
63 void expect (int size)
65 boost::mutex::scoped_lock lm (_mutex);
69 uint8_t const * buffer() const {
70 return _buffer.data();
75 boost::mutex::scoped_lock lm (_mutex);
81 bool result () const {
86 void handle (std::shared_ptr<Socket> socket) override
88 boost::mutex::scoped_lock lm (_mutex);
89 BOOST_REQUIRE (_size);
91 Socket::ReadDigestScope ds (socket);
92 socket->read (_buffer.data(), _size);
94 _condition.notify_one ();
97 socket->read (_buffer.data(), _size);
99 _condition.notify_one ();
103 boost::thread _thread;
105 boost::condition _condition;
106 std::vector<uint8_t> _buffer;
114 send (shared_ptr<Socket> socket, char const* message)
116 socket->write (reinterpret_cast<uint8_t const *>(message), strlen(message) + 1);
120 /** Basic test to see if Socket can send and receive data */
121 BOOST_AUTO_TEST_CASE (socket_basic_test)
123 using boost::asio::ip::tcp;
125 TestServer server(false);
128 boost::asio::io_service io_service;
129 tcp::resolver resolver (io_service);
130 tcp::resolver::query query ("127.0.0.1", dcp::raw_convert<string>(TEST_SERVER_PORT));
131 tcp::resolver::iterator endpoint_iterator = resolver.resolve (query);
133 auto socket = make_shared<Socket>();
134 socket->connect (*endpoint_iterator);
135 send (socket, "Hello world!");
138 BOOST_CHECK_EQUAL(strcmp(reinterpret_cast<char const *>(server.buffer()), "Hello world!"), 0);
142 /** Check that the socket "auto-digest" creation works */
143 BOOST_AUTO_TEST_CASE (socket_digest_test1)
145 using boost::asio::ip::tcp;
147 TestServer server(false);
148 server.expect (13 + 16);
150 boost::asio::io_service io_service;
151 tcp::resolver resolver (io_service);
152 tcp::resolver::query query ("127.0.0.1", dcp::raw_convert<string>(TEST_SERVER_PORT));
153 tcp::resolver::iterator endpoint_iterator = resolver.resolve (query);
155 shared_ptr<Socket> socket(new Socket);
156 socket->connect (*endpoint_iterator);
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 /* printf "%s\0" "Hello world!" | md5sum" in bash */
166 char ref[] = "\x59\x86\x88\xed\x18\xc8\x71\xdd\x57\xb9\xb7\x9f\x4b\x03\x14\xcf";
167 BOOST_CHECK_EQUAL (memcmp(server.buffer() + 13, ref, 16), 0);
171 /** Check that the socket "auto-digest" round-trip works */
172 BOOST_AUTO_TEST_CASE (socket_digest_test2)
174 using boost::asio::ip::tcp;
176 TestServer server(true);
179 boost::asio::io_service io_service;
180 tcp::resolver resolver (io_service);
181 tcp::resolver::query query ("127.0.0.1", dcp::raw_convert<string>(TEST_SERVER_PORT));
182 tcp::resolver::iterator endpoint_iterator = resolver.resolve (query);
184 shared_ptr<Socket> socket(new Socket);
185 socket->connect (*endpoint_iterator);
187 Socket::WriteDigestScope ds(socket);
188 send (socket, "Hello world!");
192 BOOST_CHECK_EQUAL(strcmp(reinterpret_cast<char const *>(server.buffer()), "Hello world!"), 0);
194 BOOST_CHECK (server.result());