From 98f142542de657c3cac09d3c68196a026ba73a88 Mon Sep 17 00:00:00 2001 From: libing23 Date: Tue, 20 Sep 2022 16:35:42 +0800 Subject: [PATCH] fixed 16130bb from https://gitee.com/libing23/security_access_token/pulls/592 add permission grant unittest Signed-off-by: libing23 --- services/accesstokenmanager/test/BUILD.gn | 1 + .../cpp/src/permission_grant_event_test.cpp | 105 ++++++++++++++++++ .../cpp/src/permission_grant_event_test.h | 38 +++++++ 3 files changed, 144 insertions(+) create mode 100644 services/accesstokenmanager/test/unittest/cpp/src/permission_grant_event_test.cpp create mode 100644 services/accesstokenmanager/test/unittest/cpp/src/permission_grant_event_test.h diff --git a/services/accesstokenmanager/test/BUILD.gn b/services/accesstokenmanager/test/BUILD.gn index 14a41d363..b6080d657 100644 --- a/services/accesstokenmanager/test/BUILD.gn +++ b/services/accesstokenmanager/test/BUILD.gn @@ -39,6 +39,7 @@ ohos_unittest("libaccesstoken_manager_service_standard_test") { sources = [ "unittest/cpp/src/accesstoken_info_manager_test.cpp", "unittest/cpp/src/native_token_receptor_test.cpp", + "unittest/cpp/src/permission_grant_event_test.cpp", ] cflags_cc = [ "-DHILOG_ENABLE" ] diff --git a/services/accesstokenmanager/test/unittest/cpp/src/permission_grant_event_test.cpp b/services/accesstokenmanager/test/unittest/cpp/src/permission_grant_event_test.cpp new file mode 100644 index 000000000..bd8fa4482 --- /dev/null +++ b/services/accesstokenmanager/test/unittest/cpp/src/permission_grant_event_test.cpp @@ -0,0 +1,105 @@ +/* + * 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 "permission_grant_event_test.h" + +#define private public +#include "permission_grant_event.h" +#undef private + +using namespace testing::ext; +using namespace OHOS::Security::AccessToken; + +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "PermissionGrantEventTest"}; +} + +void PermissionGrantEventTest::SetUpTestCase() +{} + +void PermissionGrantEventTest::TearDownTestCase() +{} + +void PermissionGrantEventTest::SetUp() +{} + +void PermissionGrantEventTest::TearDown() +{} + +/** + * @tc.name: NotifyPermGrantStoreResult001 + * @tc.desc: test notify permssion grant event success + * @tc.type: FUNC + * @tc.require:issueI5OOPG + */ +HWTEST_F(PermissionGrantEventTest, NotifyPermGrantStoreResult001, TestSize.Level1) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "NotifyPermGrantStoreResult001!"); + AccessTokenID tokenID = 0x100000; + std::string permissionName = "testpremission"; + uint64_t time; + + PermissionGrantEvent eventHandler; + eventHandler.AddEvent(tokenID, permissionName, time); + + // larger than grant timestamp + eventHandler.NotifyPermGrantStoreResult(true, time + 1); + + ASSERT_EQ(eventHandler.permGrantEventList_.size(), 0); +} + +/** + * @tc.name: NotifyPermGrantStoreResult002 + * @tc.desc: test notify permssion grant event failed + * @tc.type: FUNC + * @tc.require:issueI5OOPG + */ +HWTEST_F(PermissionGrantEventTest, NotifyPermGrantStoreResult002, TestSize.Level1) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "NotifyPermGrantStoreResult002!"); + AccessTokenID tokenID = 0x100000; + std::string permissionName = "testpremission"; + uint64_t time; + + PermissionGrantEvent eventHandler; + eventHandler.AddEvent(tokenID, permissionName, time); + + // larger than grant timestamp + eventHandler.NotifyPermGrantStoreResult(false, time + 1); + + ASSERT_EQ(eventHandler.permGrantEventList_.size(), 0); +} + +/** + * @tc.name: NotifyPermGrantStoreResult003 + * @tc.desc: test notify permssion grant event success, but timestamp is less than add timestamp + * @tc.type: FUNC + * @tc.require:issueI5OOPG + */ +HWTEST_F(PermissionGrantEventTest, NotifyPermGrantStoreResult003, TestSize.Level1) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "NotifyPermGrantStoreResult003!"); + AccessTokenID tokenID = 0x100000; + std::string permissionName = "testpremission"; + uint64_t time; + + PermissionGrantEvent eventHandler; + eventHandler.AddEvent(tokenID, permissionName, time); + + // less than grant timestamp + eventHandler.NotifyPermGrantStoreResult(true, time - 1); + + ASSERT_EQ(eventHandler.permGrantEventList_.size(), 1); +} diff --git a/services/accesstokenmanager/test/unittest/cpp/src/permission_grant_event_test.h b/services/accesstokenmanager/test/unittest/cpp/src/permission_grant_event_test.h new file mode 100644 index 000000000..8b5b199e8 --- /dev/null +++ b/services/accesstokenmanager/test/unittest/cpp/src/permission_grant_event_test.h @@ -0,0 +1,38 @@ +/* + * 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. + */ + +#ifndef PERMISSION_GRANT_EVENT_TEST_H +#define PERMISSION_GRANT_EVENT_TEST_H + +#include +#include "accesstoken_log.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +class PermissionGrantEventTest : public testing::Test { +public: + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp(); + + void TearDown(); +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS +#endif // PERMISSION_GRANT_EVENT_TEST_H \ No newline at end of file -- Gitee