rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 1 | // 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 | |
| 14 | using std::string; |
| 15 | using std::vector; |
| 16 | |
| 17 | namespace chromeos_update_engine { |
| 18 | |
| 19 | class GzipDecompressingFileWriterTest : public ::testing::Test { }; |
| 20 | |
| 21 | TEST(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 | |
| 51 | TEST(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 | |
| 62 | TEST(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 |
| 78 | system((string("cat ") + kPath + " | gzip > " + kPathgz).c_str()); |
| 79 | |
| 80 | // Now read the compressed file and put it into a DecompressingFileWriter |
| 81 | MockFileWriter mock_file_writer; |
| 82 | GzipDecompressingFileWriter decompressing_file_writer(&mock_file_writer); |
| 83 | |
| 84 | const string path("unused"); |
| 85 | ASSERT_EQ(0, decompressing_file_writer.Open( |
| 86 | path.c_str(), O_CREAT | O_LARGEFILE | O_TRUNC | O_WRONLY, 0644)); |
| 87 | |
| 88 | // Open compressed file for reading: |
| 89 | int fd_in = open(kPathgz.c_str(), O_LARGEFILE | O_RDONLY, 0); |
| 90 | ASSERT_GE(fd_in, 0); |
| 91 | char buf[100]; |
| 92 | int sz; |
| 93 | while ((sz = read(fd_in, buf, sizeof(buf))) > 0) { |
| 94 | decompressing_file_writer.Write(buf, sz); |
| 95 | } |
| 96 | close(fd_in); |
| 97 | decompressing_file_writer.Close(); |
| 98 | |
| 99 | ASSERT_EQ(kUncompressedFileSize, mock_file_writer.bytes().size()); |
| 100 | for (unsigned int i = 0; i < kUncompressedFileSize; i++) { |
| 101 | ASSERT_EQ(mock_file_writer.bytes()[i], '0' + (i % 10)) << "i = " << i; |
| 102 | } |
| 103 | unlink(kPath.c_str()); |
| 104 | unlink(kPathgz.c_str()); |
| 105 | } |
| 106 | |
| 107 | } // namespace chromeos_update_engine |