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 
16 #include <cerrno>
17 #include <cstring>
18 #include <fcntl.h>
19 #include <sys/statvfs.h>
20 
21 #include "init_file.h"
22 #include "init_service.h"
23 #include "init_service_file.h"
24 #include "init_service_manager.h"
25 #include "param_stub.h"
26 #include "securec.h"
27 
28 using namespace testing::ext;
29 using namespace std;
30 
31 namespace init_ut {
32 class ServiceFileUnitTest : public testing::Test {
33 public:
SetUpTestCase(void)34     static void SetUpTestCase(void) {};
TearDownTestCase(void)35     static void TearDownTestCase(void) {};
SetUp()36     void SetUp() {};
TearDown()37     void TearDown() {};
38 };
39 
40 HWTEST_F(ServiceFileUnitTest, TestServiceFile, TestSize.Level1)
41 {
42     const char *fileName = "/data/filetest";
43     ServiceFile *fileOpt = (ServiceFile *)calloc(1, sizeof(ServiceFile) + strlen(fileName) + 1);
44     ASSERT_NE(fileOpt, nullptr);
45     fileOpt->next = nullptr;
46     fileOpt->flags = O_RDWR;
47     fileOpt->uid = 1000;
48     fileOpt->gid = 1000;
49     fileOpt->fd = -1;
50     fileOpt->perm = 0770;
51     int ret = strncpy_s(fileOpt->fileName, strlen(fileName) + 1, fileName, strlen(fileName));
52     EXPECT_EQ(ret, 0);
53     Service *service = AddService("test_service8");
54     ASSERT_NE(nullptr, service);
55 
56     service->fileCfg = fileOpt;
57     CreateServiceFile(service);
58     ret = GetControlFile("/data/filetest");
59     EXPECT_NE(ret, -1);
60     ret = strncpy_s(fileOpt->fileName, strlen(fileName) + 1, "fileName", strlen("fileName"));
61     EXPECT_EQ(ret, 0);
62     fileOpt->fd = 100; // 100 is fd
63     CreateServiceFile(service);
64     CloseServiceFile(fileOpt);
65     ret = strncpy_s(fileOpt->fileName, strlen(fileName) + 1, "/dev/filetest", strlen("/dev/filetest"));
66     EXPECT_EQ(ret, 0);
67     char *wrongName = (char *)malloc(PATH_MAX);
68     ASSERT_NE(wrongName, nullptr);
69     EXPECT_EQ(memset_s(wrongName, PATH_MAX, 1, PATH_MAX), 0);
70     ret = GetControlFile(wrongName);
71     EXPECT_NE(ret, 0);
72     GetControlFile("notExist");
73     GetControlFile(nullptr);
74     EXPECT_EQ(setenv("OHOS_FILE_ENV_PREFIX_testPath", "5", 0), 0);
75     GetControlFile("testPath");
76     EXPECT_EQ(setenv("OHOS_FILE_ENV_PREFIX_testPath1", "aaaaaaaa", 0), 0);
77     GetControlFile("testPath1");
78     free(wrongName);
79 
80     fileOpt->fd = -1;
81     CreateServiceFile(service);
82     CloseServiceFile(fileOpt);
83     free(fileOpt);
84     service->fileCfg = nullptr;
85     ReleaseService(service);
86 }
87 }
88