1 /*
2 * Copyright (c) 2021-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 <benchmark/benchmark.h>
17 #include <gtest/gtest.h>
18 #include <string>
19 #include <vector>
20 #include <thread>
21 #undef private
22 #include "accesstoken_kit.h"
23 #include "access_token_error.h"
24 #include "accesstoken_log.h"
25 #include "permission_def.h"
26
27 using namespace std;
28 using namespace testing::ext;
29 using namespace OHOS;
30 using namespace OHOS::Security::AccessToken;
31
32 namespace {
33 int32_t TOKENID = 0;
34
35 static const std::string BENCHMARK_TEST_PERMISSION_NAME_ALPHA = "ohos.permission.ALPHA";
36 PermissionDef PERMISSIONDEF = {
37 .permissionName = "ohos.permission.test1",
38 .bundleName = "accesstoken_test",
39 .grantMode = 1,
40 .label = "label",
41 .labelId = 1,
42 .description = "open the door",
43 .descriptionId = 1,
44 .availableLevel = APL_NORMAL
45 };
46
47 class NapiAtmanagerTest : public benchmark::Fixture {
48 public:
SetUp(const::benchmark::State & state)49 void SetUp(const ::benchmark::State &state)
50 {}
TearDown(const::benchmark::State & state)51 void TearDown(const ::benchmark::State &state)
52 {}
53 };
54
55 /**
56 * @tc.name: VerifyAccessTokenTestCase001
57 * @tc.desc: VerifyAccessToken
58 * @tc.type: FUNC
59 * @tc.require:
60 */
BENCHMARK_F(NapiAtmanagerTest,VerifyAccessTokenTestCase001)61 BENCHMARK_F(NapiAtmanagerTest, VerifyAccessTokenTestCase001)(
62 benchmark::State &st)
63 {
64 GTEST_LOG_(INFO) << "NapiAtmanagerTest VerifyAccessTokenTestCase001 start!";
65 for (auto _ : st) {
66 EXPECT_EQ(AccessTokenKit::VerifyAccessToken(TOKENID, BENCHMARK_TEST_PERMISSION_NAME_ALPHA), -1);
67 }
68 }
69
70 BENCHMARK_REGISTER_F(NapiAtmanagerTest, VerifyAccessTokenTestCase001)->Iterations(100)->
71 Repetitions(3)->ReportAggregatesOnly();
72
73 /**
74 * @tc.name: GetPermissionFlagsTestCase002
75 * @tc.desc: GetPermissionFlags
76 * @tc.type: FUNC
77 * @tc.require:
78 */
BENCHMARK_F(NapiAtmanagerTest,GetPermissionFlagsTestCase002)79 BENCHMARK_F(NapiAtmanagerTest, GetPermissionFlagsTestCase002)(
80 benchmark::State &st)
81 {
82 GTEST_LOG_(INFO) << "NapiAtmanagerTest GetPermissionFlagsTestCase002 start!";
83 for (auto _ : st) {
84 int32_t flag;
85 EXPECT_EQ(AccessTokenKit::GetPermissionFlag(TOKENID, BENCHMARK_TEST_PERMISSION_NAME_ALPHA, flag),
86 AccessTokenError::ERR_PARAM_INVALID);
87 }
88 }
89
90 BENCHMARK_REGISTER_F(NapiAtmanagerTest, GetPermissionFlagsTestCase002)->Iterations(100)->
91 Repetitions(3)->ReportAggregatesOnly();
92
93 /**
94 * @tc.name: GetDefPermissionTestCase003
95 * @tc.desc: GetDefPermission
96 * @tc.type: FUNC
97 * @tc.require:
98 */
BENCHMARK_F(NapiAtmanagerTest,GetDefPermissionTestCase003)99 BENCHMARK_F(NapiAtmanagerTest, GetDefPermissionTestCase003)(
100 benchmark::State &st)
101 {
102 GTEST_LOG_(INFO) << "NapiAtmanagerTest GetDefPermissionTestCase003 start!";
103 for (auto _ : st) {
104 EXPECT_EQ(AccessTokenKit::GetDefPermission(BENCHMARK_TEST_PERMISSION_NAME_ALPHA, PERMISSIONDEF),
105 AccessTokenError::ERR_PERMISSION_NOT_EXIST);
106 }
107 }
108
109 BENCHMARK_REGISTER_F(NapiAtmanagerTest, GetDefPermissionTestCase003)->Iterations(100)->
110 Repetitions(3)->ReportAggregatesOnly();
111
112 /**
113 * @tc.name: RevokeUserGrantedPermissionTestCase004
114 * @tc.desc: RevokeUserGrantedPermission
115 * @tc.type: FUNC
116 * @tc.require:
117 */
BENCHMARK_F(NapiAtmanagerTest,RevokeUserGrantedPermissionTestCase004)118 BENCHMARK_F(NapiAtmanagerTest, RevokeUserGrantedPermissionTestCase004)(
119 benchmark::State &st)
120 {
121 GTEST_LOG_(INFO) << "NapiAtmanagerTest RevokeUserGrantedPermissionTestCase004 start!";
122 for (auto _ : st) {
123 EXPECT_EQ(AccessTokenKit::RevokePermission(TOKENID, BENCHMARK_TEST_PERMISSION_NAME_ALPHA, 0),
124 AccessTokenError::ERR_PARAM_INVALID);
125 }
126 }
127
128 BENCHMARK_REGISTER_F(NapiAtmanagerTest, RevokeUserGrantedPermissionTestCase004)->Iterations(100)->
129 Repetitions(3)->ReportAggregatesOnly();
130
131 /**
132 * @tc.name: GrantPermissionTestCase005
133 * @tc.desc: GrantPermission
134 * @tc.type: FUNC
135 * @tc.require:
136 */
BENCHMARK_F(NapiAtmanagerTest,GrantPermissionTestCase005)137 BENCHMARK_F(NapiAtmanagerTest, GrantPermissionTestCase005)(
138 benchmark::State &st)
139 {
140 GTEST_LOG_(INFO) << "NapiAtmanagerTest GrantPermissionTestCase005 start!";
141 for (auto _ : st) {
142 EXPECT_EQ(AccessTokenKit::GrantPermission(TOKENID, BENCHMARK_TEST_PERMISSION_NAME_ALPHA, 0),
143 AccessTokenError::ERR_PARAM_INVALID);
144 }
145 }
146
147 BENCHMARK_REGISTER_F(NapiAtmanagerTest, GrantPermissionTestCase005)->Iterations(100)->
148 Repetitions(3)->ReportAggregatesOnly();
149 } // namespace
150
151 BENCHMARK_MAIN();