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 596 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
607 // Read the RFC5389 fields from the RFC5769 sample STUN request. | 607 // Read the RFC5389 fields from the RFC5769 sample STUN request. |
608 TEST_F(StunTest, ReadRfc5769RequestMessage) { | 608 TEST_F(StunTest, ReadRfc5769RequestMessage) { |
609 StunMessage msg; | 609 StunMessage msg; |
610 size_t size = ReadStunMessage(&msg, kRfc5769SampleRequest); | 610 size_t size = ReadStunMessage(&msg, kRfc5769SampleRequest); |
611 CheckStunHeader(msg, STUN_BINDING_REQUEST, size); | 611 CheckStunHeader(msg, STUN_BINDING_REQUEST, size); |
612 CheckStunTransactionID(msg, kRfc5769SampleMsgTransactionId, | 612 CheckStunTransactionID(msg, kRfc5769SampleMsgTransactionId, |
613 kStunTransactionIdLength); | 613 kStunTransactionIdLength); |
614 | 614 |
615 const StunByteStringAttribute* software = | 615 const StunByteStringAttribute* software = |
616 msg.GetByteString(STUN_ATTR_SOFTWARE); | 616 msg.GetByteString(STUN_ATTR_SOFTWARE); |
617 ASSERT_TRUE(software != NULL); | 617 ASSERT_TRUE(software != nullptr); |
618 EXPECT_EQ(kRfc5769SampleMsgClientSoftware, software->GetString()); | 618 EXPECT_EQ(kRfc5769SampleMsgClientSoftware, software->GetString()); |
619 | 619 |
620 const StunByteStringAttribute* username = | 620 const StunByteStringAttribute* username = |
621 msg.GetByteString(STUN_ATTR_USERNAME); | 621 msg.GetByteString(STUN_ATTR_USERNAME); |
622 ASSERT_TRUE(username != NULL); | 622 ASSERT_TRUE(username != nullptr); |
623 EXPECT_EQ(kRfc5769SampleMsgUsername, username->GetString()); | 623 EXPECT_EQ(kRfc5769SampleMsgUsername, username->GetString()); |
624 | 624 |
625 // Actual M-I value checked in a later test. | 625 // Actual M-I value checked in a later test. |
626 ASSERT_TRUE(msg.GetByteString(STUN_ATTR_MESSAGE_INTEGRITY) != NULL); | 626 ASSERT_TRUE(msg.GetByteString(STUN_ATTR_MESSAGE_INTEGRITY) != nullptr); |
627 | 627 |
628 // Fingerprint checked in a later test, but double-check the value here. | 628 // Fingerprint checked in a later test, but double-check the value here. |
629 const StunUInt32Attribute* fingerprint = | 629 const StunUInt32Attribute* fingerprint = |
630 msg.GetUInt32(STUN_ATTR_FINGERPRINT); | 630 msg.GetUInt32(STUN_ATTR_FINGERPRINT); |
631 ASSERT_TRUE(fingerprint != NULL); | 631 ASSERT_TRUE(fingerprint != nullptr); |
632 EXPECT_EQ(0xe57a3bcf, fingerprint->value()); | 632 EXPECT_EQ(0xe57a3bcf, fingerprint->value()); |
633 } | 633 } |
634 | 634 |
635 // Read the RFC5389 fields from the RFC5769 sample STUN response. | 635 // Read the RFC5389 fields from the RFC5769 sample STUN response. |
636 TEST_F(StunTest, ReadRfc5769ResponseMessage) { | 636 TEST_F(StunTest, ReadRfc5769ResponseMessage) { |
637 StunMessage msg; | 637 StunMessage msg; |
638 size_t size = ReadStunMessage(&msg, kRfc5769SampleResponse); | 638 size_t size = ReadStunMessage(&msg, kRfc5769SampleResponse); |
639 CheckStunHeader(msg, STUN_BINDING_RESPONSE, size); | 639 CheckStunHeader(msg, STUN_BINDING_RESPONSE, size); |
640 CheckStunTransactionID(msg, kRfc5769SampleMsgTransactionId, | 640 CheckStunTransactionID(msg, kRfc5769SampleMsgTransactionId, |
641 kStunTransactionIdLength); | 641 kStunTransactionIdLength); |
642 | 642 |
643 const StunByteStringAttribute* software = | 643 const StunByteStringAttribute* software = |
644 msg.GetByteString(STUN_ATTR_SOFTWARE); | 644 msg.GetByteString(STUN_ATTR_SOFTWARE); |
645 ASSERT_TRUE(software != NULL); | 645 ASSERT_TRUE(software != nullptr); |
646 EXPECT_EQ(kRfc5769SampleMsgServerSoftware, software->GetString()); | 646 EXPECT_EQ(kRfc5769SampleMsgServerSoftware, software->GetString()); |
647 | 647 |
648 const StunAddressAttribute* mapped_address = | 648 const StunAddressAttribute* mapped_address = |
649 msg.GetAddress(STUN_ATTR_XOR_MAPPED_ADDRESS); | 649 msg.GetAddress(STUN_ATTR_XOR_MAPPED_ADDRESS); |
650 ASSERT_TRUE(mapped_address != NULL); | 650 ASSERT_TRUE(mapped_address != nullptr); |
651 EXPECT_EQ(kRfc5769SampleMsgMappedAddress, mapped_address->GetAddress()); | 651 EXPECT_EQ(kRfc5769SampleMsgMappedAddress, mapped_address->GetAddress()); |
652 | 652 |
653 // Actual M-I and fingerprint checked in later tests. | 653 // Actual M-I and fingerprint checked in later tests. |
654 ASSERT_TRUE(msg.GetByteString(STUN_ATTR_MESSAGE_INTEGRITY) != NULL); | 654 ASSERT_TRUE(msg.GetByteString(STUN_ATTR_MESSAGE_INTEGRITY) != nullptr); |
655 ASSERT_TRUE(msg.GetUInt32(STUN_ATTR_FINGERPRINT) != NULL); | 655 ASSERT_TRUE(msg.GetUInt32(STUN_ATTR_FINGERPRINT) != nullptr); |
656 } | 656 } |
657 | 657 |
658 // Read the RFC5389 fields from the RFC5769 sample STUN response for IPv6. | 658 // Read the RFC5389 fields from the RFC5769 sample STUN response for IPv6. |
659 TEST_F(StunTest, ReadRfc5769ResponseMessageIPv6) { | 659 TEST_F(StunTest, ReadRfc5769ResponseMessageIPv6) { |
660 StunMessage msg; | 660 StunMessage msg; |
661 size_t size = ReadStunMessage(&msg, kRfc5769SampleResponseIPv6); | 661 size_t size = ReadStunMessage(&msg, kRfc5769SampleResponseIPv6); |
662 CheckStunHeader(msg, STUN_BINDING_RESPONSE, size); | 662 CheckStunHeader(msg, STUN_BINDING_RESPONSE, size); |
663 CheckStunTransactionID(msg, kRfc5769SampleMsgTransactionId, | 663 CheckStunTransactionID(msg, kRfc5769SampleMsgTransactionId, |
664 kStunTransactionIdLength); | 664 kStunTransactionIdLength); |
665 | 665 |
666 const StunByteStringAttribute* software = | 666 const StunByteStringAttribute* software = |
667 msg.GetByteString(STUN_ATTR_SOFTWARE); | 667 msg.GetByteString(STUN_ATTR_SOFTWARE); |
668 ASSERT_TRUE(software != NULL); | 668 ASSERT_TRUE(software != nullptr); |
669 EXPECT_EQ(kRfc5769SampleMsgServerSoftware, software->GetString()); | 669 EXPECT_EQ(kRfc5769SampleMsgServerSoftware, software->GetString()); |
670 | 670 |
671 const StunAddressAttribute* mapped_address = | 671 const StunAddressAttribute* mapped_address = |
672 msg.GetAddress(STUN_ATTR_XOR_MAPPED_ADDRESS); | 672 msg.GetAddress(STUN_ATTR_XOR_MAPPED_ADDRESS); |
673 ASSERT_TRUE(mapped_address != NULL); | 673 ASSERT_TRUE(mapped_address != nullptr); |
674 EXPECT_EQ(kRfc5769SampleMsgIPv6MappedAddress, mapped_address->GetAddress()); | 674 EXPECT_EQ(kRfc5769SampleMsgIPv6MappedAddress, mapped_address->GetAddress()); |
675 | 675 |
676 // Actual M-I and fingerprint checked in later tests. | 676 // Actual M-I and fingerprint checked in later tests. |
677 ASSERT_TRUE(msg.GetByteString(STUN_ATTR_MESSAGE_INTEGRITY) != NULL); | 677 ASSERT_TRUE(msg.GetByteString(STUN_ATTR_MESSAGE_INTEGRITY) != nullptr); |
678 ASSERT_TRUE(msg.GetUInt32(STUN_ATTR_FINGERPRINT) != NULL); | 678 ASSERT_TRUE(msg.GetUInt32(STUN_ATTR_FINGERPRINT) != nullptr); |
679 } | 679 } |
680 | 680 |
681 // Read the RFC5389 fields from the RFC5769 sample STUN response with auth. | 681 // Read the RFC5389 fields from the RFC5769 sample STUN response with auth. |
682 TEST_F(StunTest, ReadRfc5769RequestMessageLongTermAuth) { | 682 TEST_F(StunTest, ReadRfc5769RequestMessageLongTermAuth) { |
683 StunMessage msg; | 683 StunMessage msg; |
684 size_t size = ReadStunMessage(&msg, kRfc5769SampleRequestLongTermAuth); | 684 size_t size = ReadStunMessage(&msg, kRfc5769SampleRequestLongTermAuth); |
685 CheckStunHeader(msg, STUN_BINDING_REQUEST, size); | 685 CheckStunHeader(msg, STUN_BINDING_REQUEST, size); |
686 CheckStunTransactionID(msg, kRfc5769SampleMsgWithAuthTransactionId, | 686 CheckStunTransactionID(msg, kRfc5769SampleMsgWithAuthTransactionId, |
687 kStunTransactionIdLength); | 687 kStunTransactionIdLength); |
688 | 688 |
689 const StunByteStringAttribute* username = | 689 const StunByteStringAttribute* username = |
690 msg.GetByteString(STUN_ATTR_USERNAME); | 690 msg.GetByteString(STUN_ATTR_USERNAME); |
691 ASSERT_TRUE(username != NULL); | 691 ASSERT_TRUE(username != nullptr); |
692 EXPECT_EQ(kRfc5769SampleMsgWithAuthUsername, username->GetString()); | 692 EXPECT_EQ(kRfc5769SampleMsgWithAuthUsername, username->GetString()); |
693 | 693 |
694 const StunByteStringAttribute* nonce = | 694 const StunByteStringAttribute* nonce = |
695 msg.GetByteString(STUN_ATTR_NONCE); | 695 msg.GetByteString(STUN_ATTR_NONCE); |
696 ASSERT_TRUE(nonce != NULL); | 696 ASSERT_TRUE(nonce != nullptr); |
697 EXPECT_EQ(kRfc5769SampleMsgWithAuthNonce, nonce->GetString()); | 697 EXPECT_EQ(kRfc5769SampleMsgWithAuthNonce, nonce->GetString()); |
698 | 698 |
699 const StunByteStringAttribute* realm = | 699 const StunByteStringAttribute* realm = |
700 msg.GetByteString(STUN_ATTR_REALM); | 700 msg.GetByteString(STUN_ATTR_REALM); |
701 ASSERT_TRUE(realm != NULL); | 701 ASSERT_TRUE(realm != nullptr); |
702 EXPECT_EQ(kRfc5769SampleMsgWithAuthRealm, realm->GetString()); | 702 EXPECT_EQ(kRfc5769SampleMsgWithAuthRealm, realm->GetString()); |
703 | 703 |
704 // No fingerprint, actual M-I checked in later tests. | 704 // No fingerprint, actual M-I checked in later tests. |
705 ASSERT_TRUE(msg.GetByteString(STUN_ATTR_MESSAGE_INTEGRITY) != NULL); | 705 ASSERT_TRUE(msg.GetByteString(STUN_ATTR_MESSAGE_INTEGRITY) != nullptr); |
706 ASSERT_TRUE(msg.GetUInt32(STUN_ATTR_FINGERPRINT) == NULL); | 706 ASSERT_TRUE(msg.GetUInt32(STUN_ATTR_FINGERPRINT) == nullptr); |
707 } | 707 } |
708 | 708 |
709 // The RFC3489 packet in this test is the same as | 709 // The RFC3489 packet in this test is the same as |
710 // kStunMessageWithIPv4MappedAddress, but with a different value where the | 710 // kStunMessageWithIPv4MappedAddress, but with a different value where the |
711 // magic cookie was. | 711 // magic cookie was. |
712 TEST_F(StunTest, ReadLegacyMessage) { | 712 TEST_F(StunTest, ReadLegacyMessage) { |
713 unsigned char rfc3489_packet[sizeof(kStunMessageWithIPv4MappedAddress)]; | 713 unsigned char rfc3489_packet[sizeof(kStunMessageWithIPv4MappedAddress)]; |
714 memcpy(rfc3489_packet, kStunMessageWithIPv4MappedAddress, | 714 memcpy(rfc3489_packet, kStunMessageWithIPv4MappedAddress, |
715 sizeof(kStunMessageWithIPv4MappedAddress)); | 715 sizeof(kStunMessageWithIPv4MappedAddress)); |
716 // Overwrite the magic cookie here. | 716 // Overwrite the magic cookie here. |
(...skipping 20 matching lines...) Expand all Loading... |
737 CheckStunHeader(msg, STUN_BINDING_RESPONSE, size); | 737 CheckStunHeader(msg, STUN_BINDING_RESPONSE, size); |
738 CheckStunTransactionID(msg, kTestTransactionId2, kStunTransactionIdLength); | 738 CheckStunTransactionID(msg, kTestTransactionId2, kStunTransactionIdLength); |
739 | 739 |
740 const StunAddressAttribute* addr = | 740 const StunAddressAttribute* addr = |
741 msg.GetAddress(STUN_ATTR_XOR_MAPPED_ADDRESS); | 741 msg.GetAddress(STUN_ATTR_XOR_MAPPED_ADDRESS); |
742 CheckStunAddressAttribute(addr, STUN_ADDRESS_IPV6, | 742 CheckStunAddressAttribute(addr, STUN_ADDRESS_IPV6, |
743 kTestMessagePort1, test_address); | 743 kTestMessagePort1, test_address); |
744 | 744 |
745 // Owner with a different transaction ID. | 745 // Owner with a different transaction ID. |
746 msg2.SetTransactionID("ABCDABCDABCD"); | 746 msg2.SetTransactionID("ABCDABCDABCD"); |
747 StunXorAddressAttribute addr2(STUN_ATTR_XOR_MAPPED_ADDRESS, 20, NULL); | 747 StunXorAddressAttribute addr2(STUN_ATTR_XOR_MAPPED_ADDRESS, 20, nullptr); |
748 addr2.SetIP(addr->ipaddr()); | 748 addr2.SetIP(addr->ipaddr()); |
749 addr2.SetPort(addr->port()); | 749 addr2.SetPort(addr->port()); |
750 addr2.SetOwner(&msg2); | 750 addr2.SetOwner(&msg2); |
751 // The internal IP address shouldn't change. | 751 // The internal IP address shouldn't change. |
752 ASSERT_EQ(addr2.ipaddr(), addr->ipaddr()); | 752 ASSERT_EQ(addr2.ipaddr(), addr->ipaddr()); |
753 | 753 |
754 rtc::ByteBufferWriter correct_buf; | 754 rtc::ByteBufferWriter correct_buf; |
755 rtc::ByteBufferWriter wrong_buf; | 755 rtc::ByteBufferWriter wrong_buf; |
756 EXPECT_TRUE(addr->Write(&correct_buf)); | 756 EXPECT_TRUE(addr->Write(&correct_buf)); |
757 EXPECT_TRUE(addr2.Write(&wrong_buf)); | 757 EXPECT_TRUE(addr2.Write(&wrong_buf)); |
758 // But when written out, the buffers should look different. | 758 // But when written out, the buffers should look different. |
759 ASSERT_NE(0, | 759 ASSERT_NE(0, |
760 memcmp(correct_buf.Data(), wrong_buf.Data(), wrong_buf.Length())); | 760 memcmp(correct_buf.Data(), wrong_buf.Data(), wrong_buf.Length())); |
761 // And when reading a known good value, the address should be wrong. | 761 // And when reading a known good value, the address should be wrong. |
762 rtc::ByteBufferReader read_buf(correct_buf); | 762 rtc::ByteBufferReader read_buf(correct_buf); |
763 addr2.Read(&read_buf); | 763 addr2.Read(&read_buf); |
764 ASSERT_NE(addr->ipaddr(), addr2.ipaddr()); | 764 ASSERT_NE(addr->ipaddr(), addr2.ipaddr()); |
765 addr2.SetIP(addr->ipaddr()); | 765 addr2.SetIP(addr->ipaddr()); |
766 addr2.SetPort(addr->port()); | 766 addr2.SetPort(addr->port()); |
767 // Try writing with no owner at all, should fail and write nothing. | 767 // Try writing with no owner at all, should fail and write nothing. |
768 addr2.SetOwner(NULL); | 768 addr2.SetOwner(nullptr); |
769 ASSERT_EQ(addr2.ipaddr(), addr->ipaddr()); | 769 ASSERT_EQ(addr2.ipaddr(), addr->ipaddr()); |
770 wrong_buf.Clear(); | 770 wrong_buf.Clear(); |
771 EXPECT_FALSE(addr2.Write(&wrong_buf)); | 771 EXPECT_FALSE(addr2.Write(&wrong_buf)); |
772 ASSERT_EQ(0U, wrong_buf.Length()); | 772 ASSERT_EQ(0U, wrong_buf.Length()); |
773 } | 773 } |
774 | 774 |
775 TEST_F(StunTest, SetIPv4XorAddressAttributeOwner) { | 775 TEST_F(StunTest, SetIPv4XorAddressAttributeOwner) { |
776 // Unlike the IPv6XorAddressAttributeOwner test, IPv4 XOR address attributes | 776 // Unlike the IPv6XorAddressAttributeOwner test, IPv4 XOR address attributes |
777 // should _not_ be affected by a change in owner. IPv4 XOR address uses the | 777 // should _not_ be affected by a change in owner. IPv4 XOR address uses the |
778 // magic cookie value which is fixed. | 778 // magic cookie value which is fixed. |
779 StunMessage msg; | 779 StunMessage msg; |
780 StunMessage msg2; | 780 StunMessage msg2; |
781 size_t size = ReadStunMessage(&msg, kStunMessageWithIPv4XorMappedAddress); | 781 size_t size = ReadStunMessage(&msg, kStunMessageWithIPv4XorMappedAddress); |
782 | 782 |
783 rtc::IPAddress test_address(kIPv4TestAddress1); | 783 rtc::IPAddress test_address(kIPv4TestAddress1); |
784 | 784 |
785 CheckStunHeader(msg, STUN_BINDING_RESPONSE, size); | 785 CheckStunHeader(msg, STUN_BINDING_RESPONSE, size); |
786 CheckStunTransactionID(msg, kTestTransactionId1, kStunTransactionIdLength); | 786 CheckStunTransactionID(msg, kTestTransactionId1, kStunTransactionIdLength); |
787 | 787 |
788 const StunAddressAttribute* addr = | 788 const StunAddressAttribute* addr = |
789 msg.GetAddress(STUN_ATTR_XOR_MAPPED_ADDRESS); | 789 msg.GetAddress(STUN_ATTR_XOR_MAPPED_ADDRESS); |
790 CheckStunAddressAttribute(addr, STUN_ADDRESS_IPV4, | 790 CheckStunAddressAttribute(addr, STUN_ADDRESS_IPV4, |
791 kTestMessagePort3, test_address); | 791 kTestMessagePort3, test_address); |
792 | 792 |
793 // Owner with a different transaction ID. | 793 // Owner with a different transaction ID. |
794 msg2.SetTransactionID("ABCDABCDABCD"); | 794 msg2.SetTransactionID("ABCDABCDABCD"); |
795 StunXorAddressAttribute addr2(STUN_ATTR_XOR_MAPPED_ADDRESS, 20, NULL); | 795 StunXorAddressAttribute addr2(STUN_ATTR_XOR_MAPPED_ADDRESS, 20, nullptr); |
796 addr2.SetIP(addr->ipaddr()); | 796 addr2.SetIP(addr->ipaddr()); |
797 addr2.SetPort(addr->port()); | 797 addr2.SetPort(addr->port()); |
798 addr2.SetOwner(&msg2); | 798 addr2.SetOwner(&msg2); |
799 // The internal IP address shouldn't change. | 799 // The internal IP address shouldn't change. |
800 ASSERT_EQ(addr2.ipaddr(), addr->ipaddr()); | 800 ASSERT_EQ(addr2.ipaddr(), addr->ipaddr()); |
801 | 801 |
802 rtc::ByteBufferWriter correct_buf; | 802 rtc::ByteBufferWriter correct_buf; |
803 rtc::ByteBufferWriter wrong_buf; | 803 rtc::ByteBufferWriter wrong_buf; |
804 EXPECT_TRUE(addr->Write(&correct_buf)); | 804 EXPECT_TRUE(addr->Write(&correct_buf)); |
805 EXPECT_TRUE(addr2.Write(&wrong_buf)); | 805 EXPECT_TRUE(addr2.Write(&wrong_buf)); |
806 // The same address data should be written. | 806 // The same address data should be written. |
807 ASSERT_EQ(0, | 807 ASSERT_EQ(0, |
808 memcmp(correct_buf.Data(), wrong_buf.Data(), wrong_buf.Length())); | 808 memcmp(correct_buf.Data(), wrong_buf.Data(), wrong_buf.Length())); |
809 // And an attribute should be able to un-XOR an address belonging to a message | 809 // And an attribute should be able to un-XOR an address belonging to a message |
810 // with a different transaction ID. | 810 // with a different transaction ID. |
811 rtc::ByteBufferReader read_buf(correct_buf); | 811 rtc::ByteBufferReader read_buf(correct_buf); |
812 EXPECT_TRUE(addr2.Read(&read_buf)); | 812 EXPECT_TRUE(addr2.Read(&read_buf)); |
813 ASSERT_EQ(addr->ipaddr(), addr2.ipaddr()); | 813 ASSERT_EQ(addr->ipaddr(), addr2.ipaddr()); |
814 | 814 |
815 // However, no owner is still an error, should fail and write nothing. | 815 // However, no owner is still an error, should fail and write nothing. |
816 addr2.SetOwner(NULL); | 816 addr2.SetOwner(nullptr); |
817 ASSERT_EQ(addr2.ipaddr(), addr->ipaddr()); | 817 ASSERT_EQ(addr2.ipaddr(), addr->ipaddr()); |
818 wrong_buf.Clear(); | 818 wrong_buf.Clear(); |
819 EXPECT_FALSE(addr2.Write(&wrong_buf)); | 819 EXPECT_FALSE(addr2.Write(&wrong_buf)); |
820 } | 820 } |
821 | 821 |
822 TEST_F(StunTest, CreateIPv6AddressAttribute) { | 822 TEST_F(StunTest, CreateIPv6AddressAttribute) { |
823 rtc::IPAddress test_ip(kIPv6TestAddress2); | 823 rtc::IPAddress test_ip(kIPv6TestAddress2); |
824 | 824 |
825 StunAddressAttribute* addr = | 825 StunAddressAttribute* addr = |
826 StunAttribute::CreateAddress(STUN_ATTR_MAPPED_ADDRESS); | 826 StunAttribute::CreateAddress(STUN_ATTR_MAPPED_ADDRESS); |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
992 } | 992 } |
993 | 993 |
994 TEST_F(StunTest, ReadByteStringAttribute) { | 994 TEST_F(StunTest, ReadByteStringAttribute) { |
995 StunMessage msg; | 995 StunMessage msg; |
996 size_t size = ReadStunMessage(&msg, kStunMessageWithByteStringAttribute); | 996 size_t size = ReadStunMessage(&msg, kStunMessageWithByteStringAttribute); |
997 | 997 |
998 CheckStunHeader(msg, STUN_BINDING_REQUEST, size); | 998 CheckStunHeader(msg, STUN_BINDING_REQUEST, size); |
999 CheckStunTransactionID(msg, kTestTransactionId2, kStunTransactionIdLength); | 999 CheckStunTransactionID(msg, kTestTransactionId2, kStunTransactionIdLength); |
1000 const StunByteStringAttribute* username = | 1000 const StunByteStringAttribute* username = |
1001 msg.GetByteString(STUN_ATTR_USERNAME); | 1001 msg.GetByteString(STUN_ATTR_USERNAME); |
1002 ASSERT_TRUE(username != NULL); | 1002 ASSERT_TRUE(username != nullptr); |
1003 EXPECT_EQ(kTestUserName1, username->GetString()); | 1003 EXPECT_EQ(kTestUserName1, username->GetString()); |
1004 } | 1004 } |
1005 | 1005 |
1006 TEST_F(StunTest, ReadPaddedByteStringAttribute) { | 1006 TEST_F(StunTest, ReadPaddedByteStringAttribute) { |
1007 StunMessage msg; | 1007 StunMessage msg; |
1008 size_t size = ReadStunMessage(&msg, | 1008 size_t size = ReadStunMessage(&msg, |
1009 kStunMessageWithPaddedByteStringAttribute); | 1009 kStunMessageWithPaddedByteStringAttribute); |
1010 ASSERT_NE(0U, size); | 1010 ASSERT_NE(0U, size); |
1011 CheckStunHeader(msg, STUN_BINDING_REQUEST, size); | 1011 CheckStunHeader(msg, STUN_BINDING_REQUEST, size); |
1012 CheckStunTransactionID(msg, kTestTransactionId2, kStunTransactionIdLength); | 1012 CheckStunTransactionID(msg, kTestTransactionId2, kStunTransactionIdLength); |
1013 const StunByteStringAttribute* username = | 1013 const StunByteStringAttribute* username = |
1014 msg.GetByteString(STUN_ATTR_USERNAME); | 1014 msg.GetByteString(STUN_ATTR_USERNAME); |
1015 ASSERT_TRUE(username != NULL); | 1015 ASSERT_TRUE(username != nullptr); |
1016 EXPECT_EQ(kTestUserName2, username->GetString()); | 1016 EXPECT_EQ(kTestUserName2, username->GetString()); |
1017 } | 1017 } |
1018 | 1018 |
1019 TEST_F(StunTest, ReadErrorCodeAttribute) { | 1019 TEST_F(StunTest, ReadErrorCodeAttribute) { |
1020 StunMessage msg; | 1020 StunMessage msg; |
1021 size_t size = ReadStunMessage(&msg, kStunMessageWithErrorAttribute); | 1021 size_t size = ReadStunMessage(&msg, kStunMessageWithErrorAttribute); |
1022 | 1022 |
1023 CheckStunHeader(msg, STUN_BINDING_ERROR_RESPONSE, size); | 1023 CheckStunHeader(msg, STUN_BINDING_ERROR_RESPONSE, size); |
1024 CheckStunTransactionID(msg, kTestTransactionId1, kStunTransactionIdLength); | 1024 CheckStunTransactionID(msg, kTestTransactionId1, kStunTransactionIdLength); |
1025 const StunErrorCodeAttribute* errorcode = msg.GetErrorCode(); | 1025 const StunErrorCodeAttribute* errorcode = msg.GetErrorCode(); |
1026 ASSERT_TRUE(errorcode != NULL); | 1026 ASSERT_TRUE(errorcode != nullptr); |
1027 EXPECT_EQ(kTestErrorClass, errorcode->eclass()); | 1027 EXPECT_EQ(kTestErrorClass, errorcode->eclass()); |
1028 EXPECT_EQ(kTestErrorNumber, errorcode->number()); | 1028 EXPECT_EQ(kTestErrorNumber, errorcode->number()); |
1029 EXPECT_EQ(kTestErrorReason, errorcode->reason()); | 1029 EXPECT_EQ(kTestErrorReason, errorcode->reason()); |
1030 EXPECT_EQ(kTestErrorCode, errorcode->code()); | 1030 EXPECT_EQ(kTestErrorCode, errorcode->code()); |
1031 } | 1031 } |
1032 | 1032 |
1033 TEST_F(StunTest, ReadMessageWithAUInt16ListAttribute) { | 1033 TEST_F(StunTest, ReadMessageWithAUInt16ListAttribute) { |
1034 StunMessage msg; | 1034 StunMessage msg; |
1035 size_t size = ReadStunMessage(&msg, kStunMessageWithUInt16ListAttribute); | 1035 size_t size = ReadStunMessage(&msg, kStunMessageWithUInt16ListAttribute); |
1036 CheckStunHeader(msg, STUN_BINDING_REQUEST, size); | 1036 CheckStunHeader(msg, STUN_BINDING_REQUEST, size); |
1037 const StunUInt16ListAttribute* types = msg.GetUnknownAttributes(); | 1037 const StunUInt16ListAttribute* types = msg.GetUnknownAttributes(); |
1038 ASSERT_TRUE(types != NULL); | 1038 ASSERT_TRUE(types != nullptr); |
1039 EXPECT_EQ(3U, types->Size()); | 1039 EXPECT_EQ(3U, types->Size()); |
1040 EXPECT_EQ(0x1U, types->GetType(0)); | 1040 EXPECT_EQ(0x1U, types->GetType(0)); |
1041 EXPECT_EQ(0x1000U, types->GetType(1)); | 1041 EXPECT_EQ(0x1000U, types->GetType(1)); |
1042 EXPECT_EQ(0xAB0CU, types->GetType(2)); | 1042 EXPECT_EQ(0xAB0CU, types->GetType(2)); |
1043 } | 1043 } |
1044 | 1044 |
1045 TEST_F(StunTest, ReadMessageWithAnUnknownAttribute) { | 1045 TEST_F(StunTest, ReadMessageWithAnUnknownAttribute) { |
1046 StunMessage msg; | 1046 StunMessage msg; |
1047 size_t size = ReadStunMessage(&msg, kStunMessageWithUnknownAttribute); | 1047 size_t size = ReadStunMessage(&msg, kStunMessageWithUnknownAttribute); |
1048 CheckStunHeader(msg, STUN_BINDING_REQUEST, size); | 1048 CheckStunHeader(msg, STUN_BINDING_REQUEST, size); |
1049 | 1049 |
1050 // Parsing should have succeeded and there should be a USERNAME attribute | 1050 // Parsing should have succeeded and there should be a USERNAME attribute |
1051 const StunByteStringAttribute* username = | 1051 const StunByteStringAttribute* username = |
1052 msg.GetByteString(STUN_ATTR_USERNAME); | 1052 msg.GetByteString(STUN_ATTR_USERNAME); |
1053 ASSERT_TRUE(username != NULL); | 1053 ASSERT_TRUE(username != nullptr); |
1054 EXPECT_EQ(kTestUserName2, username->GetString()); | 1054 EXPECT_EQ(kTestUserName2, username->GetString()); |
1055 } | 1055 } |
1056 | 1056 |
1057 TEST_F(StunTest, ReadMessageWithOriginAttribute) { | 1057 TEST_F(StunTest, ReadMessageWithOriginAttribute) { |
1058 StunMessage msg; | 1058 StunMessage msg; |
1059 size_t size = ReadStunMessage(&msg, kStunMessageWithOriginAttribute); | 1059 size_t size = ReadStunMessage(&msg, kStunMessageWithOriginAttribute); |
1060 CheckStunHeader(msg, STUN_BINDING_REQUEST, size); | 1060 CheckStunHeader(msg, STUN_BINDING_REQUEST, size); |
1061 const StunByteStringAttribute* origin = | 1061 const StunByteStringAttribute* origin = |
1062 msg.GetByteString(STUN_ATTR_ORIGIN); | 1062 msg.GetByteString(STUN_ATTR_ORIGIN); |
1063 ASSERT_TRUE(origin != NULL); | 1063 ASSERT_TRUE(origin != nullptr); |
1064 EXPECT_EQ(kTestOrigin, origin->GetString()); | 1064 EXPECT_EQ(kTestOrigin, origin->GetString()); |
1065 } | 1065 } |
1066 | 1066 |
1067 TEST_F(StunTest, WriteMessageWithAnErrorCodeAttribute) { | 1067 TEST_F(StunTest, WriteMessageWithAnErrorCodeAttribute) { |
1068 StunMessage msg; | 1068 StunMessage msg; |
1069 size_t size = sizeof(kStunMessageWithErrorAttribute); | 1069 size_t size = sizeof(kStunMessageWithErrorAttribute); |
1070 | 1070 |
1071 msg.SetType(STUN_BINDING_ERROR_RESPONSE); | 1071 msg.SetType(STUN_BINDING_ERROR_RESPONSE); |
1072 msg.SetTransactionID( | 1072 msg.SetTransactionID( |
1073 std::string(reinterpret_cast<const char*>(kTestTransactionId1), | 1073 std::string(reinterpret_cast<const char*>(kTestTransactionId1), |
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1377 EXPECT_EQ("0123456789ab", msg.transaction_id()); | 1377 EXPECT_EQ("0123456789ab", msg.transaction_id()); |
1378 | 1378 |
1379 msg2.SetType(STUN_BINDING_REQUEST); | 1379 msg2.SetType(STUN_BINDING_REQUEST); |
1380 msg2.SetTransactionID("0123456789ab"); | 1380 msg2.SetTransactionID("0123456789ab"); |
1381 | 1381 |
1382 in_addr legacy_in_addr; | 1382 in_addr legacy_in_addr; |
1383 legacy_in_addr.s_addr = htonl(17U); | 1383 legacy_in_addr.s_addr = htonl(17U); |
1384 rtc::IPAddress legacy_ip(legacy_in_addr); | 1384 rtc::IPAddress legacy_ip(legacy_in_addr); |
1385 | 1385 |
1386 const StunAddressAttribute* addr = msg.GetAddress(STUN_ATTR_MAPPED_ADDRESS); | 1386 const StunAddressAttribute* addr = msg.GetAddress(STUN_ATTR_MAPPED_ADDRESS); |
1387 ASSERT_TRUE(addr != NULL); | 1387 ASSERT_TRUE(addr != nullptr); |
1388 EXPECT_EQ(1, addr->family()); | 1388 EXPECT_EQ(1, addr->family()); |
1389 EXPECT_EQ(13, addr->port()); | 1389 EXPECT_EQ(13, addr->port()); |
1390 EXPECT_EQ(legacy_ip, addr->ipaddr()); | 1390 EXPECT_EQ(legacy_ip, addr->ipaddr()); |
1391 | 1391 |
1392 StunAddressAttribute* addr2 = | 1392 StunAddressAttribute* addr2 = |
1393 StunAttribute::CreateAddress(STUN_ATTR_MAPPED_ADDRESS); | 1393 StunAttribute::CreateAddress(STUN_ATTR_MAPPED_ADDRESS); |
1394 addr2->SetPort(13); | 1394 addr2->SetPort(13); |
1395 addr2->SetIP(legacy_ip); | 1395 addr2->SetIP(legacy_ip); |
1396 msg2.AddAttribute(addr2); | 1396 msg2.AddAttribute(addr2); |
1397 | 1397 |
1398 const StunByteStringAttribute* bytes = msg.GetByteString(STUN_ATTR_USERNAME); | 1398 const StunByteStringAttribute* bytes = msg.GetByteString(STUN_ATTR_USERNAME); |
1399 ASSERT_TRUE(bytes != NULL); | 1399 ASSERT_TRUE(bytes != nullptr); |
1400 EXPECT_EQ(12U, bytes->length()); | 1400 EXPECT_EQ(12U, bytes->length()); |
1401 EXPECT_EQ("abcdefghijkl", bytes->GetString()); | 1401 EXPECT_EQ("abcdefghijkl", bytes->GetString()); |
1402 | 1402 |
1403 StunByteStringAttribute* bytes2 = | 1403 StunByteStringAttribute* bytes2 = |
1404 StunAttribute::CreateByteString(STUN_ATTR_USERNAME); | 1404 StunAttribute::CreateByteString(STUN_ATTR_USERNAME); |
1405 bytes2->CopyBytes("abcdefghijkl"); | 1405 bytes2->CopyBytes("abcdefghijkl"); |
1406 msg2.AddAttribute(bytes2); | 1406 msg2.AddAttribute(bytes2); |
1407 | 1407 |
1408 const StunUInt32Attribute* uval = msg.GetUInt32(STUN_ATTR_LIFETIME); | 1408 const StunUInt32Attribute* uval = msg.GetUInt32(STUN_ATTR_LIFETIME); |
1409 ASSERT_TRUE(uval != NULL); | 1409 ASSERT_TRUE(uval != nullptr); |
1410 EXPECT_EQ(11U, uval->value()); | 1410 EXPECT_EQ(11U, uval->value()); |
1411 | 1411 |
1412 StunUInt32Attribute* uval2 = StunAttribute::CreateUInt32(STUN_ATTR_LIFETIME); | 1412 StunUInt32Attribute* uval2 = StunAttribute::CreateUInt32(STUN_ATTR_LIFETIME); |
1413 uval2->SetValue(11); | 1413 uval2->SetValue(11); |
1414 msg2.AddAttribute(uval2); | 1414 msg2.AddAttribute(uval2); |
1415 | 1415 |
1416 bytes = msg.GetByteString(STUN_ATTR_MAGIC_COOKIE); | 1416 bytes = msg.GetByteString(STUN_ATTR_MAGIC_COOKIE); |
1417 ASSERT_TRUE(bytes != NULL); | 1417 ASSERT_TRUE(bytes != nullptr); |
1418 EXPECT_EQ(4U, bytes->length()); | 1418 EXPECT_EQ(4U, bytes->length()); |
1419 EXPECT_EQ(0, | 1419 EXPECT_EQ(0, |
1420 memcmp(bytes->bytes(), | 1420 memcmp(bytes->bytes(), |
1421 TURN_MAGIC_COOKIE_VALUE, | 1421 TURN_MAGIC_COOKIE_VALUE, |
1422 sizeof(TURN_MAGIC_COOKIE_VALUE))); | 1422 sizeof(TURN_MAGIC_COOKIE_VALUE))); |
1423 | 1423 |
1424 bytes2 = StunAttribute::CreateByteString(STUN_ATTR_MAGIC_COOKIE); | 1424 bytes2 = StunAttribute::CreateByteString(STUN_ATTR_MAGIC_COOKIE); |
1425 bytes2->CopyBytes(reinterpret_cast<const char*>(TURN_MAGIC_COOKIE_VALUE), | 1425 bytes2->CopyBytes(reinterpret_cast<const char*>(TURN_MAGIC_COOKIE_VALUE), |
1426 sizeof(TURN_MAGIC_COOKIE_VALUE)); | 1426 sizeof(TURN_MAGIC_COOKIE_VALUE)); |
1427 msg2.AddAttribute(bytes2); | 1427 msg2.AddAttribute(bytes2); |
1428 | 1428 |
1429 uval = msg.GetUInt32(STUN_ATTR_BANDWIDTH); | 1429 uval = msg.GetUInt32(STUN_ATTR_BANDWIDTH); |
1430 ASSERT_TRUE(uval != NULL); | 1430 ASSERT_TRUE(uval != nullptr); |
1431 EXPECT_EQ(6U, uval->value()); | 1431 EXPECT_EQ(6U, uval->value()); |
1432 | 1432 |
1433 uval2 = StunAttribute::CreateUInt32(STUN_ATTR_BANDWIDTH); | 1433 uval2 = StunAttribute::CreateUInt32(STUN_ATTR_BANDWIDTH); |
1434 uval2->SetValue(6); | 1434 uval2->SetValue(6); |
1435 msg2.AddAttribute(uval2); | 1435 msg2.AddAttribute(uval2); |
1436 | 1436 |
1437 addr = msg.GetAddress(STUN_ATTR_DESTINATION_ADDRESS); | 1437 addr = msg.GetAddress(STUN_ATTR_DESTINATION_ADDRESS); |
1438 ASSERT_TRUE(addr != NULL); | 1438 ASSERT_TRUE(addr != nullptr); |
1439 EXPECT_EQ(1, addr->family()); | 1439 EXPECT_EQ(1, addr->family()); |
1440 EXPECT_EQ(13, addr->port()); | 1440 EXPECT_EQ(13, addr->port()); |
1441 EXPECT_EQ(legacy_ip, addr->ipaddr()); | 1441 EXPECT_EQ(legacy_ip, addr->ipaddr()); |
1442 | 1442 |
1443 addr2 = StunAttribute::CreateAddress(STUN_ATTR_DESTINATION_ADDRESS); | 1443 addr2 = StunAttribute::CreateAddress(STUN_ATTR_DESTINATION_ADDRESS); |
1444 addr2->SetPort(13); | 1444 addr2->SetPort(13); |
1445 addr2->SetIP(legacy_ip); | 1445 addr2->SetIP(legacy_ip); |
1446 msg2.AddAttribute(addr2); | 1446 msg2.AddAttribute(addr2); |
1447 | 1447 |
1448 addr = msg.GetAddress(STUN_ATTR_SOURCE_ADDRESS2); | 1448 addr = msg.GetAddress(STUN_ATTR_SOURCE_ADDRESS2); |
1449 ASSERT_TRUE(addr != NULL); | 1449 ASSERT_TRUE(addr != nullptr); |
1450 EXPECT_EQ(1, addr->family()); | 1450 EXPECT_EQ(1, addr->family()); |
1451 EXPECT_EQ(13, addr->port()); | 1451 EXPECT_EQ(13, addr->port()); |
1452 EXPECT_EQ(legacy_ip, addr->ipaddr()); | 1452 EXPECT_EQ(legacy_ip, addr->ipaddr()); |
1453 | 1453 |
1454 addr2 = StunAttribute::CreateAddress(STUN_ATTR_SOURCE_ADDRESS2); | 1454 addr2 = StunAttribute::CreateAddress(STUN_ATTR_SOURCE_ADDRESS2); |
1455 addr2->SetPort(13); | 1455 addr2->SetPort(13); |
1456 addr2->SetIP(legacy_ip); | 1456 addr2->SetIP(legacy_ip); |
1457 msg2.AddAttribute(addr2); | 1457 msg2.AddAttribute(addr2); |
1458 | 1458 |
1459 bytes = msg.GetByteString(STUN_ATTR_DATA); | 1459 bytes = msg.GetByteString(STUN_ATTR_DATA); |
1460 ASSERT_TRUE(bytes != NULL); | 1460 ASSERT_TRUE(bytes != nullptr); |
1461 EXPECT_EQ(7U, bytes->length()); | 1461 EXPECT_EQ(7U, bytes->length()); |
1462 EXPECT_EQ("abcdefg", bytes->GetString()); | 1462 EXPECT_EQ("abcdefg", bytes->GetString()); |
1463 | 1463 |
1464 bytes2 = StunAttribute::CreateByteString(STUN_ATTR_DATA); | 1464 bytes2 = StunAttribute::CreateByteString(STUN_ATTR_DATA); |
1465 bytes2->CopyBytes("abcdefg"); | 1465 bytes2->CopyBytes("abcdefg"); |
1466 msg2.AddAttribute(bytes2); | 1466 msg2.AddAttribute(bytes2); |
1467 | 1467 |
1468 rtc::ByteBufferWriter out; | 1468 rtc::ByteBufferWriter out; |
1469 EXPECT_TRUE(msg.Write(&out)); | 1469 EXPECT_TRUE(msg.Write(&out)); |
1470 EXPECT_EQ(size, out.Length()); | 1470 EXPECT_EQ(size, out.Length()); |
1471 size_t len1 = out.Length(); | 1471 size_t len1 = out.Length(); |
1472 rtc::ByteBufferReader read_buf(out); | 1472 rtc::ByteBufferReader read_buf(out); |
1473 std::string outstring; | 1473 std::string outstring; |
1474 read_buf.ReadString(&outstring, len1); | 1474 read_buf.ReadString(&outstring, len1); |
1475 EXPECT_EQ(0, memcmp(outstring.c_str(), input, len1)); | 1475 EXPECT_EQ(0, memcmp(outstring.c_str(), input, len1)); |
1476 | 1476 |
1477 rtc::ByteBufferWriter out2; | 1477 rtc::ByteBufferWriter out2; |
1478 EXPECT_TRUE(msg2.Write(&out2)); | 1478 EXPECT_TRUE(msg2.Write(&out2)); |
1479 EXPECT_EQ(size, out2.Length()); | 1479 EXPECT_EQ(size, out2.Length()); |
1480 size_t len2 = out2.Length(); | 1480 size_t len2 = out2.Length(); |
1481 rtc::ByteBufferReader read_buf2(out2); | 1481 rtc::ByteBufferReader read_buf2(out2); |
1482 std::string outstring2; | 1482 std::string outstring2; |
1483 read_buf2.ReadString(&outstring2, len2); | 1483 read_buf2.ReadString(&outstring2, len2); |
1484 EXPECT_EQ(0, memcmp(outstring2.c_str(), input, len2)); | 1484 EXPECT_EQ(0, memcmp(outstring2.c_str(), input, len2)); |
1485 } | 1485 } |
1486 | 1486 |
1487 } // namespace cricket | 1487 } // namespace cricket |
OLD | NEW |