blob: 421db1e0cdd5cf8b0c6155e356ed8c4e3e81fb88 [file] [log] [blame]
// Copyright 2018 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "modemfwd/file_decompressor.h"
#include <memory>
#include <string>
#include <base/check.h>
#include <base/files/file_path.h>
#include <base/files/file_util.h>
#include <base/logging.h>
#include <base/stl_util.h>
#include <gtest/gtest.h>
#include "modemfwd/scoped_temp_file.h"
namespace modemfwd {
namespace {
class FileDecompressorTest : public testing::Test {
: in_file_(ScopedTempFile::Create()),
out_file_(ScopedTempFile::Create()) {
std::unique_ptr<ScopedTempFile> in_file_;
std::unique_ptr<ScopedTempFile> out_file_;
TEST_F(FileDecompressorTest, DecompressNonExistentInputFile) {
DecompressXzFile(base::FilePath("non_existent_file"), out_file_->path()));
TEST_F(FileDecompressorTest, DecompressEmptyFile) {
EXPECT_FALSE(DecompressXzFile(in_file_->path(), out_file_->path()));
TEST_F(FileDecompressorTest, DecompressMalformedFile) {
std::string content = "foo";
ASSERT_EQ(base::WriteFile(in_file_->path(),, content.size()),
EXPECT_FALSE(DecompressXzFile(in_file_->path(), out_file_->path()));
TEST_F(FileDecompressorTest, Decompress1MOfZeroes) {
// Generated from `dd if=/dev/zero bs=1024 count=1024 | xz | xxd -i`
static const uint8_t kCompressedContent[] = {
0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00, 0x00, 0x04, 0xe6, 0xd6, 0xb4, 0x46,
0x02, 0x00, 0x21, 0x01, 0x16, 0x00, 0x00, 0x00, 0x74, 0x2f, 0xe5, 0xa3,
0xef, 0xff, 0xff, 0x00, 0xd9, 0x5d, 0x00, 0x00, 0x6f, 0xfd, 0xff, 0xff,
0xa3, 0xb7, 0xff, 0x47, 0x3e, 0x48, 0x15, 0x72, 0x39, 0x61, 0x51, 0xb8,
0x92, 0x28, 0xe6, 0xa3, 0x86, 0x07, 0xf9, 0xee, 0xe4, 0x1e, 0x82, 0xd3,
0x2f, 0xc5, 0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x25, 0x00, 0x00, 0x00, 0x00, 0x00,
0xc2, 0xf6, 0xba, 0x3e, 0xa2, 0x70, 0x6b, 0x60, 0x00, 0x01, 0xf5, 0x01,
0x80, 0x80, 0x40, 0x00, 0x79, 0x97, 0x7f, 0x47, 0xb1, 0xc4, 0x67, 0xfb,
0x02, 0x00, 0x00, 0x00, 0x00, 0x04, 0x59, 0x5a,
reinterpret_cast<const char*>(kCompressedContent),
EXPECT_TRUE(DecompressXzFile(in_file_->path(), out_file_->path()));
std::string content;
ASSERT_TRUE(base::ReadFileToString(out_file_->path(), &content));
EXPECT_EQ(1024 * 1024, content.size());
for (size_t i = 0; i < content.size(); ++i)
EXPECT_EQ(0, content[i]);
} // namespace
} // namespace modemfwd