diff --git a/services/accesstokenmanager/test/BUILD.gn b/services/accesstokenmanager/test/BUILD.gn index 14a41d363b5a2bd5864f1c8a78d260684ba8a180..b6080d65759f06bbba886384df9acc4a0dd6b394 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 0000000000000000000000000000000000000000..bd8fa4482511b4fca40fb114b8208dbf300b7368 --- /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 0000000000000000000000000000000000000000..8b5b199e8dae1d577cb8bc15abca4e74ee5f8487 --- /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