1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker * Copyright 2004 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker *
4*d9f75844SAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker * that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker * tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker * in the file PATENTS. All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker * be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker */
10*d9f75844SAndroid Build Coastguard Worker
11*d9f75844SAndroid Build Coastguard Worker #include "pc/srtp_session.h"
12*d9f75844SAndroid Build Coastguard Worker
13*d9f75844SAndroid Build Coastguard Worker #include <string.h>
14*d9f75844SAndroid Build Coastguard Worker
15*d9f75844SAndroid Build Coastguard Worker #include <string>
16*d9f75844SAndroid Build Coastguard Worker
17*d9f75844SAndroid Build Coastguard Worker #include "media/base/fake_rtp.h"
18*d9f75844SAndroid Build Coastguard Worker #include "pc/test/srtp_test_util.h"
19*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/byte_order.h"
20*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/ssl_stream_adapter.h" // For rtc::SRTP_*
21*d9f75844SAndroid Build Coastguard Worker #include "system_wrappers/include/metrics.h"
22*d9f75844SAndroid Build Coastguard Worker #include "test/gmock.h"
23*d9f75844SAndroid Build Coastguard Worker #include "test/gtest.h"
24*d9f75844SAndroid Build Coastguard Worker #include "test/scoped_key_value_config.h"
25*d9f75844SAndroid Build Coastguard Worker #include "third_party/libsrtp/include/srtp.h"
26*d9f75844SAndroid Build Coastguard Worker
27*d9f75844SAndroid Build Coastguard Worker using ::testing::ElementsAre;
28*d9f75844SAndroid Build Coastguard Worker using ::testing::Pair;
29*d9f75844SAndroid Build Coastguard Worker
30*d9f75844SAndroid Build Coastguard Worker namespace rtc {
31*d9f75844SAndroid Build Coastguard Worker
32*d9f75844SAndroid Build Coastguard Worker std::vector<int> kEncryptedHeaderExtensionIds;
33*d9f75844SAndroid Build Coastguard Worker
34*d9f75844SAndroid Build Coastguard Worker class SrtpSessionTest : public ::testing::Test {
35*d9f75844SAndroid Build Coastguard Worker public:
SrtpSessionTest()36*d9f75844SAndroid Build Coastguard Worker SrtpSessionTest() : s1_(field_trials_), s2_(field_trials_) {
37*d9f75844SAndroid Build Coastguard Worker webrtc::metrics::Reset();
38*d9f75844SAndroid Build Coastguard Worker }
39*d9f75844SAndroid Build Coastguard Worker
40*d9f75844SAndroid Build Coastguard Worker protected:
SetUp()41*d9f75844SAndroid Build Coastguard Worker virtual void SetUp() {
42*d9f75844SAndroid Build Coastguard Worker rtp_len_ = sizeof(kPcmuFrame);
43*d9f75844SAndroid Build Coastguard Worker rtcp_len_ = sizeof(kRtcpReport);
44*d9f75844SAndroid Build Coastguard Worker memcpy(rtp_packet_, kPcmuFrame, rtp_len_);
45*d9f75844SAndroid Build Coastguard Worker memcpy(rtcp_packet_, kRtcpReport, rtcp_len_);
46*d9f75844SAndroid Build Coastguard Worker }
TestProtectRtp(const std::string & cs)47*d9f75844SAndroid Build Coastguard Worker void TestProtectRtp(const std::string& cs) {
48*d9f75844SAndroid Build Coastguard Worker int out_len = 0;
49*d9f75844SAndroid Build Coastguard Worker EXPECT_TRUE(
50*d9f75844SAndroid Build Coastguard Worker s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_), &out_len));
51*d9f75844SAndroid Build Coastguard Worker EXPECT_EQ(out_len, rtp_len_ + rtp_auth_tag_len(cs));
52*d9f75844SAndroid Build Coastguard Worker EXPECT_NE(0, memcmp(rtp_packet_, kPcmuFrame, rtp_len_));
53*d9f75844SAndroid Build Coastguard Worker rtp_len_ = out_len;
54*d9f75844SAndroid Build Coastguard Worker }
TestProtectRtcp(const std::string & cs)55*d9f75844SAndroid Build Coastguard Worker void TestProtectRtcp(const std::string& cs) {
56*d9f75844SAndroid Build Coastguard Worker int out_len = 0;
57*d9f75844SAndroid Build Coastguard Worker EXPECT_TRUE(s1_.ProtectRtcp(rtcp_packet_, rtcp_len_, sizeof(rtcp_packet_),
58*d9f75844SAndroid Build Coastguard Worker &out_len));
59*d9f75844SAndroid Build Coastguard Worker EXPECT_EQ(out_len, rtcp_len_ + 4 + rtcp_auth_tag_len(cs)); // NOLINT
60*d9f75844SAndroid Build Coastguard Worker EXPECT_NE(0, memcmp(rtcp_packet_, kRtcpReport, rtcp_len_));
61*d9f75844SAndroid Build Coastguard Worker rtcp_len_ = out_len;
62*d9f75844SAndroid Build Coastguard Worker }
TestUnprotectRtp(const std::string & cs)63*d9f75844SAndroid Build Coastguard Worker void TestUnprotectRtp(const std::string& cs) {
64*d9f75844SAndroid Build Coastguard Worker int out_len = 0, expected_len = sizeof(kPcmuFrame);
65*d9f75844SAndroid Build Coastguard Worker EXPECT_TRUE(s2_.UnprotectRtp(rtp_packet_, rtp_len_, &out_len));
66*d9f75844SAndroid Build Coastguard Worker EXPECT_EQ(expected_len, out_len);
67*d9f75844SAndroid Build Coastguard Worker EXPECT_EQ(0, memcmp(rtp_packet_, kPcmuFrame, out_len));
68*d9f75844SAndroid Build Coastguard Worker }
TestUnprotectRtcp(const std::string & cs)69*d9f75844SAndroid Build Coastguard Worker void TestUnprotectRtcp(const std::string& cs) {
70*d9f75844SAndroid Build Coastguard Worker int out_len = 0, expected_len = sizeof(kRtcpReport);
71*d9f75844SAndroid Build Coastguard Worker EXPECT_TRUE(s2_.UnprotectRtcp(rtcp_packet_, rtcp_len_, &out_len));
72*d9f75844SAndroid Build Coastguard Worker EXPECT_EQ(expected_len, out_len);
73*d9f75844SAndroid Build Coastguard Worker EXPECT_EQ(0, memcmp(rtcp_packet_, kRtcpReport, out_len));
74*d9f75844SAndroid Build Coastguard Worker }
75*d9f75844SAndroid Build Coastguard Worker webrtc::test::ScopedKeyValueConfig field_trials_;
76*d9f75844SAndroid Build Coastguard Worker cricket::SrtpSession s1_;
77*d9f75844SAndroid Build Coastguard Worker cricket::SrtpSession s2_;
78*d9f75844SAndroid Build Coastguard Worker char rtp_packet_[sizeof(kPcmuFrame) + 10];
79*d9f75844SAndroid Build Coastguard Worker char rtcp_packet_[sizeof(kRtcpReport) + 4 + 10];
80*d9f75844SAndroid Build Coastguard Worker int rtp_len_;
81*d9f75844SAndroid Build Coastguard Worker int rtcp_len_;
82*d9f75844SAndroid Build Coastguard Worker };
83*d9f75844SAndroid Build Coastguard Worker
84*d9f75844SAndroid Build Coastguard Worker // Test that we can set up the session and keys properly.
TEST_F(SrtpSessionTest,TestGoodSetup)85*d9f75844SAndroid Build Coastguard Worker TEST_F(SrtpSessionTest, TestGoodSetup) {
86*d9f75844SAndroid Build Coastguard Worker EXPECT_TRUE(s1_.SetSend(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen,
87*d9f75844SAndroid Build Coastguard Worker kEncryptedHeaderExtensionIds));
88*d9f75844SAndroid Build Coastguard Worker EXPECT_TRUE(s2_.SetRecv(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen,
89*d9f75844SAndroid Build Coastguard Worker kEncryptedHeaderExtensionIds));
90*d9f75844SAndroid Build Coastguard Worker }
91*d9f75844SAndroid Build Coastguard Worker
92*d9f75844SAndroid Build Coastguard Worker // Test that we can't change the keys once set.
TEST_F(SrtpSessionTest,TestBadSetup)93*d9f75844SAndroid Build Coastguard Worker TEST_F(SrtpSessionTest, TestBadSetup) {
94*d9f75844SAndroid Build Coastguard Worker EXPECT_TRUE(s1_.SetSend(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen,
95*d9f75844SAndroid Build Coastguard Worker kEncryptedHeaderExtensionIds));
96*d9f75844SAndroid Build Coastguard Worker EXPECT_TRUE(s2_.SetRecv(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen,
97*d9f75844SAndroid Build Coastguard Worker kEncryptedHeaderExtensionIds));
98*d9f75844SAndroid Build Coastguard Worker EXPECT_FALSE(s1_.SetSend(kSrtpAes128CmSha1_80, kTestKey2, kTestKeyLen,
99*d9f75844SAndroid Build Coastguard Worker kEncryptedHeaderExtensionIds));
100*d9f75844SAndroid Build Coastguard Worker EXPECT_FALSE(s2_.SetRecv(kSrtpAes128CmSha1_80, kTestKey2, kTestKeyLen,
101*d9f75844SAndroid Build Coastguard Worker kEncryptedHeaderExtensionIds));
102*d9f75844SAndroid Build Coastguard Worker }
103*d9f75844SAndroid Build Coastguard Worker
104*d9f75844SAndroid Build Coastguard Worker // Test that we fail keys of the wrong length.
TEST_F(SrtpSessionTest,TestKeysTooShort)105*d9f75844SAndroid Build Coastguard Worker TEST_F(SrtpSessionTest, TestKeysTooShort) {
106*d9f75844SAndroid Build Coastguard Worker EXPECT_FALSE(s1_.SetSend(kSrtpAes128CmSha1_80, kTestKey1, 1,
107*d9f75844SAndroid Build Coastguard Worker kEncryptedHeaderExtensionIds));
108*d9f75844SAndroid Build Coastguard Worker EXPECT_FALSE(s2_.SetRecv(kSrtpAes128CmSha1_80, kTestKey1, 1,
109*d9f75844SAndroid Build Coastguard Worker kEncryptedHeaderExtensionIds));
110*d9f75844SAndroid Build Coastguard Worker }
111*d9f75844SAndroid Build Coastguard Worker
112*d9f75844SAndroid Build Coastguard Worker // Test that we can encrypt and decrypt RTP/RTCP using AES_CM_128_HMAC_SHA1_80.
TEST_F(SrtpSessionTest,TestProtect_AES_CM_128_HMAC_SHA1_80)113*d9f75844SAndroid Build Coastguard Worker TEST_F(SrtpSessionTest, TestProtect_AES_CM_128_HMAC_SHA1_80) {
114*d9f75844SAndroid Build Coastguard Worker EXPECT_TRUE(s1_.SetSend(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen,
115*d9f75844SAndroid Build Coastguard Worker kEncryptedHeaderExtensionIds));
116*d9f75844SAndroid Build Coastguard Worker EXPECT_TRUE(s2_.SetRecv(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen,
117*d9f75844SAndroid Build Coastguard Worker kEncryptedHeaderExtensionIds));
118*d9f75844SAndroid Build Coastguard Worker TestProtectRtp(kCsAesCm128HmacSha1_80);
119*d9f75844SAndroid Build Coastguard Worker TestProtectRtcp(kCsAesCm128HmacSha1_80);
120*d9f75844SAndroid Build Coastguard Worker TestUnprotectRtp(kCsAesCm128HmacSha1_80);
121*d9f75844SAndroid Build Coastguard Worker TestUnprotectRtcp(kCsAesCm128HmacSha1_80);
122*d9f75844SAndroid Build Coastguard Worker }
123*d9f75844SAndroid Build Coastguard Worker
124*d9f75844SAndroid Build Coastguard Worker // Test that we can encrypt and decrypt RTP/RTCP using AES_CM_128_HMAC_SHA1_32.
TEST_F(SrtpSessionTest,TestProtect_AES_CM_128_HMAC_SHA1_32)125*d9f75844SAndroid Build Coastguard Worker TEST_F(SrtpSessionTest, TestProtect_AES_CM_128_HMAC_SHA1_32) {
126*d9f75844SAndroid Build Coastguard Worker EXPECT_TRUE(s1_.SetSend(kSrtpAes128CmSha1_32, kTestKey1, kTestKeyLen,
127*d9f75844SAndroid Build Coastguard Worker kEncryptedHeaderExtensionIds));
128*d9f75844SAndroid Build Coastguard Worker EXPECT_TRUE(s2_.SetRecv(kSrtpAes128CmSha1_32, kTestKey1, kTestKeyLen,
129*d9f75844SAndroid Build Coastguard Worker kEncryptedHeaderExtensionIds));
130*d9f75844SAndroid Build Coastguard Worker TestProtectRtp(kCsAesCm128HmacSha1_32);
131*d9f75844SAndroid Build Coastguard Worker TestProtectRtcp(kCsAesCm128HmacSha1_32);
132*d9f75844SAndroid Build Coastguard Worker TestUnprotectRtp(kCsAesCm128HmacSha1_32);
133*d9f75844SAndroid Build Coastguard Worker TestUnprotectRtcp(kCsAesCm128HmacSha1_32);
134*d9f75844SAndroid Build Coastguard Worker }
135*d9f75844SAndroid Build Coastguard Worker
TEST_F(SrtpSessionTest,TestGetSendStreamPacketIndex)136*d9f75844SAndroid Build Coastguard Worker TEST_F(SrtpSessionTest, TestGetSendStreamPacketIndex) {
137*d9f75844SAndroid Build Coastguard Worker EXPECT_TRUE(s1_.SetSend(kSrtpAes128CmSha1_32, kTestKey1, kTestKeyLen,
138*d9f75844SAndroid Build Coastguard Worker kEncryptedHeaderExtensionIds));
139*d9f75844SAndroid Build Coastguard Worker int64_t index;
140*d9f75844SAndroid Build Coastguard Worker int out_len = 0;
141*d9f75844SAndroid Build Coastguard Worker EXPECT_TRUE(s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_),
142*d9f75844SAndroid Build Coastguard Worker &out_len, &index));
143*d9f75844SAndroid Build Coastguard Worker // `index` will be shifted by 16.
144*d9f75844SAndroid Build Coastguard Worker int64_t be64_index = static_cast<int64_t>(NetworkToHost64(1 << 16));
145*d9f75844SAndroid Build Coastguard Worker EXPECT_EQ(be64_index, index);
146*d9f75844SAndroid Build Coastguard Worker }
147*d9f75844SAndroid Build Coastguard Worker
148*d9f75844SAndroid Build Coastguard Worker // Test that we fail to unprotect if someone tampers with the RTP/RTCP paylaods.
TEST_F(SrtpSessionTest,TestTamperReject)149*d9f75844SAndroid Build Coastguard Worker TEST_F(SrtpSessionTest, TestTamperReject) {
150*d9f75844SAndroid Build Coastguard Worker int out_len;
151*d9f75844SAndroid Build Coastguard Worker EXPECT_TRUE(s1_.SetSend(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen,
152*d9f75844SAndroid Build Coastguard Worker kEncryptedHeaderExtensionIds));
153*d9f75844SAndroid Build Coastguard Worker EXPECT_TRUE(s2_.SetRecv(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen,
154*d9f75844SAndroid Build Coastguard Worker kEncryptedHeaderExtensionIds));
155*d9f75844SAndroid Build Coastguard Worker TestProtectRtp(kCsAesCm128HmacSha1_80);
156*d9f75844SAndroid Build Coastguard Worker TestProtectRtcp(kCsAesCm128HmacSha1_80);
157*d9f75844SAndroid Build Coastguard Worker rtp_packet_[0] = 0x12;
158*d9f75844SAndroid Build Coastguard Worker rtcp_packet_[1] = 0x34;
159*d9f75844SAndroid Build Coastguard Worker EXPECT_FALSE(s2_.UnprotectRtp(rtp_packet_, rtp_len_, &out_len));
160*d9f75844SAndroid Build Coastguard Worker EXPECT_METRIC_THAT(
161*d9f75844SAndroid Build Coastguard Worker webrtc::metrics::Samples("WebRTC.PeerConnection.SrtpUnprotectError"),
162*d9f75844SAndroid Build Coastguard Worker ElementsAre(Pair(srtp_err_status_bad_param, 1)));
163*d9f75844SAndroid Build Coastguard Worker EXPECT_FALSE(s2_.UnprotectRtcp(rtcp_packet_, rtcp_len_, &out_len));
164*d9f75844SAndroid Build Coastguard Worker EXPECT_METRIC_THAT(
165*d9f75844SAndroid Build Coastguard Worker webrtc::metrics::Samples("WebRTC.PeerConnection.SrtcpUnprotectError"),
166*d9f75844SAndroid Build Coastguard Worker ElementsAre(Pair(srtp_err_status_auth_fail, 1)));
167*d9f75844SAndroid Build Coastguard Worker }
168*d9f75844SAndroid Build Coastguard Worker
169*d9f75844SAndroid Build Coastguard Worker // Test that we fail to unprotect if the payloads are not authenticated.
TEST_F(SrtpSessionTest,TestUnencryptReject)170*d9f75844SAndroid Build Coastguard Worker TEST_F(SrtpSessionTest, TestUnencryptReject) {
171*d9f75844SAndroid Build Coastguard Worker int out_len;
172*d9f75844SAndroid Build Coastguard Worker EXPECT_TRUE(s1_.SetSend(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen,
173*d9f75844SAndroid Build Coastguard Worker kEncryptedHeaderExtensionIds));
174*d9f75844SAndroid Build Coastguard Worker EXPECT_TRUE(s2_.SetRecv(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen,
175*d9f75844SAndroid Build Coastguard Worker kEncryptedHeaderExtensionIds));
176*d9f75844SAndroid Build Coastguard Worker EXPECT_FALSE(s2_.UnprotectRtp(rtp_packet_, rtp_len_, &out_len));
177*d9f75844SAndroid Build Coastguard Worker EXPECT_METRIC_THAT(
178*d9f75844SAndroid Build Coastguard Worker webrtc::metrics::Samples("WebRTC.PeerConnection.SrtpUnprotectError"),
179*d9f75844SAndroid Build Coastguard Worker ElementsAre(Pair(srtp_err_status_auth_fail, 1)));
180*d9f75844SAndroid Build Coastguard Worker EXPECT_FALSE(s2_.UnprotectRtcp(rtcp_packet_, rtcp_len_, &out_len));
181*d9f75844SAndroid Build Coastguard Worker EXPECT_METRIC_THAT(
182*d9f75844SAndroid Build Coastguard Worker webrtc::metrics::Samples("WebRTC.PeerConnection.SrtcpUnprotectError"),
183*d9f75844SAndroid Build Coastguard Worker ElementsAre(Pair(srtp_err_status_cant_check, 1)));
184*d9f75844SAndroid Build Coastguard Worker }
185*d9f75844SAndroid Build Coastguard Worker
186*d9f75844SAndroid Build Coastguard Worker // Test that we fail when using buffers that are too small.
TEST_F(SrtpSessionTest,TestBuffersTooSmall)187*d9f75844SAndroid Build Coastguard Worker TEST_F(SrtpSessionTest, TestBuffersTooSmall) {
188*d9f75844SAndroid Build Coastguard Worker int out_len;
189*d9f75844SAndroid Build Coastguard Worker EXPECT_TRUE(s1_.SetSend(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen,
190*d9f75844SAndroid Build Coastguard Worker kEncryptedHeaderExtensionIds));
191*d9f75844SAndroid Build Coastguard Worker EXPECT_FALSE(s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_) - 10,
192*d9f75844SAndroid Build Coastguard Worker &out_len));
193*d9f75844SAndroid Build Coastguard Worker EXPECT_FALSE(s1_.ProtectRtcp(rtcp_packet_, rtcp_len_,
194*d9f75844SAndroid Build Coastguard Worker sizeof(rtcp_packet_) - 14, &out_len));
195*d9f75844SAndroid Build Coastguard Worker }
196*d9f75844SAndroid Build Coastguard Worker
TEST_F(SrtpSessionTest,TestReplay)197*d9f75844SAndroid Build Coastguard Worker TEST_F(SrtpSessionTest, TestReplay) {
198*d9f75844SAndroid Build Coastguard Worker static const uint16_t kMaxSeqnum = static_cast<uint16_t>(-1);
199*d9f75844SAndroid Build Coastguard Worker static const uint16_t seqnum_big = 62275;
200*d9f75844SAndroid Build Coastguard Worker static const uint16_t seqnum_small = 10;
201*d9f75844SAndroid Build Coastguard Worker static const uint16_t replay_window = 1024;
202*d9f75844SAndroid Build Coastguard Worker int out_len;
203*d9f75844SAndroid Build Coastguard Worker
204*d9f75844SAndroid Build Coastguard Worker EXPECT_TRUE(s1_.SetSend(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen,
205*d9f75844SAndroid Build Coastguard Worker kEncryptedHeaderExtensionIds));
206*d9f75844SAndroid Build Coastguard Worker EXPECT_TRUE(s2_.SetRecv(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen,
207*d9f75844SAndroid Build Coastguard Worker kEncryptedHeaderExtensionIds));
208*d9f75844SAndroid Build Coastguard Worker
209*d9f75844SAndroid Build Coastguard Worker // Initial sequence number.
210*d9f75844SAndroid Build Coastguard Worker SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_) + 2, seqnum_big);
211*d9f75844SAndroid Build Coastguard Worker EXPECT_TRUE(
212*d9f75844SAndroid Build Coastguard Worker s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_), &out_len));
213*d9f75844SAndroid Build Coastguard Worker
214*d9f75844SAndroid Build Coastguard Worker // Replay within the 1024 window should succeed.
215*d9f75844SAndroid Build Coastguard Worker SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_) + 2,
216*d9f75844SAndroid Build Coastguard Worker seqnum_big - replay_window + 1);
217*d9f75844SAndroid Build Coastguard Worker EXPECT_TRUE(
218*d9f75844SAndroid Build Coastguard Worker s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_), &out_len));
219*d9f75844SAndroid Build Coastguard Worker
220*d9f75844SAndroid Build Coastguard Worker // Replay out side of the 1024 window should fail.
221*d9f75844SAndroid Build Coastguard Worker SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_) + 2,
222*d9f75844SAndroid Build Coastguard Worker seqnum_big - replay_window - 1);
223*d9f75844SAndroid Build Coastguard Worker EXPECT_FALSE(
224*d9f75844SAndroid Build Coastguard Worker s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_), &out_len));
225*d9f75844SAndroid Build Coastguard Worker
226*d9f75844SAndroid Build Coastguard Worker // Increment sequence number to a small number.
227*d9f75844SAndroid Build Coastguard Worker SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_) + 2, seqnum_small);
228*d9f75844SAndroid Build Coastguard Worker EXPECT_TRUE(
229*d9f75844SAndroid Build Coastguard Worker s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_), &out_len));
230*d9f75844SAndroid Build Coastguard Worker
231*d9f75844SAndroid Build Coastguard Worker // Replay around 0 but out side of the 1024 window should fail.
232*d9f75844SAndroid Build Coastguard Worker SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_) + 2,
233*d9f75844SAndroid Build Coastguard Worker kMaxSeqnum + seqnum_small - replay_window - 1);
234*d9f75844SAndroid Build Coastguard Worker EXPECT_FALSE(
235*d9f75844SAndroid Build Coastguard Worker s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_), &out_len));
236*d9f75844SAndroid Build Coastguard Worker
237*d9f75844SAndroid Build Coastguard Worker // Replay around 0 but within the 1024 window should succeed.
238*d9f75844SAndroid Build Coastguard Worker for (uint16_t seqnum = 65000; seqnum < 65003; ++seqnum) {
239*d9f75844SAndroid Build Coastguard Worker SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_) + 2, seqnum);
240*d9f75844SAndroid Build Coastguard Worker EXPECT_TRUE(
241*d9f75844SAndroid Build Coastguard Worker s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_), &out_len));
242*d9f75844SAndroid Build Coastguard Worker }
243*d9f75844SAndroid Build Coastguard Worker
244*d9f75844SAndroid Build Coastguard Worker // Go back to normal sequence nubmer.
245*d9f75844SAndroid Build Coastguard Worker // NOTE: without the fix in libsrtp, this would fail. This is because
246*d9f75844SAndroid Build Coastguard Worker // without the fix, the loop above would keep incrementing local sequence
247*d9f75844SAndroid Build Coastguard Worker // number in libsrtp, eventually the new sequence number would go out side
248*d9f75844SAndroid Build Coastguard Worker // of the window.
249*d9f75844SAndroid Build Coastguard Worker SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_) + 2, seqnum_small + 1);
250*d9f75844SAndroid Build Coastguard Worker EXPECT_TRUE(
251*d9f75844SAndroid Build Coastguard Worker s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_), &out_len));
252*d9f75844SAndroid Build Coastguard Worker }
253*d9f75844SAndroid Build Coastguard Worker
254*d9f75844SAndroid Build Coastguard Worker } // namespace rtc
255