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 18 #include "c_mock.h" 19 #include "create_stream_core.h" 20 #include "fs_utils.h" 21 22 namespace OHOS { 23 namespace FileManagement { 24 namespace ModuleFileIO { 25 namespace Test { 26 using namespace std; 27 28 static const string g_streamFilePath = "/data/test/FsStreamCoreTest.txt"; 29 30 class FsStreamMockTest : public testing::Test { 31 public: SetUpTestCase(void)32 static void SetUpTestCase(void) 33 { 34 CMock::EnableMock(); 35 int32_t fd = open(g_streamFilePath.c_str(), CREATE | O_RDWR, 0644); 36 if (fd <= 0) { 37 ASSERT_TRUE(false); 38 } 39 close(fd); 40 }; TearDownTestCase()41 static void TearDownTestCase() 42 { 43 CMock::DisableMock(); 44 rmdir(g_streamFilePath.c_str()); 45 }; SetUp()46 void SetUp() {}; TearDown()47 void TearDown() {}; 48 }; 49 50 /** 51 * @tc.name: FsStreamSeekTest_0001 52 * @tc.desc: Test function of Seek() interface for fail fseek. 53 * @tc.size: MEDIUM 54 * @tc.type: FUNC 55 * @tc.level Level 1 56 */ 57 HWTEST_F(FsStreamMockTest, FsStreamSeekTest_0001, testing::ext::TestSize.Level1) 58 { 59 GTEST_LOG_(INFO) << "FsStreamMockTest-begin FsStreamSeekTest_0001"; 60 auto ret = CreateStreamCore::DoCreateStream(g_streamFilePath, "r+"); 61 ASSERT_TRUE(ret.IsSuccess()); 62 auto result = ret.GetData().value(); 63 64 auto mock_ = CMock::GetMock(); 65 EXPECT_CALL(*mock_, fseek(testing::_, testing::_, testing::_)).WillOnce(testing::Return(-1)); 66 67 auto retSk = result->Seek(1); 68 EXPECT_FALSE(retSk.IsSuccess()); 69 70 auto retCs = result->Close(); 71 ASSERT_TRUE(retCs.IsSuccess()); 72 73 GTEST_LOG_(INFO) << "FsStreamMockTest-end FsStreamSeekTest_0001"; 74 } 75 76 /** 77 * @tc.name: FsStreamSeekTest_0002 78 * @tc.desc: Test function of Seek() interface for fail ftell. 79 * @tc.size: MEDIUM 80 * @tc.type: FUNC 81 * @tc.level Level 1 82 */ 83 HWTEST_F(FsStreamMockTest, FsStreamSeekTest_0002, testing::ext::TestSize.Level1) 84 { 85 GTEST_LOG_(INFO) << "FsStreamMockTest-begin FsStreamSeekTest_0002"; 86 auto ret = CreateStreamCore::DoCreateStream(g_streamFilePath, "r+"); 87 ASSERT_TRUE(ret.IsSuccess()); 88 auto result = ret.GetData().value(); 89 90 auto mock_ = CMock::GetMock(); 91 EXPECT_CALL(*mock_, fseek(testing::_, testing::_, testing::_)).WillOnce(testing::Return(0)); 92 EXPECT_CALL(*mock_, ftell(testing::_)).WillOnce(testing::Return(-1)); 93 94 auto retSk = result->Seek(1); 95 EXPECT_FALSE(retSk.IsSuccess()); 96 97 auto retCs = result->Close(); 98 ASSERT_TRUE(retCs.IsSuccess()); 99 100 GTEST_LOG_(INFO) << "FsStreamMockTest-end FsStreamSeekTest_0002"; 101 } 102 103 /** 104 * @tc.name: FsStreamWriteTest_0001 105 * @tc.desc: Test function of Write() interface for string fail fseek. 106 * @tc.size: MEDIUM 107 * @tc.type: FUNC 108 * @tc.level Level 1 109 */ 110 HWTEST_F(FsStreamMockTest, FsStreamWriteTest_0001, testing::ext::TestSize.Level1) 111 { 112 GTEST_LOG_(INFO) << "FsStreamMockTest-begin FsStreamWriteTest_0001"; 113 auto ret = CreateStreamCore::DoCreateStream(g_streamFilePath, "w+"); 114 ASSERT_TRUE(ret.IsSuccess()); 115 auto result = ret.GetData().value(); 116 117 auto mock_ = CMock::GetMock(); 118 EXPECT_CALL(*mock_, fseek(testing::_, testing::_, testing::_)).WillOnce(testing::Return(-1)); 119 120 WriteOptions opt; 121 opt.offset = 5; 122 auto retWr = result->Write("FsStreamWriteTest_0001", opt); 123 EXPECT_FALSE(retWr.IsSuccess()); 124 125 auto retCs = result->Close(); 126 ASSERT_TRUE(retCs.IsSuccess()); 127 128 GTEST_LOG_(INFO) << "FsStreamMockTest-end FsStreamWriteTest_0001"; 129 } 130 131 /** 132 * @tc.name: FsStreamWriteTest_0002 133 * @tc.desc: Test function of Write() interface for ArrayBuffer fail fseek. 134 * @tc.size: MEDIUM 135 * @tc.type: FUNC 136 * @tc.level Level 1 137 */ 138 HWTEST_F(FsStreamMockTest, FsStreamWriteTest_0002, testing::ext::TestSize.Level1) 139 { 140 GTEST_LOG_(INFO) << "FsStreamMockTest-begin FsStreamWriteTest_0002"; 141 auto ret = CreateStreamCore::DoCreateStream(g_streamFilePath, "w+"); 142 ASSERT_TRUE(ret.IsSuccess()); 143 auto result = ret.GetData().value(); 144 145 auto mock_ = CMock::GetMock(); 146 EXPECT_CALL(*mock_, fseek(testing::_, testing::_, testing::_)).WillOnce(testing::Return(-1)); 147 148 WriteOptions opt; 149 opt.offset = 5; 150 string buf = "FsStreamWriteTest_0002"; 151 auto retWr = result->Write(ArrayBuffer(static_cast<void *>(buf.data()), buf.length()), opt); 152 EXPECT_FALSE(retWr.IsSuccess()); 153 154 auto retCs = result->Close(); 155 ASSERT_TRUE(retCs.IsSuccess()); 156 157 GTEST_LOG_(INFO) << "FsStreamMockTest-end FsStreamWriteTest_0002"; 158 } 159 160 /** 161 * @tc.name: FsStreamReadTest_0001 162 * @tc.desc: Test function of Read() interface for fail fseek. 163 * @tc.size: MEDIUM 164 * @tc.type: FUNC 165 * @tc.level Level 1 166 */ 167 HWTEST_F(FsStreamMockTest, FsStreamReadTest_0001, testing::ext::TestSize.Level1) 168 { 169 GTEST_LOG_(INFO) << "FsStreamMockTest-begin FsStreamReadTest_0001"; 170 auto ret = CreateStreamCore::DoCreateStream(g_streamFilePath, "r+"); 171 ASSERT_TRUE(ret.IsSuccess()); 172 auto result = ret.GetData().value(); 173 174 void *buffer = std::malloc(4096); 175 ArrayBuffer arrayBuffer(buffer, 4096); 176 177 auto mock_ = CMock::GetMock(); 178 EXPECT_CALL(*mock_, fseek(testing::_, testing::_, testing::_)).WillOnce(testing::Return(-1)); 179 180 ReadOptions opt; 181 opt.offset = 5; 182 auto retRd = result->Read(arrayBuffer, opt); 183 EXPECT_FALSE(retRd.IsSuccess()); 184 185 free(buffer); 186 auto retCs = result->Close(); 187 ASSERT_TRUE(retCs.IsSuccess()); 188 189 GTEST_LOG_(INFO) << "FsStreamMockTest-end FsStreamReadTest_0001"; 190 } 191 192 } // namespace Test 193 } // namespace ModuleFileIO 194 } // namespace FileManagement 195 } // namespace OHOS