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