• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "net/tools/content_decoder_tool/content_decoder_tool.h"
6 
7 #include <istream>
8 #include <memory>
9 #include <ostream>
10 #include <utility>
11 #include <zlib.h>
12 
13 #include "base/files/file_util.h"
14 #include "base/path_service.h"
15 #include "net/filter/brotli_source_stream.h"
16 #include "net/filter/filter_source_stream_test_util.h"
17 #include "net/filter/mock_source_stream.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "testing/platform_test.h"
20 
21 namespace net {
22 
23 namespace {
24 
25 const int kBufferSize = 4096;
26 
27 }  // namespace
28 
29 class ContentDecoderToolTest : public PlatformTest {
30  public:
31   ContentDecoderToolTest(const ContentDecoderToolTest&) = delete;
32   ContentDecoderToolTest& operator=(const ContentDecoderToolTest&) = delete;
33 
34  protected:
ContentDecoderToolTest()35   ContentDecoderToolTest() : gzip_encoded_len_(kBufferSize) {}
36 
SetUp()37   void SetUp() override {
38     PlatformTest::SetUp();
39 
40     // Get the path of data directory.
41     base::FilePath data_dir;
42     base::PathService::Get(base::DIR_SOURCE_ROOT, &data_dir);
43     data_dir = data_dir.AppendASCII("net");
44     data_dir = data_dir.AppendASCII("data");
45     data_dir = data_dir.AppendASCII("filter_unittests");
46 
47     // Read data from the original file into buffer.
48     base::FilePath file_path = data_dir.AppendASCII("google.txt");
49     ASSERT_TRUE(base::ReadFileToString(file_path, &source_data_));
50 
51     // Read data from the encoded file into buffer.
52     base::FilePath encoded_file_path = data_dir.AppendASCII("google.br");
53     ASSERT_TRUE(base::ReadFileToString(encoded_file_path, &brotli_encoded_));
54 
55     // Compress original file using gzip.
56     CompressGzip(source_data_.data(), source_data_.size(), gzip_encoded_,
57                  &gzip_encoded_len_, true);
58   }
59 
source_data()60   const std::string& source_data() { return source_data_; }
61 
brotli_encoded()62   const char* brotli_encoded() { return brotli_encoded_.data(); }
brotli_encoded_len()63   size_t brotli_encoded_len() { return brotli_encoded_.size(); }
64 
gzip_encoded()65   char* gzip_encoded() { return gzip_encoded_; }
gzip_encoded_len()66   size_t gzip_encoded_len() { return gzip_encoded_len_; }
67 
68  private:
69   // Original source.
70   std::string source_data_;
71   // Original source encoded with brotli.
72   std::string brotli_encoded_;
73   // Original source encoded with gzip.
74   char gzip_encoded_[kBufferSize];
75   size_t gzip_encoded_len_;
76 };
77 
TEST_F(ContentDecoderToolTest,TestGzip)78 TEST_F(ContentDecoderToolTest, TestGzip) {
79   std::istringstream in(std::string(gzip_encoded(), gzip_encoded_len()));
80   std::vector<std::string> encodings;
81   encodings.push_back("gzip");
82   std::ostringstream out_stream;
83   ContentDecoderToolProcessInput(encodings, &in, &out_stream);
84   std::string output = out_stream.str();
85   EXPECT_EQ(source_data(), output);
86 }
87 
TEST_F(ContentDecoderToolTest,TestBrotli)88 TEST_F(ContentDecoderToolTest, TestBrotli) {
89   // In Cronet build, brotli sources are excluded due to binary size concern.
90   // In such cases, skip the test.
91   auto mock_source_stream = std::make_unique<MockSourceStream>();
92   bool brotli_disabled =
93       CreateBrotliSourceStream(std::move(mock_source_stream)) == nullptr;
94   if (brotli_disabled)
95     return;
96   std::istringstream in(std::string(brotli_encoded(), brotli_encoded_len()));
97   std::vector<std::string> encodings;
98   encodings.push_back("br");
99   std::ostringstream out_stream;
100   ContentDecoderToolProcessInput(encodings, &in, &out_stream);
101   std::string output = out_stream.str();
102   EXPECT_EQ(source_data(), output);
103 }
104 
105 }  // namespace net
106