File: tcp_server_tests.cpp

package info (click to toggle)
pion 5.0.7%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,048 kB
  • ctags: 3,122
  • sloc: cpp: 14,237; perl: 269; pascal: 152; makefile: 145; sh: 70
file content (531 lines) | stat: -rw-r--r-- 20,341 bytes parent folder | download
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
// ---------------------------------------------------------------------
// pion:  a Boost C++ framework for building lightweight HTTP interfaces
// ---------------------------------------------------------------------
// Copyright (C) 2007-2014 Splunk Inc.  (https://github.com/splunk/pion)
//
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
//

#include <pion/config.hpp>
#include <pion/scheduler.hpp>
#include <pion/tcp/server.hpp>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/function/function1.hpp>
#include <boost/thread/thread.hpp>
#include <boost/test/unit_test.hpp>
#include <pion/http/request.hpp>
#include <pion/http/response.hpp>

using namespace std;
using namespace pion;


///
/// HelloServer: simple TCP server that sends "Hello there!" after receiving some data
/// 
class HelloServer
    : public pion::tcp::server
{
public:
    virtual ~HelloServer() {}

    /**
     * creates a Hello server
     *
     * @param tcp_port port number used to listen for new connections (IPv4)
     */
    HelloServer(const unsigned int tcp_port = 0) : pion::tcp::server(tcp_port) {}
    
    /**
     * handles a new TCP connection
     * 
     * @param tcp_conn the new TCP connection to handle
     */
    virtual void handle_connection(const pion::tcp::connection_ptr& tcp_conn) {
        static const std::string HELLO_MESSAGE("Hello there!\n");
        tcp_conn->set_lifecycle(pion::tcp::connection::LIFECYCLE_CLOSE);  // make sure it will get closed
        tcp_conn->async_write(boost::asio::buffer(HELLO_MESSAGE),
                              boost::bind(&HelloServer::handle_write, this, tcp_conn,
                                          boost::asio::placeholders::error));
    }

    
private:
    
    /**
     * called after the initial greeting has been sent
     *
     * @param tcp_conn the TCP connection to the server
     * @param write_error message that explains what went wrong (if anything)
     */
    void handle_write(const pion::tcp::connection_ptr& tcp_conn,
                     const boost::system::error_code& write_error)
    {
        if (write_error) {
            tcp_conn->finish();
        } else {
            tcp_conn->async_read_some(boost::bind(&HelloServer::handleRead, this, tcp_conn,
                                                  boost::asio::placeholders::error,
                                                  boost::asio::placeholders::bytes_transferred));
        }
    }
    
    /**
     * called after the client's greeting has been received
     *
     * @param tcp_conn the TCP connection to the server
     * @param read_error message that explains what went wrong (if anything)
     * @param bytes_read number of bytes read from the client
     */
    void handleRead(const pion::tcp::connection_ptr& tcp_conn,
                    const boost::system::error_code& read_error,
                    std::size_t bytes_read)
    {
        static const std::string GOODBYE_MESSAGE("Goodbye!\n");
        if (read_error) {
            tcp_conn->finish();
        } else if (bytes_read == 5 && memcmp(tcp_conn->get_read_buffer().data(), "throw", 5) == 0) {
            throw int(1);
        } else {
            tcp_conn->async_write(boost::asio::buffer(GOODBYE_MESSAGE),
                                  boost::bind(&pion::tcp::connection::finish, tcp_conn));
        }
    }
};


///
/// HelloServerTests_F: fixture used for running (Hello) server tests
/// 
class HelloServerTests_F {
public:
    // default constructor and destructor
    HelloServerTests_F()
        : hello_server_ptr(new HelloServer)
    {
        // start the HTTP server
        hello_server_ptr->start();
    }
    ~HelloServerTests_F() {
        hello_server_ptr->stop();
    }
    inline tcp::server_ptr& getServerPtr(void) { return hello_server_ptr; }

    /**
     * check at 0.1 second intervals for up to one second to see if the number
     * of connections is as expected
     *
     * @param expectedNumberOfConnections expected number of connections
     */
    void checkNumConnectionsForUpToOneSecond(std::size_t expectedNumberOfConnections)
    {
        for (int i = 0; i < 10; ++i) {
            if (getServerPtr()->get_connections() == expectedNumberOfConnections) break;
            scheduler::sleep(0, 100000000); // 0.1 seconds
        }
        BOOST_CHECK_EQUAL(getServerPtr()->get_connections(), expectedNumberOfConnections);
    }

private:
    tcp::server_ptr    hello_server_ptr;
};


