OLD | NEW |
| (Empty) |
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 #include <vector> | |
12 | |
13 #include "webrtc/test/gmock.h" | |
14 #include "webrtc/test/gtest.h" | |
15 #include "webrtc/voice_engine/test/channel_transport/udp_transport.h" | |
16 | |
17 // We include the implementation header file to get at the dependency-injecting | |
18 // constructor. | |
19 #include "webrtc/voice_engine/test/channel_transport/udp_transport_impl.h" | |
20 | |
21 // We must mock the socket manager, for which we need its definition. | |
22 #include "webrtc/voice_engine/test/channel_transport/udp_socket_manager_wrapper.
h" | |
23 | |
24 using ::testing::_; | |
25 using ::testing::Return; | |
26 | |
27 namespace webrtc { | |
28 namespace test { | |
29 | |
30 class MockUdpSocketWrapper : public UdpSocketWrapper { | |
31 public: | |
32 // The following methods have to be mocked because they are pure. | |
33 MOCK_METHOD2(SetCallback, bool(CallbackObj, IncomingSocketCallback)); | |
34 MOCK_METHOD1(Bind, bool(const SocketAddress&)); | |
35 MOCK_METHOD0(ValidHandle, bool()); | |
36 MOCK_METHOD4(SetSockopt, bool(int32_t, int32_t, | |
37 const int8_t*, | |
38 int32_t)); | |
39 MOCK_METHOD1(SetTOS, int32_t(int32_t)); | |
40 MOCK_METHOD3(SendTo, int32_t(const int8_t*, size_t, const SocketAddress&)); | |
41 MOCK_METHOD8(SetQos, bool(int32_t, int32_t, | |
42 int32_t, int32_t, | |
43 int32_t, int32_t, | |
44 const SocketAddress &, | |
45 int32_t)); | |
46 }; | |
47 | |
48 class MockUdpSocketManager : public UdpSocketManager { | |
49 public: | |
50 // Access to protected destructor. | |
51 void Destroy() { | |
52 delete this; | |
53 } | |
54 MOCK_METHOD2(Init, bool(int32_t, uint8_t&)); | |
55 MOCK_METHOD0(Start, bool()); | |
56 MOCK_METHOD0(Stop, bool()); | |
57 MOCK_METHOD1(AddSocket, bool(UdpSocketWrapper*)); | |
58 MOCK_METHOD1(RemoveSocket, bool(UdpSocketWrapper*)); | |
59 }; | |
60 | |
61 class MockSocketFactory : | |
62 public UdpTransportImpl::SocketFactoryInterface { | |
63 public: | |
64 MockSocketFactory(std::vector<MockUdpSocketWrapper*>* socket_counter) | |
65 : socket_counter_(socket_counter) { | |
66 } | |
67 UdpSocketWrapper* CreateSocket(const int32_t id, | |
68 UdpSocketManager* mgr, | |
69 CallbackObj obj, | |
70 IncomingSocketCallback cb, | |
71 bool ipV6Enable, | |
72 bool disableGQOS) { | |
73 MockUdpSocketWrapper* socket = new MockUdpSocketWrapper(); | |
74 // We instrument the socket with calls that are expected, but do | |
75 // not matter for any specific test, in order to avoid warning messages. | |
76 EXPECT_CALL(*socket, ValidHandle()).WillRepeatedly(Return(true)); | |
77 EXPECT_CALL(*socket, Bind(_)).WillOnce(Return(true)); | |
78 socket_counter_->push_back(socket); | |
79 return socket; | |
80 } | |
81 std::vector<MockUdpSocketWrapper*>* socket_counter_; | |
82 }; | |
83 | |
84 class UDPTransportTest : public ::testing::Test { | |
85 public: | |
86 UDPTransportTest() | |
87 : sockets_created_(0) { | |
88 } | |
89 | |
90 ~UDPTransportTest() { | |
91 // In production, sockets register themselves at creation time with | |
92 // an UdpSocketManager, and the UdpSocketManager is responsible for | |
93 // deleting them. In this test, we just delete them after the test. | |
94 while (!sockets_created_.empty()) { | |
95 delete sockets_created_.back(); | |
96 sockets_created_.pop_back(); | |
97 } | |
98 } | |
99 | |
100 size_t NumSocketsCreated() { | |
101 return sockets_created_.size(); | |
102 } | |
103 | |
104 std::vector<MockUdpSocketWrapper*>* sockets_created() { | |
105 return &sockets_created_; | |
106 } | |
107 private: | |
108 std::vector<MockUdpSocketWrapper*> sockets_created_; | |
109 }; | |
110 | |
111 TEST_F(UDPTransportTest, CreateTransport) { | |
112 int32_t id = 0; | |
113 uint8_t threads = 1; | |
114 UdpTransport* transport = UdpTransport::Create(id, threads); | |
115 UdpTransport::Destroy(transport); | |
116 } | |
117 | |
118 // This test verifies that the mock_socket is not called from the constructor. | |
119 TEST_F(UDPTransportTest, ConstructorDoesNotCreateSocket) { | |
120 int32_t id = 0; | |
121 UdpTransportImpl::SocketFactoryInterface* null_maker = NULL; | |
122 UdpSocketManager* null_manager = NULL; | |
123 UdpTransport* transport = new UdpTransportImpl(id, | |
124 null_maker, | |
125 null_manager); | |
126 delete transport; | |
127 } | |
128 | |
129 TEST_F(UDPTransportTest, InitializeSourcePorts) { | |
130 int32_t id = 0; | |
131 UdpTransportImpl::SocketFactoryInterface* mock_maker | |
132 = new MockSocketFactory(sockets_created()); | |
133 MockUdpSocketManager* mock_manager = new MockUdpSocketManager(); | |
134 UdpTransport* transport = new UdpTransportImpl(id, | |
135 mock_maker, | |
136 mock_manager); | |
137 EXPECT_EQ(0, transport->InitializeSourcePorts(4711, 4712)); | |
138 EXPECT_EQ(2u, NumSocketsCreated()); | |
139 | |
140 delete transport; | |
141 mock_manager->Destroy(); | |
142 } | |
143 | |
144 } // namespace test | |
145 } // namespace webrtc | |
OLD | NEW |