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.
7 #include "net/quic/quic_spdy_compressor.h"
8 #include "net/quic/quic_spdy_decompressor.h"
9 #include "net/quic/spdy_utils.h"
10 #include "net/quic/test_tools/quic_test_utils.h"
11 #include "testing/gtest/include/gtest/gtest.h"
19 class QuicSpdyDecompressorTest
: public ::testing::Test
{
21 QuicSpdyDecompressor decompressor_
;
22 QuicSpdyCompressor compressor_
;
23 TestDecompressorVisitor visitor_
;
26 TEST_F(QuicSpdyDecompressorTest
, Decompress
) {
27 SpdyHeaderBlock headers
;
28 headers
[":host"] = "www.google.com";
29 headers
[":path"] = "/index.hml";
30 headers
[":scheme"] = "https";
32 EXPECT_EQ(1u, decompressor_
.current_header_id());
33 string compressed_headers
= compressor_
.CompressHeaders(headers
).substr(4);
34 EXPECT_EQ(compressed_headers
.length(),
35 decompressor_
.DecompressData(compressed_headers
, &visitor_
));
37 EXPECT_EQ(SpdyUtils::SerializeUncompressedHeaders(headers
), visitor_
.data());
38 EXPECT_EQ(2u, decompressor_
.current_header_id());
41 TEST_F(QuicSpdyDecompressorTest
, DecompressPartial
) {
42 SpdyHeaderBlock headers
;
43 headers
[":host"] = "www.google.com";
44 headers
[":path"] = "/index.hml";
45 headers
[":scheme"] = "https";
46 string compressed_headers
= compressor_
.CompressHeaders(headers
).substr(4);
48 for (size_t i
= 0; i
< compressed_headers
.length(); ++i
) {
49 QuicSpdyDecompressor decompressor
;
50 TestDecompressorVisitor visitor
;
52 EXPECT_EQ(1u, decompressor
.current_header_id());
54 string partial_compressed_headers
= compressed_headers
.substr(0, i
);
55 EXPECT_EQ(partial_compressed_headers
.length(),
56 decompressor
.DecompressData(partial_compressed_headers
,
58 EXPECT_EQ(1u, decompressor
.current_header_id()) << "i: " << i
;
60 string remaining_compressed_headers
=
61 compressed_headers
.substr(partial_compressed_headers
.length());
62 EXPECT_EQ(remaining_compressed_headers
.length(),
63 decompressor
.DecompressData(remaining_compressed_headers
,
65 EXPECT_EQ(SpdyUtils::SerializeUncompressedHeaders(headers
), visitor
.data());
67 EXPECT_EQ(2u, decompressor
.current_header_id());
71 TEST_F(QuicSpdyDecompressorTest
, DecompressAndIgnoreTrailingData
) {
72 SpdyHeaderBlock headers
;
73 headers
[":host"] = "www.google.com";
74 headers
[":path"] = "/index.hml";
75 headers
[":scheme"] = "https";
77 string compressed_headers
= compressor_
.CompressHeaders(headers
).substr(4);
78 EXPECT_EQ(compressed_headers
.length(),
79 decompressor_
.DecompressData(compressed_headers
+ "abc123",
82 EXPECT_EQ(SpdyUtils::SerializeUncompressedHeaders(headers
), visitor_
.data());
85 TEST_F(QuicSpdyDecompressorTest
, DecompressError
) {
86 SpdyHeaderBlock headers
;
87 headers
[":host"] = "www.google.com";
88 headers
[":path"] = "/index.hml";
89 headers
[":scheme"] = "https";
91 EXPECT_EQ(1u, decompressor_
.current_header_id());
92 string compressed_headers
= compressor_
.CompressHeaders(headers
).substr(4);
93 compressed_headers
[compressed_headers
.length() - 1] ^= 0x01;
94 EXPECT_NE(compressed_headers
.length(),
95 decompressor_
.DecompressData(compressed_headers
, &visitor_
));
97 EXPECT_TRUE(visitor_
.error());
98 EXPECT_EQ("", visitor_
.data());