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