1 // Copyright 2018 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file
4 
5 #include <fcntl.h>
6 #include <stdio.h>
7 
8 #include <fbl/auto_call.h>
9 #include <fbl/function.h>
10 #include <fbl/string.h>
11 #include <fbl/unique_fd.h>
12 #include <fs-management/mount.h>
13 #include <fs-management/ramdisk.h>
14 #include <fs-test-utils/fixture.h>
15 #include <fs-test-utils/unittest.h>
16 #include <unittest/unittest.h>
17 #include <zircon/device/block.h>
18 #include <zircon/syscalls.h>
19 
20 namespace fs_test_utils {
21 namespace {
22 
OptionsUseRamdiskAndFvm()23 FixtureOptions OptionsUseRamdiskAndFvm() {
24     FixtureOptions options = FixtureOptions::Default(DISK_FORMAT_MINFS);
25     options.use_fvm = true;
26     options.fs_type = DISK_FORMAT_MINFS;
27     return options;
28 }
29 
OptionsUseRamdiskAndFvm2()30 FixtureOptions OptionsUseRamdiskAndFvm2() {
31     return OptionsUseRamdiskAndFvm();
32 }
33 
VerifyRamdiskAndFvmExist(Fixture * fixture)34 bool VerifyRamdiskAndFvmExist(Fixture* fixture) {
35     BEGIN_TEST;
36     ASSERT_FALSE(fixture->partition_path().empty(), "No partition path set");
37     ASSERT_FALSE(fixture->block_device_path().empty(), "No block device path set.");
38     ASSERT_FALSE(fixture->fs_path().empty(), "No fs_path set");
39 
40     fbl::unique_fd fs_path_fd(open(fixture->fs_path().c_str(), O_RDONLY | O_DIRECTORY));
41     ASSERT_TRUE(fs_path_fd);
42 
43     fbl::unique_fd block_fd(open(fixture->block_device_path().c_str(), O_RDONLY));
44     ASSERT_TRUE(block_fd);
45     disk_format_t actual = detect_disk_format(block_fd.get());
46     ASSERT_EQ(actual, DISK_FORMAT_FVM);
47 
48     fbl::unique_fd fs_fd(open(fixture->partition_path().c_str(), O_RDONLY));
49     ASSERT_TRUE(fs_fd);
50     actual = detect_disk_format(fs_fd.get());
51     ASSERT_EQ(actual, fixture->options().fs_type);
52 
53     END_TEST;
54 }
55 
VerifyRamdiskAndFvmExist2(Fixture * fixture)56 bool VerifyRamdiskAndFvmExist2(Fixture* fixture) {
57     return VerifyRamdiskAndFvmExist(fixture);
58 }
59 
60 BEGIN_FS_TEST_CASE(UnittestFixtureTest, OptionsUseRamdiskAndFvm)
61 RUN_FS_TEST_F(VerifyRamdiskAndFvmExist)
62 END_FS_TEST_CASE(UnittestFixtureTest, OptionsUseRamdiskAndFvm)
63 
64 // Verifies that we can define multiple test cases without collision on global symbols,
65 // and run multiple tests.
66 BEGIN_FS_TEST_CASE(UnittestFixtureTest, OptionsUseRamdiskAndFvm2)
67 RUN_FS_TEST_F(VerifyRamdiskAndFvmExist)
68 RUN_FS_TEST_F(VerifyRamdiskAndFvmExist)
69 RUN_FS_TEST_F(VerifyRamdiskAndFvmExist2)
70 END_FS_TEST_CASE(UnittestFixtureTest, OptionsUseRamdiskAndFvm2)
71 
72 } // namespace
73 } // namespace fs_test_utils
74 
75 namespace fs_test_utils_2 {
76 namespace {
77 
Options()78 fs_test_utils::FixtureOptions Options() {
79     return fs_test_utils::OptionsUseRamdiskAndFvm();
80 }
81 
82 BEGIN_FS_TEST_CASE(UnittestFixtureFromAnotherNamespaceTest, Options)
83 RUN_FS_TEST_F(fs_test_utils::VerifyRamdiskAndFvmExist)
84 END_FS_TEST_CASE(UnittestFixtureFromAnotherNamespaceTest, Options)
85 } // namespace
86 
87 } // namespace fs_test_utils_2
88