From 0211490bdddae38359b16526844b5dc5251506fb Mon Sep 17 00:00:00 2001 From: zhangkaixiang Date: Tue, 25 Oct 2022 12:39:12 +0000 Subject: [PATCH] add TDD testcases for remoteUri Signed-off-by: zhangkaixiang --- .../kits/native/remote_uri/remote_uri.cpp | 2 +- interfaces/test/unittest/BUILD.gn | 17 ++ interfaces/test/unittest/remote_uri/BUILD.gn | 40 ++++ .../unittest/remote_uri/remote_uri_test.cpp | 176 ++++++++++++++++++ .../test/unittest/resource/ohos_test.xml | 22 +++ .../unittest/resource/remote_uri_test.txt | 13 ++ 6 files changed, 269 insertions(+), 1 deletion(-) create mode 100644 interfaces/test/unittest/BUILD.gn create mode 100644 interfaces/test/unittest/remote_uri/BUILD.gn create mode 100644 interfaces/test/unittest/remote_uri/remote_uri_test.cpp create mode 100644 interfaces/test/unittest/resource/ohos_test.xml create mode 100644 interfaces/test/unittest/resource/remote_uri_test.txt diff --git a/interfaces/kits/native/remote_uri/remote_uri.cpp b/interfaces/kits/native/remote_uri/remote_uri.cpp index 3a7958596..dddc68725 100644 --- a/interfaces/kits/native/remote_uri/remote_uri.cpp +++ b/interfaces/kits/native/remote_uri/remote_uri.cpp @@ -89,7 +89,7 @@ bool RemoteUri::IsRemoteUri(const string& path, int &fd, const int& flags) int RemoteUri::ConvertUri(const int &fd, string &remoteUri) { if (fd < 0) { - return EINVAL; + return -EINVAL; } if (fdFromBinder.size() == MAX_URI_SIZE) { diff --git a/interfaces/test/unittest/BUILD.gn b/interfaces/test/unittest/BUILD.gn new file mode 100644 index 000000000..4351bd3fe --- /dev/null +++ b/interfaces/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_uri:remote_uri_test" ] +} diff --git a/interfaces/test/unittest/remote_uri/BUILD.gn b/interfaces/test/unittest/remote_uri/BUILD.gn new file mode 100644 index 000000000..fc1c7f1e7 --- /dev/null +++ b/interfaces/test/unittest/remote_uri/BUILD.gn @@ -0,0 +1,40 @@ +# 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_uri_test") { + module_out_path = "filemanagement/file_api" + + resource_config_file = "../resource/ohos_test.xml" + + sources = [ "remote_uri_test.cpp" ] + + include_dirs = [ + "include", + "//utils/system/safwk/native/include", + "//commonlibrary/c_utils/base/include", + "//foundation/filemanagement/file_api/interfaces/kits/native/remote_uri", + ] + + deps = [ + "//foundation/filemanagement/file_api/interfaces/kits/native:remote_uri_native", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "c_utils:utils", + "ipc:ipc_core", + ] +} diff --git a/interfaces/test/unittest/remote_uri/remote_uri_test.cpp b/interfaces/test/unittest/remote_uri/remote_uri_test.cpp new file mode 100644 index 000000000..39d2951e2 --- /dev/null +++ b/interfaces/test/unittest/remote_uri/remote_uri_test.cpp @@ -0,0 +1,176 @@ +/* + * 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 "remote_uri.h" + +namespace { + using namespace std; + using namespace OHOS::DistributedFS::ModuleRemoteUri; + + class RemoteUriTest : public testing::Test { + public: + static void SetUpTestCase(void) {}; + static void TearDownTestCase() {}; + void SetUp() {}; + void TearDown() {}; + }; + + /** + * @tc.name: Remote_uri_ConvertUri_0000 + * @tc.desc: Test function of ConvertUri() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000HG8M4 + */ + HWTEST_F(RemoteUriTest, Remote_uri_ConvertUri_0000, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteUriTest-begin Remote_uri_ConvertUri_0000"; + int fd = 10; + string remoteUri = ""; + string expectUri = "datashare:////#fdFromBinder=10"; + int ret = RemoteUri::ConvertUri(fd, remoteUri); + EXPECT_TRUE(ret == 0); + EXPECT_TRUE(remoteUri == expectUri); + + fd = -1; + ret = RemoteUri::ConvertUri(fd, remoteUri); + EXPECT_TRUE(ret == -EINVAL); + GTEST_LOG_(INFO) << "RemoteUriTest-end Remote_uri_ConvertUri_0000"; + } + + /** + * @tc.name: Remote_uri_ConvertUri_0001 + * @tc.desc: Test function of ConvertUri() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000HG8M4 + */ + HWTEST_F(RemoteUriTest, Remote_uri_ConvertUri_0001, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteUriTest-begin Remote_uri_ConvertUri_0001"; + const string fileStr = "/data/test/remote_uri_test.txt"; + int maxUriSize = 128; + vectorremoteUriRecords; + vectorfdRecords; + for (int i = 0; i < maxUriSize + 1; i++) { + int fd = open(fileStr.c_str(), O_RDWR); + string remoteUri = ""; + int ret = RemoteUri::ConvertUri(fd, remoteUri); + EXPECT_TRUE(ret == 0); + remoteUriRecords.emplace_back(remoteUri); + fdRecords.emplace_back(fd); + } + for (int i = 0; i < maxUriSize + 1; i++) { + int fd = 0; + bool ret = RemoteUri::IsRemoteUri(remoteUriRecords[i], fd); + EXPECT_TRUE(ret == true); + EXPECT_EQ(fd, fdRecords[i]); + } + GTEST_LOG_(INFO) << "RemoteUriTest-end Remote_uri_ConvertUri_0001"; + } + + /** + * @tc.name: Remote_uri_OpenRemoteUri_0002 + * @tc.desc: Test function of OpenRemoteUri() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000HG8M4 + */ + HWTEST_F(RemoteUriTest, Remote_uri_OpenRemoteUri_0002, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteUriTest-begin Remote_uri_OpenRemoteUri_0002"; + string remoteUri = "datashar:////#fdFromBinder=10"; + int ret = RemoteUri::OpenRemoteUri(remoteUri); + EXPECT_TRUE(ret == -1); + GTEST_LOG_(INFO) << "RemoteUriTest-end Remote_uri_OpenRemoteUri_0002"; + } + + /** + * @tc.name: Remote_uri_OpenRemoteUri_0003 + * @tc.desc: Test function of OpenRemoteUri() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000HG8M4 + */ + HWTEST_F(RemoteUriTest, Remote_uri_OpenRemoteUri_0003, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteUriTest-begin Remote_uri_OpenRemoteUri_0003"; + string remoteUri = "datashare:////fdFromBinder=10"; + int ret = RemoteUri::OpenRemoteUri(remoteUri); + EXPECT_TRUE(ret == -1); + GTEST_LOG_(INFO) << "RemoteUriTest-end Remote_uri_OpenRemoteUri_0003"; + } + + /** + * @tc.name: Remote_uri_OpenRemoteUri_0004 + * @tc.desc: Test function of OpenRemoteUri() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000HG8M4 + */ + HWTEST_F(RemoteUriTest, Remote_uri_OpenRemoteUri_0004, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteUriTest-begin Remote_uri_OpenRemoteUri_0004"; + string remoteUri = "datashare:////#fdFromBinde=10"; + int ret = RemoteUri::OpenRemoteUri(remoteUri); + EXPECT_TRUE(ret == -1); + GTEST_LOG_(INFO) << "RemoteUriTest-end Remote_uri_OpenRemoteUri_0004"; + } + + /** + * @tc.name: Remote_uri_OpenRemoteUri_0005 + * @tc.desc: Test function of OpenRemoteUri() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000HG8M4 + */ + HWTEST_F(RemoteUriTest, Remote_uri_OpenRemoteUri_0005, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteUriTest-begin Remote_uri_OpenRemoteUri0005"; + string remoteUri = "datashare:////#fdFromBinder=10abc"; + int ret = RemoteUri::OpenRemoteUri(remoteUri); + EXPECT_TRUE(ret == -1); + GTEST_LOG_(INFO) << "RemoteUriTest-end Remote_uri_OpenRemoteUri_0005"; + } + + /** + * @tc.name: Remote_uri_IsRemoteUri_006 + * @tc.desc: Test function of IsRemoteUri() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000HG8M4 + */ + HWTEST_F(RemoteUriTest, Remote_uri_IsRemoteUri_006, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteUriTest-begin Remote_uri_IsRemoteUri_0006"; + string remoteUri = "datashare:////#fdFromBinder=10"; + int fd = 0; + (void)RemoteUri::IsRemoteUri(remoteUri, fd, O_RDWR); + EXPECT_TRUE(fd == -1); + GTEST_LOG_(INFO) << "RemoteUriTest-end Remote_uri_IsRemoteUri_0006"; + } +} diff --git a/interfaces/test/unittest/resource/ohos_test.xml b/interfaces/test/unittest/resource/ohos_test.xml new file mode 100644 index 000000000..0cd402707 --- /dev/null +++ b/interfaces/test/unittest/resource/ohos_test.xml @@ -0,0 +1,22 @@ + + + + + + + + diff --git a/interfaces/test/unittest/resource/remote_uri_test.txt b/interfaces/test/unittest/resource/remote_uri_test.txt new file mode 100644 index 000000000..d0a7e644d --- /dev/null +++ b/interfaces/test/unittest/resource/remote_uri_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_uri_test -- Gitee