File: send_msg_tests.cpp

package info (click to toggle)
cpprest 2.10.10-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 6,812 kB
  • sloc: cpp: 69,785; sh: 254; makefile: 167
file content (522 lines) | stat: -rw-r--r-- 19,822 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
/***
 * Copyright (C) Microsoft. All rights reserved.
 * Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
 *
 * =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
 *
 * send_msg_tests.cpp
 *
 * Tests cases for covering sending messages from websocket client.
 *
 * =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 ****/

#include "stdafx.h"

#if defined(__cplusplus_winrt) || !defined(_M_ARM)

using namespace concurrency;
using namespace concurrency::streams;

using namespace web::websockets;
using namespace web::websockets::client;

using namespace tests::functional::websocket::utilities;

#if defined(__cplusplus_winrt)
using namespace Windows::Storage;
#endif

namespace tests
{
namespace functional
{
namespace websocket
{
namespace client
{
SUITE(send_msg_tests)
{
    utility::string_t get_full_name(const utility::string_t& name)
    {
#if defined(__cplusplus_winrt)
        // On WinRT, we must compensate for the fact that we will be accessing files in the
        // Documents folder
        auto file =
            pplx::create_task(KnownFolders::DocumentsLibrary->CreateFileAsync(ref new Platform::String(name.c_str()),
                                                                              CreationCollisionOption::ReplaceExisting))
                .get();
        return file->Path->Data();
#else
        return name;
#endif
    }

    template<typename _CharType>
    pplx::task<streams::streambuf<_CharType>> OPEN_R(const utility::string_t& name)
    {
#if !defined(__cplusplus_winrt)
        return streams::file_buffer<_CharType>::open(name, std::ios_base::in);
#else
        auto file =
            pplx::create_task(KnownFolders::DocumentsLibrary->GetFileAsync(ref new Platform::String(name.c_str())))
                .get();

        return streams::file_buffer<_CharType>::open(file, std::ios_base::in);
#endif
    }

    // Used to prepare data for stream tests
    void fill_file(const utility::string_t& name, const std::vector<uint8_t>& body, size_t repetitions = 1)
    {
        std::fstream stream(get_full_name(name), std::ios_base::out | std::ios_base::trunc);

        for (size_t i = 0; i < repetitions; i++)
            stream.write((char*)&body[0], body.size());
        stream.close();
    }

    void fill_buffer(streams::streambuf<uint8_t> rbuf, const std::vector<uint8_t>& body, size_t repetitions = 1)
    {
        size_t len = body.size();
        for (size_t i = 0; i < repetitions; i++)
            rbuf.putn_nocopy((const uint8_t*)&body[0], len).wait();
    }

    template<class SocketClientClass>
    pplx::task<void> send_text_msg_helper(SocketClientClass & client,
                                          web::uri uri,
                                          test_websocket_server & server,
                                          const std::string& body,
                                          bool connect_client = true)
    {
        server.next_message([body](test_websocket_msg msg) // Handler to verify the message sent by the client.
                            {
                                websocket_asserts::assert_message_equals(
                                    msg, body, test_websocket_message_type::WEB_SOCKET_UTF8_MESSAGE_TYPE);
                            });

        if (connect_client) client.connect(uri).wait();

        websocket_outgoing_message msg;
        msg.set_utf8_message(body);
        return client.send(msg);
    }

    template<class SocketClientClass>
    pplx::task<void> send_pong_msg_helper(SocketClientClass & client, web::uri uri, test_websocket_server & server)
    {
        server.next_message(
            [](test_websocket_msg msg) // Handler to verify the message sent by the client.
            { websocket_asserts::assert_message_equals(msg, "", test_websocket_message_type::WEB_SOCKET_PONG_TYPE); });

        client.connect(uri).wait();

        websocket_outgoing_message msg;
        msg.set_pong_message();
        return client.send(msg);
    }

    pplx::task<void> send_msg_from_stream(websocket_client & client,
                                          test_websocket_server & server,
                                          web::uri uri,
                                          const std::vector<uint8_t>& body,
                                          streams::streambuf<uint8_t> buf,
                                          test_websocket_message_type type,
                                          bool fill_data,
                                          bool connect_client = true)
    {
        server.next_message(
            [body, type](test_websocket_msg msg) { websocket_asserts::assert_message_equals(msg, body, type); });

        if (connect_client) client.connect(uri).wait();
        if (fill_data) fill_buffer(buf, body);

        websocket_outgoing_message msg;
        if (type == test_websocket_message_type::WEB_SOCKET_UTF8_MESSAGE_TYPE)
            msg.set_utf8_message(streams::istream(buf), body.size());
        else if (type == test_websocket_message_type::WEB_SOCKET_BINARY_MESSAGE_TYPE)
            msg.set_binary_message(streams::istream(buf), body.size());

        return client.send(msg);
    }

    // Send message from input stream -> data is already populated in the stream buffer
    pplx::task<void> send_msg_from_istream_helper(websocket_client & client,
                                                  test_websocket_server & server,
                                                  web::uri uri,
                                                  const std::vector<uint8_t>& body,
                                                  streams::streambuf<uint8_t> rbuf,
                                                  test_websocket_message_type type,
                                                  bool connect_client = true)
    {
        return send_msg_from_stream(client, server, uri, body, rbuf, type, false, connect_client);
    }

    pplx::task<void> send_msg_from_stream_helper(websocket_client & client,
                                                 test_websocket_server & server,
                                                 web::uri uri,
                                                 const std::vector<uint8_t>& body,
                                                 streams::streambuf<uint8_t> rbuf,
                                                 test_websocket_message_type type,
                                                 bool connect_client = true)
    {
        return send_msg_from_stream(client, server, uri, body, rbuf, type, true, connect_client);
    }

    // Send text message (no fragmentation)
    TEST_FIXTURE(uri_address, send_text_msg)
    {
        test_websocket_server server;
        websocket_client client;
        send_text_msg_helper(client, m_uri, server, "hello").wait();
        client.close().wait();
    }

    // Send text message with websocket_callback_client
    TEST_FIXTURE(uri_address, send_text_msg_callback_client)
    {
        test_websocket_server server;
        websocket_callback_client client;
        send_text_msg_helper(client, m_uri, server, "hello").wait();
        client.close().wait();
    }

    // Send text message (no fragmentation)
    // Test the stream interface to send data
    TEST_FIXTURE(uri_address, send_text_msg_stream)
    {
        test_websocket_server server;
        streams::producer_consumer_buffer<uint8_t> rbuf;
        std::vector<uint8_t> body(26);
        memcpy(&body[0], "abcdefghijklmnopqrstuvwxyz", 26);

        websocket_client client;
        send_msg_from_stream_helper(
            client, server, m_uri, body, rbuf, test_websocket_message_type::WEB_SOCKET_UTF8_MESSAGE_TYPE)
            .wait();

        rbuf.close(std::ios::out).wait();
        client.close().wait();
    }

    // Send Binary message (no fragmentation)
    TEST_FIXTURE(uri_address, send_binary_msg)
    {
        test_websocket_server server;
        streams::producer_consumer_buffer<uint8_t> rbuf;
        std::vector<uint8_t> body(6);
        memcpy(&body[0], "a\0b\0c\0", 6);

        websocket_client client;

        send_msg_from_stream_helper(
            client, server, m_uri, body, rbuf, test_websocket_message_type::WEB_SOCKET_BINARY_MESSAGE_TYPE)
            .wait();
        rbuf.close(std::ios::out);
        client.close().wait();
    }

    // Send empty text message
    // WinRT client does not handle empty messages. Verify websocket_exception is thrown.
    TEST_FIXTURE(uri_address, send_empty_text_msg)
    {
        test_websocket_server server;
        websocket_client client;

        client.connect(m_uri).wait();

        websocket_outgoing_message msg;
        msg.set_utf8_message("");
        VERIFY_THROWS(client.send(msg).wait(), websocket_exception);

        client.close().wait();
    }

    // Send multiple text messages
    TEST_FIXTURE(uri_address, send_multiple_text_msges)
    {
        test_websocket_server server;
        websocket_client client;

        send_text_msg_helper(client, m_uri, server, "hello1").wait();
        send_text_msg_helper(client, m_uri, server, "hello2", false).wait();

        client.close().wait();
    }

    // Send multiple text messages
    TEST_FIXTURE(uri_address, send_multiple_text_msges_async)
    {
        test_websocket_server server;
        websocket_client client;

        auto t1 = send_text_msg_helper(client, m_uri, server, "hello1");
        auto t2 = send_text_msg_helper(client, m_uri, server, "hello2", false);

        t2.wait();
        t1.wait();
        client.close().wait();
    }

    // Send multiple text messages from a stream
    TEST_FIXTURE(uri_address, send_multiple_text_msges_stream)
    {
        test_websocket_server server;
        streams::producer_consumer_buffer<uint8_t> rbuf;
        std::vector<uint8_t> body1(26);
        memcpy(&body1[0], "abcdefghijklmnopqrstuvwxyz", 26);
        std::vector<uint8_t> body2(26);
        memcpy(&body2[0], "zyxwvutsrqponmlkjihgfedcba", 26);

        websocket_client client;

        auto t1 = send_msg_from_stream_helper(
            client, server, m_uri, body1, rbuf, test_websocket_message_type::WEB_SOCKET_UTF8_MESSAGE_TYPE);
        auto t2 = send_msg_from_stream_helper(
            client, server, m_uri, body2, rbuf, test_websocket_message_type::WEB_SOCKET_UTF8_MESSAGE_TYPE, false);

        t1.wait();
        t2.wait();
        client.close().wait();
    }

    // Send multiple text messages from a file stream
    // send uses stream::acquire API, acquire will fail for file streams.
    TEST_FIXTURE(uri_address, send_text_msges_fstream)
    {
        test_websocket_server server;
        utility::string_t fname = U("send_multiple_text_msges_fstream.txt");
        std::vector<uint8_t> body1(26);
        memcpy(&body1[0], "abcdefghijklmnopqrstuvwxyz", 26);
        fill_file(fname, body1, 2);
        auto file_buf = OPEN_R<uint8_t>(fname).get();
        websocket_client client;

        auto t1 = send_msg_from_istream_helper(
            client, server, m_uri, body1, file_buf, test_websocket_message_type::WEB_SOCKET_UTF8_MESSAGE_TYPE);
        auto t2 = send_msg_from_istream_helper(
            client, server, m_uri, body1, file_buf, test_websocket_message_type::WEB_SOCKET_UTF8_MESSAGE_TYPE, false);

        t1.wait();
        t2.wait();
        client.close().wait();
    }

    // Send multiple text messages from a container stream, where container stream has more data than what we want to
    // send in a single message
    TEST_FIXTURE(uri_address, send_text_msges_cstream)
    {
        test_websocket_server server;
        std::vector<uint8_t> body(26);
        memcpy(&body[0], "abcdefghijklmnopqrstuvwxyz", 26);
        auto cbuf = streams::container_stream<std::vector<uint8_t>>::open_istream(body).streambuf();

        websocket_client client;

        auto t1 = send_msg_from_istream_helper(client,
                                               server,
                                               m_uri,
                                               std::vector<uint8_t>(body.begin(), body.begin() + body.size() / 2),
                                               cbuf,
                                               test_websocket_message_type::WEB_SOCKET_UTF8_MESSAGE_TYPE);
        auto t2 = send_msg_from_istream_helper(client,
                                               server,
                                               m_uri,
                                               std::vector<uint8_t>(body.begin() + body.size() / 2, body.end()),
                                               cbuf,
                                               test_websocket_message_type::WEB_SOCKET_UTF8_MESSAGE_TYPE,
                                               false);

        t1.wait();
        t2.wait();
        client.close().wait();
    }

    // Send multiple text messages from a producer consumer stream, where stream initially has less data than what we
    // want to send in a single message Write data to the buffer after initiating the send, send should succeed.
    TEST_FIXTURE(uri_address, send_text_msges_pcstream_lessdata)
    {
        test_websocket_server server;
        streams::producer_consumer_buffer<uint8_t> rbuf;
        std::vector<uint8_t> body(26);
        memcpy(&body[0], "abcdefghijklmnopqrstuvwxyz", 26);
        fill_buffer(rbuf, body);

        server.next_message([](test_websocket_msg msg) {
            websocket_asserts::assert_message_equals(
                msg, "abcdefghijklmnopqrstuvwxyzabcd", test_websocket_message_type::WEB_SOCKET_UTF8_MESSAGE_TYPE);
        });

        websocket_client client;
        client.connect(m_uri).wait();
        websocket_outgoing_message msg;
        msg.set_utf8_message(rbuf.create_istream(), 30);
        auto t1 = client.send(msg);

        fill_buffer(rbuf, body);
        t1.wait();
        client.close().wait();
    }

    // Send multiple text messages from a container stream, where stream has less data than what we want to send in a
    // single message Since container stream does not support in | out simultaneously, websocket send_msg will fail to
    // read the required number of bytes and throws an exception.
    TEST_FIXTURE(uri_address, send_text_msges_cstream_lessdata)
    {
        test_websocket_server server;
        std::vector<uint8_t> body(26);
        memcpy(&body[0], "abcdefghijklmnopqrstuvwxyz", 26);
        auto cbuf = streams::container_stream<std::vector<uint8_t>>::open_istream(body).streambuf();

        server.next_message([](test_websocket_msg msg) {
            websocket_asserts::assert_message_equals(
                msg, "abcdefghijklmnopqrstuvwxyzabcd", test_websocket_message_type::WEB_SOCKET_UTF8_MESSAGE_TYPE);
        });

        websocket_client client;
        client.connect(m_uri).wait();
        websocket_outgoing_message msg;
        msg.set_utf8_message(cbuf.create_istream(), 30);

        VERIFY_THROWS(client.send(msg).wait(), websocket_exception);
        client.close().wait();
    }

    // Send multiple binary messages from the same stream
    TEST_FIXTURE(uri_address, send_multiple_binary_msg_same_stream)
    {
        test_websocket_server server;
        streams::producer_consumer_buffer<uint8_t> rbuf;
        std::vector<uint8_t> body1(6);
        memcpy(&body1[0], "a\0b\0c\0", 6);
        std::vector<uint8_t> body2(6);
        memcpy(&body2[0], "a\0b\0c\0", 6);

        websocket_client client;

        auto t1 = send_msg_from_stream_helper(
            client, server, m_uri, body1, rbuf, test_websocket_message_type::WEB_SOCKET_BINARY_MESSAGE_TYPE);
        auto t2 = send_msg_from_stream_helper(
            client, server, m_uri, body2, rbuf, test_websocket_message_type::WEB_SOCKET_BINARY_MESSAGE_TYPE, false);

        t1.wait();
        t2.wait();
        rbuf.close(std::ios_base::out);
        client.close().wait();
    }

    // Send text message followed by binary message
    TEST_FIXTURE(uri_address, send_text_and_binary)
    {
        test_websocket_server server;
        streams::producer_consumer_buffer<uint8_t> rbuf;
        std::vector<uint8_t> body2(6);
        memcpy(&body2[0], "a\0b\0c\0", 6);

        websocket_client client;

        send_text_msg_helper(client, m_uri, server, "hello1").wait();
        send_msg_from_stream_helper(
            client, server, m_uri, body2, rbuf, test_websocket_message_type::WEB_SOCKET_BINARY_MESSAGE_TYPE, false)
            .wait();

        rbuf.close(std::ios::out).wait();
        client.close().wait();
    }

    // Send a multi byte UTF-8 text message
    TEST_FIXTURE(uri_address, send_multi_byte_utf8_msg)
    {
        test_websocket_server server;
        std::string body = "\xC3\xA0\xC3\xB8";
        websocket_client client;

        send_text_msg_helper(client, m_uri, server, body).wait();
        client.close().wait();
    }

    // Send a streamed text message without specifying length
    TEST_FIXTURE(uri_address, send_stream_utf8_msg_no_length)
    {
        test_websocket_server server;

        std::string body = "\xC3\xA0\xC3\xB8";
        std::vector<uint8_t> msgbuf(body.begin(), body.end());

        auto is = streams::container_stream<std::vector<uint8_t>>::open_istream(std::move(msgbuf));

        websocket_client client;
        {
            server.next_message([body](test_websocket_msg msg) // Handler to verify the message sent by the client.
                                {
                                    websocket_asserts::assert_message_equals(
                                        msg, body, test_websocket_message_type::WEB_SOCKET_UTF8_MESSAGE_TYPE);
                                });

            client.connect(m_uri).wait();

            websocket_outgoing_message msg;
            msg.set_utf8_message(is);
            client.send(msg).wait();
        }

        client.close().wait();
    }

    // Send a streamed binary message without specifying length
    TEST_FIXTURE(uri_address, send_stream_binary_msg_no_length)
    {
        test_websocket_server server;

        std::string body = "\x00\x01\x02\x00";
        std::vector<uint8_t> msgbuf(body.begin(), body.end());

        auto is = streams::container_stream<std::vector<uint8_t>>::open_istream(std::move(msgbuf));

        websocket_client client;
        {
            server.next_message([body](test_websocket_msg msg) // Handler to verify the message sent by the client.
                                {
                                    websocket_asserts::assert_message_equals(
                                        msg, body, test_websocket_message_type::WEB_SOCKET_BINARY_MESSAGE_TYPE);
                                });

            client.connect(m_uri).wait();

            websocket_outgoing_message msg;
            msg.set_binary_message(is);
            client.send(msg).wait();
        }

        client.close().wait();
    }

#if !defined(__cplusplus_winrt)
    // Send an unsolicited pong message to the server
    TEST_FIXTURE(uri_address, send_pong_msg)
    {
        test_websocket_server server;
        websocket_client client;
        send_pong_msg_helper(client, m_uri, server).wait();
        client.close().wait();
    }

    // Send an unsolicited pong message to the server with websocket_callback_client
    TEST_FIXTURE(uri_address, send_pong_msg_callback_client)
    {
        test_websocket_server server;
        websocket_callback_client client;
        send_pong_msg_helper(client, m_uri, server).wait();
        client.close().wait();
    }
#endif

} // SUITE(send_msg_tests)

} // namespace client
} // namespace websocket
} // namespace functional
} // namespace tests

#endif