2016-03-10 02:51:34 +01:00
|
|
|
/*
|
|
|
|
* 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 agree 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 <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <string>
|
2016-10-28 03:16:06 +02:00
|
|
|
#include <vector>
|
2016-03-10 02:51:34 +01:00
|
|
|
|
|
|
|
#include <android-base/file.h>
|
2018-07-13 22:11:09 +02:00
|
|
|
#include <android-base/logging.h>
|
|
|
|
#include <android-base/strings.h>
|
2018-03-13 05:18:52 +01:00
|
|
|
#include <bsdiff/bsdiff.h>
|
2018-04-26 03:59:40 +02:00
|
|
|
#include <gtest/gtest.h>
|
2016-10-28 03:16:06 +02:00
|
|
|
#include <openssl/sha.h>
|
2016-03-10 02:51:34 +01:00
|
|
|
|
2016-10-25 23:17:26 +02:00
|
|
|
#include "applypatch/applypatch_modes.h"
|
2016-03-10 02:51:34 +01:00
|
|
|
#include "common/test_constants.h"
|
2018-04-26 03:59:40 +02:00
|
|
|
#include "otautil/paths.h"
|
2017-09-29 23:39:33 +02:00
|
|
|
#include "otautil/print_sha1.h"
|
2018-07-13 22:11:09 +02:00
|
|
|
#include "otautil/sysutil.h"
|
2016-03-10 02:51:34 +01:00
|
|
|
|
2018-03-13 05:18:52 +01:00
|
|
|
using namespace std::string_literals;
|
|
|
|
|
2018-07-13 22:11:09 +02:00
|
|
|
// Loads a given partition and returns a string of form "EMMC:name:size:hash".
|
|
|
|
static std::string GetEmmcTargetString(const std::string& filename,
|
|
|
|
const std::string& display_name = "") {
|
2016-10-25 23:17:26 +02:00
|
|
|
std::string data;
|
2018-07-13 22:11:09 +02:00
|
|
|
if (!android::base::ReadFileToString(filename, &data)) {
|
|
|
|
PLOG(ERROR) << "Failed to read " << filename;
|
|
|
|
return {};
|
2016-10-25 23:17:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t digest[SHA_DIGEST_LENGTH];
|
|
|
|
SHA1(reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), digest);
|
2018-07-13 22:11:09 +02:00
|
|
|
|
|
|
|
return "EMMC:"s + (display_name.empty() ? filename : display_name) + ":" +
|
|
|
|
std::to_string(data.size()) + ":" + print_sha1(digest);
|
2016-03-10 02:51:34 +01:00
|
|
|
}
|
|
|
|
|
2017-10-25 22:16:54 +02:00
|
|
|
class ApplyPatchModesTest : public ::testing::Test {
|
|
|
|
protected:
|
|
|
|
void SetUp() override {
|
2018-07-13 22:11:09 +02:00
|
|
|
source = GetEmmcTargetString(from_testdata_base("boot.img"));
|
|
|
|
ASSERT_FALSE(source.empty());
|
|
|
|
|
|
|
|
std::string recovery_file = from_testdata_base("recovery.img");
|
|
|
|
recovery = GetEmmcTargetString(recovery_file);
|
|
|
|
ASSERT_FALSE(recovery.empty());
|
|
|
|
|
|
|
|
ASSERT_TRUE(android::base::WriteStringToFile("", patched_file_.path));
|
|
|
|
target = GetEmmcTargetString(recovery_file, patched_file_.path);
|
|
|
|
ASSERT_FALSE(target.empty());
|
|
|
|
|
|
|
|
Paths::Get().set_cache_temp_source(cache_source_.path);
|
2016-10-25 23:17:26 +02:00
|
|
|
}
|
2016-03-10 02:51:34 +01:00
|
|
|
|
2018-07-13 22:11:09 +02:00
|
|
|
std::string source;
|
|
|
|
std::string target;
|
|
|
|
std::string recovery;
|
|
|
|
|
|
|
|
private:
|
|
|
|
TemporaryFile cache_source_;
|
|
|
|
TemporaryFile patched_file_;
|
2017-10-25 22:16:54 +02:00
|
|
|
};
|
2016-03-10 02:51:34 +01:00
|
|
|
|
2018-07-13 22:11:09 +02:00
|
|
|
static int InvokeApplyPatchModes(const std::vector<std::string>& args) {
|
|
|
|
auto args_to_call = StringVectorToNullTerminatedArray(args);
|
|
|
|
return applypatch_modes(args_to_call.size() - 1, args_to_call.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void VerifyPatchedTarget(const std::string& target) {
|
|
|
|
std::vector<std::string> pieces = android::base::Split(target, ":");
|
|
|
|
ASSERT_EQ(4, pieces.size());
|
|
|
|
ASSERT_EQ("EMMC", pieces[0]);
|
|
|
|
|
|
|
|
std::string patched_emmc = GetEmmcTargetString(pieces[1]);
|
|
|
|
ASSERT_FALSE(patched_emmc.empty());
|
|
|
|
ASSERT_EQ(target, patched_emmc);
|
|
|
|
}
|
|
|
|
|
2017-10-25 22:16:54 +02:00
|
|
|
TEST_F(ApplyPatchModesTest, InvalidArgs) {
|
2016-10-25 23:17:26 +02:00
|
|
|
// At least two args (including the filename).
|
2018-07-13 22:11:09 +02:00
|
|
|
ASSERT_EQ(2, InvokeApplyPatchModes({ "applypatch" }));
|
2016-10-25 23:17:26 +02:00
|
|
|
|
|
|
|
// Unrecognized args.
|
2018-07-13 22:11:09 +02:00
|
|
|
ASSERT_EQ(2, InvokeApplyPatchModes({ "applypatch", "-x" }));
|
2016-10-25 23:17:26 +02:00
|
|
|
}
|
|
|
|
|
2021-01-09 05:21:30 +01:00
|
|
|
TEST_F(ApplyPatchModesTest, DISABLED_PatchModeEmmcTarget) {
|
2018-07-13 22:11:09 +02:00
|
|
|
std::vector<std::string> args{
|
|
|
|
"applypatch",
|
|
|
|
"--bonus",
|
|
|
|
from_testdata_base("bonus.file"),
|
|
|
|
"--patch",
|
|
|
|
from_testdata_base("recovery-from-boot.p"),
|
|
|
|
"--target",
|
|
|
|
target,
|
|
|
|
"--source",
|
|
|
|
source,
|
|
|
|
};
|
|
|
|
ASSERT_EQ(0, InvokeApplyPatchModes(args));
|
|
|
|
VerifyPatchedTarget(target);
|
2018-03-20 18:33:42 +01:00
|
|
|
}
|
|
|
|
|
2018-07-13 22:11:09 +02:00
|
|
|
// Tests patching an eMMC target without a separate bonus file (i.e. recovery-from-boot patch has
|
2018-03-20 18:33:42 +01:00
|
|
|
// everything).
|
2021-01-09 05:21:30 +01:00
|
|
|
TEST_F(ApplyPatchModesTest, DISABLED_PatchModeEmmcTargetWithoutBonusFile) {
|
2018-07-13 22:11:09 +02:00
|
|
|
std::vector<std::string> args{
|
|
|
|
"applypatch", "--patch", from_testdata_base("recovery-from-boot-with-bonus.p"),
|
|
|
|
"--target", target, "--source",
|
|
|
|
source,
|
|
|
|
};
|
2018-03-20 18:33:42 +01:00
|
|
|
|
2018-07-13 22:11:09 +02:00
|
|
|
ASSERT_EQ(0, InvokeApplyPatchModes(args));
|
|
|
|
VerifyPatchedTarget(target);
|
2016-10-25 23:17:26 +02:00
|
|
|
}
|
|
|
|
|
2018-03-13 05:18:52 +01:00
|
|
|
// Ensures that applypatch works with a bsdiff based recovery-from-boot.p.
|
|
|
|
TEST_F(ApplyPatchModesTest, PatchModeEmmcTargetWithBsdiffPatch) {
|
|
|
|
// Generate the bsdiff patch of recovery-from-boot.p.
|
|
|
|
std::string src_content;
|
2018-07-13 22:11:09 +02:00
|
|
|
ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("boot.img"), &src_content));
|
2018-03-13 05:18:52 +01:00
|
|
|
|
|
|
|
std::string tgt_content;
|
2018-07-13 22:11:09 +02:00
|
|
|
ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("recovery.img"), &tgt_content));
|
2018-03-13 05:18:52 +01:00
|
|
|
|
|
|
|
TemporaryFile patch_file;
|
|
|
|
ASSERT_EQ(0,
|
|
|
|
bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(src_content.data()), src_content.size(),
|
|
|
|
reinterpret_cast<const uint8_t*>(tgt_content.data()), tgt_content.size(),
|
|
|
|
patch_file.path, nullptr));
|
|
|
|
|
2018-07-13 22:11:09 +02:00
|
|
|
std::vector<std::string> args{
|
|
|
|
"applypatch", "--patch", patch_file.path, "--target", target, "--source", source,
|
|
|
|
};
|
|
|
|
ASSERT_EQ(0, InvokeApplyPatchModes(args));
|
|
|
|
VerifyPatchedTarget(target);
|
2018-03-13 05:18:52 +01:00
|
|
|
}
|
|
|
|
|
2017-10-25 22:16:54 +02:00
|
|
|
TEST_F(ApplyPatchModesTest, PatchModeInvalidArgs) {
|
2016-10-25 23:17:26 +02:00
|
|
|
// Invalid bonus file.
|
2018-07-13 22:11:09 +02:00
|
|
|
std::vector<std::string> args{
|
|
|
|
"applypatch", "--bonus", "/doesntexist", "--patch", from_testdata_base("recovery-from-boot.p"),
|
|
|
|
"--target", target, "--source", source,
|
|
|
|
};
|
|
|
|
ASSERT_NE(0, InvokeApplyPatchModes(args));
|
2016-10-25 23:17:26 +02:00
|
|
|
|
|
|
|
// With bonus file, but missing args.
|
2018-07-13 22:11:09 +02:00
|
|
|
ASSERT_NE(0,
|
|
|
|
InvokeApplyPatchModes({ "applypatch", "--bonus", from_testdata_base("bonus.file") }));
|
|
|
|
}
|
2016-10-25 23:17:26 +02:00
|
|
|
|
2018-07-13 22:11:09 +02:00
|
|
|
TEST_F(ApplyPatchModesTest, FlashMode) {
|
|
|
|
std::vector<std::string> args{
|
|
|
|
"applypatch", "--flash", from_testdata_base("recovery.img"), "--target", target,
|
2016-10-25 23:17:26 +02:00
|
|
|
};
|
2018-07-13 22:11:09 +02:00
|
|
|
ASSERT_EQ(0, InvokeApplyPatchModes(args));
|
|
|
|
VerifyPatchedTarget(target);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ApplyPatchModesTest, FlashModeInvalidArgs) {
|
|
|
|
std::vector<std::string> args{
|
|
|
|
"applypatch", "--bonus", from_testdata_base("bonus.file"), "--flash", source,
|
|
|
|
"--target", target,
|
2016-10-25 23:17:26 +02:00
|
|
|
};
|
2018-07-13 22:11:09 +02:00
|
|
|
ASSERT_NE(0, InvokeApplyPatchModes(args));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ApplyPatchModesTest, CheckMode) {
|
|
|
|
ASSERT_EQ(0, InvokeApplyPatchModes({ "applypatch", "--check", recovery }));
|
|
|
|
ASSERT_EQ(0, InvokeApplyPatchModes({ "applypatch", "--check", source }));
|
2016-10-25 23:17:26 +02:00
|
|
|
}
|
|
|
|
|
2017-10-25 22:16:54 +02:00
|
|
|
TEST_F(ApplyPatchModesTest, CheckModeInvalidArgs) {
|
2018-07-13 22:11:09 +02:00
|
|
|
ASSERT_EQ(2, InvokeApplyPatchModes({ "applypatch", "--check" }));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ApplyPatchModesTest, CheckModeNonEmmcTarget) {
|
|
|
|
ASSERT_NE(0, InvokeApplyPatchModes({ "applypatch", "--check", from_testdata_base("boot.img") }));
|
2016-10-25 23:17:26 +02:00
|
|
|
}
|
|
|
|
|
2017-10-25 22:16:54 +02:00
|
|
|
TEST_F(ApplyPatchModesTest, ShowLicenses) {
|
2018-07-13 22:11:09 +02:00
|
|
|
ASSERT_EQ(0, InvokeApplyPatchModes({ "applypatch", "--license" }));
|
2016-03-10 02:51:34 +01:00
|
|
|
}
|