aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorTianjie Xu2018-02-27 17:56:11 -0600
committerTianjie Xu2018-02-28 13:19:11 -0600
commit3bbb20f557790c015e44098098375eb6cc376a42 (patch)
tree9db0b6bd746646486aa990daecf8e3302c49b4c0 /tests
parentac4818d6afe80cb71d19d34cd6c36fd9a04e7b6d (diff)
downloadplatform-bootable-recovery-3bbb20f557790c015e44098098375eb6cc376a42.tar.gz
platform-bootable-recovery-3bbb20f557790c015e44098098375eb6cc376a42.tar.xz
platform-bootable-recovery-3bbb20f557790c015e44098098375eb6cc376a42.zip
Add a singleton CacheLocation to replace the hard coded locations
This class allows us to set the following locations dynamically: cache_temp_source, last_command_file, stash_directory_base. In the updater's main function, we reset the values of these variables to their default locations in /cache; while we can set them to temp files in unit tests or host simulation. Test: unit tests pass Change-Id: I528652650caa41373617ab055d41b1f1a4ec0f87
Diffstat (limited to 'tests')
-rw-r--r--tests/component/applypatch_test.cpp5
-rw-r--r--tests/component/updater_test.cpp21
2 files changed, 17 insertions, 9 deletions
diff --git a/tests/component/applypatch_test.cpp b/tests/component/applypatch_test.cpp
index 21c9a52d..b6d09255 100644
--- a/tests/component/applypatch_test.cpp
+++ b/tests/component/applypatch_test.cpp
@@ -35,6 +35,7 @@
35#include "applypatch/applypatch.h" 35#include "applypatch/applypatch.h"
36#include "applypatch/applypatch_modes.h" 36#include "applypatch/applypatch_modes.h"
37#include "common/test_constants.h" 37#include "common/test_constants.h"
38#include "otautil/cache_location.h"
38#include "otautil/print_sha1.h" 39#include "otautil/print_sha1.h"
39 40
40static void sha1sum(const std::string& fname, std::string* sha1, size_t* fsize = nullptr) { 41static void sha1sum(const std::string& fname, std::string* sha1, size_t* fsize = nullptr) {
@@ -93,14 +94,14 @@ class ApplyPatchCacheTest : public ApplyPatchTest {
93 protected: 94 protected:
94 void SetUp() override { 95 void SetUp() override {
95 ApplyPatchTest::SetUp(); 96 ApplyPatchTest::SetUp();
96 cache_temp_source = old_file; 97 CacheLocation::location().set_cache_temp_source(old_file);
97 } 98 }
98}; 99};
99 100
100class ApplyPatchModesTest : public ::testing::Test { 101class ApplyPatchModesTest : public ::testing::Test {
101 protected: 102 protected:
102 void SetUp() override { 103 void SetUp() override {
103 cache_temp_source = cache_source.path; 104 CacheLocation::location().set_cache_temp_source(cache_source.path);
104 } 105 }
105 106
106 TemporaryFile cache_source; 107 TemporaryFile cache_source;
diff --git a/tests/component/updater_test.cpp b/tests/component/updater_test.cpp
index 448fe493..5bfd7cb4 100644
--- a/tests/component/updater_test.cpp
+++ b/tests/component/updater_test.cpp
@@ -41,6 +41,7 @@
41#include "common/test_constants.h" 41#include "common/test_constants.h"
42#include "edify/expr.h" 42#include "edify/expr.h"
43#include "otautil/SysUtil.h" 43#include "otautil/SysUtil.h"
44#include "otautil/cache_location.h"
44#include "otautil/error_code.h" 45#include "otautil/error_code.h"
45#include "otautil/print_sha1.h" 46#include "otautil/print_sha1.h"
46#include "updater/blockimg.h" 47#include "updater/blockimg.h"
@@ -104,7 +105,16 @@ class UpdaterTest : public ::testing::Test {
104 RegisterBuiltins(); 105 RegisterBuiltins();
105 RegisterInstallFunctions(); 106 RegisterInstallFunctions();
106 RegisterBlockImageFunctions(); 107 RegisterBlockImageFunctions();
108
109 // Mock the location of last_command_file.
110 CacheLocation::location().set_cache_temp_source(temp_saved_source_.path);
111 CacheLocation::location().set_last_command_file(temp_last_command_.path);
112 CacheLocation::location().set_stash_directory_base(temp_stash_base_.path);
107 } 113 }
114
115 TemporaryFile temp_saved_source_;
116 TemporaryFile temp_last_command_;
117 TemporaryDir temp_stash_base_;
108}; 118};
109 119
110TEST_F(UpdaterTest, getprop) { 120TEST_F(UpdaterTest, getprop) {
@@ -542,7 +552,7 @@ TEST_F(UpdaterTest, block_image_update_fail) {
542 expect("", script.c_str(), kNoCause, &updater_info); 552 expect("", script.c_str(), kNoCause, &updater_info);
543 // Updater generates the stash name based on the input file name. 553 // Updater generates the stash name based on the input file name.
544 std::string name_digest = get_sha1(update_file.path); 554 std::string name_digest = get_sha1(update_file.path);
545 std::string stash_base = "/cache/recovery/" + name_digest; 555 std::string stash_base = std::string(temp_stash_base_.path) + "/" + name_digest;
546 ASSERT_EQ(0, access(stash_base.c_str(), F_OK)); 556 ASSERT_EQ(0, access(stash_base.c_str(), F_OK));
547 ASSERT_EQ(-1, access((stash_base + src_hash).c_str(), F_OK)); 557 ASSERT_EQ(-1, access((stash_base + src_hash).c_str(), F_OK));
548 ASSERT_EQ(0, rmdir(stash_base.c_str())); 558 ASSERT_EQ(0, rmdir(stash_base.c_str()));
@@ -709,8 +719,7 @@ TEST_F(UpdaterTest, brotli_new_data) {
709} 719}
710 720
711TEST_F(UpdaterTest, last_command_update) { 721TEST_F(UpdaterTest, last_command_update) {
712 TemporaryFile temp_file; 722 std::string last_command_file = CacheLocation::location().last_command_file();
713 last_command_file = temp_file.path;
714 723
715 std::string block1 = std::string(4096, '1'); 724 std::string block1 = std::string(4096, '1');
716 std::string block2 = std::string(4096, '2'); 725 std::string block2 = std::string(4096, '2');
@@ -797,8 +806,7 @@ TEST_F(UpdaterTest, last_command_update) {
797} 806}
798 807
799TEST_F(UpdaterTest, last_command_update_unresumable) { 808TEST_F(UpdaterTest, last_command_update_unresumable) {
800 TemporaryFile temp_file; 809 std::string last_command_file = CacheLocation::location().last_command_file();
801 last_command_file = temp_file.path;
802 810
803 std::string block1 = std::string(4096, '1'); 811 std::string block1 = std::string(4096, '1');
804 std::string block2 = std::string(4096, '2'); 812 std::string block2 = std::string(4096, '2');
@@ -853,8 +861,7 @@ TEST_F(UpdaterTest, last_command_update_unresumable) {
853} 861}
854 862
855TEST_F(UpdaterTest, last_command_verify) { 863TEST_F(UpdaterTest, last_command_verify) {
856 TemporaryFile temp_file; 864 std::string last_command_file = CacheLocation::location().last_command_file();
857 last_command_file = temp_file.path;
858 865
859 std::string block1 = std::string(4096, '1'); 866 std::string block1 = std::string(4096, '1');
860 std::string block2 = std::string(4096, '2'); 867 std::string block2 = std::string(4096, '2');