Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(54)

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

Issue 2757893003: Add MakeUnique from chromium and change StunMessage::AddAttribute to take a unique_ptr. (Closed)
Patch Set: add ptr_util.h to rtc_base_approved build target Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/p2p/base/stun.cc ('k') | webrtc/p2p/base/stunrequest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
11 #include <string> 11 #include <string>
12 12
13 #include "webrtc/p2p/base/stun.h"
14 #include "webrtc/base/arraysize.h" 13 #include "webrtc/base/arraysize.h"
15 #include "webrtc/base/bytebuffer.h" 14 #include "webrtc/base/bytebuffer.h"
16 #include "webrtc/base/gunit.h" 15 #include "webrtc/base/gunit.h"
17 #include "webrtc/base/logging.h" 16 #include "webrtc/base/logging.h"
18 #include "webrtc/base/messagedigest.h" 17 #include "webrtc/base/messagedigest.h"
18 #include "webrtc/base/ptr_util.h"
19 #include "webrtc/base/socketaddress.h" 19 #include "webrtc/base/socketaddress.h"
20 #include "webrtc/p2p/base/stun.h"
20 21
21 namespace cricket { 22 namespace cricket {
22 23
23 class StunTest : public ::testing::Test { 24 class StunTest : public ::testing::Test {
24 protected: 25 protected:
25 void CheckStunHeader(const StunMessage& msg, StunMessageType expected_type, 26 void CheckStunHeader(const StunMessage& msg, StunMessageType expected_type,
26 size_t expected_length) { 27 size_t expected_length) {
27 ASSERT_EQ(expected_type, msg.type()); 28 ASSERT_EQ(expected_type, msg.type());
28 ASSERT_EQ(expected_length, msg.length()); 29 ASSERT_EQ(expected_length, msg.length());
29 } 30 }
(...skipping 785 matching lines...) Expand 10 before | Expand all | Expand 10 after
815 // However, no owner is still an error, should fail and write nothing. 816 // However, no owner is still an error, should fail and write nothing.
816 addr2.SetOwner(NULL); 817 addr2.SetOwner(NULL);
817 ASSERT_EQ(addr2.ipaddr(), addr->ipaddr()); 818 ASSERT_EQ(addr2.ipaddr(), addr->ipaddr());
818 wrong_buf.Clear(); 819 wrong_buf.Clear();
819 EXPECT_FALSE(addr2.Write(&wrong_buf)); 820 EXPECT_FALSE(addr2.Write(&wrong_buf));
820 } 821 }
821 822
822 TEST_F(StunTest, CreateIPv6AddressAttribute) { 823 TEST_F(StunTest, CreateIPv6AddressAttribute) {
823 rtc::IPAddress test_ip(kIPv6TestAddress2); 824 rtc::IPAddress test_ip(kIPv6TestAddress2);
824 825
825 StunAddressAttribute* addr = 826 auto addr = StunAttribute::CreateAddress(STUN_ATTR_MAPPED_ADDRESS);
826 StunAttribute::CreateAddress(STUN_ATTR_MAPPED_ADDRESS);
827 rtc::SocketAddress test_addr(test_ip, kTestMessagePort2); 827 rtc::SocketAddress test_addr(test_ip, kTestMessagePort2);
828 addr->SetAddress(test_addr); 828 addr->SetAddress(test_addr);
829 829
830 CheckStunAddressAttribute(addr, STUN_ADDRESS_IPV6, 830 CheckStunAddressAttribute(addr.get(), STUN_ADDRESS_IPV6, kTestMessagePort2,
831 kTestMessagePort2, test_ip); 831 test_ip);
832 delete addr;
833 } 832 }
834 833
835 TEST_F(StunTest, CreateIPv4AddressAttribute) { 834 TEST_F(StunTest, CreateIPv4AddressAttribute) {
836 struct in_addr test_in_addr; 835 struct in_addr test_in_addr;
837 test_in_addr.s_addr = 0xBEB0B0BE; 836 test_in_addr.s_addr = 0xBEB0B0BE;
838 rtc::IPAddress test_ip(test_in_addr); 837 rtc::IPAddress test_ip(test_in_addr);
839 838
840 StunAddressAttribute* addr = 839 auto addr = StunAttribute::CreateAddress(STUN_ATTR_MAPPED_ADDRESS);
841 StunAttribute::CreateAddress(STUN_ATTR_MAPPED_ADDRESS);
842 rtc::SocketAddress test_addr(test_ip, kTestMessagePort2); 840 rtc::SocketAddress test_addr(test_ip, kTestMessagePort2);
843 addr->SetAddress(test_addr); 841 addr->SetAddress(test_addr);
844 842
845 CheckStunAddressAttribute(addr, STUN_ADDRESS_IPV4, 843 CheckStunAddressAttribute(addr.get(), STUN_ADDRESS_IPV4, kTestMessagePort2,
846 kTestMessagePort2, test_ip); 844 test_ip);
847 delete addr;
848 } 845 }
849 846
850 // Test that we don't care what order we set the parts of an address 847 // Test that we don't care what order we set the parts of an address
851 TEST_F(StunTest, CreateAddressInArbitraryOrder) { 848 TEST_F(StunTest, CreateAddressInArbitraryOrder) {
852 StunAddressAttribute* addr = 849 auto addr = StunAttribute::CreateAddress(STUN_ATTR_DESTINATION_ADDRESS);
853 StunAttribute::CreateAddress(STUN_ATTR_DESTINATION_ADDRESS);
854 // Port first 850 // Port first
855 addr->SetPort(kTestMessagePort1); 851 addr->SetPort(kTestMessagePort1);
856 addr->SetIP(rtc::IPAddress(kIPv4TestAddress1)); 852 addr->SetIP(rtc::IPAddress(kIPv4TestAddress1));
857 ASSERT_EQ(kTestMessagePort1, addr->port()); 853 ASSERT_EQ(kTestMessagePort1, addr->port());
858 ASSERT_EQ(rtc::IPAddress(kIPv4TestAddress1), addr->ipaddr()); 854 ASSERT_EQ(rtc::IPAddress(kIPv4TestAddress1), addr->ipaddr());
859 855
860 StunAddressAttribute* addr2 = 856 auto addr2 = StunAttribute::CreateAddress(STUN_ATTR_DESTINATION_ADDRESS);
861 StunAttribute::CreateAddress(STUN_ATTR_DESTINATION_ADDRESS);
862 // IP first 857 // IP first
863 addr2->SetIP(rtc::IPAddress(kIPv4TestAddress1)); 858 addr2->SetIP(rtc::IPAddress(kIPv4TestAddress1));
864 addr2->SetPort(kTestMessagePort2); 859 addr2->SetPort(kTestMessagePort2);
865 ASSERT_EQ(kTestMessagePort2, addr2->port()); 860 ASSERT_EQ(kTestMessagePort2, addr2->port());
866 ASSERT_EQ(rtc::IPAddress(kIPv4TestAddress1), addr2->ipaddr()); 861 ASSERT_EQ(rtc::IPAddress(kIPv4TestAddress1), addr2->ipaddr());
867
868 delete addr;
869 delete addr2;
870 } 862 }
871 863
872 TEST_F(StunTest, WriteMessageWithIPv6AddressAttribute) { 864 TEST_F(StunTest, WriteMessageWithIPv6AddressAttribute) {
873 StunMessage msg; 865 StunMessage msg;
874 size_t size = sizeof(kStunMessageWithIPv6MappedAddress); 866 size_t size = sizeof(kStunMessageWithIPv6MappedAddress);
875 867
876 rtc::IPAddress test_ip(kIPv6TestAddress1); 868 rtc::IPAddress test_ip(kIPv6TestAddress1);
877 869
878 msg.SetType(STUN_BINDING_REQUEST); 870 msg.SetType(STUN_BINDING_REQUEST);
879 msg.SetTransactionID( 871 msg.SetTransactionID(
880 std::string(reinterpret_cast<const char*>(kTestTransactionId1), 872 std::string(reinterpret_cast<const char*>(kTestTransactionId1),
881 kStunTransactionIdLength)); 873 kStunTransactionIdLength));
882 CheckStunTransactionID(msg, kTestTransactionId1, kStunTransactionIdLength); 874 CheckStunTransactionID(msg, kTestTransactionId1, kStunTransactionIdLength);
883 875
884 StunAddressAttribute* addr = 876 auto addr = StunAttribute::CreateAddress(STUN_ATTR_MAPPED_ADDRESS);
885 StunAttribute::CreateAddress(STUN_ATTR_MAPPED_ADDRESS);
886 rtc::SocketAddress test_addr(test_ip, kTestMessagePort2); 877 rtc::SocketAddress test_addr(test_ip, kTestMessagePort2);
887 addr->SetAddress(test_addr); 878 addr->SetAddress(test_addr);
888 msg.AddAttribute(addr); 879 msg.AddAttribute(std::move(addr));
889 880
890 CheckStunHeader(msg, STUN_BINDING_REQUEST, (size - 20)); 881 CheckStunHeader(msg, STUN_BINDING_REQUEST, (size - 20));
891 882
892 rtc::ByteBufferWriter out; 883 rtc::ByteBufferWriter out;
893 EXPECT_TRUE(msg.Write(&out)); 884 EXPECT_TRUE(msg.Write(&out));
894 ASSERT_EQ(out.Length(), sizeof(kStunMessageWithIPv6MappedAddress)); 885 ASSERT_EQ(out.Length(), sizeof(kStunMessageWithIPv6MappedAddress));
895 int len1 = static_cast<int>(out.Length()); 886 int len1 = static_cast<int>(out.Length());
896 rtc::ByteBufferReader read_buf(out); 887 rtc::ByteBufferReader read_buf(out);
897 std::string bytes; 888 std::string bytes;
898 read_buf.ReadString(&bytes, len1); 889 read_buf.ReadString(&bytes, len1);
899 ASSERT_EQ(0, memcmp(bytes.c_str(), kStunMessageWithIPv6MappedAddress, len1)); 890 ASSERT_EQ(0, memcmp(bytes.c_str(), kStunMessageWithIPv6MappedAddress, len1));
900 } 891 }
901 892
902 TEST_F(StunTest, WriteMessageWithIPv4AddressAttribute) { 893 TEST_F(StunTest, WriteMessageWithIPv4AddressAttribute) {
903 StunMessage msg; 894 StunMessage msg;
904 size_t size = sizeof(kStunMessageWithIPv4MappedAddress); 895 size_t size = sizeof(kStunMessageWithIPv4MappedAddress);
905 896
906 rtc::IPAddress test_ip(kIPv4TestAddress1); 897 rtc::IPAddress test_ip(kIPv4TestAddress1);
907 898
908 msg.SetType(STUN_BINDING_RESPONSE); 899 msg.SetType(STUN_BINDING_RESPONSE);
909 msg.SetTransactionID( 900 msg.SetTransactionID(
910 std::string(reinterpret_cast<const char*>(kTestTransactionId1), 901 std::string(reinterpret_cast<const char*>(kTestTransactionId1),
911 kStunTransactionIdLength)); 902 kStunTransactionIdLength));
912 CheckStunTransactionID(msg, kTestTransactionId1, kStunTransactionIdLength); 903 CheckStunTransactionID(msg, kTestTransactionId1, kStunTransactionIdLength);
913 904
914 StunAddressAttribute* addr = 905 auto addr = StunAttribute::CreateAddress(STUN_ATTR_MAPPED_ADDRESS);
915 StunAttribute::CreateAddress(STUN_ATTR_MAPPED_ADDRESS);
916 rtc::SocketAddress test_addr(test_ip, kTestMessagePort4); 906 rtc::SocketAddress test_addr(test_ip, kTestMessagePort4);
917 addr->SetAddress(test_addr); 907 addr->SetAddress(test_addr);
918 msg.AddAttribute(addr); 908 msg.AddAttribute(std::move(addr));
919 909
920 CheckStunHeader(msg, STUN_BINDING_RESPONSE, (size - 20)); 910 CheckStunHeader(msg, STUN_BINDING_RESPONSE, (size - 20));
921 911
922 rtc::ByteBufferWriter out; 912 rtc::ByteBufferWriter out;
923 EXPECT_TRUE(msg.Write(&out)); 913 EXPECT_TRUE(msg.Write(&out));
924 ASSERT_EQ(out.Length(), sizeof(kStunMessageWithIPv4MappedAddress)); 914 ASSERT_EQ(out.Length(), sizeof(kStunMessageWithIPv4MappedAddress));
925 int len1 = static_cast<int>(out.Length()); 915 int len1 = static_cast<int>(out.Length());
926 rtc::ByteBufferReader read_buf(out); 916 rtc::ByteBufferReader read_buf(out);
927 std::string bytes; 917 std::string bytes;
928 read_buf.ReadString(&bytes, len1); 918 read_buf.ReadString(&bytes, len1);
929 ASSERT_EQ(0, memcmp(bytes.c_str(), kStunMessageWithIPv4MappedAddress, len1)); 919 ASSERT_EQ(0, memcmp(bytes.c_str(), kStunMessageWithIPv4MappedAddress, len1));
930 } 920 }
931 921
932 TEST_F(StunTest, WriteMessageWithIPv6XorAddressAttribute) { 922 TEST_F(StunTest, WriteMessageWithIPv6XorAddressAttribute) {
933 StunMessage msg; 923 StunMessage msg;
934 size_t size = sizeof(kStunMessageWithIPv6XorMappedAddress); 924 size_t size = sizeof(kStunMessageWithIPv6XorMappedAddress);
935 925
936 rtc::IPAddress test_ip(kIPv6TestAddress1); 926 rtc::IPAddress test_ip(kIPv6TestAddress1);
937 927
938 msg.SetType(STUN_BINDING_RESPONSE); 928 msg.SetType(STUN_BINDING_RESPONSE);
939 msg.SetTransactionID( 929 msg.SetTransactionID(
940 std::string(reinterpret_cast<const char*>(kTestTransactionId2), 930 std::string(reinterpret_cast<const char*>(kTestTransactionId2),
941 kStunTransactionIdLength)); 931 kStunTransactionIdLength));
942 CheckStunTransactionID(msg, kTestTransactionId2, kStunTransactionIdLength); 932 CheckStunTransactionID(msg, kTestTransactionId2, kStunTransactionIdLength);
943 933
944 StunAddressAttribute* addr = 934 auto addr = StunAttribute::CreateXorAddress(STUN_ATTR_XOR_MAPPED_ADDRESS);
945 StunAttribute::CreateXorAddress(STUN_ATTR_XOR_MAPPED_ADDRESS);
946 rtc::SocketAddress test_addr(test_ip, kTestMessagePort1); 935 rtc::SocketAddress test_addr(test_ip, kTestMessagePort1);
947 addr->SetAddress(test_addr); 936 addr->SetAddress(test_addr);
948 msg.AddAttribute(addr); 937 msg.AddAttribute(std::move(addr));
949 938
950 CheckStunHeader(msg, STUN_BINDING_RESPONSE, (size - 20)); 939 CheckStunHeader(msg, STUN_BINDING_RESPONSE, (size - 20));
951 940
952 rtc::ByteBufferWriter out; 941 rtc::ByteBufferWriter out;
953 EXPECT_TRUE(msg.Write(&out)); 942 EXPECT_TRUE(msg.Write(&out));
954 ASSERT_EQ(out.Length(), sizeof(kStunMessageWithIPv6XorMappedAddress)); 943 ASSERT_EQ(out.Length(), sizeof(kStunMessageWithIPv6XorMappedAddress));
955 int len1 = static_cast<int>(out.Length()); 944 int len1 = static_cast<int>(out.Length());
956 rtc::ByteBufferReader read_buf(out); 945 rtc::ByteBufferReader read_buf(out);
957 std::string bytes; 946 std::string bytes;
958 read_buf.ReadString(&bytes, len1); 947 read_buf.ReadString(&bytes, len1);
959 ASSERT_EQ(0, 948 ASSERT_EQ(0,
960 memcmp(bytes.c_str(), kStunMessageWithIPv6XorMappedAddress, len1)); 949 memcmp(bytes.c_str(), kStunMessageWithIPv6XorMappedAddress, len1));
961 } 950 }
962 951
963 TEST_F(StunTest, WriteMessageWithIPv4XoreAddressAttribute) { 952 TEST_F(StunTest, WriteMessageWithIPv4XoreAddressAttribute) {
964 StunMessage msg; 953 StunMessage msg;
965 size_t size = sizeof(kStunMessageWithIPv4XorMappedAddress); 954 size_t size = sizeof(kStunMessageWithIPv4XorMappedAddress);
966 955
967 rtc::IPAddress test_ip(kIPv4TestAddress1); 956 rtc::IPAddress test_ip(kIPv4TestAddress1);
968 957
969 msg.SetType(STUN_BINDING_RESPONSE); 958 msg.SetType(STUN_BINDING_RESPONSE);
970 msg.SetTransactionID( 959 msg.SetTransactionID(
971 std::string(reinterpret_cast<const char*>(kTestTransactionId1), 960 std::string(reinterpret_cast<const char*>(kTestTransactionId1),
972 kStunTransactionIdLength)); 961 kStunTransactionIdLength));
973 CheckStunTransactionID(msg, kTestTransactionId1, kStunTransactionIdLength); 962 CheckStunTransactionID(msg, kTestTransactionId1, kStunTransactionIdLength);
974 963
975 StunAddressAttribute* addr = 964 auto addr = StunAttribute::CreateXorAddress(STUN_ATTR_XOR_MAPPED_ADDRESS);
976 StunAttribute::CreateXorAddress(STUN_ATTR_XOR_MAPPED_ADDRESS);
977 rtc::SocketAddress test_addr(test_ip, kTestMessagePort3); 965 rtc::SocketAddress test_addr(test_ip, kTestMessagePort3);
978 addr->SetAddress(test_addr); 966 addr->SetAddress(test_addr);
979 msg.AddAttribute(addr); 967 msg.AddAttribute(std::move(addr));
980 968
981 CheckStunHeader(msg, STUN_BINDING_RESPONSE, (size - 20)); 969 CheckStunHeader(msg, STUN_BINDING_RESPONSE, (size - 20));
982 970
983 rtc::ByteBufferWriter out; 971 rtc::ByteBufferWriter out;
984 EXPECT_TRUE(msg.Write(&out)); 972 EXPECT_TRUE(msg.Write(&out));
985 ASSERT_EQ(out.Length(), sizeof(kStunMessageWithIPv4XorMappedAddress)); 973 ASSERT_EQ(out.Length(), sizeof(kStunMessageWithIPv4XorMappedAddress));
986 int len1 = static_cast<int>(out.Length()); 974 int len1 = static_cast<int>(out.Length());
987 rtc::ByteBufferReader read_buf(out); 975 rtc::ByteBufferReader read_buf(out);
988 std::string bytes; 976 std::string bytes;
989 read_buf.ReadString(&bytes, len1); 977 read_buf.ReadString(&bytes, len1);
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
1066 1054
1067 TEST_F(StunTest, WriteMessageWithAnErrorCodeAttribute) { 1055 TEST_F(StunTest, WriteMessageWithAnErrorCodeAttribute) {
1068 StunMessage msg; 1056 StunMessage msg;
1069 size_t size = sizeof(kStunMessageWithErrorAttribute); 1057 size_t size = sizeof(kStunMessageWithErrorAttribute);
1070 1058
1071 msg.SetType(STUN_BINDING_ERROR_RESPONSE); 1059 msg.SetType(STUN_BINDING_ERROR_RESPONSE);
1072 msg.SetTransactionID( 1060 msg.SetTransactionID(
1073 std::string(reinterpret_cast<const char*>(kTestTransactionId1), 1061 std::string(reinterpret_cast<const char*>(kTestTransactionId1),
1074 kStunTransactionIdLength)); 1062 kStunTransactionIdLength));
1075 CheckStunTransactionID(msg, kTestTransactionId1, kStunTransactionIdLength); 1063 CheckStunTransactionID(msg, kTestTransactionId1, kStunTransactionIdLength);
1076 StunErrorCodeAttribute* errorcode = StunAttribute::CreateErrorCode(); 1064 auto errorcode = StunAttribute::CreateErrorCode();
1077 errorcode->SetCode(kTestErrorCode); 1065 errorcode->SetCode(kTestErrorCode);
1078 errorcode->SetReason(kTestErrorReason); 1066 errorcode->SetReason(kTestErrorReason);
1079 msg.AddAttribute(errorcode); 1067 msg.AddAttribute(std::move(errorcode));
1080 CheckStunHeader(msg, STUN_BINDING_ERROR_RESPONSE, (size - 20)); 1068 CheckStunHeader(msg, STUN_BINDING_ERROR_RESPONSE, (size - 20));
1081 1069
1082 rtc::ByteBufferWriter out; 1070 rtc::ByteBufferWriter out;
1083 EXPECT_TRUE(msg.Write(&out)); 1071 EXPECT_TRUE(msg.Write(&out));
1084 ASSERT_EQ(size, out.Length()); 1072 ASSERT_EQ(size, out.Length());
1085 // No padding. 1073 // No padding.
1086 ASSERT_EQ(0, memcmp(out.Data(), kStunMessageWithErrorAttribute, size)); 1074 ASSERT_EQ(0, memcmp(out.Data(), kStunMessageWithErrorAttribute, size));
1087 } 1075 }
1088 1076
1089 TEST_F(StunTest, WriteMessageWithAUInt16ListAttribute) { 1077 TEST_F(StunTest, WriteMessageWithAUInt16ListAttribute) {
1090 StunMessage msg; 1078 StunMessage msg;
1091 size_t size = sizeof(kStunMessageWithUInt16ListAttribute); 1079 size_t size = sizeof(kStunMessageWithUInt16ListAttribute);
1092 1080
1093 msg.SetType(STUN_BINDING_REQUEST); 1081 msg.SetType(STUN_BINDING_REQUEST);
1094 msg.SetTransactionID( 1082 msg.SetTransactionID(
1095 std::string(reinterpret_cast<const char*>(kTestTransactionId2), 1083 std::string(reinterpret_cast<const char*>(kTestTransactionId2),
1096 kStunTransactionIdLength)); 1084 kStunTransactionIdLength));
1097 CheckStunTransactionID(msg, kTestTransactionId2, kStunTransactionIdLength); 1085 CheckStunTransactionID(msg, kTestTransactionId2, kStunTransactionIdLength);
1098 StunUInt16ListAttribute* list = StunAttribute::CreateUnknownAttributes(); 1086 auto list = StunAttribute::CreateUnknownAttributes();
1099 list->AddType(0x1U); 1087 list->AddType(0x1U);
1100 list->AddType(0x1000U); 1088 list->AddType(0x1000U);
1101 list->AddType(0xAB0CU); 1089 list->AddType(0xAB0CU);
1102 msg.AddAttribute(list); 1090 msg.AddAttribute(std::move(list));
1103 CheckStunHeader(msg, STUN_BINDING_REQUEST, (size - 20)); 1091 CheckStunHeader(msg, STUN_BINDING_REQUEST, (size - 20));
1104 1092
1105 rtc::ByteBufferWriter out; 1093 rtc::ByteBufferWriter out;
1106 EXPECT_TRUE(msg.Write(&out)); 1094 EXPECT_TRUE(msg.Write(&out));
1107 ASSERT_EQ(size, out.Length()); 1095 ASSERT_EQ(size, out.Length());
1108 // Check everything up to the padding. 1096 // Check everything up to the padding.
1109 ASSERT_EQ(0, 1097 ASSERT_EQ(0,
1110 memcmp(out.Data(), kStunMessageWithUInt16ListAttribute, size - 2)); 1098 memcmp(out.Data(), kStunMessageWithUInt16ListAttribute, size - 2));
1111 } 1099 }
1112 1100
1113 TEST_F(StunTest, WriteMessageWithOriginAttribute) { 1101 TEST_F(StunTest, WriteMessageWithOriginAttribute) {
1114 StunMessage msg; 1102 StunMessage msg;
1115 size_t size = sizeof(kStunMessageWithOriginAttribute); 1103 size_t size = sizeof(kStunMessageWithOriginAttribute);
1116 1104
1117 msg.SetType(STUN_BINDING_REQUEST); 1105 msg.SetType(STUN_BINDING_REQUEST);
1118 msg.SetTransactionID( 1106 msg.SetTransactionID(
1119 std::string(reinterpret_cast<const char*>(kTestTransactionId1), 1107 std::string(reinterpret_cast<const char*>(kTestTransactionId1),
1120 kStunTransactionIdLength)); 1108 kStunTransactionIdLength));
1121 StunByteStringAttribute* origin = 1109 auto origin =
1122 new StunByteStringAttribute(STUN_ATTR_ORIGIN, kTestOrigin); 1110 rtc::MakeUnique<StunByteStringAttribute>(STUN_ATTR_ORIGIN, kTestOrigin);
1123 msg.AddAttribute(origin); 1111 msg.AddAttribute(std::move(origin));
1124 1112
1125 rtc::ByteBufferWriter out; 1113 rtc::ByteBufferWriter out;
1126 EXPECT_TRUE(msg.Write(&out)); 1114 EXPECT_TRUE(msg.Write(&out));
1127 ASSERT_EQ(size, out.Length()); 1115 ASSERT_EQ(size, out.Length());
1128 // Check everything up to the padding 1116 // Check everything up to the padding
1129 ASSERT_EQ(0, memcmp(out.Data(), kStunMessageWithOriginAttribute, size - 2)); 1117 ASSERT_EQ(0, memcmp(out.Data(), kStunMessageWithOriginAttribute, size - 2));
1130 } 1118 }
1131 1119
1132 // Test that we fail to read messages with invalid lengths. 1120 // Test that we fail to read messages with invalid lengths.
1133 void CheckFailureToRead(const unsigned char* testcase, size_t length) { 1121 void CheckFailureToRead(const unsigned char* testcase, size_t length) {
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
1382 in_addr legacy_in_addr; 1370 in_addr legacy_in_addr;
1383 legacy_in_addr.s_addr = htonl(17U); 1371 legacy_in_addr.s_addr = htonl(17U);
1384 rtc::IPAddress legacy_ip(legacy_in_addr); 1372 rtc::IPAddress legacy_ip(legacy_in_addr);
1385 1373
1386 const StunAddressAttribute* addr = msg.GetAddress(STUN_ATTR_MAPPED_ADDRESS); 1374 const StunAddressAttribute* addr = msg.GetAddress(STUN_ATTR_MAPPED_ADDRESS);
1387 ASSERT_TRUE(addr != NULL); 1375 ASSERT_TRUE(addr != NULL);
1388 EXPECT_EQ(1, addr->family()); 1376 EXPECT_EQ(1, addr->family());
1389 EXPECT_EQ(13, addr->port()); 1377 EXPECT_EQ(13, addr->port());
1390 EXPECT_EQ(legacy_ip, addr->ipaddr()); 1378 EXPECT_EQ(legacy_ip, addr->ipaddr());
1391 1379
1392 StunAddressAttribute* addr2 = 1380 auto addr2 = StunAttribute::CreateAddress(STUN_ATTR_MAPPED_ADDRESS);
1393 StunAttribute::CreateAddress(STUN_ATTR_MAPPED_ADDRESS);
1394 addr2->SetPort(13); 1381 addr2->SetPort(13);
1395 addr2->SetIP(legacy_ip); 1382 addr2->SetIP(legacy_ip);
1396 msg2.AddAttribute(addr2); 1383 msg2.AddAttribute(std::move(addr2));
1397 1384
1398 const StunByteStringAttribute* bytes = msg.GetByteString(STUN_ATTR_USERNAME); 1385 const StunByteStringAttribute* bytes = msg.GetByteString(STUN_ATTR_USERNAME);
1399 ASSERT_TRUE(bytes != NULL); 1386 ASSERT_TRUE(bytes != NULL);
1400 EXPECT_EQ(12U, bytes->length()); 1387 EXPECT_EQ(12U, bytes->length());
1401 EXPECT_EQ("abcdefghijkl", bytes->GetString()); 1388 EXPECT_EQ("abcdefghijkl", bytes->GetString());
1402 1389
1403 StunByteStringAttribute* bytes2 = 1390 auto bytes2 = StunAttribute::CreateByteString(STUN_ATTR_USERNAME);
1404 StunAttribute::CreateByteString(STUN_ATTR_USERNAME);
1405 bytes2->CopyBytes("abcdefghijkl"); 1391 bytes2->CopyBytes("abcdefghijkl");
1406 msg2.AddAttribute(bytes2); 1392 msg2.AddAttribute(std::move(bytes2));
1407 1393
1408 const StunUInt32Attribute* uval = msg.GetUInt32(STUN_ATTR_LIFETIME); 1394 const StunUInt32Attribute* uval = msg.GetUInt32(STUN_ATTR_LIFETIME);
1409 ASSERT_TRUE(uval != NULL); 1395 ASSERT_TRUE(uval != NULL);
1410 EXPECT_EQ(11U, uval->value()); 1396 EXPECT_EQ(11U, uval->value());
1411 1397
1412 StunUInt32Attribute* uval2 = StunAttribute::CreateUInt32(STUN_ATTR_LIFETIME); 1398 auto uval2 = StunAttribute::CreateUInt32(STUN_ATTR_LIFETIME);
1413 uval2->SetValue(11); 1399 uval2->SetValue(11);
1414 msg2.AddAttribute(uval2); 1400 msg2.AddAttribute(std::move(uval2));
1415 1401
1416 bytes = msg.GetByteString(STUN_ATTR_MAGIC_COOKIE); 1402 bytes = msg.GetByteString(STUN_ATTR_MAGIC_COOKIE);
1417 ASSERT_TRUE(bytes != NULL); 1403 ASSERT_TRUE(bytes != NULL);
1418 EXPECT_EQ(4U, bytes->length()); 1404 EXPECT_EQ(4U, bytes->length());
1419 EXPECT_EQ(0, 1405 EXPECT_EQ(0,
1420 memcmp(bytes->bytes(), 1406 memcmp(bytes->bytes(),
1421 TURN_MAGIC_COOKIE_VALUE, 1407 TURN_MAGIC_COOKIE_VALUE,
1422 sizeof(TURN_MAGIC_COOKIE_VALUE))); 1408 sizeof(TURN_MAGIC_COOKIE_VALUE)));
1423 1409
1424 bytes2 = StunAttribute::CreateByteString(STUN_ATTR_MAGIC_COOKIE); 1410 bytes2 = StunAttribute::CreateByteString(STUN_ATTR_MAGIC_COOKIE);
1425 bytes2->CopyBytes(reinterpret_cast<const char*>(TURN_MAGIC_COOKIE_VALUE), 1411 bytes2->CopyBytes(reinterpret_cast<const char*>(TURN_MAGIC_COOKIE_VALUE),
1426 sizeof(TURN_MAGIC_COOKIE_VALUE)); 1412 sizeof(TURN_MAGIC_COOKIE_VALUE));
1427 msg2.AddAttribute(bytes2); 1413 msg2.AddAttribute(std::move(bytes2));
1428 1414
1429 uval = msg.GetUInt32(STUN_ATTR_BANDWIDTH); 1415 uval = msg.GetUInt32(STUN_ATTR_BANDWIDTH);
1430 ASSERT_TRUE(uval != NULL); 1416 ASSERT_TRUE(uval != NULL);
1431 EXPECT_EQ(6U, uval->value()); 1417 EXPECT_EQ(6U, uval->value());
1432 1418
1433 uval2 = StunAttribute::CreateUInt32(STUN_ATTR_BANDWIDTH); 1419 uval2 = StunAttribute::CreateUInt32(STUN_ATTR_BANDWIDTH);
1434 uval2->SetValue(6); 1420 uval2->SetValue(6);
1435 msg2.AddAttribute(uval2); 1421 msg2.AddAttribute(std::move(uval2));
1436 1422
1437 addr = msg.GetAddress(STUN_ATTR_DESTINATION_ADDRESS); 1423 addr = msg.GetAddress(STUN_ATTR_DESTINATION_ADDRESS);
1438 ASSERT_TRUE(addr != NULL); 1424 ASSERT_TRUE(addr != NULL);
1439 EXPECT_EQ(1, addr->family()); 1425 EXPECT_EQ(1, addr->family());
1440 EXPECT_EQ(13, addr->port()); 1426 EXPECT_EQ(13, addr->port());
1441 EXPECT_EQ(legacy_ip, addr->ipaddr()); 1427 EXPECT_EQ(legacy_ip, addr->ipaddr());
1442 1428
1443 addr2 = StunAttribute::CreateAddress(STUN_ATTR_DESTINATION_ADDRESS); 1429 addr2 = StunAttribute::CreateAddress(STUN_ATTR_DESTINATION_ADDRESS);
1444 addr2->SetPort(13); 1430 addr2->SetPort(13);
1445 addr2->SetIP(legacy_ip); 1431 addr2->SetIP(legacy_ip);
1446 msg2.AddAttribute(addr2); 1432 msg2.AddAttribute(std::move(addr2));
1447 1433
1448 addr = msg.GetAddress(STUN_ATTR_SOURCE_ADDRESS2); 1434 addr = msg.GetAddress(STUN_ATTR_SOURCE_ADDRESS2);
1449 ASSERT_TRUE(addr != NULL); 1435 ASSERT_TRUE(addr != NULL);
1450 EXPECT_EQ(1, addr->family()); 1436 EXPECT_EQ(1, addr->family());
1451 EXPECT_EQ(13, addr->port()); 1437 EXPECT_EQ(13, addr->port());
1452 EXPECT_EQ(legacy_ip, addr->ipaddr()); 1438 EXPECT_EQ(legacy_ip, addr->ipaddr());
1453 1439
1454 addr2 = StunAttribute::CreateAddress(STUN_ATTR_SOURCE_ADDRESS2); 1440 addr2 = StunAttribute::CreateAddress(STUN_ATTR_SOURCE_ADDRESS2);
1455 addr2->SetPort(13); 1441 addr2->SetPort(13);
1456 addr2->SetIP(legacy_ip); 1442 addr2->SetIP(legacy_ip);
1457 msg2.AddAttribute(addr2); 1443 msg2.AddAttribute(std::move(addr2));
1458 1444
1459 bytes = msg.GetByteString(STUN_ATTR_DATA); 1445 bytes = msg.GetByteString(STUN_ATTR_DATA);
1460 ASSERT_TRUE(bytes != NULL); 1446 ASSERT_TRUE(bytes != NULL);
1461 EXPECT_EQ(7U, bytes->length()); 1447 EXPECT_EQ(7U, bytes->length());
1462 EXPECT_EQ("abcdefg", bytes->GetString()); 1448 EXPECT_EQ("abcdefg", bytes->GetString());
1463 1449
1464 bytes2 = StunAttribute::CreateByteString(STUN_ATTR_DATA); 1450 bytes2 = StunAttribute::CreateByteString(STUN_ATTR_DATA);
1465 bytes2->CopyBytes("abcdefg"); 1451 bytes2->CopyBytes("abcdefg");
1466 msg2.AddAttribute(bytes2); 1452 msg2.AddAttribute(std::move(bytes2));
1467 1453
1468 rtc::ByteBufferWriter out; 1454 rtc::ByteBufferWriter out;
1469 EXPECT_TRUE(msg.Write(&out)); 1455 EXPECT_TRUE(msg.Write(&out));
1470 EXPECT_EQ(size, out.Length()); 1456 EXPECT_EQ(size, out.Length());
1471 size_t len1 = out.Length(); 1457 size_t len1 = out.Length();
1472 rtc::ByteBufferReader read_buf(out); 1458 rtc::ByteBufferReader read_buf(out);
1473 std::string outstring; 1459 std::string outstring;
1474 read_buf.ReadString(&outstring, len1); 1460 read_buf.ReadString(&outstring, len1);
1475 EXPECT_EQ(0, memcmp(outstring.c_str(), input, len1)); 1461 EXPECT_EQ(0, memcmp(outstring.c_str(), input, len1));
1476 1462
1477 rtc::ByteBufferWriter out2; 1463 rtc::ByteBufferWriter out2;
1478 EXPECT_TRUE(msg2.Write(&out2)); 1464 EXPECT_TRUE(msg2.Write(&out2));
1479 EXPECT_EQ(size, out2.Length()); 1465 EXPECT_EQ(size, out2.Length());
1480 size_t len2 = out2.Length(); 1466 size_t len2 = out2.Length();
1481 rtc::ByteBufferReader read_buf2(out2); 1467 rtc::ByteBufferReader read_buf2(out2);
1482 std::string outstring2; 1468 std::string outstring2;
1483 read_buf2.ReadString(&outstring2, len2); 1469 read_buf2.ReadString(&outstring2, len2);
1484 EXPECT_EQ(0, memcmp(outstring2.c_str(), input, len2)); 1470 EXPECT_EQ(0, memcmp(outstring2.c_str(), input, len2));
1485 } 1471 }
1486 1472
1487 } // namespace cricket 1473 } // namespace cricket
OLDNEW
« no previous file with comments | « webrtc/p2p/base/stun.cc ('k') | webrtc/p2p/base/stunrequest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698