summaryrefslogtreecommitdiffhomepage
path: root/tests/test-webchat.cpp
blob: 3f70bcf0394e3a6a4c893d842c054f44510c11ea (plain)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include <gtest/gtest.h>

#include <cstring>
#include <filesystem>
#include <memory>
#include <regex>
#include <string>
#include <system_error>
#include <thread>

#include <boost/process/child.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/asio/buffers_iterator.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/local/stream_protocol.hpp>

#include <unistd.h>

#include "libreichwein/file.h"
#include "libreichwein/process.h"

#include "config.h"
#include "storage.h"
#include "whiteboard.h"

namespace bp = boost::process;
namespace fs = std::filesystem;
using namespace Reichwein;
using namespace std::string_literals;

namespace {
 const fs::path webserverConfigFilename{"./webserver.conf"};

 const fs::path testConfigFilename{"./whiteboard.conf"};
 const fs::path testDbFilename{"./whiteboard.db3"};
}

class WhiteboardTest: public ::testing::Test
{
protected:
 WhiteboardTest(){
 }

 ~WhiteboardTest() override{
 }

 void SetUp() override
 {
  File::setFile(testConfigFilename, R"CONFIG(
<config>
 <port>::1:9876</port>
 <datapath>.</datapath>
 <maxage>2592000</maxage>
 <threads>4</threads>
 <maxconnections>3</maxconnections>
</config>
)CONFIG");
  std::error_code ec;
  fs::remove(testDbFilename, ec);

  m_config = std::make_shared<Config>(testConfigFilename);

  m_pid = fork();
  if (m_pid == -1) {
   throw std::runtime_error("Error on fork(): "s + strerror(errno));
  } else if (m_pid == 0) { // child
   Whiteboard whiteboard;
   std::vector<std::string> argvv{{"whiteboard", "-c", testConfigFilename.generic_string()}};
   char* argv[] = {argvv[0].data(), argvv[1].data(), argvv[2].data(), nullptr};
   whiteboard.run(argvv.size(), argv);
   exit(0);
  }
  Process::wait_for_pid_listening_on(m_pid, 9876);
  std::this_thread::sleep_for(std::chrono::milliseconds(20));
 }

 void TearDown() override
 {
  if (m_pid == 0)
   throw std::runtime_error("Whiteboard not running on requesting SIGTERM");

  if (kill(m_pid, SIGTERM) != 0)
   throw std::runtime_error("Unable to SIGTERM Whiteboard");

  if (int result = waitpid(m_pid, NULL, 0); result != m_pid)
   throw std::runtime_error("waitpid returned "s + std::to_string(result));

  std::error_code ec;
  fs::remove(testDbFilename, ec);
  fs::remove(testConfigFilename, ec);
 }

 std::shared_ptr<Config> m_config;
 pid_t m_pid{};
};

class WebsocketClient
{
public:
 WebsocketClient()
 {
  std::string host = "::1";
  auto const  port = "9876" ;

  // These objects perform our I/O
  boost::asio::ip::tcp::resolver resolver{ioc_};
  ws_ = std::make_unique<boost::beast::websocket::stream<boost::asio::ip::tcp::socket>>(ioc_);

  // Look up the domain name
  resolver_results_ = resolver.resolve(host, port);

  connect();
  handshake();
 }

 void connect()
 {
  // Make the connection on the IP address we get from a lookup
  ep_ = boost::asio::connect(boost::beast::get_lowest_layer(*ws_), resolver_results_);
 }

 void handshake()
 {
  // Update the host_ string. This will provide the value of the
  // Host HTTP header during the WebSocket handshake.
  // See https://tools.ietf.org/html/rfc7230#section-5.4
  std::string host{"[::1]:9876"};

  // Set a decorator to change the User-Agent of the handshake
  ws_->set_option(boost::beast::websocket::stream_base::decorator(
      [](boost::beast::websocket::request_type& req)
      {
          req.set(boost::beast::http::field::user_agent,
              std::string("Reichwein.IT Test Websocket Client"));
      }));
  
  // Perform the websocket handshake
  ws_->handshake(host, "/");

 }

