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 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) { }
TearDownTestCase(void)37 void AdaptorDsoftbusFileTest::TearDownTestCase(void) { }
SetUp()38 void AdaptorDsoftbusFileTest::SetUp() { }
TearDown()39 void AdaptorDsoftbusFileTest::TearDown() { }
40
41 /**
42 * @tc.name: SoftBusAdapter_ReadFileTest_001
43 * @tc.desc: read file test
44 * @tc.type: FUNC
45 * @tc.require:
46 */
47 HWTEST_F(AdaptorDsoftbusFileTest, SoftBusReadFileTest001, TestSize.Level1)
48 {
49 int32_t fd = 0;
50 uint32_t value = 0;
51 int32_t ret;
52 ret = SoftBusOpenFile(nullptr, SOFTBUS_O_RDONLY);
53 EXPECT_EQ(SOFTBUS_INVALID_FD, ret);
54 ret = SoftBusOpenFile("/data", SOFTBUS_O_RDONLY);
55 EXPECT_NE(SOFTBUS_INVALID_FD, ret);
56 fd = SoftBusOpenFile("/dev/urandom", SOFTBUS_O_RDONLY);
57 EXPECT_NE(SOFTBUS_INVALID_FD, fd);
58 ret = SoftBusReadFile(fd, nullptr, sizeof(uint32_t));
59 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
60 ret = SoftBusReadFile(fd, &value, sizeof(uint32_t));
61 EXPECT_NE(SOFTBUS_ERR, ret);
62 SoftBusCloseFile(SOFTBUS_INVALID_FD);
63 SoftBusCloseFile(fd);
64 }
65
66 /**
67 * @tc.name: SoftBusAdapter_OpenFileWithPermsTest_001
68 * @tc.desc: softbus open file with perms test
69 * @tc.type: FUNC
70 * @tc.require:
71 */
72 HWTEST_F(AdaptorDsoftbusFileTest, SoftBusOpenFileWithPermsTest001, TestSize.Level1)
73 {
74 int32_t fd = 0;
75 int32_t ret =
76 SoftBusOpenFileWithPerms(nullptr, SOFTBUS_O_WRONLY | SOFTBUS_O_CREATE, SOFTBUS_S_IRUSR | SOFTBUS_S_IWUSR);
77 EXPECT_EQ(SOFTBUS_INVALID_FD, ret);
78 fd = SoftBusOpenFileWithPerms(
79 "/dev/urandom", SOFTBUS_O_WRONLY | SOFTBUS_O_CREATE, SOFTBUS_S_IRUSR | SOFTBUS_S_IWUSR);
80 EXPECT_NE(SOFTBUS_INVALID_FD, fd);
81 SoftBusCloseFile(fd);
82 }
83
84 /**
85 * @tc.name: SoftBusAdapter_PreadFileTest_001
86 * @tc.desc: pread file test
87 * @tc.type: FUNC
88 * @tc.require:
89 */
90 HWTEST_F(AdaptorDsoftbusFileTest, SoftBusPreadFileTest001, TestSize.Level1)
91 {
92 int32_t fd = 0;
93 uint32_t value = 0;
94 uint64_t readBytes = 4;
95 uint64_t offset = 1;
96 int64_t ret;
97 fd = SoftBusOpenFile("/dev/urandom", SOFTBUS_O_RDONLY);
98 EXPECT_NE(SOFTBUS_INVALID_FD, fd);
99 ret = SoftBusPreadFile(fd, nullptr, readBytes, offset);
100 EXPECT_EQ(SOFTBUS_ERR, ret);
101 ret = SoftBusPreadFile(fd, &value, readBytes, offset);
102 EXPECT_NE(SOFTBUS_ERR, ret);
103 ret = SoftBusPwriteFile(fd, nullptr, readBytes, offset);
104 EXPECT_EQ(SOFTBUS_ERR, ret);
105 SoftBusCloseFile(fd);
106 }
107
108 /**
109 * @tc.name: SoftBusAdapter_MakeDirTest_001
110 * @tc.desc: make dir test
111 * @tc.type: FUNC
112 * @tc.require:
113 */
114 HWTEST_F(AdaptorDsoftbusFileTest, SoftBusMakeDirTest001, TestSize.Level1)
115 {
116 int32_t ret = SoftBusMakeDir(nullptr, DEFAULT_NEW_PATH_AUTHORITY);
117 EXPECT_EQ(SOFTBUS_ERR, ret);
118 }
119
120 /**
121 * @tc.name: SoftBusAdapter_GetFileSizeTest_001
122 * @tc.desc: make dir test
123 * @tc.type: FUNC
124 * @tc.require:
125 */
126 HWTEST_F(AdaptorDsoftbusFileTest, SoftBusGetFileSize001, TestSize.Level1)
127 {
128 uint64_t fileSize = 0;
129 int32_t ret = SoftBusGetFileSize(nullptr, &fileSize);
130 EXPECT_EQ(SOFTBUS_ERR, ret);
131 ret = SoftBusGetFileSize("/data", &fileSize);
132 EXPECT_EQ(SOFTBUS_OK, ret);
133 ret = SoftBusGetFileSize("/dev/test", &fileSize);
134 EXPECT_EQ(SOFTBUS_ERR, ret);
135 }
136 } // namespace OHOS