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