• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: WriteFileTest001
106  * @tc.desc: Verify the WriteFile function
107  * @tc.type: FUNC
108  * @tc.require: SR000HRKKA
109  */
110 HWTEST_F(FileUtilsTest, WriteFileTest001, TestSize.Level1)
111 {
112     GTEST_LOG_(INFO) << "WriteFileTest001 Begin";
113     try {
114         int fd = -1;
115         void *data = nullptr;
116         off_t offset = 0;
117         size_t size = 1024;
118         int ret = FileUtils::WriteFile(fd, data, offset, size);
119         EXPECT_EQ(ret, -1);
120     } catch (...) {
121         EXPECT_TRUE(false);
122         GTEST_LOG_(INFO) << " WriteFileTest001 ERROR";
123     }
124 
125     GTEST_LOG_(INFO) << "WriteFileTest001 End";
126 }
127 
128 /**
129  * @tc.name: WriteFileTest002
130  * @tc.desc: Verify the ReadFile function
131  * @tc.type: FUNC
132  * @tc.require: SR000HRKKA
133  */
134 HWTEST_F(FileUtilsTest, WriteFileTest002, TestSize.Level1)
135 {
136     GTEST_LOG_(INFO) << "WriteFileTest002 Begin";
137     try {
138         int fd = 0;
139         const void* data = "data";
140         off_t offset = 0;
141         size_t size = 5;
142         int ret = FileUtils::WriteFile(fd, data, offset, size);
143         EXPECT_EQ(ret, -29);
144     } catch (...) {
145         EXPECT_TRUE(false);
146         GTEST_LOG_(INFO) << " WriteFileTest002 ERROR";
147     }
148 
149     GTEST_LOG_(INFO) << "WriteFileTest002 End";
150 }
151 } // namespace OHOS::FileManagement::CloudSync::Test
152