1 //
2 // Copyright (C) 2023 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "super_flash_helper.h"
18
19 #include <unistd.h>
20
21 #include <android-base/file.h>
22 #include <android-base/unique_fd.h>
23 #include <gtest/gtest.h>
24 #include <sparse/sparse.h>
25
26 using android::base::unique_fd;
27
OpenTestFile(const std::string & file,int flags)28 unique_fd OpenTestFile(const std::string& file, int flags) {
29 std::string path = "testdata/" + file;
30
31 unique_fd fd(open(path.c_str(), flags));
32 if (fd >= 0) {
33 return fd;
34 }
35
36 path = android::base::GetExecutableDirectory() + "/" + path;
37 return unique_fd{open(path.c_str(), flags)};
38 }
39
40 class TestImageSource final : public ImageSource {
41 public:
ReadFile(const std::string &,std::vector<char> *) const42 bool ReadFile(const std::string&, std::vector<char>*) const override {
43 // Not used here.
44 return false;
45 }
OpenFile(const std::string & name) const46 unique_fd OpenFile(const std::string& name) const override {
47 return OpenTestFile(name, O_RDONLY | O_CLOEXEC);
48 }
49 };
50
TEST(SuperFlashHelper,ImageEquality)51 TEST(SuperFlashHelper, ImageEquality) {
52 auto super_empty_fd = OpenTestFile("super_empty.img", O_RDONLY);
53 ASSERT_GE(super_empty_fd, 0);
54
55 TestImageSource source;
56 SuperFlashHelper helper(source);
57 ASSERT_TRUE(helper.Open(super_empty_fd));
58 ASSERT_TRUE(helper.AddPartition("system_a", "system.img", false));
59
60 auto sparse_file = helper.GetSparseLayout();
61 ASSERT_NE(sparse_file, nullptr);
62
63 TemporaryFile fb_super;
64 ASSERT_GE(fb_super.fd, 0);
65 ASSERT_EQ(sparse_file_write(sparse_file.get(), fb_super.fd, false, false, false), 0);
66
67 auto real_super_fd = OpenTestFile("super.img", O_RDONLY);
68 ASSERT_GE(real_super_fd, 0);
69
70 std::string expected(get_file_size(real_super_fd), '\0');
71 ASSERT_FALSE(expected.empty());
72 ASSERT_TRUE(android::base::ReadFully(real_super_fd, expected.data(), expected.size()));
73
74 std::string actual(get_file_size(fb_super.fd), '\0');
75 ASSERT_FALSE(actual.empty());
76 ASSERT_EQ(lseek(fb_super.fd, 0, SEEK_SET), 0);
77 ASSERT_TRUE(android::base::ReadFully(fb_super.fd, actual.data(), actual.size()));
78
79 // The helper doesn't add any extra zeroes to the image, whereas lpmake does, to
80 // pad to the entire super size.
81 ASSERT_LE(actual.size(), expected.size());
82 for (size_t i = 0; i < actual.size(); i++) {
83 ASSERT_EQ(actual[i], expected[i]) << "byte mismatch at position " << i;
84 }
85 for (size_t i = actual.size(); i < expected.size(); i++) {
86 ASSERT_EQ(expected[i], 0) << "byte mismatch at position " << i;
87 }
88 }
89