| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2013 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include <memory> | 11 #include <memory> |
| 12 | 12 |
| 13 #include "webrtc/api/datachannel.h" | 13 #include "webrtc/api/datachannel.h" |
| 14 #include "webrtc/api/sctputils.h" | 14 #include "webrtc/api/sctputils.h" |
| 15 #include "webrtc/api/test/fakedatachannelprovider.h" | 15 #include "webrtc/api/test/fakedatachannelprovider.h" |
| 16 #include "webrtc/base/gunit.h" | 16 #include "webrtc/base/gunit.h" |
| 17 | 17 |
| 18 using webrtc::DataChannel; | 18 using webrtc::DataChannel; |
| 19 using webrtc::SctpSidAllocator; | 19 using webrtc::SctpSidAllocator; |
| 20 | 20 |
| 21 static constexpr int kDefaultTimeout = 10000; |
| 22 |
| 21 class FakeDataChannelObserver : public webrtc::DataChannelObserver { | 23 class FakeDataChannelObserver : public webrtc::DataChannelObserver { |
| 22 public: | 24 public: |
| 23 FakeDataChannelObserver() | 25 FakeDataChannelObserver() |
| 24 : messages_received_(0), | 26 : messages_received_(0), |
| 25 on_state_change_count_(0), | 27 on_state_change_count_(0), |
| 26 on_buffered_amount_change_count_(0) {} | 28 on_buffered_amount_change_count_(0) {} |
| 27 | 29 |
| 28 void OnStateChange() { | 30 void OnStateChange() { |
| 29 ++on_state_change_count_; | 31 ++on_state_change_count_; |
| 30 } | 32 } |
| (...skipping 28 matching lines...) Expand all Loading... |
| 59 | 61 |
| 60 private: | 62 private: |
| 61 size_t messages_received_; | 63 size_t messages_received_; |
| 62 size_t on_state_change_count_; | 64 size_t on_state_change_count_; |
| 63 size_t on_buffered_amount_change_count_; | 65 size_t on_buffered_amount_change_count_; |
| 64 }; | 66 }; |
| 65 | 67 |
| 66 class SctpDataChannelTest : public testing::Test { | 68 class SctpDataChannelTest : public testing::Test { |
| 67 protected: | 69 protected: |
| 68 SctpDataChannelTest() | 70 SctpDataChannelTest() |
| 69 : webrtc_data_channel_( | 71 : provider_(new FakeDataChannelProvider()), |
| 70 DataChannel::Create( | 72 webrtc_data_channel_(DataChannel::Create(provider_.get(), |
| 71 &provider_, cricket::DCT_SCTP, "test", init_)) { | 73 cricket::DCT_SCTP, |
| 72 } | 74 "test", |
| 75 init_)) {} |
| 73 | 76 |
| 74 void SetChannelReady() { | 77 void SetChannelReady() { |
| 75 provider_.set_transport_available(true); | 78 provider_->set_transport_available(true); |
| 76 webrtc_data_channel_->OnTransportChannelCreated(); | 79 webrtc_data_channel_->OnTransportChannelCreated(); |
| 77 if (webrtc_data_channel_->id() < 0) { | 80 if (webrtc_data_channel_->id() < 0) { |
| 78 webrtc_data_channel_->SetSctpSid(0); | 81 webrtc_data_channel_->SetSctpSid(0); |
| 79 } | 82 } |
| 80 provider_.set_ready_to_send(true); | 83 provider_->set_ready_to_send(true); |
| 81 } | 84 } |
| 82 | 85 |
| 83 void AddObserver() { | 86 void AddObserver() { |
| 84 observer_.reset(new FakeDataChannelObserver()); | 87 observer_.reset(new FakeDataChannelObserver()); |
| 85 webrtc_data_channel_->RegisterObserver(observer_.get()); | 88 webrtc_data_channel_->RegisterObserver(observer_.get()); |
| 86 } | 89 } |
| 87 | 90 |
| 88 webrtc::InternalDataChannelInit init_; | 91 webrtc::InternalDataChannelInit init_; |
| 89 FakeDataChannelProvider provider_; | 92 std::unique_ptr<FakeDataChannelProvider> provider_; |
| 90 std::unique_ptr<FakeDataChannelObserver> observer_; | 93 std::unique_ptr<FakeDataChannelObserver> observer_; |
| 91 rtc::scoped_refptr<DataChannel> webrtc_data_channel_; | 94 rtc::scoped_refptr<DataChannel> webrtc_data_channel_; |
| 92 }; | 95 }; |
| 93 | 96 |
| 94 // Verifies that the data channel is connected to the transport after creation. | 97 // Verifies that the data channel is connected to the transport after creation. |
| 95 TEST_F(SctpDataChannelTest, ConnectedToTransportOnCreated) { | 98 TEST_F(SctpDataChannelTest, ConnectedToTransportOnCreated) { |
| 96 provider_.set_transport_available(true); | 99 provider_->set_transport_available(true); |
| 97 rtc::scoped_refptr<DataChannel> dc = DataChannel::Create( | 100 rtc::scoped_refptr<DataChannel> dc = |
| 98 &provider_, cricket::DCT_SCTP, "test1", init_); | 101 DataChannel::Create(provider_.get(), cricket::DCT_SCTP, "test1", init_); |
| 99 | 102 |
| 100 EXPECT_TRUE(provider_.IsConnected(dc.get())); | 103 EXPECT_TRUE(provider_->IsConnected(dc.get())); |
| 101 // The sid is not set yet, so it should not have added the streams. | 104 // The sid is not set yet, so it should not have added the streams. |
| 102 EXPECT_FALSE(provider_.IsSendStreamAdded(dc->id())); | 105 EXPECT_FALSE(provider_->IsSendStreamAdded(dc->id())); |
| 103 EXPECT_FALSE(provider_.IsRecvStreamAdded(dc->id())); | 106 EXPECT_FALSE(provider_->IsRecvStreamAdded(dc->id())); |
| 104 | 107 |
| 105 dc->SetSctpSid(0); | 108 dc->SetSctpSid(0); |
| 106 EXPECT_TRUE(provider_.IsSendStreamAdded(dc->id())); | 109 EXPECT_TRUE(provider_->IsSendStreamAdded(dc->id())); |
| 107 EXPECT_TRUE(provider_.IsRecvStreamAdded(dc->id())); | 110 EXPECT_TRUE(provider_->IsRecvStreamAdded(dc->id())); |
| 108 } | 111 } |
| 109 | 112 |
| 110 // Verifies that the data channel is connected to the transport if the transport | 113 // Verifies that the data channel is connected to the transport if the transport |
| 111 // is not available initially and becomes available later. | 114 // is not available initially and becomes available later. |
| 112 TEST_F(SctpDataChannelTest, ConnectedAfterTransportBecomesAvailable) { | 115 TEST_F(SctpDataChannelTest, ConnectedAfterTransportBecomesAvailable) { |
| 113 EXPECT_FALSE(provider_.IsConnected(webrtc_data_channel_.get())); | 116 EXPECT_FALSE(provider_->IsConnected(webrtc_data_channel_.get())); |
| 114 | 117 |
| 115 provider_.set_transport_available(true); | 118 provider_->set_transport_available(true); |
| 116 webrtc_data_channel_->OnTransportChannelCreated(); | 119 webrtc_data_channel_->OnTransportChannelCreated(); |
| 117 EXPECT_TRUE(provider_.IsConnected(webrtc_data_channel_.get())); | 120 EXPECT_TRUE(provider_->IsConnected(webrtc_data_channel_.get())); |
| 118 } | 121 } |
| 119 | 122 |
| 120 // Tests the state of the data channel. | 123 // Tests the state of the data channel. |
| 121 TEST_F(SctpDataChannelTest, StateTransition) { | 124 TEST_F(SctpDataChannelTest, StateTransition) { |
| 122 EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, | 125 EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, |
| 123 webrtc_data_channel_->state()); | 126 webrtc_data_channel_->state()); |
| 124 SetChannelReady(); | 127 SetChannelReady(); |
| 125 | 128 |
| 126 EXPECT_EQ(webrtc::DataChannelInterface::kOpen, webrtc_data_channel_->state()); | 129 EXPECT_EQ(webrtc::DataChannelInterface::kOpen, webrtc_data_channel_->state()); |
| 127 webrtc_data_channel_->Close(); | 130 webrtc_data_channel_->Close(); |
| 128 EXPECT_EQ(webrtc::DataChannelInterface::kClosed, | 131 EXPECT_EQ(webrtc::DataChannelInterface::kClosed, |
| 129 webrtc_data_channel_->state()); | 132 webrtc_data_channel_->state()); |
| 130 // Verifies that it's disconnected from the transport. | 133 // Verifies that it's disconnected from the transport. |
| 131 EXPECT_FALSE(provider_.IsConnected(webrtc_data_channel_.get())); | 134 EXPECT_FALSE(provider_->IsConnected(webrtc_data_channel_.get())); |
| 132 } | 135 } |
| 133 | 136 |
| 134 // Tests that DataChannel::buffered_amount() is correct after the channel is | 137 // Tests that DataChannel::buffered_amount() is correct after the channel is |
| 135 // blocked. | 138 // blocked. |
| 136 TEST_F(SctpDataChannelTest, BufferedAmountWhenBlocked) { | 139 TEST_F(SctpDataChannelTest, BufferedAmountWhenBlocked) { |
| 137 AddObserver(); | 140 AddObserver(); |
| 138 SetChannelReady(); | 141 SetChannelReady(); |
| 139 webrtc::DataBuffer buffer("abcd"); | 142 webrtc::DataBuffer buffer("abcd"); |
| 140 EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); | 143 EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
| 141 | 144 |
| 142 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount()); | 145 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount()); |
| 143 EXPECT_EQ(0U, observer_->on_buffered_amount_change_count()); | 146 EXPECT_EQ(0U, observer_->on_buffered_amount_change_count()); |
| 144 | 147 |
| 145 provider_.set_send_blocked(true); | 148 provider_->set_send_blocked(true); |
| 146 | 149 |
| 147 const int number_of_packets = 3; | 150 const int number_of_packets = 3; |
| 148 for (int i = 0; i < number_of_packets; ++i) { | 151 for (int i = 0; i < number_of_packets; ++i) { |
| 149 EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); | 152 EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
| 150 } | 153 } |
| 151 EXPECT_EQ(buffer.data.size() * number_of_packets, | 154 EXPECT_EQ(buffer.data.size() * number_of_packets, |
| 152 webrtc_data_channel_->buffered_amount()); | 155 webrtc_data_channel_->buffered_amount()); |
| 153 EXPECT_EQ(number_of_packets, observer_->on_buffered_amount_change_count()); | 156 EXPECT_EQ(number_of_packets, observer_->on_buffered_amount_change_count()); |
| 154 } | 157 } |
| 155 | 158 |
| 156 // Tests that the queued data are sent when the channel transitions from blocked | 159 // Tests that the queued data are sent when the channel transitions from blocked |
| 157 // to unblocked. | 160 // to unblocked. |
| 158 TEST_F(SctpDataChannelTest, QueuedDataSentWhenUnblocked) { | 161 TEST_F(SctpDataChannelTest, QueuedDataSentWhenUnblocked) { |
| 159 AddObserver(); | 162 AddObserver(); |
| 160 SetChannelReady(); | 163 SetChannelReady(); |
| 161 webrtc::DataBuffer buffer("abcd"); | 164 webrtc::DataBuffer buffer("abcd"); |
| 162 provider_.set_send_blocked(true); | 165 provider_->set_send_blocked(true); |
| 163 EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); | 166 EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
| 164 | 167 |
| 165 EXPECT_EQ(1U, observer_->on_buffered_amount_change_count()); | 168 EXPECT_EQ(1U, observer_->on_buffered_amount_change_count()); |
| 166 | 169 |
| 167 provider_.set_send_blocked(false); | 170 provider_->set_send_blocked(false); |
| 168 SetChannelReady(); | 171 SetChannelReady(); |
| 169 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount()); | 172 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount()); |
| 170 EXPECT_EQ(2U, observer_->on_buffered_amount_change_count()); | 173 EXPECT_EQ(2U, observer_->on_buffered_amount_change_count()); |
| 171 } | 174 } |
| 172 | 175 |
| 173 // Tests that no crash when the channel is blocked right away while trying to | 176 // Tests that no crash when the channel is blocked right away while trying to |
| 174 // send queued data. | 177 // send queued data. |
| 175 TEST_F(SctpDataChannelTest, BlockedWhenSendQueuedDataNoCrash) { | 178 TEST_F(SctpDataChannelTest, BlockedWhenSendQueuedDataNoCrash) { |
| 176 AddObserver(); | 179 AddObserver(); |
| 177 SetChannelReady(); | 180 SetChannelReady(); |
| 178 webrtc::DataBuffer buffer("abcd"); | 181 webrtc::DataBuffer buffer("abcd"); |
| 179 provider_.set_send_blocked(true); | 182 provider_->set_send_blocked(true); |
| 180 EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); | 183 EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
| 181 EXPECT_EQ(1U, observer_->on_buffered_amount_change_count()); | 184 EXPECT_EQ(1U, observer_->on_buffered_amount_change_count()); |
| 182 | 185 |
| 183 // Set channel ready while it is still blocked. | 186 // Set channel ready while it is still blocked. |
| 184 SetChannelReady(); | 187 SetChannelReady(); |
| 185 EXPECT_EQ(buffer.size(), webrtc_data_channel_->buffered_amount()); | 188 EXPECT_EQ(buffer.size(), webrtc_data_channel_->buffered_amount()); |
| 186 EXPECT_EQ(1U, observer_->on_buffered_amount_change_count()); | 189 EXPECT_EQ(1U, observer_->on_buffered_amount_change_count()); |
| 187 | 190 |
| 188 // Unblock the channel to send queued data again, there should be no crash. | 191 // Unblock the channel to send queued data again, there should be no crash. |
| 189 provider_.set_send_blocked(false); | 192 provider_->set_send_blocked(false); |
| 190 SetChannelReady(); | 193 SetChannelReady(); |
| 191 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount()); | 194 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount()); |
| 192 EXPECT_EQ(2U, observer_->on_buffered_amount_change_count()); | 195 EXPECT_EQ(2U, observer_->on_buffered_amount_change_count()); |
| 193 } | 196 } |
| 194 | 197 |
| 195 // Tests that the queued control message is sent when channel is ready. | 198 // Tests that the queued control message is sent when channel is ready. |
| 196 TEST_F(SctpDataChannelTest, OpenMessageSent) { | 199 TEST_F(SctpDataChannelTest, OpenMessageSent) { |
| 197 // Initially the id is unassigned. | 200 // Initially the id is unassigned. |
| 198 EXPECT_EQ(-1, webrtc_data_channel_->id()); | 201 EXPECT_EQ(-1, webrtc_data_channel_->id()); |
| 199 | 202 |
| 200 SetChannelReady(); | 203 SetChannelReady(); |
| 201 EXPECT_GE(webrtc_data_channel_->id(), 0); | 204 EXPECT_GE(webrtc_data_channel_->id(), 0); |
| 202 EXPECT_EQ(cricket::DMT_CONTROL, provider_.last_send_data_params().type); | 205 EXPECT_EQ(cricket::DMT_CONTROL, provider_->last_send_data_params().type); |
| 203 EXPECT_EQ(provider_.last_send_data_params().ssrc, | 206 EXPECT_EQ(provider_->last_send_data_params().ssrc, |
| 204 static_cast<uint32_t>(webrtc_data_channel_->id())); | 207 static_cast<uint32_t>(webrtc_data_channel_->id())); |
| 205 } | 208 } |
| 206 | 209 |
| 207 TEST_F(SctpDataChannelTest, QueuedOpenMessageSent) { | 210 TEST_F(SctpDataChannelTest, QueuedOpenMessageSent) { |
| 208 provider_.set_send_blocked(true); | 211 provider_->set_send_blocked(true); |
| 209 SetChannelReady(); | 212 SetChannelReady(); |
| 210 provider_.set_send_blocked(false); | 213 provider_->set_send_blocked(false); |
| 211 | 214 |
| 212 EXPECT_EQ(cricket::DMT_CONTROL, provider_.last_send_data_params().type); | 215 EXPECT_EQ(cricket::DMT_CONTROL, provider_->last_send_data_params().type); |
| 213 EXPECT_EQ(provider_.last_send_data_params().ssrc, | 216 EXPECT_EQ(provider_->last_send_data_params().ssrc, |
| 214 static_cast<uint32_t>(webrtc_data_channel_->id())); | 217 static_cast<uint32_t>(webrtc_data_channel_->id())); |
| 215 } | 218 } |
| 216 | 219 |
| 217 // Tests that the DataChannel created after transport gets ready can enter OPEN | 220 // Tests that the DataChannel created after transport gets ready can enter OPEN |
| 218 // state. | 221 // state. |
| 219 TEST_F(SctpDataChannelTest, LateCreatedChannelTransitionToOpen) { | 222 TEST_F(SctpDataChannelTest, LateCreatedChannelTransitionToOpen) { |
| 220 SetChannelReady(); | 223 SetChannelReady(); |
| 221 webrtc::InternalDataChannelInit init; | 224 webrtc::InternalDataChannelInit init; |
| 222 init.id = 1; | 225 init.id = 1; |
| 223 rtc::scoped_refptr<DataChannel> dc = DataChannel::Create( | 226 rtc::scoped_refptr<DataChannel> dc = |
| 224 &provider_, cricket::DCT_SCTP, "test1", init); | 227 DataChannel::Create(provider_.get(), cricket::DCT_SCTP, "test1", init); |
| 225 EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, dc->state()); | 228 EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, dc->state()); |
| 226 EXPECT_TRUE_WAIT(webrtc::DataChannelInterface::kOpen == dc->state(), | 229 EXPECT_TRUE_WAIT(webrtc::DataChannelInterface::kOpen == dc->state(), |
| 227 1000); | 230 1000); |
| 228 } | 231 } |
| 229 | 232 |
| 230 // Tests that an unordered DataChannel sends data as ordered until the OPEN_ACK | 233 // Tests that an unordered DataChannel sends data as ordered until the OPEN_ACK |
| 231 // message is received. | 234 // message is received. |
| 232 TEST_F(SctpDataChannelTest, SendUnorderedAfterReceivesOpenAck) { | 235 TEST_F(SctpDataChannelTest, SendUnorderedAfterReceivesOpenAck) { |
| 233 SetChannelReady(); | 236 SetChannelReady(); |
| 234 webrtc::InternalDataChannelInit init; | 237 webrtc::InternalDataChannelInit init; |
| 235 init.id = 1; | 238 init.id = 1; |
| 236 init.ordered = false; | 239 init.ordered = false; |
| 237 rtc::scoped_refptr<DataChannel> dc = DataChannel::Create( | 240 rtc::scoped_refptr<DataChannel> dc = |
| 238 &provider_, cricket::DCT_SCTP, "test1", init); | 241 DataChannel::Create(provider_.get(), cricket::DCT_SCTP, "test1", init); |
| 239 | 242 |
| 240 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000); | 243 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000); |
| 241 | 244 |
| 242 // Sends a message and verifies it's ordered. | 245 // Sends a message and verifies it's ordered. |
| 243 webrtc::DataBuffer buffer("some data"); | 246 webrtc::DataBuffer buffer("some data"); |
| 244 ASSERT_TRUE(dc->Send(buffer)); | 247 ASSERT_TRUE(dc->Send(buffer)); |
| 245 EXPECT_TRUE(provider_.last_send_data_params().ordered); | 248 EXPECT_TRUE(provider_->last_send_data_params().ordered); |
| 246 | 249 |
| 247 // Emulates receiving an OPEN_ACK message. | 250 // Emulates receiving an OPEN_ACK message. |
| 248 cricket::ReceiveDataParams params; | 251 cricket::ReceiveDataParams params; |
| 249 params.ssrc = init.id; | 252 params.ssrc = init.id; |
| 250 params.type = cricket::DMT_CONTROL; | 253 params.type = cricket::DMT_CONTROL; |
| 251 rtc::CopyOnWriteBuffer payload; | 254 rtc::CopyOnWriteBuffer payload; |
| 252 webrtc::WriteDataChannelOpenAckMessage(&payload); | 255 webrtc::WriteDataChannelOpenAckMessage(&payload); |
| 253 dc->OnDataReceived(NULL, params, payload); | 256 dc->OnDataReceived(NULL, params, payload); |
| 254 | 257 |
| 255 // Sends another message and verifies it's unordered. | 258 // Sends another message and verifies it's unordered. |
| 256 ASSERT_TRUE(dc->Send(buffer)); | 259 ASSERT_TRUE(dc->Send(buffer)); |
| 257 EXPECT_FALSE(provider_.last_send_data_params().ordered); | 260 EXPECT_FALSE(provider_->last_send_data_params().ordered); |
| 258 } | 261 } |
| 259 | 262 |
| 260 // Tests that an unordered DataChannel sends unordered data after any DATA | 263 // Tests that an unordered DataChannel sends unordered data after any DATA |
| 261 // message is received. | 264 // message is received. |
| 262 TEST_F(SctpDataChannelTest, SendUnorderedAfterReceiveData) { | 265 TEST_F(SctpDataChannelTest, SendUnorderedAfterReceiveData) { |
| 263 SetChannelReady(); | 266 SetChannelReady(); |
| 264 webrtc::InternalDataChannelInit init; | 267 webrtc::InternalDataChannelInit init; |
| 265 init.id = 1; | 268 init.id = 1; |
| 266 init.ordered = false; | 269 init.ordered = false; |
| 267 rtc::scoped_refptr<DataChannel> dc = DataChannel::Create( | 270 rtc::scoped_refptr<DataChannel> dc = |
| 268 &provider_, cricket::DCT_SCTP, "test1", init); | 271 DataChannel::Create(provider_.get(), cricket::DCT_SCTP, "test1", init); |
| 269 | 272 |
| 270 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000); | 273 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000); |
| 271 | 274 |
| 272 // Emulates receiving a DATA message. | 275 // Emulates receiving a DATA message. |
| 273 cricket::ReceiveDataParams params; | 276 cricket::ReceiveDataParams params; |
| 274 params.ssrc = init.id; | 277 params.ssrc = init.id; |
| 275 params.type = cricket::DMT_TEXT; | 278 params.type = cricket::DMT_TEXT; |
| 276 webrtc::DataBuffer buffer("data"); | 279 webrtc::DataBuffer buffer("data"); |
| 277 dc->OnDataReceived(NULL, params, buffer.data); | 280 dc->OnDataReceived(NULL, params, buffer.data); |
| 278 | 281 |
| 279 // Sends a message and verifies it's unordered. | 282 // Sends a message and verifies it's unordered. |
| 280 ASSERT_TRUE(dc->Send(buffer)); | 283 ASSERT_TRUE(dc->Send(buffer)); |
| 281 EXPECT_FALSE(provider_.last_send_data_params().ordered); | 284 EXPECT_FALSE(provider_->last_send_data_params().ordered); |
| 282 } | 285 } |
| 283 | 286 |
| 284 // Tests that the channel can't open until it's successfully sent the OPEN | 287 // Tests that the channel can't open until it's successfully sent the OPEN |
| 285 // message. | 288 // message. |
| 286 TEST_F(SctpDataChannelTest, OpenWaitsForOpenMesssage) { | 289 TEST_F(SctpDataChannelTest, OpenWaitsForOpenMesssage) { |
| 287 webrtc::DataBuffer buffer("foo"); | 290 webrtc::DataBuffer buffer("foo"); |
| 288 | 291 |
| 289 provider_.set_send_blocked(true); | 292 provider_->set_send_blocked(true); |
| 290 SetChannelReady(); | 293 SetChannelReady(); |
| 291 EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, | 294 EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, |
| 292 webrtc_data_channel_->state()); | 295 webrtc_data_channel_->state()); |
| 293 provider_.set_send_blocked(false); | 296 provider_->set_send_blocked(false); |
| 294 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, | 297 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, |
| 295 webrtc_data_channel_->state(), 1000); | 298 webrtc_data_channel_->state(), 1000); |
| 296 EXPECT_EQ(cricket::DMT_CONTROL, provider_.last_send_data_params().type); | 299 EXPECT_EQ(cricket::DMT_CONTROL, provider_->last_send_data_params().type); |
| 297 } | 300 } |
| 298 | 301 |
| 299 // Tests that close first makes sure all queued data gets sent. | 302 // Tests that close first makes sure all queued data gets sent. |
| 300 TEST_F(SctpDataChannelTest, QueuedCloseFlushes) { | 303 TEST_F(SctpDataChannelTest, QueuedCloseFlushes) { |
| 301 webrtc::DataBuffer buffer("foo"); | 304 webrtc::DataBuffer buffer("foo"); |
| 302 | 305 |
| 303 provider_.set_send_blocked(true); | 306 provider_->set_send_blocked(true); |
| 304 SetChannelReady(); | 307 SetChannelReady(); |
| 305 EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, | 308 EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, |
| 306 webrtc_data_channel_->state()); | 309 webrtc_data_channel_->state()); |
| 307 provider_.set_send_blocked(false); | 310 provider_->set_send_blocked(false); |
| 308 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, | 311 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, |
| 309 webrtc_data_channel_->state(), 1000); | 312 webrtc_data_channel_->state(), 1000); |
| 310 provider_.set_send_blocked(true); | 313 provider_->set_send_blocked(true); |
| 311 webrtc_data_channel_->Send(buffer); | 314 webrtc_data_channel_->Send(buffer); |
| 312 webrtc_data_channel_->Close(); | 315 webrtc_data_channel_->Close(); |
| 313 provider_.set_send_blocked(false); | 316 provider_->set_send_blocked(false); |
| 314 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kClosed, | 317 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kClosed, |
| 315 webrtc_data_channel_->state(), 1000); | 318 webrtc_data_channel_->state(), 1000); |
| 316 EXPECT_EQ(cricket::DMT_TEXT, provider_.last_send_data_params().type); | 319 EXPECT_EQ(cricket::DMT_TEXT, provider_->last_send_data_params().type); |
| 317 } | 320 } |
| 318 | 321 |
| 319 // Tests that messages are sent with the right ssrc. | 322 // Tests that messages are sent with the right ssrc. |
| 320 TEST_F(SctpDataChannelTest, SendDataSsrc) { | 323 TEST_F(SctpDataChannelTest, SendDataSsrc) { |
| 321 webrtc_data_channel_->SetSctpSid(1); | 324 webrtc_data_channel_->SetSctpSid(1); |
| 322 SetChannelReady(); | 325 SetChannelReady(); |
| 323 webrtc::DataBuffer buffer("data"); | 326 webrtc::DataBuffer buffer("data"); |
| 324 EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); | 327 EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
| 325 EXPECT_EQ(1U, provider_.last_send_data_params().ssrc); | 328 EXPECT_EQ(1U, provider_->last_send_data_params().ssrc); |
| 326 } | 329 } |
| 327 | 330 |
| 328 // Tests that the incoming messages with wrong ssrcs are rejected. | 331 // Tests that the incoming messages with wrong ssrcs are rejected. |
| 329 TEST_F(SctpDataChannelTest, ReceiveDataWithInvalidSsrc) { | 332 TEST_F(SctpDataChannelTest, ReceiveDataWithInvalidSsrc) { |
| 330 webrtc_data_channel_->SetSctpSid(1); | 333 webrtc_data_channel_->SetSctpSid(1); |
| 331 SetChannelReady(); | 334 SetChannelReady(); |
| 332 | 335 |
| 333 AddObserver(); | 336 AddObserver(); |
| 334 | 337 |
| 335 cricket::ReceiveDataParams params; | 338 cricket::ReceiveDataParams params; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 357 | 360 |
| 358 // Tests that no CONTROL message is sent if the datachannel is negotiated and | 361 // Tests that no CONTROL message is sent if the datachannel is negotiated and |
| 359 // not created from an OPEN message. | 362 // not created from an OPEN message. |
| 360 TEST_F(SctpDataChannelTest, NoMsgSentIfNegotiatedAndNotFromOpenMsg) { | 363 TEST_F(SctpDataChannelTest, NoMsgSentIfNegotiatedAndNotFromOpenMsg) { |
| 361 webrtc::InternalDataChannelInit config; | 364 webrtc::InternalDataChannelInit config; |
| 362 config.id = 1; | 365 config.id = 1; |
| 363 config.negotiated = true; | 366 config.negotiated = true; |
| 364 config.open_handshake_role = webrtc::InternalDataChannelInit::kNone; | 367 config.open_handshake_role = webrtc::InternalDataChannelInit::kNone; |
| 365 | 368 |
| 366 SetChannelReady(); | 369 SetChannelReady(); |
| 367 rtc::scoped_refptr<DataChannel> dc = DataChannel::Create( | 370 rtc::scoped_refptr<DataChannel> dc = |
| 368 &provider_, cricket::DCT_SCTP, "test1", config); | 371 DataChannel::Create(provider_.get(), cricket::DCT_SCTP, "test1", config); |
| 369 | 372 |
| 370 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000); | 373 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000); |
| 371 EXPECT_EQ(0U, provider_.last_send_data_params().ssrc); | 374 EXPECT_EQ(0U, provider_->last_send_data_params().ssrc); |
| 372 } | 375 } |
| 373 | 376 |
| 374 // Tests that OPEN_ACK message is sent if the datachannel is created from an | 377 // Tests that OPEN_ACK message is sent if the datachannel is created from an |
| 375 // OPEN message. | 378 // OPEN message. |
| 376 TEST_F(SctpDataChannelTest, OpenAckSentIfCreatedFromOpenMessage) { | 379 TEST_F(SctpDataChannelTest, OpenAckSentIfCreatedFromOpenMessage) { |
| 377 webrtc::InternalDataChannelInit config; | 380 webrtc::InternalDataChannelInit config; |
| 378 config.id = 1; | 381 config.id = 1; |
| 379 config.negotiated = true; | 382 config.negotiated = true; |
| 380 config.open_handshake_role = webrtc::InternalDataChannelInit::kAcker; | 383 config.open_handshake_role = webrtc::InternalDataChannelInit::kAcker; |
| 381 | 384 |
| 382 SetChannelReady(); | 385 SetChannelReady(); |
| 383 rtc::scoped_refptr<DataChannel> dc = DataChannel::Create( | 386 rtc::scoped_refptr<DataChannel> dc = |
| 384 &provider_, cricket::DCT_SCTP, "test1", config); | 387 DataChannel::Create(provider_.get(), cricket::DCT_SCTP, "test1", config); |
| 385 | 388 |
| 386 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000); | 389 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000); |
| 387 | 390 |
| 388 EXPECT_EQ(static_cast<unsigned int>(config.id), | 391 EXPECT_EQ(static_cast<unsigned int>(config.id), |
| 389 provider_.last_send_data_params().ssrc); | 392 provider_->last_send_data_params().ssrc); |
| 390 EXPECT_EQ(cricket::DMT_CONTROL, provider_.last_send_data_params().type); | 393 EXPECT_EQ(cricket::DMT_CONTROL, provider_->last_send_data_params().type); |
| 391 } | 394 } |
| 392 | 395 |
| 393 // Tests the OPEN_ACK role assigned by InternalDataChannelInit. | 396 // Tests the OPEN_ACK role assigned by InternalDataChannelInit. |
| 394 TEST_F(SctpDataChannelTest, OpenAckRoleInitialization) { | 397 TEST_F(SctpDataChannelTest, OpenAckRoleInitialization) { |
| 395 webrtc::InternalDataChannelInit init; | 398 webrtc::InternalDataChannelInit init; |
| 396 EXPECT_EQ(webrtc::InternalDataChannelInit::kOpener, init.open_handshake_role); | 399 EXPECT_EQ(webrtc::InternalDataChannelInit::kOpener, init.open_handshake_role); |
| 397 EXPECT_FALSE(init.negotiated); | 400 EXPECT_FALSE(init.negotiated); |
| 398 | 401 |
| 399 webrtc::DataChannelInit base; | 402 webrtc::DataChannelInit base; |
| 400 base.negotiated = true; | 403 base.negotiated = true; |
| 401 webrtc::InternalDataChannelInit init2(base); | 404 webrtc::InternalDataChannelInit init2(base); |
| 402 EXPECT_EQ(webrtc::InternalDataChannelInit::kNone, init2.open_handshake_role); | 405 EXPECT_EQ(webrtc::InternalDataChannelInit::kNone, init2.open_handshake_role); |
| 403 } | 406 } |
| 404 | 407 |
| 405 // Tests that the DataChannel is closed if the sending buffer is full. | 408 // Tests that the DataChannel is closed if the sending buffer is full. |
| 406 TEST_F(SctpDataChannelTest, ClosedWhenSendBufferFull) { | 409 TEST_F(SctpDataChannelTest, ClosedWhenSendBufferFull) { |
| 407 SetChannelReady(); | 410 SetChannelReady(); |
| 408 | 411 |
| 409 rtc::CopyOnWriteBuffer buffer(1024); | 412 rtc::CopyOnWriteBuffer buffer(1024); |
| 410 memset(buffer.data(), 0, buffer.size()); | 413 memset(buffer.data(), 0, buffer.size()); |
| 411 | 414 |
| 412 webrtc::DataBuffer packet(buffer, true); | 415 webrtc::DataBuffer packet(buffer, true); |
| 413 provider_.set_send_blocked(true); | 416 provider_->set_send_blocked(true); |
| 414 | 417 |
| 415 for (size_t i = 0; i < 16 * 1024 + 1; ++i) { | 418 for (size_t i = 0; i < 16 * 1024 + 1; ++i) { |
| 416 EXPECT_TRUE(webrtc_data_channel_->Send(packet)); | 419 EXPECT_TRUE(webrtc_data_channel_->Send(packet)); |
| 417 } | 420 } |
| 418 | 421 |
| 419 EXPECT_TRUE( | 422 EXPECT_TRUE( |
| 420 webrtc::DataChannelInterface::kClosed == webrtc_data_channel_->state() || | 423 webrtc::DataChannelInterface::kClosed == webrtc_data_channel_->state() || |
| 421 webrtc::DataChannelInterface::kClosing == webrtc_data_channel_->state()); | 424 webrtc::DataChannelInterface::kClosing == webrtc_data_channel_->state()); |
| 422 } | 425 } |
| 423 | 426 |
| 424 // Tests that the DataChannel is closed on transport errors. | 427 // Tests that the DataChannel is closed on transport errors. |
| 425 TEST_F(SctpDataChannelTest, ClosedOnTransportError) { | 428 TEST_F(SctpDataChannelTest, ClosedOnTransportError) { |
| 426 SetChannelReady(); | 429 SetChannelReady(); |
| 427 webrtc::DataBuffer buffer("abcd"); | 430 webrtc::DataBuffer buffer("abcd"); |
| 428 provider_.set_transport_error(); | 431 provider_->set_transport_error(); |
| 429 | 432 |
| 430 EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); | 433 EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
| 431 | 434 |
| 432 EXPECT_EQ(webrtc::DataChannelInterface::kClosed, | 435 EXPECT_EQ(webrtc::DataChannelInterface::kClosed, |
| 433 webrtc_data_channel_->state()); | 436 webrtc_data_channel_->state()); |
| 434 } | 437 } |
| 435 | 438 |
| 436 // Tests that a already closed DataChannel does not fire onStateChange again. | 439 // Tests that a already closed DataChannel does not fire onStateChange again. |
| 437 TEST_F(SctpDataChannelTest, ClosedDataChannelDoesNotFireOnStateChange) { | 440 TEST_F(SctpDataChannelTest, ClosedDataChannelDoesNotFireOnStateChange) { |
| 438 AddObserver(); | 441 AddObserver(); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 481 webrtc_data_channel_->state()); | 484 webrtc_data_channel_->state()); |
| 482 | 485 |
| 483 webrtc::DataBuffer buffer(""); | 486 webrtc::DataBuffer buffer(""); |
| 484 EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); | 487 EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
| 485 EXPECT_EQ(webrtc::DataChannelInterface::kOpen, | 488 EXPECT_EQ(webrtc::DataChannelInterface::kOpen, |
| 486 webrtc_data_channel_->state()); | 489 webrtc_data_channel_->state()); |
| 487 } | 490 } |
| 488 | 491 |
| 489 // Tests that a channel can be closed without being opened or assigned an sid. | 492 // Tests that a channel can be closed without being opened or assigned an sid. |
| 490 TEST_F(SctpDataChannelTest, NeverOpened) { | 493 TEST_F(SctpDataChannelTest, NeverOpened) { |
| 491 provider_.set_transport_available(true); | 494 provider_->set_transport_available(true); |
| 492 webrtc_data_channel_->OnTransportChannelCreated(); | 495 webrtc_data_channel_->OnTransportChannelCreated(); |
| 493 webrtc_data_channel_->Close(); | 496 webrtc_data_channel_->Close(); |
| 494 } | 497 } |
| 495 | 498 |
| 499 // Test that the data channel goes to the "closed" state (and doesn't crash) |
| 500 // when its transport goes away, even while data is buffered. |
| 501 TEST_F(SctpDataChannelTest, TransportDestroyedWhileDataBuffered) { |
| 502 SetChannelReady(); |
| 503 |
| 504 rtc::CopyOnWriteBuffer buffer(1024); |
| 505 memset(buffer.data(), 0, buffer.size()); |
| 506 webrtc::DataBuffer packet(buffer, true); |
| 507 |
| 508 // Send a packet while sending is blocked so it ends up buffered. |
| 509 provider_->set_send_blocked(true); |
| 510 EXPECT_TRUE(webrtc_data_channel_->Send(packet)); |
| 511 |
| 512 // Tell the data channel that its tranpsort is being destroyed. |
| 513 // It should then stop using the transport (allowing us to delete it) and |
| 514 // transition to the "closed" state. |
| 515 webrtc_data_channel_->OnTransportChannelDestroyed(); |
| 516 provider_.reset(nullptr); |
| 517 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kClosed, |
| 518 webrtc_data_channel_->state(), kDefaultTimeout); |
| 519 } |
| 520 |
| 496 class SctpSidAllocatorTest : public testing::Test { | 521 class SctpSidAllocatorTest : public testing::Test { |
| 497 protected: | 522 protected: |
| 498 SctpSidAllocator allocator_; | 523 SctpSidAllocator allocator_; |
| 499 }; | 524 }; |
| 500 | 525 |
| 501 // Verifies that an even SCTP id is allocated for SSL_CLIENT and an odd id for | 526 // Verifies that an even SCTP id is allocated for SSL_CLIENT and an odd id for |
| 502 // SSL_SERVER. | 527 // SSL_SERVER. |
| 503 TEST_F(SctpSidAllocatorTest, SctpIdAllocationBasedOnRole) { | 528 TEST_F(SctpSidAllocatorTest, SctpIdAllocationBasedOnRole) { |
| 504 int id; | 529 int id; |
| 505 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &id)); | 530 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &id)); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 557 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &allocated_id)); | 582 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &allocated_id)); |
| 558 EXPECT_EQ(even_id, allocated_id); | 583 EXPECT_EQ(even_id, allocated_id); |
| 559 | 584 |
| 560 // Verifies that used higher ids are not reused. | 585 // Verifies that used higher ids are not reused. |
| 561 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &allocated_id)); | 586 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &allocated_id)); |
| 562 EXPECT_EQ(odd_id + 6, allocated_id); | 587 EXPECT_EQ(odd_id + 6, allocated_id); |
| 563 | 588 |
| 564 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &allocated_id)); | 589 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &allocated_id)); |
| 565 EXPECT_EQ(even_id + 6, allocated_id); | 590 EXPECT_EQ(even_id + 6, allocated_id); |
| 566 } | 591 } |
| OLD | NEW |