summaryrefslogtreecommitdiffstats
path: root/net/spdy/spdy_frame_reader_test.cc
blob: c72e5bebac066b39e771f46c1641b9192e5bcfb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#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) {
  // Frame data in network byte order.
  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) {
  // Frame data in network byte order.
  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) {
  // Frame data in network byte order.
  const char kFrameData[] = {
    0x00, 0x02,  // uint16(2)
    0x48, 0x69,  // "Hi"
    0x00, 0x10,  // uint16(16)
    0x54, 0x65, 0x73, 0x74,
    0x69, 0x6e, 0x67, 0x2c,
    0x20, 0x31, 0x2c, 0x20,
    0x32, 0x2c, 0x20, 0x33,  // "Testing, 1, 2, 3"
  };

  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) {
  // Frame data in network byte order.
  const char kFrameData[] = {
    0x00, 0x00, 0x00, 0x03,  // uint32(3)
    0x66, 0x6f, 0x6f,  // "foo"
    0x00, 0x00, 0x00, 0x10,  // uint32(16)
    0x54, 0x65, 0x73, 0x74,
    0x69, 0x6e, 0x67, 0x2c,
    0x20, 0x34, 0x2c, 0x20,
    0x35, 0x2c, 0x20, 0x36,  // "Testing, 4, 5, 6"
  };

  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) {
  // Frame data in network byte order.
  const char kFrameData[] = {
    0x00,  // part of a uint16
  };

  SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData));
  EXPECT_FALSE(frame_reader.IsDoneReading());

  uint16 uint16_val;
  EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val));
}

TEST(SpdyFrameReaderTest, ReadUInt32WithBufferTooSmall) {
  // Frame data in network byte order.
  const char kFrameData[] = {
    0x00, 0x00, 0x00,  // part of a uint32
  };

  SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData));
  EXPECT_FALSE(frame_reader.IsDoneReading());

  uint32 uint32_val;
  EXPECT_FALSE(frame_reader.ReadUInt32(&uint32_val));

  // Also make sure that trying to read a uint16, which technically could work,
  // fails immediately due to previously encountered failed read.
  uint16 uint16_val;
  EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val));
}

// Tests ReadStringPiece16() with a buffer too small to fit the entire string.
TEST(SpdyFrameReaderTest, ReadStringPiece16WithBufferTooSmall) {
  // Frame data in network byte order.
  const char kFrameData[] = {
    0x00, 0x03,  // uint16(3)
    0x48, 0x69,  // "Hi"
  };

  SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData));
  EXPECT_FALSE(frame_reader.IsDoneReading());

  base::StringPiece stringpiece_val;
  EXPECT_FALSE(frame_reader.ReadStringPiece16(&stringpiece_val));

  // Also make sure that trying to read a uint16, which technically could work,
  // fails immediately due to previously encountered failed read.
  uint16 uint16_val;
  EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val));
}

// Tests ReadStringPiece16() with a buffer too small even to fit the length.
TEST(SpdyFrameReaderTest, ReadStringPiece16WithBufferWayTooSmall) {
  // Frame data in network byte order.
  const char kFrameData[] = {
    0x00,  // part of a uint16
  };

  SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData));
  EXPECT_FALSE(frame_reader.IsDoneReading());

  base::StringPiece stringpiece_val;
  EXPECT_FALSE(frame_reader.ReadStringPiece16(&stringpiece_val));

  // Also make sure that trying to read a uint16, which technically could work,
  // fails immediately due to previously encountered failed read.
  uint16 uint16_val;
  EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val));
}

// Tests ReadStringPiece32() with a buffer too small to fit the entire string.
TEST(SpdyFrameReaderTest, ReadStringPiece32WithBufferTooSmall) {
  // Frame data in network byte order.
  const char kFrameData[] = {
    0x00, 0x00, 0x00, 0x03,  // uint32(3)
    0x48, 0x69,  // "Hi"
  };

  SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData));
  EXPECT_FALSE(frame_reader.IsDoneReading());

  base::StringPiece stringpiece_val;
  EXPECT_FALSE(frame_reader.ReadStringPiece32(&stringpiece_val));

  // Also make sure that trying to read a uint16, which technically could work,
  // fails immediately due to previously encountered failed read.
  uint16 uint16_val;
  EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val));
}

// Tests ReadStringPiece32() with a buffer too small even to fit the length.
TEST(SpdyFrameReaderTest, ReadStringPiece32WithBufferWayTooSmall) {
  // Frame data in network byte order.
  const char kFrameData[] = {
    0x00, 0x00, 0x00,  // part of a uint32
  };

  SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData));
  EXPECT_FALSE(frame_reader.IsDoneReading());

  base::StringPiece stringpiece_val;
  EXPECT_FALSE(frame_reader.ReadStringPiece32(&stringpiece_val));

  // Also make sure that trying to read a uint16, which technically could work,
  // fails immediately due to previously encountered failed read.
  uint16 uint16_val;
  EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val));
}

TEST(SpdyFrameReaderTest, ReadBytes) {
  // Frame data in network byte order.
  const char kFrameData[] = {
    0x66, 0x6f, 0x6f,  // "foo"
    0x48, 0x69,  // "Hi"
  };

  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) {
  // Frame data in network byte order.
  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);
}

}  // namespace net