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