 void write(const std::string& data)
 {
  ws_->write(boost::asio::buffer(data));
 }

 std::string read()
 {
  boost::beast::flat_buffer buffer;
  ws_->read(buffer);
  return {boost::asio::buffers_begin(buffer.data()), boost::asio::buffers_end(buffer.data())};
 }

 ~WebsocketClient()
 {
 }

 bool is_open()
 {
  return ws_->is_open();
 }

private:
 boost::asio::io_context ioc_;
 boost::asio::ip::tcp::resolver::results_type resolver_results_;
 std::unique_ptr<boost::beast::websocket::stream<boost::asio::ip::tcp::socket>> ws_;
 boost::asio::ip::tcp::endpoint ep_;
};

//
// tests via websocket server in separate process (hides coverage)
//

TEST_F(WhiteboardTest, websocket_server_connection)
{
 WebsocketClient wc;
}

TEST_F(WhiteboardTest, websocket_server_generate_id)
{
 WebsocketClient wc;

 wc.write("<request><command>newid</command></request>");
 std::string result0 {wc.read()};
 ASSERT_TRUE(boost::algorithm::starts_with(result0, "<serverinfo><type>newid</type><id>"));
 ASSERT_TRUE(boost::algorithm::ends_with(result0, "</id></serverinfo>"));
 ASSERT_EQ(result0.size(), 58);
 
 wc.write("<request><command>newid</command></request>");
 std::string result1 {wc.read()};
 ASSERT_TRUE(boost::algorithm::starts_with(result1, "<serverinfo><type>newid</type><id>"));
 ASSERT_TRUE(boost::algorithm::ends_with(result1, "</id></serverinfo>"));
 ASSERT_EQ(result1.size(), 58);
 
 ASSERT_NE(result0, result1);
}

// check number of threads as configured
TEST_F(WhiteboardTest, threads)
{
 ASSERT_GE(Process::number_of_threads(m_pid), 4);
}

TEST_F(WhiteboardTest, max_connections)
{
 WebsocketClient wc1;
 std::this_thread::sleep_for(std::chrono::milliseconds(20));
 ASSERT_TRUE(wc1.is_open());
 
 WebsocketClient wc2;
 std::this_thread::sleep_for(std::chrono::milliseconds(20));
 ASSERT_TRUE(wc2.is_open());
 
 WebsocketClient wc3;
 std::this_thread::sleep_for(std::chrono::milliseconds(20));
 ASSERT_TRUE(wc3.is_open());
 
 ASSERT_THROW(WebsocketClient wc4, std::exception);
}

TEST_F(WhiteboardTest, id)
{
 WebsocketClient wc;

 wc.write("<request><command>getfile</command><id>1</id></request>");
 std::string result {wc.read()};

 EXPECT_EQ(result, "<serverinfo><type>getfile</type><data/><revision>0</revision><pos>0</pos></serverinfo>");
 
 wc.write("<request><command>getfile</command><id></id></request>");
 result = wc.read();
 EXPECT_EQ(result, "<serverinfo><type>error</type><message>Message handling error: Invalid id (empty)</message></serverinfo>");
 
 wc.write("<request><command>getfile</command><id>01234567890123456789</id></request>");
 result = wc.read();
 EXPECT_EQ(result, "<serverinfo><type>error</type><message>Message handling error: Invalid id (size &gt; 16)</message></serverinfo>");
 
 wc.write("<request><command>getfile</command><id>X</id></request>");
 result = wc.read();
 EXPECT_EQ(result, "<serverinfo><type>error</type><message>Message handling error: Invalid id char: X</message></serverinfo>");
 
 wc.write("<request><command>getfile</command><id>a.</id></request>");
 result = wc.read();
 EXPECT_EQ(result, "<serverinfo><type>error</type><message>Message handling error: Invalid id char: .</message></serverinfo>");
 
 wc.write("<request><command>getfile</command><id>a$b</id></request>");
 result = wc.read();
 EXPECT_EQ(result, "<serverinfo><type>error</type><message>Message handling error: Invalid id char: $</message></serverinfo>");
}