From 8a0e9dfcdb86aa44a6f85b7a6550e502f0644344 Mon Sep 17 00:00:00 2001 From: "zhangkaixiang5@huawei.com" Date: Mon, 13 Feb 2023 16:41:10 +0800 Subject: [PATCH] Add File Share Unit Test Signed-off-by: zhangkaixiang5@huawei.com --- test/unittest/BUILD.gn | 5 +- test/unittest/file_share_native/BUILD.gn | 30 +++ .../file_share_native/file_share_test.cpp | 234 ++++++++++++++++++ 3 files changed, 268 insertions(+), 1 deletion(-) create mode 100644 test/unittest/file_share_native/BUILD.gn create mode 100644 test/unittest/file_share_native/file_share_test.cpp diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index d81880b40..bce4a610a 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -13,5 +13,8 @@ group("unittest") { testonly = true - deps = [ "remote_file_share:remote_file_share_test" ] + deps = [ + "file_share_native:file_share_test", + "remote_file_share:remote_file_share_test", + ] } diff --git a/test/unittest/file_share_native/BUILD.gn b/test/unittest/file_share_native/BUILD.gn new file mode 100644 index 000000000..e14a6b012 --- /dev/null +++ b/test/unittest/file_share_native/BUILD.gn @@ -0,0 +1,30 @@ +# Copyright (c) 2023 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("file_share_test") { + module_out_path = "filemanagement/app_file_service" + sources = [ "file_share_test.cpp" ] + + external_deps = [ + "ability_base:base", + "ability_base:want", + "ability_base:zuri", + "access_token:libaccesstoken_sdk", + "app_file_service:fileshare_native", + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] +} diff --git a/test/unittest/file_share_native/file_share_test.cpp b/test/unittest/file_share_native/file_share_test.cpp new file mode 100644 index 000000000..59fac0e79 --- /dev/null +++ b/test/unittest/file_share_native/file_share_test.cpp @@ -0,0 +1,234 @@ +/* + * Copyright (c) 2023 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 +#include "accesstoken_kit.h" +#include "file_share.h" +#include "ipc_skeleton.h" + +namespace { + using namespace std; + using namespace OHOS::AppFileService; + using namespace OHOS::Security::AccessToken; + + const int E_OK = 0; + const int E_INVALID_ARGUMENT = 12100002; + + class FileShareTest : public testing::Test { + public: + static void SetUpTestCase(void) {}; + static void TearDownTestCase() {}; + void SetUp() {}; + void TearDown() {}; + }; + + /** + * @tc.name: file_share_test_0000 + * @tc.desc: Test function of CreateShareFile() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H63TL + */ + HWTEST_F(FileShareTest, File_share_CreateShareFile_0000, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileShareTest-begin File_share_CreateShareFile_0000"; + + int32_t uid = -1; + uid = OHOS::IPCSkeleton::GetCallingUid(); + + string bundleNameA = "com.ohos.settingsdata"; + string fileStr = "/data/app/el2/" + to_string(uid) + "/base/" + bundleNameA + "/files/test.txt"; + int32_t fd = open(fileStr.c_str(), O_RDWR | O_CREAT); + ASSERT_TRUE(fd != -1) << "FileShareTest Create File Failed!"; + string uri = "file://" + bundleNameA + "/data/storage/el2/base/files/test.txt"; + + string bundleNameB = "com.ohos.systemui"; + int32_t tokenId = AccessTokenKit::GetHapTokenID(uid, bundleNameB, 0); + + int32_t flag = 3; + int32_t ret = FileShare::CreateShareFile(uri, tokenId, flag); + EXPECT_EQ(ret, E_OK); + GTEST_LOG_(INFO) << "FileShareTest-end File_share_CreateShareFile_0000"; + } + + /** + * @tc.name: file_share_test_0001 + * @tc.desc: Test function of CreateShareFile() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H63TL + */ + HWTEST_F(FileShareTest, File_share_CreateShareFile_0001, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileShareTest-begin File_share_CreateShareFile_0001"; + + int32_t uid = -1; + uid = OHOS::IPCSkeleton::GetCallingUid(); + + string bundleNameA = "com.ohos.settingsdata"; + string fileStr = "/data/app/el2/" + to_string(uid) + "/base/" + bundleNameA + "/files/test.txt"; + int32_t fd = open(fileStr.c_str(), O_RDWR | O_CREAT); + ASSERT_TRUE(fd != -1) << "FileShareTest Create File Failed!"; + string uri = "file://" + bundleNameA + "/data/test/el2/base/files/test.txt"; + + string bundleNameB = "com.ohos.systemui"; + int32_t tokenId = AccessTokenKit::GetHapTokenID(uid, bundleNameB, 0); + + int32_t flag = 3; + int32_t ret = FileShare::CreateShareFile(uri, tokenId, flag); + EXPECT_EQ(ret, -EINVAL); + GTEST_LOG_(INFO) << "FileShareTest-end File_share_CreateShareFile_0001"; + } + + /** + * @tc.name: file_share_test_0002 + * @tc.desc: Test function of CreateShareFile() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H63TL + */ + HWTEST_F(FileShareTest, File_share_CreateShareFile_0002, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileShareTest-begin File_share_CreateShareFile_0002"; + int32_t uid = -1; + uid = OHOS::IPCSkeleton::GetCallingUid(); + + string bundleNameA = "com.ohos.settingsdata"; + string fileStr = "/data/app/el2/" + to_string(uid) + "/base/" + bundleNameA + "/files/test.txt"; + int32_t fd = open(fileStr.c_str(), O_RDWR | O_CREAT); + ASSERT_TRUE(fd != -1) << "FileShareTest Create File Failed!"; + string uri = "file://" + bundleNameA + "/data/storage/el2/base/files/test.txt"; + int32_t tokenId = 100; + + int32_t flag = 3; + int32_t ret = FileShare::CreateShareFile(uri, tokenId, flag); + EXPECT_EQ(ret, E_INVALID_ARGUMENT); + GTEST_LOG_(INFO) << "FileShareTest-end File_share_CreateShareFile_0002"; + } + + /** + * @tc.name: file_share_test_0003 + * @tc.desc: Test function of CreateShareFile() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H63TL + */ + HWTEST_F(FileShareTest, File_share_CreateShareFile_0003, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileShareTest-begin File_share_CreateShareFile_0003"; + int32_t uid = -1; + uid = OHOS::IPCSkeleton::GetCallingUid(); + + string bundleNameA = "com.ohos.settingsdata"; + string fileStr = "/data/app/el2/" + to_string(uid) + "/base/" + bundleNameA + "/files/test.txt"; + int32_t fd = open(fileStr.c_str(), O_RDWR | O_CREAT); + ASSERT_TRUE(fd != -1) << "FileShareTest Create File Failed!"; + string uri = ""; + + string bundleNameB = "com.ohos.systemui"; + int32_t tokenId = AccessTokenKit::GetHapTokenID(uid, bundleNameB, 0); + + int32_t flag = 3; + int32_t ret = FileShare::CreateShareFile(uri, tokenId, flag); + EXPECT_EQ(ret, -EINVAL); + GTEST_LOG_(INFO) << "FileShareTest-end File_share_CreateShareFile_0003"; + } + + /** + * @tc.name: file_share_test_0004 + * @tc.desc: Test function of CreateShareFile() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H63TL + */ + HWTEST_F(FileShareTest, File_share_CreateShareFile_0004, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileShareTest-begin File_share_CreateShareFile_0004"; + int32_t uid = -1; + uid = OHOS::IPCSkeleton::GetCallingUid(); + + string bundleNameA = "com.ohos.settingsdata"; + string fileStr = "/data/app/el2/" + to_string(uid) + "/base/" + bundleNameA + "/files/test.txt"; + int32_t fd = open(fileStr.c_str(), O_RDWR | O_CREAT); + ASSERT_TRUE(fd != -1) << "FileShareTest Create File Failed!"; + string uri = ""; + + string bundleNameB = "com.ohos.systemui"; + int32_t tokenId = AccessTokenKit::GetHapTokenID(uid, bundleNameB, 0); + + int32_t flag = 4; + int32_t ret = FileShare::CreateShareFile(uri, tokenId, flag); + EXPECT_EQ(ret, -EINVAL); + GTEST_LOG_(INFO) << "FileShareTest-end File_share_CreateShareFile_0004"; + } + + /** + * @tc.name: file_share_test_0005 + * @tc.desc: Test function of DeleteShareFile() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H63TL + */ + HWTEST_F(FileShareTest, File_share_DeleteShareFile_0005, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileShareTest-begin File_share_DeleteShareFile_0005"; + int32_t uid = -1; + uid = OHOS::IPCSkeleton::GetCallingUid(); + + string bundleNameB = "com.ohos.systemui"; + int32_t tokenId = AccessTokenKit::GetHapTokenID(uid, bundleNameB, 0); + vector sharePathList; + string bundleNameA = "com.ohos.settingsdata"; + string uri = "file://" + bundleNameA + "/data/storage/el2/base/files/test.txt"; + sharePathList.push_back(uri); + int32_t ret = FileShare::DeleteShareFile(tokenId, sharePathList); + EXPECT_EQ(ret, E_OK); + GTEST_LOG_(INFO) << "FileShareTest-end File_share_DeleteShareFile_0005"; + } + + /** + * @tc.name: file_share_test_0006 + * @tc.desc: Test function of DeleteShareFile() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H63TL + */ + HWTEST_F(FileShareTest, File_share_DeleteShareFile_0006, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileShareTest-begin File_share_DeleteShareFile_0006"; + int32_t tokenId = 104; + vector sharePathList; + string bundleNameA = "com.ohos.settingsdata"; + string uri = "file://" + bundleNameA + "/data/storage/el2/base/files/test.txt"; + sharePathList.push_back(uri); + int32_t ret = FileShare::DeleteShareFile(tokenId, sharePathList); + EXPECT_EQ(ret, -EINVAL); + GTEST_LOG_(INFO) << "FileShareTest-end File_share_DeleteShareFile_0006"; + } +} \ No newline at end of file -- Gitee