diff --git a/bundle.json b/bundle.json index 571944b86baa272f99cdf0dacf95f3a402faaac4..dab8e4353e7c78b8dd8e570bf17f599df6c98afe 100644 --- a/bundle.json +++ b/bundle.json @@ -41,7 +41,8 @@ } ], "test": [ - "//foundation/filemanagement/app_file_service/test/fuzztest:fuzztest" + "//foundation/filemanagement/app_file_service/test/fuzztest:fuzztest", + "//foundation/filemanagement/app_file_service/test/unittest:unittest" ] } } diff --git a/services/remote_file_share/src/remote_file_share.cpp b/services/remote_file_share/src/remote_file_share.cpp index 2e8eb47ca8e2fb13667e008a0fd991cb0c594aaa..c994779fbb49eaca0812f608d17b1d855aad48ea 100644 --- a/services/remote_file_share/src/remote_file_share.cpp +++ b/services/remote_file_share/src/remote_file_share.cpp @@ -77,7 +77,7 @@ static std::string GetFileName(const int &fd) } std::string fileName = filePath; - int firstSlash = fileName.rfind("/"); + std::size_t firstSlash = fileName.rfind("/"); if (firstSlash == fileName.npos) { LOGE("RemoteFileShare::GetFileName, get error path with %{public}s", fileName.c_str()); return ""; @@ -108,7 +108,7 @@ static std::string GetLowerSharePath(const int &userId, const std::string &packa return LOWER_SHARE_PATH_HEAD + std::to_string(userId) + LOWER_SHARE_PATH_MID + packageName; } -static bool DeleteShareDir(const std::string PACKAGE_PATH, const std::string SHARE_PATH) +static bool DeleteShareDir(const std::string &PACKAGE_PATH, const std::string &SHARE_PATH) { bool result = true; if (access(SHARE_PATH.c_str(), F_OK) == 0) { diff --git a/test/fuzztest/remotefileshare_fuzzer/remotefileshare_fuzzer.cpp b/test/fuzztest/remotefileshare_fuzzer/remotefileshare_fuzzer.cpp index 560b6123d224907b98b527ddaecd1836677bd9df..c47b367c473b69a2be68d55936ff1b345cd18ad4 100644 --- a/test/fuzztest/remotefileshare_fuzzer/remotefileshare_fuzzer.cpp +++ b/test/fuzztest/remotefileshare_fuzzer/remotefileshare_fuzzer.cpp @@ -48,7 +48,7 @@ bool ShareFilePathIoctlFdAndCidFuzzTest(const uint8_t* data, size_t size) int32_t ret = 0; int32_t dirFd; - if (size <= 0) { + if (size == 0) { return false; } @@ -91,7 +91,7 @@ bool ShareFilePathIoctlCidFuzzTest(const uint8_t* data, size_t size) int32_t dirFd; int32_t srcFd; - if (size <= 0) { + if (size == 0) { return false; } @@ -142,7 +142,7 @@ bool ShareFilePathIoctlFdFuzzTest(const uint8_t* data, size_t size) int32_t ret = 0; int32_t dirFd; - if (size <= 0) { + if (size == 0) { return false; } diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..d81880b40dc7bac6fbbf07567b4158fb81ce316b --- /dev/null +++ b/test/unittest/BUILD.gn @@ -0,0 +1,17 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +group("unittest") { + testonly = true + deps = [ "remote_file_share:remote_file_share_test" ] +} diff --git a/test/unittest/remote_file_share/BUILD.gn b/test/unittest/remote_file_share/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..a9c46ef1dbf2125968bc61c1a2661dc896cefd30 --- /dev/null +++ b/test/unittest/remote_file_share/BUILD.gn @@ -0,0 +1,34 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") + +ohos_unittest("remote_file_share_test") { + module_out_path = "filemanagement/app_file_service" + + resource_config_file = "../resource/ohos_test.xml" + + sources = [ "remote_file_share_test.cpp" ] + + include_dirs = [ + "include", + "../../../interfaces/innerkits/remote_file_share/native", + "//utils/system/safwk/native/include", + "//commonlibrary/c_utils/base/include", + ] + + deps = [ + "../../../interfaces/innerkits/remote_file_share/native:remote_file_share_native", + "//third_party/googletest:gtest_main", + ] +} diff --git a/test/unittest/remote_file_share/remote_file_share_test.cpp b/test/unittest/remote_file_share/remote_file_share_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34c67d07e3d48d3c5faa68bf4f9274ffc5a6de9e --- /dev/null +++ b/test/unittest/remote_file_share/remote_file_share_test.cpp @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "remote_file_share.h" + +namespace { + using namespace std; + using namespace OHOS::AppFileService::ModuleRemoteFileShare; + + const int E_INVALID_ARGUMENT = 22; + const int E_OK = 0; + + class RemoteFileShareTest : public testing::Test { + public: + static void SetUpTestCase(void) {}; + static void TearDownTestCase() {}; + void SetUp() {}; + void TearDown() {}; + }; + + /** + * @tc.name: remote_file_share_test_0000 + * @tc.desc: Test function of RemoteFileShare() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H63TL + */ + HWTEST_F(RemoteFileShareTest, Remote_file_share_RemoteFileShare_0000, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_RemoteFileShare_0000"; + RemoteFileShare* test = new RemoteFileShare; + ASSERT_TRUE(test != nullptr) << "RemoteFileShare Construct Failed!"; + delete test; + GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_RemoteFileShare_0000"; + } + + /** + * @tc.name: remote_file_share_test_0001 + * @tc.desc: Test function of CreateSharePath() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H63TL + */ + HWTEST_F(RemoteFileShareTest, Remote_file_share_CreateSharePath_0001, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_CreateSharePath_0001"; + const int fd = -1; + const int userId = 100; + const string deviceId = "0"; + string sharePath = ""; + int ret = RemoteFileShare::CreateSharePath(fd, sharePath, userId, deviceId); + EXPECT_EQ(ret, E_INVALID_ARGUMENT); + GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_CreateSharePath_0001"; + } + + /** + * @tc.name: remote_file_share_test_0002 + * @tc.desc: Test function of CreateSharePath() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H63TL + */ + HWTEST_F(RemoteFileShareTest, Remote_file_share_CreateSharePath_0002, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_CreateSharePath_0002"; + const int fd = 10; + const int userId = 90; + const string deviceId = "0"; + string sharePath = ""; + int ret = RemoteFileShare::CreateSharePath(fd, sharePath, userId, deviceId); + EXPECT_EQ(ret, E_INVALID_ARGUMENT); + GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_CreateSharePath_0002"; + } + + /** + * @tc.name: remote_file_share_test_0003 + * @tc.desc: Test function of CreateSharePath() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H63TL + */ + HWTEST_F(RemoteFileShareTest, Remote_file_share_CreateSharePath_0003, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_CreateSharePath_0003"; + const int fd = 10; + const int userId = 100; + const string deviceId = "00"; + string sharePath = ""; + int ret = RemoteFileShare::CreateSharePath(fd, sharePath, userId, deviceId); + EXPECT_EQ(ret, E_INVALID_ARGUMENT); + GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_CreateSharePath_0003"; + } + + /** + * @tc.name: remote_file_share_test_0004 + * @tc.desc: Test function of CreateSharePath() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H63TL + */ + HWTEST_F(RemoteFileShareTest, Remote_file_share_CreateSharePath_0004, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_CreateSharePath_0004"; + const string fileStr = "/data/test/remote_file_share_test.txt"; + int fd = open(fileStr.c_str(), O_RDWR); + ASSERT_TRUE(fd != -1) << "RemoteFileShareTest Create File Failed!"; + const int userId = 100; + const string deviceId = "0"; + string sharePath = ""; + int ret = RemoteFileShare::CreateSharePath(fd, sharePath, userId, deviceId); + close(fd); + EXPECT_EQ(ret, E_OK); + GTEST_LOG_(INFO) << "RemoteFileShareTest Create Share Path " << sharePath; + GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_CreateSharePath_0004"; + } +} diff --git a/test/unittest/resource/ohos_test.xml b/test/unittest/resource/ohos_test.xml new file mode 100644 index 0000000000000000000000000000000000000000..966c8ea6fe8417224b00e767934a6637eddf5157 --- /dev/null +++ b/test/unittest/resource/ohos_test.xml @@ -0,0 +1,22 @@ + + + + + + + + diff --git a/test/unittest/resource/remote_file_share_test.txt b/test/unittest/resource/remote_file_share_test.txt new file mode 100644 index 0000000000000000000000000000000000000000..c658b9b65d5f8b5f3a7e5c3c3e7caf5caa8f7672 --- /dev/null +++ b/test/unittest/resource/remote_file_share_test.txt @@ -0,0 +1,13 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +remote_file_share_test