1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "permission_grant_event_test.h"
17 
18 #define private public
19 #include "permission_grant_event.h"
20 #undef private
21 
22 using namespace testing::ext;
23 using namespace OHOS::Security::AccessToken;
24 
25 namespace {
26 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
27     LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "PermissionGrantEventTest"};
28 }
29 
SetUpTestCase()30 void PermissionGrantEventTest::SetUpTestCase()
31 {}
32 
TearDownTestCase()33 void PermissionGrantEventTest::TearDownTestCase()
34 {
35     sleep(3); // delay 3 minutes
36 }
37 
SetUp()38 void PermissionGrantEventTest::SetUp()
39 {}
40 
TearDown()41 void PermissionGrantEventTest::TearDown()
42 {}
43 
44 /**
45  * @tc.name: NotifyPermGrantStoreResult001
46  * @tc.desc: test notify permssion grant event success
47  * @tc.type: FUNC
48  * @tc.require:issueI5OOPG
49  */
50 HWTEST_F(PermissionGrantEventTest, NotifyPermGrantStoreResult001, TestSize.Level1)
51 {
52     ACCESSTOKEN_LOG_INFO(LABEL, "NotifyPermGrantStoreResult001!");
53     AccessTokenID tokenID = 0x100000;
54     std::string permissionName = "testpremission";
55     uint64_t time;
56 
57     PermissionGrantEvent eventHandler;
58     eventHandler.AddEvent(tokenID, permissionName, time);
59 
60     // larger than grant timestamp
61     eventHandler.NotifyPermGrantStoreResult(true, time + 1);
62 
63     ASSERT_EQ(eventHandler.permGrantEventList_.size(), static_cast<uint32_t>(0));
64 }
65 
66 /**
67  * @tc.name: NotifyPermGrantStoreResult002
68  * @tc.desc: test notify permssion grant event failed
69  * @tc.type: FUNC
70  * @tc.require:issueI5OOPG
71  */
72 HWTEST_F(PermissionGrantEventTest, NotifyPermGrantStoreResult002, TestSize.Level1)
73 {
74     ACCESSTOKEN_LOG_INFO(LABEL, "NotifyPermGrantStoreResult002!");
75     AccessTokenID tokenID = 0x100000;
76     std::string permissionName = "testpremission";
77     uint64_t time;
78 
79     PermissionGrantEvent eventHandler;
80     eventHandler.AddEvent(tokenID, permissionName, time);
81 
82     // larger than grant timestamp
83     eventHandler.NotifyPermGrantStoreResult(false, time + 1);
84 
85     ASSERT_EQ(eventHandler.permGrantEventList_.size(), static_cast<uint32_t>(0));
86 }
87 
88 /**
89  * @tc.name: NotifyPermGrantStoreResult003
90  * @tc.desc: test notify permssion grant event success, but timestamp is less than add timestamp
91  * @tc.type: FUNC
92  * @tc.require:issueI5OOPG
93  */
94 HWTEST_F(PermissionGrantEventTest, NotifyPermGrantStoreResult003, TestSize.Level1)
95 {
96     ACCESSTOKEN_LOG_INFO(LABEL, "NotifyPermGrantStoreResult003!");
97     AccessTokenID tokenID = 0x100000;
98     std::string permissionName = "testpremission";
99     uint64_t time;
100 
101     PermissionGrantEvent eventHandler;
102     eventHandler.AddEvent(tokenID, permissionName, time);
103 
104     // less than grant timestamp
105     eventHandler.NotifyPermGrantStoreResult(true, time - 1);
106 
107     ASSERT_EQ(eventHandler.permGrantEventList_.size(), static_cast<uint32_t>(1));
108 }
109