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 void SetUp() override
{ spdy_version_
= GetParam(); }
17 // Major version of SPDY protocol to be used.
18 SpdyMajorVersion spdy_version_
;
21 // All tests are run with three different SPDY versions: SPDY/2, SPDY/3 and
23 INSTANTIATE_TEST_CASE_P(SpdyFrameBuilderTests
,
25 ::testing::Values(SPDY2
, SPDY3
, HTTP2
));
27 TEST_P(SpdyFrameBuilderTest
, GetWritableBuffer
) {
28 const size_t builder_size
= 10;
29 SpdyFrameBuilder
builder(builder_size
, spdy_version_
);
30 char* writable_buffer
= builder
.GetWritableBuffer(builder_size
);
31 memset(writable_buffer
, ~1, builder_size
);
32 EXPECT_TRUE(builder
.Seek(builder_size
));
33 scoped_ptr
<SpdyFrame
> frame(builder
.take());
34 char expected
[builder_size
];
35 memset(expected
, ~1, builder_size
);
36 EXPECT_EQ(base::StringPiece(expected
, builder_size
),
37 base::StringPiece(frame
->data(), builder_size
));
40 TEST_P(SpdyFrameBuilderTest
, RewriteLength
) {
41 // Create an empty SETTINGS frame both via framer and manually via builder.
42 // The one created via builder is initially given the incorrect length, but
43 // then is corrected via RewriteLength().
44 SpdyFramer
framer(spdy_version_
);
45 SpdySettingsIR settings_ir
;
46 scoped_ptr
<SpdyFrame
> expected(framer
.SerializeSettings(settings_ir
));
47 SpdyFrameBuilder
builder(expected
->size() + 1, spdy_version_
);
48 if (spdy_version_
<= SPDY3
) {
49 builder
.WriteControlFrameHeader(framer
, SETTINGS
, 0);
50 builder
.WriteUInt32(0); // Write the number of settings.
52 builder
.BeginNewFrame(framer
, SETTINGS
, 0, 0);
54 EXPECT_TRUE(builder
.GetWritableBuffer(1) != NULL
);
55 builder
.RewriteLength(framer
);
56 scoped_ptr
<SpdyFrame
> built(builder
.take());
57 EXPECT_EQ(base::StringPiece(expected
->data(), expected
->size()),
58 base::StringPiece(built
->data(), expected
->size()));
61 TEST_P(SpdyFrameBuilderTest
, OverwriteFlags
) {
62 // Create a HEADERS frame both via framer and manually via builder with
63 // different flags set, then make them match using OverwriteFlags().
64 SpdyFramer
framer(spdy_version_
);
65 if (spdy_version_
<= SPDY3
) {
68 SpdyHeadersIR
headers_ir(1);
69 scoped_ptr
<SpdyFrame
> expected(framer
.SerializeHeaders(headers_ir
));
70 SpdyFrameBuilder
builder(expected
->size(), spdy_version_
);
71 builder
.BeginNewFrame(framer
, HEADERS
, 0, 1);
72 builder
.OverwriteFlags(framer
, HEADERS_FLAG_END_HEADERS
);
73 scoped_ptr
<SpdyFrame
> built(builder
.take());
74 EXPECT_EQ(base::StringPiece(expected
->data(), expected
->size()),
75 base::StringPiece(built
->data(), built
->size()));