1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <cerrno>
16 #include <unistd.h>
17 #include <fcntl.h>
18 #include "fs_manager/fs_manager.h"
19 #include "param_stub.h"
20 #include "init_mount.h"
21 #include "init.h"
22 #include "securec.h"
23 using namespace std;
24 using namespace testing::ext;
25 
26 namespace init_ut {
27 class MountUnitTest : public testing::Test {
28 public:
SetUpTestCase(void)29     static void SetUpTestCase(void) {}
TearDownTestCase(void)30     static void TearDownTestCase(void) {}
SetUp()31     void SetUp() {};
TearDown()32     void TearDown() {};
33 };
34 
35 HWTEST_F(MountUnitTest, TestGetBlockDevicePath, TestSize.Level1)
36 {
37     char path[20] = {0}; // 20 is path length
38     int fd = open("/bin/updater", O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC,  S_IRWXU);
39     if (fd < 0) {
40         return;
41     }
42     GetBlockDevicePath("/test", path, sizeof(path));
43     close(fd);
44     ReadConfig();
45     unlink("/bin/updater");
46     ReadConfig();
47     int ret = GetBlockDeviceByMountPoint(nullptr, nullptr, nullptr, 0);
48     EXPECT_EQ(ret, -1);
49     FstabItem fstabitem = {(char *)"deviceName", (char *)"mountPoint",
50         (char *)"fsType", (char *)"mountOptions", 1, nullptr};
51     Fstab fstab = {&fstabitem};
52     char devicename[20]; // 20 is devicename length
53     ret = GetBlockDeviceByMountPoint("notmountpoint", &fstab, devicename, sizeof(devicename));
54     EXPECT_EQ(ret, -1);
55     ret = GetBlockDeviceByMountPoint("mountPoint", &fstab, devicename, 0);
56     EXPECT_EQ(ret, -1);
57     ret = GetBlockDeviceByMountPoint("mountPoint", &fstab, devicename, sizeof(devicename));
58     EXPECT_EQ(ret, 0);
59 }
60 HWTEST_F(MountUnitTest, TestInvalidParam, TestSize.Level1)
61 {
62     int ret = MountRequriedPartitions(nullptr);
63     EXPECT_NE(ret, 0);
64 }
65 } // namespace init_ut
66