| OLD | NEW |
| 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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 } else { | 55 } else { |
| 56 ASSERT_TRUE(addr->family() == STUN_ADDRESS_IPV6 || | 56 ASSERT_TRUE(addr->family() == STUN_ADDRESS_IPV6 || |
| 57 addr->family() == STUN_ADDRESS_IPV4); | 57 addr->family() == STUN_ADDRESS_IPV4); |
| 58 } | 58 } |
| 59 } | 59 } |
| 60 | 60 |
| 61 size_t ReadStunMessageTestCase(StunMessage* msg, | 61 size_t ReadStunMessageTestCase(StunMessage* msg, |
| 62 const unsigned char* testcase, | 62 const unsigned char* testcase, |
| 63 size_t size) { | 63 size_t size) { |
| 64 const char* input = reinterpret_cast<const char*>(testcase); | 64 const char* input = reinterpret_cast<const char*>(testcase); |
| 65 rtc::ByteBuffer buf(input, size); | 65 rtc::ByteBufferReader buf(input, size); |
| 66 if (msg->Read(&buf)) { | 66 if (msg->Read(&buf)) { |
| 67 // Returns the size the stun message should report itself as being | 67 // Returns the size the stun message should report itself as being |
| 68 return (size - 20); | 68 return (size - 20); |
| 69 } else { | 69 } else { |
| 70 return 0; | 70 return 0; |
| 71 } | 71 } |
| 72 } | 72 } |
| 73 }; | 73 }; |
| 74 | 74 |
| 75 | 75 |
| (...skipping 656 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 732 | 732 |
| 733 // Owner with a different transaction ID. | 733 // Owner with a different transaction ID. |
| 734 msg2.SetTransactionID("ABCDABCDABCD"); | 734 msg2.SetTransactionID("ABCDABCDABCD"); |
| 735 StunXorAddressAttribute addr2(STUN_ATTR_XOR_MAPPED_ADDRESS, 20, NULL); | 735 StunXorAddressAttribute addr2(STUN_ATTR_XOR_MAPPED_ADDRESS, 20, NULL); |
| 736 addr2.SetIP(addr->ipaddr()); | 736 addr2.SetIP(addr->ipaddr()); |
| 737 addr2.SetPort(addr->port()); | 737 addr2.SetPort(addr->port()); |
| 738 addr2.SetOwner(&msg2); | 738 addr2.SetOwner(&msg2); |
| 739 // The internal IP address shouldn't change. | 739 // The internal IP address shouldn't change. |
| 740 ASSERT_EQ(addr2.ipaddr(), addr->ipaddr()); | 740 ASSERT_EQ(addr2.ipaddr(), addr->ipaddr()); |
| 741 | 741 |
| 742 rtc::ByteBuffer correct_buf; | 742 rtc::ByteBufferWriter correct_buf; |
| 743 rtc::ByteBuffer wrong_buf; | 743 rtc::ByteBufferWriter wrong_buf; |
| 744 EXPECT_TRUE(addr->Write(&correct_buf)); | 744 EXPECT_TRUE(addr->Write(&correct_buf)); |
| 745 EXPECT_TRUE(addr2.Write(&wrong_buf)); | 745 EXPECT_TRUE(addr2.Write(&wrong_buf)); |
| 746 // But when written out, the buffers should look different. | 746 // But when written out, the buffers should look different. |
| 747 ASSERT_NE(0, | 747 ASSERT_NE(0, |
| 748 memcmp(correct_buf.Data(), wrong_buf.Data(), wrong_buf.Length())); | 748 memcmp(correct_buf.Data(), wrong_buf.Data(), wrong_buf.Length())); |
| 749 // And when reading a known good value, the address should be wrong. | 749 // And when reading a known good value, the address should be wrong. |
| 750 addr2.Read(&correct_buf); | 750 rtc::ByteBufferReader read_buf(correct_buf); |
| 751 addr2.Read(&read_buf); |
| 751 ASSERT_NE(addr->ipaddr(), addr2.ipaddr()); | 752 ASSERT_NE(addr->ipaddr(), addr2.ipaddr()); |
| 752 addr2.SetIP(addr->ipaddr()); | 753 addr2.SetIP(addr->ipaddr()); |
| 753 addr2.SetPort(addr->port()); | 754 addr2.SetPort(addr->port()); |
| 754 // Try writing with no owner at all, should fail and write nothing. | 755 // Try writing with no owner at all, should fail and write nothing. |
| 755 addr2.SetOwner(NULL); | 756 addr2.SetOwner(NULL); |
| 756 ASSERT_EQ(addr2.ipaddr(), addr->ipaddr()); | 757 ASSERT_EQ(addr2.ipaddr(), addr->ipaddr()); |
| 757 wrong_buf.Consume(wrong_buf.Length()); | 758 wrong_buf.Clear(); |
| 758 EXPECT_FALSE(addr2.Write(&wrong_buf)); | 759 EXPECT_FALSE(addr2.Write(&wrong_buf)); |
| 759 ASSERT_EQ(0U, wrong_buf.Length()); | 760 ASSERT_EQ(0U, wrong_buf.Length()); |
| 760 } | 761 } |
| 761 | 762 |
| 762 TEST_F(StunTest, SetIPv4XorAddressAttributeOwner) { | 763 TEST_F(StunTest, SetIPv4XorAddressAttributeOwner) { |
| 763 // Unlike the IPv6XorAddressAttributeOwner test, IPv4 XOR address attributes | 764 // Unlike the IPv6XorAddressAttributeOwner test, IPv4 XOR address attributes |
| 764 // should _not_ be affected by a change in owner. IPv4 XOR address uses the | 765 // should _not_ be affected by a change in owner. IPv4 XOR address uses the |
| 765 // magic cookie value which is fixed. | 766 // magic cookie value which is fixed. |
| 766 StunMessage msg; | 767 StunMessage msg; |
| 767 StunMessage msg2; | 768 StunMessage msg2; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 779 | 780 |
| 780 // Owner with a different transaction ID. | 781 // Owner with a different transaction ID. |
| 781 msg2.SetTransactionID("ABCDABCDABCD"); | 782 msg2.SetTransactionID("ABCDABCDABCD"); |
| 782 StunXorAddressAttribute addr2(STUN_ATTR_XOR_MAPPED_ADDRESS, 20, NULL); | 783 StunXorAddressAttribute addr2(STUN_ATTR_XOR_MAPPED_ADDRESS, 20, NULL); |
| 783 addr2.SetIP(addr->ipaddr()); | 784 addr2.SetIP(addr->ipaddr()); |
| 784 addr2.SetPort(addr->port()); | 785 addr2.SetPort(addr->port()); |
| 785 addr2.SetOwner(&msg2); | 786 addr2.SetOwner(&msg2); |
| 786 // The internal IP address shouldn't change. | 787 // The internal IP address shouldn't change. |
| 787 ASSERT_EQ(addr2.ipaddr(), addr->ipaddr()); | 788 ASSERT_EQ(addr2.ipaddr(), addr->ipaddr()); |
| 788 | 789 |
| 789 rtc::ByteBuffer correct_buf; | 790 rtc::ByteBufferWriter correct_buf; |
| 790 rtc::ByteBuffer wrong_buf; | 791 rtc::ByteBufferWriter wrong_buf; |
| 791 EXPECT_TRUE(addr->Write(&correct_buf)); | 792 EXPECT_TRUE(addr->Write(&correct_buf)); |
| 792 EXPECT_TRUE(addr2.Write(&wrong_buf)); | 793 EXPECT_TRUE(addr2.Write(&wrong_buf)); |
| 793 // The same address data should be written. | 794 // The same address data should be written. |
| 794 ASSERT_EQ(0, | 795 ASSERT_EQ(0, |
| 795 memcmp(correct_buf.Data(), wrong_buf.Data(), wrong_buf.Length())); | 796 memcmp(correct_buf.Data(), wrong_buf.Data(), wrong_buf.Length())); |
| 796 // And an attribute should be able to un-XOR an address belonging to a message | 797 // And an attribute should be able to un-XOR an address belonging to a message |
| 797 // with a different transaction ID. | 798 // with a different transaction ID. |
| 798 EXPECT_TRUE(addr2.Read(&correct_buf)); | 799 rtc::ByteBufferReader read_buf(correct_buf); |
| 800 EXPECT_TRUE(addr2.Read(&read_buf)); |
| 799 ASSERT_EQ(addr->ipaddr(), addr2.ipaddr()); | 801 ASSERT_EQ(addr->ipaddr(), addr2.ipaddr()); |
| 800 | 802 |
| 801 // However, no owner is still an error, should fail and write nothing. | 803 // However, no owner is still an error, should fail and write nothing. |
| 802 addr2.SetOwner(NULL); | 804 addr2.SetOwner(NULL); |
| 803 ASSERT_EQ(addr2.ipaddr(), addr->ipaddr()); | 805 ASSERT_EQ(addr2.ipaddr(), addr->ipaddr()); |
| 804 wrong_buf.Consume(wrong_buf.Length()); | 806 wrong_buf.Clear(); |
| 805 EXPECT_FALSE(addr2.Write(&wrong_buf)); | 807 EXPECT_FALSE(addr2.Write(&wrong_buf)); |
| 806 } | 808 } |
| 807 | 809 |
| 808 TEST_F(StunTest, CreateIPv6AddressAttribute) { | 810 TEST_F(StunTest, CreateIPv6AddressAttribute) { |
| 809 rtc::IPAddress test_ip(kIPv6TestAddress2); | 811 rtc::IPAddress test_ip(kIPv6TestAddress2); |
| 810 | 812 |
| 811 StunAddressAttribute* addr = | 813 StunAddressAttribute* addr = |
| 812 StunAttribute::CreateAddress(STUN_ATTR_MAPPED_ADDRESS); | 814 StunAttribute::CreateAddress(STUN_ATTR_MAPPED_ADDRESS); |
| 813 rtc::SocketAddress test_addr(test_ip, kTestMessagePort2); | 815 rtc::SocketAddress test_addr(test_ip, kTestMessagePort2); |
| 814 addr->SetAddress(test_addr); | 816 addr->SetAddress(test_addr); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 868 CheckStunTransactionID(msg, kTestTransactionId1, kStunTransactionIdLength); | 870 CheckStunTransactionID(msg, kTestTransactionId1, kStunTransactionIdLength); |
| 869 | 871 |
| 870 StunAddressAttribute* addr = | 872 StunAddressAttribute* addr = |
| 871 StunAttribute::CreateAddress(STUN_ATTR_MAPPED_ADDRESS); | 873 StunAttribute::CreateAddress(STUN_ATTR_MAPPED_ADDRESS); |
| 872 rtc::SocketAddress test_addr(test_ip, kTestMessagePort2); | 874 rtc::SocketAddress test_addr(test_ip, kTestMessagePort2); |
| 873 addr->SetAddress(test_addr); | 875 addr->SetAddress(test_addr); |
| 874 EXPECT_TRUE(msg.AddAttribute(addr)); | 876 EXPECT_TRUE(msg.AddAttribute(addr)); |
| 875 | 877 |
| 876 CheckStunHeader(msg, STUN_BINDING_REQUEST, (size - 20)); | 878 CheckStunHeader(msg, STUN_BINDING_REQUEST, (size - 20)); |
| 877 | 879 |
| 878 rtc::ByteBuffer out; | 880 rtc::ByteBufferWriter out; |
| 879 EXPECT_TRUE(msg.Write(&out)); | 881 EXPECT_TRUE(msg.Write(&out)); |
| 880 ASSERT_EQ(out.Length(), sizeof(kStunMessageWithIPv6MappedAddress)); | 882 ASSERT_EQ(out.Length(), sizeof(kStunMessageWithIPv6MappedAddress)); |
| 881 int len1 = static_cast<int>(out.Length()); | 883 int len1 = static_cast<int>(out.Length()); |
| 884 rtc::ByteBufferReader read_buf(out); |
| 882 std::string bytes; | 885 std::string bytes; |
| 883 out.ReadString(&bytes, len1); | 886 read_buf.ReadString(&bytes, len1); |
| 884 ASSERT_EQ(0, memcmp(bytes.c_str(), kStunMessageWithIPv6MappedAddress, len1)); | 887 ASSERT_EQ(0, memcmp(bytes.c_str(), kStunMessageWithIPv6MappedAddress, len1)); |
| 885 } | 888 } |
| 886 | 889 |
| 887 TEST_F(StunTest, WriteMessageWithIPv4AddressAttribute) { | 890 TEST_F(StunTest, WriteMessageWithIPv4AddressAttribute) { |
| 888 StunMessage msg; | 891 StunMessage msg; |
| 889 size_t size = sizeof(kStunMessageWithIPv4MappedAddress); | 892 size_t size = sizeof(kStunMessageWithIPv4MappedAddress); |
| 890 | 893 |
| 891 rtc::IPAddress test_ip(kIPv4TestAddress1); | 894 rtc::IPAddress test_ip(kIPv4TestAddress1); |
| 892 | 895 |
| 893 msg.SetType(STUN_BINDING_RESPONSE); | 896 msg.SetType(STUN_BINDING_RESPONSE); |
| 894 msg.SetTransactionID( | 897 msg.SetTransactionID( |
| 895 std::string(reinterpret_cast<const char*>(kTestTransactionId1), | 898 std::string(reinterpret_cast<const char*>(kTestTransactionId1), |
| 896 kStunTransactionIdLength)); | 899 kStunTransactionIdLength)); |
| 897 CheckStunTransactionID(msg, kTestTransactionId1, kStunTransactionIdLength); | 900 CheckStunTransactionID(msg, kTestTransactionId1, kStunTransactionIdLength); |
| 898 | 901 |
| 899 StunAddressAttribute* addr = | 902 StunAddressAttribute* addr = |
| 900 StunAttribute::CreateAddress(STUN_ATTR_MAPPED_ADDRESS); | 903 StunAttribute::CreateAddress(STUN_ATTR_MAPPED_ADDRESS); |
| 901 rtc::SocketAddress test_addr(test_ip, kTestMessagePort4); | 904 rtc::SocketAddress test_addr(test_ip, kTestMessagePort4); |
| 902 addr->SetAddress(test_addr); | 905 addr->SetAddress(test_addr); |
| 903 EXPECT_TRUE(msg.AddAttribute(addr)); | 906 EXPECT_TRUE(msg.AddAttribute(addr)); |
| 904 | 907 |
| 905 CheckStunHeader(msg, STUN_BINDING_RESPONSE, (size - 20)); | 908 CheckStunHeader(msg, STUN_BINDING_RESPONSE, (size - 20)); |
| 906 | 909 |
| 907 rtc::ByteBuffer out; | 910 rtc::ByteBufferWriter out; |
| 908 EXPECT_TRUE(msg.Write(&out)); | 911 EXPECT_TRUE(msg.Write(&out)); |
| 909 ASSERT_EQ(out.Length(), sizeof(kStunMessageWithIPv4MappedAddress)); | 912 ASSERT_EQ(out.Length(), sizeof(kStunMessageWithIPv4MappedAddress)); |
| 910 int len1 = static_cast<int>(out.Length()); | 913 int len1 = static_cast<int>(out.Length()); |
| 914 rtc::ByteBufferReader read_buf(out); |
| 911 std::string bytes; | 915 std::string bytes; |
| 912 out.ReadString(&bytes, len1); | 916 read_buf.ReadString(&bytes, len1); |
| 913 ASSERT_EQ(0, memcmp(bytes.c_str(), kStunMessageWithIPv4MappedAddress, len1)); | 917 ASSERT_EQ(0, memcmp(bytes.c_str(), kStunMessageWithIPv4MappedAddress, len1)); |
| 914 } | 918 } |
| 915 | 919 |
| 916 TEST_F(StunTest, WriteMessageWithIPv6XorAddressAttribute) { | 920 TEST_F(StunTest, WriteMessageWithIPv6XorAddressAttribute) { |
| 917 StunMessage msg; | 921 StunMessage msg; |
| 918 size_t size = sizeof(kStunMessageWithIPv6XorMappedAddress); | 922 size_t size = sizeof(kStunMessageWithIPv6XorMappedAddress); |
| 919 | 923 |
| 920 rtc::IPAddress test_ip(kIPv6TestAddress1); | 924 rtc::IPAddress test_ip(kIPv6TestAddress1); |
| 921 | 925 |
| 922 msg.SetType(STUN_BINDING_RESPONSE); | 926 msg.SetType(STUN_BINDING_RESPONSE); |
| 923 msg.SetTransactionID( | 927 msg.SetTransactionID( |
| 924 std::string(reinterpret_cast<const char*>(kTestTransactionId2), | 928 std::string(reinterpret_cast<const char*>(kTestTransactionId2), |
| 925 kStunTransactionIdLength)); | 929 kStunTransactionIdLength)); |
| 926 CheckStunTransactionID(msg, kTestTransactionId2, kStunTransactionIdLength); | 930 CheckStunTransactionID(msg, kTestTransactionId2, kStunTransactionIdLength); |
| 927 | 931 |
| 928 StunAddressAttribute* addr = | 932 StunAddressAttribute* addr = |
| 929 StunAttribute::CreateXorAddress(STUN_ATTR_XOR_MAPPED_ADDRESS); | 933 StunAttribute::CreateXorAddress(STUN_ATTR_XOR_MAPPED_ADDRESS); |
| 930 rtc::SocketAddress test_addr(test_ip, kTestMessagePort1); | 934 rtc::SocketAddress test_addr(test_ip, kTestMessagePort1); |
| 931 addr->SetAddress(test_addr); | 935 addr->SetAddress(test_addr); |
| 932 EXPECT_TRUE(msg.AddAttribute(addr)); | 936 EXPECT_TRUE(msg.AddAttribute(addr)); |
| 933 | 937 |
| 934 CheckStunHeader(msg, STUN_BINDING_RESPONSE, (size - 20)); | 938 CheckStunHeader(msg, STUN_BINDING_RESPONSE, (size - 20)); |
| 935 | 939 |
| 936 rtc::ByteBuffer out; | 940 rtc::ByteBufferWriter out; |
| 937 EXPECT_TRUE(msg.Write(&out)); | 941 EXPECT_TRUE(msg.Write(&out)); |
| 938 ASSERT_EQ(out.Length(), sizeof(kStunMessageWithIPv6XorMappedAddress)); | 942 ASSERT_EQ(out.Length(), sizeof(kStunMessageWithIPv6XorMappedAddress)); |
| 939 int len1 = static_cast<int>(out.Length()); | 943 int len1 = static_cast<int>(out.Length()); |
| 944 rtc::ByteBufferReader read_buf(out); |
| 940 std::string bytes; | 945 std::string bytes; |
| 941 out.ReadString(&bytes, len1); | 946 read_buf.ReadString(&bytes, len1); |
| 942 ASSERT_EQ(0, | 947 ASSERT_EQ(0, |
| 943 memcmp(bytes.c_str(), kStunMessageWithIPv6XorMappedAddress, len1)); | 948 memcmp(bytes.c_str(), kStunMessageWithIPv6XorMappedAddress, len1)); |
| 944 } | 949 } |
| 945 | 950 |
| 946 TEST_F(StunTest, WriteMessageWithIPv4XoreAddressAttribute) { | 951 TEST_F(StunTest, WriteMessageWithIPv4XoreAddressAttribute) { |
| 947 StunMessage msg; | 952 StunMessage msg; |
| 948 size_t size = sizeof(kStunMessageWithIPv4XorMappedAddress); | 953 size_t size = sizeof(kStunMessageWithIPv4XorMappedAddress); |
| 949 | 954 |
| 950 rtc::IPAddress test_ip(kIPv4TestAddress1); | 955 rtc::IPAddress test_ip(kIPv4TestAddress1); |
| 951 | 956 |
| 952 msg.SetType(STUN_BINDING_RESPONSE); | 957 msg.SetType(STUN_BINDING_RESPONSE); |
| 953 msg.SetTransactionID( | 958 msg.SetTransactionID( |
| 954 std::string(reinterpret_cast<const char*>(kTestTransactionId1), | 959 std::string(reinterpret_cast<const char*>(kTestTransactionId1), |
| 955 kStunTransactionIdLength)); | 960 kStunTransactionIdLength)); |
| 956 CheckStunTransactionID(msg, kTestTransactionId1, kStunTransactionIdLength); | 961 CheckStunTransactionID(msg, kTestTransactionId1, kStunTransactionIdLength); |
| 957 | 962 |
| 958 StunAddressAttribute* addr = | 963 StunAddressAttribute* addr = |
| 959 StunAttribute::CreateXorAddress(STUN_ATTR_XOR_MAPPED_ADDRESS); | 964 StunAttribute::CreateXorAddress(STUN_ATTR_XOR_MAPPED_ADDRESS); |
| 960 rtc::SocketAddress test_addr(test_ip, kTestMessagePort3); | 965 rtc::SocketAddress test_addr(test_ip, kTestMessagePort3); |
| 961 addr->SetAddress(test_addr); | 966 addr->SetAddress(test_addr); |
| 962 EXPECT_TRUE(msg.AddAttribute(addr)); | 967 EXPECT_TRUE(msg.AddAttribute(addr)); |
| 963 | 968 |
| 964 CheckStunHeader(msg, STUN_BINDING_RESPONSE, (size - 20)); | 969 CheckStunHeader(msg, STUN_BINDING_RESPONSE, (size - 20)); |
| 965 | 970 |
| 966 rtc::ByteBuffer out; | 971 rtc::ByteBufferWriter out; |
| 967 EXPECT_TRUE(msg.Write(&out)); | 972 EXPECT_TRUE(msg.Write(&out)); |
| 968 ASSERT_EQ(out.Length(), sizeof(kStunMessageWithIPv4XorMappedAddress)); | 973 ASSERT_EQ(out.Length(), sizeof(kStunMessageWithIPv4XorMappedAddress)); |
| 969 int len1 = static_cast<int>(out.Length()); | 974 int len1 = static_cast<int>(out.Length()); |
| 975 rtc::ByteBufferReader read_buf(out); |
| 970 std::string bytes; | 976 std::string bytes; |
| 971 out.ReadString(&bytes, len1); | 977 read_buf.ReadString(&bytes, len1); |
| 972 ASSERT_EQ(0, | 978 ASSERT_EQ(0, |
| 973 memcmp(bytes.c_str(), kStunMessageWithIPv4XorMappedAddress, len1)); | 979 memcmp(bytes.c_str(), kStunMessageWithIPv4XorMappedAddress, len1)); |
| 974 } | 980 } |
| 975 | 981 |
| 976 TEST_F(StunTest, ReadByteStringAttribute) { | 982 TEST_F(StunTest, ReadByteStringAttribute) { |
| 977 StunMessage msg; | 983 StunMessage msg; |
| 978 size_t size = ReadStunMessage(&msg, kStunMessageWithByteStringAttribute); | 984 size_t size = ReadStunMessage(&msg, kStunMessageWithByteStringAttribute); |
| 979 | 985 |
| 980 CheckStunHeader(msg, STUN_BINDING_REQUEST, size); | 986 CheckStunHeader(msg, STUN_BINDING_REQUEST, size); |
| 981 CheckStunTransactionID(msg, kTestTransactionId2, kStunTransactionIdLength); | 987 CheckStunTransactionID(msg, kTestTransactionId2, kStunTransactionIdLength); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1054 msg.SetTransactionID( | 1060 msg.SetTransactionID( |
| 1055 std::string(reinterpret_cast<const char*>(kTestTransactionId1), | 1061 std::string(reinterpret_cast<const char*>(kTestTransactionId1), |
| 1056 kStunTransactionIdLength)); | 1062 kStunTransactionIdLength)); |
| 1057 CheckStunTransactionID(msg, kTestTransactionId1, kStunTransactionIdLength); | 1063 CheckStunTransactionID(msg, kTestTransactionId1, kStunTransactionIdLength); |
| 1058 StunErrorCodeAttribute* errorcode = StunAttribute::CreateErrorCode(); | 1064 StunErrorCodeAttribute* errorcode = StunAttribute::CreateErrorCode(); |
| 1059 errorcode->SetCode(kTestErrorCode); | 1065 errorcode->SetCode(kTestErrorCode); |
| 1060 errorcode->SetReason(kTestErrorReason); | 1066 errorcode->SetReason(kTestErrorReason); |
| 1061 EXPECT_TRUE(msg.AddAttribute(errorcode)); | 1067 EXPECT_TRUE(msg.AddAttribute(errorcode)); |
| 1062 CheckStunHeader(msg, STUN_BINDING_ERROR_RESPONSE, (size - 20)); | 1068 CheckStunHeader(msg, STUN_BINDING_ERROR_RESPONSE, (size - 20)); |
| 1063 | 1069 |
| 1064 rtc::ByteBuffer out; | 1070 rtc::ByteBufferWriter out; |
| 1065 EXPECT_TRUE(msg.Write(&out)); | 1071 EXPECT_TRUE(msg.Write(&out)); |
| 1066 ASSERT_EQ(size, out.Length()); | 1072 ASSERT_EQ(size, out.Length()); |
| 1067 // No padding. | 1073 // No padding. |
| 1068 ASSERT_EQ(0, memcmp(out.Data(), kStunMessageWithErrorAttribute, size)); | 1074 ASSERT_EQ(0, memcmp(out.Data(), kStunMessageWithErrorAttribute, size)); |
| 1069 } | 1075 } |
| 1070 | 1076 |
| 1071 TEST_F(StunTest, WriteMessageWithAUInt16ListAttribute) { | 1077 TEST_F(StunTest, WriteMessageWithAUInt16ListAttribute) { |
| 1072 StunMessage msg; | 1078 StunMessage msg; |
| 1073 size_t size = sizeof(kStunMessageWithUInt16ListAttribute); | 1079 size_t size = sizeof(kStunMessageWithUInt16ListAttribute); |
| 1074 | 1080 |
| 1075 msg.SetType(STUN_BINDING_REQUEST); | 1081 msg.SetType(STUN_BINDING_REQUEST); |
| 1076 msg.SetTransactionID( | 1082 msg.SetTransactionID( |
| 1077 std::string(reinterpret_cast<const char*>(kTestTransactionId2), | 1083 std::string(reinterpret_cast<const char*>(kTestTransactionId2), |
| 1078 kStunTransactionIdLength)); | 1084 kStunTransactionIdLength)); |
| 1079 CheckStunTransactionID(msg, kTestTransactionId2, kStunTransactionIdLength); | 1085 CheckStunTransactionID(msg, kTestTransactionId2, kStunTransactionIdLength); |
| 1080 StunUInt16ListAttribute* list = StunAttribute::CreateUnknownAttributes(); | 1086 StunUInt16ListAttribute* list = StunAttribute::CreateUnknownAttributes(); |
| 1081 list->AddType(0x1U); | 1087 list->AddType(0x1U); |
| 1082 list->AddType(0x1000U); | 1088 list->AddType(0x1000U); |
| 1083 list->AddType(0xAB0CU); | 1089 list->AddType(0xAB0CU); |
| 1084 EXPECT_TRUE(msg.AddAttribute(list)); | 1090 EXPECT_TRUE(msg.AddAttribute(list)); |
| 1085 CheckStunHeader(msg, STUN_BINDING_REQUEST, (size - 20)); | 1091 CheckStunHeader(msg, STUN_BINDING_REQUEST, (size - 20)); |
| 1086 | 1092 |
| 1087 rtc::ByteBuffer out; | 1093 rtc::ByteBufferWriter out; |
| 1088 EXPECT_TRUE(msg.Write(&out)); | 1094 EXPECT_TRUE(msg.Write(&out)); |
| 1089 ASSERT_EQ(size, out.Length()); | 1095 ASSERT_EQ(size, out.Length()); |
| 1090 // Check everything up to the padding. | 1096 // Check everything up to the padding. |
| 1091 ASSERT_EQ(0, | 1097 ASSERT_EQ(0, |
| 1092 memcmp(out.Data(), kStunMessageWithUInt16ListAttribute, size - 2)); | 1098 memcmp(out.Data(), kStunMessageWithUInt16ListAttribute, size - 2)); |
| 1093 } | 1099 } |
| 1094 | 1100 |
| 1095 TEST_F(StunTest, WriteMessageWithOriginAttribute) { | 1101 TEST_F(StunTest, WriteMessageWithOriginAttribute) { |
| 1096 StunMessage msg; | 1102 StunMessage msg; |
| 1097 size_t size = sizeof(kStunMessageWithOriginAttribute); | 1103 size_t size = sizeof(kStunMessageWithOriginAttribute); |
| 1098 | 1104 |
| 1099 msg.SetType(STUN_BINDING_REQUEST); | 1105 msg.SetType(STUN_BINDING_REQUEST); |
| 1100 msg.SetTransactionID( | 1106 msg.SetTransactionID( |
| 1101 std::string(reinterpret_cast<const char*>(kTestTransactionId1), | 1107 std::string(reinterpret_cast<const char*>(kTestTransactionId1), |
| 1102 kStunTransactionIdLength)); | 1108 kStunTransactionIdLength)); |
| 1103 StunByteStringAttribute* origin = | 1109 StunByteStringAttribute* origin = |
| 1104 new StunByteStringAttribute(STUN_ATTR_ORIGIN, kTestOrigin); | 1110 new StunByteStringAttribute(STUN_ATTR_ORIGIN, kTestOrigin); |
| 1105 EXPECT_TRUE(msg.AddAttribute(origin)); | 1111 EXPECT_TRUE(msg.AddAttribute(origin)); |
| 1106 | 1112 |
| 1107 rtc::ByteBuffer out; | 1113 rtc::ByteBufferWriter out; |
| 1108 EXPECT_TRUE(msg.Write(&out)); | 1114 EXPECT_TRUE(msg.Write(&out)); |
| 1109 ASSERT_EQ(size, out.Length()); | 1115 ASSERT_EQ(size, out.Length()); |
| 1110 // Check everything up to the padding | 1116 // Check everything up to the padding |
| 1111 ASSERT_EQ(0, memcmp(out.Data(), kStunMessageWithOriginAttribute, size - 2)); | 1117 ASSERT_EQ(0, memcmp(out.Data(), kStunMessageWithOriginAttribute, size - 2)); |
| 1112 } | 1118 } |
| 1113 | 1119 |
| 1114 // Test that we fail to read messages with invalid lengths. | 1120 // Test that we fail to read messages with invalid lengths. |
| 1115 void CheckFailureToRead(const unsigned char* testcase, size_t length) { | 1121 void CheckFailureToRead(const unsigned char* testcase, size_t length) { |
| 1116 StunMessage msg; | 1122 StunMessage msg; |
| 1117 const char* input = reinterpret_cast<const char*>(testcase); | 1123 const char* input = reinterpret_cast<const char*>(testcase); |
| 1118 rtc::ByteBuffer buf(input, length); | 1124 rtc::ByteBufferReader buf(input, length); |
| 1119 ASSERT_FALSE(msg.Read(&buf)); | 1125 ASSERT_FALSE(msg.Read(&buf)); |
| 1120 } | 1126 } |
| 1121 | 1127 |
| 1122 TEST_F(StunTest, FailToReadInvalidMessages) { | 1128 TEST_F(StunTest, FailToReadInvalidMessages) { |
| 1123 CheckFailureToRead(kStunMessageWithZeroLength, | 1129 CheckFailureToRead(kStunMessageWithZeroLength, |
| 1124 kRealLengthOfInvalidLengthTestCases); | 1130 kRealLengthOfInvalidLengthTestCases); |
| 1125 CheckFailureToRead(kStunMessageWithSmallLength, | 1131 CheckFailureToRead(kStunMessageWithSmallLength, |
| 1126 kRealLengthOfInvalidLengthTestCases); | 1132 kRealLengthOfInvalidLengthTestCases); |
| 1127 CheckFailureToRead(kStunMessageWithExcessLength, | 1133 CheckFailureToRead(kStunMessageWithExcessLength, |
| 1128 kRealLengthOfInvalidLengthTestCases); | 1134 kRealLengthOfInvalidLengthTestCases); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1200 EXPECT_EQ(i >= sizeof(buf) - 8, StunMessage::ValidateMessageIntegrity( | 1206 EXPECT_EQ(i >= sizeof(buf) - 8, StunMessage::ValidateMessageIntegrity( |
| 1201 buf, sizeof(buf), kRfc5769SampleMsgPassword)); | 1207 buf, sizeof(buf), kRfc5769SampleMsgPassword)); |
| 1202 } | 1208 } |
| 1203 } | 1209 } |
| 1204 | 1210 |
| 1205 // Validate that we generate correct MESSAGE-INTEGRITY attributes. | 1211 // Validate that we generate correct MESSAGE-INTEGRITY attributes. |
| 1206 // Note the use of IceMessage instead of StunMessage; this is necessary because | 1212 // Note the use of IceMessage instead of StunMessage; this is necessary because |
| 1207 // the RFC5769 test messages used include attributes not found in basic STUN. | 1213 // the RFC5769 test messages used include attributes not found in basic STUN. |
| 1208 TEST_F(StunTest, AddMessageIntegrity) { | 1214 TEST_F(StunTest, AddMessageIntegrity) { |
| 1209 IceMessage msg; | 1215 IceMessage msg; |
| 1210 rtc::ByteBuffer buf( | 1216 rtc::ByteBufferReader buf( |
| 1211 reinterpret_cast<const char*>(kRfc5769SampleRequestWithoutMI), | 1217 reinterpret_cast<const char*>(kRfc5769SampleRequestWithoutMI), |
| 1212 sizeof(kRfc5769SampleRequestWithoutMI)); | 1218 sizeof(kRfc5769SampleRequestWithoutMI)); |
| 1213 EXPECT_TRUE(msg.Read(&buf)); | 1219 EXPECT_TRUE(msg.Read(&buf)); |
| 1214 EXPECT_TRUE(msg.AddMessageIntegrity(kRfc5769SampleMsgPassword)); | 1220 EXPECT_TRUE(msg.AddMessageIntegrity(kRfc5769SampleMsgPassword)); |
| 1215 const StunByteStringAttribute* mi_attr = | 1221 const StunByteStringAttribute* mi_attr = |
| 1216 msg.GetByteString(STUN_ATTR_MESSAGE_INTEGRITY); | 1222 msg.GetByteString(STUN_ATTR_MESSAGE_INTEGRITY); |
| 1217 EXPECT_EQ(20U, mi_attr->length()); | 1223 EXPECT_EQ(20U, mi_attr->length()); |
| 1218 EXPECT_EQ(0, memcmp( | 1224 EXPECT_EQ(0, memcmp( |
| 1219 mi_attr->bytes(), kCalculatedHmac1, sizeof(kCalculatedHmac1))); | 1225 mi_attr->bytes(), kCalculatedHmac1, sizeof(kCalculatedHmac1))); |
| 1220 | 1226 |
| 1221 rtc::ByteBuffer buf1; | 1227 rtc::ByteBufferWriter buf1; |
| 1222 EXPECT_TRUE(msg.Write(&buf1)); | 1228 EXPECT_TRUE(msg.Write(&buf1)); |
| 1223 EXPECT_TRUE(StunMessage::ValidateMessageIntegrity( | 1229 EXPECT_TRUE(StunMessage::ValidateMessageIntegrity( |
| 1224 reinterpret_cast<const char*>(buf1.Data()), buf1.Length(), | 1230 reinterpret_cast<const char*>(buf1.Data()), buf1.Length(), |
| 1225 kRfc5769SampleMsgPassword)); | 1231 kRfc5769SampleMsgPassword)); |
| 1226 | 1232 |
| 1227 IceMessage msg2; | 1233 IceMessage msg2; |
| 1228 rtc::ByteBuffer buf2( | 1234 rtc::ByteBufferReader buf2( |
| 1229 reinterpret_cast<const char*>(kRfc5769SampleResponseWithoutMI), | 1235 reinterpret_cast<const char*>(kRfc5769SampleResponseWithoutMI), |
| 1230 sizeof(kRfc5769SampleResponseWithoutMI)); | 1236 sizeof(kRfc5769SampleResponseWithoutMI)); |
| 1231 EXPECT_TRUE(msg2.Read(&buf2)); | 1237 EXPECT_TRUE(msg2.Read(&buf2)); |
| 1232 EXPECT_TRUE(msg2.AddMessageIntegrity(kRfc5769SampleMsgPassword)); | 1238 EXPECT_TRUE(msg2.AddMessageIntegrity(kRfc5769SampleMsgPassword)); |
| 1233 const StunByteStringAttribute* mi_attr2 = | 1239 const StunByteStringAttribute* mi_attr2 = |
| 1234 msg2.GetByteString(STUN_ATTR_MESSAGE_INTEGRITY); | 1240 msg2.GetByteString(STUN_ATTR_MESSAGE_INTEGRITY); |
| 1235 EXPECT_EQ(20U, mi_attr2->length()); | 1241 EXPECT_EQ(20U, mi_attr2->length()); |
| 1236 EXPECT_EQ( | 1242 EXPECT_EQ( |
| 1237 0, memcmp(mi_attr2->bytes(), kCalculatedHmac2, sizeof(kCalculatedHmac2))); | 1243 0, memcmp(mi_attr2->bytes(), kCalculatedHmac2, sizeof(kCalculatedHmac2))); |
| 1238 | 1244 |
| 1239 rtc::ByteBuffer buf3; | 1245 rtc::ByteBufferWriter buf3; |
| 1240 EXPECT_TRUE(msg2.Write(&buf3)); | 1246 EXPECT_TRUE(msg2.Write(&buf3)); |
| 1241 EXPECT_TRUE(StunMessage::ValidateMessageIntegrity( | 1247 EXPECT_TRUE(StunMessage::ValidateMessageIntegrity( |
| 1242 reinterpret_cast<const char*>(buf3.Data()), buf3.Length(), | 1248 reinterpret_cast<const char*>(buf3.Data()), buf3.Length(), |
| 1243 kRfc5769SampleMsgPassword)); | 1249 kRfc5769SampleMsgPassword)); |
| 1244 } | 1250 } |
| 1245 | 1251 |
| 1246 // Check our STUN message validation code against the RFC5769 test messages. | 1252 // Check our STUN message validation code against the RFC5769 test messages. |
| 1247 TEST_F(StunTest, ValidateFingerprint) { | 1253 TEST_F(StunTest, ValidateFingerprint) { |
| 1248 EXPECT_TRUE(StunMessage::ValidateFingerprint( | 1254 EXPECT_TRUE(StunMessage::ValidateFingerprint( |
| 1249 reinterpret_cast<const char*>(kRfc5769SampleRequest), | 1255 reinterpret_cast<const char*>(kRfc5769SampleRequest), |
| (...skipping 25 matching lines...) Expand all Loading... |
| 1275 buf[i - 1] ^= 0x01; | 1281 buf[i - 1] ^= 0x01; |
| 1276 EXPECT_FALSE(StunMessage::ValidateFingerprint(buf, sizeof(buf))); | 1282 EXPECT_FALSE(StunMessage::ValidateFingerprint(buf, sizeof(buf))); |
| 1277 } | 1283 } |
| 1278 // Put them all back to normal and the check should pass again. | 1284 // Put them all back to normal and the check should pass again. |
| 1279 buf[sizeof(buf) - 1] ^= 0x01; | 1285 buf[sizeof(buf) - 1] ^= 0x01; |
| 1280 EXPECT_TRUE(StunMessage::ValidateFingerprint(buf, sizeof(buf))); | 1286 EXPECT_TRUE(StunMessage::ValidateFingerprint(buf, sizeof(buf))); |
| 1281 } | 1287 } |
| 1282 | 1288 |
| 1283 TEST_F(StunTest, AddFingerprint) { | 1289 TEST_F(StunTest, AddFingerprint) { |
| 1284 IceMessage msg; | 1290 IceMessage msg; |
| 1285 rtc::ByteBuffer buf( | 1291 rtc::ByteBufferReader buf( |
| 1286 reinterpret_cast<const char*>(kRfc5769SampleRequestWithoutMI), | 1292 reinterpret_cast<const char*>(kRfc5769SampleRequestWithoutMI), |
| 1287 sizeof(kRfc5769SampleRequestWithoutMI)); | 1293 sizeof(kRfc5769SampleRequestWithoutMI)); |
| 1288 EXPECT_TRUE(msg.Read(&buf)); | 1294 EXPECT_TRUE(msg.Read(&buf)); |
| 1289 EXPECT_TRUE(msg.AddFingerprint()); | 1295 EXPECT_TRUE(msg.AddFingerprint()); |
| 1290 | 1296 |
| 1291 rtc::ByteBuffer buf1; | 1297 rtc::ByteBufferWriter buf1; |
| 1292 EXPECT_TRUE(msg.Write(&buf1)); | 1298 EXPECT_TRUE(msg.Write(&buf1)); |
| 1293 EXPECT_TRUE(StunMessage::ValidateFingerprint( | 1299 EXPECT_TRUE(StunMessage::ValidateFingerprint( |
| 1294 reinterpret_cast<const char*>(buf1.Data()), buf1.Length())); | 1300 reinterpret_cast<const char*>(buf1.Data()), buf1.Length())); |
| 1295 } | 1301 } |
| 1296 | 1302 |
| 1297 // Sample "GTURN" relay message. | 1303 // Sample "GTURN" relay message. |
| 1298 static const unsigned char kRelayMessage[] = { | 1304 static const unsigned char kRelayMessage[] = { |
| 1299 0x00, 0x01, 0x00, 88, // message header | 1305 0x00, 0x01, 0x00, 88, // message header |
| 1300 0x21, 0x12, 0xA4, 0x42, // magic cookie | 1306 0x21, 0x12, 0xA4, 0x42, // magic cookie |
| 1301 '0', '1', '2', '3', // transaction id | 1307 '0', '1', '2', '3', // transaction id |
| (...skipping 22 matching lines...) Expand all Loading... |
| 1324 'a', 'b', 'c', 'd', | 1330 'a', 'b', 'c', 'd', |
| 1325 'e', 'f', 'g', 0 // DATA must be padded per rfc5766. | 1331 'e', 'f', 'g', 0 // DATA must be padded per rfc5766. |
| 1326 }; | 1332 }; |
| 1327 | 1333 |
| 1328 // Test that we can read the GTURN-specific fields. | 1334 // Test that we can read the GTURN-specific fields. |
| 1329 TEST_F(StunTest, ReadRelayMessage) { | 1335 TEST_F(StunTest, ReadRelayMessage) { |
| 1330 RelayMessage msg, msg2; | 1336 RelayMessage msg, msg2; |
| 1331 | 1337 |
| 1332 const char* input = reinterpret_cast<const char*>(kRelayMessage); | 1338 const char* input = reinterpret_cast<const char*>(kRelayMessage); |
| 1333 size_t size = sizeof(kRelayMessage); | 1339 size_t size = sizeof(kRelayMessage); |
| 1334 rtc::ByteBuffer buf(input, size); | 1340 rtc::ByteBufferReader buf(input, size); |
| 1335 EXPECT_TRUE(msg.Read(&buf)); | 1341 EXPECT_TRUE(msg.Read(&buf)); |
| 1336 | 1342 |
| 1337 EXPECT_EQ(STUN_BINDING_REQUEST, msg.type()); | 1343 EXPECT_EQ(STUN_BINDING_REQUEST, msg.type()); |
| 1338 EXPECT_EQ(size - 20, msg.length()); | 1344 EXPECT_EQ(size - 20, msg.length()); |
| 1339 EXPECT_EQ("0123456789ab", msg.transaction_id()); | 1345 EXPECT_EQ("0123456789ab", msg.transaction_id()); |
| 1340 | 1346 |
| 1341 msg2.SetType(STUN_BINDING_REQUEST); | 1347 msg2.SetType(STUN_BINDING_REQUEST); |
| 1342 msg2.SetTransactionID("0123456789ab"); | 1348 msg2.SetTransactionID("0123456789ab"); |
| 1343 | 1349 |
| 1344 in_addr legacy_in_addr; | 1350 in_addr legacy_in_addr; |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1420 | 1426 |
| 1421 bytes = msg.GetByteString(STUN_ATTR_DATA); | 1427 bytes = msg.GetByteString(STUN_ATTR_DATA); |
| 1422 ASSERT_TRUE(bytes != NULL); | 1428 ASSERT_TRUE(bytes != NULL); |
| 1423 EXPECT_EQ(7U, bytes->length()); | 1429 EXPECT_EQ(7U, bytes->length()); |
| 1424 EXPECT_EQ("abcdefg", bytes->GetString()); | 1430 EXPECT_EQ("abcdefg", bytes->GetString()); |
| 1425 | 1431 |
| 1426 bytes2 = StunAttribute::CreateByteString(STUN_ATTR_DATA); | 1432 bytes2 = StunAttribute::CreateByteString(STUN_ATTR_DATA); |
| 1427 bytes2->CopyBytes("abcdefg"); | 1433 bytes2->CopyBytes("abcdefg"); |
| 1428 EXPECT_TRUE(msg2.AddAttribute(bytes2)); | 1434 EXPECT_TRUE(msg2.AddAttribute(bytes2)); |
| 1429 | 1435 |
| 1430 rtc::ByteBuffer out; | 1436 rtc::ByteBufferWriter out; |
| 1431 EXPECT_TRUE(msg.Write(&out)); | 1437 EXPECT_TRUE(msg.Write(&out)); |
| 1432 EXPECT_EQ(size, out.Length()); | 1438 EXPECT_EQ(size, out.Length()); |
| 1433 size_t len1 = out.Length(); | 1439 size_t len1 = out.Length(); |
| 1440 rtc::ByteBufferReader read_buf(out); |
| 1434 std::string outstring; | 1441 std::string outstring; |
| 1435 out.ReadString(&outstring, len1); | 1442 read_buf.ReadString(&outstring, len1); |
| 1436 EXPECT_EQ(0, memcmp(outstring.c_str(), input, len1)); | 1443 EXPECT_EQ(0, memcmp(outstring.c_str(), input, len1)); |
| 1437 | 1444 |
| 1438 rtc::ByteBuffer out2; | 1445 rtc::ByteBufferWriter out2; |
| 1439 EXPECT_TRUE(msg2.Write(&out2)); | 1446 EXPECT_TRUE(msg2.Write(&out2)); |
| 1440 EXPECT_EQ(size, out2.Length()); | 1447 EXPECT_EQ(size, out2.Length()); |
| 1441 size_t len2 = out2.Length(); | 1448 size_t len2 = out2.Length(); |
| 1449 rtc::ByteBufferReader read_buf2(out2); |
| 1442 std::string outstring2; | 1450 std::string outstring2; |
| 1443 out2.ReadString(&outstring2, len2); | 1451 read_buf2.ReadString(&outstring2, len2); |
| 1444 EXPECT_EQ(0, memcmp(outstring2.c_str(), input, len2)); | 1452 EXPECT_EQ(0, memcmp(outstring2.c_str(), input, len2)); |
| 1445 } | 1453 } |
| 1446 | 1454 |
| 1447 } // namespace cricket | 1455 } // namespace cricket |
| OLD | NEW |