1 //
2 // Copyright (C) 2018 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 "update_engine/payload_generator/boot_img_filesystem.h"
18
19 #include <vector>
20
21 #include <brillo/secure_blob.h>
22 #include <gtest/gtest.h>
23
24 #include "update_engine/common/test_utils.h"
25 #include "update_engine/common/utils.h"
26
27 namespace chromeos_update_engine {
28
29 using std::unique_ptr;
30 using std::vector;
31
32 class BootImgFilesystemTest : public ::testing::Test {
33 protected:
GetBootImg(const brillo::Blob & kernel,const brillo::Blob & ramdisk)34 brillo::Blob GetBootImg(const brillo::Blob& kernel,
35 const brillo::Blob& ramdisk) {
36 brillo::Blob boot_img(16 * 1024);
37 BootImgFilesystem::boot_img_hdr hdr;
38 memcpy(hdr.magic, BOOT_MAGIC, BOOT_MAGIC_SIZE);
39 hdr.kernel_size = kernel.size();
40 hdr.ramdisk_size = ramdisk.size();
41 hdr.page_size = 4096;
42 size_t offset = 0;
43 memcpy(boot_img.data() + offset, &hdr, sizeof(hdr));
44 offset += utils::RoundUp(sizeof(hdr), hdr.page_size);
45 memcpy(boot_img.data() + offset, kernel.data(), kernel.size());
46 offset += utils::RoundUp(kernel.size(), hdr.page_size);
47 memcpy(boot_img.data() + offset, ramdisk.data(), ramdisk.size());
48 return boot_img;
49 }
50
51 test_utils::ScopedTempFile boot_file_;
52 };
53
TEST_F(BootImgFilesystemTest,SimpleTest)54 TEST_F(BootImgFilesystemTest, SimpleTest) {
55 test_utils::WriteFileVector(
56 boot_file_.path(),
57 GetBootImg(brillo::Blob(1234, 'k'), brillo::Blob(5678, 'r')));
58 unique_ptr<BootImgFilesystem> fs =
59 BootImgFilesystem::CreateFromFile(boot_file_.path());
60 EXPECT_NE(nullptr, fs);
61
62 vector<FilesystemInterface::File> files;
63 EXPECT_TRUE(fs->GetFiles(&files));
64 ASSERT_EQ(2u, files.size());
65
66 EXPECT_EQ("<kernel>", files[0].name);
67 EXPECT_EQ(1u, files[0].extents.size());
68 EXPECT_EQ(1u, files[0].extents[0].start_block());
69 EXPECT_EQ(1u, files[0].extents[0].num_blocks());
70 EXPECT_TRUE(files[0].deflates.empty());
71
72 EXPECT_EQ("<ramdisk>", files[1].name);
73 EXPECT_EQ(1u, files[1].extents.size());
74 EXPECT_EQ(2u, files[1].extents[0].start_block());
75 EXPECT_EQ(2u, files[1].extents[0].num_blocks());
76 EXPECT_TRUE(files[1].deflates.empty());
77 }
78
TEST_F(BootImgFilesystemTest,BadImageTest)79 TEST_F(BootImgFilesystemTest, BadImageTest) {
80 brillo::Blob boot_img = GetBootImg({}, {});
81 boot_img[7] = '?';
82 test_utils::WriteFileVector(boot_file_.path(), boot_img);
83 unique_ptr<BootImgFilesystem> fs =
84 BootImgFilesystem::CreateFromFile(boot_file_.path());
85 EXPECT_EQ(nullptr, fs);
86 }
87
TEST_F(BootImgFilesystemTest,GZipRamdiskTest)88 TEST_F(BootImgFilesystemTest, GZipRamdiskTest) {
89 // echo ramdisk | gzip | hexdump -v -e '/1 "0x%02x, "'
90 const brillo::Blob ramdisk = {0x1f, 0x8b, 0x08, 0x00, 0x3a, 0x83, 0x35,
91 0x5b, 0x00, 0x03, 0x2b, 0x4a, 0xcc, 0x4d,
92 0xc9, 0x2c, 0xce, 0xe6, 0x02, 0x00, 0x2e,
93 0xf6, 0x0b, 0x08, 0x08, 0x00, 0x00, 0x00};
94 test_utils::WriteFileVector(boot_file_.path(),
95 GetBootImg(brillo::Blob(5678, 'k'), ramdisk));
96 unique_ptr<BootImgFilesystem> fs =
97 BootImgFilesystem::CreateFromFile(boot_file_.path());
98 EXPECT_NE(nullptr, fs);
99
100 vector<FilesystemInterface::File> files;
101 EXPECT_TRUE(fs->GetFiles(&files));
102 ASSERT_EQ(2u, files.size());
103
104 EXPECT_EQ("<kernel>", files[0].name);
105 EXPECT_EQ(1u, files[0].extents.size());
106 EXPECT_EQ(1u, files[0].extents[0].start_block());
107 EXPECT_EQ(2u, files[0].extents[0].num_blocks());
108 EXPECT_TRUE(files[0].deflates.empty());
109
110 EXPECT_EQ("<ramdisk>", files[1].name);
111 EXPECT_EQ(1u, files[1].extents.size());
112 EXPECT_EQ(3u, files[1].extents[0].start_block());
113 EXPECT_EQ(1u, files[1].extents[0].num_blocks());
114 EXPECT_EQ(1u, files[1].deflates.size());
115 }
116
117 } // namespace chromeos_update_engine
118