// HelloServer Test Cases

BOOST_FIXTURE_TEST_SUITE(HelloServerTests_S, HelloServerTests_F)

BOOST_AUTO_TEST_CASE(checkTcpServerIsListening) {
    BOOST_CHECK(getServerPtr()->is_listening());
}

BOOST_AUTO_TEST_CASE(checkNumberOfActiveServerConnections) {
    // there should be no connections to start, but wait if needed
    // just in case other tests ran before this one, which are still connected
    checkNumConnectionsForUpToOneSecond(static_cast<std::size_t>(0));

    // open a connection
    boost::asio::ip::tcp::endpoint localhost(boost::asio::ip::address::from_string("127.0.0.1"), getServerPtr()->get_port());
    boost::asio::ip::tcp::iostream tcp_stream_a(localhost);
    // we need to wait for the server to accept the connection since it happens
    // in another thread.  This should always take less than one second.
    checkNumConnectionsForUpToOneSecond(static_cast<std::size_t>(1));

    // open a few more connections;
    boost::asio::ip::tcp::iostream tcp_stream_b(localhost);
    checkNumConnectionsForUpToOneSecond(static_cast<std::size_t>(2));

    boost::asio::ip::tcp::iostream tcp_stream_c(localhost);
    checkNumConnectionsForUpToOneSecond(static_cast<std::size_t>(3));

    boost::asio::ip::tcp::iostream tcp_stream_d(localhost);
    checkNumConnectionsForUpToOneSecond(static_cast<std::size_t>(4));
    
    // close connections    
    tcp_stream_a.close();
    checkNumConnectionsForUpToOneSecond(static_cast<std::size_t>(3));

    tcp_stream_b.close();
    checkNumConnectionsForUpToOneSecond(static_cast<std::size_t>(2));

    tcp_stream_c.close();
    checkNumConnectionsForUpToOneSecond(static_cast<std::size_t>(1));

    tcp_stream_d.close();
    checkNumConnectionsForUpToOneSecond(static_cast<std::size_t>(0));
}

BOOST_AUTO_TEST_CASE(checkServerConnectionBehavior) {
    // open a connection
    boost::asio::ip::tcp::endpoint localhost(boost::asio::ip::address::from_string("127.0.0.1"), getServerPtr()->get_port());
    boost::asio::ip::tcp::iostream tcp_stream_a(localhost);

    // read greeting from the server
    std::string str;
    std::getline(tcp_stream_a, str);
    BOOST_CHECK(str == "Hello there!");

    // open a second connection & read the greeting
    boost::asio::ip::tcp::iostream tcp_stream_b(localhost);
    std::getline(tcp_stream_b, str);
    BOOST_CHECK(str == "Hello there!");

    // send greeting to the first server
    tcp_stream_a << "Hi!\n";
    tcp_stream_a.flush();

    // send greeting to the second server
    tcp_stream_b << "Hi!\n";
    tcp_stream_b.flush();
    
    // receive goodbye from the first server
    std::getline(tcp_stream_a, str);
    tcp_stream_a.close();
    BOOST_CHECK(str == "Goodbye!");

    // receive goodbye from the second server
    std::getline(tcp_stream_b, str);
    tcp_stream_b.close();
    BOOST_CHECK(str == "Goodbye!");
}

BOOST_AUTO_TEST_CASE(checkServerExceptionsGetCaught) {
    // open a connection
    boost::asio::ip::tcp::endpoint localhost(boost::asio::ip::address::from_string("127.0.0.1"), getServerPtr()->get_port());
    boost::asio::ip::tcp::iostream tcp_stream_a(localhost);

    // read greeting from the server
    std::string str;
    std::getline(tcp_stream_a, str);
    BOOST_CHECK(str == "Hello there!");

    // send throw request to the server
    tcp_stream_a << "throw";
    tcp_stream_a.flush();
    tcp_stream_a.close();
}

BOOST_AUTO_TEST_SUITE_END()

