udp_socket_unittest.cc 12.4 KB
Newer Older
1
// Copyright (C) 2011, 2015  Internet Systems Consortium, Inc. ("ISC")
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

/// \brief Test of UDPSocket
///
17
/// Tests the functionality of a UDPSocket by working through an open-send-
18
19
20
/// receive-close sequence and checking that the asynchronous notifications
/// work.

21
22
#include <config.h>

23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <string>

#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>

#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <vector>

#include <gtest/gtest.h>

#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>

40
41
#include <util/buffer.h>
#include <util/io_utilities.h>
42

43
44
45
46
47
48
49
#include <asio.hpp>

#include <asiolink/io_service.h>
#include <asiolink/udp_endpoint.h>
#include <asiolink/udp_socket.h>

using namespace asio;
50
using namespace isc::util;
51
using namespace isc::asiolink;
52
53
54
55
using namespace std;

namespace {

56
const char SERVER_ADDRESS[] = "127.0.0.1";
57
58
const unsigned short SERVER_PORT = 5301;

59
// TODO: Shouldn't we send something that is real message?
60
61
62
63
64
65
66
67
68
const char OUTBOUND_DATA[] = "Data sent from client to server";
const char INBOUND_DATA[] = "Returned data from server to client";
}

///
/// An instance of this object is passed to the asynchronous I/O functions
/// and the operator() method is called when when an asynchronous I/O
/// completes.  The arguments to the completion callback are stored for later
/// retrieval.
Stephen Morris's avatar
Stephen Morris committed
69
class UDPCallback {
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
public:

    struct PrivateData {
        PrivateData() :
            error_code_(), length_(0), called_(false), name_("")
        {}

        asio::error_code    error_code_;    ///< Completion error code
        size_t              length_;        ///< Number of bytes transferred
        bool                called_;        ///< Set true when callback called
        std::string         name_;          ///< Which of the objects this is
    };

    /// \brief Constructor
    ///
    /// Constructs the object.  It also creates the data member pointed to by
    /// a shared pointer.  When used as a callback object, this is copied as it
    /// is passed into the asynchronous function.  This means that there are two
    /// objects and inspecting the one we passed in does not tell us anything.
    ///
    /// Therefore we use a boost::shared_ptr.  When the object is copied, the
    /// shared pointer is copied, which leaves both objects pointing to the same
    /// data.
    ///
    /// \param which Which of the two callback objects this is
    UDPCallback(std::string which) : ptr_(new PrivateData())
    {
        setName(which);
    }

    /// \brief Destructor
    ///
    /// No code needed, destroying the shared pointer destroys the private data.
    virtual ~UDPCallback()
    {}

    /// \brief Callback Function
    ///
    /// Called when an asynchronous I/O completes, this stores the
    /// completion error code and the number of bytes transferred.
    ///
    /// \param ec I/O completion error code passed to callback function.
    /// \param length Number of bytes transferred
    virtual void operator()(asio::error_code ec, size_t length = 0) {
        ptr_->error_code_ = ec;
        setLength(length);
        setCalled(true);
    }

    /// \brief Get I/O completion error code
    int getCode() {
        return (ptr_->error_code_.value());
    }

    /// \brief Set I/O completion code
    ///
    /// \param code New value of completion code
    void setCode(int code) {
        ptr_->error_code_ = asio::error_code(code, asio::error_code().category());
    }

    /// \brief Get number of bytes transferred in I/O
132
    size_t getLength() const {
133
134
135
136
137
138
139
140
141
142
143
        return (ptr_->length_);
    }

    /// \brief Set number of bytes transferred in I/O
    ///
    /// \param length New value of length parameter
    void setLength(size_t length) {
        ptr_->length_ = length;
    }

    /// \brief Get flag to say when callback was called
144
    bool getCalled() const {
145
146
147
148
149
150
151
152
153
154
155
        return (ptr_->called_);
    }

    /// \brief Set flag to say when callback was called
    ///
    /// \param called New value of called parameter
    void setCalled(bool called) {
        ptr_->called_ = called;
    }

    /// \brief Return instance of callback name
156
    std::string getName() const {
157
158
159
160
161
162
163
164
165
166
167
168
169
170
        return (ptr_->name_);
    }

