summaryrefslogtreecommitdiffstats
path: root/tests/unit/applypatch_test.cpp
blob: 066f981b4b7737b03c0b3ed68c25752c9aa84681 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 * 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 <inttypes.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/logging.h>
#include <android-base/stringprintf.h>
#include <android-base/test_utils.h>
#include <android-base/unique_fd.h>
#include <gtest/gtest.h>

#include "applypatch/applypatch.h"
#include "common/test_constants.h"
#include "edify/expr.h"
#include "otautil/paths.h"
#include "otautil/print_sha1.h"

using namespace std::string_literals;

class ApplyPatchTest : public ::testing::Test {
 protected:
  void SetUp() override {
    source_file = from_testdata_base("boot.img");
    FileContents boot_fc;
    ASSERT_TRUE(LoadFileContents(source_file, &boot_fc));
    source_size = boot_fc.data.size();
    source_sha1 = print_sha1(boot_fc.sha1);

    target_file = from_testdata_base("recovery.img");
    FileContents recovery_fc;
    ASSERT_TRUE(LoadFileContents(target_file, &recovery_fc));
    target_size = recovery_fc.data.size();
    target_sha1 = print_sha1(recovery_fc.sha1);

    source_partition = Partition(source_file, source_size, source_sha1);
    target_partition = Partition(partition_file.path, target_size, target_sha1);

    srand(time(nullptr));
    bad_sha1_a = android::base::StringPrintf("%040x", rand());
    bad_sha1_b = android::base::StringPrintf("%040x", rand());

    // Reset the cache backup file.
    Paths::Get().set_cache_temp_source(cache_temp_source.path);
  }

  void TearDown() override {
    ASSERT_TRUE(android::base::RemoveFileIfExists(cache_temp_source.path));
  }

  std::string source_file;
  std::string source_sha1;
  size_t source_size;

  std::string target_file;
  std::string target_sha1;
  size_t target_size;

  std::string bad_sha1_a;
  std::string bad_sha1_b;

  Partition source_partition;
  Partition target_partition;

 private:
  TemporaryFile partition_file;
  TemporaryFile cache_temp_source;
};

TEST_F(ApplyPatchTest, CheckPartition) {
  ASSERT_TRUE(CheckPartition(source_partition));
}

TEST_F(ApplyPatchTest, CheckPartition_Mismatching) {
  ASSERT_FALSE(CheckPartition(Partition(source_file, target_size, target_sha1)));
  ASSERT_FALSE(CheckPartition(Partition(source_file, source_size, bad_sha1_a)));

  ASSERT_FALSE(CheckPartition(Partition(source_file, source_size - 1, source_sha1)));
  ASSERT_FALSE(CheckPartition(Partition(source_file, source_size + 1, source_sha1)));
}

TEST_F(ApplyPatchTest, PatchPartitionCheck) {
  ASSERT_TRUE(PatchPartitionCheck(target_partition, source_partition));

  ASSERT_TRUE(
      PatchPartitionCheck(Partition(source_file, source_size - 1, source_sha1), source_partition));

  ASSERT_TRUE(
      PatchPartitionCheck(Partition(source_file, source_size + 1, source_sha1), source_partition));
}

TEST_F(ApplyPatchTest, PatchPartitionCheck_UseBackup) {
  ASSERT_FALSE(
      PatchPartitionCheck(target_partition, Partition(target_file, source_size, source_sha1)));

  Paths::Get().set_cache_temp_source(source_file);
  ASSERT_TRUE(
      PatchPartitionCheck(target_partition, Partition(target_file, source_size, source_sha1)));
}

TEST_F(ApplyPatchTest, PatchPartitionCheck_UseBackup_BothCorrupted) {
  ASSERT_FALSE(
      PatchPartitionCheck(target_partition, Partition(target_file, source_size, source_sha1)));

  Paths::Get().set_cache_temp_source(target_file);
  ASSERT_FALSE(
      PatchPartitionCheck(target_partition, Partition(target_file, source_size, source_sha1)));
}

TEST_F(ApplyPatchTest, PatchPartition) {
  FileContents patch_fc;
  ASSERT_TRUE(LoadFileContents(from_testdata_base("recovery-from-boot.p"), &patch_fc));
  Value patch(Value::Type::BLOB, std::string(patch_fc.data.cbegin(), patch_fc.data.cend()));

  FileContents bonus_fc;
  ASSERT_TRUE(LoadFileContents(from_testdata_base("bonus.file"), &bonus_fc));
  Value bonus(Value::Type::BLOB, std::string(bonus_fc.data.cbegin(), bonus_fc.data.cend()));

  ASSERT_TRUE(PatchPartition(target_partition, source_partition, patch, &bonus));
}

// Tests patching an eMMC target without a separate bonus file (i.e. recovery-from-boot patch has
// everything).
TEST_F(ApplyPatchTest, PatchPartitionWithoutBonusFile) {
  FileContents patch_fc;
  ASSERT_TRUE(LoadFileContents(from_testdata_base("recovery-from-boot-with-bonus.p"), &patch_fc));
  Value patch(Value::Type::BLOB, std::string(patch_fc.data.cbegin(), patch_fc.data.cend()));

  ASSERT_TRUE(PatchPartition(target_partition, source_partition, patch, nullptr));
}

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;
  }

  void AddFilesToDir(const std::string& dir, const std::vector<std::string>& files) {
    std::string zeros(4096, 0);
    for (const auto& file : files) {
      temporary_files_.push_back(dir + "/" + file);
      ASSERT_TRUE(android::base::WriteStringToFile(zeros, temporary_files_.back()));
    }
  }

  void SetUp() override {
    Paths::Get().set_cache_log_directory(mock_log_dir.path);
    temporary_files_.clear();
  }

  void TearDown() override {
    for (const auto& file : temporary_files_) {
      ASSERT_TRUE(android::base::RemoveFileIfExists(file));
    }
  }

  // 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.
  static 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;

 private:
  std::vector<std::string> temporary_files_;
};

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, MockFreeSpaceChecker));

  ASSERT_EQ(std::vector<std::string>{ "file3" }, FindFilesInDir(mock_cache.path));
  ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_cache.path));
}

TEST_F(FreeCacheTest, FreeCacheFreeSpaceCheckerError) {
  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_FALSE(
      RemoveFilesInDirectory(4096 * 9, mock_cache.path, [](const std::string&) { return -1; }));
}

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, MockFreeSpaceChecker));

  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, MockFreeSpaceChecker));

  // 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, MockFreeSpaceChecker));

  // 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, MockFreeSpaceChecker));

  // 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));
}