///
/// MockSyncServer: simple TCP server that synchronously receives HTTP requests using http::message::receive(),
/// and checks that the received request object has some expected properties.
///
class MockSyncServer
    : public pion::tcp::server
{
public:
    /**
     * MockSyncServer constructor
     *
     * @param sched the scheduler that will be used to manage worker threads
     * @param tcp_port port number used to listen for new connections (IPv4)
     */
    MockSyncServer(scheduler& sched, const unsigned int tcp_port = 0)
        : pion::tcp::server(sched, tcp_port) {}
    
    virtual ~MockSyncServer() {}

    /**
     * handles a new TCP connection
     * 
     * @param tcp_conn the new TCP connection to handle
     */
    virtual void handle_connection(const pion::tcp::connection_ptr& tcp_conn) {
        // wait until an HTTP request is received or an error occurs
        boost::system::error_code error_code;
        http::request http_request;
        http_request.receive(*tcp_conn, error_code);
        BOOST_REQUIRE(!error_code);

        // check the received request for expected headers and content
        for (std::map<std::string, std::string>::const_iterator i = m_expectedHeaders.begin(); i != m_expectedHeaders.end(); ++i) {
            BOOST_CHECK_EQUAL(http_request.get_header(i->first), i->second);
        }
        BOOST_CHECK_EQUAL(m_expectedContent, http_request.get_content());

        if (m_additional_request_test)
            BOOST_CHECK(m_additional_request_test(http_request));

        // send a simple response as evidence that this part of the code was reached
        static const std::string GOODBYE_MESSAGE("Goodbye!\n");
        tcp_conn->write(boost::asio::buffer(GOODBYE_MESSAGE), error_code);

        // wrap up
        tcp_conn->set_lifecycle(pion::tcp::connection::LIFECYCLE_CLOSE);
        tcp_conn->finish();
    }

    void setExpectations(const std::map<std::string, std::string>& expectedHeaders, 
                         const std::string& expectedContent,
                         boost::function1<bool, http::request&> additional_request_test = NULL)
    {
        m_expectedHeaders = expectedHeaders;
        m_expectedContent = expectedContent;
        m_additional_request_test = additional_request_test;
    }

private:
    std::map<std::string, std::string> m_expectedHeaders;
    std::string m_expectedContent;
    boost::function1<bool, http::request&> m_additional_request_test;
};


///
/// MockSyncServerTests_F: fixture used for running MockSyncServer tests
/// 
class MockSyncServerTests_F {
public:
    MockSyncServerTests_F()
        : m_scheduler(), m_sync_server_ptr(new MockSyncServer(m_scheduler))
    {
        m_sync_server_ptr->start();
    }
    ~MockSyncServerTests_F() {
        m_sync_server_ptr->stop();
    }
    inline boost::shared_ptr<MockSyncServer>& getServerPtr(void) { return m_sync_server_ptr; }
    inline boost::asio::io_service& get_io_service(void) { return m_scheduler.get_io_service(); }

private:
    single_service_scheduler          m_scheduler;
    boost::shared_ptr<MockSyncServer>   m_sync_server_ptr;
};


// MockSyncServer Test Cases

BOOST_FIXTURE_TEST_SUITE(MockSyncServerTests_S, MockSyncServerTests_F)

BOOST_AUTO_TEST_CASE(checkMockSyncServerIsListening) {
    BOOST_CHECK(getServerPtr()->is_listening());
}

BOOST_AUTO_TEST_CASE(checkReceivedRequestUsingStream) {
    // open a connection
    boost::asio::ip::tcp::endpoint localhost(boost::asio::ip::address::from_string("127.0.0.1"), getServerPtr()->get_port());
    boost::asio::ip::tcp::iostream tcp_stream(localhost);

    // set expectations for received request
    std::map<std::string, std::string> expectedHeaders;
    expectedHeaders[http::types::HEADER_CONTENT_LENGTH] = "8";
    expectedHeaders[http::types::HEADER_TRANSFER_ENCODING] = ""; // i.e. check that there is no transfer encoding header
    getServerPtr()->setExpectations(expectedHeaders, "12345678");
    
    // send request to the server
    tcp_stream << "POST /resource1 HTTP/1.1" << http::types::STRING_CRLF;
    tcp_stream << http::types::HEADER_CONTENT_LENGTH << ": 8" << http::types::STRING_CRLF << http::types::STRING_CRLF;
    tcp_stream << "12345678";
    tcp_stream.flush();

    // receive goodbye from the server
    std::string str;
    std::getline(tcp_stream, str);
    BOOST_CHECK(str == "Goodbye!");
    tcp_stream.close();
}

