Andrew de los Reyes | 0cca421 | 2010-04-29 14:00:58 -0700 | [diff] [blame^] | 1 | // Copyright (c) 2010 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 <algorithm> |
| 6 | #include <string> |
| 7 | #include <vector> |
| 8 | #include <gtest/gtest.h> |
| 9 | #include "update_engine/split_file_writer.h" |
| 10 | #include "update_engine/test_utils.h" |
| 11 | #include "update_engine/utils.h" |
| 12 | |
| 13 | using std::min; |
| 14 | using std::string; |
| 15 | using std::vector; |
| 16 | |
| 17 | namespace chromeos_update_engine { |
| 18 | |
| 19 | class SplitFileWriterTest : public ::testing::Test { |
| 20 | protected: |
| 21 | void DoTest(size_t bytes_per_call); |
| 22 | }; |
| 23 | |
| 24 | void SplitFileWriterTest::DoTest(size_t bytes_per_call) { |
| 25 | string first_file; |
| 26 | EXPECT_TRUE(utils::MakeTempFile("/tmp/SplitFileWriterTestFirst.XXXXXX", |
| 27 | &first_file, |
| 28 | NULL)); |
| 29 | ScopedPathUnlinker first_file_unlinker(first_file); |
| 30 | string second_file; |
| 31 | EXPECT_TRUE(utils::MakeTempFile("/tmp/SplitFileWriterTestSecond.XXXXXX", |
| 32 | &second_file, |
| 33 | NULL)); |
| 34 | ScopedPathUnlinker second_file_unlinker(second_file); |
| 35 | |
| 36 | // Create joined data |
| 37 | uint64_t first_bytes = 789; |
| 38 | uint64_t second_bytes = 1000; |
| 39 | |
| 40 | vector<char> buf(sizeof(uint64_t) + first_bytes + second_bytes); |
| 41 | uint64_t big_endian_first_bytes = htobe64(first_bytes); |
| 42 | |
| 43 | FillWithData(&buf); |
| 44 | memcpy(&buf[0], &big_endian_first_bytes, sizeof(big_endian_first_bytes)); |
| 45 | |
| 46 | // Create FileWriters |
| 47 | DirectFileWriter first_file_writer; |
| 48 | DirectFileWriter second_file_writer; |
| 49 | SplitFileWriter split_file_writer(&first_file_writer, &second_file_writer); |
| 50 | |
| 51 | split_file_writer.SetFirstOpenArgs( |
| 52 | first_file.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644); |
| 53 | EXPECT_GE(split_file_writer.Open( |
| 54 | second_file.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644), 0); |
| 55 | ScopedFileWriterCloser closer(&split_file_writer); |
| 56 | |
| 57 | size_t bytes_written_total = 0; |
| 58 | for (size_t i = 0; i < buf.size(); i += bytes_per_call) { |
| 59 | size_t bytes_this_iteration = min(bytes_per_call, |
| 60 | buf.size() - bytes_written_total); |
| 61 | EXPECT_EQ(bytes_this_iteration, |
| 62 | split_file_writer.Write(&buf[bytes_written_total], |
| 63 | bytes_this_iteration)); |
| 64 | bytes_written_total += bytes_this_iteration; |
| 65 | } |
| 66 | EXPECT_EQ(buf.size(), bytes_written_total); |
| 67 | |
| 68 | vector<char> first_actual_data; |
| 69 | vector<char> second_actual_data; |
| 70 | EXPECT_TRUE(utils::ReadFile(first_file, &first_actual_data)); |
| 71 | EXPECT_TRUE(utils::ReadFile(second_file, &second_actual_data)); |
| 72 | |
| 73 | EXPECT_EQ(first_bytes, first_actual_data.size()); |
| 74 | EXPECT_EQ(second_bytes, second_actual_data.size()); |
| 75 | |
| 76 | vector<char> first_part_from_orig(&buf[sizeof(uint64_t)], |
| 77 | &buf[sizeof(uint64_t) + first_bytes]); |
| 78 | vector<char> second_part_from_orig( |
| 79 | &buf[sizeof(uint64_t) + first_bytes], |
| 80 | &buf[sizeof(uint64_t) + first_bytes + second_bytes]); |
| 81 | |
| 82 | EXPECT_TRUE(ExpectVectorsEq(first_part_from_orig, first_actual_data)); |
| 83 | EXPECT_TRUE(ExpectVectorsEq(second_part_from_orig, second_actual_data)); |
| 84 | } |
| 85 | |
| 86 | TEST_F(SplitFileWriterTest, SimpleTest) { |
| 87 | DoTest(1024 * 1024 * 1024); // Something very big |
| 88 | } |
| 89 | |
| 90 | TEST_F(SplitFileWriterTest, OneByteAtATimeTest) { |
| 91 | DoTest(1); |
| 92 | } |
| 93 | |
| 94 | TEST_F(SplitFileWriterTest, ThreeBytesAtATimeTest) { |
| 95 | DoTest(3); |
| 96 | } |
| 97 | |
| 98 | } // namespace chromeos_update_engine |