1/*
2 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11// Tests for the UdpSocketWrapper interface.
12// This will test the UdpSocket implementations on various platforms.
13// Note that this test is using a real SocketManager, which starts up
14// an extra worker thread, making the testing more complex than it
15// should be.
16// This is because on Posix, the CloseBlocking function waits for the
17// ReadyForDeletion function to be called, which has to be called after
18// CloseBlocking, and thus has to be called from another thread.
19// The manager is the one actually doing the deleting.
20// This is done differently in the Winsock2 code, but that code
21// will also hang if the destructor is called directly.
22
23#include "testing/gmock/include/gmock/gmock.h"
24#include "testing/gtest/include/gtest/gtest.h"
25#include "webrtc/test/channel_transport/udp_socket_manager_wrapper.h"
26#include "webrtc/test/channel_transport/udp_socket_wrapper.h"
27
28using ::testing::_;
29using ::testing::Return;
30
31namespace webrtc {
32namespace test {
33
34class MockSocketManager : public UdpSocketManager {
35 public:
36  MockSocketManager() {}
37  // Access to protected destructor.
38  void Destroy() {
39    delete this;
40  }
41  MOCK_METHOD2(Init, bool(int32_t, uint8_t&));
42  MOCK_METHOD1(ChangeUniqueId, int32_t(const int32_t));
43  MOCK_METHOD0(Start, bool());
44  MOCK_METHOD0(Stop, bool());
45  MOCK_METHOD1(AddSocket, bool(UdpSocketWrapper*));
46  MOCK_METHOD1(RemoveSocket, bool(UdpSocketWrapper*));
47};
48
49// Creates a socket using the static constructor method and verifies that
50// it's added to the socket manager.
51TEST(UdpSocketWrapper, CreateSocket) {
52  int32_t id = 42;
53  // We can't test deletion of sockets without a socket manager.
54  uint8_t threads = 1;
55  UdpSocketManager* mgr = UdpSocketManager::Create(id, threads);
56  UdpSocketWrapper* socket =
57      UdpSocketWrapper::CreateSocket(id,
58                                     mgr,
59                                     NULL,  // CallbackObj
60                                     NULL,  // IncomingSocketCallback
61                                     false,  // ipV6Enable
62                                     false);  // disableGQOS
63  socket->CloseBlocking();
64  UdpSocketManager::Return();
65}
66
67}  // namespace test
68}  // namespace webrtc
69