BOOST_AUTO_TEST_CASE(checkReceivedRequestUsingChunkedStream) {
    // open a connection
    boost::asio::ip::tcp::endpoint localhost(boost::asio::ip::address::from_string("127.0.0.1"), getServerPtr()->get_port());
    boost::asio::ip::tcp::iostream tcp_stream(localhost);

    // set expectations for received request
    std::map<std::string, std::string> expectedHeaders;
    expectedHeaders[http::types::HEADER_TRANSFER_ENCODING] = "chunked";
    expectedHeaders[http::types::HEADER_CONTENT_LENGTH] = ""; // i.e. check that there is no content length header
    getServerPtr()->setExpectations(expectedHeaders, "abcdefghijklmno");
    
    // send request to the server
    tcp_stream << "POST /resource1 HTTP/1.1" << http::types::STRING_CRLF;
    tcp_stream << http::types::HEADER_TRANSFER_ENCODING << ": chunked" << http::types::STRING_CRLF << http::types::STRING_CRLF;
    // write first chunk size
    tcp_stream << "A" << http::types::STRING_CRLF;
    // write first chunk 
    tcp_stream << "abcdefghij" << http::types::STRING_CRLF;
    // write second chunk size
    tcp_stream << "5" << http::types::STRING_CRLF;
    // write second chunk 
    tcp_stream << "klmno" << http::types::STRING_CRLF;
    // write final chunk size
    tcp_stream << "0" << http::types::STRING_CRLF;
    tcp_stream << http::types::STRING_CRLF;
    tcp_stream.flush();

    // receive goodbye from the server
    std::string str;
    std::getline(tcp_stream, str);
    BOOST_CHECK(str == "Goodbye!");
    tcp_stream.close();
}

BOOST_AUTO_TEST_CASE(checkReceivedRequestUsingExtraWhiteSpaceAroundChunkSizes) {
    // open a connection
    boost::asio::ip::tcp::endpoint localhost(boost::asio::ip::address::from_string("127.0.0.1"), getServerPtr()->get_port());
    boost::asio::ip::tcp::iostream tcp_stream(localhost);

    // set expectations for received request
    std::map<std::string, std::string> expectedHeaders;
    expectedHeaders[http::types::HEADER_TRANSFER_ENCODING] = "chunked";
    getServerPtr()->setExpectations(expectedHeaders, "abcdefghijklmno");
    
    // send request to the server
    tcp_stream << "POST /resource1 HTTP/1.1" << http::types::STRING_CRLF;
    tcp_stream << http::types::HEADER_TRANSFER_ENCODING << ": chunked" << http::types::STRING_CRLF << http::types::STRING_CRLF;

    // write some chunks with chunk sizes with leading and/or trailing tabs and spaces
    tcp_stream <<      " 2"       << http::types::STRING_CRLF << "ab" << http::types::STRING_CRLF;
    tcp_stream <<       "2\t \t " << http::types::STRING_CRLF << "cd" << http::types::STRING_CRLF;
    tcp_stream <<     "  2  "     << http::types::STRING_CRLF << "ef" << http::types::STRING_CRLF;
    tcp_stream << "\t \t 2\t\t"   << http::types::STRING_CRLF << "gh" << http::types::STRING_CRLF;

    // write chunks with extra CRLF before chunk size
    // (extra CRLF after chunk size not allowed, since it would be ambiguous)
    tcp_stream << http::types::STRING_CRLF << "2"   << http::types::STRING_CRLF << "ij" << http::types::STRING_CRLF;
    tcp_stream << http::types::STRING_CRLF << " 5 " << http::types::STRING_CRLF << "klmno" << http::types::STRING_CRLF;

    // write final chunk size
    tcp_stream << "0" << http::types::STRING_CRLF;
    tcp_stream << http::types::STRING_CRLF;
    tcp_stream.flush();

    // receive goodbye from the server
    std::string str;
    std::getline(tcp_stream, str);
    BOOST_CHECK(str == "Goodbye!");
    tcp_stream.close();
}

BOOST_AUTO_TEST_CASE(checkReceivedRequestUsingRequestObject) {
    // open a connection
    pion::tcp::connection tcp_conn(get_io_service());
    boost::system::error_code error_code;
    error_code = tcp_conn.connect(boost::asio::ip::address::from_string("127.0.0.1"), getServerPtr()->get_port());
    BOOST_REQUIRE(!error_code);

    std::map<std::string, std::string> expectedHeaders;
    expectedHeaders[http::types::HEADER_CONTENT_LENGTH] = "4";
    expectedHeaders[http::types::HEADER_TRANSFER_ENCODING] = ""; // i.e. check that there is no transfer encoding header
    expectedHeaders["foo"] = "bar";
    getServerPtr()->setExpectations(expectedHeaders, "wxyz");
    
    // send request to the server
    http::request http_request;
    http_request.add_header("foo", "bar");
    http_request.set_content_length(4);
    http_request.create_content_buffer();
    memcpy(http_request.get_content(), "wxyz", 4);
    http_request.send(tcp_conn, error_code);
    BOOST_REQUIRE(!error_code);

    // receive the response from the server
    tcp_conn.read_some(error_code);
    BOOST_CHECK(!error_code);
    BOOST_CHECK(strncmp(tcp_conn.get_read_buffer().data(), "Goodbye!", strlen("Goodbye!")) == 0);
}

