OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * libjingle |
| 3 * Copyright 2016 Google Inc. |
| 4 * |
| 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions are met: |
| 7 * |
| 8 * 1. Redistributions of source code must retain the above copyright notice, |
| 9 * this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 * this list of conditions and the following disclaimer in the documentation |
| 12 * and/or other materials provided with the distribution. |
| 13 * 3. The name of the author may not be used to endorse or promote products |
| 14 * derived from this software without specific prior written permission. |
| 15 * |
| 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 */ |
| 27 |
| 28 #include "talk/media/base/turnutils.h" |
| 29 |
| 30 #include <stddef.h> |
| 31 |
| 32 #include "webrtc/base/gunit.h" |
| 33 |
| 34 namespace cricket { |
| 35 |
| 36 // Invalid TURN send indication messages. Messages are proper STUN |
| 37 // messages with incorrect values in attributes. |
| 38 TEST(TurnUtilsTest, InvalidTurnSendIndicationMessages) { |
| 39 size_t content_pos = SIZE_MAX; |
| 40 size_t content_size = SIZE_MAX; |
| 41 |
| 42 // Stun Indication message with Zero length |
| 43 uint8_t kTurnSendIndicationMsgWithNoAttributes[] = { |
| 44 0x00, 0x16, 0x00, 0x00, // Zero length |
| 45 0x21, 0x12, 0xA4, 0x42, // magic cookie |
| 46 '0', '1', '2', '3', // transaction id |
| 47 '4', '5', '6', '7', '8', '9', 'a', 'b', |
| 48 }; |
| 49 EXPECT_FALSE(UnwrapTurnPacket(kTurnSendIndicationMsgWithNoAttributes, |
| 50 sizeof(kTurnSendIndicationMsgWithNoAttributes), |
| 51 &content_pos, &content_size)); |
| 52 EXPECT_EQ(SIZE_MAX, content_pos); |
| 53 EXPECT_EQ(SIZE_MAX, content_size); |
| 54 |
| 55 // Stun Send Indication message with invalid length in stun header. |
| 56 const uint8_t kTurnSendIndicationMsgWithInvalidLength[] = { |
| 57 0x00, 0x16, 0xFF, 0x00, // length of 0xFF00 |
| 58 0x21, 0x12, 0xA4, 0x42, // magic cookie |
| 59 '0', '1', '2', '3', // transaction id |
| 60 '4', '5', '6', '7', '8', '9', 'a', 'b', |
| 61 }; |
| 62 EXPECT_FALSE(UnwrapTurnPacket(kTurnSendIndicationMsgWithInvalidLength, |
| 63 sizeof(kTurnSendIndicationMsgWithInvalidLength), |
| 64 &content_pos, &content_size)); |
| 65 EXPECT_EQ(SIZE_MAX, content_pos); |
| 66 EXPECT_EQ(SIZE_MAX, content_size); |
| 67 |
| 68 // Stun Send Indication message with no DATA attribute in message. |
| 69 const uint8_t kTurnSendIndicatinMsgWithNoDataAttribute[] = { |
| 70 0x00, 0x16, 0x00, 0x08, // length of |
| 71 0x21, 0x12, 0xA4, 0x42, // magic cookie |
| 72 '0', '1', '2', '3', // transaction id |
| 73 '4', '5', '6', '7', '8', '9', 'a', 'b', |
| 74 0x00, 0x20, 0x00, 0x04, // Mapped address. |
| 75 0x00, 0x00, 0x00, 0x00, |
| 76 }; |
| 77 EXPECT_FALSE( |
| 78 UnwrapTurnPacket(kTurnSendIndicatinMsgWithNoDataAttribute, |
| 79 sizeof(kTurnSendIndicatinMsgWithNoDataAttribute), |
| 80 &content_pos, &content_size)); |
| 81 EXPECT_EQ(SIZE_MAX, content_pos); |
| 82 EXPECT_EQ(SIZE_MAX, content_size); |
| 83 } |
| 84 |
| 85 // Valid TURN Send Indication messages. |
| 86 TEST(TurnUtilsTest, ValidTurnSendIndicationMessage) { |
| 87 size_t content_pos = SIZE_MAX; |
| 88 size_t content_size = SIZE_MAX; |
| 89 // A valid STUN indication message with a valid RTP header in data attribute |
| 90 // payload field and no extension bit set. |
| 91 const uint8_t kTurnSendIndicationMsgWithoutRtpExtension[] = { |
| 92 0x00, 0x16, 0x00, 0x18, // length of |
| 93 0x21, 0x12, 0xA4, 0x42, // magic cookie |
| 94 '0', '1', '2', '3', // transaction id |
| 95 '4', '5', '6', '7', '8', '9', 'a', 'b', |
| 96 0x00, 0x20, 0x00, 0x04, // Mapped address. |
| 97 0x00, 0x00, 0x00, 0x00, |
| 98 0x00, 0x13, 0x00, 0x0C, // Data attribute. |
| 99 0x80, 0x00, 0x00, 0x00, // RTP packet. |
| 100 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 101 }; |
| 102 EXPECT_TRUE(UnwrapTurnPacket( |
| 103 kTurnSendIndicationMsgWithoutRtpExtension, |
| 104 sizeof(kTurnSendIndicationMsgWithoutRtpExtension), &content_pos, |
| 105 &content_size)); |
| 106 EXPECT_EQ(12U, content_size); |
| 107 EXPECT_EQ(32U, content_pos); |
| 108 } |
| 109 |
| 110 // Verify that parsing of valid TURN Channel Messages. |
| 111 TEST(TurnUtilsTest, ValidTurnChannelMessages) { |
| 112 const uint8_t kTurnChannelMsgWithRtpPacket[] = { |
| 113 0x40, 0x00, 0x00, 0x0C, |
| 114 0x80, 0x00, 0x00, 0x00, // RTP packet. |
| 115 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 116 }; |
| 117 |
| 118 size_t content_pos = 0, content_size = 0; |
| 119 EXPECT_TRUE(UnwrapTurnPacket( |
| 120 kTurnChannelMsgWithRtpPacket, |
| 121 sizeof(kTurnChannelMsgWithRtpPacket), &content_pos, &content_size)); |
| 122 EXPECT_EQ(12U, content_size); |
| 123 EXPECT_EQ(4U, content_pos); |
| 124 } |
| 125 |
| 126 TEST(TurnUtilsTest, ChannelMessageZeroLength) { |
| 127 const uint8_t kTurnChannelMsgWithZeroLength[] = {0x40, 0x00, 0x00, 0x00}; |
| 128 size_t content_pos = SIZE_MAX; |
| 129 size_t content_size = SIZE_MAX; |
| 130 EXPECT_TRUE(UnwrapTurnPacket(kTurnChannelMsgWithZeroLength, |
| 131 sizeof(kTurnChannelMsgWithZeroLength), |
| 132 &content_pos, &content_size)); |
| 133 EXPECT_EQ(4, content_pos); |
| 134 EXPECT_EQ(0, content_size); |
| 135 } |
| 136 |
| 137 } // namespace cricket |
OLD | NEW |