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