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 <linux/fs.h>
18 #include <mntent.h>
19
20 #include <algorithm>
21 #include <iterator>
22 #include <set>
23 #include <string>
24 #include <utility>
25 #include <vector>
26
27 #include <android-base/file.h>
28 #include <android-base/properties.h>
29 #include <android-base/strings.h>
30 #include <fs_mgr.h>
31 #include <fstab/fstab.h>
32 #include <gmock/gmock.h>
33 #include <gtest/gtest.h>
34
35 #include "../fs_mgr_priv.h"
36
37 using namespace android::fs_mgr;
38 using namespace testing;
39
40 namespace {
41
42 const std::string cmdline =
43 "rcupdate.rcu_expedited=1 rootwait ro "
44 "init=/init androidboot.bootdevice=1d84000.ufshc "
45 "androidboot.baseband=sdy androidboot.keymaster=1 skip_initramfs "
46 "androidboot.serialno=BLAHBLAHBLAH androidboot.slot_suffix=_a "
47 "androidboot.hardware.platform=sdw813 androidboot.hardware=foo "
48 "androidboot.revision=EVT1.0 androidboot.bootloader=burp-0.1-7521 "
49 "androidboot.hardware.sku=mary androidboot.hardware.radio.subtype=0 "
50 "androidboot.dtbo_idx=2 androidboot.mode=normal "
51 "androidboot.hardware.ddr=1GB,combuchi,LPDDR4X "
52 "androidboot.ddr_info=combuchiandroidboot.ddr_size=2GB "
53 "androidboot.hardware.ufs=2GB,combushi "
54 "androidboot.boottime=0BLE:58,1BLL:22,1BLE:571,2BLL:105,ODT:0,AVB:123 "
55 "androidboot.ramdump=disabled "
56 "dm=\"1 vroot none ro 1,0 10416 verity 1 624684 fec_start 624684\" "
57 "root=/dev/dm-0 "
58 "androidboot.vbmeta.device=PARTUUID=aa08f1a4-c7c9-402e-9a66-9707cafa9ceb "
59 "androidboot.vbmeta.avb_version=\"1.1\" "
60 "androidboot.vbmeta.device_state=unlocked "
61 "androidboot.vbmeta.hash_alg=sha256 androidboot.vbmeta.size=5248 "
62 "androidboot.vbmeta.digest="
63 "ac13147e959861c20f2a6da97d25fe79e60e902c022a371c5c039d31e7c68860 "
64 "androidboot.vbmeta.invalidate_on_error=yes "
65 "androidboot.veritymode=enforcing androidboot.verifiedbootstate=orange "
66 "androidboot.space=\"sha256 5248 androidboot.nospace=nope\" "
67 "printk.devkmsg=on msm_rtb.filter=0x237 ehci-hcd.park=3 "
68 "\"string =\"\"string '\" "
69 "service_locator.enable=1 firmware_class.path=/vendor/firmware "
70 "cgroup.memory=nokmem lpm_levels.sleep_disabled=1 "
71 "buildvariant=userdebug console=null "
72 "terminator=\"truncated";
73
74 const std::vector<std::pair<std::string, std::string>> result_space = {
75 {"rcupdate.rcu_expedited", "1"},
76 {"rootwait", ""},
77 {"ro", ""},
78 {"init", "/init"},
79 {"androidboot.bootdevice", "1d84000.ufshc"},
80 {"androidboot.baseband", "sdy"},
81 {"androidboot.keymaster", "1"},
82 {"skip_initramfs", ""},
83 {"androidboot.serialno", "BLAHBLAHBLAH"},
84 {"androidboot.slot_suffix", "_a"},
85 {"androidboot.hardware.platform", "sdw813"},
86 {"androidboot.hardware", "foo"},
87 {"androidboot.revision", "EVT1.0"},
88 {"androidboot.bootloader", "burp-0.1-7521"},
89 {"androidboot.hardware.sku", "mary"},
90 {"androidboot.hardware.radio.subtype", "0"},
91 {"androidboot.dtbo_idx", "2"},
92 {"androidboot.mode", "normal"},
93 {"androidboot.hardware.ddr", "1GB,combuchi,LPDDR4X"},
94 {"androidboot.ddr_info", "combuchiandroidboot.ddr_size=2GB"},
95 {"androidboot.hardware.ufs", "2GB,combushi"},
96 {"androidboot.boottime", "0BLE:58,1BLL:22,1BLE:571,2BLL:105,ODT:0,AVB:123"},
97 {"androidboot.ramdump", "disabled"},
98 {"dm", "1 vroot none ro 1,0 10416 verity 1 624684 fec_start 624684"},
99 {"root", "/dev/dm-0"},
100 {"androidboot.vbmeta.device", "PARTUUID=aa08f1a4-c7c9-402e-9a66-9707cafa9ceb"},
101 {"androidboot.vbmeta.avb_version", "1.1"},
102 {"androidboot.vbmeta.device_state", "unlocked"},
103 {"androidboot.vbmeta.hash_alg", "sha256"},
104 {"androidboot.vbmeta.size", "5248"},
105 {"androidboot.vbmeta.digest",
106 "ac13147e959861c20f2a6da97d25fe79e60e902c022a371c5c039d31e7c68860"},
107 {"androidboot.vbmeta.invalidate_on_error", "yes"},
108 {"androidboot.veritymode", "enforcing"},
109 {"androidboot.verifiedbootstate", "orange"},
110 {"androidboot.space", "sha256 5248 androidboot.nospace=nope"},
111 {"printk.devkmsg", "on"},
112 {"msm_rtb.filter", "0x237"},
113 {"ehci-hcd.park", "3"},
114 {"string ", "string '"},
115 {"service_locator.enable", "1"},
116 {"firmware_class.path", "/vendor/firmware"},
117 {"cgroup.memory", "nokmem"},
118 {"lpm_levels.sleep_disabled", "1"},
119 {"buildvariant", "userdebug"},
120 {"console", "null"},
121 {"terminator", "truncated"},
122 };
123
124 const std::string bootconfig = R"(
125 androidboot.bootdevice = "1d84000.ufshc"
126 androidboot.boot_devices = "dev1", "dev2,withcomma", "dev3"
127 androidboot.baseband = "sdy"
128 androidboot.keymaster = "1"
129 androidboot.serialno = "BLAHBLAHBLAH"
130 androidboot.slot_suffix = "_a"
131 androidboot.hardware.platform = "sdw813"
132 androidboot.hardware = "foo"
133 androidboot.revision = "EVT1.0"
134 androidboot.bootloader = "burp-0.1-7521"
135 androidboot.hardware.sku = "mary"
136 androidboot.hardware.radio.subtype = "0"
137 androidboot.dtbo_idx = "2"
138 androidboot.mode = "normal"
139 androidboot.hardware.ddr = "1GB,combuchi,LPDDR4X"
140 androidboot.ddr_info = "combuchiandroidboot.ddr_size=2GB"
141 androidboot.hardware.ufs = "2GB,combushi"
142 androidboot.boottime = "0BLE:58,1BLL:22,1BLE:571,2BLL:105,ODT:0,AVB:123"
143 androidboot.ramdump = "disabled"
144 androidboot.vbmeta.device = "PARTUUID=aa08f1a4-c7c9-402e-9a66-9707cafa9ceb"
145 androidboot.vbmeta.avb_version = "1.1"
146 androidboot.vbmeta.device_state = "unlocked"
147 androidboot.vbmeta.hash_alg = "sha256"
148 androidboot.vbmeta.size = "5248"
149 androidboot.vbmeta.digest = "ac13147e959861c20f2a6da97d25fe79e60e902c022a371c5c039d31e7c68860"
150 androidboot.vbmeta.invalidate_on_error = "yes"
151 androidboot.veritymode = "enforcing"
152 androidboot.verifiedbootstate = "orange"
153 androidboot.space = "sha256 5248 androidboot.nospace = nope"
154 just.key
155 key.empty.value =
156 dessert.value = "ice, cream"
157 dessert.list = "ice", "cream"
158 ambiguous.list = ", ", ", "
159 )";
160
161 const std::vector<std::pair<std::string, std::string>> bootconfig_result_space = {
162 {"androidboot.bootdevice", "1d84000.ufshc"},
163 {"androidboot.boot_devices", "dev1, dev2,withcomma, dev3"},
164 {"androidboot.baseband", "sdy"},
165 {"androidboot.keymaster", "1"},
166 {"androidboot.serialno", "BLAHBLAHBLAH"},
167 {"androidboot.slot_suffix", "_a"},
168 {"androidboot.hardware.platform", "sdw813"},
169 {"androidboot.hardware", "foo"},
170 {"androidboot.revision", "EVT1.0"},
171 {"androidboot.bootloader", "burp-0.1-7521"},
172 {"androidboot.hardware.sku", "mary"},
173 {"androidboot.hardware.radio.subtype", "0"},
174 {"androidboot.dtbo_idx", "2"},
175 {"androidboot.mode", "normal"},
176 {"androidboot.hardware.ddr", "1GB,combuchi,LPDDR4X"},
177 {"androidboot.ddr_info", "combuchiandroidboot.ddr_size=2GB"},
178 {"androidboot.hardware.ufs", "2GB,combushi"},
179 {"androidboot.boottime", "0BLE:58,1BLL:22,1BLE:571,2BLL:105,ODT:0,AVB:123"},
180 {"androidboot.ramdump", "disabled"},
181 {"androidboot.vbmeta.device", "PARTUUID=aa08f1a4-c7c9-402e-9a66-9707cafa9ceb"},
182 {"androidboot.vbmeta.avb_version", "1.1"},
183 {"androidboot.vbmeta.device_state", "unlocked"},
184 {"androidboot.vbmeta.hash_alg", "sha256"},
185 {"androidboot.vbmeta.size", "5248"},
186 {"androidboot.vbmeta.digest",
187 "ac13147e959861c20f2a6da97d25fe79e60e902c022a371c5c039d31e7c68860"},
188 {"androidboot.vbmeta.invalidate_on_error", "yes"},
189 {"androidboot.veritymode", "enforcing"},
190 {"androidboot.verifiedbootstate", "orange"},
191 {"androidboot.space", "sha256 5248 androidboot.nospace = nope"},
192 {"just.key", ""},
193 {"key.empty.value", ""},
194 {"dessert.value", "ice, cream"},
195 {"dessert.list", "ice,cream"},
196 {"ambiguous.list", ", ,, "},
197 };
198
CompareFlags(FstabEntry::FsMgrFlags & lhs,FstabEntry::FsMgrFlags & rhs)199 bool CompareFlags(FstabEntry::FsMgrFlags& lhs, FstabEntry::FsMgrFlags& rhs) {
200 // clang-format off
201 return lhs.wait == rhs.wait &&
202 lhs.check == rhs.check &&
203 lhs.crypt == rhs.crypt &&
204 lhs.nonremovable == rhs.nonremovable &&
205 lhs.vold_managed == rhs.vold_managed &&
206 lhs.recovery_only == rhs.recovery_only &&
207 lhs.no_emulated_sd == rhs.no_emulated_sd &&
208 lhs.no_trim == rhs.no_trim &&
209 lhs.file_encryption == rhs.file_encryption &&
210 lhs.formattable == rhs.formattable &&
211 lhs.slot_select == rhs.slot_select &&
212 lhs.late_mount == rhs.late_mount &&
213 lhs.no_fail == rhs.no_fail &&
214 lhs.quota == rhs.quota &&
215 lhs.avb == rhs.avb &&
216 lhs.logical == rhs.logical &&
217 lhs.checkpoint_blk == rhs.checkpoint_blk &&
218 lhs.checkpoint_fs == rhs.checkpoint_fs &&
219 lhs.first_stage_mount == rhs.first_stage_mount &&
220 lhs.slot_select_other == rhs.slot_select_other &&
221 lhs.fs_verity == rhs.fs_verity;
222 // clang-format on
223 }
224
225 } // namespace
226
TEST(fs_mgr,ImportKernelCmdline)227 TEST(fs_mgr, ImportKernelCmdline) {
228 std::vector<std::pair<std::string, std::string>> result;
229 ImportKernelCmdlineFromString(
230 cmdline, [&](std::string key, std::string value) { result.emplace_back(key, value); });
231 EXPECT_THAT(result, ContainerEq(result_space));
232 }
233
TEST(fs_mgr,GetKernelCmdline)234 TEST(fs_mgr, GetKernelCmdline) {
235 std::string content;
236 for (const auto& [key, value] : result_space) {
237 EXPECT_TRUE(GetKernelCmdlineFromString(cmdline, key, &content)) << " for " << key;
238 EXPECT_EQ(content, value);
239 }
240
241 const std::string kUnmodifiedToken = "<UNMODIFIED>";
242 content = kUnmodifiedToken;
243 EXPECT_FALSE(GetKernelCmdlineFromString(cmdline, "", &content));
244 EXPECT_EQ(content, kUnmodifiedToken) << "output parameter shouldn't be overridden";
245
246 content = kUnmodifiedToken;
247 EXPECT_FALSE(GetKernelCmdlineFromString(cmdline, "androidboot.vbmeta.avb_versio", &content));
248 EXPECT_EQ(content, kUnmodifiedToken) << "output parameter shouldn't be overridden";
249
250 content = kUnmodifiedToken;
251 EXPECT_FALSE(GetKernelCmdlineFromString(bootconfig, "androidboot.nospace", &content));
252 EXPECT_EQ(content, kUnmodifiedToken) << "output parameter shouldn't be overridden";
253 }
254
TEST(fs_mgr,ImportBootconfig)255 TEST(fs_mgr, ImportBootconfig) {
256 std::vector<std::pair<std::string, std::string>> result;
257 ImportBootconfigFromString(bootconfig, [&](std::string key, std::string value) {
258 result.emplace_back(key, value);
259 });
260 EXPECT_THAT(result, ContainerEq(bootconfig_result_space));
261 }
262
TEST(fs_mgr,GetBootconfig)263 TEST(fs_mgr, GetBootconfig) {
264 std::string content;
265 for (const auto& [key, value] : bootconfig_result_space) {
266 EXPECT_TRUE(GetBootconfigFromString(bootconfig, key, &content)) << " for " << key;
267 EXPECT_EQ(content, value);
268 }
269
270 const std::string kUnmodifiedToken = "<UNMODIFIED>";
271 content = kUnmodifiedToken;
272 EXPECT_FALSE(GetBootconfigFromString(bootconfig, "", &content));
273 EXPECT_EQ(content, kUnmodifiedToken) << "output parameter shouldn't be overridden";
274
275 content = kUnmodifiedToken;
276 EXPECT_FALSE(GetBootconfigFromString(bootconfig, "androidboot.vbmeta.avb_versio", &content));
277 EXPECT_EQ(content, kUnmodifiedToken) << "output parameter shouldn't be overridden";
278
279 content = kUnmodifiedToken;
280 EXPECT_FALSE(GetBootconfigFromString(bootconfig, "androidboot.nospace", &content));
281 EXPECT_EQ(content, kUnmodifiedToken) << "output parameter shouldn't be overridden";
282 }
283
TEST(fs_mgr,fs_mgr_read_fstab_file_proc_mounts)284 TEST(fs_mgr, fs_mgr_read_fstab_file_proc_mounts) {
285 Fstab fstab;
286 ASSERT_TRUE(ReadFstabFromFile("/proc/mounts", &fstab));
287
288 std::unique_ptr<std::FILE, int (*)(std::FILE*)> mounts(setmntent("/proc/mounts", "re"),
289 endmntent);
290 ASSERT_NE(mounts, nullptr);
291
292 mntent* mentry;
293 size_t i = 0;
294 while ((mentry = getmntent(mounts.get())) != nullptr) {
295 ASSERT_LT(i, fstab.size());
296 auto& entry = fstab[i];
297
298 EXPECT_EQ(mentry->mnt_fsname, entry.blk_device);
299 EXPECT_EQ(mentry->mnt_dir, entry.mount_point);
300 EXPECT_EQ(mentry->mnt_type, entry.fs_type);
301
302 std::set<std::string> mnt_opts;
303 for (auto& s : android::base::Split(mentry->mnt_opts, ",")) {
304 mnt_opts.emplace(s);
305 }
306 std::set<std::string> fs_options;
307 if (!entry.fs_options.empty()) {
308 for (auto& s : android::base::Split(entry.fs_options, ",")) {
309 fs_options.emplace(s);
310 }
311 }
312 // matches private content in fs_mgr_fstab.c
313 static struct flag_list {
314 const char* name;
315 unsigned int flag;
316 } mount_flags[] = {
317 {"noatime", MS_NOATIME},
318 {"noexec", MS_NOEXEC},
319 {"nosuid", MS_NOSUID},
320 {"nodev", MS_NODEV},
321 {"nodiratime", MS_NODIRATIME},
322 {"ro", MS_RDONLY},
323 {"rw", 0},
324 {"sync", MS_SYNCHRONOUS},
325 {"remount", MS_REMOUNT},
326 {"bind", MS_BIND},
327 {"rec", MS_REC},
328 {"unbindable", MS_UNBINDABLE},
329 {"private", MS_PRIVATE},
330 {"slave", MS_SLAVE},
331 {"shared", MS_SHARED},
332 {"defaults", 0},
333 {0, 0},
334 };
335 for (auto f = 0; mount_flags[f].name; ++f) {
336 if (mount_flags[f].flag & entry.flags) {
337 fs_options.emplace(mount_flags[f].name);
338 }
339 }
340 if (!(entry.flags & MS_RDONLY)) {
341 fs_options.emplace("rw");
342 }
343 EXPECT_EQ(mnt_opts, fs_options) << "At line " << i;
344 ++i;
345 }
346 EXPECT_EQ(i, fstab.size());
347 }
348
TEST(fs_mgr,ReadFstabFromFile_MountOptions)349 TEST(fs_mgr, ReadFstabFromFile_MountOptions) {
350 TemporaryFile tf;
351 ASSERT_TRUE(tf.fd != -1);
352 std::string fstab_contents = R"fs(
353 source / ext4 ro,barrier=1 wait,avb
354 source /metadata ext4 noatime,nosuid,nodev,discard wait,formattable
355
356 source /data f2fs noatime,nosuid,nodev,discard,reserve_root=32768,resgid=1065,fsync_mode=nobarrier latemount,wait,check,fileencryption=ice,keydirectory=/metadata/vold/metadata_encryption,quota,formattable,sysfs_path=/sys/devices/platform/soc/1d84000.ufshc,reservedsize=128M
357
358 source /misc emmc defaults defaults
359
360 source /vendor/firmware_mnt vfat ro,shortname=lower,uid=1000,gid=1000,dmask=227,fmask=337,context=u:object_r:firmware_file:s0 wait
361
362 source auto vfat defaults voldmanaged=usb:auto
363 source none swap defaults zramsize=1073741824,max_comp_streams=8
364 source none2 swap nodiratime,remount,bind zramsize=1073741824,max_comp_streams=8
365 source none3 swap unbindable,private,slave zramsize=1073741824,max_comp_streams=8
366 source none4 swap noexec,shared,rec zramsize=1073741824,max_comp_streams=8
367 source none5 swap rw zramsize=1073741824,max_comp_streams=8
368 )fs";
369 ASSERT_TRUE(android::base::WriteStringToFile(fstab_contents, tf.path));
370
371 Fstab fstab;
372 EXPECT_TRUE(ReadFstabFromFile(tf.path, &fstab));
373 ASSERT_LE(11U, fstab.size());
374
375 FstabEntry* entry = GetEntryForMountPoint(&fstab, "/");
376 ASSERT_NE(nullptr, entry);
377 EXPECT_EQ(static_cast<unsigned long>(MS_RDONLY), entry->flags);
378 EXPECT_EQ("barrier=1", entry->fs_options);
379
380 entry = GetEntryForMountPoint(&fstab, "/metadata");
381 ASSERT_NE(nullptr, entry);
382 EXPECT_EQ(static_cast<unsigned long>(MS_NOATIME | MS_NOSUID | MS_NODEV), entry->flags);
383 EXPECT_EQ("discard", entry->fs_options);
384
385 entry = GetEntryForMountPoint(&fstab, "/data");
386 ASSERT_NE(nullptr, entry);
387 EXPECT_EQ(static_cast<unsigned long>(MS_NOATIME | MS_NOSUID | MS_NODEV), entry->flags);
388 EXPECT_EQ("discard,reserve_root=32768,resgid=1065,fsync_mode=nobarrier", entry->fs_options);
389
390 entry = GetEntryForMountPoint(&fstab, "/misc");
391 ASSERT_NE(nullptr, entry);
392 EXPECT_EQ(0U, entry->flags);
393 EXPECT_EQ("", entry->fs_options);
394
395 entry = GetEntryForMountPoint(&fstab, "/vendor/firmware_mnt");
396 ASSERT_NE(nullptr, entry);
397 EXPECT_EQ(static_cast<unsigned long>(MS_RDONLY), entry->flags);
398 EXPECT_EQ(
399 "shortname=lower,uid=1000,gid=1000,dmask=227,fmask=337,"
400 "context=u:object_r:firmware_file:s0",
401 entry->fs_options);
402
403 entry = GetEntryForMountPoint(&fstab, "auto");
404 ASSERT_NE(nullptr, entry);
405 EXPECT_EQ(0U, entry->flags);
406 EXPECT_EQ("", entry->fs_options);
407
408 entry = GetEntryForMountPoint(&fstab, "none");
409 ASSERT_NE(nullptr, entry);
410 EXPECT_EQ(0U, entry->flags);
411 EXPECT_EQ("", entry->fs_options);
412
413 entry = GetEntryForMountPoint(&fstab, "none2");
414 ASSERT_NE(nullptr, entry);
415 EXPECT_EQ(static_cast<unsigned long>(MS_NODIRATIME | MS_REMOUNT | MS_BIND), entry->flags);
416 EXPECT_EQ("", entry->fs_options);
417
418 entry = GetEntryForMountPoint(&fstab, "none3");
419 ASSERT_NE(nullptr, entry);
420 EXPECT_EQ(static_cast<unsigned long>(MS_UNBINDABLE | MS_PRIVATE | MS_SLAVE), entry->flags);
421 EXPECT_EQ("", entry->fs_options);
422
423 entry = GetEntryForMountPoint(&fstab, "none4");
424 ASSERT_NE(nullptr, entry);
425 EXPECT_EQ(static_cast<unsigned long>(MS_NOEXEC | MS_SHARED | MS_REC), entry->flags);
426 EXPECT_EQ("", entry->fs_options);
427
428 entry = GetEntryForMountPoint(&fstab, "none5");
429 ASSERT_NE(nullptr, entry);
430 // rw is the default.
431 EXPECT_EQ(0U, entry->flags);
432 EXPECT_EQ("", entry->fs_options);
433 }
434
TEST(fs_mgr,ReadFstabFromFile_FsMgrFlags)435 TEST(fs_mgr, ReadFstabFromFile_FsMgrFlags) {
436 TemporaryFile tf;
437 ASSERT_TRUE(tf.fd != -1);
438 std::string fstab_contents = R"fs(
439 source none0 swap defaults wait,check,nonremovable,recoveryonly
440 source none1 swap defaults avb,noemulatedsd,notrim,formattable,nofail
441 source none2 swap defaults first_stage_mount,latemount,quota,logical
442 source none3 swap defaults checkpoint=block
443 source none4 swap defaults checkpoint=fs
444 source none5 swap defaults defaults
445 )fs";
446 ASSERT_TRUE(android::base::WriteStringToFile(fstab_contents, tf.path));
447
448 Fstab fstab;
449 EXPECT_TRUE(ReadFstabFromFile(tf.path, &fstab));
450 ASSERT_LE(6U, fstab.size());
451
452 FstabEntry* entry = GetEntryForMountPoint(&fstab, "none0");
453 ASSERT_NE(nullptr, entry);
454 {
455 FstabEntry::FsMgrFlags flags = {};
456 flags.wait = true;
457 flags.check = true;
458 flags.nonremovable = true;
459 flags.recovery_only = true;
460 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
461 }
462
463 entry = GetEntryForMountPoint(&fstab, "none1");
464 ASSERT_NE(nullptr, entry);
465 {
466 FstabEntry::FsMgrFlags flags = {};
467 flags.avb = true;
468 flags.no_emulated_sd = true;
469 flags.no_trim = true;
470 flags.formattable = true;
471 flags.no_fail = true;
472 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
473 }
474
475 entry = GetEntryForMountPoint(&fstab, "none2");
476 ASSERT_NE(nullptr, entry);
477 {
478 FstabEntry::FsMgrFlags flags = {};
479 flags.first_stage_mount = true;
480 flags.late_mount = true;
481 flags.quota = true;
482 flags.logical = true;
483 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
484 }
485
486 entry = GetEntryForMountPoint(&fstab, "none3");
487 ASSERT_NE(nullptr, entry);
488 {
489 FstabEntry::FsMgrFlags flags = {};
490 flags.checkpoint_blk = true;
491 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
492 }
493
494 entry = GetEntryForMountPoint(&fstab, "none4");
495 ASSERT_NE(nullptr, entry);
496 {
497 FstabEntry::FsMgrFlags flags = {};
498 flags.checkpoint_fs = true;
499 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
500 }
501
502 entry = GetEntryForMountPoint(&fstab, "none5");
503 ASSERT_NE(nullptr, entry);
504 {
505 FstabEntry::FsMgrFlags flags = {};
506 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
507 }
508 }
509
TEST(fs_mgr,ReadFstabFromFile_FsMgrOptions_AllBad)510 TEST(fs_mgr, ReadFstabFromFile_FsMgrOptions_AllBad) {
511 TemporaryFile tf;
512 ASSERT_TRUE(tf.fd != -1);
513 std::string fstab_contents = R"fs(
514 source none0 swap defaults fileencryption,keydirectory,length,swapprio,zramsize,max_comp_streams,reservedsize,eraseblk,logicalblk,sysfs_path,zram_backingdev_size
515
516 source none1 swap defaults fileencryption=,keydirectory=,length=,swapprio=,zramsize=,max_comp_streams=,avb=,reservedsize=,eraseblk=,logicalblk=,sysfs_path=,zram_backingdev_size=
517
518 )fs";
519 ASSERT_TRUE(android::base::WriteStringToFile(fstab_contents, tf.path));
520
521 Fstab fstab;
522 EXPECT_TRUE(ReadFstabFromFile(tf.path, &fstab));
523 ASSERT_LE(2U, fstab.size());
524
525 auto entry = fstab.begin();
526 EXPECT_EQ("none0", entry->mount_point);
527 {
528 FstabEntry::FsMgrFlags flags = {};
529 flags.file_encryption = true;
530 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
531 }
532 EXPECT_EQ("", entry->metadata_key_dir);
533 EXPECT_EQ(0, entry->length);
534 EXPECT_EQ("", entry->label);
535 EXPECT_EQ(-1, entry->partnum);
536 EXPECT_EQ(-1, entry->swap_prio);
537 EXPECT_EQ(0, entry->max_comp_streams);
538 EXPECT_EQ(0, entry->zram_size);
539 EXPECT_EQ(0, entry->reserved_size);
540 EXPECT_EQ("", entry->encryption_options);
541 EXPECT_EQ(0, entry->erase_blk_size);
542 EXPECT_EQ(0, entry->logical_blk_size);
543 EXPECT_EQ("", entry->sysfs_path);
544 EXPECT_EQ(0U, entry->zram_backingdev_size);
545 entry++;
546
547 EXPECT_EQ("none1", entry->mount_point);
548 {
549 FstabEntry::FsMgrFlags flags = {};
550 flags.file_encryption = true;
551 flags.avb = true;
552 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
553 }
554 EXPECT_EQ("", entry->metadata_key_dir);
555 EXPECT_EQ(0, entry->length);
556 EXPECT_EQ("", entry->label);
557 EXPECT_EQ(-1, entry->partnum);
558 EXPECT_EQ(-1, entry->swap_prio);
559 EXPECT_EQ(0, entry->max_comp_streams);
560 EXPECT_EQ(0, entry->zram_size);
561 EXPECT_EQ(0, entry->reserved_size);
562 EXPECT_EQ("", entry->encryption_options);
563 EXPECT_EQ(0, entry->erase_blk_size);
564 EXPECT_EQ(0, entry->logical_blk_size);
565 EXPECT_EQ("", entry->sysfs_path);
566 EXPECT_EQ(0U, entry->zram_backingdev_size);
567 }
568
569 // FDE is no longer supported, so an fstab with FDE enabled should be rejected.
TEST(fs_mgr,ReadFstabFromFile_FsMgrOptions_FDE)570 TEST(fs_mgr, ReadFstabFromFile_FsMgrOptions_FDE) {
571 TemporaryFile tf;
572 ASSERT_TRUE(tf.fd != -1);
573 std::string fstab_contents = R"fs(
574 source /data ext4 noatime forceencrypt=footer
575 )fs";
576 ASSERT_TRUE(android::base::WriteStringToFile(fstab_contents, tf.path));
577
578 Fstab fstab;
579 EXPECT_FALSE(ReadFstabFromFile(tf.path, &fstab));
580 }
581
TEST(fs_mgr,ReadFstabFromFile_FsMgrOptions_AdoptableStorage)582 TEST(fs_mgr, ReadFstabFromFile_FsMgrOptions_AdoptableStorage) {
583 TemporaryFile tf;
584 ASSERT_TRUE(tf.fd != -1);
585 std::string fstab_contents = R"fs(
586 source none0 swap defaults encryptable=userdata,voldmanaged=sdcard:auto
587 )fs";
588 ASSERT_TRUE(android::base::WriteStringToFile(fstab_contents, tf.path));
589
590 Fstab fstab;
591 EXPECT_TRUE(ReadFstabFromFile(tf.path, &fstab));
592 ASSERT_LE(1U, fstab.size());
593
594 FstabEntry::FsMgrFlags flags = {};
595 flags.crypt = true;
596 flags.vold_managed = true;
597
598 auto entry = fstab.begin();
599 EXPECT_EQ("none0", entry->mount_point);
600 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
601 }
602
TEST(fs_mgr,ReadFstabFromFile_FsMgrOptions_VoldManaged)603 TEST(fs_mgr, ReadFstabFromFile_FsMgrOptions_VoldManaged) {
604 TemporaryFile tf;
605 ASSERT_TRUE(tf.fd != -1);
606 std::string fstab_contents = R"fs(
607 source none0 swap defaults voldmanaged=:
608 source none1 swap defaults voldmanaged=sdcard
609 source none2 swap defaults voldmanaged=sdcard:3
610 source none3 swap defaults voldmanaged=sdcard:auto
611 )fs";
612 ASSERT_TRUE(android::base::WriteStringToFile(fstab_contents, tf.path));
613
614 Fstab fstab;
615 EXPECT_TRUE(ReadFstabFromFile(tf.path, &fstab));
616 ASSERT_LE(4U, fstab.size());
617
618 FstabEntry::FsMgrFlags flags = {};
619 flags.vold_managed = true;
620
621 auto entry = fstab.begin();
622 EXPECT_EQ("none0", entry->mount_point);
623 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
624 EXPECT_TRUE(entry->label.empty());
625 EXPECT_EQ(-1, entry->partnum);
626 entry++;
627
628 EXPECT_EQ("none1", entry->mount_point);
629 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
630 EXPECT_TRUE(entry->label.empty());
631 EXPECT_EQ(-1, entry->partnum);
632 entry++;
633
634 EXPECT_EQ("none2", entry->mount_point);
635 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
636 EXPECT_EQ("sdcard", entry->label);
637 EXPECT_EQ(3, entry->partnum);
638 entry++;
639
640 EXPECT_EQ("none3", entry->mount_point);
641 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
642 EXPECT_EQ("sdcard", entry->label);
643 EXPECT_EQ(-1, entry->partnum);
644 }
645
TEST(fs_mgr,ReadFstabFromFile_FsMgrOptions_Length)646 TEST(fs_mgr, ReadFstabFromFile_FsMgrOptions_Length) {
647 TemporaryFile tf;
648 ASSERT_TRUE(tf.fd != -1);
649 std::string fstab_contents = R"fs(
650 source none0 swap defaults length=blah
651 source none1 swap defaults length=123456
652 )fs";
653 ASSERT_TRUE(android::base::WriteStringToFile(fstab_contents, tf.path));
654
655 Fstab fstab;
656 EXPECT_TRUE(ReadFstabFromFile(tf.path, &fstab));
657 ASSERT_LE(2U, fstab.size());
658
659 FstabEntry::FsMgrFlags flags = {};
660
661 auto entry = fstab.begin();
662 EXPECT_EQ("none0", entry->mount_point);
663 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
664 EXPECT_EQ(0, entry->length);
665 entry++;
666
667 EXPECT_EQ("none1", entry->mount_point);
668 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
669 EXPECT_EQ(123456, entry->length);
670 }
671
TEST(fs_mgr,ReadFstabFromFile_FsMgrOptions_Swapprio)672 TEST(fs_mgr, ReadFstabFromFile_FsMgrOptions_Swapprio) {
673 TemporaryFile tf;
674 ASSERT_TRUE(tf.fd != -1);
675 std::string fstab_contents = R"fs(
676 source none0 swap defaults swapprio=blah
677 source none1 swap defaults swapprio=123456
678 )fs";
679 ASSERT_TRUE(android::base::WriteStringToFile(fstab_contents, tf.path));
680
681 Fstab fstab;
682 EXPECT_TRUE(ReadFstabFromFile(tf.path, &fstab));
683 ASSERT_LE(2U, fstab.size());
684
685 FstabEntry::FsMgrFlags flags = {};
686
687 auto entry = fstab.begin();
688 EXPECT_EQ("none0", entry->mount_point);
689 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
690 EXPECT_EQ(-1, entry->swap_prio);
691 entry++;
692
693 EXPECT_EQ("none1", entry->mount_point);
694 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
695 EXPECT_EQ(123456, entry->swap_prio);
696 }
697
TEST(fs_mgr,ReadFstabFromFile_FsMgrOptions_ZramSize)698 TEST(fs_mgr, ReadFstabFromFile_FsMgrOptions_ZramSize) {
699 TemporaryFile tf;
700 ASSERT_TRUE(tf.fd != -1);
701 std::string fstab_contents = R"fs(
702 source none0 swap defaults zramsize=blah
703 source none1 swap defaults zramsize=123456
704 source none2 swap defaults zramsize=blah%
705 source none3 swap defaults zramsize=5%
706 source none4 swap defaults zramsize=105%
707 source none5 swap defaults zramsize=%
708 )fs";
709 ASSERT_TRUE(android::base::WriteStringToFile(fstab_contents, tf.path));
710
711 Fstab fstab;
712 EXPECT_TRUE(ReadFstabFromFile(tf.path, &fstab));
713 ASSERT_LE(6U, fstab.size());
714
715 FstabEntry::FsMgrFlags flags = {};
716
717 auto entry = fstab.begin();
718 EXPECT_EQ("none0", entry->mount_point);
719 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
720 EXPECT_EQ(0, entry->zram_size);
721 entry++;
722
723 EXPECT_EQ("none1", entry->mount_point);
724 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
725 EXPECT_EQ(123456, entry->zram_size);
726 entry++;
727
728 EXPECT_EQ("none2", entry->mount_point);
729 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
730 EXPECT_EQ(0, entry->zram_size);
731 entry++;
732
733 EXPECT_EQ("none3", entry->mount_point);
734 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
735 EXPECT_NE(0, entry->zram_size);
736 entry++;
737
738 EXPECT_EQ("none4", entry->mount_point);
739 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
740 EXPECT_EQ(0, entry->zram_size);
741 entry++;
742
743 EXPECT_EQ("none5", entry->mount_point);
744 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
745 EXPECT_EQ(0, entry->zram_size);
746 }
747
TEST(fs_mgr,ReadFstabFromFile_FsMgrOptions_FileEncryption)748 TEST(fs_mgr, ReadFstabFromFile_FsMgrOptions_FileEncryption) {
749 TemporaryFile tf;
750 ASSERT_TRUE(tf.fd != -1);
751 std::string fstab_contents = R"fs(
752 source none0 swap defaults fileencryption=aes-256-xts:aes-256-cts:v1
753 )fs";
754
755 ASSERT_TRUE(android::base::WriteStringToFile(fstab_contents, tf.path));
756
757 Fstab fstab;
758 EXPECT_TRUE(ReadFstabFromFile(tf.path, &fstab));
759 ASSERT_LE(1U, fstab.size());
760
761 FstabEntry::FsMgrFlags flags = {};
762 flags.file_encryption = true;
763
764 auto entry = fstab.begin();
765 EXPECT_EQ("none0", entry->mount_point);
766 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
767 EXPECT_EQ("aes-256-xts:aes-256-cts:v1", entry->encryption_options);
768 }
769
TEST(fs_mgr,ReadFstabFromFile_FsMgrOptions_MaxCompStreams)770 TEST(fs_mgr, ReadFstabFromFile_FsMgrOptions_MaxCompStreams) {
771 TemporaryFile tf;
772 ASSERT_TRUE(tf.fd != -1);
773 std::string fstab_contents = R"fs(
774 source none0 swap defaults max_comp_streams=blah
775 source none1 swap defaults max_comp_streams=123456
776 )fs";
777 ASSERT_TRUE(android::base::WriteStringToFile(fstab_contents, tf.path));
778
779 Fstab fstab;
780 EXPECT_TRUE(ReadFstabFromFile(tf.path, &fstab));
781 ASSERT_LE(2U, fstab.size());
782
783 FstabEntry::FsMgrFlags flags = {};
784
785 auto entry = fstab.begin();
786 EXPECT_EQ("none0", entry->mount_point);
787 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
788 EXPECT_EQ(0, entry->max_comp_streams);
789 entry++;
790
791 EXPECT_EQ("none1", entry->mount_point);
792 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
793 EXPECT_EQ(123456, entry->max_comp_streams);
794 }
795
TEST(fs_mgr,ReadFstabFromFile_FsMgrOptions_ReservedSize)796 TEST(fs_mgr, ReadFstabFromFile_FsMgrOptions_ReservedSize) {
797 TemporaryFile tf;
798 ASSERT_TRUE(tf.fd != -1);
799 std::string fstab_contents = R"fs(
800 source none0 swap defaults reservedsize=blah
801 source none1 swap defaults reservedsize=2
802 source none2 swap defaults reservedsize=1K
803 source none3 swap defaults reservedsize=2m
804 )fs";
805 ASSERT_TRUE(android::base::WriteStringToFile(fstab_contents, tf.path));
806
807 Fstab fstab;
808 EXPECT_TRUE(ReadFstabFromFile(tf.path, &fstab));
809 ASSERT_LE(4U, fstab.size());
810
811 FstabEntry::FsMgrFlags flags = {};
812
813 auto entry = fstab.begin();
814 EXPECT_EQ("none0", entry->mount_point);
815 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
816 EXPECT_EQ(0, entry->reserved_size);
817 entry++;
818
819 EXPECT_EQ("none1", entry->mount_point);
820 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
821 EXPECT_EQ(2, entry->reserved_size);
822 entry++;
823
824 EXPECT_EQ("none2", entry->mount_point);
825 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
826 EXPECT_EQ(1024, entry->reserved_size);
827 entry++;
828
829 EXPECT_EQ("none3", entry->mount_point);
830 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
831 EXPECT_EQ(2 * 1024 * 1024, entry->reserved_size);
832 }
833
TEST(fs_mgr,ReadFstabFromFile_FsMgrOptions_EraseBlk)834 TEST(fs_mgr, ReadFstabFromFile_FsMgrOptions_EraseBlk) {
835 TemporaryFile tf;
836 ASSERT_TRUE(tf.fd != -1);
837 std::string fstab_contents = R"fs(
838 source none0 swap defaults eraseblk=blah
839 source none1 swap defaults eraseblk=4000
840 source none2 swap defaults eraseblk=5000
841 source none3 swap defaults eraseblk=8192
842 )fs";
843 ASSERT_TRUE(android::base::WriteStringToFile(fstab_contents, tf.path));
844
845 Fstab fstab;
846 EXPECT_TRUE(ReadFstabFromFile(tf.path, &fstab));
847 ASSERT_LE(4U, fstab.size());
848
849 FstabEntry::FsMgrFlags flags = {};
850
851 auto entry = fstab.begin();
852 EXPECT_EQ("none0", entry->mount_point);
853 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
854 EXPECT_EQ(0, entry->erase_blk_size);
855 entry++;
856
857 EXPECT_EQ("none1", entry->mount_point);
858 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
859 EXPECT_EQ(0, entry->erase_blk_size);
860 entry++;
861
862 EXPECT_EQ("none2", entry->mount_point);
863 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
864 EXPECT_EQ(0, entry->erase_blk_size);
865 entry++;
866
867 EXPECT_EQ("none3", entry->mount_point);
868 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
869 EXPECT_EQ(8192, entry->erase_blk_size);
870 }
871
TEST(fs_mgr,ReadFstabFromFile_FsMgrOptions_Logicalblk)872 TEST(fs_mgr, ReadFstabFromFile_FsMgrOptions_Logicalblk) {
873 TemporaryFile tf;
874 ASSERT_TRUE(tf.fd != -1);
875 std::string fstab_contents = R"fs(
876 source none0 swap defaults logicalblk=blah
877 source none1 swap defaults logicalblk=4000
878 source none2 swap defaults logicalblk=5000
879 source none3 swap defaults logicalblk=8192
880 )fs";
881 ASSERT_TRUE(android::base::WriteStringToFile(fstab_contents, tf.path));
882
883 Fstab fstab;
884 EXPECT_TRUE(ReadFstabFromFile(tf.path, &fstab));
885 ASSERT_LE(4U, fstab.size());
886
887 FstabEntry::FsMgrFlags flags = {};
888
889 auto entry = fstab.begin();
890 EXPECT_EQ("none0", entry->mount_point);
891 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
892 EXPECT_EQ(0, entry->logical_blk_size);
893 entry++;
894
895 EXPECT_EQ("none1", entry->mount_point);
896 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
897 EXPECT_EQ(0, entry->logical_blk_size);
898 entry++;
899
900 EXPECT_EQ("none2", entry->mount_point);
901 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
902 EXPECT_EQ(0, entry->logical_blk_size);
903 entry++;
904
905 EXPECT_EQ("none3", entry->mount_point);
906 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
907 EXPECT_EQ(8192, entry->logical_blk_size);
908 }
909
TEST(fs_mgr,ReadFstabFromFile_FsMgrOptions_Avb)910 TEST(fs_mgr, ReadFstabFromFile_FsMgrOptions_Avb) {
911 TemporaryFile tf;
912 ASSERT_TRUE(tf.fd != -1);
913 std::string fstab_contents = R"fs(
914 source none0 swap defaults avb=vbmeta_partition
915 source none1 swap defaults avb_keys=/path/to/test.avbpubkey
916 )fs";
917
918 ASSERT_TRUE(android::base::WriteStringToFile(fstab_contents, tf.path));
919
920 Fstab fstab;
921 EXPECT_TRUE(ReadFstabFromFile(tf.path, &fstab));
922 ASSERT_LE(2U, fstab.size());
923
924 auto entry = fstab.begin();
925 EXPECT_EQ("none0", entry->mount_point);
926
927 FstabEntry::FsMgrFlags flags = {};
928 flags.avb = true;
929 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
930
931 EXPECT_EQ("vbmeta_partition", entry->vbmeta_partition);
932 entry++;
933
934 EXPECT_EQ("none1", entry->mount_point);
935 FstabEntry::FsMgrFlags empty_flags = {}; // no flags should be set for avb_keys.
936 EXPECT_TRUE(CompareFlags(empty_flags, entry->fs_mgr_flags));
937 EXPECT_EQ("/path/to/test.avbpubkey", entry->avb_keys);
938 }
939
TEST(fs_mgr,ReadFstabFromFile_FsMgrOptions_KeyDirectory)940 TEST(fs_mgr, ReadFstabFromFile_FsMgrOptions_KeyDirectory) {
941 TemporaryFile tf;
942 ASSERT_TRUE(tf.fd != -1);
943 std::string fstab_contents = R"fs(
944 source none0 swap defaults keydirectory=/dir/key
945 )fs";
946
947 ASSERT_TRUE(android::base::WriteStringToFile(fstab_contents, tf.path));
948
949 Fstab fstab;
950 EXPECT_TRUE(ReadFstabFromFile(tf.path, &fstab));
951 ASSERT_LE(1U, fstab.size());
952
953 auto entry = fstab.begin();
954 EXPECT_EQ("none0", entry->mount_point);
955
956 FstabEntry::FsMgrFlags flags = {};
957 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
958
959 EXPECT_EQ("/dir/key", entry->metadata_key_dir);
960 }
961
TEST(fs_mgr,ReadFstabFromFile_FsMgrOptions_MetadataEncryption)962 TEST(fs_mgr, ReadFstabFromFile_FsMgrOptions_MetadataEncryption) {
963 TemporaryFile tf;
964 ASSERT_TRUE(tf.fd != -1);
965 std::string fstab_contents = R"fs(
966 source none0 swap defaults keydirectory=/dir/key,metadata_encryption=adiantum
967 )fs";
968
969 ASSERT_TRUE(android::base::WriteStringToFile(fstab_contents, tf.path));
970
971 Fstab fstab;
972 EXPECT_TRUE(ReadFstabFromFile(tf.path, &fstab));
973 ASSERT_LE(1U, fstab.size());
974
975 auto entry = fstab.begin();
976 EXPECT_EQ("adiantum", entry->metadata_encryption_options);
977 }
978
TEST(fs_mgr,ReadFstabFromFile_FsMgrOptions_MetadataEncryption_WrappedKey)979 TEST(fs_mgr, ReadFstabFromFile_FsMgrOptions_MetadataEncryption_WrappedKey) {
980 TemporaryFile tf;
981 ASSERT_TRUE(tf.fd != -1);
982 std::string fstab_contents = R"fs(
983 source none0 swap defaults keydirectory=/dir/key,metadata_encryption=aes-256-xts:wrappedkey_v0
984 )fs";
985
986 ASSERT_TRUE(android::base::WriteStringToFile(fstab_contents, tf.path));
987
988 Fstab fstab;
989 EXPECT_TRUE(ReadFstabFromFile(tf.path, &fstab));
990 ASSERT_LE(1U, fstab.size());
991
992 auto entry = fstab.begin();
993 EXPECT_EQ("aes-256-xts:wrappedkey_v0", entry->metadata_encryption_options);
994 auto parts = android::base::Split(entry->metadata_encryption_options, ":");
995 EXPECT_EQ(2U, parts.size());
996 EXPECT_EQ("aes-256-xts", parts[0]);
997 EXPECT_EQ("wrappedkey_v0", parts[1]);
998 }
999
TEST(fs_mgr,ReadFstabFromFile_FsMgrOptions_SysfsPath)1000 TEST(fs_mgr, ReadFstabFromFile_FsMgrOptions_SysfsPath) {
1001 TemporaryFile tf;
1002 ASSERT_TRUE(tf.fd != -1);
1003 std::string fstab_contents = R"fs(
1004 source none0 swap defaults sysfs_path=/sys/device
1005 )fs";
1006
1007 ASSERT_TRUE(android::base::WriteStringToFile(fstab_contents, tf.path));
1008
1009 Fstab fstab;
1010 EXPECT_TRUE(ReadFstabFromFile(tf.path, &fstab));
1011 ASSERT_LE(1U, fstab.size());
1012
1013 auto entry = fstab.begin();
1014 EXPECT_EQ("none0", entry->mount_point);
1015
1016 FstabEntry::FsMgrFlags flags = {};
1017 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
1018
1019 EXPECT_EQ("/sys/device", entry->sysfs_path);
1020 }
1021
TEST(fs_mgr,ReadFstabFromFile_FsMgrOptions_Zram)1022 TEST(fs_mgr, ReadFstabFromFile_FsMgrOptions_Zram) {
1023 TemporaryFile tf;
1024 ASSERT_TRUE(tf.fd != -1);
1025 std::string fstab_contents = R"fs(
1026 source none1 swap defaults zram_backingdev_size=blah
1027 source none2 swap defaults zram_backingdev_size=2
1028 source none3 swap defaults zram_backingdev_size=1K
1029 source none4 swap defaults zram_backingdev_size=2m
1030
1031 )fs";
1032
1033 ASSERT_TRUE(android::base::WriteStringToFile(fstab_contents, tf.path));
1034
1035 Fstab fstab;
1036 EXPECT_TRUE(ReadFstabFromFile(tf.path, &fstab));
1037 ASSERT_LE(4U, fstab.size());
1038
1039 auto entry = fstab.begin();
1040
1041 EXPECT_EQ("none1", entry->mount_point);
1042 EXPECT_EQ(0U, entry->zram_backingdev_size);
1043 entry++;
1044
1045 EXPECT_EQ("none2", entry->mount_point);
1046 EXPECT_EQ(2U, entry->zram_backingdev_size);
1047 entry++;
1048
1049 EXPECT_EQ("none3", entry->mount_point);
1050 EXPECT_EQ(1024U, entry->zram_backingdev_size);
1051 entry++;
1052
1053 EXPECT_EQ("none4", entry->mount_point);
1054 EXPECT_EQ(2U * 1024U * 1024U, entry->zram_backingdev_size);
1055 entry++;
1056 }
1057
TEST(fs_mgr,DefaultFstabContainsUserdata)1058 TEST(fs_mgr, DefaultFstabContainsUserdata) {
1059 Fstab fstab;
1060 ASSERT_TRUE(ReadDefaultFstab(&fstab)) << "Failed to read default fstab";
1061 ASSERT_NE(nullptr, GetEntryForMountPoint(&fstab, "/data"))
1062 << "Default fstab doesn't contain /data entry";
1063 }
1064
TEST(fs_mgr,UserdataMountedFromDefaultFstab)1065 TEST(fs_mgr, UserdataMountedFromDefaultFstab) {
1066 if (getuid() != 0) {
1067 GTEST_SKIP() << "Must be run as root.";
1068 return;
1069 }
1070 Fstab fstab;
1071 ASSERT_TRUE(ReadDefaultFstab(&fstab)) << "Failed to read default fstab";
1072 Fstab proc_mounts;
1073 ASSERT_TRUE(ReadFstabFromFile("/proc/mounts", &proc_mounts)) << "Failed to read /proc/mounts";
1074 auto mounted_entry = GetEntryForMountPoint(&proc_mounts, "/data");
1075 ASSERT_NE(mounted_entry, nullptr) << "/data is not mounted";
1076 std::string block_device;
1077 ASSERT_TRUE(android::base::Realpath(mounted_entry->blk_device, &block_device));
1078 ASSERT_NE(nullptr, fs_mgr_get_mounted_entry_for_userdata(&fstab, block_device))
1079 << "/data wasn't mounted from default fstab";
1080 }
1081
TEST(fs_mgr,ReadFstabFromFile_FsMgrOptions_Readahead_Size_KB)1082 TEST(fs_mgr, ReadFstabFromFile_FsMgrOptions_Readahead_Size_KB) {
1083 TemporaryFile tf;
1084 ASSERT_TRUE(tf.fd != -1);
1085 std::string fstab_contents = R"fs(
1086 source none0 swap defaults readahead_size_kb=blah
1087 source none1 swap defaults readahead_size_kb=128
1088 source none2 swap defaults readahead_size_kb=5%
1089 source none3 swap defaults readahead_size_kb=5kb
1090 source none4 swap defaults readahead_size_kb=16385
1091 source none5 swap defaults readahead_size_kb=-128
1092 source none6 swap defaults readahead_size_kb=0
1093 )fs";
1094 ASSERT_TRUE(android::base::WriteStringToFile(fstab_contents, tf.path));
1095
1096 Fstab fstab;
1097 EXPECT_TRUE(ReadFstabFromFile(tf.path, &fstab));
1098 ASSERT_LE(7U, fstab.size());
1099
1100 FstabEntry::FsMgrFlags flags = {};
1101
1102 auto entry = fstab.begin();
1103 EXPECT_EQ("none0", entry->mount_point);
1104 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
1105 EXPECT_EQ(-1, entry->readahead_size_kb);
1106 entry++;
1107
1108 EXPECT_EQ("none1", entry->mount_point);
1109 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
1110 EXPECT_EQ(128, entry->readahead_size_kb);
1111 entry++;
1112
1113 EXPECT_EQ("none2", entry->mount_point);
1114 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
1115 EXPECT_EQ(-1, entry->readahead_size_kb);
1116 entry++;
1117
1118 EXPECT_EQ("none3", entry->mount_point);
1119 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
1120 EXPECT_EQ(-1, entry->readahead_size_kb);
1121 entry++;
1122
1123 EXPECT_EQ("none4", entry->mount_point);
1124 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
1125 EXPECT_EQ(-1, entry->readahead_size_kb);
1126 entry++;
1127
1128 EXPECT_EQ("none5", entry->mount_point);
1129 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
1130 EXPECT_EQ(-1, entry->readahead_size_kb);
1131 entry++;
1132
1133 EXPECT_EQ("none6", entry->mount_point);
1134 EXPECT_TRUE(CompareFlags(flags, entry->fs_mgr_flags));
1135 EXPECT_EQ(0, entry->readahead_size_kb);
1136 }
1137
TEST(fs_mgr,TransformFstabForDsu)1138 TEST(fs_mgr, TransformFstabForDsu) {
1139 TemporaryFile tf;
1140 ASSERT_TRUE(tf.fd != -1);
1141 std::string fstab_contents = R"fs(
1142 data /data f2fs noatime wait,latemount
1143 system /system erofs ro wait,logical,first_stage_mount
1144 system /system ext4 ro wait,logical,first_stage_mount
1145 vendor /vendor ext4 ro wait,logical,first_stage_mount
1146 )fs";
1147
1148 ASSERT_TRUE(android::base::WriteStringToFile(fstab_contents, tf.path));
1149
1150 // If GSI is installed, ReadFstabFromFile() would have called TransformFstabForDsu() implicitly.
1151 // In other words, TransformFstabForDsu() would be called two times if running CTS-on-GSI,
1152 // which implies TransformFstabForDsu() should be idempotent.
1153 Fstab fstab;
1154 EXPECT_TRUE(ReadFstabFromFile(tf.path, &fstab));
1155 TransformFstabForDsu(&fstab, "dsu", {"system_gsi", "userdata_gsi"});
1156 ASSERT_EQ(4U, fstab.size());
1157
1158 auto entry = fstab.begin();
1159
1160 EXPECT_EQ("/data", entry->mount_point);
1161 EXPECT_EQ("userdata_gsi", entry->blk_device);
1162 entry++;
1163
1164 EXPECT_EQ("/system", entry->mount_point);
1165 EXPECT_EQ("system_gsi", entry->blk_device);
1166 EXPECT_EQ("erofs", entry->fs_type);
1167 entry++;
1168
1169 EXPECT_EQ("/system", entry->mount_point);
1170 EXPECT_EQ("system_gsi", entry->blk_device);
1171 EXPECT_EQ("ext4", entry->fs_type);
1172 entry++;
1173
1174 EXPECT_EQ("/vendor", entry->mount_point);
1175 EXPECT_EQ("vendor", entry->blk_device);
1176 entry++;
1177 }
1178
TEST(fs_mgr,TransformFstabForDsu_synthesisExt4Entry)1179 TEST(fs_mgr, TransformFstabForDsu_synthesisExt4Entry) {
1180 TemporaryFile tf;
1181 ASSERT_TRUE(tf.fd != -1);
1182 std::string fstab_contents = R"fs(
1183 system /system erofs ro wait,logical,first_stage_mount
1184 vendor /vendor ext4 ro wait,logical,first_stage_mount
1185 data /data f2fs noatime wait,latemount
1186 )fs";
1187
1188 ASSERT_TRUE(android::base::WriteStringToFile(fstab_contents, tf.path));
1189
1190 Fstab fstab;
1191 EXPECT_TRUE(ReadFstabFromFile(tf.path, &fstab));
1192 TransformFstabForDsu(&fstab, "dsu", {"system_gsi", "userdata_gsi"});
1193 ASSERT_EQ(4U, fstab.size());
1194
1195 auto entry = fstab.begin();
1196
1197 EXPECT_EQ("/system", entry->mount_point);
1198 EXPECT_EQ("system_gsi", entry->blk_device);
1199 EXPECT_EQ("erofs", entry->fs_type);
1200 entry++;
1201
1202 EXPECT_EQ("/system", entry->mount_point);
1203 EXPECT_EQ("system_gsi", entry->blk_device);
1204 EXPECT_EQ("ext4", entry->fs_type);
1205 entry++;
1206
1207 EXPECT_EQ("/vendor", entry->mount_point);
1208 EXPECT_EQ("vendor", entry->blk_device);
1209 entry++;
1210
1211 EXPECT_EQ("/data", entry->mount_point);
1212 EXPECT_EQ("userdata_gsi", entry->blk_device);
1213 entry++;
1214 }
1215
TEST(fs_mgr,TransformFstabForDsu_synthesisAllMissingEntries)1216 TEST(fs_mgr, TransformFstabForDsu_synthesisAllMissingEntries) {
1217 TemporaryFile tf;
1218 ASSERT_TRUE(tf.fd != -1);
1219 std::string fstab_contents = R"fs(
1220 data /data f2fs noatime wait,latemount
1221 vendor /vendor ext4 ro wait,logical,first_stage_mount
1222 )fs";
1223
1224 ASSERT_TRUE(android::base::WriteStringToFile(fstab_contents, tf.path));
1225
1226 Fstab fstab;
1227 EXPECT_TRUE(ReadFstabFromFile(tf.path, &fstab));
1228 TransformFstabForDsu(&fstab, "dsu", {"system_gsi", "userdata_gsi"});
1229 ASSERT_EQ(4U, fstab.size());
1230
1231 auto entry = fstab.begin();
1232
1233 EXPECT_EQ("/data", entry->mount_point);
1234 EXPECT_EQ("userdata_gsi", entry->blk_device);
1235 entry++;
1236
1237 EXPECT_EQ("/vendor", entry->mount_point);
1238 EXPECT_EQ("vendor", entry->blk_device);
1239 entry++;
1240
1241 EXPECT_EQ("/system", entry->mount_point);
1242 EXPECT_EQ("system_gsi", entry->blk_device);
1243 EXPECT_EQ("ext4", entry->fs_type);
1244 entry++;
1245
1246 EXPECT_EQ("/system", entry->mount_point);
1247 EXPECT_EQ("system_gsi", entry->blk_device);
1248 EXPECT_EQ("erofs", entry->fs_type);
1249 entry++;
1250 }
1251