From b6c6c3d89bc40c65a699582a9448c2171bce9144 Mon Sep 17 00:00:00 2001 From: dengjun Date: Wed, 7 Feb 2024 11:55:38 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9Endk=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: dengjun --- test/unittest/BUILD.gn | 2 + test/unittest/file_uri_ndk_test/BUILD.gn | 39 +++ .../file_uri_ndk_test/file_uri_ndk_test.cpp | 309 ++++++++++++++++++ test/unittest/resource/file_uri_ndk_test.txt | 13 + test/unittest/resource/ohos_test.xml | 5 + 5 files changed, 368 insertions(+) create mode 100644 test/unittest/file_uri_ndk_test/BUILD.gn create mode 100644 test/unittest/file_uri_ndk_test/file_uri_ndk_test.cpp create mode 100644 test/unittest/resource/file_uri_ndk_test.txt diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 6a4a28f1c..1683754ed 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -17,6 +17,8 @@ group("unittest") { "file_permission_native:file_permission_test", "file_share_native:file_share_test", "file_uri_native:file_uri_test", + "file_uri_ndk_test:file_uri_ndk_test", + "js_file_uri_test:FileUriTest", "remote_file_share:remote_file_share_test", "resource/bundle_dependencies/fileShareA:filesharea", "resource/bundle_dependencies/fileShareB:fileshareb", diff --git a/test/unittest/file_uri_ndk_test/BUILD.gn b/test/unittest/file_uri_ndk_test/BUILD.gn new file mode 100644 index 000000000..4f8846fe6 --- /dev/null +++ b/test/unittest/file_uri_ndk_test/BUILD.gn @@ -0,0 +1,39 @@ +# Copyright (c) 2024 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") +import("//foundation/filemanagement/app_file_service/app_file_service.gni") + +ohos_unittest("file_uri_ndk_test") { + module_out_path = "filemanagement/app_file_service" + include_dirs = [ + "include", + "${app_file_service_path}/interfaces/kits/ndk/fileuri/include", + "//third_party/googletest/include", + ] + resource_config_file = "../resource/ohos_test.xml" + sources = [ "file_uri_ndk_test.cpp" ] + external_deps = [ + "ability_base:zuri", + "app_file_service:fileuri_native", + "app_file_service:ohfileuri", + "c_utils:utils", + "file_api:filemgmt_libn", + "hilog:libhilog", + "init:libbegetutil", + ] + deps = [ + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] +} diff --git a/test/unittest/file_uri_ndk_test/file_uri_ndk_test.cpp b/test/unittest/file_uri_ndk_test/file_uri_ndk_test.cpp new file mode 100644 index 000000000..3fe5aa11c --- /dev/null +++ b/test/unittest/file_uri_ndk_test/file_uri_ndk_test.cpp @@ -0,0 +1,309 @@ +/* + * Copyright (c) 2024 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 "common_func.h" +#include "oh_file_uri.h" +#include "error_code.h" +#include "parameter.h" + +#include +#include +#include +#include + +using namespace testing::ext; +using namespace OHOS::AppFileService; +namespace { +const std::string bundleA = "com.example.filesharea"; +const char *g_fileManagerFullMountEnableParameter = "const.filemanager.full_mount.enable"; +} + +std::string CommonFunc::GetSelfBundleName() +{ + return bundleA; +} + +namespace OHOS::AppFileService::ModuleFileUri { + +static void FreeResult(char *freeResult) +{ + if (freeResult != NULL) { + free(freeResult); + freeResult = NULL; + } +} + +static bool CheckFileManagerFullMountEnable() +{ + char value[] = "false"; + int retSystem = GetParameter(g_fileManagerFullMountEnableParameter, "false", value, sizeof(value)); + if (retSystem > 0 && !std::strcmp(value, "true")) { + return true; + } + GTEST_LOG_(INFO) << "Not supporting all mounts"; + return false; +} + +class NDKFileUriTest : public testing::Test { +public: + static void SetUpTestCase(void) {}; + static void TearDownTestCase(void) {}; + void SetUp() {}; + void TearDown() {}; +}; + +/** + * @tc.number: get_path_from_uri_test_001 + * @tc.name: Test function of OH_FileUri_GetPathFromUri() interface for sandbox uri + * @tc.desc: Set uri and get path + * @tc.type: FUNC + */ +HWTEST_F(NDKFileUriTest, get_path_from_uri_test_001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "get_path_from_uri_test_001 start"; + std::string fileUriStr = "file://" + bundleA + "/data/storage/el2/base/files/GetPathFromUri001.txt"; + const char *fileUri = fileUriStr.c_str(); + const char filePath[] = "/data/storage/el2/base/files/GetPathFromUri001.txt"; + char *result = NULL; + unsigned int length = fileUriStr.size(); + FileManagement_ErrCode ret = OH_FileUri_GetPathFromUri(fileUri, length, &result); + EXPECT_EQ(ret, ERR_OK); + GTEST_LOG_(INFO) << result; + EXPECT_EQ(strcmp(result, filePath), 0); + FreeResult(result); + GTEST_LOG_(INFO) << "get_path_from_uri_test_001 end"; +} + +/** + * @tc.number: get_path_from_uri_test_002 + * @tc.name: Test function of OH_FileUri_GetPathFromUri() interface for document uri + * @tc.desc: Set uri and get path + * @tc.type: FUNC + */ +HWTEST_F(NDKFileUriTest, get_path_from_uri_test_002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "get_path_from_uri_test_002 start"; + const char fileUri[] = "file://docs/storage/Users/currentUser/Documents/GetPathFromUri002.txt"; + if (!CheckFileManagerFullMountEnable()) { + const char filePath[] = "/data/storage/el2/share/r/docs/storage/Users/currentUser/Documents/GetPathFromUri002.txt"; + } else { + const char filePath[] = "/storage/Users/currentUser/Documents/GetPathFromUri002.txt"; + } + char *result = NULL; + unsigned int length = strlen(fileUri); + FileManagement_ErrCode ret = OH_FileUri_GetPathFromUri(fileUri, length, &result); + EXPECT_EQ(ret, ERR_OK); + GTEST_LOG_(INFO) << result; + EXPECT_EQ(strcmp(result, filePath), 0); + FreeResult(result); + GTEST_LOG_(INFO) << "get_path_from_uri_test_002 end"; +} + +/** + * @tc.number: get_path_from_uri_test_003 + * @tc.name: Test function of OH_FileUri_GetPathFromUri() interface for different applications uri + * @tc.desc: Set uri and get path + * @tc.type: FUNC + */ +HWTEST_F(NDKFileUriTest, get_path_from_uri_test_003, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "get_path_from_uri_test_003 start"; + std::string bundleB = "com.example.fileshareb"; + std::string fileUriStr = "file://" + bundleB + "/data/storage/el2/base/files/GetPathFromUri003.txt"; + const char *fileUri = fileUriStr.c_str(); + std::string filePathStr = + "/data/storage/el2/share/r/" + bundleB + "/data/storage/el2/base/files/GetPathFromUri003.txt"; + const char *filePath = filePathStr.c_str(); + char *result = NULL; + unsigned int length = fileUriStr.size(); + FileManagement_ErrCode ret = OH_FileUri_GetPathFromUri(fileUri, length, &result); + EXPECT_EQ(ret, ERR_OK); + GTEST_LOG_(INFO) << result; + EXPECT_EQ(strcmp(result, filePath), 0); + FreeResult(result); + GTEST_LOG_(INFO) << "get_path_from_uri_test_003 end"; +} + +/** + * @tc.number: get_path_from_uri_test_004 + * @tc.name: Test function of OH_FileUri_GetPathFromUri() interface for distributed files uri + * @tc.desc: Set uri and get path + * @tc.type: FUNC + */ +HWTEST_F(NDKFileUriTest, get_path_from_uri_test_004, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "get_path_from_uri_test_004 start"; + std::string fileUriStr = "file://" + bundleA + "/data/storage/el2/distributedfiles/.remote_share/"; + fileUriStr += "data/storage/el2/base/haps/entry/files/GetPathFromUri004.txt"; + fileUriStr += "?networkid=64799ecdf70788e396f454ff4a6e6ae4b09e20227c39c21f6e67a2aacbcef7b9"; + const char *fileUri = fileUriStr.c_str(); + std::string filePathStr = "/data/storage/el2/distributedfiles/.remote_share/"; + filePathStr += "data/storage/el2/base/haps/entry/files/GetPathFromUri004.txt"; + const char *filePath = filePathStr.c_str(); + char *result = NULL; + unsigned int length = fileUriStr.size(); + FileManagement_ErrCode ret = OH_FileUri_GetPathFromUri(fileUri, length, &result); + EXPECT_EQ(ret, ERR_OK); + GTEST_LOG_(INFO) << result; + EXPECT_EQ(strcmp(result, filePath), 0); + FreeResult(result); + GTEST_LOG_(INFO) << "get_path_from_uri_test_004 end"; +} + +/** + * @tc.number: get_path_from_uri_test_005 + * @tc.name: Test function of OH_FileUri_GetPathFromUri() interface for distributed files uri + * @tc.desc: Set uri and get path + * @tc.type: FUNC + */ +HWTEST_F(NDKFileUriTest, get_path_from_uri_test_005, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "get_path_from_uri_test_005 start"; + std::string fileUriStr = "file://docs/storage/Users/currentUser/Documents/GetPathFromUri005.txt"; + fileUriStr += "?networkid=64799ecdf70788e396f454ff4a6e6ae4b09e20227c39c21f6e67a2aacbcef7b9"; + const char *fileUri = fileUriStr.c_str(); + const char filePath[] = "/data/storage/el2/share/r/docs/storage/Users/currentUser/Documents/GetPathFromUri005.txt"; + char *result = NULL; + unsigned int length = fileUriStr.size(); + FileManagement_ErrCode ret = OH_FileUri_GetPathFromUri(fileUri, length, &result); + EXPECT_EQ(ret, ERR_OK); + GTEST_LOG_(INFO) << result; + EXPECT_EQ(strcmp(result, filePath), 0); + FreeResult(result); + GTEST_LOG_(INFO) << "get_path_from_uri_test_005 end"; +} + +/** + * @tc.number: get_path_from_uri_test_006 + * @tc.name: Test function of OH_FileUri_GetPathFromUri() interface for distributed files uri + * @tc.desc: Set uri and get path + * @tc.type: FUNC + */ +HWTEST_F(NDKFileUriTest, get_path_from_uri_test_006, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "get_path_from_uri_test_006 start"; + std::string bundleB = "com.example.fileshareb"; + std::string fileUriStr = "file://" + bundleB + "/data/storage/el2/distributedfiles/.remote_share/"; + fileUriStr += "data/storage/el2/base/haps/entry/files/GetPathFromUri006.txt"; + fileUriStr += "?networkid=64799ecdf70788e396f454ff4a6e6ae4b09e20227c39c21f6e67a2aacbcef7b9"; + const char *fileUri = fileUriStr.c_str(); + std::string filePathUri = "/data/storage/el2/share/r/" + bundleB; + filePathUri += "/data/storage/el2/distributedfiles/.remote_share/"; + filePathUri += "data/storage/el2/base/haps/entry/files/GetPathFromUri006.txt"; + const char *filePath = filePathUri.c_str(); + char *result = NULL; + unsigned int length = fileUriStr.size(); + FileManagement_ErrCode ret = OH_FileUri_GetPathFromUri(fileUri, length, &result); + EXPECT_EQ(ret, ERR_OK); + GTEST_LOG_(INFO) << result; + EXPECT_EQ(strcmp(result, filePath), 0); + FreeResult(result); + GTEST_LOG_(INFO) << "get_path_from_uri_test_006 end"; +} + +/** + * @tc.number: get_uri_from_path_test_001 + * @tc.name: Test function of OH_FileUri_GetUriFromPath() interface for document uri + * @tc.desc: Set path and get uri + * @tc.type: FUNC + */ +HWTEST_F(NDKFileUriTest, get_uri_from_path_test_001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "get_uri_from_path_test_001 start"; + const char filePath[] = "storage/Users/currentUser/Documents/GetPathFromUri001.txt"; + const char fileUri[] = "file://com.example.filesharea/storage/Users/currentUser/Documents/GetPathFromUri001.txt"; + char *result = NULL; + unsigned int length = strlen(filePath); + FileManagement_ErrCode ret = OH_FileUri_GetUriFromPath(filePath, length, &result); + EXPECT_EQ(ret, ERR_OK); + GTEST_LOG_(INFO) << result; + EXPECT_EQ(strcmp(result, fileUri), 0); + FreeResult(result); + GTEST_LOG_(INFO) << "get_uri_from_path_test_001 end"; +} + +/** + * @tc.number: get_full_directory_uri_test_001 + * @tc.name: Test function of OH_FileUri_GetFullDirectoryUri() interface for unknown path + * @tc.desc: Set uri and get full directory uri + * @tc.type: FUNC + */ +HWTEST_F(NDKFileUriTest, get_full_directory_uri_test_001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "get_full_directory_uri_test_001 start"; + const char fileUri[] = "file://docs/storage/Users/currentUser/Documents/GetFullDirectoryUri001.txt"; + char *result = NULL; + unsigned int length = strlen(fileUri); + FileManagement_ErrCode ret = OH_FileUri_GetFullDirectoryUri(fileUri, length, &result); + EXPECT_EQ(ret, ERR_ENOENT); + FreeResult(result); + GTEST_LOG_(INFO) << "get_full_directory_uri_test_001 end"; +} + +/** + * @tc.number: get_full_directory_uri_test_002 + * @tc.name: Test function of OH_FileUri_GetFullDirectoryUri() interface for success + * @tc.desc: Set uri and get full directory uri + * @tc.type: FUNC + */ +HWTEST_F(NDKFileUriTest, get_full_directory_uri_test_002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "get_full_directory_uri_test_002 start"; + const char fileUri[] = "file://com.example.filesharea/data/test/file_uri_ndk_test.txt"; + const char resultUri[] = "file://com.example.filesharea/data/test"; + char *result = NULL; + unsigned int length = strlen(fileUri); + FileManagement_ErrCode ret = OH_FileUri_GetFullDirectoryUri(fileUri, length, &result); + EXPECT_EQ(ret, ERR_OK); + if (result != NULL) { + GTEST_LOG_(INFO) << result; + EXPECT_EQ(strcmp(result, resultUri), 0); + } + FreeResult(result); + GTEST_LOG_(INFO) << "get_full_directory_uri_test_002 end"; +} + +/** + * @tc.number: is_valid_uri_test_001 + * @tc.name: Test function of OH_FileUri_IsValidUri() interface for real uri + * @tc.desc: Set URI and make a judgment + * @tc.type: FUNC + */ +HWTEST_F(NDKFileUriTest, is_valid_uri_test_001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "is_valid_uri_test_001"; + const char fileUri[] = "file://com.demo.a/data/storage/el2/base/files/IsValidUriTest001.txt"; + unsigned int length = strlen(fileUri); + bool flags = OH_FileUri_IsValidUri(fileUri, length); + EXPECT_EQ(flags, true); + GTEST_LOG_(INFO) << "is_valid_uri_test_001"; +} + +/** + * @tc.number: is_valid_uri_test_002 + * @tc.name: Test function of OH_FileUri_IsValidUri() interface for unreal uri + * @tc.desc: Set URI and make a judgment + * @tc.type: FUNC + */ +HWTEST_F(NDKFileUriTest, is_valid_uri_test_002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "is_valid_uri_test_002"; + const char fileUri[] = "com.demo.a/data/storage/el2/base/files/IsValidUriTest002.txt"; + unsigned int length = strlen(fileUri); + bool flags = OH_FileUri_IsValidUri(fileUri, length); + EXPECT_EQ(flags, false); + GTEST_LOG_(INFO) << "is_valid_uri_test_002"; +} + +} // namespace OHOS::AppFileService::ModuleFileUri \ No newline at end of file diff --git a/test/unittest/resource/file_uri_ndk_test.txt b/test/unittest/resource/file_uri_ndk_test.txt new file mode 100644 index 000000000..f9de88b85 --- /dev/null +++ b/test/unittest/resource/file_uri_ndk_test.txt @@ -0,0 +1,13 @@ +# Copyright (c) 2024 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. +file_uri_ndk_test diff --git a/test/unittest/resource/ohos_test.xml b/test/unittest/resource/ohos_test.xml index 87959e606..3802d1bb5 100644 --- a/test/unittest/resource/ohos_test.xml +++ b/test/unittest/resource/ohos_test.xml @@ -44,4 +44,9 @@