From 36c35119526023c9e28ec22915b26b1bf7da6bc3 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Tue, 25 Oct 2016 14:17:26 -0700 Subject: applypatch: Add testcases for applypatch executable. Refactor applypatch/main.cpp into libapplypatch_modes so that we can add testcases. Some changes to applypatch/main.cpp: - Replace char** argv with const char**; - Use android::base::Split() to split ":"; - Use android::base::ParseUInt(). Bug: 32383590 Test: Unit tests pass, install-recovery.sh works. Change-Id: I44e7bfa5ab717d439ea1d0ee9ddb7b2c40bb95a4 --- applypatch/Android.mk | 30 +++++- applypatch/applypatch_main.cpp | 28 ++++++ applypatch/applypatch_modes.cpp | 204 ++++++++++++++++++++++++++++++++++++++++ applypatch/applypatch_modes.h | 22 +++++ applypatch/main.cpp | 202 --------------------------------------- 5 files changed, 279 insertions(+), 207 deletions(-) create mode 100644 applypatch/applypatch_main.cpp create mode 100644 applypatch/applypatch_modes.cpp create mode 100644 applypatch/applypatch_modes.h delete mode 100644 applypatch/main.cpp (limited to 'applypatch') diff --git a/applypatch/Android.mk b/applypatch/Android.mk index 9bbac4410..fa0fe8a37 100644 --- a/applypatch/Android.mk +++ b/applypatch/Android.mk @@ -68,21 +68,41 @@ LOCAL_STATIC_LIBRARIES += libcrypto libbz libz LOCAL_CFLAGS := -Werror include $(BUILD_HOST_STATIC_LIBRARY) -# applypatch (executable) +# libapplypatch_modes (static library) # =============================== include $(CLEAR_VARS) LOCAL_CLANG := true -LOCAL_SRC_FILES := main.cpp +LOCAL_SRC_FILES := \ + applypatch_modes.cpp +LOCAL_MODULE := libapplypatch_modes +LOCAL_C_INCLUDES := bootable/recovery +LOCAL_STATIC_LIBRARIES := \ + libapplypatch \ + libbase \ + libedify \ + libcrypto +LOCAL_CFLAGS := -Werror +include $(BUILD_STATIC_LIBRARY) + +# applypatch (target executable) +# =============================== +include $(CLEAR_VARS) +LOCAL_CLANG := true +LOCAL_SRC_FILES := applypatch_main.cpp LOCAL_MODULE := applypatch -LOCAL_C_INCLUDES += bootable/recovery -LOCAL_STATIC_LIBRARIES += \ +LOCAL_C_INCLUDES := bootable/recovery +LOCAL_STATIC_LIBRARIES := \ + libapplypatch_modes \ libapplypatch \ libbase \ libedify \ libotafault \ libcrypto \ libbz -LOCAL_SHARED_LIBRARIES += libbase libz libcutils libc +LOCAL_SHARED_LIBRARIES := \ + libbase \ + libz \ + libcutils LOCAL_CFLAGS := -Werror include $(BUILD_EXECUTABLE) diff --git a/applypatch/applypatch_main.cpp b/applypatch/applypatch_main.cpp new file mode 100644 index 000000000..197077c93 --- /dev/null +++ b/applypatch/applypatch_main.cpp @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2016 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. + */ + +#include "applypatch_modes.h" + +// This program (applypatch) applies binary patches to files in a way that +// is safe (the original file is not touched until we have the desired +// replacement for it) and idempotent (it's okay to run this program +// multiple times). +// +// See the comments to applypatch_modes() function. + +int main(int argc, char** argv) { + return applypatch_modes(argc, const_cast(argv)); +} diff --git a/applypatch/applypatch_modes.cpp b/applypatch/applypatch_modes.cpp new file mode 100644 index 000000000..7b191a801 --- /dev/null +++ b/applypatch/applypatch_modes.cpp @@ -0,0 +1,204 @@ +/* + * Copyright (C) 2009 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. + */ + +#include "applypatch_modes.h" + +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + +#include "applypatch/applypatch.h" +#include "edify/expr.h" + +static int CheckMode(int argc, const char** argv) { + if (argc < 3) { + return 2; + } + std::vector sha1; + for (int i = 3; i < argc; i++) { + sha1.push_back(argv[i]); + } + + return applypatch_check(argv[2], sha1); +} + +static int SpaceMode(int argc, const char** argv) { + if (argc != 3) { + return 2; + } + + size_t bytes; + if (!android::base::ParseUint(argv[2], &bytes) || bytes == 0) { + printf("can't parse \"%s\" as byte count\n\n", argv[2]); + return 1; + } + return CacheSizeCheck(bytes); +} + +// Parse arguments (which should be of the form ":" into the +// new parallel arrays *sha1s and *files. Returns true on success. +static bool ParsePatchArgs(int argc, const char** argv, std::vector* sha1s, + std::vector* files) { + if (sha1s == nullptr) { + return false; + } + for (int i = 0; i < argc; ++i) { + std::vector pieces = android::base::Split(argv[i], ":"); + if (pieces.size() != 2) { + printf("failed to parse patch argument \"%s\"\n", argv[i]); + return false; + } + + uint8_t digest[SHA_DIGEST_LENGTH]; + if (ParseSha1(pieces[0].c_str(), digest) != 0) { + printf("failed to parse sha1 \"%s\"\n", argv[i]); + return false; + } + + sha1s->push_back(pieces[0]); + FileContents fc; + if (LoadFileContents(pieces[1].c_str(), &fc) != 0) { + return false; + } + files->push_back(std::move(fc)); + } + return true; +} + +static int FlashMode(const char* src_filename, const char* tgt_filename, + const char* tgt_sha1, size_t tgt_size) { + return applypatch_flash(src_filename, tgt_filename, tgt_sha1, tgt_size); +} + +static int PatchMode(int argc, const char** argv) { + FileContents bonusFc; + Value bonus(VAL_INVALID, ""); + + if (argc >= 3 && strcmp(argv[1], "-b") == 0) { + if (LoadFileContents(argv[2], &bonusFc) != 0) { + printf("failed to load bonus file %s\n", argv[2]); + return 1; + } + bonus.type = VAL_BLOB; + bonus.data = std::string(bonusFc.data.cbegin(), bonusFc.data.cend()); + argc -= 2; + argv += 2; + } + + if (argc < 4) { + return 2; + } + + size_t target_size; + if (!android::base::ParseUint(argv[4], &target_size) || target_size == 0) { + printf("can't parse \"%s\" as byte count\n\n", argv[4]); + return 1; + } + + // If no : is provided, it is in flash mode. + if (argc == 5) { + if (bonus.type != VAL_INVALID) { + printf("bonus file not supported in flash mode\n"); + return 1; + } + return FlashMode(argv[1], argv[2], argv[3], target_size); + } + + std::vector sha1s; + std::vector files; + if (!ParsePatchArgs(argc-5, argv+5, &sha1s, &files)) { + printf("failed to parse patch args\n"); + return 1; + } + + std::vector> patches; + for (size_t i = 0; i < files.size(); ++i) { + patches.push_back(std::make_unique( + VAL_BLOB, std::string(files[i].data.cbegin(), files[i].data.cend()))); + } + return applypatch(argv[1], argv[2], argv[3], target_size, sha1s, patches, &bonus); +} + +// This program (applypatch) applies binary patches to files in a way that +// is safe (the original file is not touched until we have the desired +// replacement for it) and idempotent (it's okay to run this program +// multiple times). +// +// - if the sha1 hash of is , does nothing and exits +// successfully. +// +// - otherwise, if no : is provided, flashes with +// . must be a partition name, while must +// be a regular image file. will not be deleted on success. +// +// - otherwise, if the sha1 hash of is , applies the +// bsdiff to to produce a new file (the type of patch +// is automatically detected from the file header). If that new +// file has sha1 hash , moves it to replace , and +// exits successfully. Note that if and are +// not the same, is NOT deleted on success. +// may be the string "-" to mean "the same as src-file". +// +// - otherwise, or if any error is encountered, exits with non-zero +// status. +// +// (or in check mode) may refer to an EMMC partition +// to read the source data. See the comments for the +// LoadPartitionContents() function for the format of such a filename. + +int applypatch_modes(int argc, const char** argv) { + if (argc < 2) { + usage: + printf( + "usage: %s [-b ] " + "[: ...]\n" + " or %s -c [ ...]\n" + " or %s -s \n" + " or %s -l\n" + "\n" + "Filenames may be of the form\n" + " EMMC::::::...\n" + "to specify reading from or writing to an EMMC partition.\n\n", + argv[0], argv[0], argv[0], argv[0]); + return 2; + } + + int result; + + if (strncmp(argv[1], "-l", 3) == 0) { + result = ShowLicenses(); + } else if (strncmp(argv[1], "-c", 3) == 0) { + result = CheckMode(argc, argv); + } else if (strncmp(argv[1], "-s", 3) == 0) { + result = SpaceMode(argc, argv); + } else { + result = PatchMode(argc, argv); + } + + if (result == 2) { + goto usage; + } + return result; +} diff --git a/applypatch/applypatch_modes.h b/applypatch/applypatch_modes.h new file mode 100644 index 000000000..3d9d08df5 --- /dev/null +++ b/applypatch/applypatch_modes.h @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2016 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. + */ + +#ifndef _APPLYPATCH_MODES_H +#define _APPLYPATCH_MODES_H + +int applypatch_modes(int argc, const char** argv); + +#endif // _APPLYPATCH_MODES_H diff --git a/applypatch/main.cpp b/applypatch/main.cpp deleted file mode 100644 index 988b6f9b1..000000000 --- a/applypatch/main.cpp +++ /dev/null @@ -1,202 +0,0 @@ -/* - * Copyright (C) 2009 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. - */ - -#include -#include -#include -#include - -#include -#include -#include - -#include - -#include "applypatch/applypatch.h" -#include "edify/expr.h" - -static int CheckMode(int argc, char** argv) { - if (argc < 3) { - return 2; - } - std::vector sha1; - for (int i = 3; i < argc; i++) { - sha1.push_back(argv[i]); - } - - return applypatch_check(argv[2], sha1); -} - -static int SpaceMode(int argc, char** argv) { - if (argc != 3) { - return 2; - } - char* endptr; - size_t bytes = strtol(argv[2], &endptr, 10); - if (bytes == 0 && endptr == argv[2]) { - printf("can't parse \"%s\" as byte count\n\n", argv[2]); - return 1; - } - return CacheSizeCheck(bytes); -} - -// Parse arguments (which should be of the form ":" -// into the new parallel arrays *sha1s and *files.Returns true on -// success. -static bool ParsePatchArgs(int argc, char** argv, std::vector* sha1s, - std::vector* files) { - if (sha1s == nullptr) { - return false; - } - for (int i = 0; i < argc; ++i) { - uint8_t digest[SHA_DIGEST_LENGTH]; - char* colon = strchr(argv[i], ':'); - if (colon == nullptr) { - printf("no ':' in patch argument \"%s\"\n", argv[i]); - return false; - } - *colon = '\0'; - ++colon; - if (ParseSha1(argv[i], digest) != 0) { - printf("failed to parse sha1 \"%s\"\n", argv[i]); - return false; - } - - sha1s->push_back(argv[i]); - FileContents fc; - if (LoadFileContents(colon, &fc) != 0) { - return false; - } - files->push_back(std::move(fc)); - } - return true; -} - -static int FlashMode(const char* src_filename, const char* tgt_filename, - const char* tgt_sha1, size_t tgt_size) { - return applypatch_flash(src_filename, tgt_filename, tgt_sha1, tgt_size); -} - -static int PatchMode(int argc, char** argv) { - FileContents bonusFc; - Value bonus(VAL_INVALID, ""); - - if (argc >= 3 && strcmp(argv[1], "-b") == 0) { - if (LoadFileContents(argv[2], &bonusFc) != 0) { - printf("failed to load bonus file %s\n", argv[2]); - return 1; - } - bonus.type = VAL_BLOB; - bonus.data = std::string(bonusFc.data.cbegin(), bonusFc.data.cend()); - argc -= 2; - argv += 2; - } - - if (argc < 4) { - return 2; - } - - char* endptr; - size_t target_size = strtol(argv[4], &endptr, 10); - if (target_size == 0 && endptr == argv[4]) { - printf("can't parse \"%s\" as byte count\n\n", argv[4]); - return 1; - } - - // If no : is provided, it is in flash mode. - if (argc == 5) { - if (bonus.type != VAL_INVALID) { - printf("bonus file not supported in flash mode\n"); - return 1; - } - return FlashMode(argv[1], argv[2], argv[3], target_size); - } - std::vector sha1s; - std::vector files; - if (!ParsePatchArgs(argc-5, argv+5, &sha1s, &files)) { - printf("failed to parse patch args\n"); - return 1; - } - - std::vector> patches; - for (size_t i = 0; i < files.size(); ++i) { - patches.push_back(std::make_unique( - VAL_BLOB, std::string(files[i].data.cbegin(), files[i].data.cend()))); - } - return applypatch(argv[1], argv[2], argv[3], target_size, sha1s, patches, &bonus); -} - -// This program applies binary patches to files in a way that is safe -// (the original file is not touched until we have the desired -// replacement for it) and idempotent (it's okay to run this program -// multiple times). -// -// - if the sha1 hash of is , does nothing and exits -// successfully. -// -// - otherwise, if no : is provided, flashes with -// . must be a partition name, while must -// be a regular image file. will not be deleted on success. -// -// - otherwise, if the sha1 hash of is , applies the -// bsdiff to to produce a new file (the type of patch -// is automatically detected from the file header). If that new -// file has sha1 hash , moves it to replace , and -// exits successfully. Note that if and are -// not the same, is NOT deleted on success. -// may be the string "-" to mean "the same as src-file". -// -// - otherwise, or if any error is encountered, exits with non-zero -// status. -// -// (or in check mode) may refer to an EMMC partition -// to read the source data. See the comments for the -// LoadPartitionContents() function for the format of such a filename. - -int main(int argc, char** argv) { - if (argc < 2) { - usage: - printf( - "usage: %s [-b ] " - "[: ...]\n" - " or %s -c [ ...]\n" - " or %s -s \n" - " or %s -l\n" - "\n" - "Filenames may be of the form\n" - " EMMC::::::...\n" - "to specify reading from or writing to an EMMC partition.\n\n", - argv[0], argv[0], argv[0], argv[0]); - return 2; - } - - int result; - - if (strncmp(argv[1], "-l", 3) == 0) { - result = ShowLicenses(); - } else if (strncmp(argv[1], "-c", 3) == 0) { - result = CheckMode(argc, argv); - } else if (strncmp(argv[1], "-s", 3) == 0) { - result = SpaceMode(argc, argv); - } else { - result = PatchMode(argc, argv); - } - - if (result == 2) { - goto usage; - } - return result; -} -- cgit v1.2.3