summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorTao Bao <tbao@google.com>2018-07-06 23:27:30 +0200
committerandroid-build-merger <android-build-merger@google.com>2018-07-06 23:27:30 +0200
commitcac6932cf4ab481dd150f27ad253c62640204a30 (patch)
treec64bc8cf32ce13086fa9499a077c44a01a2e340e /tests
parentMerge "minui/drm: wait for page flip completion" (diff)
parentMerge "tests: Split unit tests out of component/applypatch_test.cpp." (diff)
downloadandroid_bootable_recovery-cac6932cf4ab481dd150f27ad253c62640204a30.tar
android_bootable_recovery-cac6932cf4ab481dd150f27ad253c62640204a30.tar.gz
android_bootable_recovery-cac6932cf4ab481dd150f27ad253c62640204a30.tar.bz2
android_bootable_recovery-cac6932cf4ab481dd150f27ad253c62640204a30.tar.lz
android_bootable_recovery-cac6932cf4ab481dd150f27ad253c62640204a30.tar.xz
android_bootable_recovery-cac6932cf4ab481dd150f27ad253c62640204a30.tar.zst
android_bootable_recovery-cac6932cf4ab481dd150f27ad253c62640204a30.zip
Diffstat (limited to 'tests')
-rw-r--r--tests/Android.mk45
-rw-r--r--tests/component/applypatch_modes_test.cpp (renamed from tests/component/applypatch_test.cpp)272
-rw-r--r--tests/unit/applypatch_test.cpp303
3 files changed, 331 insertions, 289 deletions
diff --git a/tests/Android.mk b/tests/Android.mk
index d8cbf1c59..daec11f11 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -16,12 +16,34 @@
LOCAL_PATH := $(call my-dir)
+# libapplypatch, libapplypatch_modes, libimgdiff, libimgpatch.
+libapplypatch_static_libraries := \
+ libapplypatch_modes \
+ libapplypatch \
+ libedify \
+ libimgdiff \
+ libimgpatch \
+ libotafault \
+ libotautil \
+ libbsdiff \
+ libbspatch \
+ libdivsufsort \
+ libdivsufsort64 \
+ libutils \
+ libbase \
+ libbrotli \
+ libbz \
+ libcrypto \
+ libz \
+ libziparchive \
+
# Unit tests
include $(CLEAR_VARS)
LOCAL_CFLAGS := -Wall -Werror
LOCAL_MODULE := recovery_unit_test
LOCAL_COMPATIBILITY_SUITE := device-tests
LOCAL_STATIC_LIBRARIES := \
+ $(libapplypatch_static_libraries) \
libverifier \
librecovery_ui \
libminui \
@@ -37,6 +59,7 @@ LOCAL_STATIC_LIBRARIES := \
libBionicGtestMain
LOCAL_SRC_FILES := \
+ unit/applypatch_test.cpp \
unit/asn1_decoder_test.cpp \
unit/commands_test.cpp \
unit/dirutil_test.cpp \
@@ -77,7 +100,7 @@ LOCAL_MODULE := recovery_component_test
LOCAL_COMPATIBILITY_SUITE := device-tests
LOCAL_C_INCLUDES := bootable/recovery
LOCAL_SRC_FILES := \
- component/applypatch_test.cpp \
+ component/applypatch_modes_test.cpp \
component/bootloader_message_test.cpp \
component/edify_test.cpp \
component/imgdiff_test.cpp \
@@ -92,26 +115,6 @@ LOCAL_SRC_FILES := \
LOCAL_SHARED_LIBRARIES := \
libhidlbase
-# libapplypatch, libapplypatch_modes, libimgdiff, libimgpatch.
-libapplypatch_static_libraries := \
- libapplypatch_modes \
- libapplypatch \
- libedify \
- libimgdiff \
- libimgpatch \
- libotafault \
- libotautil \
- libbsdiff \
- libbspatch \
- libdivsufsort \
- libdivsufsort64 \
- libutils \
- libbase \
- libbz \
- libcrypto \
- libz \
- libziparchive \
-
tune2fs_static_libraries := \
libext2_com_err \
libext2_blkid \
diff --git a/tests/component/applypatch_test.cpp b/tests/component/applypatch_modes_test.cpp
index 4e4430919..b0a52b1e8 100644
--- a/tests/component/applypatch_test.cpp
+++ b/tests/component/applypatch_modes_test.cpp
@@ -14,18 +14,9 @@
* limitations under the License.
*/
-#include <dirent.h>
-#include <fcntl.h>
-#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
-#include <sys/stat.h>
-#include <sys/statvfs.h>
-#include <sys/types.h>
-#include <time.h>
-#include <algorithm>
-#include <memory>
#include <string>
#include <vector>
@@ -33,13 +24,11 @@
#include <android-base/logging.h>
#include <android-base/stringprintf.h>
#include <android-base/test_utils.h>
-#include <android-base/unique_fd.h>
#include <bsdiff/bsdiff.h>
#include <gtest/gtest.h>
#include <openssl/sha.h>
#include <zlib.h>
-#include "applypatch/applypatch.h"
#include "applypatch/applypatch_modes.h"
#include "common/test_constants.h"
#include "otautil/paths.h"
@@ -103,14 +92,6 @@ static void sha1sum(const std::string& fname, std::string* sha1, size_t* fsize =
*sha1 = print_sha1(digest);
}
-static void mangle_file(const std::string& fname) {
- std::string content(1024, '\0');
- for (size_t i = 0; i < 1024; i++) {
- content[i] = rand() % 256;
- }
- ASSERT_TRUE(android::base::WriteStringToFile(content, fname));
-}
-
static void test_logger(android::base::LogId /* id */, android::base::LogSeverity severity,
const char* /* tag */, const char* /* file */, unsigned int /* line */,
const char* message) {
@@ -119,43 +100,6 @@ static void test_logger(android::base::LogId /* id */, android::base::LogSeverit
}
}
-class ApplyPatchTest : public ::testing::Test {
- public:
- virtual void SetUp() override {
- // set up files
- old_file = from_testdata_base("old.file");
- new_file = from_testdata_base("new.file");
- nonexistent_file = from_testdata_base("nonexistent.file");
-
- // set up SHA constants
- sha1sum(old_file, &old_sha1, &old_size);
- sha1sum(new_file, &new_sha1, &new_size);
- srand(time(nullptr));
- bad_sha1_a = android::base::StringPrintf("%040x", rand());
- bad_sha1_b = android::base::StringPrintf("%040x", rand());
- }
-
- std::string old_file;
- std::string new_file;
- std::string nonexistent_file;
-
- std::string old_sha1;
- std::string new_sha1;
- std::string bad_sha1_a;
- std::string bad_sha1_b;
-
- size_t old_size;
- size_t new_size;
-};
-
-class ApplyPatchCacheTest : public ApplyPatchTest {
- protected:
- void SetUp() override {
- ApplyPatchTest::SetUp();
- Paths::Get().set_cache_temp_source(old_file);
- }
-};
-
class ApplyPatchModesTest : public ::testing::Test {
protected:
void SetUp() override {
@@ -167,132 +111,6 @@ class ApplyPatchModesTest : public ::testing::Test {
TemporaryFile cache_source;
};
-class FreeCacheTest : public ::testing::Test {
- protected:
- static constexpr size_t PARTITION_SIZE = 4096 * 10;
-
- // Returns a sorted list of files in |dirname|.
- static std::vector<std::string> FindFilesInDir(const std::string& dirname) {
- std::vector<std::string> file_list;
-
- std::unique_ptr<DIR, decltype(&closedir)> d(opendir(dirname.c_str()), closedir);
- struct dirent* de;
- while ((de = readdir(d.get())) != 0) {
- std::string path = dirname + "/" + de->d_name;
-
- struct stat st;
- if (stat(path.c_str(), &st) == 0 && S_ISREG(st.st_mode)) {
- file_list.emplace_back(de->d_name);
- }
- }
-
- std::sort(file_list.begin(), file_list.end());
- return file_list;
- }
-
- static void AddFilesToDir(const std::string& dir, const std::vector<std::string>& files) {
- std::string zeros(4096, 0);
- for (const auto& file : files) {
- std::string path = dir + "/" + file;
- ASSERT_TRUE(android::base::WriteStringToFile(zeros, path));
- }
- }
-
- void SetUp() override {
- Paths::Get().set_cache_log_directory(mock_log_dir.path);
- }
-
- // A mock method to calculate the free space. It assumes the partition has a total size of 40960
- // bytes and all files are 4096 bytes in size.
- size_t MockFreeSpaceChecker(const std::string& dirname) {
- std::vector<std::string> files = FindFilesInDir(dirname);
- return PARTITION_SIZE - 4096 * files.size();
- }
-
- TemporaryDir mock_cache;
- TemporaryDir mock_log_dir;
-};
-
-TEST_F(ApplyPatchTest, CheckModeSkip) {
- std::vector<std::string> sha1s;
- ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s));
-}
-
-TEST_F(ApplyPatchTest, CheckModeSingle) {
- std::vector<std::string> sha1s = { old_sha1 };
- ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s));
-}
-
-TEST_F(ApplyPatchTest, CheckModeMultiple) {
- std::vector<std::string> sha1s = { bad_sha1_a, old_sha1, bad_sha1_b };
- ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s));
-}
-
-TEST_F(ApplyPatchTest, CheckModeFailure) {
- std::vector<std::string> sha1s = { bad_sha1_a, bad_sha1_b };
- ASSERT_NE(0, applypatch_check(&old_file[0], sha1s));
-}
-
-TEST_F(ApplyPatchTest, CheckModeEmmcTarget) {
- // EMMC:old_file:size:sha1 should pass the check.
- std::string src_file =
- "EMMC:" + old_file + ":" + std::to_string(old_size) + ":" + old_sha1;
- std::vector<std::string> sha1s;
- ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s));
-
- // EMMC:old_file:(size-1):sha1:(size+1):sha1 should fail the check.
- src_file = "EMMC:" + old_file + ":" + std::to_string(old_size - 1) + ":" + old_sha1 + ":" +
- std::to_string(old_size + 1) + ":" + old_sha1;
- ASSERT_EQ(1, applypatch_check(src_file.c_str(), sha1s));
-
- // EMMC:old_file:(size-1):sha1:size:sha1:(size+1):sha1 should pass the check.
- src_file = "EMMC:" + old_file + ":" +
- std::to_string(old_size - 1) + ":" + old_sha1 + ":" +
- std::to_string(old_size) + ":" + old_sha1 + ":" +
- std::to_string(old_size + 1) + ":" + old_sha1;
- ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s));
-
- // EMMC:old_file:(size+1):sha1:(size-1):sha1:size:sha1 should pass the check.
- src_file = "EMMC:" + old_file + ":" +
- std::to_string(old_size + 1) + ":" + old_sha1 + ":" +
- std::to_string(old_size - 1) + ":" + old_sha1 + ":" +
- std::to_string(old_size) + ":" + old_sha1;
- ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s));
-
- // EMMC:new_file:(size+1):old_sha1:(size-1):old_sha1:size:old_sha1:size:new_sha1
- // should pass the check.
- src_file = "EMMC:" + new_file + ":" +
- std::to_string(old_size + 1) + ":" + old_sha1 + ":" +
- std::to_string(old_size - 1) + ":" + old_sha1 + ":" +
- std::to_string(old_size) + ":" + old_sha1 + ":" +
- std::to_string(new_size) + ":" + new_sha1;
- ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s));
-}
-
-TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedSourceSingle) {
- TemporaryFile temp_file;
- mangle_file(temp_file.path);
- std::vector<std::string> sha1s_single = { old_sha1 };
- ASSERT_EQ(0, applypatch_check(temp_file.path, sha1s_single));
- ASSERT_EQ(0, applypatch_check(nonexistent_file.c_str(), sha1s_single));
-}
-
-TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedSourceMultiple) {
- TemporaryFile temp_file;
- mangle_file(temp_file.path);
- std::vector<std::string> sha1s_multiple = { bad_sha1_a, old_sha1, bad_sha1_b };
- ASSERT_EQ(0, applypatch_check(temp_file.path, sha1s_multiple));
- ASSERT_EQ(0, applypatch_check(nonexistent_file.c_str(), sha1s_multiple));
-}
-
-TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedSourceFailure) {
- TemporaryFile temp_file;
- mangle_file(temp_file.path);
- std::vector<std::string> sha1s_failure = { bad_sha1_a, bad_sha1_b };
- ASSERT_NE(0, applypatch_check(temp_file.path, sha1s_failure));
- ASSERT_NE(0, applypatch_check(nonexistent_file.c_str(), sha1s_failure));
-}
-
TEST_F(ApplyPatchModesTest, InvalidArgs) {
// At least two args (including the filename).
ASSERT_EQ(2, applypatch_modes(1, (const char* []){ "applypatch" }));
@@ -490,7 +308,7 @@ TEST_F(ApplyPatchModesTest, PatchModeInvalidArgs) {
boot_img.c_str(),
tmp4.path,
recovery_img_sha1.c_str(),
- recovery_img_size.c_str()
+ recovery_img_size.c_str(),
};
ASSERT_NE(0, applypatch_modes(args4.size(), args4.data()));
@@ -504,7 +322,7 @@ TEST_F(ApplyPatchModesTest, PatchModeInvalidArgs) {
tmp5.path,
recovery_img_sha1.c_str(),
recovery_img_size.c_str(),
- bad_arg1.c_str()
+ bad_arg1.c_str(),
};
ASSERT_NE(0, applypatch_modes(args5.size(), args5.data()));
@@ -512,12 +330,9 @@ TEST_F(ApplyPatchModesTest, PatchModeInvalidArgs) {
TemporaryFile tmp6;
std::string patch = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot-with-bonus.p");
std::vector<const char*> args6 = {
- "applypatch",
- boot_img.c_str(),
- tmp6.path,
- recovery_img_sha1.c_str(),
+ "applypatch", boot_img.c_str(), tmp6.path, recovery_img_sha1.c_str(),
"0", // target size
- patch.c_str()
+ patch.c_str(),
};
ASSERT_NE(0, applypatch_modes(args6.size(), args6.data()));
}
@@ -530,82 +345,3 @@ TEST_F(ApplyPatchModesTest, CheckModeInvalidArgs) {
TEST_F(ApplyPatchModesTest, ShowLicenses) {
ASSERT_EQ(0, applypatch_modes(2, (const char* []){ "applypatch", "-l" }));
}
-
-TEST_F(FreeCacheTest, FreeCacheSmoke) {
- std::vector<std::string> files = { "file1", "file2", "file3" };
- AddFilesToDir(mock_cache.path, files);
- ASSERT_EQ(files, FindFilesInDir(mock_cache.path));
- ASSERT_EQ(4096 * 7, MockFreeSpaceChecker(mock_cache.path));
-
- ASSERT_TRUE(RemoveFilesInDirectory(4096 * 9, mock_cache.path, [&](const std::string& dir) {
- return this->MockFreeSpaceChecker(dir);
- }));
-
- ASSERT_EQ(std::vector<std::string>{ "file3" }, FindFilesInDir(mock_cache.path));
- ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_cache.path));
-}
-
-TEST_F(FreeCacheTest, FreeCacheOpenFile) {
- std::vector<std::string> files = { "file1", "file2" };
- AddFilesToDir(mock_cache.path, files);
- ASSERT_EQ(files, FindFilesInDir(mock_cache.path));
- ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_cache.path));
-
- std::string file1_path = mock_cache.path + "/file1"s;
- android::base::unique_fd fd(open(file1_path.c_str(), O_RDONLY));
-
- // file1 can't be deleted as it's opened by us.
- ASSERT_FALSE(RemoveFilesInDirectory(4096 * 10, mock_cache.path, [&](const std::string& dir) {
- return this->MockFreeSpaceChecker(dir);
- }));
-
- ASSERT_EQ(std::vector<std::string>{ "file1" }, FindFilesInDir(mock_cache.path));
-}
-
-TEST_F(FreeCacheTest, FreeCacheLogsSmoke) {
- std::vector<std::string> log_files = { "last_log", "last_log.1", "last_kmsg.2", "last_log.5",
- "last_log.10" };
- AddFilesToDir(mock_log_dir.path, log_files);
- ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path));
-
- ASSERT_TRUE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, [&](const std::string& dir) {
- return this->MockFreeSpaceChecker(dir);
- }));
-
- // Logs with a higher index will be deleted first
- std::vector<std::string> expected = { "last_log", "last_log.1" };
- ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path));
- ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_log_dir.path));
-}
-
-TEST_F(FreeCacheTest, FreeCacheLogsStringComparison) {
- std::vector<std::string> log_files = { "last_log.1", "last_kmsg.1", "last_log.not_number",
- "last_kmsgrandom" };
- AddFilesToDir(mock_log_dir.path, log_files);
- ASSERT_EQ(4096 * 6, MockFreeSpaceChecker(mock_log_dir.path));
-
- ASSERT_TRUE(RemoveFilesInDirectory(4096 * 9, mock_log_dir.path, [&](const std::string& dir) {
- return this->MockFreeSpaceChecker(dir);
- }));
-
- // Logs with incorrect format will be deleted first; and the last_kmsg with the same index is
- // deleted before last_log.
- std::vector<std::string> expected = { "last_log.1" };
- ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path));
- ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_log_dir.path));
-}
-
-TEST_F(FreeCacheTest, FreeCacheLogsOtherFiles) {
- std::vector<std::string> log_files = { "last_install", "command", "block.map", "last_log",
- "last_kmsg.1" };
- AddFilesToDir(mock_log_dir.path, log_files);
- ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path));
-
- ASSERT_FALSE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, [&](const std::string& dir) {
- return this->MockFreeSpaceChecker(dir);
- }));
-
- // Non log files in /cache/recovery won't be deleted.
- std::vector<std::string> expected = { "block.map", "command", "last_install" };
- ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path));
-}
diff --git a/tests/unit/applypatch_test.cpp b/tests/unit/applypatch_test.cpp
new file mode 100644
index 000000000..4fbdd37fa
--- /dev/null
+++ b/tests/unit/applypatch_test.cpp
@@ -0,0 +1,303 @@
+/*
+ * 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 <dirent.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <time.h>
+#include <unistd.h>
+
+#include <algorithm>
+#include <memory>
+#include <string>
+#include <vector>
+
+#include <android-base/file.h>
+#include <android-base/stringprintf.h>
+#include <android-base/test_utils.h>
+#include <android-base/unique_fd.h>
+#include <gtest/gtest.h>
+#include <openssl/sha.h>
+
+#include "applypatch/applypatch.h"
+#include "common/test_constants.h"
+#include "otautil/paths.h"
+#include "otautil/print_sha1.h"
+
+using namespace std::string_literals;
+
+static void sha1sum(const std::string& fname, std::string* sha1, size_t* fsize = nullptr) {
+ ASSERT_TRUE(sha1 != nullptr);
+
+ std::string data;
+ ASSERT_TRUE(android::base::ReadFileToString(fname, &data));
+
+ if (fsize != nullptr) {
+ *fsize = data.size();
+ }
+
+ uint8_t digest[SHA_DIGEST_LENGTH];
+ SHA1(reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), digest);
+ *sha1 = print_sha1(digest);
+}
+
+static void mangle_file(const std::string& fname) {
+ std::string content(1024, '\0');
+ for (size_t i = 0; i < 1024; i++) {
+ content[i] = rand() % 256;
+ }
+ ASSERT_TRUE(android::base::WriteStringToFile(content, fname));
+}
+
+class ApplyPatchTest : public ::testing::Test {
+ public:
+ void SetUp() override {
+ // set up files
+ old_file = from_testdata_base("old.file");
+ new_file = from_testdata_base("new.file");
+ nonexistent_file = from_testdata_base("nonexistent.file");
+
+ // set up SHA constants
+ sha1sum(old_file, &old_sha1, &old_size);
+ sha1sum(new_file, &new_sha1, &new_size);
+ srand(time(nullptr));
+ bad_sha1_a = android::base::StringPrintf("%040x", rand());
+ bad_sha1_b = android::base::StringPrintf("%040x", rand());
+ }
+
+ std::string old_file;
+ std::string new_file;
+ std::string nonexistent_file;
+
+ std::string old_sha1;
+ std::string new_sha1;
+ std::string bad_sha1_a;
+ std::string bad_sha1_b;
+
+ size_t old_size;
+ size_t new_size;
+};
+
+TEST_F(ApplyPatchTest, CheckModeSkip) {
+ std::vector<std::string> sha1s;
+ ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s));
+}
+
+TEST_F(ApplyPatchTest, CheckModeSingle) {
+ std::vector<std::string> sha1s = { old_sha1 };
+ ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s));
+}
+
+TEST_F(ApplyPatchTest, CheckModeMultiple) {
+ std::vector<std::string> sha1s = { bad_sha1_a, old_sha1, bad_sha1_b };
+ ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s));
+}
+
+TEST_F(ApplyPatchTest, CheckModeFailure) {
+ std::vector<std::string> sha1s = { bad_sha1_a, bad_sha1_b };
+ ASSERT_NE(0, applypatch_check(&old_file[0], sha1s));
+}
+
+TEST_F(ApplyPatchTest, CheckModeEmmcTarget) {
+ // EMMC:old_file:size:sha1 should pass the check.
+ std::string src_file = "EMMC:" + old_file + ":" + std::to_string(old_size) + ":" + old_sha1;
+ std::vector<std::string> sha1s;
+ ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s));
+
+ // EMMC:old_file:(size-1):sha1:(size+1):sha1 should fail the check.
+ src_file = "EMMC:" + old_file + ":" + std::to_string(old_size - 1) + ":" + old_sha1 + ":" +
+ std::to_string(old_size + 1) + ":" + old_sha1;
+ ASSERT_EQ(1, applypatch_check(src_file.c_str(), sha1s));
+
+ // EMMC:old_file:(size-1):sha1:size:sha1:(size+1):sha1 should pass the check.
+ src_file = "EMMC:" + old_file + ":" + std::to_string(old_size - 1) + ":" + old_sha1 + ":" +
+ std::to_string(old_size) + ":" + old_sha1 + ":" + std::to_string(old_size + 1) + ":" +
+ old_sha1;
+ ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s));
+
+ // EMMC:old_file:(size+1):sha1:(size-1):sha1:size:sha1 should pass the check.
+ src_file = "EMMC:" + old_file + ":" + std::to_string(old_size + 1) + ":" + old_sha1 + ":" +
+ std::to_string(old_size - 1) + ":" + old_sha1 + ":" + std::to_string(old_size) + ":" +
+ old_sha1;
+ ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s));
+
+ // EMMC:new_file:(size+1):old_sha1:(size-1):old_sha1:size:old_sha1:size:new_sha1
+ // should pass the check.
+ src_file = "EMMC:" + new_file + ":" + std::to_string(old_size + 1) + ":" + old_sha1 + ":" +
+ std::to_string(old_size - 1) + ":" + old_sha1 + ":" + std::to_string(old_size) + ":" +
+ old_sha1 + ":" + std::to_string(new_size) + ":" + new_sha1;
+ ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s));
+}
+
+class ApplyPatchCacheTest : public ApplyPatchTest {
+ protected:
+ void SetUp() override {
+ ApplyPatchTest::SetUp();
+ Paths::Get().set_cache_temp_source(old_file);
+ }
+};
+
+TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedSourceSingle) {
+ TemporaryFile temp_file;
+ mangle_file(temp_file.path);
+ std::vector<std::string> sha1s_single = { old_sha1 };
+ ASSERT_EQ(0, applypatch_check(temp_file.path, sha1s_single));
+ ASSERT_EQ(0, applypatch_check(nonexistent_file.c_str(), sha1s_single));
+}
+
+TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedSourceMultiple) {
+ TemporaryFile temp_file;
+ mangle_file(temp_file.path);
+ std::vector<std::string> sha1s_multiple = { bad_sha1_a, old_sha1, bad_sha1_b };
+ ASSERT_EQ(0, applypatch_check(temp_file.path, sha1s_multiple));
+ ASSERT_EQ(0, applypatch_check(nonexistent_file.c_str(), sha1s_multiple));
+}
+
+TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedSourceFailure) {
+ TemporaryFile temp_file;
+ mangle_file(temp_file.path);
+ std::vector<std::string> sha1s_failure = { bad_sha1_a, bad_sha1_b };
+ ASSERT_NE(0, applypatch_check(temp_file.path, sha1s_failure));
+ ASSERT_NE(0, applypatch_check(nonexistent_file.c_str(), sha1s_failure));
+}
+
+class FreeCacheTest : public ::testing::Test {
+ protected:
+ static constexpr size_t PARTITION_SIZE = 4096 * 10;
+
+ // Returns a sorted list of files in |dirname|.
+ static std::vector<std::string> FindFilesInDir(const std::string& dirname) {
+ std::vector<std::string> file_list;
+
+ std::unique_ptr<DIR, decltype(&closedir)> d(opendir(dirname.c_str()), closedir);
+ struct dirent* de;
+ while ((de = readdir(d.get())) != 0) {
+ std::string path = dirname + "/" + de->d_name;
+
+ struct stat st;
+ if (stat(path.c_str(), &st) == 0 && S_ISREG(st.st_mode)) {
+ file_list.emplace_back(de->d_name);
+ }
+ }
+
+ std::sort(file_list.begin(), file_list.end());
+ return file_list;
+ }
+
+ static void AddFilesToDir(const std::string& dir, const std::vector<std::string>& files) {
+ std::string zeros(4096, 0);
+ for (const auto& file : files) {
+ std::string path = dir + "/" + file;
+ ASSERT_TRUE(android::base::WriteStringToFile(zeros, path));
+ }
+ }
+
+ void SetUp() override {
+ Paths::Get().set_cache_log_directory(mock_log_dir.path);
+ }
+
+ // A mock method to calculate the free space. It assumes the partition has a total size of 40960
+ // bytes and all files are 4096 bytes in size.
+ size_t MockFreeSpaceChecker(const std::string& dirname) {
+ std::vector<std::string> files = FindFilesInDir(dirname);
+ return PARTITION_SIZE - 4096 * files.size();
+ }
+
+ TemporaryDir mock_cache;
+ TemporaryDir mock_log_dir;
+};
+
+TEST_F(FreeCacheTest, FreeCacheSmoke) {
+ std::vector<std::string> files = { "file1", "file2", "file3" };
+ AddFilesToDir(mock_cache.path, files);
+ ASSERT_EQ(files, FindFilesInDir(mock_cache.path));
+ ASSERT_EQ(4096 * 7, MockFreeSpaceChecker(mock_cache.path));
+
+ ASSERT_TRUE(RemoveFilesInDirectory(4096 * 9, mock_cache.path, [&](const std::string& dir) {
+ return this->MockFreeSpaceChecker(dir);
+ }));
+
+ ASSERT_EQ(std::vector<std::string>{ "file3" }, FindFilesInDir(mock_cache.path));
+ ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_cache.path));
+}
+
+TEST_F(FreeCacheTest, FreeCacheOpenFile) {
+ std::vector<std::string> files = { "file1", "file2" };
+ AddFilesToDir(mock_cache.path, files);
+ ASSERT_EQ(files, FindFilesInDir(mock_cache.path));
+ ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_cache.path));
+
+ std::string file1_path = mock_cache.path + "/file1"s;
+ android::base::unique_fd fd(open(file1_path.c_str(), O_RDONLY));
+
+ // file1 can't be deleted as it's opened by us.
+ ASSERT_FALSE(RemoveFilesInDirectory(4096 * 10, mock_cache.path, [&](const std::string& dir) {
+ return this->MockFreeSpaceChecker(dir);
+ }));
+
+ ASSERT_EQ(std::vector<std::string>{ "file1" }, FindFilesInDir(mock_cache.path));
+}
+
+TEST_F(FreeCacheTest, FreeCacheLogsSmoke) {
+ std::vector<std::string> log_files = { "last_log", "last_log.1", "last_kmsg.2", "last_log.5",
+ "last_log.10" };
+ AddFilesToDir(mock_log_dir.path, log_files);
+ ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path));
+
+ ASSERT_TRUE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, [&](const std::string& dir) {
+ return this->MockFreeSpaceChecker(dir);
+ }));
+
+ // Logs with a higher index will be deleted first
+ std::vector<std::string> expected = { "last_log", "last_log.1" };
+ ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path));
+ ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_log_dir.path));
+}
+
+TEST_F(FreeCacheTest, FreeCacheLogsStringComparison) {
+ std::vector<std::string> log_files = { "last_log.1", "last_kmsg.1", "last_log.not_number",
+ "last_kmsgrandom" };
+ AddFilesToDir(mock_log_dir.path, log_files);
+ ASSERT_EQ(4096 * 6, MockFreeSpaceChecker(mock_log_dir.path));
+
+ ASSERT_TRUE(RemoveFilesInDirectory(4096 * 9, mock_log_dir.path, [&](const std::string& dir) {
+ return this->MockFreeSpaceChecker(dir);
+ }));
+
+ // Logs with incorrect format will be deleted first; and the last_kmsg with the same index is
+ // deleted before last_log.
+ std::vector<std::string> expected = { "last_log.1" };
+ ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path));
+ ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_log_dir.path));
+}
+
+TEST_F(FreeCacheTest, FreeCacheLogsOtherFiles) {
+ std::vector<std::string> log_files = { "last_install", "command", "block.map", "last_log",
+ "last_kmsg.1" };
+ AddFilesToDir(mock_log_dir.path, log_files);
+ ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path));
+
+ ASSERT_FALSE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, [&](const std::string& dir) {
+ return this->MockFreeSpaceChecker(dir);
+ }));
+
+ // Non log files in /cache/recovery won't be deleted.
+ std::vector<std::string> expected = { "block.map", "command", "last_install" };
+ ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path));
+}