• 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 "create_stream_core.h"
18 
19 #define CREATE_STREAM_FILE_PATH "/data/test/CreateStreamCoreTest.txt"
20 
21 namespace OHOS {
22 namespace FileManagement {
23 namespace ModuleFileIO {
24 using namespace std;
25 class CreateStreamCoreTest : public testing::Test {
26 public:
SetUpTestCase(void)27     static void SetUpTestCase(void)
28     {
29         int32_t fd = open(CREATE_STREAM_FILE_PATH, CREATE | O_RDWR, 0644);
30         close(fd);
31     };
TearDownTestCase()32     static void TearDownTestCase()
33     {
34         rmdir(CREATE_STREAM_FILE_PATH);
35     };
SetUp()36     void SetUp() {};
TearDown()37     void TearDown() {};
38 };
39 /**
40 * @tc.name: DoCreateStreamTest_0001
41 * @tc.desc: Test function of DoCreateStream() interface for success.
42 * @tc.size: MEDIUM
43 * @tc.type: FUNC
44 * @tc.level Level 1
45 */
46 HWTEST_F(CreateStreamCoreTest, DoCreateStreamTest_0001, testing::ext::TestSize.Level1)
47 {
48     GTEST_LOG_(INFO) << "CreateStreamCoreTest-begin DoCreateStreamTest_0001";
49     auto ret = CreateStreamCore::DoCreateStream(CREATE_STREAM_FILE_PATH, "r");
50     ASSERT_TRUE(ret.IsSuccess());
51 
52     auto stream = ret.GetData().value();
53     ASSERT_NE(stream, nullptr);
54     auto retClose = stream->Close();
55     EXPECT_TRUE(retClose.IsSuccess());
56 
57     GTEST_LOG_(INFO) << "CreateStreamCoreTest-end DoCreateStreamTest_0001";
58 }
59 
60 /**
61 * @tc.name: DoCreateStreamTest_0002
62 * @tc.desc: Test function of DoCreateStream() interface for fail.
63 * @tc.size: MEDIUM
64 * @tc.type: FUNC
65 * @tc.level Level 1
66 */
67 HWTEST_F(CreateStreamCoreTest, DoCreateStreamTest_0002, testing::ext::TestSize.Level1)
68 {
69     GTEST_LOG_(INFO) << "CreateStreamCoreTest-begin DoCreateStreamTest_0002";
70     auto ret = CreateStreamCore::DoCreateStream(CREATE_STREAM_FILE_PATH, "ssss");
71     EXPECT_FALSE(ret.IsSuccess());
72 
73     auto err = ret.GetError();
74     EXPECT_EQ(err.GetErrNo(), 13900020);
75 
76     GTEST_LOG_(INFO) << "CreateStreamCoreTest-end DoCreateStreamTest_0002";
77 }
78 
79 }
80 }
81 }