1 // Copyright (c) 2013 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.
5 #include "net/spdy/spdy_frame_builder.h"
7 #include "net/spdy/spdy_framer.h"
8 #include "net/spdy/spdy_protocol.h"
9 #include "testing/platform_test.h"
13 class SpdyFrameBuilderTest
: public ::testing::TestWithParam
<SpdyMajorVersion
> {
15 virtual void SetUp() {
16 spdy_version_
= GetParam();
19 // Major version of SPDY protocol to be used.
20 SpdyMajorVersion spdy_version_
;
23 // All tests are run with two different SPDY versions: SPDY/2 and SPDY/3.
24 INSTANTIATE_TEST_CASE_P(SpdyFrameBuilderTests
,
26 ::testing::Values(SPDY2
, SPDY3
, SPDY4
));
28 TEST_P(SpdyFrameBuilderTest
, GetWritableBuffer
) {
29 const size_t builder_size
= 10;
30 SpdyFrameBuilder
builder(builder_size
, spdy_version_
);
31 char* writable_buffer
= builder
.GetWritableBuffer(builder_size
);
32 memset(writable_buffer
, ~1, builder_size
);
33 EXPECT_TRUE(builder
.Seek(builder_size
));
34 scoped_ptr
<SpdyFrame
> frame(builder
.take());
35 char expected
[builder_size
];
36 memset(expected
, ~1, builder_size
);
37 EXPECT_EQ(base::StringPiece(expected
, builder_size
),
38 base::StringPiece(frame
->data(), builder_size
));
41 TEST_P(SpdyFrameBuilderTest
, RewriteLength
) {
42 // Create an empty SETTINGS frame both via framer and manually via builder.
43 // The one created via builder is initially given the incorrect length, but
44 // then is corrected via RewriteLength().
45 SpdyFramer
framer(spdy_version_
);
46 SpdySettingsIR settings_ir
;
47 scoped_ptr
<SpdyFrame
> expected(framer
.SerializeSettings(settings_ir
));
48 SpdyFrameBuilder
builder(expected
->size() + 1, spdy_version_
);
49 if (spdy_version_
<= SPDY3
) {
50 builder
.WriteControlFrameHeader(framer
, SETTINGS
, 0);
51 builder
.WriteUInt32(0); // Write the number of settings.
53 builder
.BeginNewFrame(framer
, SETTINGS
, 0, 0);
55 EXPECT_TRUE(builder
.GetWritableBuffer(1) != NULL
);
56 builder
.RewriteLength(framer
);
57 scoped_ptr
<SpdyFrame
> built(builder
.take());
58 EXPECT_EQ(base::StringPiece(expected
->data(), expected
->size()),
59 base::StringPiece(built
->data(), expected
->size()));
62 TEST_P(SpdyFrameBuilderTest
, OverwriteFlags
) {
63 // Create a HEADERS frame both via framer and manually via builder with
64 // different flags set, then make them match using OverwriteFlags().
65 SpdyFramer
framer(spdy_version_
);
66 if (spdy_version_
<= SPDY3
) {
69 SpdyHeadersIR
headers_ir(1);
70 scoped_ptr
<SpdyFrame
> expected(framer
.SerializeHeaders(headers_ir
));
71 SpdyFrameBuilder
builder(expected
->size(), spdy_version_
);
72 builder
.BeginNewFrame(framer
, HEADERS
, 0, 1);
73 builder
.OverwriteFlags(framer
, HEADERS_FLAG_END_HEADERS
);
74 scoped_ptr
<SpdyFrame
> built(builder
.take());
75 EXPECT_EQ(base::StringPiece(expected
->data(), expected
->size()),
76 base::StringPiece(built
->data(), built
->size()));