This source file includes following definitions.
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
#include <algorithm>
#include <iostream>
#include "base/memory/scoped_ptr.h"
#include "base/sys_byteorder.h"
#include "net/spdy/spdy_frame_reader.h"
#include "testing/platform_test.h"
namespace net {
TEST(SpdyFrameReaderTest, ReadUInt16) {
const uint16 kFrameData[] = {
htons(1), htons(1<<15),
};
SpdyFrameReader frame_reader(reinterpret_cast<const char *>(kFrameData),
arraysize(kFrameData) * sizeof(uint16));
EXPECT_FALSE(frame_reader.IsDoneReading());
uint16 uint16_val;
EXPECT_TRUE(frame_reader.ReadUInt16(&uint16_val));
EXPECT_FALSE(frame_reader.IsDoneReading());
EXPECT_EQ(1, uint16_val);
EXPECT_TRUE(frame_reader.ReadUInt16(&uint16_val));
EXPECT_TRUE(frame_reader.IsDoneReading());
EXPECT_EQ(1<<15, uint16_val);
}
TEST(SpdyFrameReaderTest, ReadUInt32) {
const uint32 kFrameData[] = {
htonl(1), htonl(1<<31),
};
SpdyFrameReader frame_reader(reinterpret_cast<const char *>(kFrameData),
arraysize(kFrameData) * sizeof(uint32));
EXPECT_FALSE(frame_reader.IsDoneReading());
uint32 uint32_val;
EXPECT_TRUE(frame_reader.ReadUInt32(&uint32_val));
EXPECT_FALSE(frame_reader.IsDoneReading());
EXPECT_EQ(1u, uint32_val);
EXPECT_TRUE(frame_reader.ReadUInt32(&uint32_val));
EXPECT_TRUE(frame_reader.IsDoneReading());
EXPECT_EQ(1u<<31, uint32_val);
}
TEST(SpdyFrameReaderTest, ReadStringPiece16) {
const char kFrameData[] = {
0x00, 0x02,
0x48, 0x69,
0x00, 0x10,
0x54, 0x65, 0x73, 0x74,
0x69, 0x6e, 0x67, 0x2c,
0x20, 0x31, 0x2c, 0x20,
0x32, 0x2c, 0x20, 0x33,
};
SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData));
EXPECT_FALSE(frame_reader.IsDoneReading());
base::StringPiece stringpiece_val;
EXPECT_TRUE(frame_reader.ReadStringPiece16(&stringpiece_val));
EXPECT_FALSE(frame_reader.IsDoneReading());
EXPECT_EQ(0, stringpiece_val.compare("Hi"));
EXPECT_TRUE(frame_reader.ReadStringPiece16(&stringpiece_val));
EXPECT_TRUE(frame_reader.IsDoneReading());
EXPECT_EQ(0, stringpiece_val.compare("Testing, 1, 2, 3"));
}
TEST(SpdyFrameReaderTest, ReadStringPiece32) {
const char kFrameData[] = {
0x00, 0x00, 0x00, 0x03,
0x66, 0x6f, 0x6f,
0x00, 0x00, 0x00, 0x10,
0x54, 0x65, 0x73, 0x74,
0x69, 0x6e, 0x67, 0x2c,
0x20, 0x34, 0x2c, 0x20,
0x35, 0x2c, 0x20, 0x36,
};
SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData));
EXPECT_FALSE(frame_reader.IsDoneReading());
base::StringPiece stringpiece_val;
EXPECT_TRUE(frame_reader.ReadStringPiece32(&stringpiece_val));
EXPECT_FALSE(frame_reader.IsDoneReading());
EXPECT_EQ(0, stringpiece_val.compare("foo"));
EXPECT_TRUE(frame_reader.ReadStringPiece32(&stringpiece_val));
EXPECT_TRUE(frame_reader.IsDoneReading());
EXPECT_EQ(0, stringpiece_val.compare("Testing, 4, 5, 6"));
}
TEST(SpdyFrameReaderTest, ReadUInt16WithBufferTooSmall) {
const char kFrameData[] = {
0x00,
};
SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData));
EXPECT_FALSE(frame_reader.IsDoneReading());
uint16 uint16_val;
EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val));
}
TEST(SpdyFrameReaderTest, ReadUInt32WithBufferTooSmall) {
const char kFrameData[] = {
0x00, 0x00, 0x00,
};
SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData));
EXPECT_FALSE(frame_reader.IsDoneReading());
uint32 uint32_val;
EXPECT_FALSE(frame_reader.ReadUInt32(&uint32_val));
uint16 uint16_val;
EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val));
}
TEST(SpdyFrameReaderTest, ReadStringPiece16WithBufferTooSmall) {
const char kFrameData[] = {
0x00, 0x03,
0x48, 0x69,
};
SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData));
EXPECT_FALSE(frame_reader.IsDoneReading());
base::StringPiece stringpiece_val;
EXPECT_FALSE(frame_reader.ReadStringPiece16(&stringpiece_val));
uint16 uint16_val;
EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val));
}
TEST(SpdyFrameReaderTest, ReadStringPiece16WithBufferWayTooSmall) {
const char kFrameData[] = {
0x00,
};
SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData));
EXPECT_FALSE(frame_reader.IsDoneReading());
base::StringPiece stringpiece_val;
EXPECT_FALSE(frame_reader.ReadStringPiece16(&stringpiece_val));
uint16 uint16_val;
EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val));
}
TEST(SpdyFrameReaderTest, ReadStringPiece32WithBufferTooSmall) {
const char kFrameData[] = {
0x00, 0x00, 0x00, 0x03,
0x48, 0x69,
};
SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData));
EXPECT_FALSE(frame_reader.IsDoneReading());
base::StringPiece stringpiece_val;
EXPECT_FALSE(frame_reader.ReadStringPiece32(&stringpiece_val));
uint16 uint16_val;
EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val));
}
TEST(SpdyFrameReaderTest, ReadStringPiece32WithBufferWayTooSmall) {
const char kFrameData[] = {
0x00, 0x00, 0x00,
};
SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData));
EXPECT_FALSE(frame_reader.IsDoneReading());
base::StringPiece stringpiece_val;
EXPECT_FALSE(frame_reader.ReadStringPiece32(&stringpiece_val));
uint16 uint16_val;
EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val));
}
TEST(SpdyFrameReaderTest, ReadBytes) {
const char kFrameData[] = {
0x66, 0x6f, 0x6f,
0x48, 0x69,
};
SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData));
EXPECT_FALSE(frame_reader.IsDoneReading());
char dest1[3] = {};
EXPECT_TRUE(frame_reader.ReadBytes(&dest1, arraysize(dest1)));
EXPECT_FALSE(frame_reader.IsDoneReading());
EXPECT_EQ("foo", base::StringPiece(dest1, arraysize(dest1)));
char dest2[2] = {};
EXPECT_TRUE(frame_reader.ReadBytes(&dest2, arraysize(dest2)));
EXPECT_TRUE(frame_reader.IsDoneReading());
EXPECT_EQ("Hi", base::StringPiece(dest2, arraysize(dest2)));
}
TEST(SpdyFrameReaderTest, ReadBytesWithBufferTooSmall) {
const char kFrameData[] = {
0x01,
};
SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData));
EXPECT_FALSE(frame_reader.IsDoneReading());
char dest[arraysize(kFrameData) + 2] = {};
EXPECT_FALSE(frame_reader.ReadBytes(&dest, arraysize(kFrameData) + 1));
EXPECT_STREQ("", dest);
}
}