• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "gtest/gtest.h"
16 #include <securec.h>
17 
18 #include "softbus_adapter_errcode.h"
19 #include "softbus_adapter_file.h"
20 #include "softbus_def.h"
21 #include "softbus_error_code.h"
22 
23 #define TEST_PATH_MAX 8
24 
25 using namespace std;
26 using namespace testing::ext;
27 
28 namespace OHOS {
29 const int32_t DEFAULT_NEW_PATH_AUTHORITY = 750;
30 
31 class AdaptorDsoftbusFileTest : public testing::Test {
32 protected:
33     static void SetUpTestCase(void);
34     static void TearDownTestCase(void);
35     void SetUp();
36     void TearDown();
37 };
SetUpTestCase(void)38 void AdaptorDsoftbusFileTest::SetUpTestCase(void) { }
TearDownTestCase(void)39 void AdaptorDsoftbusFileTest::TearDownTestCase(void) { }
SetUp()40 void AdaptorDsoftbusFileTest::SetUp() { }
TearDown()41 void AdaptorDsoftbusFileTest::TearDown() { }
42 
43 /**
44  * @tc.name: SoftBusAdapter_ReadFileTest_001
45  * @tc.desc: read file test
46  * @tc.type: FUNC
47  * @tc.require:
48  */
49 HWTEST_F(AdaptorDsoftbusFileTest, SoftBusReadFileTest001, TestSize.Level1)
50 {
51     int32_t fd = 0;
52     uint32_t value = 0;
53     int32_t ret;
54     ret = SoftBusOpenFile(nullptr, SOFTBUS_O_RDONLY);
55     EXPECT_EQ(SOFTBUS_INVALID_FD, ret);
56     ret = SoftBusOpenFile("/data", SOFTBUS_O_RDONLY);
57     EXPECT_NE(SOFTBUS_INVALID_FD, ret);
58     fd = SoftBusOpenFile("/dev/urandom", SOFTBUS_O_RDONLY);
59     EXPECT_NE(SOFTBUS_INVALID_FD, fd);
60     ret = SoftBusReadFile(fd, nullptr, sizeof(uint32_t));
61     EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
62     ret = SoftBusReadFile(fd, &value, sizeof(uint32_t));
63     EXPECT_NE(SOFTBUS_ERR, ret);
64     SoftBusCloseFile(SOFTBUS_INVALID_FD);
65     SoftBusCloseFile(fd);
66 }
67 
68 /**
69  * @tc.name: SoftBusAdapter_OpenFileWithPermsTest_001
70  * @tc.desc: softbus open file with perms test
71  * @tc.type: FUNC
72  * @tc.require:
73  */
74 HWTEST_F(AdaptorDsoftbusFileTest, SoftBusOpenFileWithPermsTest001, TestSize.Level1)
75 {
76     int32_t fd = 0;
77     int32_t ret =
78         SoftBusOpenFileWithPerms(nullptr, SOFTBUS_O_WRONLY | SOFTBUS_O_CREATE, SOFTBUS_S_IRUSR | SOFTBUS_S_IWUSR);
79     EXPECT_EQ(SOFTBUS_INVALID_FD, ret);
80     fd = SoftBusOpenFileWithPerms(
81         "/dev/urandom", SOFTBUS_O_WRONLY | SOFTBUS_O_CREATE, SOFTBUS_S_IRUSR | SOFTBUS_S_IWUSR);
82     EXPECT_NE(SOFTBUS_INVALID_FD, fd);
83     SoftBusCloseFile(fd);
84 }
85 
86 /**
87  * @tc.name: SoftBusAdapter_PreadFileTest_001
88  * @tc.desc: pread file test
89  * @tc.type: FUNC
90  * @tc.require:
91  */
92 HWTEST_F(AdaptorDsoftbusFileTest, SoftBusPreadFileTest001, TestSize.Level1)
93 {
94     int32_t fd = 0;
95     uint32_t value = 0;
96     uint64_t readBytes = 4;
97     uint64_t offset = 1;
98     int64_t ret;
99     fd = SoftBusOpenFile("/dev/urandom", SOFTBUS_O_RDONLY);
100     EXPECT_NE(SOFTBUS_INVALID_FD, fd);
101     ret = SoftBusPreadFile(fd, nullptr, readBytes, offset);
102     EXPECT_EQ(SOFTBUS_ERR, ret);
103     ret = SoftBusPreadFile(fd, &value, readBytes, offset);
104     EXPECT_NE(SOFTBUS_ERR, ret);
105     ret = SoftBusPwriteFile(fd, nullptr, readBytes, offset);
106     EXPECT_EQ(SOFTBUS_ERR, ret);
107     SoftBusCloseFile(fd);
108 }
109 
110 /**
111  * @tc.name: SoftBusAdapter_MakeDirTest_001
112  * @tc.desc: make dir test
113  * @tc.type: FUNC
114  * @tc.require:
115  */
116 HWTEST_F(AdaptorDsoftbusFileTest, SoftBusMakeDirTest001, TestSize.Level1)
117 {
118     int32_t ret = SoftBusMakeDir(nullptr, DEFAULT_NEW_PATH_AUTHORITY);
119     EXPECT_EQ(SOFTBUS_ERR, ret);
120 }
121 
122 /**
123  * @tc.name: SoftBusAdapter_GetFileSizeTest_001
124  * @tc.desc: make dir test
125  * @tc.type: FUNC
126  * @tc.require:
127  */
128 HWTEST_F(AdaptorDsoftbusFileTest, SoftBusGetFileSize001, TestSize.Level1)
129 {
130     uint64_t fileSize = 0;
131     int32_t ret = SoftBusGetFileSize(nullptr, &fileSize);
132     EXPECT_EQ(SOFTBUS_ERR, ret);
133     ret = SoftBusGetFileSize("/data", &fileSize);
134     EXPECT_EQ(SOFTBUS_OK, ret);
135     ret = SoftBusGetFileSize("/dev/test", &fileSize);
136     EXPECT_EQ(SOFTBUS_ERR, ret);
137 }
138 
139 /**
140  * @tc.name: SoftBusAdapter_SoftBusWriteFileFd_001
141  * @tc.desc: SoftBusWriteFileFd will return SOFTBUS_FILE_ERR when given invalid param
142  * @tc.type: FUNC
143  * @tc.require:
144  */
145 HWTEST_F(AdaptorDsoftbusFileTest, SoftBusWriteFileFd001, TestSize.Level1)
146 {
147     int32_t fd = 1;
148     string buff = "0123456";
149     uint32_t len = buff.length();
150     int32_t ret = SoftBusWriteFileFd(fd, buff.c_str(), 0);
151     EXPECT_EQ(SOFTBUS_FILE_ERR, ret);
152     ret = SoftBusWriteFileFd(fd, NULL, len);
153     EXPECT_EQ(SOFTBUS_FILE_ERR, ret);
154     ret = SoftBusWriteFileFd(fd, NULL, 0);
155     EXPECT_EQ(SOFTBUS_FILE_ERR, ret);
156 }
157 
158 /**
159  * @tc.name: SoftBusAdapter_SoftBusAccessFile_001
160  * @tc.desc: SoftBusAccessFile will return SOFTBUS_ERR when given invalid param
161  * @tc.type: FUNC
162  * @tc.require:
163  */
164 HWTEST_F(AdaptorDsoftbusFileTest, SoftBusAccessFile, TestSize.Level1)
165 {
166     int32_t ret = SoftBusAccessFile(NULL, F_OK);
167     SoftBusRemoveFile(NULL);
168     EXPECT_EQ(SOFTBUS_ERR, ret);
169 }
170 
171 /**
172  * @tc.name: SoftBusAdapter_SoftBusRealPath_001
173  * @tc.desc: SoftBusRealPath will return NULL when given invalid param
174  * @tc.type: FUNC
175  * @tc.require:
176  */
177 HWTEST_F(AdaptorDsoftbusFileTest, SoftBusRealPath, TestSize.Level1)
178 {
179     string path = "./path";
180     char absPath[TEST_PATH_MAX] = { 0 };
181     char* ret = SoftBusRealPath(path.c_str(), NULL);
182     EXPECT_EQ(NULL, ret);
183     ret = SoftBusRealPath(NULL, NULL);
184     EXPECT_EQ(NULL, ret);
185     ret = SoftBusRealPath(NULL, absPath);
186     EXPECT_EQ(NULL, ret);
187 }
188 } // namespace OHOS
189