872136a7196c02f2beaed2b4962425e6bee3475a
[dcpomatic.git] / test / socket_test.cc
1 /*
2     Copyright (C) 2020 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 #include "lib/server.h"
22 #include "lib/dcpomatic_socket.h"
23 #include <dcp/raw_convert.h>
24 #include <boost/thread.hpp>
25 #include <boost/test/unit_test.hpp>
26 #include <boost/shared_ptr.hpp>
27 #include <cstring>
28 #include <iostream>
29
30
31 using std::string;
32 using boost::shared_ptr;
33 using boost::bind;
34
35
36 #define TEST_SERVER_PORT 9142
37 #define TEST_SERVER_BUFFER_LENGTH 1024
38
39
40 class TestServer : public Server
41 {
42 public:
43         TestServer (bool digest)
44                 : Server (TEST_SERVER_PORT, 30)
45                 , _buffer (new uint8_t[TEST_SERVER_BUFFER_LENGTH])
46                 , _size (0)
47                 , _result (false)
48                 , _digest (digest)
49         {
50                 _thread = boost::thread(bind(&TestServer::run, this));
51         }
52
53         ~TestServer ()
54         {
55                 boost::this_thread::disable_interruption dis;
56                 stop ();
57                 try {
58                         _thread.join ();
59                 } catch (...) {}
60                 delete[] _buffer;
61         }
62
63         void expect (int size)
64         {
65                 boost::mutex::scoped_lock lm (_mutex);
66                 _size = size;
67         }
68
69         uint8_t const * buffer() const {
70                 return _buffer;
71         }
72
73         void await ()
74         {
75                 boost::mutex::scoped_lock lm (_mutex);
76                 if (_size) {
77                         _condition.wait (lm);
78                 }
79         }
80
81         bool result () const {
82                 return _result;
83         }
84
85 private:
86         void handle (boost::shared_ptr<Socket> socket)
87         {
88                 boost::mutex::scoped_lock lm (_mutex);
89                 BOOST_REQUIRE (_size);
90                 if (_digest) {
91                         Socket::ReadDigestScope ds (socket);
92                         socket->read (_buffer, _size);
93                         _size = 0;
94                         _condition.notify_one ();
95                         _result = ds.check();
96                 } else {
97                         socket->read (_buffer, _size);
98                         _size = 0;
99                         _condition.notify_one ();
100                 }
101         }
102
103         boost::thread _thread;
104         boost::mutex _mutex;
105         boost::condition _condition;
106         uint8_t* _buffer;
107         int _size;
108         bool _result;
109         bool _digest;
110 };
111
112
113 void
114 send (shared_ptr<Socket> socket, char const* message)
115 {
116         socket->write (reinterpret_cast<uint8_t const *>(message), strlen(message) + 1);
117 }
118
119 /** Basic test to see if Socket can send and receive data */
120 BOOST_AUTO_TEST_CASE (socket_basic_test)
121 {
122         using boost::asio::ip::tcp;
123
124         TestServer server(false);
125         server.expect (13);
126
127         boost::asio::io_service io_service;
128         tcp::resolver resolver (io_service);
129         tcp::resolver::query query ("127.0.0.1", dcp::raw_convert<string>(TEST_SERVER_PORT));
130         tcp::resolver::iterator endpoint_iterator = resolver.resolve (query);
131
132         shared_ptr<Socket> socket (new Socket);
133         socket->connect (*endpoint_iterator);
134         send (socket, "Hello world!");
135
136         server.await ();
137         BOOST_CHECK_EQUAL(strcmp(reinterpret_cast<char const *>(server.buffer()), "Hello world!"), 0);
138 }
139
140
141 /** Check that the socket "auto-digest" creation works */
142 BOOST_AUTO_TEST_CASE (socket_digest_test1)
143 {
144         using boost::asio::ip::tcp;
145
146         TestServer server(false);
147         server.expect (13 + 16);
148
149         boost::asio::io_service io_service;
150         tcp::resolver resolver (io_service);
151         tcp::resolver::query query ("127.0.0.1", dcp::raw_convert<string>(TEST_SERVER_PORT));
152         tcp::resolver::iterator endpoint_iterator = resolver.resolve (query);
153
154         shared_ptr<Socket> socket(new Socket);
155         socket->connect (*endpoint_iterator);
156         {
157                 Socket::WriteDigestScope ds(socket);
158                 send (socket, "Hello world!");
159         }
160
161         server.await ();
162         BOOST_CHECK_EQUAL(strcmp(reinterpret_cast<char const *>(server.buffer()), "Hello world!"), 0);
163
164         /* printf "%s\0" "Hello world!" | md5sum" in bash */
165         char ref[] = "\x59\x86\x88\xed\x18\xc8\x71\xdd\x57\xb9\xb7\x9f\x4b\x03\x14\xcf";
166         BOOST_CHECK_EQUAL (memcmp(server.buffer() + 13, ref, 16), 0);
167 }
168
169
170 /** Check that the socket "auto-digest" round-trip works */
171 BOOST_AUTO_TEST_CASE (socket_digest_test2)
172 {
173         using boost::asio::ip::tcp;
174
175         TestServer server(true);
176         server.expect (13);
177
178         boost::asio::io_service io_service;
179         tcp::resolver resolver (io_service);
180         tcp::resolver::query query ("127.0.0.1", dcp::raw_convert<string>(TEST_SERVER_PORT));
181         tcp::resolver::iterator endpoint_iterator = resolver.resolve (query);
182
183         shared_ptr<Socket> socket(new Socket);
184         socket->connect (*endpoint_iterator);
185         {
186                 Socket::WriteDigestScope ds(socket);
187                 send (socket, "Hello world!");
188         }
189
190         server.await ();
191         BOOST_CHECK_EQUAL(strcmp(reinterpret_cast<char const *>(server.buffer()), "Hello world!"), 0);
192
193         BOOST_CHECK (server.result());
194 }
195