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 two different SPDY versions: SPDY/2 and SPDY/3.
22 INSTANTIATE_TEST_CASE_P(SpdyFrameBuilderTests
,
24 ::testing::Values(SPDY2
, SPDY3
, SPDY4
));
26 TEST_P(SpdyFrameBuilderTest
, GetWritableBuffer
) {
27 const size_t builder_size
= 10;
28 SpdyFrameBuilder
builder(builder_size
, spdy_version_
);
29 char* writable_buffer
= builder
.GetWritableBuffer(builder_size
);
30 memset(writable_buffer
, ~1, builder_size
);
31 EXPECT_TRUE(builder
.Seek(builder_size
));
32 scoped_ptr
<SpdyFrame
> frame(builder
.take());
33 char expected
[builder_size
];
34 memset(expected
, ~1, builder_size
);
35 EXPECT_EQ(base::StringPiece(expected
, builder_size
),
36 base::StringPiece(frame
->data(), builder_size
));
39 TEST_P(SpdyFrameBuilderTest
, RewriteLength
) {
40 // Create an empty SETTINGS frame both via framer and manually via builder.
41 // The one created via builder is initially given the incorrect length, but
42 // then is corrected via RewriteLength().
43 SpdyFramer
framer(spdy_version_
);
44 SpdySettingsIR settings_ir
;
45 scoped_ptr
<SpdyFrame
> expected(framer
.SerializeSettings(settings_ir
));
46 SpdyFrameBuilder
builder(expected
->size() + 1, spdy_version_
);
47 if (spdy_version_
<= SPDY3
) {
48 builder
.WriteControlFrameHeader(framer
, SETTINGS
, 0);
49 builder
.WriteUInt32(0); // Write the number of settings.
51 builder
.BeginNewFrame(framer
, SETTINGS
, 0, 0);
53 EXPECT_TRUE(builder
.GetWritableBuffer(1) != NULL
);
54 builder
.RewriteLength(framer
);
55 scoped_ptr
<SpdyFrame
> built(builder
.take());
56 EXPECT_EQ(base::StringPiece(expected
->data(), expected
->size()),
57 base::StringPiece(built
->data(), expected
->size()));
60 TEST_P(SpdyFrameBuilderTest
, OverwriteFlags
) {
61 // Create a HEADERS frame both via framer and manually via builder with
62 // different flags set, then make them match using OverwriteFlags().
63 SpdyFramer
framer(spdy_version_
);
64 if (spdy_version_
<= SPDY3
) {
67 SpdyHeadersIR
headers_ir(1);
68 scoped_ptr
<SpdyFrame
> expected(framer
.SerializeHeaders(headers_ir
));
69 SpdyFrameBuilder
builder(expected
->size(), spdy_version_
);
70 builder
.BeginNewFrame(framer
, HEADERS
, 0, 1);
71 builder
.OverwriteFlags(framer
, HEADERS_FLAG_END_HEADERS
);
72 scoped_ptr
<SpdyFrame
> built(builder
.take());
73 EXPECT_EQ(base::StringPiece(expected
->data(), expected
->size()),
74 base::StringPiece(built
->data(), built
->size()));