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 "token_callback_test.h"
17 #include "string_ex.h"
18 #include "token_callback_stub.h"
19
20 using namespace testing::ext;
21 using namespace OHOS;
22 using namespace OHOS::Security::AccessToken;
23
24 const static int32_t RET_NO_ERROR = 0;
25
SetUpTestCase()26 void TokenCallbackTest::SetUpTestCase()
27 {
28 }
29
TearDownTestCase()30 void TokenCallbackTest::TearDownTestCase()
31 {
32 }
33
SetUp()34 void TokenCallbackTest::SetUp()
35 {
36 }
37
TearDown()38 void TokenCallbackTest::TearDown()
39 {
40 }
41
42 class TestCallBack : public TokenCallbackStub {
43 public:
44 TestCallBack() = default;
45 virtual ~TestCallBack() = default;
46
GrantResultsCallback(const std::vector<std::string> & permissions,const std::vector<int> & grantResults)47 void GrantResultsCallback(
48 const std::vector<std::string> &permissions, const std::vector<int> &grantResults)
49 {
50 GTEST_LOG_(INFO) << "GrantResultsCallback, permissions.size:" << permissions.size() <<
51 ", grantResults.size :" << grantResults.size();
52 }
53 };
54
55 /**
56 * @tc.name: OnRemoteRequest001
57 * @tc.desc: OnRemoteRequest empty.
58 * @tc.type: FUNC
59 * @tc.require: issueI5NU8U
60 */
61 HWTEST_F(TokenCallbackTest, OnRemoteRequest001, TestSize.Level1)
62 {
63 std::vector<std::string> permissions;
64 std::vector<int32_t> grantResults;
65 uint32_t listSize = permissions.size();
66 uint32_t resultSize = grantResults.size();
67
68 TestCallBack callback;
69 MessageParcel data;
70 ASSERT_EQ(true, data.WriteInterfaceToken(ITokenCallback::GetDescriptor()));
71
72 ASSERT_EQ(true, data.WriteUint32(listSize));
73 for (uint32_t i = 0; i < listSize; i++) {
74 ASSERT_EQ(true, data.WriteString16(Str8ToStr16(permissions[i])));
75 }
76
77 ASSERT_EQ(true, data.WriteUint32(resultSize));
78 for (uint32_t i = 0; i < resultSize; i++) {
79 ASSERT_EQ(true, data.WriteInt32(grantResults[i]));
80 }
81
82 MessageParcel reply;
83 MessageOption option(MessageOption::TF_SYNC);
84 ASSERT_EQ(RET_NO_ERROR,
85 callback.OnRemoteRequest(static_cast<uint32_t>(ITokenCallback::GRANT_RESULT_CALLBACK), data, reply, option));
86 }
87
88 /**
89 * @tc.name: OnRemoteRequest002
90 * @tc.desc: OnRemoteRequest not empty.
91 * @tc.type: FUNC
92 * @tc.require: issueI5NU8U
93 */
94 HWTEST_F(TokenCallbackTest, OnRemoteRequest002, TestSize.Level1)
95 {
96 std::vector<std::string> permissions;
97 std::vector<int32_t> grantResults;
98 permissions.emplace_back("ohos.permission.CAMERA");
99 grantResults.emplace_back(0);
100 uint32_t listSize = permissions.size();
101 uint32_t resultSize = grantResults.size();
102
103 TestCallBack callback;
104 MessageParcel data;
105 ASSERT_EQ(true, data.WriteInterfaceToken(ITokenCallback::GetDescriptor()));
106
107 ASSERT_EQ(true, data.WriteUint32(listSize));
108 for (uint32_t i = 0; i < listSize; i++) {
109 ASSERT_EQ(true, data.WriteString16(Str8ToStr16(permissions[i])));
110 }
111
112 ASSERT_EQ(true, data.WriteUint32(resultSize));
113 for (uint32_t i = 0; i < resultSize; i++) {
114 ASSERT_EQ(true, data.WriteInt32(grantResults[i]));
115 }
116
117 MessageParcel reply;
118 MessageOption option(MessageOption::TF_SYNC);
119 ASSERT_EQ(RET_NO_ERROR,
120 callback.OnRemoteRequest(static_cast<uint32_t>(ITokenCallback::GRANT_RESULT_CALLBACK), data, reply, option));
121 }
122