• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2011 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 "testsupport/packet_reader.h"
12 
13 #include "gtest/gtest.h"
14 #include "testsupport/unittest_utils.h"
15 
16 namespace webrtc {
17 namespace test {
18 
19 class PacketReaderTest: public PacketRelatedTest {
20  protected:
PacketReaderTest()21   PacketReaderTest() {}
~PacketReaderTest()22   virtual ~PacketReaderTest() {}
SetUp()23   void SetUp() {
24     reader_ = new PacketReader();
25   }
TearDown()26   void TearDown() {
27     delete reader_;
28   }
VerifyPacketData(int expected_length,int actual_length,WebRtc_UWord8 * original_data_pointer,WebRtc_UWord8 * new_data_pointer)29   void VerifyPacketData(int expected_length,
30                         int actual_length,
31                         WebRtc_UWord8* original_data_pointer,
32                         WebRtc_UWord8* new_data_pointer) {
33     EXPECT_EQ(expected_length, actual_length);
34     EXPECT_EQ(*original_data_pointer, *new_data_pointer);
35     EXPECT_EQ(0, memcmp(original_data_pointer, new_data_pointer,
36                         actual_length));
37   }
38   PacketReader* reader_;
39 };
40 
41 // Test lack of initialization
TEST_F(PacketReaderTest,Uninitialized)42 TEST_F(PacketReaderTest, Uninitialized) {
43   WebRtc_UWord8* data_pointer = NULL;
44   EXPECT_EQ(-1, reader_->NextPacket(&data_pointer));
45   EXPECT_EQ(NULL, data_pointer);
46 }
47 
TEST_F(PacketReaderTest,InitializeZeroLengthArgument)48 TEST_F(PacketReaderTest, InitializeZeroLengthArgument) {
49   reader_->InitializeReading(packet_data_, 0, kPacketSizeInBytes);
50   ASSERT_EQ(0, reader_->NextPacket(&packet_data_pointer_));
51 }
52 
53 // Test with something smaller than one packet
TEST_F(PacketReaderTest,NormalSmallData)54 TEST_F(PacketReaderTest, NormalSmallData) {
55   const int kDataLengthInBytes = 1499;
56   WebRtc_UWord8 data[kDataLengthInBytes];
57   WebRtc_UWord8* data_pointer = data;
58   memset(data, 1, kDataLengthInBytes);
59 
60   reader_->InitializeReading(data, kDataLengthInBytes, kPacketSizeInBytes);
61   int length_to_read = reader_->NextPacket(&data_pointer);
62   VerifyPacketData(kDataLengthInBytes, length_to_read, data, data_pointer);
63   EXPECT_EQ(0, data_pointer - data);  // pointer hasn't moved
64 
65   // Reading another one shall result in 0 bytes:
66   length_to_read = reader_->NextPacket(&data_pointer);
67   EXPECT_EQ(0, length_to_read);
68   EXPECT_EQ(kDataLengthInBytes, data_pointer - data);
69 }
70 
71 // Test with data length that exactly matches one packet
TEST_F(PacketReaderTest,NormalOnePacketData)72 TEST_F(PacketReaderTest, NormalOnePacketData) {
73   WebRtc_UWord8 data[kPacketSizeInBytes];
74   WebRtc_UWord8* data_pointer = data;
75   memset(data, 1, kPacketSizeInBytes);
76 
77   reader_->InitializeReading(data, kPacketSizeInBytes, kPacketSizeInBytes);
78   int length_to_read = reader_->NextPacket(&data_pointer);
79   VerifyPacketData(kPacketSizeInBytes, length_to_read, data, data_pointer);
80   EXPECT_EQ(0, data_pointer - data);  // pointer hasn't moved
81 
82   // Reading another one shall result in 0 bytes:
83   length_to_read = reader_->NextPacket(&data_pointer);
84   EXPECT_EQ(0, length_to_read);
85   EXPECT_EQ(kPacketSizeInBytes, data_pointer - data);
86 }
87 
88 // Test with data length that will result in 3 packets
TEST_F(PacketReaderTest,NormalLargeData)89 TEST_F(PacketReaderTest, NormalLargeData) {
90   reader_->InitializeReading(packet_data_, kPacketDataLength,
91                              kPacketSizeInBytes);
92 
93   int length_to_read = reader_->NextPacket(&packet_data_pointer_);
94   VerifyPacketData(kPacketSizeInBytes, length_to_read,
95                    packet1_, packet_data_pointer_);
96 
97   length_to_read = reader_->NextPacket(&packet_data_pointer_);
98   VerifyPacketData(kPacketSizeInBytes, length_to_read,
99                    packet2_, packet_data_pointer_);
100 
101   length_to_read = reader_->NextPacket(&packet_data_pointer_);
102   VerifyPacketData(1u, length_to_read,
103                    packet3_, packet_data_pointer_);
104 
105   // Reading another one shall result in 0 bytes:
106   length_to_read = reader_->NextPacket(&packet_data_pointer_);
107   EXPECT_EQ(0, length_to_read);
108   EXPECT_EQ(kPacketDataLength, packet_data_pointer_ - packet_data_);
109 }
110 
111 // Test with empty data.
TEST_F(PacketReaderTest,EmptyData)112 TEST_F(PacketReaderTest, EmptyData) {
113   const int kDataLengthInBytes = 0;
114   WebRtc_UWord8* data = new WebRtc_UWord8[kDataLengthInBytes];
115   reader_->InitializeReading(data, kDataLengthInBytes, kPacketSizeInBytes);
116   EXPECT_EQ(kDataLengthInBytes, reader_->NextPacket(&data));
117   // Do it again to make sure nothing changes
118   EXPECT_EQ(kDataLengthInBytes, reader_->NextPacket(&data));
119   delete[] data;
120 }
121 
122 }  // namespace test
123 }  // namespace webrtc
124