From 26b645447f889d7c923c2e56888b6b656d4cdec3 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Wed, 15 Aug 2018 15:18:48 -0700 Subject: tests: Add the missing tests in applypatch_modes_test.cpp. This file was somehow missed when converting from Android.mk to Android.bp. This CL addresses the issue by picking up all the .cpp files in the test source dir. Test: Run recovery_{unit,component}_test on marlin. Check the reported number of tests (94 for unit test and 157 for component test). Change-Id: I50435b07fcb8602ef7b3f7e7d3a69e10b6a5932d --- tests/Android.bp | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/tests/Android.bp b/tests/Android.bp index d305e2560..4764bc415 100644 --- a/tests/Android.bp +++ b/tests/Android.bp @@ -108,15 +108,7 @@ cc_test { test_suites: ["device-tests"], srcs: [ - "unit/asn1_decoder_test.cpp", - "unit/applypatch_test.cpp", - "unit/commands_test.cpp", - "unit/dirutil_test.cpp", - "unit/locale_test.cpp", - "unit/rangeset_test.cpp", - "unit/screen_ui_test.cpp", - "unit/sysutil_test.cpp", - "unit/zip_test.cpp", + "unit/*.cpp", ], static_libs: libapplypatch_static_libs + [ @@ -161,16 +153,7 @@ cc_test { test_suites: ["device-tests"], srcs: [ - "component/bootloader_message_test.cpp", - "component/edify_test.cpp", - "component/imgdiff_test.cpp", - "component/install_test.cpp", - "component/resources_test.cpp", - "component/sideload_test.cpp", - "component/uncrypt_test.cpp", - "component/updater_test.cpp", - "component/update_verifier_test.cpp", - "component/verifier_test.cpp", + "component/*.cpp", ], static_libs: libapplypatch_static_libs + librecovery_static_libs + [ -- cgit v1.2.3 From 435dfac15caf0e89c366d12d779c813c4b763042 Mon Sep 17 00:00:00 2001 From: Andreas Huber Date: Wed, 15 Aug 2018 16:11:25 -0700 Subject: Fixing the Mac SDK build recovery_host_test is now excluded from darwin targets as its dependency libimgdiff is. Change-Id: I6aa085125109ed2218572df97f35289b71c354ab --- tests/Android.bp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/Android.bp b/tests/Android.bp index d305e2560..a880b76d8 100644 --- a/tests/Android.bp +++ b/tests/Android.bp @@ -215,4 +215,11 @@ cc_test_host { ], data: ["testdata/*"], + + target: { + darwin: { + // libimgdiff is not available on the Mac. + enabled: false, + }, + }, } -- cgit v1.2.3 From 3c8927390ed740715602edb99b8da7014e123ff4 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Mon, 18 Jun 2018 09:44:33 -0700 Subject: updater: Add SourceInfo::{ReadAll,DumpBuffer,Overlaps}. Bug: 112151972 Test: Run recovery_unit_test on marlin. Change-Id: Ica2a7b3c768f5d8ca5d591a9560bca9f8ed847c5 --- tests/unit/commands_test.cpp | 116 +++++++++++++++++++++++++++++++++++++ updater/commands.cpp | 70 ++++++++++++++++++++++ updater/include/private/commands.h | 26 +++++++++ 3 files changed, 212 insertions(+) diff --git a/tests/unit/commands_test.cpp b/tests/unit/commands_test.cpp index 19841d676..5c69e07b5 100644 --- a/tests/unit/commands_test.cpp +++ b/tests/unit/commands_test.cpp @@ -14,10 +14,13 @@ * limitations under the License. */ +#include #include #include +#include +#include "otautil/print_sha1.h" #include "otautil/rangeset.h" #include "private/commands.h" @@ -380,3 +383,116 @@ TEST(CommandsTest, Parse_InvalidNumberOfArgs) { ASSERT_FALSE(Command::Parse(input, 0, &err)); } } + +TEST(SourceInfoTest, Overlaps) { + ASSERT_TRUE(SourceInfo("1d74d1a60332fd38cf9405f1bae67917888da6cb", + RangeSet({ { 7, 9 }, { 16, 20 } }), {}, {}) + .Overlaps(TargetInfo("1d74d1a60332fd38cf9405f1bae67917888da6cb", + RangeSet({ { 7, 9 }, { 16, 20 } })))); + + ASSERT_TRUE(SourceInfo("1d74d1a60332fd38cf9405f1bae67917888da6cb", + RangeSet({ { 7, 9 }, { 16, 20 } }), {}, {}) + .Overlaps(TargetInfo("1d74d1a60332fd38cf9405f1bae67917888da6cb", + RangeSet({ { 4, 7 }, { 16, 23 } })))); + + ASSERT_FALSE(SourceInfo("1d74d1a60332fd38cf9405f1bae67917888da6cb", + RangeSet({ { 7, 9 }, { 16, 20 } }), {}, {}) + .Overlaps(TargetInfo("1d74d1a60332fd38cf9405f1bae67917888da6cb", + RangeSet({ { 9, 16 } })))); +} + +TEST(SourceInfoTest, Overlaps_EmptySourceOrTarget) { + ASSERT_FALSE(SourceInfo().Overlaps(TargetInfo())); + + ASSERT_FALSE(SourceInfo().Overlaps( + TargetInfo("1d74d1a60332fd38cf9405f1bae67917888da6cb", RangeSet({ { 7, 9 }, { 16, 20 } })))); + + ASSERT_FALSE(SourceInfo("1d74d1a60332fd38cf9405f1bae67917888da6cb", + RangeSet({ { 7, 9 }, { 16, 20 } }), {}, {}) + .Overlaps(TargetInfo())); +} + +TEST(SourceInfoTest, Overlaps_WithStashes) { + ASSERT_FALSE(SourceInfo("a6cbdf3f416960f02189d3a814ec7e9e95c44a0d", + RangeSet({ { 81, 175 }, { 265, 266 } }), // source ranges + RangeSet({ { 0, 94 }, { 95, 96 } }), // source location + { StashInfo("9eedf00d11061549e32503cadf054ec6fbfa7a23", + RangeSet({ { 94, 95 } })) }) + .Overlaps(TargetInfo("1d74d1a60332fd38cf9405f1bae67917888da6cb", + RangeSet({ { 175, 265 } })))); + + ASSERT_TRUE(SourceInfo("a6cbdf3f416960f02189d3a814ec7e9e95c44a0d", + RangeSet({ { 81, 175 }, { 265, 266 } }), // source ranges + RangeSet({ { 0, 94 }, { 95, 96 } }), // source location + { StashInfo("9eedf00d11061549e32503cadf054ec6fbfa7a23", + RangeSet({ { 94, 95 } })) }) + .Overlaps(TargetInfo("1d74d1a60332fd38cf9405f1bae67917888da6cb", + RangeSet({ { 265, 266 } })))); +} + +// The block size should be specified by the caller of ReadAll (i.e. from Command instance during +// normal run). +constexpr size_t kBlockSize = 4096; + +TEST(SourceInfoTest, ReadAll) { + // "2727756cfee3fbfe24bf5650123fd7743d7b3465" is the SHA-1 hex digest of 8192 * 'a'. + const SourceInfo source("2727756cfee3fbfe24bf5650123fd7743d7b3465", RangeSet({ { 0, 2 } }), {}, + {}); + auto block_reader = [](const RangeSet& src, std::vector* block_buffer) -> int { + std::fill_n(block_buffer->begin(), src.blocks() * kBlockSize, 'a'); + return 0; + }; + auto stash_reader = [](const std::string&, std::vector*) -> int { return 0; }; + std::vector buffer(source.blocks() * kBlockSize); + ASSERT_TRUE(source.ReadAll(&buffer, kBlockSize, block_reader, stash_reader)); + ASSERT_EQ(source.blocks() * kBlockSize, buffer.size()); + + uint8_t digest[SHA_DIGEST_LENGTH]; + SHA1(buffer.data(), buffer.size(), digest); + ASSERT_EQ(source.hash(), print_sha1(digest)); +} + +TEST(SourceInfoTest, ReadAll_WithStashes) { + const SourceInfo source( + // SHA-1 hex digest of 8192 * 'a' + 4096 * 'b'. + "ee3ebea26130769c10ad13604712100346d48660", RangeSet({ { 0, 2 } }), RangeSet({ { 0, 2 } }), + { StashInfo("1e41f7a59e80c6eb4dc043caae80d273f130bed8", RangeSet({ { 2, 3 } })) }); + auto block_reader = [](const RangeSet& src, std::vector* block_buffer) -> int { + std::fill_n(block_buffer->begin(), src.blocks() * kBlockSize, 'a'); + return 0; + }; + auto stash_reader = [](const std::string&, std::vector* stash_buffer) -> int { + std::fill_n(stash_buffer->begin(), kBlockSize, 'b'); + return 0; + }; + std::vector buffer(source.blocks() * kBlockSize); + ASSERT_TRUE(source.ReadAll(&buffer, kBlockSize, block_reader, stash_reader)); + ASSERT_EQ(source.blocks() * kBlockSize, buffer.size()); + + uint8_t digest[SHA_DIGEST_LENGTH]; + SHA1(buffer.data(), buffer.size(), digest); + ASSERT_EQ(source.hash(), print_sha1(digest)); +} + +TEST(SourceInfoTest, ReadAll_BufferTooSmall) { + const SourceInfo source("2727756cfee3fbfe24bf5650123fd7743d7b3465", RangeSet({ { 0, 2 } }), {}, + {}); + auto block_reader = [](const RangeSet&, std::vector*) -> int { return 0; }; + auto stash_reader = [](const std::string&, std::vector*) -> int { return 0; }; + std::vector buffer(source.blocks() * kBlockSize - 1); + ASSERT_FALSE(source.ReadAll(&buffer, kBlockSize, block_reader, stash_reader)); +} + +TEST(SourceInfoTest, ReadAll_FailingReader) { + const SourceInfo source( + "ee3ebea26130769c10ad13604712100346d48660", RangeSet({ { 0, 2 } }), RangeSet({ { 0, 2 } }), + { StashInfo("1e41f7a59e80c6eb4dc043caae80d273f130bed8", RangeSet({ { 2, 3 } })) }); + std::vector buffer(source.blocks() * kBlockSize); + auto failing_block_reader = [](const RangeSet&, std::vector*) -> int { return -1; }; + auto stash_reader = [](const std::string&, std::vector*) -> int { return 0; }; + ASSERT_FALSE(source.ReadAll(&buffer, kBlockSize, failing_block_reader, stash_reader)); + + auto block_reader = [](const RangeSet&, std::vector*) -> int { return 0; }; + auto failing_stash_reader = [](const std::string&, std::vector*) -> int { return -1; }; + ASSERT_FALSE(source.ReadAll(&buffer, kBlockSize, block_reader, failing_stash_reader)); +} diff --git a/updater/commands.cpp b/updater/commands.cpp index 4a90ea873..017086323 100644 --- a/updater/commands.cpp +++ b/updater/commands.cpp @@ -16,6 +16,10 @@ #include "private/commands.h" +#include +#include + +#include #include #include #include @@ -24,7 +28,9 @@ #include #include #include +#include +#include "otautil/print_sha1.h" #include "otautil/rangeset.h" using namespace std::string_literals; @@ -303,6 +309,70 @@ Command Command::Parse(const std::string& line, size_t index, std::string* err) return Command(op, index, line, patch_info, target_info, source_info, stash_info); } +bool SourceInfo::Overlaps(const TargetInfo& target) const { + return ranges_.Overlaps(target.ranges()); +} + +// Moves blocks in the 'source' vector to the specified locations (as in 'locs') in the 'dest' +// vector. Note that source and dest may be the same buffer. +static void MoveRange(std::vector* dest, const RangeSet& locs, + const std::vector& source, size_t block_size) { + const uint8_t* from = source.data(); + uint8_t* to = dest->data(); + size_t start = locs.blocks(); + // Must do the movement backward. + for (auto it = locs.crbegin(); it != locs.crend(); it++) { + size_t blocks = it->second - it->first; + start -= blocks; + memmove(to + (it->first * block_size), from + (start * block_size), blocks * block_size); + } +} + +bool SourceInfo::ReadAll( + std::vector* buffer, size_t block_size, + const std::function*)>& block_reader, + const std::function*)>& stash_reader) const { + if (buffer->size() < blocks() * block_size) { + return false; + } + + // Read in the source ranges. + if (ranges_) { + if (block_reader(ranges_, buffer) != 0) { + return false; + } + if (location_) { + MoveRange(buffer, location_, *buffer, block_size); + } + } + + // Read in the stashes. + for (const StashInfo& stash : stashes_) { + std::vector stash_buffer(stash.blocks() * block_size); + if (stash_reader(stash.id(), &stash_buffer) != 0) { + return false; + } + MoveRange(buffer, stash.ranges(), stash_buffer, block_size); + } + return true; +} + +void SourceInfo::DumpBuffer(const std::vector& buffer, size_t block_size) const { + LOG(INFO) << "Dumping hashes in hex for " << ranges_.blocks() << " source blocks"; + + const RangeSet& location = location_ ? location_ : RangeSet({ Range{ 0, ranges_.blocks() } }); + for (size_t i = 0; i < ranges_.blocks(); i++) { + size_t block_num = ranges_.GetBlockNumber(i); + size_t buffer_index = location.GetBlockNumber(i); + CHECK_LE((buffer_index + 1) * block_size, buffer.size()); + + uint8_t digest[SHA_DIGEST_LENGTH]; + SHA1(buffer.data() + buffer_index * block_size, block_size, digest); + std::string hexdigest = print_sha1(digest); + LOG(INFO) << " block number: " << block_num << ", SHA-1: " << hexdigest; + } +} + std::ostream& operator<<(std::ostream& os, const Command& command) { os << command.index() << ": " << command.cmdline(); return os; diff --git a/updater/include/private/commands.h b/updater/include/private/commands.h index 85b52883b..521289780 100644 --- a/updater/include/private/commands.h +++ b/updater/include/private/commands.h @@ -16,6 +16,9 @@ #pragma once +#include + +#include #include #include #include @@ -111,6 +114,23 @@ class SourceInfo { } } + // Reads all the data specified by this SourceInfo object into the given 'buffer', by calling the + // given readers. Caller needs to specify the block size for the represented blocks. The given + // buffer needs to be sufficiently large. Otherwise it returns false. 'block_reader' and + // 'stash_reader' read the specified data into the given buffer (guaranteed to be large enough) + // respectively. The readers should return 0 on success, or -1 on error. + bool ReadAll( + std::vector* buffer, size_t block_size, + const std::function*)>& block_reader, + const std::function*)>& stash_reader) const; + + // Whether this SourceInfo overlaps with the given TargetInfo object. + bool Overlaps(const TargetInfo& target) const; + + // Dumps the hashes in hex for the given buffer that's loaded from this SourceInfo object + // (excluding the stashed blocks which are handled separately). + void DumpBuffer(const std::vector& buffer, size_t block_size) const; + const std::string& hash() const { return hash_; } @@ -334,6 +354,10 @@ class Command { return hash_tree_info_; } + size_t block_size() const { + return block_size_; + } + constexpr explicit operator bool() const { return type_ != Type::LAST; } @@ -377,6 +401,8 @@ class Command { StashInfo stash_; // The hash_tree info. Only meaningful for COMPUTE_HASH_TREE. HashTreeInfo hash_tree_info_; + // The unit size of each block to be used in this command. + size_t block_size_{ 4096 }; }; std::ostream& operator<<(std::ostream& os, const Command& command); -- cgit v1.2.3 From 53be3327110be6bd52d01cea22223914634dd9fa Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Thu, 16 Aug 2018 11:48:50 -0700 Subject: ui: Only set brightness when needed. There is a regression due to recent change in commit b76af93ab56bc3296e01e65a6fe64a0622ab5b91, where it started to set brightness to normal on every key input. This is unneeded and spamming the recovery log. Test: Run recovery_unit_test on marlin. Test: Built and boot into recovery image. Press a few keys, then check the recovery log. Change-Id: Ia3a7ea400bcccdebbb27a6a7586e435bfb915923 --- ui.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/ui.cpp b/ui.cpp index f1e30f500..14b5b0941 100644 --- a/ui.cpp +++ b/ui.cpp @@ -454,8 +454,7 @@ int RecoveryUI::WaitKey() { return static_cast(KeyError::INTERRUPTED); } - // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is - // plugged in. + // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is plugged in. do { bool rc = key_queue_cond.wait_for(lk, std::chrono::seconds(UI_WAIT_KEY_TIMEOUT_SEC), [this] { return this->key_queue_len != 0 || key_interrupted_; @@ -466,13 +465,13 @@ int RecoveryUI::WaitKey() { } if (screensaver_state_ != ScreensaverState::DISABLED) { if (!rc) { - // Lower the brightness level: NORMAL -> DIMMED; DIMMED -> OFF. + // Must be after a timeout. Lower the brightness level: NORMAL -> DIMMED; DIMMED -> OFF. if (screensaver_state_ == ScreensaverState::NORMAL) { SetScreensaverState(ScreensaverState::DIMMED); } else if (screensaver_state_ == ScreensaverState::DIMMED) { SetScreensaverState(ScreensaverState::OFF); } - } else { + } else if (screensaver_state_ != ScreensaverState::NORMAL) { // Drop the first key if it's changing from OFF to NORMAL. if (screensaver_state_ == ScreensaverState::OFF) { if (key_queue_len > 0) { -- cgit v1.2.3 From 7e520d24fe3a4e97ee900c455a2a4d06a1492287 Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Mon, 13 Aug 2018 16:41:30 -0700 Subject: Add a python binary to generate the protobuf for care_map This binary parses the legacy care_map text in the input file and writes the generated protobuf message into the output file. For test purpose, it also has a "--parse_proto" option to reverse the process and convert a protobuf message file into plain text. The build script will then call the binary to generate the care_map.txt in the new format. Bug: 77867897 Test: Run the binary to convert a care_map.txt, run update_verifier Change-Id: I3ca65e19027404806132aa8d51e9bff766630c99 --- update_verifier/Android.bp | 31 +++++++++- update_verifier/care_map_generator.py | 110 ++++++++++++++++++++++++++++++++++ 2 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 update_verifier/care_map_generator.py diff --git a/update_verifier/Android.bp b/update_verifier/Android.bp index f4dc1f498..7a860a149 100644 --- a/update_verifier/Android.bp +++ b/update_verifier/Android.bp @@ -54,7 +54,7 @@ cc_library_static { proto: { type: "lite", export_proto_headers: true, - } + }, } cc_binary { @@ -88,3 +88,32 @@ cc_binary { "update_verifier.rc", ], } + +python_binary_host { + name: "care_map_generator", + + srcs: [ + "care_map_generator.py", + "care_map.proto", + ], + libs: [ + "python-symbol", + // Soong won't add "libprotobuf-python" to the dependencies if + // filegroup contains .proto files. So add it here explicitly. + "libprotobuf-python", + ], + proto: { + canonical_path_from_root: false, + }, + + version: { + py2: { + enabled: true, + embedded_launcher: true, + }, + py3: { + enabled: false, + embedded_launcher: false, + }, + }, +} diff --git a/update_verifier/care_map_generator.py b/update_verifier/care_map_generator.py new file mode 100644 index 000000000..5057ffea7 --- /dev/null +++ b/update_verifier/care_map_generator.py @@ -0,0 +1,110 @@ +#!/usr/bin/env python +# +# Copyright (C) 2018 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +""" +Parses a input care_map.txt in plain text format; converts it into the proto +buf message; and writes the result to the output file. + +""" + +import argparse +import logging +import sys + +import care_map_pb2 + + +def GenerateCareMapProtoFromLegacyFormat(lines): + """Constructs a care map proto message from the lines of the input file.""" + + # Expected format of the legacy care_map.txt: + # system + # system's care_map ranges + # [vendor] + # [vendor's care_map ranges] + # ... + assert len(lines) % 2 == 0, "line count must be even: {}".format(len(lines)) + + care_map_proto = care_map_pb2.CareMap() + for index in range(0, len(lines), 2): + info = care_map_proto.partitions.add() + info.name = lines[index] + info.ranges = lines[index + 1] + + logging.info("Adding '%s': '%s' to care map", info.name, info.ranges) + + return care_map_proto + + +def ParseProtoMessage(message): + """Parses the care_map proto message and returns its text representation. + Args: + message: care_map in protobuf message + + Returns: + A string of the care_map information, similar to the care_map legacy + format. + """ + care_map_proto = care_map_pb2.CareMap() + care_map_proto.MergeFromString(message) + + info_list = [] + for info in care_map_proto.partitions: + assert info.name, "partition name is required in care_map" + assert info.ranges, "source range is required in care_map" + info_list += [info.name, info.ranges] + + # TODO(xunchang) add a flag to output id & fingerprint also. + return '\n'.join(info_list) + + +def main(argv): + parser = argparse.ArgumentParser( + description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter) + parser.add_argument("input_care_map", + help="Path to the legacy care_map file (or path to" + " care_map in protobuf format if --parse_proto is" + " specified).") + parser.add_argument("output_file", + help="Path to output file to write the result.") + parser.add_argument("--parse_proto", "-p", action="store_true", + help="Parses the input as proto message, and outputs" + " the care_map in plain text.") + parser.add_argument("--verbose", "-v", action="store_true") + + args = parser.parse_args(argv) + + logging_format = '%(filename)s %(levelname)s: %(message)s' + logging.basicConfig(level=logging.INFO if args.verbose else logging.WARNING, + format=logging_format) + + with open(args.input_care_map, 'r') as input_care_map: + content = input_care_map.read() + + if args.parse_proto: + result = ParseProtoMessage(content) + else: + care_map_proto = GenerateCareMapProtoFromLegacyFormat( + content.rstrip().splitlines()) + result = care_map_proto.SerializeToString() + + with open(args.output_file, 'w') as output: + output.write(result) + + +if __name__ == '__main__': + main(sys.argv[1:]) -- cgit v1.2.3 From f8811bbd3a40df5962cf6f6274802a7cfd229f2e Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Mon, 18 Jun 2018 10:03:52 -0700 Subject: updater: Add TransferList class. This would be the top-level class that represents and holds the info parsed from a transfer list file. Bug: 112151972 Test: Run recovery_unit_test and recovery_component_test on marlin. Change-Id: I83b54df9d1411542eeeb8ef4a2db167e97f989c3 --- tests/component/updater_test.cpp | 22 +++++++------ tests/unit/commands_test.cpp | 56 +++++++++++++++++++++++++++++++ updater/commands.cpp | 51 +++++++++++++++++++++++++++++ updater/include/private/commands.h | 67 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 186 insertions(+), 10 deletions(-) diff --git a/tests/component/updater_test.cpp b/tests/component/updater_test.cpp index 248b469b0..2ed797e62 100644 --- a/tests/component/updater_test.cpp +++ b/tests/component/updater_test.cpp @@ -56,8 +56,6 @@ using namespace std::string_literals; using PackageEntries = std::unordered_map; -static constexpr size_t kTransferListHeaderLines = 4; - struct selabel_handle* sehandle = nullptr; static void expect(const char* expected, const std::string& expr_str, CauseCode cause_code, @@ -846,7 +844,8 @@ TEST_F(UpdaterTest, last_command_update) { }; // "2\nstash " + block3_hash + " 2,2,3" - std::string last_command_content = "2\n" + transfer_list_fail[kTransferListHeaderLines + 2]; + std::string last_command_content = + "2\n" + transfer_list_fail[TransferList::kTransferListHeaderLines + 2]; RunBlockImageUpdate(false, entries, image_file_, ""); @@ -895,7 +894,8 @@ TEST_F(UpdaterTest, last_command_update_unresumable) { ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1, image_file_)); - std::string last_command_content = "0\n" + transfer_list_unresumable[kTransferListHeaderLines]; + std::string last_command_content = + "0\n" + transfer_list_unresumable[TransferList::kTransferListHeaderLines]; ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file_)); RunBlockImageUpdate(false, entries, image_file_, ""); @@ -934,7 +934,8 @@ TEST_F(UpdaterTest, last_command_verify) { ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1 + block3, image_file_)); // Last command: "move " + block1_hash + " 2,1,2 1 2,0,1" - std::string last_command_content = "2\n" + transfer_list_verify[kTransferListHeaderLines + 2]; + std::string last_command_content = + "2\n" + transfer_list_verify[TransferList::kTransferListHeaderLines + 2]; // First run: expect the verification to succeed and the last_command_file is intact. ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file_)); @@ -1129,16 +1130,17 @@ static const std::vector g_transfer_list = GenerateTransferList(); INSTANTIATE_TEST_CASE_P(InterruptAfterEachCommand, ResumableUpdaterTest, ::testing::Range(static_cast(0), - g_transfer_list.size() - kTransferListHeaderLines)); + g_transfer_list.size() - + TransferList::kTransferListHeaderLines)); TEST_P(ResumableUpdaterTest, InterruptVerifyResume) { ASSERT_TRUE(android::base::WriteStringToFile(g_source_image, image_file_)); LOG(INFO) << "Interrupting at line " << index_ << " (" - << g_transfer_list[kTransferListHeaderLines + index_] << ")"; + << g_transfer_list[TransferList::kTransferListHeaderLines + index_] << ")"; std::vector transfer_list_copy{ g_transfer_list }; - transfer_list_copy[kTransferListHeaderLines + index_] = "abort"; + transfer_list_copy[TransferList::kTransferListHeaderLines + index_] = "abort"; g_entries["transfer_list"] = android::base::Join(transfer_list_copy, '\n'); @@ -1151,8 +1153,8 @@ TEST_P(ResumableUpdaterTest, InterruptVerifyResume) { if (index_ == 0) { ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK)); } else { - last_command_expected = - std::to_string(index_ - 1) + "\n" + g_transfer_list[kTransferListHeaderLines + index_ - 1]; + last_command_expected = std::to_string(index_ - 1) + "\n" + + g_transfer_list[TransferList::kTransferListHeaderLines + index_ - 1]; std::string last_command_actual; ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual)); ASSERT_EQ(last_command_expected, last_command_actual); diff --git a/tests/unit/commands_test.cpp b/tests/unit/commands_test.cpp index 5c69e07b5..8a54df703 100644 --- a/tests/unit/commands_test.cpp +++ b/tests/unit/commands_test.cpp @@ -17,6 +17,7 @@ #include #include +#include #include #include @@ -496,3 +497,58 @@ TEST(SourceInfoTest, ReadAll_FailingReader) { auto failing_stash_reader = [](const std::string&, std::vector*) -> int { return -1; }; ASSERT_FALSE(source.ReadAll(&buffer, kBlockSize, block_reader, failing_stash_reader)); } + +TEST(TransferListTest, Parse) { + std::vector input_lines{ + "4", // version + "2", // total blocks + "1", // max stashed entries + "1", // max stashed blocks + "stash 1d74d1a60332fd38cf9405f1bae67917888da6cb 2,0,1", + "move 1d74d1a60332fd38cf9405f1bae67917888da6cb 2,0,1 1 2,0,1", + }; + + std::string err; + TransferList transfer_list = TransferList::Parse(android::base::Join(input_lines, '\n'), &err); + ASSERT_TRUE(static_cast(transfer_list)); + ASSERT_EQ(4, transfer_list.version()); + ASSERT_EQ(2, transfer_list.total_blocks()); + ASSERT_EQ(1, transfer_list.stash_max_entries()); + ASSERT_EQ(1, transfer_list.stash_max_blocks()); + ASSERT_EQ(2U, transfer_list.commands().size()); + ASSERT_EQ(Command::Type::STASH, transfer_list.commands()[0].type()); + ASSERT_EQ(Command::Type::MOVE, transfer_list.commands()[1].type()); +} + +TEST(TransferListTest, Parse_InvalidCommand) { + std::vector input_lines{ + "4", // version + "2", // total blocks + "1", // max stashed entries + "1", // max stashed blocks + "stash 1d74d1a60332fd38cf9405f1bae67917888da6cb 2,0,1", + "move 1d74d1a60332fd38cf9405f1bae67917888da6cb 2,0,1 1", + }; + + std::string err; + TransferList transfer_list = TransferList::Parse(android::base::Join(input_lines, '\n'), &err); + ASSERT_FALSE(static_cast(transfer_list)); +} + +TEST(TransferListTest, Parse_ZeroTotalBlocks) { + std::vector input_lines{ + "4", // version + "0", // total blocks + "0", // max stashed entries + "0", // max stashed blocks + }; + + std::string err; + TransferList transfer_list = TransferList::Parse(android::base::Join(input_lines, '\n'), &err); + ASSERT_TRUE(static_cast(transfer_list)); + ASSERT_EQ(4, transfer_list.version()); + ASSERT_EQ(0, transfer_list.total_blocks()); + ASSERT_EQ(0, transfer_list.stash_max_entries()); + ASSERT_EQ(0, transfer_list.stash_max_blocks()); + ASSERT_TRUE(transfer_list.commands().empty()); +} diff --git a/updater/commands.cpp b/updater/commands.cpp index 017086323..aed63369c 100644 --- a/updater/commands.cpp +++ b/updater/commands.cpp @@ -401,3 +401,54 @@ std::ostream& operator<<(std::ostream& os, const SourceInfo& source) { } return os; } + +TransferList TransferList::Parse(const std::string& transfer_list_str, std::string* err) { + TransferList result{}; + + std::vector lines = android::base::Split(transfer_list_str, "\n"); + if (lines.size() < kTransferListHeaderLines) { + *err = android::base::StringPrintf("too few lines in the transfer list [%zu]", lines.size()); + return TransferList{}; + } + + // First line in transfer list is the version number. + if (!android::base::ParseInt(lines[0], &result.version_, 3, 4)) { + *err = "unexpected transfer list version ["s + lines[0] + "]"; + return TransferList{}; + } + + // Second line in transfer list is the total number of blocks we expect to write. + if (!android::base::ParseUint(lines[1], &result.total_blocks_)) { + *err = "unexpected block count ["s + lines[1] + "]"; + return TransferList{}; + } + + // Third line is how many stash entries are needed simultaneously. + if (!android::base::ParseUint(lines[2], &result.stash_max_entries_)) { + return TransferList{}; + } + + // Fourth line is the maximum number of blocks that will be stashed simultaneously. + if (!android::base::ParseUint(lines[3], &result.stash_max_blocks_)) { + *err = "unexpected maximum stash blocks ["s + lines[3] + "]"; + return TransferList{}; + } + + // Subsequent lines are all individual transfer commands. + for (size_t i = kTransferListHeaderLines; i < lines.size(); i++) { + const std::string& line = lines[i]; + if (line.empty()) continue; + + size_t cmdindex = i - kTransferListHeaderLines; + std::string parsing_error; + Command command = Command::Parse(line, cmdindex, &parsing_error); + if (!command) { + *err = android::base::StringPrintf("Failed to parse command %zu [%s]: %s", cmdindex, + line.c_str(), parsing_error.c_str()); + return TransferList{}; + } + result.commands_.push_back(command); + } + + return result; +} diff --git a/updater/include/private/commands.h b/updater/include/private/commands.h index 521289780..79f915434 100644 --- a/updater/include/private/commands.h +++ b/updater/include/private/commands.h @@ -406,3 +406,70 @@ class Command { }; std::ostream& operator<<(std::ostream& os, const Command& command); + +// TransferList represents the info for a transfer list, which is parsed from input text lines +// containing commands to transfer data from one place to another on the target partition. +// +// The creator of the transfer list will guarantee that no block is read (i.e., used as the source +// for a patch or move) after it has been written. +// +// The creator will guarantee that a given stash is loaded (with a stash command) before it's used +// in a move/bsdiff/imgdiff command. +// +// Within one command the source and target ranges may overlap so in general we need to read the +// entire source into memory before writing anything to the target blocks. +// +// All the patch data is concatenated into one patch_data file in the update package. It must be +// stored uncompressed because we memory-map it in directly from the archive. (Since patches are +// already compressed, we lose very little by not compressing their concatenation.) +// +// Commands that read data from the partition (i.e. move/bsdiff/imgdiff/stash) have one or more +// additional hashes before the range parameters, which are used to check if the command has +// already been completed and verify the integrity of the source data. +class TransferList { + public: + // Number of header lines. + static constexpr size_t kTransferListHeaderLines = 4; + + TransferList() = default; + + // Parses the given input string and returns a TransferList object. Sets error message if any. + static TransferList Parse(const std::string& transfer_list_str, std::string* err); + + int version() const { + return version_; + } + + size_t total_blocks() const { + return total_blocks_; + } + + size_t stash_max_entries() const { + return stash_max_entries_; + } + + size_t stash_max_blocks() const { + return stash_max_blocks_; + } + + const std::vector& commands() const { + return commands_; + } + + // Returns whether the TransferList is valid. + constexpr explicit operator bool() const { + return version_ != 0; + } + + private: + // BBOTA version. + int version_{ 0 }; + // Total number of blocks to be written in this transfer. + size_t total_blocks_; + // Maximum number of stashes that exist at the same time. + size_t stash_max_entries_; + // Maximum number of blocks to be stashed. + size_t stash_max_blocks_; + // Commands in this transfer. + std::vector commands_; +}; -- cgit v1.2.3