1060ff233Sopenharmony_ci/* 2060ff233Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3060ff233Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4060ff233Sopenharmony_ci * you may not use this file except in compliance with the License. 5060ff233Sopenharmony_ci * You may obtain a copy of the License at 6060ff233Sopenharmony_ci * 7060ff233Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8060ff233Sopenharmony_ci * 9060ff233Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10060ff233Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11060ff233Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12060ff233Sopenharmony_ci * See the License for the specific language governing permissions and 13060ff233Sopenharmony_ci * limitations under the License. 14060ff233Sopenharmony_ci */ 15060ff233Sopenharmony_ci#include <securec.h> 16060ff233Sopenharmony_ci#include "gtest/gtest.h" 17060ff233Sopenharmony_ci 18060ff233Sopenharmony_ci#include "softbus_adapter_file.h" 19060ff233Sopenharmony_ci#include "softbus_adapter_errcode.h" 20060ff233Sopenharmony_ci#include "softbus_def.h" 21060ff233Sopenharmony_ci#include "softbus_errcode.h" 22060ff233Sopenharmony_ci 23060ff233Sopenharmony_ciusing namespace std; 24060ff233Sopenharmony_ciusing namespace testing::ext; 25060ff233Sopenharmony_ci 26060ff233Sopenharmony_cinamespace OHOS { 27060ff233Sopenharmony_ciconst int32_t DEFAULT_NEW_PATH_AUTHORITY = 750; 28060ff233Sopenharmony_ci 29060ff233Sopenharmony_ciclass AdaptorDsoftbusFileTest : public testing::Test { 30060ff233Sopenharmony_ciprotected: 31060ff233Sopenharmony_ci static void SetUpTestCase(void); 32060ff233Sopenharmony_ci static void TearDownTestCase(void); 33060ff233Sopenharmony_ci void SetUp(); 34060ff233Sopenharmony_ci void TearDown(); 35060ff233Sopenharmony_ci}; 36060ff233Sopenharmony_civoid AdaptorDsoftbusFileTest::SetUpTestCase(void) 37060ff233Sopenharmony_ci{ 38060ff233Sopenharmony_ci} 39060ff233Sopenharmony_civoid AdaptorDsoftbusFileTest::TearDownTestCase(void) 40060ff233Sopenharmony_ci{ 41060ff233Sopenharmony_ci} 42060ff233Sopenharmony_civoid AdaptorDsoftbusFileTest::SetUp() 43060ff233Sopenharmony_ci{ 44060ff233Sopenharmony_ci} 45060ff233Sopenharmony_civoid AdaptorDsoftbusFileTest::TearDown() 46060ff233Sopenharmony_ci{ 47060ff233Sopenharmony_ci} 48060ff233Sopenharmony_ci 49060ff233Sopenharmony_ci/** 50060ff233Sopenharmony_ci * @tc.name: SoftBusAdapter_ReadFileTest_001 51060ff233Sopenharmony_ci * @tc.desc: read file test 52060ff233Sopenharmony_ci * @tc.type: FUNC 53060ff233Sopenharmony_ci * @tc.require: 54060ff233Sopenharmony_ci */ 55060ff233Sopenharmony_ciHWTEST_F(AdaptorDsoftbusFileTest, SoftBusReadFileTest001, TestSize.Level1) 56060ff233Sopenharmony_ci{ 57060ff233Sopenharmony_ci int32_t fd = 0; 58060ff233Sopenharmony_ci uint32_t value = 0; 59060ff233Sopenharmony_ci int32_t ret; 60060ff233Sopenharmony_ci ret = SoftBusOpenFile(NULL, SOFTBUS_O_RDONLY); 61060ff233Sopenharmony_ci EXPECT_EQ(SOFTBUS_INVALID_FD, ret); 62060ff233Sopenharmony_ci ret = SoftBusOpenFile("/data", SOFTBUS_O_RDONLY); 63060ff233Sopenharmony_ci EXPECT_NE(SOFTBUS_INVALID_FD, ret); 64060ff233Sopenharmony_ci fd = SoftBusOpenFile("/dev/urandom", SOFTBUS_O_RDONLY); 65060ff233Sopenharmony_ci EXPECT_NE(SOFTBUS_INVALID_FD, fd); 66060ff233Sopenharmony_ci ret = SoftBusReadFile(fd, NULL, sizeof(uint32_t)); 67060ff233Sopenharmony_ci EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret); 68060ff233Sopenharmony_ci ret = SoftBusReadFile(fd, &value, sizeof(uint32_t)); 69060ff233Sopenharmony_ci EXPECT_NE(SOFTBUS_ERR, ret); 70060ff233Sopenharmony_ci SoftBusCloseFile(SOFTBUS_INVALID_FD); 71060ff233Sopenharmony_ci SoftBusCloseFile(fd); 72060ff233Sopenharmony_ci} 73060ff233Sopenharmony_ci 74060ff233Sopenharmony_ci/** 75060ff233Sopenharmony_ci * @tc.name: SoftBusAdapter_OpenFileWithPermsTest_001 76060ff233Sopenharmony_ci * @tc.desc: softbus open file with perms test 77060ff233Sopenharmony_ci * @tc.type: FUNC 78060ff233Sopenharmony_ci * @tc.require: 79060ff233Sopenharmony_ci */ 80060ff233Sopenharmony_ciHWTEST_F(AdaptorDsoftbusFileTest, SoftBusOpenFileWithPermsTest001, TestSize.Level1) 81060ff233Sopenharmony_ci{ 82060ff233Sopenharmony_ci int32_t fd = 0; 83060ff233Sopenharmony_ci int32_t ret = SoftBusOpenFileWithPerms(NULL, SOFTBUS_O_WRONLY | SOFTBUS_O_CREATE, 84060ff233Sopenharmony_ci SOFTBUS_S_IRUSR | SOFTBUS_S_IWUSR); 85060ff233Sopenharmony_ci EXPECT_EQ(SOFTBUS_INVALID_FD, ret); 86060ff233Sopenharmony_ci fd = SoftBusOpenFileWithPerms("/dev/urandom", SOFTBUS_O_WRONLY | SOFTBUS_O_CREATE, 87060ff233Sopenharmony_ci SOFTBUS_S_IRUSR | SOFTBUS_S_IWUSR); 88060ff233Sopenharmony_ci EXPECT_NE(SOFTBUS_INVALID_FD, fd); 89060ff233Sopenharmony_ci SoftBusCloseFile(fd); 90060ff233Sopenharmony_ci} 91060ff233Sopenharmony_ci 92060ff233Sopenharmony_ci/** 93060ff233Sopenharmony_ci * @tc.name: SoftBusAdapter_PreadFileTest_001 94060ff233Sopenharmony_ci * @tc.desc: pread file test 95060ff233Sopenharmony_ci * @tc.type: FUNC 96060ff233Sopenharmony_ci * @tc.require: 97060ff233Sopenharmony_ci */ 98060ff233Sopenharmony_ciHWTEST_F(AdaptorDsoftbusFileTest, SoftBusPreadFileTest001, TestSize.Level1) 99060ff233Sopenharmony_ci{ 100060ff233Sopenharmony_ci int32_t fd = 0; 101060ff233Sopenharmony_ci uint32_t value = 0; 102060ff233Sopenharmony_ci uint64_t readBytes = 4; 103060ff233Sopenharmony_ci uint64_t offset = 1; 104060ff233Sopenharmony_ci int64_t ret; 105060ff233Sopenharmony_ci fd = SoftBusOpenFile("/dev/urandom", SOFTBUS_O_RDONLY); 106060ff233Sopenharmony_ci EXPECT_NE(SOFTBUS_INVALID_FD, fd); 107060ff233Sopenharmony_ci ret = SoftBusPreadFile(fd, NULL, readBytes, offset); 108060ff233Sopenharmony_ci EXPECT_EQ(SOFTBUS_ERR, ret); 109060ff233Sopenharmony_ci ret = SoftBusPreadFile(fd, &value, readBytes, offset); 110060ff233Sopenharmony_ci EXPECT_NE(SOFTBUS_ERR, ret); 111060ff233Sopenharmony_ci ret = SoftBusPwriteFile(fd, NULL, readBytes, offset); 112060ff233Sopenharmony_ci EXPECT_EQ(SOFTBUS_ERR, ret); 113060ff233Sopenharmony_ci SoftBusCloseFile(fd); 114060ff233Sopenharmony_ci} 115060ff233Sopenharmony_ci 116060ff233Sopenharmony_ci/** 117060ff233Sopenharmony_ci * @tc.name: SoftBusAdapter_MakeDirTest_001 118060ff233Sopenharmony_ci * @tc.desc: make dir test 119060ff233Sopenharmony_ci * @tc.type: FUNC 120060ff233Sopenharmony_ci * @tc.require: 121060ff233Sopenharmony_ci */ 122060ff233Sopenharmony_ciHWTEST_F(AdaptorDsoftbusFileTest, SoftBusMakeDirTest001, TestSize.Level1) 123060ff233Sopenharmony_ci{ 124060ff233Sopenharmony_ci int32_t ret = SoftBusMakeDir(NULL, DEFAULT_NEW_PATH_AUTHORITY); 125060ff233Sopenharmony_ci EXPECT_EQ(SOFTBUS_ERR, ret); 126060ff233Sopenharmony_ci} 127060ff233Sopenharmony_ci 128060ff233Sopenharmony_ci/** 129060ff233Sopenharmony_ci * @tc.name: SoftBusAdapter_GetFileSizeTest_001 130060ff233Sopenharmony_ci * @tc.desc: make dir test 131060ff233Sopenharmony_ci * @tc.type: FUNC 132060ff233Sopenharmony_ci * @tc.require: 133060ff233Sopenharmony_ci */ 134060ff233Sopenharmony_ciHWTEST_F(AdaptorDsoftbusFileTest, SoftBusGetFileSize001, TestSize.Level1) 135060ff233Sopenharmony_ci{ 136060ff233Sopenharmony_ci uint64_t fileSize = 0; 137060ff233Sopenharmony_ci int32_t ret = SoftBusGetFileSize(NULL, &fileSize); 138060ff233Sopenharmony_ci EXPECT_EQ(SOFTBUS_ERR, ret); 139060ff233Sopenharmony_ci ret = SoftBusGetFileSize("/data", &fileSize); 140060ff233Sopenharmony_ci EXPECT_EQ(SOFTBUS_OK, ret); 141060ff233Sopenharmony_ci ret = SoftBusGetFileSize("/dev/test", &fileSize); 142060ff233Sopenharmony_ci EXPECT_EQ(SOFTBUS_ERR, ret); 143060ff233Sopenharmony_ci} 144060ff233Sopenharmony_ci}