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 UdpSocketManager interface.
12// Note: This tests UdpSocketManager together with UdpSocketWrapper,
13// due to the way the code is full of static-casts to the platform dependent
14// subtypes.
15// It also uses the static UdpSocketManager object.
16// The most important property of these tests is that they do not leak memory.
17
18#include "testing/gtest/include/gtest/gtest.h"
19#include "webrtc/system_wrappers/include/trace.h"
20#include "webrtc/test/channel_transport/udp_socket_manager_wrapper.h"
21#include "webrtc/test/channel_transport/udp_socket_wrapper.h"
22
23namespace webrtc {
24namespace test {
25
26TEST(UdpSocketManager, CreateCallsInitAndDoesNotLeakMemory) {
27  int32_t id = 42;
28  uint8_t threads = 1;
29  UdpSocketManager* mgr = UdpSocketManager::Create(id, threads);
30  // Create is supposed to have called init on the object.
31  EXPECT_FALSE(mgr->Init(id, threads))
32      << "Init should return false since Create is supposed to call it.";
33  UdpSocketManager::Return();
34}
35
36// Creates a socket and adds it to the socket manager, and then removes it
37// before destroying the socket manager.
38TEST(UdpSocketManager, AddAndRemoveSocketDoesNotLeakMemory) {
39  int32_t id = 42;
40  uint8_t threads = 1;
41  UdpSocketManager* mgr = UdpSocketManager::Create(id, threads);
42  UdpSocketWrapper* socket =
43      UdpSocketWrapper::CreateSocket(id,
44                                     mgr,
45                                     NULL,  // CallbackObj
46                                     NULL,  // IncomingSocketCallback
47                                     false,  // ipV6Enable
48                                     false);  // disableGQOS
49  // The constructor will do AddSocket on the manager.
50  // RemoveSocket indirectly calls Delete.
51  EXPECT_EQ(true, mgr->RemoveSocket(socket));
52  UdpSocketManager::Return();
53}
54
55// Creates a socket and add it to the socket manager, but does not remove it
56// before destroying the socket manager.
57// On Posix, this destroys the socket.
58// On Winsock2 Windows, it enters an infinite wait for all the sockets
59// to go away.
60TEST(UdpSocketManager, UnremovedSocketsGetCollectedAtManagerDeletion) {
61#if defined(_WIN32)
62  // It's hard to test an infinite wait, so we don't.
63#else
64  int32_t id = 42;
65  uint8_t threads = 1;
66  UdpSocketManager* mgr = UdpSocketManager::Create(id, threads);
67  UdpSocketWrapper* unused_socket = UdpSocketWrapper::CreateSocket(
68      id,
69      mgr,
70      NULL,  // CallbackObj
71      NULL,  // IncomingSocketCallback
72      false,  // ipV6Enable
73      false);  // disableGQOS
74  // The constructor will do AddSocket on the manager.
75  // Call a member funtion to work around "set but not used" compliation
76  // error on ChromeOS ARM.
77  unused_socket->SetEventToNull();
78  unused_socket = NULL;
79  UdpSocketManager::Return();
80#endif
81}
82
83}  // namespace test
84}  // namespace webrtc
85