| 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 "net/spdy/core/spdy_frame_reader.h" | 5 #include "net/spdy/core/spdy_frame_reader.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <iostream> | 8 #include <iostream> |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 sizeof(kFrameData)); | 23 sizeof(kFrameData)); |
| 24 EXPECT_FALSE(frame_reader.IsDoneReading()); | 24 EXPECT_FALSE(frame_reader.IsDoneReading()); |
| 25 | 25 |
| 26 uint16_t uint16_val; | 26 uint16_t uint16_val; |
| 27 EXPECT_TRUE(frame_reader.ReadUInt16(&uint16_val)); | 27 EXPECT_TRUE(frame_reader.ReadUInt16(&uint16_val)); |
| 28 EXPECT_FALSE(frame_reader.IsDoneReading()); | 28 EXPECT_FALSE(frame_reader.IsDoneReading()); |
| 29 EXPECT_EQ(1, uint16_val); | 29 EXPECT_EQ(1, uint16_val); |
| 30 | 30 |
| 31 EXPECT_TRUE(frame_reader.ReadUInt16(&uint16_val)); | 31 EXPECT_TRUE(frame_reader.ReadUInt16(&uint16_val)); |
| 32 EXPECT_TRUE(frame_reader.IsDoneReading()); | 32 EXPECT_TRUE(frame_reader.IsDoneReading()); |
| 33 EXPECT_EQ(1<<15, uint16_val); | 33 EXPECT_EQ(1 << 15, uint16_val); |
| 34 } | 34 } |
| 35 | 35 |
| 36 TEST(SpdyFrameReaderTest, ReadUInt32) { | 36 TEST(SpdyFrameReaderTest, ReadUInt32) { |
| 37 // Frame data in network byte order. | 37 // Frame data in network byte order. |
| 38 const uint32_t kFrameData[] = { | 38 const uint32_t kFrameData[] = { |
| 39 base::HostToNet32(1), base::HostToNet32(0x80000000), | 39 base::HostToNet32(1), base::HostToNet32(0x80000000), |
| 40 }; | 40 }; |
| 41 | 41 |
| 42 SpdyFrameReader frame_reader(reinterpret_cast<const char*>(kFrameData), | 42 SpdyFrameReader frame_reader(reinterpret_cast<const char*>(kFrameData), |
| 43 arraysize(kFrameData) * sizeof(uint32_t)); | 43 arraysize(kFrameData) * sizeof(uint32_t)); |
| 44 EXPECT_FALSE(frame_reader.IsDoneReading()); | 44 EXPECT_FALSE(frame_reader.IsDoneReading()); |
| 45 | 45 |
| 46 uint32_t uint32_val; | 46 uint32_t uint32_val; |
| 47 EXPECT_TRUE(frame_reader.ReadUInt32(&uint32_val)); | 47 EXPECT_TRUE(frame_reader.ReadUInt32(&uint32_val)); |
| 48 EXPECT_FALSE(frame_reader.IsDoneReading()); | 48 EXPECT_FALSE(frame_reader.IsDoneReading()); |
| 49 EXPECT_EQ(1u, uint32_val); | 49 EXPECT_EQ(1u, uint32_val); |
| 50 | 50 |
| 51 EXPECT_TRUE(frame_reader.ReadUInt32(&uint32_val)); | 51 EXPECT_TRUE(frame_reader.ReadUInt32(&uint32_val)); |
| 52 EXPECT_TRUE(frame_reader.IsDoneReading()); | 52 EXPECT_TRUE(frame_reader.IsDoneReading()); |
| 53 EXPECT_EQ(1u<<31, uint32_val); | 53 EXPECT_EQ(1u << 31, uint32_val); |
| 54 } | 54 } |
| 55 | 55 |
| 56 TEST(SpdyFrameReaderTest, ReadStringPiece16) { | 56 TEST(SpdyFrameReaderTest, ReadStringPiece16) { |
| 57 // Frame data in network byte order. | 57 // Frame data in network byte order. |
| 58 const char kFrameData[] = { | 58 const char kFrameData[] = { |
| 59 0x00, 0x02, // uint16_t(2) | 59 0x00, 0x02, // uint16_t(2) |
| 60 0x48, 0x69, // "Hi" | 60 0x48, 0x69, // "Hi" |
| 61 0x00, 0x10, // uint16_t(16) | 61 0x00, 0x10, // uint16_t(16) |
| 62 0x54, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67, 0x2c, | 62 0x54, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67, 0x2c, |
| 63 0x20, 0x31, 0x2c, 0x20, 0x32, 0x2c, 0x20, 0x33, // "Testing, 1, 2, 3" | 63 0x20, 0x31, 0x2c, 0x20, 0x32, 0x2c, 0x20, 0x33, // "Testing, 1, 2, 3" |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 0x00, 0x00, 0x00, // part of a uint32_t | 118 0x00, 0x00, 0x00, // part of a uint32_t |
| 119 }; | 119 }; |
| 120 | 120 |
| 121 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); | 121 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); |
| 122 EXPECT_FALSE(frame_reader.IsDoneReading()); | 122 EXPECT_FALSE(frame_reader.IsDoneReading()); |
| 123 | 123 |
| 124 uint32_t uint32_val; | 124 uint32_t uint32_val; |
| 125 EXPECT_FALSE(frame_reader.ReadUInt32(&uint32_val)); | 125 EXPECT_FALSE(frame_reader.ReadUInt32(&uint32_val)); |
| 126 | 126 |
| 127 // Also make sure that trying to read a uint16_t, which technically could | 127 // Also make sure that trying to read a uint16_t, which technically could |
| 128 // work, | 128 // work, fails immediately due to previously encountered failed read. |
| 129 // fails immediately due to previously encountered failed read. | |
| 130 uint16_t uint16_val; | 129 uint16_t uint16_val; |
| 131 EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val)); | 130 EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val)); |
| 132 } | 131 } |
| 133 | 132 |
| 134 // 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. |
| 135 TEST(SpdyFrameReaderTest, ReadStringPiece16WithBufferTooSmall) { | 134 TEST(SpdyFrameReaderTest, ReadStringPiece16WithBufferTooSmall) { |
| 136 // Frame data in network byte order. | 135 // Frame data in network byte order. |
| 137 const char kFrameData[] = { | 136 const char kFrameData[] = { |
| 138 0x00, 0x03, // uint16_t(3) | 137 0x00, 0x03, // uint16_t(3) |
| 139 0x48, 0x69, // "Hi" | 138 0x48, 0x69, // "Hi" |
| 140 }; | 139 }; |
| 141 | 140 |
| 142 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); | 141 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); |
| 143 EXPECT_FALSE(frame_reader.IsDoneReading()); | 142 EXPECT_FALSE(frame_reader.IsDoneReading()); |
| 144 | 143 |
| 145 SpdyStringPiece stringpiece_val; | 144 SpdyStringPiece stringpiece_val; |
| 146 EXPECT_FALSE(frame_reader.ReadStringPiece16(&stringpiece_val)); | 145 EXPECT_FALSE(frame_reader.ReadStringPiece16(&stringpiece_val)); |
| 147 | 146 |
| 148 // Also make sure that trying to read a uint16_t, which technically could | 147 // Also make sure that trying to read a uint16_t, which technically could |
| 149 // work, | 148 // work, fails immediately due to previously encountered failed read. |
| 150 // fails immediately due to previously encountered failed read. | |
| 151 uint16_t uint16_val; | 149 uint16_t uint16_val; |
| 152 EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val)); | 150 EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val)); |
| 153 } | 151 } |
| 154 | 152 |
| 155 // Tests ReadStringPiece16() with a buffer too small even to fit the length. | 153 // Tests ReadStringPiece16() with a buffer too small even to fit the length. |
| 156 TEST(SpdyFrameReaderTest, ReadStringPiece16WithBufferWayTooSmall) { | 154 TEST(SpdyFrameReaderTest, ReadStringPiece16WithBufferWayTooSmall) { |
| 157 // Frame data in network byte order. | 155 // Frame data in network byte order. |
| 158 const char kFrameData[] = { | 156 const char kFrameData[] = { |
| 159 0x00, // part of a uint16_t | 157 0x00, // part of a uint16_t |
| 160 }; | 158 }; |
| 161 | 159 |
| 162 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); | 160 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); |
| 163 EXPECT_FALSE(frame_reader.IsDoneReading()); | 161 EXPECT_FALSE(frame_reader.IsDoneReading()); |
| 164 | 162 |
| 165 SpdyStringPiece stringpiece_val; | 163 SpdyStringPiece stringpiece_val; |
| 166 EXPECT_FALSE(frame_reader.ReadStringPiece16(&stringpiece_val)); | 164 EXPECT_FALSE(frame_reader.ReadStringPiece16(&stringpiece_val)); |
| 167 | 165 |
| 168 // Also make sure that trying to read a uint16_t, which technically could | 166 // Also make sure that trying to read a uint16_t, which technically could |
| 169 // work, | 167 // work, fails immediately due to previously encountered failed read. |
| 170 // fails immediately due to previously encountered failed read. | |
| 171 uint16_t uint16_val; | 168 uint16_t uint16_val; |
| 172 EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val)); | 169 EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val)); |
| 173 } | 170 } |
| 174 | 171 |
| 175 // Tests ReadStringPiece32() with a buffer too small to fit the entire string. | 172 // Tests ReadStringPiece32() with a buffer too small to fit the entire string. |
| 176 TEST(SpdyFrameReaderTest, ReadStringPiece32WithBufferTooSmall) { | 173 TEST(SpdyFrameReaderTest, ReadStringPiece32WithBufferTooSmall) { |
| 177 // Frame data in network byte order. | 174 // Frame data in network byte order. |
| 178 const char kFrameData[] = { | 175 const char kFrameData[] = { |
| 179 0x00, 0x00, 0x00, 0x03, // uint32_t(3) | 176 0x00, 0x00, 0x00, 0x03, // uint32_t(3) |
| 180 0x48, 0x69, // "Hi" | 177 0x48, 0x69, // "Hi" |
| 181 }; | 178 }; |
| 182 | 179 |
| 183 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); | 180 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); |
| 184 EXPECT_FALSE(frame_reader.IsDoneReading()); | 181 EXPECT_FALSE(frame_reader.IsDoneReading()); |
| 185 | 182 |
| 186 SpdyStringPiece stringpiece_val; | 183 SpdyStringPiece stringpiece_val; |
| 187 EXPECT_FALSE(frame_reader.ReadStringPiece32(&stringpiece_val)); | 184 EXPECT_FALSE(frame_reader.ReadStringPiece32(&stringpiece_val)); |
| 188 | 185 |
| 189 // Also make sure that trying to read a uint16_t, which technically could | 186 // Also make sure that trying to read a uint16_t, which technically could |
| 190 // work, | 187 // work, fails immediately due to previously encountered failed read. |
| 191 // fails immediately due to previously encountered failed read. | |
| 192 uint16_t uint16_val; | 188 uint16_t uint16_val; |
| 193 EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val)); | 189 EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val)); |
| 194 } | 190 } |
| 195 | 191 |
| 196 // Tests ReadStringPiece32() with a buffer too small even to fit the length. | 192 // Tests ReadStringPiece32() with a buffer too small even to fit the length. |
| 197 TEST(SpdyFrameReaderTest, ReadStringPiece32WithBufferWayTooSmall) { | 193 TEST(SpdyFrameReaderTest, ReadStringPiece32WithBufferWayTooSmall) { |
| 198 // Frame data in network byte order. | 194 // Frame data in network byte order. |
| 199 const char kFrameData[] = { | 195 const char kFrameData[] = { |
| 200 0x00, 0x00, 0x00, // part of a uint32_t | 196 0x00, 0x00, 0x00, // part of a uint32_t |
| 201 }; | 197 }; |
| 202 | 198 |
| 203 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); | 199 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); |
| 204 EXPECT_FALSE(frame_reader.IsDoneReading()); | 200 EXPECT_FALSE(frame_reader.IsDoneReading()); |
| 205 | 201 |
| 206 SpdyStringPiece stringpiece_val; | 202 SpdyStringPiece stringpiece_val; |
| 207 EXPECT_FALSE(frame_reader.ReadStringPiece32(&stringpiece_val)); | 203 EXPECT_FALSE(frame_reader.ReadStringPiece32(&stringpiece_val)); |
| 208 | 204 |
| 209 // Also make sure that trying to read a uint16_t, which technically could | 205 // Also make sure that trying to read a uint16_t, which technically could |
| 210 // work, | 206 // work, fails immediately due to previously encountered failed read. |
| 211 // fails immediately due to previously encountered failed read. | |
| 212 uint16_t uint16_val; | 207 uint16_t uint16_val; |
| 213 EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val)); | 208 EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val)); |
| 214 } | 209 } |
| 215 | 210 |
| 216 TEST(SpdyFrameReaderTest, ReadBytes) { | 211 TEST(SpdyFrameReaderTest, ReadBytes) { |
| 217 // Frame data in network byte order. | 212 // Frame data in network byte order. |
| 218 const char kFrameData[] = { | 213 const char kFrameData[] = { |
| 219 0x66, 0x6f, 0x6f, // "foo" | 214 0x66, 0x6f, 0x6f, // "foo" |
| 220 0x48, 0x69, // "Hi" | 215 0x48, 0x69, // "Hi" |
| 221 }; | 216 }; |
| 222 | 217 |
| 223 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); | 218 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); |
| 224 EXPECT_FALSE(frame_reader.IsDoneReading()); | 219 EXPECT_FALSE(frame_reader.IsDoneReading()); |
| 225 | 220 |
| 226 char dest1[3] = {}; | 221 char dest1[3] = {}; |
| 227 EXPECT_TRUE(frame_reader.ReadBytes(&dest1, arraysize(dest1))); | 222 EXPECT_TRUE(frame_reader.ReadBytes(&dest1, arraysize(dest1))); |
| 228 EXPECT_FALSE(frame_reader.IsDoneReading()); | 223 EXPECT_FALSE(frame_reader.IsDoneReading()); |
| 229 EXPECT_EQ("foo", SpdyStringPiece(dest1, arraysize(dest1))); | 224 EXPECT_EQ("foo", SpdyStringPiece(dest1, arraysize(dest1))); |
| 230 | 225 |
| 231 char dest2[2] = {}; | 226 char dest2[2] = {}; |
| 232 EXPECT_TRUE(frame_reader.ReadBytes(&dest2, arraysize(dest2))); | 227 EXPECT_TRUE(frame_reader.ReadBytes(&dest2, arraysize(dest2))); |
| 233 EXPECT_TRUE(frame_reader.IsDoneReading()); | 228 EXPECT_TRUE(frame_reader.IsDoneReading()); |
| 234 EXPECT_EQ("Hi", SpdyStringPiece(dest2, arraysize(dest2))); | 229 EXPECT_EQ("Hi", SpdyStringPiece(dest2, arraysize(dest2))); |
| 235 } | 230 } |
| 236 | 231 |
| 237 TEST(SpdyFrameReaderTest, ReadBytesWithBufferTooSmall) { | 232 TEST(SpdyFrameReaderTest, ReadBytesWithBufferTooSmall) { |
| 238 // Frame data in network byte order. | 233 // Frame data in network byte order. |
| 239 const char kFrameData[] = { | 234 const char kFrameData[] = { |
| 240 0x01, | 235 0x01, |
| 241 }; | 236 }; |
| 242 | 237 |
| 243 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); | 238 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); |
| 244 EXPECT_FALSE(frame_reader.IsDoneReading()); | 239 EXPECT_FALSE(frame_reader.IsDoneReading()); |
| 245 | 240 |
| 246 char dest[arraysize(kFrameData) + 2] = {}; | 241 char dest[arraysize(kFrameData) + 2] = {}; |
| 247 EXPECT_FALSE(frame_reader.ReadBytes(&dest, arraysize(kFrameData) + 1)); | 242 EXPECT_FALSE(frame_reader.ReadBytes(&dest, arraysize(kFrameData) + 1)); |
| 248 EXPECT_STREQ("", dest); | 243 EXPECT_STREQ("", dest); |
| 249 } | 244 } |
| 250 | 245 |
| 251 } // namespace net | 246 } // namespace net |
| OLD | NEW |