bool queryKeyXHasValueY(http::request& http_request) {
    return http_request.get_query("x") == "y";
}

BOOST_AUTO_TEST_CASE(checkQueryOfReceivedRequestParsed) {
    // open a connection
    boost::asio::ip::tcp::endpoint localhost(boost::asio::ip::address::from_string("127.0.0.1"), getServerPtr()->get_port());
    boost::asio::ip::tcp::iostream tcp_stream(localhost);

    // set expectations for received request
    std::map<std::string, std::string> empty_map;
    getServerPtr()->setExpectations(empty_map, "", queryKeyXHasValueY);
    
    // send request to the server
    tcp_stream << "GET /resource1?x=y HTTP/1.1" << http::types::STRING_CRLF << http::types::STRING_CRLF;
    tcp_stream.flush();

    // receive goodbye from the server
    std::string str;
    std::getline(tcp_stream, str);
    BOOST_CHECK(str == "Goodbye!");
    tcp_stream.close();
}

BOOST_AUTO_TEST_CASE(checkUrlEncodedQueryInPostContentParsed) {
    // open a connection
    boost::asio::ip::tcp::endpoint localhost(boost::asio::ip::address::from_string("127.0.0.1"), getServerPtr()->get_port());
    boost::asio::ip::tcp::iostream tcp_stream(localhost);

    // set expectations for received request
    std::map<std::string, std::string> expectedHeaders;
    expectedHeaders[http::types::HEADER_CONTENT_LENGTH] = "3";
    getServerPtr()->setExpectations(expectedHeaders, "x=y", queryKeyXHasValueY);
    
    // send request to the server
    tcp_stream << "POST /resource1 HTTP/1.1" << http::types::STRING_CRLF
               << http::types::HEADER_CONTENT_LENGTH << ": 3" << http::types::STRING_CRLF
               << http::types::HEADER_CONTENT_TYPE << ": " << http::types::CONTENT_TYPE_URLENCODED << "; charset=ECMA-cyrillic"
               << http::types::STRING_CRLF << http::types::STRING_CRLF
               << "x=y";
    tcp_stream.flush();

    // receive goodbye from the server
    std::string str;
    std::getline(tcp_stream, str);
    BOOST_CHECK(str == "Goodbye!");
    tcp_stream.close();
}
/*
Charset parsing removed due to performance concerns, but might be restored later.

bool charsetIsEcmaCyrillic(http::request& http_request) {
    return http_request.getCharset() == "ECMA-cyrillic";
}

BOOST_AUTO_TEST_CASE(checkCharsetOfReceivedRequest) {
    // open a connection
    boost::asio::ip::tcp::endpoint localhost(boost::asio::ip::address::from_string("127.0.0.1"), getServerPtr()->get_port());
    boost::asio::ip::tcp::iostream tcp_stream(localhost);

    // set expectations for received request
    std::map<std::string, std::string> expectedHeaders;
    expectedHeaders[http::types::HEADER_CONTENT_LENGTH] = "3";
    getServerPtr()->setExpectations(expectedHeaders, "x=y", charsetIsEcmaCyrillic);
    
    // send request to the server
    tcp_stream << "POST /resource1 HTTP/1.1" << http::types::STRING_CRLF
               << http::types::HEADER_CONTENT_LENGTH << ": 3" << http::types::STRING_CRLF
               << http::types::HEADER_CONTENT_TYPE << ": " << http::types::CONTENT_TYPE_URLENCODED << "; charset=ECMA-cyrillic"
               << http::types::STRING_CRLF << http::types::STRING_CRLF
               << "x=y";
    tcp_stream.flush();

    // receive goodbye from the server
    std::string str;
    std::getline(tcp_stream, str);
    BOOST_CHECK(str == "Goodbye!");
    tcp_stream.close();
}
*/

BOOST_AUTO_TEST_SUITE_END()