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