    /// \brief Set callback name
    ///
    /// \param name New value of the callback name
    void setName(const std::string& name) {
        ptr_->name_ = name;
    }

private:
    boost::shared_ptr<PrivateData>  ptr_;   ///< Pointer to private data
};

171
172
173
// Receive complete method should return true regardless of what is in the first
// two bytes of a buffer.

174
TEST(UDPSocket, processReceivedData) {
175
176
177
    IOService               service;        // Used to instantiate socket
    UDPSocket<UDPCallback>  test(service);  // Socket under test
    uint8_t                 inbuff[32];     // Buffer to check
178
179
    OutputBufferPtr         outbuff(new OutputBuffer(16));
                                            // Where data is put
180
                                            // cppcheck-suppress variableScope
181
    size_t                  expected;       // Expected amount of data
182
                                            // cppcheck-suppress variableScope    
183
    size_t                  offset;         // Where to put next data
184
                                            // cppcheck-suppress variableScope
185
    size_t                  cumulative;     // Cumulative data received
186

187
    // Set some dummy values in the buffer to check
188
    for (uint8_t i = 0; i < sizeof(inbuff); ++i) {
189
        inbuff[i] = i;
190
191
    }

192
193
    // Expect that the value is true whatever number is written in the first
    // two bytes of the buffer.
194
195
    uint16_t count = 0;
    for (uint32_t i = 0; i < (2 << 16); ++i, ++count) {
196
        writeUint16(count, inbuff, sizeof(inbuff));
197
198
199
200
201

        // Set some random values
        cumulative = 5;
        offset = 10;
        expected = 15;
202
203
        outbuff->clear();

204
205
206
207
208
209
210
        bool completed = test.processReceivedData(inbuff, sizeof(inbuff),
                                                  cumulative, offset, expected,
                                                  outbuff);
        EXPECT_TRUE(completed);
        EXPECT_EQ(sizeof(inbuff), cumulative);
        EXPECT_EQ(0, offset);
        EXPECT_EQ(sizeof(inbuff), expected);
211

212
213
        const uint8_t* dataptr = static_cast<const uint8_t*>(outbuff->getData());
        EXPECT_TRUE(equal(inbuff, inbuff + sizeof(inbuff) - 1, dataptr));
214
215
216
    }
}

217
// TODO: Need to add a test to check the cancel() method
218
219
220
221
222
223
224
225
226

