• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2025 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 <gtest/gtest.h>
17 #include <read_text_core.h>
18 
19 #define FILE_INFO "hello world"
20 #define FILE_PATH "/data/test/ReadTextCoreTest.txt"
21 
22 namespace OHOS {
23 namespace FileManagement {
24 namespace ModuleFileIO {
25 using namespace std;
26 
27 const string g_content = "hello world";
28 
29 class ReadTextCoreTest : public testing::Test {
30 public:
SetUpTestCase(void)31     static void SetUpTestCase(void)
32     {
33         int32_t fd = open(FILE_PATH, CREATE | O_RDWR, 0644);
34         write(fd, g_content.c_str(), g_content.length());
35         close(fd);
36     };
TearDownTestCase()37     static void TearDownTestCase()
38     {
39         rmdir(FILE_PATH);
40     };
SetUp()41     void SetUp() {};
TearDown()42     void TearDown() {};
43 };
44 /**
45 * @tc.name: DoReadTextTest_0001
46 * @tc.desc: Test function of DoReadText() interface for single argument.
47 * @tc.size: MEDIUM
48 * @tc.type: FUNC
49 * @tc.level Level 1
50 */
51 HWTEST_F(ReadTextCoreTest, DoReadTextTest_0001, testing::ext::TestSize.Level1)
52 {
53     GTEST_LOG_(INFO) << "ReadTextCoreTest-begin DoReadTextTest_0001";
54     auto ret = ReadTextCore::DoReadText(FILE_PATH);
55     EXPECT_TRUE(ret.IsSuccess());
56 
57     const auto &resText = ret.GetData().value();
58     string res = std::get<0>(resText);
59     EXPECT_EQ(res, FILE_INFO);
60 
61     GTEST_LOG_(INFO) << "ReadTextCoreTest-end DoReadTextTest_0001";
62 }
63 
64 /**
65 * @tc.name: DoReadTextTest_0002
66 * @tc.desc: Test function of DoReadText() interface for invalid offset.
67 * @tc.size: MEDIUM
68 * @tc.type: FUNC
69 * @tc.level Level 1
70 */
71 HWTEST_F(ReadTextCoreTest, DoReadTextTest_0002, testing::ext::TestSize.Level1)
72 {
73     GTEST_LOG_(INFO) << "ReadTextCoreTest-begin DoReadTextTest_0002";
74     ReadTextOptions options;
75     options.offset = -1;
76     auto ret = ReadTextCore::DoReadText(FILE_PATH, options);
77     EXPECT_FALSE(ret.IsSuccess());
78 
79     auto err = ret.GetError();
80     EXPECT_EQ(err.GetErrNo(), 13900020);
81 
82     GTEST_LOG_(INFO) << "ReadTextCoreTest-end DoReadTextTest_0002";
83 }
84 
85 /**
86 * @tc.name: DoReadTextTest_0003
87 * @tc.desc: Test function of DoReadText() interface for invalid length < 0.
88 * @tc.size: MEDIUM
89 * @tc.type: FUNC
90 * @tc.level Level 1
91 */
92 HWTEST_F(ReadTextCoreTest, DoReadTextTest_0003, testing::ext::TestSize.Level1)
93 {
94     GTEST_LOG_(INFO) << "ReadTextCoreTest-begin DoReadTextTest_0003";
95     ReadTextOptions options;
96     options.length = -5;
97     auto ret = ReadTextCore::DoReadText(FILE_PATH, options);
98     EXPECT_FALSE(ret.IsSuccess());
99 
100     auto err = ret.GetError();
101     EXPECT_EQ(err.GetErrNo(), 13900020);
102 
103     GTEST_LOG_(INFO) << "ReadTextCoreTest-end DoReadTextTest_0003";
104 }
105 
106 /**
107 * @tc.name: DoReadTextTest_0004
108 * @tc.desc: Test function of DoReadText() interface for invalid length > UINT_MAX.
109 * @tc.size: MEDIUM
110 * @tc.type: FUNC
111 * @tc.level Level 1
112 */
113 HWTEST_F(ReadTextCoreTest, DoReadTextTest_0004, testing::ext::TestSize.Level1)
114 {
115     GTEST_LOG_(INFO) << "ReadTextCoreTest-begin DoReadTextTest_0004";
116     ReadTextOptions options;
117     options.length = static_cast<int64_t>(UINT_MAX) + 1;
118     auto ret = ReadTextCore::DoReadText(FILE_PATH, options);
119     EXPECT_FALSE(ret.IsSuccess());
120 
121     auto err = ret.GetError();
122     EXPECT_EQ(err.GetErrNo(), 13900020);
123 
124     GTEST_LOG_(INFO) << "ReadTextCoreTest-end DoReadTextTest_0004";
125 }
126 
127 /**
128 * @tc.name: DoReadTextTest_0005
129 * @tc.desc: Test function of DoReadText() interface for invalid encoding.
130 * @tc.size: MEDIUM
131 * @tc.type: FUNC
132 * @tc.level Level 1
133 */
134 HWTEST_F(ReadTextCoreTest, DoReadTextTest_0005, testing::ext::TestSize.Level1)
135 {
136     GTEST_LOG_(INFO) << "ReadTextCoreTest-begin DoReadTextTest_0005";
137     ReadTextOptions options;
138     options.encoding = "gbk";
139     auto ret = ReadTextCore::DoReadText(FILE_PATH, options);
140     EXPECT_FALSE(ret.IsSuccess());
141 
142     auto err = ret.GetError();
143     EXPECT_EQ(err.GetErrNo(), 13900020);
144 
145     GTEST_LOG_(INFO) << "ReadTextCoreTest-end DoReadTextTest_0005";
146 }
147 
148 /**
149 * @tc.name: DoReadTextTest_0006
150 * @tc.desc: Test function of DoReadText() interface for no such file or directory.
151 * @tc.size: MEDIUM
152 * @tc.type: FUNC
153 * @tc.level Level 1
154 */
155 HWTEST_F(ReadTextCoreTest, DoReadTextTest_0006, testing::ext::TestSize.Level1)
156 {
157     GTEST_LOG_(INFO) << "ReadTextCoreTest-begin DoReadTextTest_0006";
158     auto ret = ReadTextCore::DoReadText("ReadTextCoreTest-begin-DoReadTextTest_0006.txt");
159     EXPECT_FALSE(ret.IsSuccess());
160 
161     auto err = ret.GetError();
162     EXPECT_EQ(err.GetErrNo(), 13900002);
163 
164     GTEST_LOG_(INFO) << "ReadTextCoreTest-end DoReadTextTest_0006";
165 }
166 
167 /**
168 * @tc.name: DoReadTextTest_0007
169 * @tc.desc: Test function of DoReadText() interface for huge offset.
170 * @tc.size: MEDIUM
171 * @tc.type: FUNC
172 * @tc.level Level 1
173 */
174 HWTEST_F(ReadTextCoreTest, DoReadTextTest_0007, testing::ext::TestSize.Level1)
175 {
176     GTEST_LOG_(INFO) << "ReadTextCoreTest-begin DoReadTextTest_0007";
177     ReadTextOptions options;
178     options.offset = 1000000; // 假设文件较小
179     auto ret = ReadTextCore::DoReadText(FILE_PATH, options);
180     EXPECT_FALSE(ret.IsSuccess());
181 
182     auto err = ret.GetError();
183     EXPECT_EQ(err.GetErrNo(), 13900020);
184 
185     GTEST_LOG_(INFO) << "ReadTextCoreTest-end DoReadTextTest_0007";
186 }
187 
188 /**
189 * @tc.name: DoReadTextTest_0008
190 * @tc.desc: Test function of DoReadText() interface for no such file or directory.
191 * @tc.size: MEDIUM
192 * @tc.type: FUNC
193 * @tc.level Level 1
194 */
195 HWTEST_F(ReadTextCoreTest, DoReadTextTest_0008, testing::ext::TestSize.Level1)
196 {
197     GTEST_LOG_(INFO) << "ReadTextCoreTest-begin DoReadTextTest_0008";
198     ReadTextOptions options;
199     options.length = 1000000; // 超过实际大小
200     auto ret = ReadTextCore::DoReadText(FILE_PATH, options);
201     ASSERT_TRUE(ret.IsSuccess());
202 
203     const auto &resText = ret.GetData().value();
204     string res = std::get<0>(resText);
205     EXPECT_EQ(res, FILE_INFO);
206 
207     GTEST_LOG_(INFO) << "ReadTextCoreTest-end DoReadTextTest_0008";
208 }
209 
210 /**
211 * @tc.name: DoReadTextTest_0009
212 * @tc.desc: Test function of DoReadText() interface for success.
213 * @tc.size: MEDIUM
214 * @tc.type: FUNC
215 * @tc.level Level 1
216 */
217 HWTEST_F(ReadTextCoreTest, DoReadTextTest_0009, testing::ext::TestSize.Level1)
218 {
219     GTEST_LOG_(INFO) << "ReadTextCoreTest-begin DoReadTextTest_0009";
220     ReadTextOptions options;
221     options.offset = 2;
222     options.length = 5;
223     auto ret = ReadTextCore::DoReadText(FILE_PATH, options);
224     ASSERT_TRUE(ret.IsSuccess());
225 
226     const auto &resText = ret.GetData().value();
227     string res = std::get<0>(resText);
228     std::string extracted = std::string(FILE_INFO).substr(
229         options.offset.value(), options.length.value());
230     EXPECT_EQ(res, extracted);
231 
232     GTEST_LOG_(INFO) << "ReadTextCoreTest-end DoReadTextTest_0009";
233 }
234 
235 }
236 }
237 }