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 "fdopen_stream_core.h" 18 19 #define FDOPEN_STREAM_FILE_PATH "/data/test/FdopenStreamCoreTest.txt" 20 21 namespace OHOS { 22 namespace FileManagement { 23 namespace ModuleFileIO { 24 using namespace std; 25 class FdopenStreamCoreTest : public testing::Test { 26 public: SetUpTestCase(void)27 static void SetUpTestCase(void) 28 { 29 int32_t fd = open(FDOPEN_STREAM_FILE_PATH, CREATE | O_RDWR, 0644); 30 close(fd); 31 }; TearDownTestCase()32 static void TearDownTestCase() 33 { 34 rmdir(FDOPEN_STREAM_FILE_PATH); 35 }; SetUp()36 void SetUp() {}; TearDown()37 void TearDown() {}; 38 }; 39 40 /** 41 * @tc.name: DoFdopenStreamTest_0001 42 * @tc.desc: Test function of DoFdopenStream() interface for success. 43 * @tc.size: MEDIUM 44 * @tc.type: FUNC 45 * @tc.level Level 1 46 */ 47 HWTEST_F(FdopenStreamCoreTest, DoFdopenStreamTest_0001, testing::ext::TestSize.Level1) 48 { 49 GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin DoFdopenStreamTest_0001"; 50 int32_t fd = open(FDOPEN_STREAM_FILE_PATH, O_RDWR); 51 if (fd <= 0) { 52 close(fd); 53 ASSERT_TRUE(false); 54 } 55 56 auto ret = FdopenStreamCore::DoFdopenStream(fd, "r"); 57 ASSERT_TRUE(ret.IsSuccess()); 58 59 auto stream = ret.GetData().value(); 60 ASSERT_NE(stream, nullptr); 61 auto retClose = stream->Close(); 62 EXPECT_TRUE(retClose.IsSuccess()); 63 64 close(fd); 65 66 GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end DoFdopenStreamTest_0001"; 67 } 68 69 /** 70 * @tc.name: DoFdopenStreamTest_0002 71 * @tc.desc: Test function of DoFdopenStream() interface for fd < 0. 72 * @tc.size: MEDIUM 73 * @tc.type: FUNC 74 * @tc.level Level 1 75 */ 76 HWTEST_F(FdopenStreamCoreTest, DoFdopenStreamTest_0002, testing::ext::TestSize.Level1) 77 { 78 GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin DoFdopenStreamTest_0002"; 79 auto ret = FdopenStreamCore::DoFdopenStream(-1, "r"); 80 EXPECT_FALSE(ret.IsSuccess()); 81 82 auto err = ret.GetError(); 83 EXPECT_EQ(err.GetErrNo(), 13900020); 84 85 GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end DoFdopenStreamTest_0002"; 86 } 87 88 /** 89 * @tc.name: DoFdopenStreamTest_0003 90 * @tc.desc: Test function of DoFdopenStream() interface for fail. 91 * @tc.size: MEDIUM 92 * @tc.type: FUNC 93 * @tc.level Level 1 94 */ 95 HWTEST_F(FdopenStreamCoreTest, DoFdopenStreamTest_0003, testing::ext::TestSize.Level1) 96 { 97 int32_t fd = open(FDOPEN_STREAM_FILE_PATH, O_RDWR); 98 if (fd <= 0) { 99 close(fd); 100 ASSERT_TRUE(false); 101 } 102 103 GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin DoFdopenStreamTest_0003"; 104 auto ret = FdopenStreamCore::DoFdopenStream(fd, "sssssss"); 105 EXPECT_FALSE(ret.IsSuccess()); 106 107 auto err = ret.GetError(); 108 EXPECT_EQ(err.GetErrNo(), 13900020); 109 110 close(fd); 111 112 GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end DoFdopenStreamTest_0003"; 113 } 114 115 } 116 } 117 }