1 /*
2  *  Copyright (c) 2015 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 "webrtc/voice_engine/include/voe_network.h"
12 
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "webrtc/voice_engine/include/voe_errors.h"
15 #include "webrtc/voice_engine/voice_engine_fixture.h"
16 
17 namespace webrtc {
18 
19 enum {
20   kMinValidSizeOfRtcpPacketInBytes = 4,
21   kMinValidSizeOfRtpPacketInBytes = 12,
22   kMaxValidSizeOfRtpPacketInBytes = 1292
23 };
24 
25 // A packet with a valid header for both RTP and RTCP.
26 // Methods that are tested in this file are checking only packet header.
27 static const uint8_t kPacket[kMinValidSizeOfRtpPacketInBytes] = {0x80};
28 static const uint8_t kPacketJunk[kMinValidSizeOfRtpPacketInBytes] = {};
29 
30 static const int kNonExistingChannel = 1234;
31 
32 class VoENetworkTest : public VoiceEngineFixture {
33  protected:
CreateChannelAndRegisterExternalTransport()34   int CreateChannelAndRegisterExternalTransport() {
35     EXPECT_EQ(0, base_->Init(&adm_, nullptr));
36     int channelID = base_->CreateChannel();
37     EXPECT_NE(channelID, -1);
38     EXPECT_EQ(0, network_->RegisterExternalTransport(channelID, transport_));
39     return channelID;
40   }
41 };
42 
TEST_F(VoENetworkTest,RegisterAndDeRegisterExternalTransport)43 TEST_F(VoENetworkTest, RegisterAndDeRegisterExternalTransport) {
44   int channelID = CreateChannelAndRegisterExternalTransport();
45   EXPECT_EQ(0, network_->DeRegisterExternalTransport(channelID));
46 }
47 
TEST_F(VoENetworkTest,RegisterExternalTransportOnNonExistingChannelShouldFail)48 TEST_F(VoENetworkTest,
49        RegisterExternalTransportOnNonExistingChannelShouldFail) {
50   EXPECT_EQ(0, base_->Init(&adm_, nullptr));
51   EXPECT_NE(
52       0, network_->RegisterExternalTransport(kNonExistingChannel, transport_));
53 }
54 
TEST_F(VoENetworkTest,DeRegisterExternalTransportOnNonExistingChannelShouldFail)55 TEST_F(VoENetworkTest,
56        DeRegisterExternalTransportOnNonExistingChannelShouldFail) {
57   EXPECT_EQ(0, base_->Init(&adm_, nullptr));
58   EXPECT_NE(0, network_->DeRegisterExternalTransport(kNonExistingChannel));
59 }
60 
TEST_F(VoENetworkTest,DeRegisterExternalTransportBeforeRegister)61 TEST_F(VoENetworkTest, DeRegisterExternalTransportBeforeRegister) {
62   EXPECT_EQ(0, base_->Init(&adm_, nullptr));
63   int channelID = base_->CreateChannel();
64   EXPECT_NE(channelID, -1);
65   EXPECT_EQ(0, network_->DeRegisterExternalTransport(channelID));
66 }
67 
TEST_F(VoENetworkTest,ReceivedRTPPacketWithJunkDataShouldFail)68 TEST_F(VoENetworkTest, ReceivedRTPPacketWithJunkDataShouldFail) {
69   int channelID = CreateChannelAndRegisterExternalTransport();
70   EXPECT_EQ(-1, network_->ReceivedRTPPacket(channelID, kPacketJunk,
71                                             sizeof(kPacketJunk)));
72 }
73 
TEST_F(VoENetworkTest,ReceivedRTPPacketOnNonExistingChannelShouldFail)74 TEST_F(VoENetworkTest, ReceivedRTPPacketOnNonExistingChannelShouldFail) {
75   EXPECT_EQ(0, base_->Init(&adm_, nullptr));
76   EXPECT_EQ(-1, network_->ReceivedRTPPacket(kNonExistingChannel, kPacket,
77                                             sizeof(kPacket)));
78 }
79 
TEST_F(VoENetworkTest,ReceivedRTPPacketOnChannelWithoutTransportShouldFail)80 TEST_F(VoENetworkTest, ReceivedRTPPacketOnChannelWithoutTransportShouldFail) {
81   EXPECT_EQ(0, base_->Init(&adm_, nullptr));
82   int channelID = base_->CreateChannel();
83   EXPECT_NE(channelID, -1);
84   EXPECT_EQ(-1,
85             network_->ReceivedRTPPacket(channelID, kPacket, sizeof(kPacket)));
86 }
87 
TEST_F(VoENetworkTest,ReceivedTooSmallRTPPacketShouldFail)88 TEST_F(VoENetworkTest, ReceivedTooSmallRTPPacketShouldFail) {
89   int channelID = CreateChannelAndRegisterExternalTransport();
90   EXPECT_EQ(-1, network_->ReceivedRTPPacket(
91                     channelID, kPacket, kMinValidSizeOfRtpPacketInBytes - 1));
92 }
93 
TEST_F(VoENetworkTest,ReceivedTooLargeRTPPacketShouldFail)94 TEST_F(VoENetworkTest, ReceivedTooLargeRTPPacketShouldFail) {
95   int channelID = CreateChannelAndRegisterExternalTransport();
96   EXPECT_EQ(-1, network_->ReceivedRTPPacket(
97                     channelID, kPacket, kMaxValidSizeOfRtpPacketInBytes + 1));
98 }
99 
TEST_F(VoENetworkTest,ReceivedRTCPPacketWithJunkDataShouldFail)100 TEST_F(VoENetworkTest, ReceivedRTCPPacketWithJunkDataShouldFail) {
101   int channelID = CreateChannelAndRegisterExternalTransport();
102   EXPECT_EQ(0, network_->ReceivedRTCPPacket(channelID, kPacketJunk,
103                                             sizeof(kPacketJunk)));
104   EXPECT_EQ(VE_SOCKET_TRANSPORT_MODULE_ERROR, base_->LastError());
105 }
106 
TEST_F(VoENetworkTest,ReceivedRTCPPacketOnNonExistingChannelShouldFail)107 TEST_F(VoENetworkTest, ReceivedRTCPPacketOnNonExistingChannelShouldFail) {
108   EXPECT_EQ(0, base_->Init(&adm_, nullptr));
109   EXPECT_EQ(-1, network_->ReceivedRTCPPacket(kNonExistingChannel, kPacket,
110                                              sizeof(kPacket)));
111 }
112 
TEST_F(VoENetworkTest,ReceivedRTCPPacketOnChannelWithoutTransportShouldFail)113 TEST_F(VoENetworkTest, ReceivedRTCPPacketOnChannelWithoutTransportShouldFail) {
114   EXPECT_EQ(0, base_->Init(&adm_, nullptr));
115   int channelID = base_->CreateChannel();
116   EXPECT_NE(channelID, -1);
117   EXPECT_EQ(-1,
118             network_->ReceivedRTCPPacket(channelID, kPacket, sizeof(kPacket)));
119 }
120 
TEST_F(VoENetworkTest,ReceivedTooSmallRTCPPacket4ShouldFail)121 TEST_F(VoENetworkTest, ReceivedTooSmallRTCPPacket4ShouldFail) {
122   int channelID = CreateChannelAndRegisterExternalTransport();
123   EXPECT_EQ(-1, network_->ReceivedRTCPPacket(
124                     channelID, kPacket, kMinValidSizeOfRtcpPacketInBytes - 1));
125 }
126 
127 }  // namespace webrtc
128