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