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