| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <algorithm> | 5 #include <algorithm> |
| 6 #include <iostream> | 6 #include <iostream> |
| 7 | 7 |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/sys_byteorder.h" | 9 #include "base/sys_byteorder.h" |
| 10 #include "net/spdy/spdy_frame_reader.h" | 10 #include "net/spdy/spdy_frame_reader.h" |
| 11 #include "testing/platform_test.h" | 11 #include "testing/platform_test.h" |
| 12 | 12 |
| 13 namespace net { | 13 namespace net { |
| 14 | 14 |
| 15 TEST(SpdyFrameReaderTest, ReadUInt16) { | 15 TEST(SpdyFrameReaderTest, ReadUInt16) { |
| 16 // Frame data in network byte order. | 16 // Frame data in network byte order. |
| 17 const uint16 kFrameData[] = { | 17 const uint16_t kFrameData[] = { |
| 18 base::HostToNet16(1), base::HostToNet16(1 << 15), | 18 base::HostToNet16(1), base::HostToNet16(1 << 15), |
| 19 }; | 19 }; |
| 20 | 20 |
| 21 SpdyFrameReader frame_reader(reinterpret_cast<const char*>(kFrameData), | 21 SpdyFrameReader frame_reader(reinterpret_cast<const char*>(kFrameData), |
| 22 sizeof(kFrameData)); | 22 sizeof(kFrameData)); |
| 23 EXPECT_FALSE(frame_reader.IsDoneReading()); | 23 EXPECT_FALSE(frame_reader.IsDoneReading()); |
| 24 | 24 |
| 25 uint16 uint16_val; | 25 uint16_t uint16_val; |
| 26 EXPECT_TRUE(frame_reader.ReadUInt16(&uint16_val)); | 26 EXPECT_TRUE(frame_reader.ReadUInt16(&uint16_val)); |
| 27 EXPECT_FALSE(frame_reader.IsDoneReading()); | 27 EXPECT_FALSE(frame_reader.IsDoneReading()); |
| 28 EXPECT_EQ(1, uint16_val); | 28 EXPECT_EQ(1, uint16_val); |
| 29 | 29 |
| 30 EXPECT_TRUE(frame_reader.ReadUInt16(&uint16_val)); | 30 EXPECT_TRUE(frame_reader.ReadUInt16(&uint16_val)); |
| 31 EXPECT_TRUE(frame_reader.IsDoneReading()); | 31 EXPECT_TRUE(frame_reader.IsDoneReading()); |
| 32 EXPECT_EQ(1<<15, uint16_val); | 32 EXPECT_EQ(1<<15, uint16_val); |
| 33 } | 33 } |
| 34 | 34 |
| 35 TEST(SpdyFrameReaderTest, ReadUInt32) { | 35 TEST(SpdyFrameReaderTest, ReadUInt32) { |
| 36 // Frame data in network byte order. | 36 // Frame data in network byte order. |
| 37 const uint32 kFrameData[] = { | 37 const uint32_t kFrameData[] = { |
| 38 base::HostToNet32(1), base::HostToNet32(0x80000000), | 38 base::HostToNet32(1), base::HostToNet32(0x80000000), |
| 39 }; | 39 }; |
| 40 | 40 |
| 41 SpdyFrameReader frame_reader(reinterpret_cast<const char *>(kFrameData), | 41 SpdyFrameReader frame_reader(reinterpret_cast<const char*>(kFrameData), |
| 42 arraysize(kFrameData) * sizeof(uint32)); | 42 arraysize(kFrameData) * sizeof(uint32_t)); |
| 43 EXPECT_FALSE(frame_reader.IsDoneReading()); | 43 EXPECT_FALSE(frame_reader.IsDoneReading()); |
| 44 | 44 |
| 45 uint32 uint32_val; | 45 uint32_t uint32_val; |
| 46 EXPECT_TRUE(frame_reader.ReadUInt32(&uint32_val)); | 46 EXPECT_TRUE(frame_reader.ReadUInt32(&uint32_val)); |
| 47 EXPECT_FALSE(frame_reader.IsDoneReading()); | 47 EXPECT_FALSE(frame_reader.IsDoneReading()); |
| 48 EXPECT_EQ(1u, uint32_val); | 48 EXPECT_EQ(1u, uint32_val); |
| 49 | 49 |
| 50 EXPECT_TRUE(frame_reader.ReadUInt32(&uint32_val)); | 50 EXPECT_TRUE(frame_reader.ReadUInt32(&uint32_val)); |
| 51 EXPECT_TRUE(frame_reader.IsDoneReading()); | 51 EXPECT_TRUE(frame_reader.IsDoneReading()); |
| 52 EXPECT_EQ(1u<<31, uint32_val); | 52 EXPECT_EQ(1u<<31, uint32_val); |
| 53 } | 53 } |
| 54 | 54 |
| 55 TEST(SpdyFrameReaderTest, ReadStringPiece16) { | 55 TEST(SpdyFrameReaderTest, ReadStringPiece16) { |
| 56 // Frame data in network byte order. | 56 // Frame data in network byte order. |
| 57 const char kFrameData[] = { | 57 const char kFrameData[] = { |
| 58 0x00, 0x02, // uint16(2) | 58 0x00, 0x02, // uint16_t(2) |
| 59 0x48, 0x69, // "Hi" | 59 0x48, 0x69, // "Hi" |
| 60 0x00, 0x10, // uint16(16) | 60 0x00, 0x10, // uint16_t(16) |
| 61 0x54, 0x65, 0x73, 0x74, | 61 0x54, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67, 0x2c, |
| 62 0x69, 0x6e, 0x67, 0x2c, | 62 0x20, 0x31, 0x2c, 0x20, 0x32, 0x2c, 0x20, 0x33, // "Testing, 1, 2, 3" |
| 63 0x20, 0x31, 0x2c, 0x20, | |
| 64 0x32, 0x2c, 0x20, 0x33, // "Testing, 1, 2, 3" | |
| 65 }; | 63 }; |
| 66 | 64 |
| 67 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); | 65 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); |
| 68 EXPECT_FALSE(frame_reader.IsDoneReading()); | 66 EXPECT_FALSE(frame_reader.IsDoneReading()); |
| 69 | 67 |
| 70 base::StringPiece stringpiece_val; | 68 base::StringPiece stringpiece_val; |
| 71 EXPECT_TRUE(frame_reader.ReadStringPiece16(&stringpiece_val)); | 69 EXPECT_TRUE(frame_reader.ReadStringPiece16(&stringpiece_val)); |
| 72 EXPECT_FALSE(frame_reader.IsDoneReading()); | 70 EXPECT_FALSE(frame_reader.IsDoneReading()); |
| 73 EXPECT_EQ(0, stringpiece_val.compare("Hi")); | 71 EXPECT_EQ(0, stringpiece_val.compare("Hi")); |
| 74 | 72 |
| 75 EXPECT_TRUE(frame_reader.ReadStringPiece16(&stringpiece_val)); | 73 EXPECT_TRUE(frame_reader.ReadStringPiece16(&stringpiece_val)); |
| 76 EXPECT_TRUE(frame_reader.IsDoneReading()); | 74 EXPECT_TRUE(frame_reader.IsDoneReading()); |
| 77 EXPECT_EQ(0, stringpiece_val.compare("Testing, 1, 2, 3")); | 75 EXPECT_EQ(0, stringpiece_val.compare("Testing, 1, 2, 3")); |
| 78 } | 76 } |
| 79 | 77 |
| 80 TEST(SpdyFrameReaderTest, ReadStringPiece32) { | 78 TEST(SpdyFrameReaderTest, ReadStringPiece32) { |
| 81 // Frame data in network byte order. | 79 // Frame data in network byte order. |
| 82 const char kFrameData[] = { | 80 const char kFrameData[] = { |
| 83 0x00, 0x00, 0x00, 0x03, // uint32(3) | 81 0x00, 0x00, 0x00, 0x03, // uint32_t(3) |
| 84 0x66, 0x6f, 0x6f, // "foo" | 82 0x66, 0x6f, 0x6f, // "foo" |
| 85 0x00, 0x00, 0x00, 0x10, // uint32(16) | 83 0x00, 0x00, 0x00, 0x10, // uint32_t(16) |
| 86 0x54, 0x65, 0x73, 0x74, | 84 0x54, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67, 0x2c, |
| 87 0x69, 0x6e, 0x67, 0x2c, | 85 0x20, 0x34, 0x2c, 0x20, 0x35, 0x2c, 0x20, 0x36, // "Testing, 4, 5, 6" |
| 88 0x20, 0x34, 0x2c, 0x20, | |
| 89 0x35, 0x2c, 0x20, 0x36, // "Testing, 4, 5, 6" | |
| 90 }; | 86 }; |
| 91 | 87 |
| 92 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); | 88 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); |
| 93 EXPECT_FALSE(frame_reader.IsDoneReading()); | 89 EXPECT_FALSE(frame_reader.IsDoneReading()); |
| 94 | 90 |
| 95 base::StringPiece stringpiece_val; | 91 base::StringPiece stringpiece_val; |
| 96 EXPECT_TRUE(frame_reader.ReadStringPiece32(&stringpiece_val)); | 92 EXPECT_TRUE(frame_reader.ReadStringPiece32(&stringpiece_val)); |
| 97 EXPECT_FALSE(frame_reader.IsDoneReading()); | 93 EXPECT_FALSE(frame_reader.IsDoneReading()); |
| 98 EXPECT_EQ(0, stringpiece_val.compare("foo")); | 94 EXPECT_EQ(0, stringpiece_val.compare("foo")); |
| 99 | 95 |
| 100 EXPECT_TRUE(frame_reader.ReadStringPiece32(&stringpiece_val)); | 96 EXPECT_TRUE(frame_reader.ReadStringPiece32(&stringpiece_val)); |
| 101 EXPECT_TRUE(frame_reader.IsDoneReading()); | 97 EXPECT_TRUE(frame_reader.IsDoneReading()); |
| 102 EXPECT_EQ(0, stringpiece_val.compare("Testing, 4, 5, 6")); | 98 EXPECT_EQ(0, stringpiece_val.compare("Testing, 4, 5, 6")); |
| 103 } | 99 } |
| 104 | 100 |
| 105 TEST(SpdyFrameReaderTest, ReadUInt16WithBufferTooSmall) { | 101 TEST(SpdyFrameReaderTest, ReadUInt16WithBufferTooSmall) { |
| 106 // Frame data in network byte order. | 102 // Frame data in network byte order. |
| 107 const char kFrameData[] = { | 103 const char kFrameData[] = { |
| 108 0x00, // part of a uint16 | 104 0x00, // part of a uint16_t |
| 109 }; | 105 }; |
| 110 | 106 |
| 111 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); | 107 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); |
| 112 EXPECT_FALSE(frame_reader.IsDoneReading()); | 108 EXPECT_FALSE(frame_reader.IsDoneReading()); |
| 113 | 109 |
| 114 uint16 uint16_val; | 110 uint16_t uint16_val; |
| 115 EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val)); | 111 EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val)); |
| 116 } | 112 } |
| 117 | 113 |
| 118 TEST(SpdyFrameReaderTest, ReadUInt32WithBufferTooSmall) { | 114 TEST(SpdyFrameReaderTest, ReadUInt32WithBufferTooSmall) { |
| 119 // Frame data in network byte order. | 115 // Frame data in network byte order. |
| 120 const char kFrameData[] = { | 116 const char kFrameData[] = { |
| 121 0x00, 0x00, 0x00, // part of a uint32 | 117 0x00, 0x00, 0x00, // part of a uint32_t |
| 122 }; | 118 }; |
| 123 | 119 |
| 124 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); | 120 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); |
| 125 EXPECT_FALSE(frame_reader.IsDoneReading()); | 121 EXPECT_FALSE(frame_reader.IsDoneReading()); |
| 126 | 122 |
| 127 uint32 uint32_val; | 123 uint32_t uint32_val; |
| 128 EXPECT_FALSE(frame_reader.ReadUInt32(&uint32_val)); | 124 EXPECT_FALSE(frame_reader.ReadUInt32(&uint32_val)); |
| 129 | 125 |
| 130 // Also make sure that trying to read a uint16, which technically could work, | 126 // Also make sure that trying to read a uint16_t, which technically could |
| 127 // work, |
| 131 // fails immediately due to previously encountered failed read. | 128 // fails immediately due to previously encountered failed read. |
| 132 uint16 uint16_val; | 129 uint16_t uint16_val; |
| 133 EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val)); | 130 EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val)); |
| 134 } | 131 } |
| 135 | 132 |
| 136 // Tests ReadStringPiece16() with a buffer too small to fit the entire string. | 133 // Tests ReadStringPiece16() with a buffer too small to fit the entire string. |
| 137 TEST(SpdyFrameReaderTest, ReadStringPiece16WithBufferTooSmall) { | 134 TEST(SpdyFrameReaderTest, ReadStringPiece16WithBufferTooSmall) { |
| 138 // Frame data in network byte order. | 135 // Frame data in network byte order. |
| 139 const char kFrameData[] = { | 136 const char kFrameData[] = { |
| 140 0x00, 0x03, // uint16(3) | 137 0x00, 0x03, // uint16_t(3) |
| 141 0x48, 0x69, // "Hi" | 138 0x48, 0x69, // "Hi" |
| 142 }; | 139 }; |
| 143 | 140 |
| 144 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); | 141 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); |
| 145 EXPECT_FALSE(frame_reader.IsDoneReading()); | 142 EXPECT_FALSE(frame_reader.IsDoneReading()); |
| 146 | 143 |
| 147 base::StringPiece stringpiece_val; | 144 base::StringPiece stringpiece_val; |
| 148 EXPECT_FALSE(frame_reader.ReadStringPiece16(&stringpiece_val)); | 145 EXPECT_FALSE(frame_reader.ReadStringPiece16(&stringpiece_val)); |
| 149 | 146 |
| 150 // Also make sure that trying to read a uint16, which technically could work, | 147 // Also make sure that trying to read a uint16_t, which technically could |
| 148 // work, |
| 151 // fails immediately due to previously encountered failed read. | 149 // fails immediately due to previously encountered failed read. |
| 152 uint16 uint16_val; | 150 uint16_t uint16_val; |
| 153 EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val)); | 151 EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val)); |
| 154 } | 152 } |
| 155 | 153 |
| 156 // Tests ReadStringPiece16() with a buffer too small even to fit the length. | 154 // Tests ReadStringPiece16() with a buffer too small even to fit the length. |
| 157 TEST(SpdyFrameReaderTest, ReadStringPiece16WithBufferWayTooSmall) { | 155 TEST(SpdyFrameReaderTest, ReadStringPiece16WithBufferWayTooSmall) { |
| 158 // Frame data in network byte order. | 156 // Frame data in network byte order. |
| 159 const char kFrameData[] = { | 157 const char kFrameData[] = { |
| 160 0x00, // part of a uint16 | 158 0x00, // part of a uint16_t |
| 161 }; | 159 }; |
| 162 | 160 |
| 163 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); | 161 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); |
| 164 EXPECT_FALSE(frame_reader.IsDoneReading()); | 162 EXPECT_FALSE(frame_reader.IsDoneReading()); |
| 165 | 163 |
| 166 base::StringPiece stringpiece_val; | 164 base::StringPiece stringpiece_val; |
| 167 EXPECT_FALSE(frame_reader.ReadStringPiece16(&stringpiece_val)); | 165 EXPECT_FALSE(frame_reader.ReadStringPiece16(&stringpiece_val)); |
| 168 | 166 |
| 169 // Also make sure that trying to read a uint16, which technically could work, | 167 // Also make sure that trying to read a uint16_t, which technically could |
| 168 // work, |
| 170 // fails immediately due to previously encountered failed read. | 169 // fails immediately due to previously encountered failed read. |
| 171 uint16 uint16_val; | 170 uint16_t uint16_val; |
| 172 EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val)); | 171 EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val)); |
| 173 } | 172 } |
| 174 | 173 |
| 175 // Tests ReadStringPiece32() with a buffer too small to fit the entire string. | 174 // Tests ReadStringPiece32() with a buffer too small to fit the entire string. |
| 176 TEST(SpdyFrameReaderTest, ReadStringPiece32WithBufferTooSmall) { | 175 TEST(SpdyFrameReaderTest, ReadStringPiece32WithBufferTooSmall) { |
| 177 // Frame data in network byte order. | 176 // Frame data in network byte order. |
| 178 const char kFrameData[] = { | 177 const char kFrameData[] = { |
| 179 0x00, 0x00, 0x00, 0x03, // uint32(3) | 178 0x00, 0x00, 0x00, 0x03, // uint32_t(3) |
| 180 0x48, 0x69, // "Hi" | 179 0x48, 0x69, // "Hi" |
| 181 }; | 180 }; |
| 182 | 181 |
| 183 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); | 182 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); |
| 184 EXPECT_FALSE(frame_reader.IsDoneReading()); | 183 EXPECT_FALSE(frame_reader.IsDoneReading()); |
| 185 | 184 |
| 186 base::StringPiece stringpiece_val; | 185 base::StringPiece stringpiece_val; |
| 187 EXPECT_FALSE(frame_reader.ReadStringPiece32(&stringpiece_val)); | 186 EXPECT_FALSE(frame_reader.ReadStringPiece32(&stringpiece_val)); |
| 188 | 187 |
| 189 // Also make sure that trying to read a uint16, which technically could work, | 188 // Also make sure that trying to read a uint16_t, which technically could |
| 189 // work, |
| 190 // fails immediately due to previously encountered failed read. | 190 // fails immediately due to previously encountered failed read. |
| 191 uint16 uint16_val; | 191 uint16_t uint16_val; |
| 192 EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val)); | 192 EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val)); |
| 193 } | 193 } |
| 194 | 194 |
| 195 // Tests ReadStringPiece32() with a buffer too small even to fit the length. | 195 // Tests ReadStringPiece32() with a buffer too small even to fit the length. |
| 196 TEST(SpdyFrameReaderTest, ReadStringPiece32WithBufferWayTooSmall) { | 196 TEST(SpdyFrameReaderTest, ReadStringPiece32WithBufferWayTooSmall) { |
| 197 // Frame data in network byte order. | 197 // Frame data in network byte order. |
| 198 const char kFrameData[] = { | 198 const char kFrameData[] = { |
| 199 0x00, 0x00, 0x00, // part of a uint32 | 199 0x00, 0x00, 0x00, // part of a uint32_t |
| 200 }; | 200 }; |
| 201 | 201 |
| 202 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); | 202 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); |
| 203 EXPECT_FALSE(frame_reader.IsDoneReading()); | 203 EXPECT_FALSE(frame_reader.IsDoneReading()); |
| 204 | 204 |
| 205 base::StringPiece stringpiece_val; | 205 base::StringPiece stringpiece_val; |
| 206 EXPECT_FALSE(frame_reader.ReadStringPiece32(&stringpiece_val)); | 206 EXPECT_FALSE(frame_reader.ReadStringPiece32(&stringpiece_val)); |
| 207 | 207 |
| 208 // Also make sure that trying to read a uint16, which technically could work, | 208 // Also make sure that trying to read a uint16_t, which technically could |
| 209 // work, |
| 209 // fails immediately due to previously encountered failed read. | 210 // fails immediately due to previously encountered failed read. |
| 210 uint16 uint16_val; | 211 uint16_t uint16_val; |
| 211 EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val)); | 212 EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val)); |
| 212 } | 213 } |
| 213 | 214 |
| 214 TEST(SpdyFrameReaderTest, ReadBytes) { | 215 TEST(SpdyFrameReaderTest, ReadBytes) { |
| 215 // Frame data in network byte order. | 216 // Frame data in network byte order. |
| 216 const char kFrameData[] = { | 217 const char kFrameData[] = { |
| 217 0x66, 0x6f, 0x6f, // "foo" | 218 0x66, 0x6f, 0x6f, // "foo" |
| 218 0x48, 0x69, // "Hi" | 219 0x48, 0x69, // "Hi" |
| 219 }; | 220 }; |
| 220 | 221 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 240 | 241 |
| 241 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); | 242 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); |
| 242 EXPECT_FALSE(frame_reader.IsDoneReading()); | 243 EXPECT_FALSE(frame_reader.IsDoneReading()); |
| 243 | 244 |
| 244 char dest[arraysize(kFrameData) + 2] = {}; | 245 char dest[arraysize(kFrameData) + 2] = {}; |
| 245 EXPECT_FALSE(frame_reader.ReadBytes(&dest, arraysize(kFrameData) + 1)); | 246 EXPECT_FALSE(frame_reader.ReadBytes(&dest, arraysize(kFrameData) + 1)); |
| 246 EXPECT_STREQ("", dest); | 247 EXPECT_STREQ("", dest); |
| 247 } | 248 } |
| 248 | 249 |
| 249 } // namespace net | 250 } // namespace net |
| OLD | NEW |