1 /*
2 * Copyright (C) 2023 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 #include "gtest/gtest.h"
16 #include <memory>
17 #include "mock_constant.h"
18 #include "convert_callback_stub.h"
19 #include "convert_callback_proxy.h"
20 #include "convert_callback_interface_code.h"
21 #include "app_domain_verify_parcel_util.h"
22 namespace OHOS::AppDomainVerify {
23 using namespace testing;
24 using namespace testing::ext;
25 class AppDomainVerifyConvertCallbackTest : public testing::Test {
26 public:
27 static void SetUpTestCase(void);
28 static void TearDownTestCase(void);
29 void SetUp();
30 void TearDown();
31 };
32
SetUpTestCase(void)33 void AppDomainVerifyConvertCallbackTest::SetUpTestCase(void)
34 {
35 }
36
TearDownTestCase(void)37 void AppDomainVerifyConvertCallbackTest::TearDownTestCase(void)
38 {
39 }
40
SetUp(void)41 void AppDomainVerifyConvertCallbackTest::SetUp(void)
42 {
43 }
44
TearDown(void)45 void AppDomainVerifyConvertCallbackTest::TearDown(void)
46 {
47 }
48 class CallBack : public ConvertCallbackStub {
49 public:
OnConvert(int resCode,AAFwk::Want & want)50 void OnConvert(int resCode, AAFwk::Want& want) override
51 {
52 }
53 };
54 /**
55 * @tc.name: AppDomainVerifyConvertCallbackStubTest001
56 * @tc.desc: AddTask test
57 * @tc.type: FUNC
58 */
59 HWTEST_F(AppDomainVerifyConvertCallbackTest, AppDomainVerifyConvertCallbackStubTest001, TestSize.Level0)
60 {
61 MessageParcel data;
62 MessageParcel reply;
63 MessageOption option(MessageOption::TF_SYNC);
64
65 int resCode = 0;
66 OHOS::AAFwk::Want want;
67 WRITE_PARCEL_AND_RETURN_IF_FAIL(InterfaceToken, data, IConvertCallback::GetDescriptor());
68 WRITE_PARCEL_AND_RETURN_IF_FAIL(Int32, data, resCode);
69 WRITE_PARCEL_AND_RETURN_IF_FAIL(Parcelable, data, &want);
70 sptr<CallBack> callback = new CallBack;
71 auto ret = callback->OnRemoteRequest(
72 static_cast<int32_t>(ConvertCallbackInterfaceCode::ON_CONVERT_CALLBACK), data, reply, option);
73 ASSERT_TRUE(ret == 0);
74 }
75 /**
76 * @tc.name: AppDomainVerifyConvertCallbackStubTest002
77 * @tc.desc: AddTask test without description
78 * @tc.type: FUNC
79 */
80 HWTEST_F(AppDomainVerifyConvertCallbackTest, AppDomainVerifyConvertCallbackStubTest002, TestSize.Level0)
81 {
82 MessageParcel data;
83 MessageParcel reply;
84 MessageOption option(MessageOption::TF_SYNC);
85
86 int resCode = 0;
87 OHOS::AAFwk::Want want;
88 WRITE_PARCEL_AND_RETURN_IF_FAIL(Int32, data, resCode);
89 WRITE_PARCEL_AND_RETURN_IF_FAIL(Parcelable, data, &want);
90 sptr<CallBack> callback = new CallBack;
91 auto ret = callback->OnRemoteRequest(
92 static_cast<int32_t>(ConvertCallbackInterfaceCode::ON_CONVERT_CALLBACK), data, reply, option);
93 ASSERT_TRUE(ret != 0);
94 }
95 /**
96 * @tc.name: AppDomainVerifyConvertCallbackStubTest003
97 * @tc.desc: AddTask test with wrong code
98 * @tc.type: FUNC
99 */
100 HWTEST_F(AppDomainVerifyConvertCallbackTest, AppDomainVerifyConvertCallbackStubTest003, TestSize.Level0)
101 {
102 MessageParcel data;
103 MessageParcel reply;
104 MessageOption option(MessageOption::TF_SYNC);
105
106 int resCode = 0;
107 OHOS::AAFwk::Want want;
108 WRITE_PARCEL_AND_RETURN_IF_FAIL(InterfaceToken, data, IConvertCallback::GetDescriptor());
109 WRITE_PARCEL_AND_RETURN_IF_FAIL(Int32, data, resCode);
110 WRITE_PARCEL_AND_RETURN_IF_FAIL(Parcelable, data, &want);
111 sptr<CallBack> callback = new CallBack;
112 auto ret = callback->OnRemoteRequest(100, data, reply, option);
113 ASSERT_TRUE(ret != 0);
114 }
115 /**
116 * @tc.name: AppDomainVerifyConvertCallbackStubTest004
117 * @tc.desc: AddTask test with without want
118 * @tc.type: FUNC
119 */
120 HWTEST_F(AppDomainVerifyConvertCallbackTest, AppDomainVerifyConvertCallbackStubTest004, TestSize.Level0)
121 {
122 MessageParcel data;
123 MessageParcel reply;
124 MessageOption option(MessageOption::TF_SYNC);
125
126 int resCode = 0;
127 OHOS::AAFwk::Want want;
128 WRITE_PARCEL_AND_RETURN_IF_FAIL(InterfaceToken, data, IConvertCallback::GetDescriptor());
129 WRITE_PARCEL_AND_RETURN_IF_FAIL(Int32, data, resCode);
130 sptr<CallBack> callback = new CallBack;
131 auto ret = callback->OnRemoteRequest(
132 static_cast<int32_t>(ConvertCallbackInterfaceCode::ON_CONVERT_CALLBACK), data, reply, option);
133 ASSERT_TRUE(ret != 0);
134 }
135 /**
136 * @tc.name: AppDomainVerifyConvertCallbackProxyTest001
137 * @tc.desc: AddTask test
138 * @tc.type: FUNC
139 */
140 HWTEST_F(AppDomainVerifyConvertCallbackTest, AppDomainVerifyConvertCallbackProxyTest001, TestSize.Level0)
141 {
142 sptr<ConvertCallbackProxy> proxy = new ConvertCallbackProxy(nullptr);
143 int resCode = 0;
144 OHOS::AAFwk::Want want;
145 proxy->OnConvert(resCode, want);
146 }
147 }