1 /*
2  * Copyright (c) 2022 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 <securec.h>
16 #include "gtest/gtest.h"
17 
18 #include "softbus_adapter_file.h"
19 #include "softbus_adapter_errcode.h"
20 #include "softbus_def.h"
21 #include "softbus_errcode.h"
22 
23 using namespace std;
24 using namespace testing::ext;
25 
26 namespace OHOS {
27 const int32_t DEFAULT_NEW_PATH_AUTHORITY = 750;
28 
29 class AdaptorDsoftbusFileTest : public testing::Test {
30 protected:
31     static void SetUpTestCase(void);
32     static void TearDownTestCase(void);
33     void SetUp();
34     void TearDown();
35 };
SetUpTestCase(void)36 void AdaptorDsoftbusFileTest::SetUpTestCase(void)
37 {
38 }
TearDownTestCase(void)39 void AdaptorDsoftbusFileTest::TearDownTestCase(void)
40 {
41 }
SetUp()42 void AdaptorDsoftbusFileTest::SetUp()
43 {
44 }
TearDown()45 void AdaptorDsoftbusFileTest::TearDown()
46 {
47 }
48 
49 /**
50  * @tc.name: SoftBusAdapter_ReadFileTest_001
51  * @tc.desc: read file test
52  * @tc.type: FUNC
53  * @tc.require:
54  */
55 HWTEST_F(AdaptorDsoftbusFileTest, SoftBusReadFileTest001, TestSize.Level1)
56 {
57     int32_t fd = 0;
58     uint32_t value = 0;
59     int32_t ret;
60     ret = SoftBusOpenFile(NULL, SOFTBUS_O_RDONLY);
61     EXPECT_EQ(SOFTBUS_INVALID_FD, ret);
62     ret = SoftBusOpenFile("/data", SOFTBUS_O_RDONLY);
63     EXPECT_NE(SOFTBUS_INVALID_FD, ret);
64     fd = SoftBusOpenFile("/dev/urandom", SOFTBUS_O_RDONLY);
65     EXPECT_NE(SOFTBUS_INVALID_FD, fd);
66     ret = SoftBusReadFile(fd, NULL, sizeof(uint32_t));
67     EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
68     ret = SoftBusReadFile(fd, &value, sizeof(uint32_t));
69     EXPECT_NE(SOFTBUS_ERR, ret);
70     SoftBusCloseFile(SOFTBUS_INVALID_FD);
71     SoftBusCloseFile(fd);
72 }
73 
74 /**
75  * @tc.name: SoftBusAdapter_OpenFileWithPermsTest_001
76  * @tc.desc: softbus open file with perms test
77  * @tc.type: FUNC
78  * @tc.require:
79  */
80 HWTEST_F(AdaptorDsoftbusFileTest, SoftBusOpenFileWithPermsTest001, TestSize.Level1)
81 {
82     int32_t fd = 0;
83     int32_t ret = SoftBusOpenFileWithPerms(NULL, SOFTBUS_O_WRONLY | SOFTBUS_O_CREATE,
84         SOFTBUS_S_IRUSR | SOFTBUS_S_IWUSR);
85     EXPECT_EQ(SOFTBUS_INVALID_FD, ret);
86     fd = SoftBusOpenFileWithPerms("/dev/urandom", SOFTBUS_O_WRONLY | SOFTBUS_O_CREATE,
87         SOFTBUS_S_IRUSR | SOFTBUS_S_IWUSR);
88     EXPECT_NE(SOFTBUS_INVALID_FD, fd);
89     SoftBusCloseFile(fd);
90 }
91 
92 /**
93  * @tc.name: SoftBusAdapter_PreadFileTest_001
94  * @tc.desc: pread file test
95  * @tc.type: FUNC
96  * @tc.require:
97  */
98 HWTEST_F(AdaptorDsoftbusFileTest, SoftBusPreadFileTest001, TestSize.Level1)
99 {
100     int32_t fd = 0;
101     uint32_t value = 0;
102     uint64_t readBytes = 4;
103     uint64_t offset = 1;
104     int64_t ret;
105     fd = SoftBusOpenFile("/dev/urandom", SOFTBUS_O_RDONLY);
106     EXPECT_NE(SOFTBUS_INVALID_FD, fd);
107     ret = SoftBusPreadFile(fd, NULL, readBytes, offset);
108     EXPECT_EQ(SOFTBUS_ERR, ret);
109     ret = SoftBusPreadFile(fd, &value, readBytes, offset);
110     EXPECT_NE(SOFTBUS_ERR, ret);
111     ret = SoftBusPwriteFile(fd, NULL, readBytes, offset);
112     EXPECT_EQ(SOFTBUS_ERR, ret);
113     SoftBusCloseFile(fd);
114 }
115 
116 /**
117  * @tc.name: SoftBusAdapter_MakeDirTest_001
118  * @tc.desc: make dir test
119  * @tc.type: FUNC
120  * @tc.require:
121  */
122 HWTEST_F(AdaptorDsoftbusFileTest, SoftBusMakeDirTest001, TestSize.Level1)
123 {
124     int32_t ret = SoftBusMakeDir(NULL, DEFAULT_NEW_PATH_AUTHORITY);
125     EXPECT_EQ(SOFTBUS_ERR, ret);
126 }
127 
128 /**
129  * @tc.name: SoftBusAdapter_GetFileSizeTest_001
130  * @tc.desc: make dir test
131  * @tc.type: FUNC
132  * @tc.require:
133  */
134 HWTEST_F(AdaptorDsoftbusFileTest, SoftBusGetFileSize001, TestSize.Level1)
135 {
136     uint64_t fileSize = 0;
137     int32_t ret = SoftBusGetFileSize(NULL, &fileSize);
138     EXPECT_EQ(SOFTBUS_ERR, ret);
139     ret = SoftBusGetFileSize("/data", &fileSize);
140     EXPECT_EQ(SOFTBUS_OK, ret);
141     ret = SoftBusGetFileSize("/dev/test", &fileSize);
142     EXPECT_EQ(SOFTBUS_ERR, ret);
143 }
144 }