Chromium Code Reviews

Side by Side Diff: webrtc/p2p/base/port_unittest.cc

Issue 2685783014: Replace NULL with nullptr in all C++ files. (Closed)
Patch Set: Fixing android. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
OLDNEW
1 /* 1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2004 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
(...skipping 211 matching lines...)
222 int type_preference_ = 0; 222 int type_preference_ = 0;
223 }; 223 };
224 224
225 class TestChannel : public sigslot::has_slots<> { 225 class TestChannel : public sigslot::has_slots<> {
226 public: 226 public:
227 // Takes ownership of |p1| (but not |p2|). 227 // Takes ownership of |p1| (but not |p2|).
228 TestChannel(Port* p1) 228 TestChannel(Port* p1)
229 : ice_mode_(ICEMODE_FULL), 229 : ice_mode_(ICEMODE_FULL),
230 port_(p1), 230 port_(p1),
231 complete_count_(0), 231 complete_count_(0),
232 conn_(NULL), 232 conn_(nullptr),
233 remote_request_(), 233 remote_request_(),
234 nominated_(false) { 234 nominated_(false) {
235 port_->SignalPortComplete.connect(this, &TestChannel::OnPortComplete); 235 port_->SignalPortComplete.connect(this, &TestChannel::OnPortComplete);
236 port_->SignalUnknownAddress.connect(this, &TestChannel::OnUnknownAddress); 236 port_->SignalUnknownAddress.connect(this, &TestChannel::OnUnknownAddress);
237 port_->SignalDestroyed.connect(this, &TestChannel::OnSrcPortDestroyed); 237 port_->SignalDestroyed.connect(this, &TestChannel::OnSrcPortDestroyed);
238 } 238 }
239 239
240 int complete_count() { return complete_count_; } 240 int complete_count() { return complete_count_; }
241 Connection* conn() { return conn_; } 241 Connection* conn() { return conn_; }
242 const SocketAddress& remote_address() { return remote_address_; } 242 const SocketAddress& remote_address() { return remote_address_; }
(...skipping 13 matching lines...)
256 &TestChannel::OnConnectionReadyToSend); 256 &TestChannel::OnConnectionReadyToSend);
257 connection_ready_to_send_ = false; 257 connection_ready_to_send_ = false;
258 } 258 }
259 void OnConnectionStateChange(Connection* conn) { 259 void OnConnectionStateChange(Connection* conn) {
260 if (conn->write_state() == Connection::STATE_WRITABLE) { 260 if (conn->write_state() == Connection::STATE_WRITABLE) {
261 conn->set_use_candidate_attr(true); 261 conn->set_use_candidate_attr(true);
262 nominated_ = true; 262 nominated_ = true;
263 } 263 }
264 } 264 }
265 void AcceptConnection(const Candidate& remote_candidate) { 265 void AcceptConnection(const Candidate& remote_candidate) {
266 ASSERT_TRUE(remote_request_.get() != NULL); 266 ASSERT_TRUE(remote_request_.get() != nullptr);
267 Candidate c = remote_candidate; 267 Candidate c = remote_candidate;
268 c.set_address(remote_address_); 268 c.set_address(remote_address_);
269 conn_ = port_->CreateConnection(c, Port::ORIGIN_MESSAGE); 269 conn_ = port_->CreateConnection(c, Port::ORIGIN_MESSAGE);
270 conn_->SignalDestroyed.connect(this, &TestChannel::OnDestroyed); 270 conn_->SignalDestroyed.connect(this, &TestChannel::OnDestroyed);
271 port_->SendBindingResponse(remote_request_.get(), remote_address_); 271 port_->SendBindingResponse(remote_request_.get(), remote_address_);
272 remote_request_.reset(); 272 remote_request_.reset();
273 } 273 }
274 void Ping() { 274 void Ping() {
275 Ping(0); 275 Ping(0);
276 } 276 }
(...skipping 23 matching lines...)
300 ASSERT_EQ(port_.get(), port); 300 ASSERT_EQ(port_.get(), port);
301 if (!remote_address_.IsNil()) { 301 if (!remote_address_.IsNil()) {
302 ASSERT_EQ(remote_address_, addr); 302 ASSERT_EQ(remote_address_, addr);
303 } 303 }
304 const cricket::StunUInt32Attribute* priority_attr = 304 const cricket::StunUInt32Attribute* priority_attr =
305 msg->GetUInt32(STUN_ATTR_PRIORITY); 305 msg->GetUInt32(STUN_ATTR_PRIORITY);
306 const cricket::StunByteStringAttribute* mi_attr = 306 const cricket::StunByteStringAttribute* mi_attr =
307 msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY); 307 msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY);
308 const cricket::StunUInt32Attribute* fingerprint_attr = 308 const cricket::StunUInt32Attribute* fingerprint_attr =
309 msg->GetUInt32(STUN_ATTR_FINGERPRINT); 309 msg->GetUInt32(STUN_ATTR_FINGERPRINT);
310 EXPECT_TRUE(priority_attr != NULL); 310 EXPECT_TRUE(priority_attr != nullptr);
311 EXPECT_TRUE(mi_attr != NULL); 311 EXPECT_TRUE(mi_attr != nullptr);
312 EXPECT_TRUE(fingerprint_attr != NULL); 312 EXPECT_TRUE(fingerprint_attr != nullptr);
313 remote_address_ = addr; 313 remote_address_ = addr;
314 remote_request_.reset(CopyStunMessage(msg)); 314 remote_request_.reset(CopyStunMessage(msg));
315 remote_frag_ = rf; 315 remote_frag_ = rf;
316 } 316 }
317 317
318 void OnDestroyed(Connection* conn) { 318 void OnDestroyed(Connection* conn) {
319 ASSERT_EQ(conn_, conn); 319 ASSERT_EQ(conn_, conn);
320 LOG(INFO) << "OnDestroy connection " << conn << " deleted"; 320 LOG(INFO) << "OnDestroy connection " << conn << " deleted";
321 conn_ = NULL; 321 conn_ = nullptr;
322 // When the connection is destroyed, also clear these fields so future 322 // When the connection is destroyed, also clear these fields so future
323 // connections are possible. 323 // connections are possible.
324 remote_request_.reset(); 324 remote_request_.reset();
325 remote_address_.Clear(); 325 remote_address_.Clear();
326 } 326 }
327 327
328 void OnSrcPortDestroyed(PortInterface* port) { 328 void OnSrcPortDestroyed(PortInterface* port) {
329 Port* destroyed_src = port_.release(); 329 Port* destroyed_src = port_.release();
330 ASSERT_EQ(destroyed_src, port); 330 ASSERT_EQ(destroyed_src, port);
331 } 331 }
(...skipping 315 matching lines...)
647 647
648 // Ensure redundant SignalClose events on TcpConnection won't break tcp 648 // Ensure redundant SignalClose events on TcpConnection won't break tcp
649 // reconnection. Chromium will fire SignalClose for all outstanding IPC 649 // reconnection. Chromium will fire SignalClose for all outstanding IPC
650 // packets during reconnection. 650 // packets during reconnection.
651 tcp_conn1->socket()->SignalClose(tcp_conn1->socket(), 0); 651 tcp_conn1->socket()->SignalClose(tcp_conn1->socket(), 0);
652 tcp_conn2->socket()->SignalClose(tcp_conn2->socket(), 0); 652 tcp_conn2->socket()->SignalClose(tcp_conn2->socket(), 0);
653 653
654 // Speed up destroying ch2's connection such that the test is ready to 654 // Speed up destroying ch2's connection such that the test is ready to
655 // accept a new connection from ch1 before ch1's connection destroys itself. 655 // accept a new connection from ch1 before ch1's connection destroys itself.
656 ch2->conn()->Destroy(); 656 ch2->conn()->Destroy();
657 EXPECT_TRUE_WAIT(ch2->conn() == NULL, kDefaultTimeout); 657 EXPECT_TRUE_WAIT(ch2->conn() == nullptr, kDefaultTimeout);
658 } 658 }
659 659
660 void TestTcpReconnect(bool ping_after_disconnected, 660 void TestTcpReconnect(bool ping_after_disconnected,
661 bool send_after_disconnected) { 661 bool send_after_disconnected) {
662 Port* port1 = CreateTcpPort(kLocalAddr1); 662 Port* port1 = CreateTcpPort(kLocalAddr1);
663 port1->SetIceRole(cricket::ICEROLE_CONTROLLING); 663 port1->SetIceRole(cricket::ICEROLE_CONTROLLING);
664 Port* port2 = CreateTcpPort(kLocalAddr2); 664 Port* port2 = CreateTcpPort(kLocalAddr2);
665 port2->SetIceRole(cricket::ICEROLE_CONTROLLED); 665 port2->SetIceRole(cricket::ICEROLE_CONTROLLED);
666 666
667 port1->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); 667 port1->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT);
(...skipping 54 matching lines...)
722 EXPECT_EQ(ch1.conn()->write_state(), Connection::STATE_WRITABLE); 722 EXPECT_EQ(ch1.conn()->write_state(), Connection::STATE_WRITABLE);
723 // Since the reconnection never happens, the connections should have been 723 // Since the reconnection never happens, the connections should have been
724 // destroyed after the timeout. 724 // destroyed after the timeout.
725 EXPECT_TRUE_WAIT(!ch1.conn(), kTcpReconnectTimeout + kDefaultTimeout); 725 EXPECT_TRUE_WAIT(!ch1.conn(), kTcpReconnectTimeout + kDefaultTimeout);
726 EXPECT_TRUE(!ch2.conn()); 726 EXPECT_TRUE(!ch2.conn());
727 } 727 }
728 728
729 // Tear down and ensure that goes smoothly. 729 // Tear down and ensure that goes smoothly.
730 ch1.Stop(); 730 ch1.Stop();
731 ch2.Stop(); 731 ch2.Stop();
732 EXPECT_TRUE_WAIT(ch1.conn() == NULL, kDefaultTimeout); 732 EXPECT_TRUE_WAIT(ch1.conn() == nullptr, kDefaultTimeout);
733 EXPECT_TRUE_WAIT(ch2.conn() == NULL, kDefaultTimeout); 733 EXPECT_TRUE_WAIT(ch2.conn() == nullptr, kDefaultTimeout);
734 } 734 }
735 735
736 IceMessage* CreateStunMessage(int type) { 736 IceMessage* CreateStunMessage(int type) {
737 IceMessage* msg = new IceMessage(); 737 IceMessage* msg = new IceMessage();
738 msg->SetType(type); 738 msg->SetType(type);
739 msg->SetTransactionID("TESTTESTTEST"); 739 msg->SetTransactionID("TESTTESTTEST");
740 return msg; 740 return msg;
741 } 741 }
742 IceMessage* CreateStunMessageWithUsername(int type, 742 IceMessage* CreateStunMessageWithUsername(int type,
743 const std::string& username) { 743 const std::string& username) {
(...skipping 77 matching lines...)
821 EXPECT_EQ(0, ch2.complete_count()); 821 EXPECT_EQ(0, ch2.complete_count());
822 822
823 // Acquire addresses. 823 // Acquire addresses.
824 ch1.Start(); 824 ch1.Start();
825 ch2.Start(); 825 ch2.Start();
826 ASSERT_EQ_SIMULATED_WAIT(1, ch1.complete_count(), kDefaultTimeout, clock); 826 ASSERT_EQ_SIMULATED_WAIT(1, ch1.complete_count(), kDefaultTimeout, clock);
827 ASSERT_EQ_SIMULATED_WAIT(1, ch2.complete_count(), kDefaultTimeout, clock); 827 ASSERT_EQ_SIMULATED_WAIT(1, ch2.complete_count(), kDefaultTimeout, clock);
828 828
829 // Send a ping from src to dst. This may or may not make it. 829 // Send a ping from src to dst. This may or may not make it.
830 ch1.CreateConnection(GetCandidate(port2)); 830 ch1.CreateConnection(GetCandidate(port2));
831 ASSERT_TRUE(ch1.conn() != NULL); 831 ASSERT_TRUE(ch1.conn() != nullptr);
832 EXPECT_TRUE_SIMULATED_WAIT(ch1.conn()->connected(), kDefaultTimeout, 832 EXPECT_TRUE_SIMULATED_WAIT(ch1.conn()->connected(), kDefaultTimeout,
833 clock); // for TCP connect 833 clock); // for TCP connect
834 ch1.Ping(); 834 ch1.Ping();
835 SIMULATED_WAIT(!ch2.remote_address().IsNil(), kShortTimeout, clock); 835 SIMULATED_WAIT(!ch2.remote_address().IsNil(), kShortTimeout, clock);
836 836
837 if (accept) { 837 if (accept) {
838 // We are able to send a ping from src to dst. This is the case when 838 // We are able to send a ping from src to dst. This is the case when
839 // sending to UDP ports and cone NATs. 839 // sending to UDP ports and cone NATs.
840 EXPECT_TRUE(ch1.remote_address().IsNil()); 840 EXPECT_TRUE(ch1.remote_address().IsNil());
841 EXPECT_EQ(ch2.remote_fragment(), port1->username_fragment()); 841 EXPECT_EQ(ch2.remote_fragment(), port1->username_fragment());
842 842
843 // Ensure the ping came from the same address used for src. 843 // Ensure the ping came from the same address used for src.
844 // This is the case unless the source NAT was symmetric. 844 // This is the case unless the source NAT was symmetric.
845 if (same_addr1) EXPECT_EQ(ch2.remote_address(), GetAddress(port1)); 845 if (same_addr1) EXPECT_EQ(ch2.remote_address(), GetAddress(port1));
846 EXPECT_TRUE(same_addr2); 846 EXPECT_TRUE(same_addr2);
847 847
848 // Send a ping from dst to src. 848 // Send a ping from dst to src.
849 ch2.AcceptConnection(GetCandidate(port1)); 849 ch2.AcceptConnection(GetCandidate(port1));
850 ASSERT_TRUE(ch2.conn() != NULL); 850 ASSERT_TRUE(ch2.conn() != nullptr);
851 ch2.Ping(); 851 ch2.Ping();
852 EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE, 852 EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE,
853 ch2.conn()->write_state(), kDefaultTimeout, clock); 853 ch2.conn()->write_state(), kDefaultTimeout, clock);
854 } else { 854 } else {
855 // We can't send a ping from src to dst, so flip it around. This will happen 855 // We can't send a ping from src to dst, so flip it around. This will happen
856 // when the destination NAT is addr/port restricted or symmetric. 856 // when the destination NAT is addr/port restricted or symmetric.
857 EXPECT_TRUE(ch1.remote_address().IsNil()); 857 EXPECT_TRUE(ch1.remote_address().IsNil());
858 EXPECT_TRUE(ch2.remote_address().IsNil()); 858 EXPECT_TRUE(ch2.remote_address().IsNil());
859 859
860 // Send a ping from dst to src. Again, this may or may not make it. 860 // Send a ping from dst to src. Again, this may or may not make it.
861 ch2.CreateConnection(GetCandidate(port1)); 861 ch2.CreateConnection(GetCandidate(port1));
862 ASSERT_TRUE(ch2.conn() != NULL); 862 ASSERT_TRUE(ch2.conn() != nullptr);
863 ch2.Ping(); 863 ch2.Ping();
864 SIMULATED_WAIT(ch2.conn()->write_state() == Connection::STATE_WRITABLE, 864 SIMULATED_WAIT(ch2.conn()->write_state() == Connection::STATE_WRITABLE,
865 kShortTimeout, clock); 865 kShortTimeout, clock);
866 866
867 if (same_addr1 && same_addr2) { 867 if (same_addr1 && same_addr2) {
868 // The new ping got back to the source. 868 // The new ping got back to the source.
869 EXPECT_TRUE(ch1.conn()->receiving()); 869 EXPECT_TRUE(ch1.conn()->receiving());
870 EXPECT_EQ(Connection::STATE_WRITABLE, ch2.conn()->write_state()); 870 EXPECT_EQ(Connection::STATE_WRITABLE, ch2.conn()->write_state());
871 871
872 // First connection may not be writable if the first ping did not get 872 // First connection may not be writable if the first ping did not get
(...skipping 13 matching lines...)
886 // However, since we have now sent a ping to the source IP, we should be 886 // However, since we have now sent a ping to the source IP, we should be
887 // able to get a ping from it. This gives us the real source address. 887 // able to get a ping from it. This gives us the real source address.
888 ch1.Ping(); 888 ch1.Ping();
889 EXPECT_TRUE_SIMULATED_WAIT(!ch2.remote_address().IsNil(), kDefaultTimeout, 889 EXPECT_TRUE_SIMULATED_WAIT(!ch2.remote_address().IsNil(), kDefaultTimeout,
890 clock); 890 clock);
891 EXPECT_FALSE(ch2.conn()->receiving()); 891 EXPECT_FALSE(ch2.conn()->receiving());
892 EXPECT_TRUE(ch1.remote_address().IsNil()); 892 EXPECT_TRUE(ch1.remote_address().IsNil());
893 893
894 // Pick up the actual address and establish the connection. 894 // Pick up the actual address and establish the connection.
895 ch2.AcceptConnection(GetCandidate(port1)); 895 ch2.AcceptConnection(GetCandidate(port1));
896 ASSERT_TRUE(ch2.conn() != NULL); 896 ASSERT_TRUE(ch2.conn() != nullptr);
897 ch2.Ping(); 897 ch2.Ping();
898 EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE, 898 EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE,
899 ch2.conn()->write_state(), kDefaultTimeout, 899 ch2.conn()->write_state(), kDefaultTimeout,
900 clock); 900 clock);
901 } else if (!same_addr2 && possible) { 901 } else if (!same_addr2 && possible) {
902 // The new ping came in, but from an unexpected address. This will happen 902 // The new ping came in, but from an unexpected address. This will happen
903 // when the destination NAT is symmetric. 903 // when the destination NAT is symmetric.
904 EXPECT_FALSE(ch1.remote_address().IsNil()); 904 EXPECT_FALSE(ch1.remote_address().IsNil());
905 EXPECT_FALSE(ch1.conn()->receiving()); 905 EXPECT_FALSE(ch1.conn()->receiving());
906 906
907 // Update our address and complete the connection. 907 // Update our address and complete the connection.
908 ch1.AcceptConnection(GetCandidate(port2)); 908 ch1.AcceptConnection(GetCandidate(port2));
909 ch1.Ping(); 909 ch1.Ping();
910 EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE, 910 EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE,
911 ch1.conn()->write_state(), kDefaultTimeout, 911 ch1.conn()->write_state(), kDefaultTimeout,
912 clock); 912 clock);
913 } else { // (!possible) 913 } else { // (!possible)
914 // There should be s no way for the pings to reach each other. Check it. 914 // There should be s no way for the pings to reach each other. Check it.
915 EXPECT_TRUE(ch1.remote_address().IsNil()); 915 EXPECT_TRUE(ch1.remote_address().IsNil());
916 EXPECT_TRUE(ch2.remote_address().IsNil()); 916 EXPECT_TRUE(ch2.remote_address().IsNil());
917 ch1.Ping(); 917 ch1.Ping();
918 SIMULATED_WAIT(!ch2.remote_address().IsNil(), kShortTimeout, clock); 918 SIMULATED_WAIT(!ch2.remote_address().IsNil(), kShortTimeout, clock);
919 EXPECT_TRUE(ch1.remote_address().IsNil()); 919 EXPECT_TRUE(ch1.remote_address().IsNil());
920 EXPECT_TRUE(ch2.remote_address().IsNil()); 920 EXPECT_TRUE(ch2.remote_address().IsNil());
921 } 921 }
922 } 922 }
923 923
924 // Everything should be good, unless we know the situation is impossible. 924 // Everything should be good, unless we know the situation is impossible.
925 ASSERT_TRUE(ch1.conn() != NULL); 925 ASSERT_TRUE(ch1.conn() != nullptr);
926 ASSERT_TRUE(ch2.conn() != NULL); 926 ASSERT_TRUE(ch2.conn() != nullptr);
927 if (possible) { 927 if (possible) {
928 EXPECT_TRUE(ch1.conn()->receiving()); 928 EXPECT_TRUE(ch1.conn()->receiving());
929 EXPECT_EQ(Connection::STATE_WRITABLE, ch1.conn()->write_state()); 929 EXPECT_EQ(Connection::STATE_WRITABLE, ch1.conn()->write_state());
930 EXPECT_TRUE(ch2.conn()->receiving()); 930 EXPECT_TRUE(ch2.conn()->receiving());
931 EXPECT_EQ(Connection::STATE_WRITABLE, ch2.conn()->write_state()); 931 EXPECT_EQ(Connection::STATE_WRITABLE, ch2.conn()->write_state());
932 } else { 932 } else {
933 EXPECT_FALSE(ch1.conn()->receiving()); 933 EXPECT_FALSE(ch1.conn()->receiving());
934 EXPECT_NE(Connection::STATE_WRITABLE, ch1.conn()->write_state()); 934 EXPECT_NE(Connection::STATE_WRITABLE, ch1.conn()->write_state());
935 EXPECT_FALSE(ch2.conn()->receiving()); 935 EXPECT_FALSE(ch2.conn()->receiving());
936 EXPECT_NE(Connection::STATE_WRITABLE, ch2.conn()->write_state()); 936 EXPECT_NE(Connection::STATE_WRITABLE, ch2.conn()->write_state());
937 } 937 }
938 938
939 // Tear down and ensure that goes smoothly. 939 // Tear down and ensure that goes smoothly.
940 ch1.Stop(); 940 ch1.Stop();
941 ch2.Stop(); 941 ch2.Stop();
942 EXPECT_TRUE_SIMULATED_WAIT(ch1.conn() == NULL, kDefaultTimeout, clock); 942 EXPECT_TRUE_SIMULATED_WAIT(ch1.conn() == nullptr, kDefaultTimeout, clock);
943 EXPECT_TRUE_SIMULATED_WAIT(ch2.conn() == NULL, kDefaultTimeout, clock); 943 EXPECT_TRUE_SIMULATED_WAIT(ch2.conn() == nullptr, kDefaultTimeout, clock);
944 } 944 }
945 945
946 class FakePacketSocketFactory : public rtc::PacketSocketFactory { 946 class FakePacketSocketFactory : public rtc::PacketSocketFactory {
947 public: 947 public:
948 FakePacketSocketFactory() 948 FakePacketSocketFactory()
949 : next_udp_socket_(NULL), 949 : next_udp_socket_(nullptr),
950 next_server_tcp_socket_(NULL), 950 next_server_tcp_socket_(nullptr),
951 next_client_tcp_socket_(NULL) { 951 next_client_tcp_socket_(nullptr) {}
952 }
953 ~FakePacketSocketFactory() override { } 952 ~FakePacketSocketFactory() override { }
954 953
955 AsyncPacketSocket* CreateUdpSocket(const SocketAddress& address, 954 AsyncPacketSocket* CreateUdpSocket(const SocketAddress& address,
956 uint16_t min_port, 955 uint16_t min_port,
957 uint16_t max_port) override { 956 uint16_t max_port) override {
958 EXPECT_TRUE(next_udp_socket_ != NULL); 957 EXPECT_TRUE(next_udp_socket_ != nullptr);
959 AsyncPacketSocket* result = next_udp_socket_; 958 AsyncPacketSocket* result = next_udp_socket_;
960 next_udp_socket_ = NULL; 959 next_udp_socket_ = nullptr;
961 return result; 960 return result;
962 } 961 }
963 962
964 AsyncPacketSocket* CreateServerTcpSocket(const SocketAddress& local_address, 963 AsyncPacketSocket* CreateServerTcpSocket(const SocketAddress& local_address,
965 uint16_t min_port, 964 uint16_t min_port,
966 uint16_t max_port, 965 uint16_t max_port,
967 int opts) override { 966 int opts) override {
968 EXPECT_TRUE(next_server_tcp_socket_ != NULL); 967 EXPECT_TRUE(next_server_tcp_socket_ != nullptr);
969 AsyncPacketSocket* result = next_server_tcp_socket_; 968 AsyncPacketSocket* result = next_server_tcp_socket_;
970 next_server_tcp_socket_ = NULL; 969 next_server_tcp_socket_ = nullptr;
971 return result; 970 return result;
972 } 971 }
973 972
974 // TODO: |proxy_info| and |user_agent| should be set 973 // TODO: |proxy_info| and |user_agent| should be set
975 // per-factory and not when socket is created. 974 // per-factory and not when socket is created.
976 AsyncPacketSocket* CreateClientTcpSocket(const SocketAddress& local_address, 975 AsyncPacketSocket* CreateClientTcpSocket(const SocketAddress& local_address,
977 const SocketAddress& remote_address, 976 const SocketAddress& remote_address,
978 const rtc::ProxyInfo& proxy_info, 977 const rtc::ProxyInfo& proxy_info,
979 const std::string& user_agent, 978 const std::string& user_agent,
980 int opts) override { 979 int opts) override {
981 EXPECT_TRUE(next_client_tcp_socket_ != NULL); 980 EXPECT_TRUE(next_client_tcp_socket_ != nullptr);
982 AsyncPacketSocket* result = next_client_tcp_socket_; 981 AsyncPacketSocket* result = next_client_tcp_socket_;
983 next_client_tcp_socket_ = NULL; 982 next_client_tcp_socket_ = nullptr;
984 return result; 983 return result;
985 } 984 }
986 985
987 void set_next_udp_socket(AsyncPacketSocket* next_udp_socket) { 986 void set_next_udp_socket(AsyncPacketSocket* next_udp_socket) {
988 next_udp_socket_ = next_udp_socket; 987 next_udp_socket_ = next_udp_socket;
989 } 988 }
990 void set_next_server_tcp_socket(AsyncPacketSocket* next_server_tcp_socket) { 989 void set_next_server_tcp_socket(AsyncPacketSocket* next_server_tcp_socket) {
991 next_server_tcp_socket_ = next_server_tcp_socket; 990 next_server_tcp_socket_ = next_server_tcp_socket;
992 } 991 }
993 void set_next_client_tcp_socket(AsyncPacketSocket* next_client_tcp_socket) { 992 void set_next_client_tcp_socket(AsyncPacketSocket* next_client_tcp_socket) {
994 next_client_tcp_socket_ = next_client_tcp_socket; 993 next_client_tcp_socket_ = next_client_tcp_socket;
995 } 994 }
996 rtc::AsyncResolverInterface* CreateAsyncResolver() override { 995 rtc::AsyncResolverInterface* CreateAsyncResolver() override {
997 return NULL; 996 return nullptr;
998 } 997 }
999 998
1000 private: 999 private:
1001 AsyncPacketSocket* next_udp_socket_; 1000 AsyncPacketSocket* next_udp_socket_;
1002 AsyncPacketSocket* next_server_tcp_socket_; 1001 AsyncPacketSocket* next_server_tcp_socket_;
1003 AsyncPacketSocket* next_client_tcp_socket_; 1002 AsyncPacketSocket* next_client_tcp_socket_;
1004 }; 1003 };
1005 1004
1006 class FakeAsyncPacketSocket : public AsyncPacketSocket { 1005 class FakeAsyncPacketSocket : public AsyncPacketSocket {
1007 public: 1006 public:
1008 // Returns current local address. Address may be set to NULL if the 1007 // Returns current local address. Address may be set to null if the
1009 // socket is not bound yet (GetState() returns STATE_BINDING). 1008 // socket is not bound yet (GetState() returns STATE_BINDING).
1010 virtual SocketAddress GetLocalAddress() const { 1009 virtual SocketAddress GetLocalAddress() const {
1011 return SocketAddress(); 1010 return SocketAddress();
1012 } 1011 }
1013 1012
1014 // Returns remote address. Returns zeroes if this is not a client TCP socket. 1013 // Returns remote address. Returns zeroes if this is not a client TCP socket.
1015 virtual SocketAddress GetRemoteAddress() const { 1014 virtual SocketAddress GetRemoteAddress() const {
1016 return SocketAddress(); 1015 return SocketAddress();
1017 } 1016 }
1018 1017
(...skipping 334 matching lines...)
1353 std::unique_ptr<TestPort> lport( 1352 std::unique_ptr<TestPort> lport(
1354 CreateTestPort(kLocalAddr1, "lfrag", "lpass")); 1353 CreateTestPort(kLocalAddr1, "lfrag", "lpass"));
1355 lport->SetIceRole(cricket::ICEROLE_CONTROLLING); 1354 lport->SetIceRole(cricket::ICEROLE_CONTROLLING);
1356 lport->SetIceTiebreaker(kTiebreaker1); 1355 lport->SetIceTiebreaker(kTiebreaker1);
1357 lport->PrepareAddress(); 1356 lport->PrepareAddress();
1358 ASSERT_FALSE(lport->Candidates().empty()); 1357 ASSERT_FALSE(lport->Candidates().empty());
1359 Connection* conn = lport->CreateConnection(lport->Candidates()[0], 1358 Connection* conn = lport->CreateConnection(lport->Candidates()[0],
1360 Port::ORIGIN_MESSAGE); 1359 Port::ORIGIN_MESSAGE);
1361 conn->Ping(0); 1360 conn->Ping(0);
1362 1361
1363 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); 1362 ASSERT_TRUE_WAIT(lport->last_stun_msg() != nullptr, kDefaultTimeout);
1364 IceMessage* msg = lport->last_stun_msg(); 1363 IceMessage* msg = lport->last_stun_msg();
1365 EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); 1364 EXPECT_EQ(STUN_BINDING_REQUEST, msg->type());
1366 conn->OnReadPacket(lport->last_stun_buf()->data<char>(), 1365 conn->OnReadPacket(lport->last_stun_buf()->data<char>(),
1367 lport->last_stun_buf()->size(), 1366 lport->last_stun_buf()->size(),
1368 rtc::PacketTime()); 1367 rtc::PacketTime());
1369 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); 1368 ASSERT_TRUE_WAIT(lport->last_stun_msg() != nullptr, kDefaultTimeout);
1370 msg = lport->last_stun_msg(); 1369 msg = lport->last_stun_msg();
1371 EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type()); 1370 EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type());
1372 1371
1373 // If the tiebreaker value is different from port, we expect a error 1372 // If the tiebreaker value is different from port, we expect a error
1374 // response. 1373 // response.
1375 lport->Reset(); 1374 lport->Reset();
1376 lport->AddCandidateAddress(kLocalAddr2); 1375 lport->AddCandidateAddress(kLocalAddr2);
1377 // Creating a different connection as |conn| is receiving. 1376 // Creating a different connection as |conn| is receiving.
1378 Connection* conn1 = lport->CreateConnection(lport->Candidates()[1], 1377 Connection* conn1 = lport->CreateConnection(lport->Candidates()[1],
1379 Port::ORIGIN_MESSAGE); 1378 Port::ORIGIN_MESSAGE);
1380 conn1->Ping(0); 1379 conn1->Ping(0);
1381 1380
1382 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); 1381 ASSERT_TRUE_WAIT(lport->last_stun_msg() != nullptr, kDefaultTimeout);
1383 msg = lport->last_stun_msg(); 1382 msg = lport->last_stun_msg();
1384 EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); 1383 EXPECT_EQ(STUN_BINDING_REQUEST, msg->type());
1385 std::unique_ptr<IceMessage> modified_req( 1384 std::unique_ptr<IceMessage> modified_req(
1386 CreateStunMessage(STUN_BINDING_REQUEST)); 1385 CreateStunMessage(STUN_BINDING_REQUEST));
1387 const StunByteStringAttribute* username_attr = msg->GetByteString( 1386 const StunByteStringAttribute* username_attr = msg->GetByteString(
1388 STUN_ATTR_USERNAME); 1387 STUN_ATTR_USERNAME);
1389 modified_req->AddAttribute(new StunByteStringAttribute( 1388 modified_req->AddAttribute(new StunByteStringAttribute(
1390 STUN_ATTR_USERNAME, username_attr->GetString())); 1389 STUN_ATTR_USERNAME, username_attr->GetString()));
1391 // To make sure we receive error response, adding tiebreaker less than 1390 // To make sure we receive error response, adding tiebreaker less than
1392 // what's present in request. 1391 // what's present in request.
1393 modified_req->AddAttribute(new StunUInt64Attribute( 1392 modified_req->AddAttribute(new StunUInt64Attribute(
1394 STUN_ATTR_ICE_CONTROLLING, kTiebreaker1 - 1)); 1393 STUN_ATTR_ICE_CONTROLLING, kTiebreaker1 - 1));
1395 modified_req->AddMessageIntegrity("lpass"); 1394 modified_req->AddMessageIntegrity("lpass");
1396 modified_req->AddFingerprint(); 1395 modified_req->AddFingerprint();
1397 1396
1398 lport->Reset(); 1397 lport->Reset();
1399 std::unique_ptr<ByteBufferWriter> buf(new ByteBufferWriter()); 1398 std::unique_ptr<ByteBufferWriter> buf(new ByteBufferWriter());
1400 WriteStunMessage(modified_req.get(), buf.get()); 1399 WriteStunMessage(modified_req.get(), buf.get());
1401 conn1->OnReadPacket(buf->Data(), buf->Length(), rtc::PacketTime()); 1400 conn1->OnReadPacket(buf->Data(), buf->Length(), rtc::PacketTime());
1402 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); 1401 ASSERT_TRUE_WAIT(lport->last_stun_msg() != nullptr, kDefaultTimeout);
1403 msg = lport->last_stun_msg(); 1402 msg = lport->last_stun_msg();
1404 EXPECT_EQ(STUN_BINDING_ERROR_RESPONSE, msg->type()); 1403 EXPECT_EQ(STUN_BINDING_ERROR_RESPONSE, msg->type());
1405 } 1404 }
1406 1405
1407 // This test verifies role conflict signal is received when there is 1406 // This test verifies role conflict signal is received when there is
1408 // conflict in the role. In this case both ports are in controlling and 1407 // conflict in the role. In this case both ports are in controlling and
1409 // |rport| has higher tiebreaker value than |lport|. Since |lport| has lower 1408 // |rport| has higher tiebreaker value than |lport|. Since |lport| has lower
1410 // value of tiebreaker, when it receives ping request from |rport| it will 1409 // value of tiebreaker, when it receives ping request from |rport| it will
1411 // send role conflict signal. 1410 // send role conflict signal.
1412 TEST_F(PortTest, TestIceRoleConflict) { 1411 TEST_F(PortTest, TestIceRoleConflict) {
1413 std::unique_ptr<TestPort> lport( 1412 std::unique_ptr<TestPort> lport(
1414 CreateTestPort(kLocalAddr1, "lfrag", "lpass")); 1413 CreateTestPort(kLocalAddr1, "lfrag", "lpass"));
1415 lport->SetIceRole(cricket::ICEROLE_CONTROLLING); 1414 lport->SetIceRole(cricket::ICEROLE_CONTROLLING);
1416 lport->SetIceTiebreaker(kTiebreaker1); 1415 lport->SetIceTiebreaker(kTiebreaker1);
1417 std::unique_ptr<TestPort> rport( 1416 std::unique_ptr<TestPort> rport(
1418 CreateTestPort(kLocalAddr2, "rfrag", "rpass")); 1417 CreateTestPort(kLocalAddr2, "rfrag", "rpass"));
1419 rport->SetIceRole(cricket::ICEROLE_CONTROLLING); 1418 rport->SetIceRole(cricket::ICEROLE_CONTROLLING);
1420 rport->SetIceTiebreaker(kTiebreaker2); 1419 rport->SetIceTiebreaker(kTiebreaker2);
1421 1420
1422 lport->PrepareAddress(); 1421 lport->PrepareAddress();
1423 rport->PrepareAddress(); 1422 rport->PrepareAddress();
1424 ASSERT_FALSE(lport->Candidates().empty()); 1423 ASSERT_FALSE(lport->Candidates().empty());
1425 ASSERT_FALSE(rport->Candidates().empty()); 1424 ASSERT_FALSE(rport->Candidates().empty());
1426 Connection* lconn = lport->CreateConnection(rport->Candidates()[0], 1425 Connection* lconn = lport->CreateConnection(rport->Candidates()[0],
1427 Port::ORIGIN_MESSAGE); 1426 Port::ORIGIN_MESSAGE);
1428 Connection* rconn = rport->CreateConnection(lport->Candidates()[0], 1427 Connection* rconn = rport->CreateConnection(lport->Candidates()[0],
1429 Port::ORIGIN_MESSAGE); 1428 Port::ORIGIN_MESSAGE);
1430 rconn->Ping(0); 1429 rconn->Ping(0);
1431 1430
1432 ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, kDefaultTimeout); 1431 ASSERT_TRUE_WAIT(rport->last_stun_msg() != nullptr, kDefaultTimeout);
1433 IceMessage* msg = rport->last_stun_msg(); 1432 IceMessage* msg = rport->last_stun_msg();
1434 EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); 1433 EXPECT_EQ(STUN_BINDING_REQUEST, msg->type());
1435 // Send rport binding request to lport. 1434 // Send rport binding request to lport.
1436 lconn->OnReadPacket(rport->last_stun_buf()->data<char>(), 1435 lconn->OnReadPacket(rport->last_stun_buf()->data<char>(),
1437 rport->last_stun_buf()->size(), 1436 rport->last_stun_buf()->size(),
1438 rtc::PacketTime()); 1437 rtc::PacketTime());
1439 1438
1440 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); 1439 ASSERT_TRUE_WAIT(lport->last_stun_msg() != nullptr, kDefaultTimeout);
1441 EXPECT_EQ(STUN_BINDING_RESPONSE, lport->last_stun_msg()->type()); 1440 EXPECT_EQ(STUN_BINDING_RESPONSE, lport->last_stun_msg()->type());
1442 EXPECT_TRUE(role_conflict()); 1441 EXPECT_TRUE(role_conflict());
1443 } 1442 }
1444 1443
1445 TEST_F(PortTest, TestTcpNoDelay) { 1444 TEST_F(PortTest, TestTcpNoDelay) {
1446 TCPPort* port1 = CreateTcpPort(kLocalAddr1); 1445 TCPPort* port1 = CreateTcpPort(kLocalAddr1);
1447 port1->SetIceRole(cricket::ICEROLE_CONTROLLING); 1446 port1->SetIceRole(cricket::ICEROLE_CONTROLLING);
1448 int option_value = -1; 1447 int option_value = -1;
1449 int success = port1->GetOption(rtc::Socket::OPT_NODELAY, 1448 int success = port1->GetOption(rtc::Socket::OPT_NODELAY,
1450 &option_value); 1449 &option_value);
(...skipping 55 matching lines...)
1506 ports[i]->PrepareAddress(); 1505 ports[i]->PrepareAddress();
1507 } 1506 }
1508 1507
1509 // IPv4 Port, connects to IPv6 candidate and then to IPv4 candidate. 1508 // IPv4 Port, connects to IPv6 candidate and then to IPv4 candidate.
1510 if (type == SOCK_STREAM) { 1509 if (type == SOCK_STREAM) {
1511 FakeAsyncPacketSocket* clientsocket = new FakeAsyncPacketSocket(); 1510 FakeAsyncPacketSocket* clientsocket = new FakeAsyncPacketSocket();
1512 factory.set_next_client_tcp_socket(clientsocket); 1511 factory.set_next_client_tcp_socket(clientsocket);
1513 } 1512 }
1514 Connection* c = ports[0]->CreateConnection(GetCandidate(ports[2].get()), 1513 Connection* c = ports[0]->CreateConnection(GetCandidate(ports[2].get()),
1515 Port::ORIGIN_MESSAGE); 1514 Port::ORIGIN_MESSAGE);
1516 EXPECT_TRUE(NULL == c); 1515 EXPECT_TRUE(nullptr == c);
1517 EXPECT_EQ(0U, ports[0]->connections().size()); 1516 EXPECT_EQ(0U, ports[0]->connections().size());
1518 c = ports[0]->CreateConnection(GetCandidate(ports[1].get()), 1517 c = ports[0]->CreateConnection(GetCandidate(ports[1].get()),
1519 Port::ORIGIN_MESSAGE); 1518 Port::ORIGIN_MESSAGE);
1520 EXPECT_FALSE(NULL == c); 1519 EXPECT_FALSE(nullptr == c);
1521 EXPECT_EQ(1U, ports[0]->connections().size()); 1520 EXPECT_EQ(1U, ports[0]->connections().size());
1522 1521
1523 // IPv6 Port, connects to IPv4 candidate and to IPv6 candidate. 1522 // IPv6 Port, connects to IPv4 candidate and to IPv6 candidate.
1524 if (type == SOCK_STREAM) { 1523 if (type == SOCK_STREAM) {
1525 FakeAsyncPacketSocket* clientsocket = new FakeAsyncPacketSocket(); 1524 FakeAsyncPacketSocket* clientsocket = new FakeAsyncPacketSocket();
1526 factory.set_next_client_tcp_socket(clientsocket); 1525 factory.set_next_client_tcp_socket(clientsocket);
1527 } 1526 }
1528 c = ports[2]->CreateConnection(GetCandidate(ports[0].get()), 1527 c = ports[2]->CreateConnection(GetCandidate(ports[0].get()),
1529 Port::ORIGIN_MESSAGE); 1528 Port::ORIGIN_MESSAGE);
1530 EXPECT_TRUE(NULL == c); 1529 EXPECT_TRUE(nullptr == c);
1531 EXPECT_EQ(0U, ports[2]->connections().size()); 1530 EXPECT_EQ(0U, ports[2]->connections().size());
1532 c = ports[2]->CreateConnection(GetCandidate(ports[3].get()), 1531 c = ports[2]->CreateConnection(GetCandidate(ports[3].get()),
1533 Port::ORIGIN_MESSAGE); 1532 Port::ORIGIN_MESSAGE);
1534 EXPECT_FALSE(NULL == c); 1533 EXPECT_FALSE(nullptr == c);
1535 EXPECT_EQ(1U, ports[2]->connections().size()); 1534 EXPECT_EQ(1U, ports[2]->connections().size());
1536 } 1535 }
1537 1536
1538 TEST_F(PortTest, TestSkipCrossFamilyTcp) { 1537 TEST_F(PortTest, TestSkipCrossFamilyTcp) {
1539 TestCrossFamilyPorts(SOCK_STREAM); 1538 TestCrossFamilyPorts(SOCK_STREAM);
1540 } 1539 }
1541 1540
1542 TEST_F(PortTest, TestSkipCrossFamilyUdp) { 1541 TEST_F(PortTest, TestSkipCrossFamilyUdp) {
1543 TestCrossFamilyPorts(SOCK_DGRAM); 1542 TestCrossFamilyPorts(SOCK_DGRAM);
1544 } 1543 }
1545 1544
1546 void PortTest::ExpectPortsCanConnect(bool can_connect, Port* p1, Port* p2) { 1545 void PortTest::ExpectPortsCanConnect(bool can_connect, Port* p1, Port* p2) {
1547 Connection* c = p1->CreateConnection(GetCandidate(p2), 1546 Connection* c = p1->CreateConnection(GetCandidate(p2),
1548 Port::ORIGIN_MESSAGE); 1547 Port::ORIGIN_MESSAGE);
1549 if (can_connect) { 1548 if (can_connect) {
1550 EXPECT_FALSE(NULL == c); 1549 EXPECT_FALSE(nullptr == c);
1551 EXPECT_EQ(1U, p1->connections().size()); 1550 EXPECT_EQ(1U, p1->connections().size());
1552 } else { 1551 } else {
1553 EXPECT_TRUE(NULL == c); 1552 EXPECT_TRUE(nullptr == c);
1554 EXPECT_EQ(0U, p1->connections().size()); 1553 EXPECT_EQ(0U, p1->connections().size());
1555 } 1554 }
1556 } 1555 }
1557 1556
1558 TEST_F(PortTest, TestUdpV6CrossTypePorts) { 1557 TEST_F(PortTest, TestUdpV6CrossTypePorts) {
1559 FakePacketSocketFactory factory; 1558 FakePacketSocketFactory factory;
1560 std::unique_ptr<Port> ports[4]; 1559 std::unique_ptr<Port> ports[4];
1561 SocketAddress addresses[4] = {SocketAddress("2001:db8::1", 0), 1560 SocketAddress addresses[4] = {SocketAddress("2001:db8::1", 0),
1562 SocketAddress("fe80::1", 0), 1561 SocketAddress("fe80::1", 0),
1563 SocketAddress("fe80::2", 0), 1562 SocketAddress("fe80::2", 0),
(...skipping 73 matching lines...)
1637 lport->PrepareAddress(); 1636 lport->PrepareAddress();
1638 rport->PrepareAddress(); 1637 rport->PrepareAddress();
1639 ASSERT_FALSE(rport->Candidates().empty()); 1638 ASSERT_FALSE(rport->Candidates().empty());
1640 Connection* lconn = lport->CreateConnection( 1639 Connection* lconn = lport->CreateConnection(
1641 rport->Candidates()[0], Port::ORIGIN_MESSAGE); 1640 rport->Candidates()[0], Port::ORIGIN_MESSAGE);
1642 Connection* rconn = rport->CreateConnection( 1641 Connection* rconn = rport->CreateConnection(
1643 lport->Candidates()[0], Port::ORIGIN_MESSAGE); 1642 lport->Candidates()[0], Port::ORIGIN_MESSAGE);
1644 lconn->Ping(0); 1643 lconn->Ping(0);
1645 1644
1646 // Check that it's a proper BINDING-REQUEST. 1645 // Check that it's a proper BINDING-REQUEST.
1647 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); 1646 ASSERT_TRUE_WAIT(lport->last_stun_msg() != nullptr, kDefaultTimeout);
1648 IceMessage* msg = lport->last_stun_msg(); 1647 IceMessage* msg = lport->last_stun_msg();
1649 EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); 1648 EXPECT_EQ(STUN_BINDING_REQUEST, msg->type());
1650 EXPECT_FALSE(msg->IsLegacy()); 1649 EXPECT_FALSE(msg->IsLegacy());
1651 const StunByteStringAttribute* username_attr = 1650 const StunByteStringAttribute* username_attr =
1652 msg->GetByteString(STUN_ATTR_USERNAME); 1651 msg->GetByteString(STUN_ATTR_USERNAME);
1653 ASSERT_TRUE(username_attr != NULL); 1652 ASSERT_TRUE(username_attr != nullptr);
1654 const StunUInt32Attribute* priority_attr = msg->GetUInt32(STUN_ATTR_PRIORITY); 1653 const StunUInt32Attribute* priority_attr = msg->GetUInt32(STUN_ATTR_PRIORITY);
1655 ASSERT_TRUE(priority_attr != NULL); 1654 ASSERT_TRUE(priority_attr != nullptr);
1656 EXPECT_EQ(kDefaultPrflxPriority, priority_attr->value()); 1655 EXPECT_EQ(kDefaultPrflxPriority, priority_attr->value());
1657 EXPECT_EQ("rfrag:lfrag", username_attr->GetString()); 1656 EXPECT_EQ("rfrag:lfrag", username_attr->GetString());
1658 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY) != NULL); 1657 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY) != nullptr);
1659 EXPECT_TRUE(StunMessage::ValidateMessageIntegrity( 1658 EXPECT_TRUE(StunMessage::ValidateMessageIntegrity(
1660 lport->last_stun_buf()->data<char>(), lport->last_stun_buf()->size(), 1659 lport->last_stun_buf()->data<char>(), lport->last_stun_buf()->size(),
1661 "rpass")); 1660 "rpass"));
1662 const StunUInt64Attribute* ice_controlling_attr = 1661 const StunUInt64Attribute* ice_controlling_attr =
1663 msg->GetUInt64(STUN_ATTR_ICE_CONTROLLING); 1662 msg->GetUInt64(STUN_ATTR_ICE_CONTROLLING);
1664 ASSERT_TRUE(ice_controlling_attr != NULL); 1663 ASSERT_TRUE(ice_controlling_attr != nullptr);
1665 EXPECT_EQ(lport->IceTiebreaker(), ice_controlling_attr->value()); 1664 EXPECT_EQ(lport->IceTiebreaker(), ice_controlling_attr->value());
1666 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_ICE_CONTROLLED) == NULL); 1665 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_ICE_CONTROLLED) == nullptr);
1667 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) != NULL); 1666 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) != nullptr);
1668 EXPECT_TRUE(msg->GetUInt32(STUN_ATTR_FINGERPRINT) != NULL); 1667 EXPECT_TRUE(msg->GetUInt32(STUN_ATTR_FINGERPRINT) != nullptr);
1669 EXPECT_TRUE(StunMessage::ValidateFingerprint( 1668 EXPECT_TRUE(StunMessage::ValidateFingerprint(
1670 lport->last_stun_buf()->data<char>(), lport->last_stun_buf()->size())); 1669 lport->last_stun_buf()->data<char>(), lport->last_stun_buf()->size()));
1671 1670
1672 // Request should not include ping count. 1671 // Request should not include ping count.
1673 ASSERT_TRUE(msg->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT) == NULL); 1672 ASSERT_TRUE(msg->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT) == nullptr);
1674 1673
1675 // Save a copy of the BINDING-REQUEST for use below. 1674 // Save a copy of the BINDING-REQUEST for use below.
1676 std::unique_ptr<IceMessage> request(CopyStunMessage(msg)); 1675 std::unique_ptr<IceMessage> request(CopyStunMessage(msg));
1677 1676
1678 // Receive the BINDING-REQUEST and respond with BINDING-RESPONSE. 1677 // Receive the BINDING-REQUEST and respond with BINDING-RESPONSE.
1679 rconn->OnReadPacket(lport->last_stun_buf()->data<char>(), 1678 rconn->OnReadPacket(lport->last_stun_buf()->data<char>(),
1680 lport->last_stun_buf()->size(), rtc::PacketTime()); 1679 lport->last_stun_buf()->size(), rtc::PacketTime());
1681 msg = rport->last_stun_msg(); 1680 msg = rport->last_stun_msg();
1682 ASSERT_TRUE(msg != NULL); 1681 ASSERT_TRUE(msg != nullptr);
1683 EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type()); 1682 EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type());
1684 // Received a BINDING-RESPONSE. 1683 // Received a BINDING-RESPONSE.
1685 lconn->OnReadPacket(rport->last_stun_buf()->data<char>(), 1684 lconn->OnReadPacket(rport->last_stun_buf()->data<char>(),
1686 rport->last_stun_buf()->size(), rtc::PacketTime()); 1685 rport->last_stun_buf()->size(), rtc::PacketTime());
1687 // Verify the STUN Stats. 1686 // Verify the STUN Stats.
1688 EXPECT_EQ(1U, lconn->stats().sent_ping_requests_total); 1687 EXPECT_EQ(1U, lconn->stats().sent_ping_requests_total);
1689 EXPECT_EQ(1U, lconn->stats().sent_ping_requests_before_first_response); 1688 EXPECT_EQ(1U, lconn->stats().sent_ping_requests_before_first_response);
1690 EXPECT_EQ(1U, lconn->stats().recv_ping_responses); 1689 EXPECT_EQ(1U, lconn->stats().recv_ping_responses);
1691 EXPECT_EQ(1U, rconn->stats().recv_ping_requests); 1690 EXPECT_EQ(1U, rconn->stats().recv_ping_requests);
1692 EXPECT_EQ(1U, rconn->stats().sent_ping_responses); 1691 EXPECT_EQ(1U, rconn->stats().sent_ping_responses);
1693 1692
1694 EXPECT_FALSE(msg->IsLegacy()); 1693 EXPECT_FALSE(msg->IsLegacy());
1695 const StunAddressAttribute* addr_attr = msg->GetAddress( 1694 const StunAddressAttribute* addr_attr = msg->GetAddress(
1696 STUN_ATTR_XOR_MAPPED_ADDRESS); 1695 STUN_ATTR_XOR_MAPPED_ADDRESS);
1697 ASSERT_TRUE(addr_attr != NULL); 1696 ASSERT_TRUE(addr_attr != nullptr);
1698 EXPECT_EQ(lport->Candidates()[0].address(), addr_attr->GetAddress()); 1697 EXPECT_EQ(lport->Candidates()[0].address(), addr_attr->GetAddress());
1699 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY) != NULL); 1698 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY) != nullptr);
1700 EXPECT_TRUE(StunMessage::ValidateMessageIntegrity( 1699 EXPECT_TRUE(StunMessage::ValidateMessageIntegrity(
1701 rport->last_stun_buf()->data<char>(), rport->last_stun_buf()->size(), 1700 rport->last_stun_buf()->data<char>(), rport->last_stun_buf()->size(),
1702 "rpass")); 1701 "rpass"));
1703 EXPECT_TRUE(msg->GetUInt32(STUN_ATTR_FINGERPRINT) != NULL); 1702 EXPECT_TRUE(msg->GetUInt32(STUN_ATTR_FINGERPRINT) != nullptr);
1704 EXPECT_TRUE(StunMessage::ValidateFingerprint( 1703 EXPECT_TRUE(StunMessage::ValidateFingerprint(
1705 lport->last_stun_buf()->data<char>(), lport->last_stun_buf()->size())); 1704 lport->last_stun_buf()->data<char>(), lport->last_stun_buf()->size()));
1706 // No USERNAME or PRIORITY in ICE responses. 1705 // No USERNAME or PRIORITY in ICE responses.
1707 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USERNAME) == NULL); 1706 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USERNAME) == nullptr);
1708 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_PRIORITY) == NULL); 1707 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_PRIORITY) == nullptr);
1709 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_MAPPED_ADDRESS) == NULL); 1708 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_MAPPED_ADDRESS) == nullptr);
1710 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_ICE_CONTROLLING) == NULL); 1709 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_ICE_CONTROLLING) == nullptr);
1711 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_ICE_CONTROLLED) == NULL); 1710 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_ICE_CONTROLLED) == nullptr);
1712 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) == NULL); 1711 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) == nullptr);
1713 1712
1714 // Response should not include ping count. 1713 // Response should not include ping count.
1715 ASSERT_TRUE(msg->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT) == NULL); 1714 ASSERT_TRUE(msg->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT) == nullptr);
1716 1715
1717 // Respond with a BINDING-ERROR-RESPONSE. This wouldn't happen in real life, 1716 // Respond with a BINDING-ERROR-RESPONSE. This wouldn't happen in real life,
1718 // but we can do it here. 1717 // but we can do it here.
1719 rport->SendBindingErrorResponse(request.get(), 1718 rport->SendBindingErrorResponse(request.get(),
1720 lport->Candidates()[0].address(), 1719 lport->Candidates()[0].address(),
1721 STUN_ERROR_SERVER_ERROR, 1720 STUN_ERROR_SERVER_ERROR,
1722 STUN_ERROR_REASON_SERVER_ERROR); 1721 STUN_ERROR_REASON_SERVER_ERROR);
1723 msg = rport->last_stun_msg(); 1722 msg = rport->last_stun_msg();
1724 ASSERT_TRUE(msg != NULL); 1723 ASSERT_TRUE(msg != nullptr);
1725 EXPECT_EQ(STUN_BINDING_ERROR_RESPONSE, msg->type()); 1724 EXPECT_EQ(STUN_BINDING_ERROR_RESPONSE, msg->type());
1726 EXPECT_FALSE(msg->IsLegacy()); 1725 EXPECT_FALSE(msg->IsLegacy());
1727 const StunErrorCodeAttribute* error_attr = msg->GetErrorCode(); 1726 const StunErrorCodeAttribute* error_attr = msg->GetErrorCode();
1728 ASSERT_TRUE(error_attr != NULL); 1727 ASSERT_TRUE(error_attr != nullptr);
1729 EXPECT_EQ(STUN_ERROR_SERVER_ERROR, error_attr->code()); 1728 EXPECT_EQ(STUN_ERROR_SERVER_ERROR, error_attr->code());
1730 EXPECT_EQ(std::string(STUN_ERROR_REASON_SERVER_ERROR), error_attr->reason()); 1729 EXPECT_EQ(std::string(STUN_ERROR_REASON_SERVER_ERROR), error_attr->reason());
1731 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY) != NULL); 1730 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY) != nullptr);
1732 EXPECT_TRUE(StunMessage::ValidateMessageIntegrity( 1731 EXPECT_TRUE(StunMessage::ValidateMessageIntegrity(
1733 rport->last_stun_buf()->data<char>(), rport->last_stun_buf()->size(), 1732 rport->last_stun_buf()->data<char>(), rport->last_stun_buf()->size(),
1734 "rpass")); 1733 "rpass"));
1735 EXPECT_TRUE(msg->GetUInt32(STUN_ATTR_FINGERPRINT) != NULL); 1734 EXPECT_TRUE(msg->GetUInt32(STUN_ATTR_FINGERPRINT) != nullptr);
1736 EXPECT_TRUE(StunMessage::ValidateFingerprint( 1735 EXPECT_TRUE(StunMessage::ValidateFingerprint(
1737 lport->last_stun_buf()->data<char>(), lport->last_stun_buf()->size())); 1736 lport->last_stun_buf()->data<char>(), lport->last_stun_buf()->size()));
1738 // No USERNAME with ICE. 1737 // No USERNAME with ICE.
1739 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USERNAME) == NULL); 1738 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USERNAME) == nullptr);
1740 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_PRIORITY) == NULL); 1739 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_PRIORITY) == nullptr);
1741 1740
1742 // Testing STUN binding requests from rport --> lport, having ICE_CONTROLLED 1741 // Testing STUN binding requests from rport --> lport, having ICE_CONTROLLED
1743 // and (incremented) RETRANSMIT_COUNT attributes. 1742 // and (incremented) RETRANSMIT_COUNT attributes.
1744 rport->Reset(); 1743 rport->Reset();
1745 rport->set_send_retransmit_count_attribute(true); 1744 rport->set_send_retransmit_count_attribute(true);
1746 rconn->Ping(0); 1745 rconn->Ping(0);
1747 rconn->Ping(0); 1746 rconn->Ping(0);
1748 rconn->Ping(0); 1747 rconn->Ping(0);
1749 ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, kDefaultTimeout); 1748 ASSERT_TRUE_WAIT(rport->last_stun_msg() != nullptr, kDefaultTimeout);
1750 msg = rport->last_stun_msg(); 1749 msg = rport->last_stun_msg();
1751 EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); 1750 EXPECT_EQ(STUN_BINDING_REQUEST, msg->type());
1752 const StunUInt64Attribute* ice_controlled_attr = 1751 const StunUInt64Attribute* ice_controlled_attr =
1753 msg->GetUInt64(STUN_ATTR_ICE_CONTROLLED); 1752 msg->GetUInt64(STUN_ATTR_ICE_CONTROLLED);
1754 ASSERT_TRUE(ice_controlled_attr != NULL); 1753 ASSERT_TRUE(ice_controlled_attr != nullptr);
1755 EXPECT_EQ(rport->IceTiebreaker(), ice_controlled_attr->value()); 1754 EXPECT_EQ(rport->IceTiebreaker(), ice_controlled_attr->value());
1756 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) == NULL); 1755 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) == nullptr);
1757 1756
1758 // Request should include ping count. 1757 // Request should include ping count.
1759 const StunUInt32Attribute* retransmit_attr = 1758 const StunUInt32Attribute* retransmit_attr =
1760 msg->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT); 1759 msg->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT);
1761 ASSERT_TRUE(retransmit_attr != NULL); 1760 ASSERT_TRUE(retransmit_attr != nullptr);
1762 EXPECT_EQ(2U, retransmit_attr->value()); 1761 EXPECT_EQ(2U, retransmit_attr->value());
1763 1762
1764 // Respond with a BINDING-RESPONSE. 1763 // Respond with a BINDING-RESPONSE.
1765 request.reset(CopyStunMessage(msg)); 1764 request.reset(CopyStunMessage(msg));
1766 lconn->OnReadPacket(rport->last_stun_buf()->data<char>(), 1765 lconn->OnReadPacket(rport->last_stun_buf()->data<char>(),
1767 rport->last_stun_buf()->size(), rtc::PacketTime()); 1766 rport->last_stun_buf()->size(), rtc::PacketTime());
1768 msg = lport->last_stun_msg(); 1767 msg = lport->last_stun_msg();
1769 // Receive the BINDING-RESPONSE. 1768 // Receive the BINDING-RESPONSE.
1770 rconn->OnReadPacket(lport->last_stun_buf()->data<char>(), 1769 rconn->OnReadPacket(lport->last_stun_buf()->data<char>(),
1771 lport->last_stun_buf()->size(), rtc::PacketTime()); 1770 lport->last_stun_buf()->size(), rtc::PacketTime());
1772 1771
1773 // Verify the Stun ping stats. 1772 // Verify the Stun ping stats.
1774 EXPECT_EQ(3U, rconn->stats().sent_ping_requests_total); 1773 EXPECT_EQ(3U, rconn->stats().sent_ping_requests_total);
1775 EXPECT_EQ(3U, rconn->stats().sent_ping_requests_before_first_response); 1774 EXPECT_EQ(3U, rconn->stats().sent_ping_requests_before_first_response);
1776 EXPECT_EQ(1U, rconn->stats().recv_ping_responses); 1775 EXPECT_EQ(1U, rconn->stats().recv_ping_responses);
1777 EXPECT_EQ(1U, lconn->stats().sent_ping_responses); 1776 EXPECT_EQ(1U, lconn->stats().sent_ping_responses);
1778 EXPECT_EQ(1U, lconn->stats().recv_ping_requests); 1777 EXPECT_EQ(1U, lconn->stats().recv_ping_requests);
1779 // Ping after receiver the first response 1778 // Ping after receiver the first response
1780 rconn->Ping(0); 1779 rconn->Ping(0);
1781 rconn->Ping(0); 1780 rconn->Ping(0);
1782 EXPECT_EQ(5U, rconn->stats().sent_ping_requests_total); 1781 EXPECT_EQ(5U, rconn->stats().sent_ping_requests_total);
1783 EXPECT_EQ(3U, rconn->stats().sent_ping_requests_before_first_response); 1782 EXPECT_EQ(3U, rconn->stats().sent_ping_requests_before_first_response);
1784 1783
1785 // Response should include same ping count. 1784 // Response should include same ping count.
1786 retransmit_attr = msg->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT); 1785 retransmit_attr = msg->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT);
1787 ASSERT_TRUE(retransmit_attr != NULL); 1786 ASSERT_TRUE(retransmit_attr != nullptr);
1788 EXPECT_EQ(2U, retransmit_attr->value()); 1787 EXPECT_EQ(2U, retransmit_attr->value());
1789 } 1788 }
1790 1789
1791 TEST_F(PortTest, TestUseCandidateAttribute) { 1790 TEST_F(PortTest, TestUseCandidateAttribute) {
1792 std::unique_ptr<TestPort> lport( 1791 std::unique_ptr<TestPort> lport(
1793 CreateTestPort(kLocalAddr1, "lfrag", "lpass")); 1792 CreateTestPort(kLocalAddr1, "lfrag", "lpass"));
1794 std::unique_ptr<TestPort> rport( 1793 std::unique_ptr<TestPort> rport(
1795 CreateTestPort(kLocalAddr2, "rfrag", "rpass")); 1794 CreateTestPort(kLocalAddr2, "rfrag", "rpass"));
1796 lport->SetIceRole(cricket::ICEROLE_CONTROLLING); 1795 lport->SetIceRole(cricket::ICEROLE_CONTROLLING);
1797 lport->SetIceTiebreaker(kTiebreaker1); 1796 lport->SetIceTiebreaker(kTiebreaker1);
1798 rport->SetIceRole(cricket::ICEROLE_CONTROLLED); 1797 rport->SetIceRole(cricket::ICEROLE_CONTROLLED);
1799 rport->SetIceTiebreaker(kTiebreaker2); 1798 rport->SetIceTiebreaker(kTiebreaker2);
1800 1799
1801 // Send a fake ping from lport to rport. 1800 // Send a fake ping from lport to rport.
1802 lport->PrepareAddress(); 1801 lport->PrepareAddress();
1803 rport->PrepareAddress(); 1802 rport->PrepareAddress();
1804 ASSERT_FALSE(rport->Candidates().empty()); 1803 ASSERT_FALSE(rport->Candidates().empty());
1805 Connection* lconn = lport->CreateConnection( 1804 Connection* lconn = lport->CreateConnection(
1806 rport->Candidates()[0], Port::ORIGIN_MESSAGE); 1805 rport->Candidates()[0], Port::ORIGIN_MESSAGE);
1807 lconn->Ping(0); 1806 lconn->Ping(0);
1808 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); 1807 ASSERT_TRUE_WAIT(lport->last_stun_msg() != nullptr, kDefaultTimeout);
1809 IceMessage* msg = lport->last_stun_msg(); 1808 IceMessage* msg = lport->last_stun_msg();
1810 const StunUInt64Attribute* ice_controlling_attr = 1809 const StunUInt64Attribute* ice_controlling_attr =
1811 msg->GetUInt64(STUN_ATTR_ICE_CONTROLLING); 1810 msg->GetUInt64(STUN_ATTR_ICE_CONTROLLING);
1812 ASSERT_TRUE(ice_controlling_attr != NULL); 1811 ASSERT_TRUE(ice_controlling_attr != nullptr);
1813 const StunByteStringAttribute* use_candidate_attr = msg->GetByteString( 1812 const StunByteStringAttribute* use_candidate_attr = msg->GetByteString(
1814 STUN_ATTR_USE_CANDIDATE); 1813 STUN_ATTR_USE_CANDIDATE);
1815 ASSERT_TRUE(use_candidate_attr != NULL); 1814 ASSERT_TRUE(use_candidate_attr != nullptr);
1816 } 1815 }
1817 1816
1818 // Tests that when the network type changes, the network cost of the port will 1817 // Tests that when the network type changes, the network cost of the port will
1819 // change, the network cost of the local candidates will change. Also tests that 1818 // change, the network cost of the local candidates will change. Also tests that
1820 // the remote network costs are updated with the stun binding requests. 1819 // the remote network costs are updated with the stun binding requests.
1821 TEST_F(PortTest, TestNetworkCostChange) { 1820 TEST_F(PortTest, TestNetworkCostChange) {
1822 std::unique_ptr<TestPort> lport( 1821 std::unique_ptr<TestPort> lport(
1823 CreateTestPort(kLocalAddr1, "lfrag", "lpass")); 1822 CreateTestPort(kLocalAddr1, "lfrag", "lpass"));
1824 std::unique_ptr<TestPort> rport( 1823 std::unique_ptr<TestPort> rport(
1825 CreateTestPort(kLocalAddr2, "rfrag", "rpass")); 1824 CreateTestPort(kLocalAddr2, "rfrag", "rpass"));
(...skipping 31 matching lines...)
1857 SetNetworkType(rtc::ADAPTER_TYPE_WIFI); 1856 SetNetworkType(rtc::ADAPTER_TYPE_WIFI);
1858 Connection* rconn = 1857 Connection* rconn =
1859 rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); 1858 rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE);
1860 SetNetworkType(rtc::ADAPTER_TYPE_CELLULAR); 1859 SetNetworkType(rtc::ADAPTER_TYPE_CELLULAR);
1861 lconn->Ping(0); 1860 lconn->Ping(0);
1862 // The rconn's remote candidate cost is rtc::kNetworkCostLow, but the ping 1861 // The rconn's remote candidate cost is rtc::kNetworkCostLow, but the ping
1863 // contains an attribute of network cost of rtc::kNetworkCostHigh. Once the 1862 // contains an attribute of network cost of rtc::kNetworkCostHigh. Once the
1864 // message is handled in rconn, The rconn's remote candidate will have cost 1863 // message is handled in rconn, The rconn's remote candidate will have cost
1865 // rtc::kNetworkCostHigh; 1864 // rtc::kNetworkCostHigh;
1866 EXPECT_EQ(rtc::kNetworkCostLow, rconn->remote_candidate().network_cost()); 1865 EXPECT_EQ(rtc::kNetworkCostLow, rconn->remote_candidate().network_cost());
1867 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); 1866 ASSERT_TRUE_WAIT(lport->last_stun_msg() != nullptr, kDefaultTimeout);
1868 IceMessage* msg = lport->last_stun_msg(); 1867 IceMessage* msg = lport->last_stun_msg();
1869 EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); 1868 EXPECT_EQ(STUN_BINDING_REQUEST, msg->type());
1870 // Pass the binding request to rport. 1869 // Pass the binding request to rport.
1871 rconn->OnReadPacket(lport->last_stun_buf()->data<char>(), 1870 rconn->OnReadPacket(lport->last_stun_buf()->data<char>(),
1872 lport->last_stun_buf()->size(), rtc::PacketTime()); 1871 lport->last_stun_buf()->size(), rtc::PacketTime());
1873 // Wait until rport sends the response and then check the remote network cost. 1872 // Wait until rport sends the response and then check the remote network cost.
1874 ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, kDefaultTimeout); 1873 ASSERT_TRUE_WAIT(rport->last_stun_msg() != nullptr, kDefaultTimeout);
1875 EXPECT_EQ(rtc::kNetworkCostHigh, rconn->remote_candidate().network_cost()); 1874 EXPECT_EQ(rtc::kNetworkCostHigh, rconn->remote_candidate().network_cost());
1876 } 1875 }
1877 1876
1878 TEST_F(PortTest, TestNetworkInfoAttribute) { 1877 TEST_F(PortTest, TestNetworkInfoAttribute) {
1879 std::unique_ptr<TestPort> lport( 1878 std::unique_ptr<TestPort> lport(
1880 CreateTestPort(kLocalAddr1, "lfrag", "lpass")); 1879 CreateTestPort(kLocalAddr1, "lfrag", "lpass"));
1881 std::unique_ptr<TestPort> rport( 1880 std::unique_ptr<TestPort> rport(
1882 CreateTestPort(kLocalAddr2, "rfrag", "rpass")); 1881 CreateTestPort(kLocalAddr2, "rfrag", "rpass"));
1883 lport->SetIceRole(cricket::ICEROLE_CONTROLLING); 1882 lport->SetIceRole(cricket::ICEROLE_CONTROLLING);
1884 lport->SetIceTiebreaker(kTiebreaker1); 1883 lport->SetIceTiebreaker(kTiebreaker1);
1885 rport->SetIceRole(cricket::ICEROLE_CONTROLLED); 1884 rport->SetIceRole(cricket::ICEROLE_CONTROLLED);
1886 rport->SetIceTiebreaker(kTiebreaker2); 1885 rport->SetIceTiebreaker(kTiebreaker2);
1887 1886
1888 uint16_t lnetwork_id = 9; 1887 uint16_t lnetwork_id = 9;
1889 lport->Network()->set_id(lnetwork_id); 1888 lport->Network()->set_id(lnetwork_id);
1890 // Send a fake ping from lport to rport. 1889 // Send a fake ping from lport to rport.
1891 lport->PrepareAddress(); 1890 lport->PrepareAddress();
1892 rport->PrepareAddress(); 1891 rport->PrepareAddress();
1893 Connection* lconn = 1892 Connection* lconn =
1894 lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); 1893 lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE);
1895 lconn->Ping(0); 1894 lconn->Ping(0);
1896 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); 1895 ASSERT_TRUE_WAIT(lport->last_stun_msg() != nullptr, kDefaultTimeout);
1897 IceMessage* msg = lport->last_stun_msg(); 1896 IceMessage* msg = lport->last_stun_msg();
1898 const StunUInt32Attribute* network_info_attr = 1897 const StunUInt32Attribute* network_info_attr =
1899 msg->GetUInt32(STUN_ATTR_NETWORK_INFO); 1898 msg->GetUInt32(STUN_ATTR_NETWORK_INFO);
1900 ASSERT_TRUE(network_info_attr != NULL); 1899 ASSERT_TRUE(network_info_attr != nullptr);
1901 uint32_t network_info = network_info_attr->value(); 1900 uint32_t network_info = network_info_attr->value();
1902 EXPECT_EQ(lnetwork_id, network_info >> 16); 1901 EXPECT_EQ(lnetwork_id, network_info >> 16);
1903 // Default network has unknown type and cost kNetworkCostUnknown. 1902 // Default network has unknown type and cost kNetworkCostUnknown.
1904 EXPECT_EQ(rtc::kNetworkCostUnknown, network_info & 0xFFFF); 1903 EXPECT_EQ(rtc::kNetworkCostUnknown, network_info & 0xFFFF);
1905 1904
1906 // Set the network type to be cellular so its cost will be kNetworkCostHigh. 1905 // Set the network type to be cellular so its cost will be kNetworkCostHigh.
1907 // Send a fake ping from rport to lport. 1906 // Send a fake ping from rport to lport.
1908 SetNetworkType(rtc::ADAPTER_TYPE_CELLULAR); 1907 SetNetworkType(rtc::ADAPTER_TYPE_CELLULAR);
1909 uint16_t rnetwork_id = 8; 1908 uint16_t rnetwork_id = 8;
1910 rport->Network()->set_id(rnetwork_id); 1909 rport->Network()->set_id(rnetwork_id);
1911 Connection* rconn = 1910 Connection* rconn =
1912 rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); 1911 rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE);
1913 rconn->Ping(0); 1912 rconn->Ping(0);
1914 ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, kDefaultTimeout); 1913 ASSERT_TRUE_WAIT(rport->last_stun_msg() != nullptr, kDefaultTimeout);
1915 msg = rport->last_stun_msg(); 1914 msg = rport->last_stun_msg();
1916 network_info_attr = msg->GetUInt32(STUN_ATTR_NETWORK_INFO); 1915 network_info_attr = msg->GetUInt32(STUN_ATTR_NETWORK_INFO);
1917 ASSERT_TRUE(network_info_attr != NULL); 1916 ASSERT_TRUE(network_info_attr != nullptr);
1918 network_info = network_info_attr->value(); 1917 network_info = network_info_attr->value();
1919 EXPECT_EQ(rnetwork_id, network_info >> 16); 1918 EXPECT_EQ(rnetwork_id, network_info >> 16);
1920 EXPECT_EQ(rtc::kNetworkCostHigh, network_info & 0xFFFF); 1919 EXPECT_EQ(rtc::kNetworkCostHigh, network_info & 0xFFFF);
1921 } 1920 }
1922 1921
1923 // Test handling STUN messages. 1922 // Test handling STUN messages.
1924 TEST_F(PortTest, TestHandleStunMessage) { 1923 TEST_F(PortTest, TestHandleStunMessage) {
1925 // Our port will act as the "remote" port. 1924 // Our port will act as the "remote" port.
1926 std::unique_ptr<TestPort> port(CreateTestPort(kLocalAddr2, "rfrag", "rpass")); 1925 std::unique_ptr<TestPort> port(CreateTestPort(kLocalAddr2, "rfrag", "rpass"));
1927 1926
1928 std::unique_ptr<IceMessage> in_msg, out_msg; 1927 std::unique_ptr<IceMessage> in_msg, out_msg;
1929 std::unique_ptr<ByteBufferWriter> buf(new ByteBufferWriter()); 1928 std::unique_ptr<ByteBufferWriter> buf(new ByteBufferWriter());
1930 rtc::SocketAddress addr(kLocalAddr1); 1929 rtc::SocketAddress addr(kLocalAddr1);
1931 std::string username; 1930 std::string username;
1932 1931
1933 // BINDING-REQUEST from local to remote with valid ICE username, 1932 // BINDING-REQUEST from local to remote with valid ICE username,
1934 // MESSAGE-INTEGRITY, and FINGERPRINT. 1933 // MESSAGE-INTEGRITY, and FINGERPRINT.
1935 in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST, 1934 in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST,
1936 "rfrag:lfrag")); 1935 "rfrag:lfrag"));
1937 in_msg->AddMessageIntegrity("rpass"); 1936 in_msg->AddMessageIntegrity("rpass");
1938 in_msg->AddFingerprint(); 1937 in_msg->AddFingerprint();
1939 WriteStunMessage(in_msg.get(), buf.get()); 1938 WriteStunMessage(in_msg.get(), buf.get());
1940 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, 1939 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg,
1941 &username)); 1940 &username));
1942 EXPECT_TRUE(out_msg.get() != NULL); 1941 EXPECT_TRUE(out_msg.get() != nullptr);
1943 EXPECT_EQ("lfrag", username); 1942 EXPECT_EQ("lfrag", username);
1944 1943
1945 // BINDING-RESPONSE without username, with MESSAGE-INTEGRITY and FINGERPRINT. 1944 // BINDING-RESPONSE without username, with MESSAGE-INTEGRITY and FINGERPRINT.
1946 in_msg.reset(CreateStunMessage(STUN_BINDING_RESPONSE)); 1945 in_msg.reset(CreateStunMessage(STUN_BINDING_RESPONSE));
1947 in_msg->AddAttribute( 1946 in_msg->AddAttribute(
1948 new StunXorAddressAttribute(STUN_ATTR_XOR_MAPPED_ADDRESS, kLocalAddr2)); 1947 new StunXorAddressAttribute(STUN_ATTR_XOR_MAPPED_ADDRESS, kLocalAddr2));
1949 in_msg->AddMessageIntegrity("rpass"); 1948 in_msg->AddMessageIntegrity("rpass");
1950 in_msg->AddFingerprint(); 1949 in_msg->AddFingerprint();
1951 WriteStunMessage(in_msg.get(), buf.get()); 1950 WriteStunMessage(in_msg.get(), buf.get());
1952 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, 1951 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg,
1953 &username)); 1952 &username));
1954 EXPECT_TRUE(out_msg.get() != NULL); 1953 EXPECT_TRUE(out_msg.get() != nullptr);
1955 EXPECT_EQ("", username); 1954 EXPECT_EQ("", username);
1956 1955
1957 // BINDING-ERROR-RESPONSE without username, with error, M-I, and FINGERPRINT. 1956 // BINDING-ERROR-RESPONSE without username, with error, M-I, and FINGERPRINT.
1958 in_msg.reset(CreateStunMessage(STUN_BINDING_ERROR_RESPONSE)); 1957 in_msg.reset(CreateStunMessage(STUN_BINDING_ERROR_RESPONSE));
1959 in_msg->AddAttribute(new StunErrorCodeAttribute(STUN_ATTR_ERROR_CODE, 1958 in_msg->AddAttribute(new StunErrorCodeAttribute(STUN_ATTR_ERROR_CODE,
1960 STUN_ERROR_SERVER_ERROR, STUN_ERROR_REASON_SERVER_ERROR)); 1959 STUN_ERROR_SERVER_ERROR, STUN_ERROR_REASON_SERVER_ERROR));
1961 in_msg->AddFingerprint(); 1960 in_msg->AddFingerprint();
1962 WriteStunMessage(in_msg.get(), buf.get()); 1961 WriteStunMessage(in_msg.get(), buf.get());
1963 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, 1962 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg,
1964 &username)); 1963 &username));
1965 EXPECT_TRUE(out_msg.get() != NULL); 1964 EXPECT_TRUE(out_msg.get() != nullptr);
1966 EXPECT_EQ("", username); 1965 EXPECT_EQ("", username);
1967 ASSERT_TRUE(out_msg->GetErrorCode() != NULL); 1966 ASSERT_TRUE(out_msg->GetErrorCode() != nullptr);
1968 EXPECT_EQ(STUN_ERROR_SERVER_ERROR, out_msg->GetErrorCode()->code()); 1967 EXPECT_EQ(STUN_ERROR_SERVER_ERROR, out_msg->GetErrorCode()->code());
1969 EXPECT_EQ(std::string(STUN_ERROR_REASON_SERVER_ERROR), 1968 EXPECT_EQ(std::string(STUN_ERROR_REASON_SERVER_ERROR),
1970 out_msg->GetErrorCode()->reason()); 1969 out_msg->GetErrorCode()->reason());
1971 } 1970 }
1972 1971
1973 // Tests handling of ICE binding requests with missing or incorrect usernames. 1972 // Tests handling of ICE binding requests with missing or incorrect usernames.
1974 TEST_F(PortTest, TestHandleStunMessageBadUsername) { 1973 TEST_F(PortTest, TestHandleStunMessageBadUsername) {
1975 std::unique_ptr<TestPort> port(CreateTestPort(kLocalAddr2, "rfrag", "rpass")); 1974 std::unique_ptr<TestPort> port(CreateTestPort(kLocalAddr2, "rfrag", "rpass"));
1976 1975
1977 std::unique_ptr<IceMessage> in_msg, out_msg; 1976 std::unique_ptr<IceMessage> in_msg, out_msg;
1978 std::unique_ptr<ByteBufferWriter> buf(new ByteBufferWriter()); 1977 std::unique_ptr<ByteBufferWriter> buf(new ByteBufferWriter());
1979 rtc::SocketAddress addr(kLocalAddr1); 1978 rtc::SocketAddress addr(kLocalAddr1);
1980 std::string username; 1979 std::string username;
1981 1980
1982 // BINDING-REQUEST with no username. 1981 // BINDING-REQUEST with no username.
1983 in_msg.reset(CreateStunMessage(STUN_BINDING_REQUEST)); 1982 in_msg.reset(CreateStunMessage(STUN_BINDING_REQUEST));
1984 in_msg->AddMessageIntegrity("rpass"); 1983 in_msg->AddMessageIntegrity("rpass");
1985 in_msg->AddFingerprint(); 1984 in_msg->AddFingerprint();
1986 WriteStunMessage(in_msg.get(), buf.get()); 1985 WriteStunMessage(in_msg.get(), buf.get());
1987 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, 1986 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg,
1988 &username)); 1987 &username));
1989 EXPECT_TRUE(out_msg.get() == NULL); 1988 EXPECT_TRUE(out_msg.get() == nullptr);
1990 EXPECT_EQ("", username); 1989 EXPECT_EQ("", username);
1991 EXPECT_EQ(STUN_ERROR_BAD_REQUEST, port->last_stun_error_code()); 1990 EXPECT_EQ(STUN_ERROR_BAD_REQUEST, port->last_stun_error_code());
1992 1991
1993 // BINDING-REQUEST with empty username. 1992 // BINDING-REQUEST with empty username.
1994 in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "")); 1993 in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST, ""));
1995 in_msg->AddMessageIntegrity("rpass"); 1994 in_msg->AddMessageIntegrity("rpass");
1996 in_msg->AddFingerprint(); 1995 in_msg->AddFingerprint();
1997 WriteStunMessage(in_msg.get(), buf.get()); 1996 WriteStunMessage(in_msg.get(), buf.get());
1998 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, 1997 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg,
1999 &username)); 1998 &username));
2000 EXPECT_TRUE(out_msg.get() == NULL); 1999 EXPECT_TRUE(out_msg.get() == nullptr);
2001 EXPECT_EQ("", username); 2000 EXPECT_EQ("", username);
2002 EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code()); 2001 EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code());
2003 2002
2004 // BINDING-REQUEST with too-short username. 2003 // BINDING-REQUEST with too-short username.
2005 in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "rfra")); 2004 in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "rfra"));
2006 in_msg->AddMessageIntegrity("rpass"); 2005 in_msg->AddMessageIntegrity("rpass");
2007 in_msg->AddFingerprint(); 2006 in_msg->AddFingerprint();
2008 WriteStunMessage(in_msg.get(), buf.get()); 2007 WriteStunMessage(in_msg.get(), buf.get());
2009 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, 2008 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg,
2010 &username)); 2009 &username));
2011 EXPECT_TRUE(out_msg.get() == NULL); 2010 EXPECT_TRUE(out_msg.get() == nullptr);
2012 EXPECT_EQ("", username); 2011 EXPECT_EQ("", username);
2013 EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code()); 2012 EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code());
2014 2013
2015 // BINDING-REQUEST with reversed username. 2014 // BINDING-REQUEST with reversed username.
2016 in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST, 2015 in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST,
2017 "lfrag:rfrag")); 2016 "lfrag:rfrag"));
2018 in_msg->AddMessageIntegrity("rpass"); 2017 in_msg->AddMessageIntegrity("rpass");
2019 in_msg->AddFingerprint(); 2018 in_msg->AddFingerprint();
2020 WriteStunMessage(in_msg.get(), buf.get()); 2019 WriteStunMessage(in_msg.get(), buf.get());
2021 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, 2020 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg,
2022 &username)); 2021 &username));
2023 EXPECT_TRUE(out_msg.get() == NULL); 2022 EXPECT_TRUE(out_msg.get() == nullptr);
2024 EXPECT_EQ("", username); 2023 EXPECT_EQ("", username);
2025 EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code()); 2024 EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code());
2026 2025
2027 // BINDING-REQUEST with garbage username. 2026 // BINDING-REQUEST with garbage username.
2028 in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST, 2027 in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST,
2029 "abcd:efgh")); 2028 "abcd:efgh"));
2030 in_msg->AddMessageIntegrity("rpass"); 2029 in_msg->AddMessageIntegrity("rpass");
2031 in_msg->AddFingerprint(); 2030 in_msg->AddFingerprint();
2032 WriteStunMessage(in_msg.get(), buf.get()); 2031 WriteStunMessage(in_msg.get(), buf.get());
2033 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, 2032 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg,
2034 &username)); 2033 &username));
2035 EXPECT_TRUE(out_msg.get() == NULL); 2034 EXPECT_TRUE(out_msg.get() == nullptr);
2036 EXPECT_EQ("", username); 2035 EXPECT_EQ("", username);
2037 EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code()); 2036 EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code());
2038 } 2037 }
2039 2038
2040 // Test handling STUN messages with missing or malformed M-I. 2039 // Test handling STUN messages with missing or malformed M-I.
2041 TEST_F(PortTest, TestHandleStunMessageBadMessageIntegrity) { 2040 TEST_F(PortTest, TestHandleStunMessageBadMessageIntegrity) {
2042 // Our port will act as the "remote" port. 2041 // Our port will act as the "remote" port.
2043 std::unique_ptr<TestPort> port(CreateTestPort(kLocalAddr2, "rfrag", "rpass")); 2042 std::unique_ptr<TestPort> port(CreateTestPort(kLocalAddr2, "rfrag", "rpass"));
2044 2043
2045 std::unique_ptr<IceMessage> in_msg, out_msg; 2044 std::unique_ptr<IceMessage> in_msg, out_msg;
2046 std::unique_ptr<ByteBufferWriter> buf(new ByteBufferWriter()); 2045 std::unique_ptr<ByteBufferWriter> buf(new ByteBufferWriter());
2047 rtc::SocketAddress addr(kLocalAddr1); 2046 rtc::SocketAddress addr(kLocalAddr1);
2048 std::string username; 2047 std::string username;
2049 2048
2050 // BINDING-REQUEST from local to remote with valid ICE username and 2049 // BINDING-REQUEST from local to remote with valid ICE username and
2051 // FINGERPRINT, but no MESSAGE-INTEGRITY. 2050 // FINGERPRINT, but no MESSAGE-INTEGRITY.
2052 in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST, 2051 in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST,
2053 "rfrag:lfrag")); 2052 "rfrag:lfrag"));
2054 in_msg->AddFingerprint(); 2053 in_msg->AddFingerprint();
2055 WriteStunMessage(in_msg.get(), buf.get()); 2054 WriteStunMessage(in_msg.get(), buf.get());
2056 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, 2055 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg,
2057 &username)); 2056 &username));
2058 EXPECT_TRUE(out_msg.get() == NULL); 2057 EXPECT_TRUE(out_msg.get() == nullptr);
2059 EXPECT_EQ("", username); 2058 EXPECT_EQ("", username);
2060 EXPECT_EQ(STUN_ERROR_BAD_REQUEST, port->last_stun_error_code()); 2059 EXPECT_EQ(STUN_ERROR_BAD_REQUEST, port->last_stun_error_code());
2061 2060
2062 // BINDING-REQUEST from local to remote with valid ICE username and 2061 // BINDING-REQUEST from local to remote with valid ICE username and
2063 // FINGERPRINT, but invalid MESSAGE-INTEGRITY. 2062 // FINGERPRINT, but invalid MESSAGE-INTEGRITY.
2064 in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST, 2063 in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST,
2065 "rfrag:lfrag")); 2064 "rfrag:lfrag"));
2066 in_msg->AddMessageIntegrity("invalid"); 2065 in_msg->AddMessageIntegrity("invalid");
2067 in_msg->AddFingerprint(); 2066 in_msg->AddFingerprint();
2068 WriteStunMessage(in_msg.get(), buf.get()); 2067 WriteStunMessage(in_msg.get(), buf.get());
2069 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, 2068 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg,
2070 &username)); 2069 &username));
2071 EXPECT_TRUE(out_msg.get() == NULL); 2070 EXPECT_TRUE(out_msg.get() == nullptr);
2072 EXPECT_EQ("", username); 2071 EXPECT_EQ("", username);
2073 EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code()); 2072 EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code());
2074 2073
2075 // TODO: BINDING-RESPONSES and BINDING-ERROR-RESPONSES are checked 2074 // TODO: BINDING-RESPONSES and BINDING-ERROR-RESPONSES are checked
2076 // by the Connection, not the Port, since they require the remote username. 2075 // by the Connection, not the Port, since they require the remote username.
2077 // Change this test to pass in data via Connection::OnReadPacket instead. 2076 // Change this test to pass in data via Connection::OnReadPacket instead.
2078 } 2077 }
2079 2078
2080 // Test handling STUN messages with missing or malformed FINGERPRINT. 2079 // Test handling STUN messages with missing or malformed FINGERPRINT.
2081 TEST_F(PortTest, TestHandleStunMessageBadFingerprint) { 2080 TEST_F(PortTest, TestHandleStunMessageBadFingerprint) {
(...skipping 72 matching lines...)
2154 std::unique_ptr<IceMessage> in_msg, out_msg; 2153 std::unique_ptr<IceMessage> in_msg, out_msg;
2155 std::unique_ptr<ByteBufferWriter> buf(new ByteBufferWriter()); 2154 std::unique_ptr<ByteBufferWriter> buf(new ByteBufferWriter());
2156 rtc::SocketAddress addr(kLocalAddr1); 2155 rtc::SocketAddress addr(kLocalAddr1);
2157 std::string username; 2156 std::string username;
2158 2157
2159 in_msg.reset(CreateStunMessage(STUN_BINDING_INDICATION)); 2158 in_msg.reset(CreateStunMessage(STUN_BINDING_INDICATION));
2160 in_msg->AddFingerprint(); 2159 in_msg->AddFingerprint();
2161 WriteStunMessage(in_msg.get(), buf.get()); 2160 WriteStunMessage(in_msg.get(), buf.get());
2162 EXPECT_TRUE(lport->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, 2161 EXPECT_TRUE(lport->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg,
2163 &username)); 2162 &username));
2164 EXPECT_TRUE(out_msg.get() != NULL); 2163 EXPECT_TRUE(out_msg.get() != nullptr);
2165 EXPECT_EQ(out_msg->type(), STUN_BINDING_INDICATION); 2164 EXPECT_EQ(out_msg->type(), STUN_BINDING_INDICATION);
2166 EXPECT_EQ("", username); 2165 EXPECT_EQ("", username);
2167 2166
2168 // Verify connection can handle STUN indication and updates 2167 // Verify connection can handle STUN indication and updates
2169 // last_ping_received. 2168 // last_ping_received.
2170 std::unique_ptr<TestPort> rport( 2169 std::unique_ptr<TestPort> rport(
2171 CreateTestPort(kLocalAddr2, "rfrag", "rpass")); 2170 CreateTestPort(kLocalAddr2, "rfrag", "rpass"));
2172 rport->SetIceRole(cricket::ICEROLE_CONTROLLED); 2171 rport->SetIceRole(cricket::ICEROLE_CONTROLLED);
2173 rport->SetIceTiebreaker(kTiebreaker2); 2172 rport->SetIceTiebreaker(kTiebreaker2);
2174 2173
2175 lport->PrepareAddress(); 2174 lport->PrepareAddress();
2176 rport->PrepareAddress(); 2175 rport->PrepareAddress();
2177 ASSERT_FALSE(lport->Candidates().empty()); 2176 ASSERT_FALSE(lport->Candidates().empty());
2178 ASSERT_FALSE(rport->Candidates().empty()); 2177 ASSERT_FALSE(rport->Candidates().empty());
2179 2178
2180 Connection* lconn = lport->CreateConnection(rport->Candidates()[0], 2179 Connection* lconn = lport->CreateConnection(rport->Candidates()[0],
2181 Port::ORIGIN_MESSAGE); 2180 Port::ORIGIN_MESSAGE);
2182 Connection* rconn = rport->CreateConnection(lport->Candidates()[0], 2181 Connection* rconn = rport->CreateConnection(lport->Candidates()[0],
2183 Port::ORIGIN_MESSAGE); 2182 Port::ORIGIN_MESSAGE);
2184 rconn->Ping(0); 2183 rconn->Ping(0);
2185 2184
2186 ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, kDefaultTimeout); 2185 ASSERT_TRUE_WAIT(rport->last_stun_msg() != nullptr, kDefaultTimeout);
2187 IceMessage* msg = rport->last_stun_msg(); 2186 IceMessage* msg = rport->last_stun_msg();
2188 EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); 2187 EXPECT_EQ(STUN_BINDING_REQUEST, msg->type());
2189 // Send rport binding request to lport. 2188 // Send rport binding request to lport.
2190 lconn->OnReadPacket(rport->last_stun_buf()->data<char>(), 2189 lconn->OnReadPacket(rport->last_stun_buf()->data<char>(),
2191 rport->last_stun_buf()->size(), 2190 rport->last_stun_buf()->size(),
2192 rtc::PacketTime()); 2191 rtc::PacketTime());
2193 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); 2192 ASSERT_TRUE_WAIT(lport->last_stun_msg() != nullptr, kDefaultTimeout);
2194 EXPECT_EQ(STUN_BINDING_RESPONSE, lport->last_stun_msg()->type()); 2193 EXPECT_EQ(STUN_BINDING_RESPONSE, lport->last_stun_msg()->type());
2195 int64_t last_ping_received1 = lconn->last_ping_received(); 2194 int64_t last_ping_received1 = lconn->last_ping_received();
2196 2195
2197 // Adding a delay of 100ms. 2196 // Adding a delay of 100ms.
2198 rtc::Thread::Current()->ProcessMessages(100); 2197 rtc::Thread::Current()->ProcessMessages(100);
2199 // Pinging lconn using stun indication message. 2198 // Pinging lconn using stun indication message.
2200 lconn->OnReadPacket(buf->Data(), buf->Length(), rtc::PacketTime()); 2199 lconn->OnReadPacket(buf->Data(), buf->Length(), rtc::PacketTime());
2201 int64_t last_ping_received2 = lconn->last_ping_received(); 2200 int64_t last_ping_received2 = lconn->last_ping_received();
2202 EXPECT_GT(last_ping_received2, last_ping_received1); 2201 EXPECT_GT(last_ping_received2, last_ping_received1);
2203 } 2202 }
(...skipping 234 matching lines...)
2438 TestChannel ch2(port2); 2437 TestChannel ch2(port2);
2439 2438
2440 // Acquire addresses. 2439 // Acquire addresses.
2441 ch1.Start(); 2440 ch1.Start();
2442 ch2.Start(); 2441 ch2.Start();
2443 ASSERT_EQ_SIMULATED_WAIT(1, ch1.complete_count(), kDefaultTimeout, clock); 2442 ASSERT_EQ_SIMULATED_WAIT(1, ch1.complete_count(), kDefaultTimeout, clock);
2444 ASSERT_EQ_SIMULATED_WAIT(1, ch2.complete_count(), kDefaultTimeout, clock); 2443 ASSERT_EQ_SIMULATED_WAIT(1, ch2.complete_count(), kDefaultTimeout, clock);
2445 2444
2446 // Send a ping from src to dst. 2445 // Send a ping from src to dst.
2447 ch1.CreateConnection(GetCandidate(port2)); 2446 ch1.CreateConnection(GetCandidate(port2));
2448 ASSERT_TRUE(ch1.conn() != NULL); 2447 ASSERT_TRUE(ch1.conn() != nullptr);
2449 EXPECT_EQ(Connection::STATE_WRITE_INIT, ch1.conn()->write_state()); 2448 EXPECT_EQ(Connection::STATE_WRITE_INIT, ch1.conn()->write_state());
2450 // for TCP connect 2449 // for TCP connect
2451 EXPECT_TRUE_SIMULATED_WAIT(ch1.conn()->connected(), kDefaultTimeout, clock); 2450 EXPECT_TRUE_SIMULATED_WAIT(ch1.conn()->connected(), kDefaultTimeout, clock);
2452 ch1.Ping(); 2451 ch1.Ping();
2453 SIMULATED_WAIT(!ch2.remote_address().IsNil(), kShortTimeout, clock); 2452 SIMULATED_WAIT(!ch2.remote_address().IsNil(), kShortTimeout, clock);
2454 2453
2455 // Data should be sendable before the connection is accepted. 2454 // Data should be sendable before the connection is accepted.
2456 char data[] = "abcd"; 2455 char data[] = "abcd";
2457 int data_size = arraysize(data); 2456 int data_size = arraysize(data);
2458 rtc::PacketOptions options; 2457 rtc::PacketOptions options;
(...skipping 49 matching lines...)
2508 2507
2509 // Set up channels. 2508 // Set up channels.
2510 TestChannel ch1(port1); 2509 TestChannel ch1(port1);
2511 TestChannel ch2(port2); 2510 TestChannel ch2(port2);
2512 2511
2513 // Acquire addresses. 2512 // Acquire addresses.
2514 ch1.Start(); 2513 ch1.Start();
2515 ch2.Start(); 2514 ch2.Start();
2516 2515
2517 ch1.CreateConnection(GetCandidate(port2)); 2516 ch1.CreateConnection(GetCandidate(port2));
2518 ASSERT_TRUE(ch1.conn() != NULL); 2517 ASSERT_TRUE(ch1.conn() != nullptr);
2519 EXPECT_EQ(Connection::STATE_WRITE_INIT, ch1.conn()->write_state()); 2518 EXPECT_EQ(Connection::STATE_WRITE_INIT, ch1.conn()->write_state());
2520 2519
2521 // Attempt to go directly to write timeout. 2520 // Attempt to go directly to write timeout.
2522 for (uint32_t i = 1; i <= CONNECTION_WRITE_CONNECT_FAILURES; ++i) { 2521 for (uint32_t i = 1; i <= CONNECTION_WRITE_CONNECT_FAILURES; ++i) {
2523 ch1.Ping(i); 2522 ch1.Ping(i);
2524 } 2523 }
2525 ch1.conn()->UpdateState(CONNECTION_WRITE_TIMEOUT + 500u); 2524 ch1.conn()->UpdateState(CONNECTION_WRITE_TIMEOUT + 500u);
2526 EXPECT_EQ(Connection::STATE_WRITE_TIMEOUT, ch1.conn()->write_state()); 2525 EXPECT_EQ(Connection::STATE_WRITE_TIMEOUT, ch1.conn()->write_state());
2527 } 2526 }
2528 2527
(...skipping 14 matching lines...)
2543 ch1.SetIceMode(ICEMODE_FULL); 2542 ch1.SetIceMode(ICEMODE_FULL);
2544 2543
2545 // Start gathering candidates. 2544 // Start gathering candidates.
2546 ch1.Start(); 2545 ch1.Start();
2547 ice_lite_port->PrepareAddress(); 2546 ice_lite_port->PrepareAddress();
2548 2547
2549 ASSERT_EQ_WAIT(1, ch1.complete_count(), kDefaultTimeout); 2548 ASSERT_EQ_WAIT(1, ch1.complete_count(), kDefaultTimeout);
2550 ASSERT_FALSE(ice_lite_port->Candidates().empty()); 2549 ASSERT_FALSE(ice_lite_port->Candidates().empty());
2551 2550
2552 ch1.CreateConnection(GetCandidate(ice_lite_port.get())); 2551 ch1.CreateConnection(GetCandidate(ice_lite_port.get()));
2553 ASSERT_TRUE(ch1.conn() != NULL); 2552 ASSERT_TRUE(ch1.conn() != nullptr);
2554 EXPECT_EQ(Connection::STATE_WRITE_INIT, ch1.conn()->write_state()); 2553 EXPECT_EQ(Connection::STATE_WRITE_INIT, ch1.conn()->write_state());
2555 2554
2556 // Send ping from full mode client. 2555 // Send ping from full mode client.
2557 // This ping must not have USE_CANDIDATE_ATTR. 2556 // This ping must not have USE_CANDIDATE_ATTR.
2558 ch1.Ping(); 2557 ch1.Ping();
2559 2558
2560 // Verify stun ping is without USE_CANDIDATE_ATTR. Getting message directly 2559 // Verify stun ping is without USE_CANDIDATE_ATTR. Getting message directly
2561 // from port. 2560 // from port.
2562 ASSERT_TRUE_WAIT(ice_full_port->last_stun_msg() != NULL, kDefaultTimeout); 2561 ASSERT_TRUE_WAIT(ice_full_port->last_stun_msg() != nullptr, kDefaultTimeout);
2563 IceMessage* msg = ice_full_port->last_stun_msg(); 2562 IceMessage* msg = ice_full_port->last_stun_msg();
2564 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) == NULL); 2563 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) == nullptr);
2565 2564
2566 // Respond with a BINDING-RESPONSE from litemode client. 2565 // Respond with a BINDING-RESPONSE from litemode client.
2567 // NOTE: Ideally we should't create connection at this stage from lite 2566 // NOTE: Ideally we should't create connection at this stage from lite
2568 // port, as it should be done only after receiving ping with USE_CANDIDATE. 2567 // port, as it should be done only after receiving ping with USE_CANDIDATE.
2569 // But we need a connection to send a response message. 2568 // But we need a connection to send a response message.
2570 ice_lite_port->CreateConnection( 2569 ice_lite_port->CreateConnection(
2571 ice_full_port->Candidates()[0], cricket::Port::ORIGIN_MESSAGE); 2570 ice_full_port->Candidates()[0], cricket::Port::ORIGIN_MESSAGE);
2572 std::unique_ptr<IceMessage> request(CopyStunMessage(msg)); 2571 std::unique_ptr<IceMessage> request(CopyStunMessage(msg));
2573 ice_lite_port->SendBindingResponse( 2572 ice_lite_port->SendBindingResponse(
2574 request.get(), ice_full_port->Candidates()[0].address()); 2573 request.get(), ice_full_port->Candidates()[0].address());
2575 2574
2576 // Feeding the respone message from litemode to the full mode connection. 2575 // Feeding the respone message from litemode to the full mode connection.
2577 ch1.conn()->OnReadPacket(ice_lite_port->last_stun_buf()->data<char>(), 2576 ch1.conn()->OnReadPacket(ice_lite_port->last_stun_buf()->data<char>(),
2578 ice_lite_port->last_stun_buf()->size(), 2577 ice_lite_port->last_stun_buf()->size(),
2579 rtc::PacketTime()); 2578 rtc::PacketTime());
2580 // Verifying full mode connection becomes writable from the response. 2579 // Verifying full mode connection becomes writable from the response.
2581 EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, ch1.conn()->write_state(), 2580 EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, ch1.conn()->write_state(),
2582 kDefaultTimeout); 2581 kDefaultTimeout);
2583 EXPECT_TRUE_WAIT(ch1.nominated(), kDefaultTimeout); 2582 EXPECT_TRUE_WAIT(ch1.nominated(), kDefaultTimeout);
2584 2583
2585 // Clear existing stun messsages. Otherwise we will process old stun 2584 // Clear existing stun messsages. Otherwise we will process old stun
2586 // message right after we send ping. 2585 // message right after we send ping.
2587 ice_full_port->Reset(); 2586 ice_full_port->Reset();
2588 // Send ping. This must have USE_CANDIDATE_ATTR. 2587 // Send ping. This must have USE_CANDIDATE_ATTR.
2589 ch1.Ping(); 2588 ch1.Ping();
2590 ASSERT_TRUE_WAIT(ice_full_port->last_stun_msg() != NULL, kDefaultTimeout); 2589 ASSERT_TRUE_WAIT(ice_full_port->last_stun_msg() != nullptr, kDefaultTimeout);
2591 msg = ice_full_port->last_stun_msg(); 2590 msg = ice_full_port->last_stun_msg();
2592 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) != NULL); 2591 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) != nullptr);
2593 ch1.Stop(); 2592 ch1.Stop();
2594 } 2593 }
2595 2594
2596 // This test case verifies that both the controlling port and the controlled 2595 // This test case verifies that both the controlling port and the controlled
2597 // port will time out after connectivity is lost, if they are not marked as 2596 // port will time out after connectivity is lost, if they are not marked as
2598 // "keep alive until pruned." 2597 // "keep alive until pruned."
2599 TEST_F(PortTest, TestPortTimeoutIfNotKeptAlive) { 2598 TEST_F(PortTest, TestPortTimeoutIfNotKeptAlive) {
2600 rtc::ScopedFakeClock clock; 2599 rtc::ScopedFakeClock clock;
2601 int timeout_delay = 100; 2600 int timeout_delay = 100;
2602 UDPPort* port1 = CreateUdpPort(kLocalAddr1); 2601 UDPPort* port1 = CreateUdpPort(kLocalAddr1);
(...skipping 159 matching lines...)
2762 port->CreateConnection(candidate, Port::ORIGIN_MESSAGE); 2761 port->CreateConnection(candidate, Port::ORIGIN_MESSAGE);
2763 EXPECT_NE(conn1, conn2); 2762 EXPECT_NE(conn1, conn2);
2764 conn_in_use = port->GetConnection(address); 2763 conn_in_use = port->GetConnection(address);
2765 EXPECT_EQ(conn2, conn_in_use); 2764 EXPECT_EQ(conn2, conn_in_use);
2766 EXPECT_EQ(2u, conn_in_use->remote_candidate().generation()); 2765 EXPECT_EQ(2u, conn_in_use->remote_candidate().generation());
2767 2766
2768 // Make sure the new connection was not deleted. 2767 // Make sure the new connection was not deleted.
2769 rtc::Thread::Current()->ProcessMessages(300); 2768 rtc::Thread::Current()->ProcessMessages(300);
2770 EXPECT_TRUE(port->GetConnection(address) != nullptr); 2769 EXPECT_TRUE(port->GetConnection(address) != nullptr);
2771 } 2770 }
OLDNEW

Powered by Google App Engine