// Tests the operation of a UDPSocket by opening it, sending an asynchronous
// message to a server, receiving an asynchronous message from the server and
// closing.
TEST(UDPSocket, SequenceTest) {

    // Common objects.
    IOService   service;                    // Service object for async control

Stephen Morris's avatar
Stephen Morris committed
227
228
229
230
231
232
233
    // Server
    IOAddress   server_address(SERVER_ADDRESS); // Address of target server
    UDPCallback server_cb("Server");        // Server callback
    UDPEndpoint server_endpoint(            // Endpoint describing server
        server_address, SERVER_PORT);
    UDPEndpoint server_remote_endpoint;     // Address where server received message from

234
    // The client - the UDPSocket being tested
Stephen Morris's avatar
Stephen Morris committed
235
    UDPSocket<UDPCallback>  client(service);// Socket under test
236
    UDPCallback client_cb("Client");        // Async I/O callback function
Stephen Morris's avatar
Stephen Morris committed
237
    UDPEndpoint client_remote_endpoint;     // Where client receives message from
238
    size_t      client_cumulative = 0;      // Cumulative data received
239
240
241
242
    size_t      client_offset = 0;          // Offset into buffer where data is put
    size_t      client_expected = 0;        // Expected amount of data
    OutputBufferPtr client_buffer(new OutputBuffer(16));
                                            // Where data is put
243
244
245
246

    // The server - with which the client communicates.  For convenience, we
    // use the same io_service, and use the endpoint object created for
    // the client to send to as the endpoint object in the constructor.
Stephen Morris's avatar
Stephen Morris committed
247
248
    asio::ip::udp::socket server(service.get_io_service(),
        server_endpoint.getASIOEndpoint());
249
250
251
    server.set_option(socket_base::reuse_address(true));

    // Assertion to ensure that the server buffer is large enough
252
    char data[UDPSocket<UDPCallback>::MIN_SIZE];
253
254
255
    ASSERT_GT(sizeof(data), sizeof(OUTBOUND_DATA));

    // Open the client socket - the operation should be synchronous
256
257
    EXPECT_TRUE(client.isOpenSynchronous());
    client.open(&server_endpoint, client_cb);
258
259
260
261
262
263
264
265
266
267
268
269

    // Issue read on the server.  Completion callback should not have run.
    server_cb.setCalled(false);
    server_cb.setCode(42); // Answer to Life, the Universe and Everything!
    server.async_receive_from(buffer(data, sizeof(data)),
        server_remote_endpoint.getASIOEndpoint(), server_cb);
    EXPECT_FALSE(server_cb.getCalled());

    // Write something to the server using the client - the callback should not
    // be called until we call the io_service.run() method.
    client_cb.setCalled(false);
    client_cb.setCode(7);  // Arbitrary number
Stephen Morris's avatar
Stephen Morris committed
270
    client.asyncSend(OUTBOUND_DATA, sizeof(OUTBOUND_DATA), &server_endpoint, client_cb);
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
    EXPECT_FALSE(client_cb.getCalled());

    // Execute the two callbacks.
    service.run_one();
    service.run_one();

    EXPECT_TRUE(client_cb.getCalled());
    EXPECT_EQ(0, client_cb.getCode());
    EXPECT_EQ(sizeof(OUTBOUND_DATA), client_cb.getLength());

    EXPECT_TRUE(server_cb.getCalled());
    EXPECT_EQ(0, server_cb.getCode());
    EXPECT_EQ(sizeof(OUTBOUND_DATA), server_cb.getLength());

    EXPECT_TRUE(equal(&data[0], &data[server_cb.getLength() - 1], OUTBOUND_DATA));

    // Now return data from the server to the client.  Issue the read on the
    // client.
    client_cb.setLength(12345);             // Arbitrary number
    client_cb.setCalled(false);
    client_cb.setCode(32);                  // Arbitrary number
292
293
    client.asyncReceive(data, sizeof(data), client_cumulative,
        &client_remote_endpoint, client_cb);
294
295
296
297
298
299
300
301

    // Issue the write on the server side to the source of the data it received.
    server_cb.setLength(22345);             // Arbitrary number
    server_cb.setCalled(false);
    server_cb.setCode(232);                 // Arbitrary number
    server.async_send_to(buffer(INBOUND_DATA, sizeof(INBOUND_DATA)),
        server_remote_endpoint.getASIOEndpoint(), server_cb);

302
303
304
    // Expect two callbacks to run.
    service.run_one();
    service.run_one();
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320

    EXPECT_TRUE(client_cb.getCalled());
    EXPECT_EQ(0, client_cb.getCode());
    EXPECT_EQ(sizeof(INBOUND_DATA), client_cb.getLength());

    EXPECT_TRUE(server_cb.getCalled());
    EXPECT_EQ(0, server_cb.getCode());
    EXPECT_EQ(sizeof(INBOUND_DATA), server_cb.getLength());

    EXPECT_TRUE(equal(&data[0], &data[server_cb.getLength() - 1], INBOUND_DATA));

    // Check that the address/port received by the client corresponds to the
    // address and port the server is listening on.
    EXPECT_TRUE(server_address == client_remote_endpoint.getAddress());
    EXPECT_EQ(SERVER_PORT, client_remote_endpoint.getPort());

321
322
323
324
325
    // Check that the receive received a complete buffer's worth of data.
    EXPECT_TRUE(client.processReceivedData(&data[0], client_cb.getLength(),
                                           client_cumulative, client_offset,
                                           client_expected, client_buffer));

326
    EXPECT_EQ(client_cb.getLength(), client_cumulative);
327
328
329
330
331
332
333
    EXPECT_EQ(0, client_offset);
    EXPECT_EQ(client_cb.getLength(), client_expected);
    EXPECT_EQ(client_cb.getLength(), client_buffer->getLength());

    // ...and check that the data was copied to the output client buffer.
    const char* client_char_data = static_cast<const char*>(client_buffer->getData());
    EXPECT_TRUE(equal(&data[0], &data[client_cb.getLength() - 1], client_char_data));
334

335
336
337
338
    // Close client and server.
    EXPECT_NO_THROW(client.close());
    EXPECT_NO_THROW(server.close());
}