blob: c1666dbc77c5345cc7a2373384279228fb2d7ec4 [file] [log] [blame]
rspangler@google.com49fdf182009-10-10 00:57:34 +00001// Copyright (c) 2009 The Chromium OS 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.
4
5#include <string.h>
6#include <unistd.h>
7#include <string>
8#include <vector>
9#include <gtest/gtest.h>
10#include "update_engine/decompressing_file_writer.h"
11#include "update_engine/mock_file_writer.h"
12#include "update_engine/test_utils.h"
13
14using std::string;
15using std::vector;
16
17namespace chromeos_update_engine {
18
19class GzipDecompressingFileWriterTest : public ::testing::Test { };
20
21TEST(GzipDecompressingFileWriterTest, SimpleTest) {
22 MockFileWriter mock_file_writer;
23 GzipDecompressingFileWriter decompressing_file_writer(&mock_file_writer);
24
25 // Here is the shell magic to include binary file in C source:
26 // hexdump -v -e '" " 12/1 "0x%02x, " "\n"' $FILENAME
27 // | sed -e '$s/0x ,//g' -e 's/^/ /g' | awk
28 // 'BEGIN { print "unsigned char file[] = {" } END { print "};" } { print }'
29
30 // uncompressed, contains just 3 bytes: "hi\n"
31 unsigned char hi_txt_gz[] = {
32 0x1f, 0x8b, 0x08, 0x08, 0x62, 0xf5, 0x8a, 0x4a,
33 0x02, 0x03, 0x68, 0x69, 0x2e, 0x74, 0x78, 0x74,
34 0x00, 0xcb, 0xc8, 0xe4, 0x02, 0x00, 0x7a, 0x7a,
35 0x6f, 0xed, 0x03, 0x00, 0x00, 0x00,
36 };
37 char hi[] = "hi\n";
38 vector<char> hi_vector(hi, hi + strlen(hi));
39
40 const string path("unused");
41 ASSERT_EQ(0, decompressing_file_writer.Open(
42 path.c_str(), O_CREAT | O_LARGEFILE | O_TRUNC | O_WRONLY, 0644));
43 ASSERT_EQ(sizeof(hi_txt_gz),
44 decompressing_file_writer.Write(hi_txt_gz, sizeof(hi_txt_gz)));
45 ASSERT_EQ(hi_vector.size(), mock_file_writer.bytes().size());
46 for (unsigned int i = 0; i < hi_vector.size(); i++) {
47 EXPECT_EQ(hi_vector[i], mock_file_writer.bytes()[i]) << "i = " << i;
48 }
49}
50
51TEST(GzipDecompressingFileWriterTest, IllegalStreamTest) {
52 MockFileWriter mock_file_writer;
53 GzipDecompressingFileWriter decompressing_file_writer(&mock_file_writer);
54
55 const string path("unused");
56 ASSERT_EQ(0, decompressing_file_writer.Open(
57 path.c_str(), O_CREAT | O_LARGEFILE | O_TRUNC | O_WRONLY, 0644));
58 EXPECT_EQ(0, decompressing_file_writer.Write("\0\0\0\0\0\0\0\0", 8));
59 EXPECT_EQ(0, mock_file_writer.bytes().size());
60}
61
62TEST(GzipDecompressingFileWriterTest, LargeTest) {
63 const string kPath("/tmp/GzipDecompressingFileWriterTest");
64 const string kPathgz(kPath + ".gz");
65 // First, generate some data, say 10 megs:
66 DirectFileWriter uncompressed_file;
67 const char* k10bytes = "0123456789";
68 const unsigned int k10bytesSize = 10;
69 const unsigned int kUncompressedFileSize = strlen(k10bytes) * 1024 * 1024;
70 uncompressed_file.Open(kPath.c_str(),
71 O_CREAT | O_LARGEFILE | O_TRUNC | O_WRONLY, 0644);
72 for (unsigned int i = 0; i < kUncompressedFileSize / k10bytesSize; i++) {
73 ASSERT_EQ(k10bytesSize, uncompressed_file.Write("0123456789", 10));
74 }
75 uncompressed_file.Close();
76
77 // compress the file
Andrew de los Reyes08c4e272010-04-15 14:02:17 -070078 EXPECT_EQ(0,
79 system((string("cat ") + kPath + " | gzip > " + kPathgz).c_str()));
rspangler@google.com49fdf182009-10-10 00:57:34 +000080
81 // Now read the compressed file and put it into a DecompressingFileWriter
82 MockFileWriter mock_file_writer;
83 GzipDecompressingFileWriter decompressing_file_writer(&mock_file_writer);
84
85 const string path("unused");
86 ASSERT_EQ(0, decompressing_file_writer.Open(
87 path.c_str(), O_CREAT | O_LARGEFILE | O_TRUNC | O_WRONLY, 0644));
88
89 // Open compressed file for reading:
90 int fd_in = open(kPathgz.c_str(), O_LARGEFILE | O_RDONLY, 0);
91 ASSERT_GE(fd_in, 0);
92 char buf[100];
93 int sz;
94 while ((sz = read(fd_in, buf, sizeof(buf))) > 0) {
95 decompressing_file_writer.Write(buf, sz);
96 }
97 close(fd_in);
98 decompressing_file_writer.Close();
99
100 ASSERT_EQ(kUncompressedFileSize, mock_file_writer.bytes().size());
101 for (unsigned int i = 0; i < kUncompressedFileSize; i++) {
102 ASSERT_EQ(mock_file_writer.bytes()[i], '0' + (i % 10)) << "i = " << i;
103 }
104 unlink(kPath.c_str());
105 unlink(kPathgz.c_str());
106}
107
108} // namespace chromeos_update_engine