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