1 /*
2 * Copyright (c) 2023 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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
18 #include <memory>
19
20 #include "directory_ex.h"
21 #include "file_utils.h"
22
23 namespace OHOS::FileManagement::CloudSync::Test {
24 using namespace testing;
25 using namespace testing::ext;
26 using namespace std;
27
28 class FileUtilsTest : public testing::Test {
29 public:
30 static void SetUpTestCase(void);
31 static void TearDownTestCase(void);
32 void SetUp();
33 void TearDown();
34 };
35
SetUpTestCase(void)36 void FileUtilsTest::SetUpTestCase(void)
37 {
38 GTEST_LOG_(INFO) << "SetUpTestCase";
39 }
40
TearDownTestCase(void)41 void FileUtilsTest::TearDownTestCase(void)
42 {
43 GTEST_LOG_(INFO) << "TearDownTestCase";
44 }
45
SetUp(void)46 void FileUtilsTest::SetUp(void)
47 {
48 GTEST_LOG_(INFO) << "SetUp";
49 }
50
TearDown(void)51 void FileUtilsTest::TearDown(void)
52 {
53 GTEST_LOG_(INFO) << "TearDown";
54 }
55
56 /**
57 * @tc.name: ReadFileTest001
58 * @tc.desc: Verify the ReadFile function
59 * @tc.type: FUNC
60 * @tc.require: SR000HRKKA
61 */
62 HWTEST_F(FileUtilsTest, ReadFileTest001, TestSize.Level1)
63 {
64 GTEST_LOG_(INFO) << "ReadFileTest001 Begin";
65 try {
66 int fd = -1;
67 off_t offset = 0;
68 size_t size = 1024;
69 void *data = nullptr;
70 int ret = FileUtils::ReadFile(fd, offset, size, data);
71 EXPECT_EQ(ret, -1);
72 } catch (...) {
73 EXPECT_TRUE(false);
74 GTEST_LOG_(INFO) << " ReadFileTest001 ERROR";
75 }
76
77 GTEST_LOG_(INFO) << "ReadFileTest001 End";
78 }
79
80 /**
81 * @tc.name: ReadFileTest002
82 * @tc.desc: Verify the ReadFile function
83 * @tc.type: FUNC
84 * @tc.require: SR000HRKKA
85 */
86 HWTEST_F(FileUtilsTest, ReadFileTest002, TestSize.Level1)
87 {
88 GTEST_LOG_(INFO) << "ReadFileTest002 Begin";
89 try {
90 int fd = 1;
91 off_t offset = 1;
92 size_t size = 5;
93 void *data = new int8_t;
94 int ret = FileUtils::ReadFile(fd, offset, size, data);
95 EXPECT_EQ(ret, -29);
96 } catch (...) {
97 EXPECT_TRUE(false);
98 GTEST_LOG_(INFO) << " ReadFileTest002 ERROR";
99 }
100
101 GTEST_LOG_(INFO) << "ReadFileTest002 End";
102 }
103
104 /**
105 * @tc.name: ReadFileTest003
106 * @tc.desc: Verify the ReadFile function
107 * @tc.type: FUNC
108 * @tc.require: issueI7SP3A
109 */
110 HWTEST_F(FileUtilsTest, ReadFileTest003, TestSize.Level1)
111 {
112 GTEST_LOG_(INFO) << "ReadFileTest003 Start";
113 try {
114 auto data = make_shared<char>(1);
115 int64_t ret = FileUtils::ReadFile(-1, 0, 0, data.get());
116 EXPECT_EQ(ret, -1);
117 ret = FileUtils::ReadFile(0, -1, 0, data.get());
118 EXPECT_EQ(ret, -1);
119 ret = FileUtils::ReadFile(0, 0, 0, nullptr);
120 EXPECT_EQ(ret, -1);
121 } catch (...) {
122 EXPECT_FALSE(false);
123 GTEST_LOG_(INFO) << " ReadFileTest003 ERROR";
124 }
125 GTEST_LOG_(INFO) << "ReadFileTest003 End";
126 }
127
128 /**
129 * @tc.name: WriteFileTest001
130 * @tc.desc: Verify the WriteFile function
131 * @tc.type: FUNC
132 * @tc.require: SR000HRKKA
133 */
134 HWTEST_F(FileUtilsTest, WriteFileTest001, TestSize.Level1)
135 {
136 GTEST_LOG_(INFO) << "WriteFileTest001 Begin";
137 try {
138 int fd = -1;
139 void *data = nullptr;
140 off_t offset = 0;
141 size_t size = 1024;
142 int ret = FileUtils::WriteFile(fd, data, offset, size);
143 EXPECT_EQ(ret, -1);
144 } catch (...) {
145 EXPECT_TRUE(false);
146 GTEST_LOG_(INFO) << " WriteFileTest001 ERROR";
147 }
148
149 GTEST_LOG_(INFO) << "WriteFileTest001 End";
150 }
151
152 /**
153 * @tc.name: WriteFileTest002
154 * @tc.desc: Verify the ReadFile function
155 * @tc.type: FUNC
156 * @tc.require: SR000HRKKA
157 */
158 HWTEST_F(FileUtilsTest, WriteFileTest002, TestSize.Level1)
159 {
160 GTEST_LOG_(INFO) << "WriteFileTest002 Begin";
161 try {
162 int fd = 0;
163 const void* data = "data";
164 off_t offset = 0;
165 size_t size = 5;
166 int ret = FileUtils::WriteFile(fd, data, offset, size);
167 EXPECT_EQ(ret, -29);
168 } catch (...) {
169 EXPECT_TRUE(false);
170 GTEST_LOG_(INFO) << " WriteFileTest002 ERROR";
171 }
172
173 GTEST_LOG_(INFO) << "WriteFileTest002 End";
174 }
175
176 /**
177 * @tc.name: WriteFileTest003
178 * @tc.desc: Verify the WriteFile function
179 * @tc.type: FUNC
180 * @tc.require: issueI7SP3A
181 */
182 HWTEST_F(FileUtilsTest, WriteFileTest003, TestSize.Level1)
183 {
184 GTEST_LOG_(INFO) << "WriteFileTest003 Start";
185 try {
186 auto data = make_shared<char>(1);
187 int64_t ret = FileUtils::WriteFile(-1, data.get(), 0, 1);
188 EXPECT_EQ(ret, -1);
189 ret = FileUtils::WriteFile(0, nullptr, 0, 0);
190 EXPECT_EQ(ret, -1);
191 ret = FileUtils::WriteFile(0, data.get(), -1, 1);
192 EXPECT_EQ(ret, -1);
193 } catch (...) {
194 EXPECT_FALSE(false);
195 GTEST_LOG_(INFO) << " WriteFileTest003 ERROR";
196 }
197 GTEST_LOG_(INFO) << "WriteFileTest003 End";
198 }
199 } // namespace OHOS::FileManagement::CloudSync::Test
200