• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "target_info.h"
23 namespace OHOS::AppDomainVerify {
24 using namespace testing;
25 using namespace testing::ext;
26 class AppDomainVerifyConvertCallbackTest : public testing::Test {
27 public:
28     static void SetUpTestCase(void);
29     static void TearDownTestCase(void);
30     void SetUp();
31     void TearDown();
32 };
33 
SetUpTestCase(void)34 void AppDomainVerifyConvertCallbackTest::SetUpTestCase(void)
35 {
36 }
37 
TearDownTestCase(void)38 void AppDomainVerifyConvertCallbackTest::TearDownTestCase(void)
39 {
40 }
41 
SetUp(void)42 void AppDomainVerifyConvertCallbackTest::SetUp(void)
43 {
44 }
45 
TearDown(void)46 void AppDomainVerifyConvertCallbackTest::TearDown(void)
47 {
48 }
49 class CallBack : public ConvertCallbackStub {
50 public:
OnConvert(int resCode,TargetInfo & targetInfo)51     void OnConvert(int resCode, TargetInfo& targetInfo) override
52     {
53     }
54 };
55 
56 /**
57  * @tc.name: AppDomainVerifyConvertCallbackStubTest001
58  * @tc.desc: AddTask test
59  * @tc.type: FUNC
60  */
61 HWTEST_F(AppDomainVerifyConvertCallbackTest, AppDomainVerifyConvertCallbackStubTest001, TestSize.Level0)
62 {
63     MessageParcel data;
64     MessageParcel reply;
65     MessageOption option(MessageOption::TF_SYNC);
66 
67     int resCode = 0;
68     OHOS::AAFwk::Want want;
69     TargetInfo targetInfo;
70     WRITE_PARCEL_AND_RETURN_IF_FAIL(InterfaceToken, data, IConvertCallback::GetDescriptor());
71     WRITE_PARCEL_AND_RETURN_IF_FAIL(Int32, data, resCode);
72     WRITE_PARCEL_AND_RETURN_IF_FAIL(Parcelable, data, &targetInfo);
73     sptr<CallBack> callback = new CallBack;
74     auto ret = callback->OnRemoteRequest(
75         static_cast<int32_t>(ConvertCallbackInterfaceCode::ON_CONVERT_CALLBACK), data, reply, option);
76     ASSERT_TRUE(ret == 0);
77 }
78 /**
79  * @tc.name: AppDomainVerifyConvertCallbackStubTest002
80  * @tc.desc: AddTask test without description
81  * @tc.type: FUNC
82  */
83 HWTEST_F(AppDomainVerifyConvertCallbackTest, AppDomainVerifyConvertCallbackStubTest002, TestSize.Level0)
84 {
85     MessageParcel data;
86     MessageParcel reply;
87     MessageOption option(MessageOption::TF_SYNC);
88 
89     int resCode = 0;
90     OHOS::AAFwk::Want want;
91     WRITE_PARCEL_AND_RETURN_IF_FAIL(Int32, data, resCode);
92     WRITE_PARCEL_AND_RETURN_IF_FAIL(Parcelable, data, &want);
93     sptr<CallBack> callback = new CallBack;
94     auto ret = callback->OnRemoteRequest(
95         static_cast<int32_t>(ConvertCallbackInterfaceCode::ON_CONVERT_CALLBACK), data, reply, option);
96     ASSERT_TRUE(ret != 0);
97 }
98 /**
99  * @tc.name: AppDomainVerifyConvertCallbackStubTest003
100  * @tc.desc: AddTask test with wrong code
101  * @tc.type: FUNC
102  */
103 HWTEST_F(AppDomainVerifyConvertCallbackTest, AppDomainVerifyConvertCallbackStubTest003, TestSize.Level0)
104 {
105     MessageParcel data;
106     MessageParcel reply;
107     MessageOption option(MessageOption::TF_SYNC);
108 
109     int resCode = 0;
110     OHOS::AAFwk::Want want;
111     WRITE_PARCEL_AND_RETURN_IF_FAIL(InterfaceToken, data, IConvertCallback::GetDescriptor());
112     WRITE_PARCEL_AND_RETURN_IF_FAIL(Int32, data, resCode);
113     WRITE_PARCEL_AND_RETURN_IF_FAIL(Parcelable, data, &want);
114     sptr<CallBack> callback = new CallBack;
115     auto ret = callback->OnRemoteRequest(100, data, reply, option);
116     ASSERT_TRUE(ret != 0);
117 }
118 /**
119  * @tc.name: AppDomainVerifyConvertCallbackStubTest004
120  * @tc.desc: AddTask test with without want
121  * @tc.type: FUNC
122  */
123 HWTEST_F(AppDomainVerifyConvertCallbackTest, AppDomainVerifyConvertCallbackStubTest004, TestSize.Level0)
124 {
125     MessageParcel data;
126     MessageParcel reply;
127     MessageOption option(MessageOption::TF_SYNC);
128 
129     int resCode = 0;
130     OHOS::AAFwk::Want want;
131     WRITE_PARCEL_AND_RETURN_IF_FAIL(InterfaceToken, data, IConvertCallback::GetDescriptor());
132     WRITE_PARCEL_AND_RETURN_IF_FAIL(Int32, data, resCode);
133     sptr<CallBack> callback = new CallBack;
134     auto ret = callback->OnRemoteRequest(
135         static_cast<int32_t>(ConvertCallbackInterfaceCode::ON_CONVERT_CALLBACK), data, reply, option);
136     ASSERT_TRUE(ret != 0);
137 }
138 /**
139  * @tc.name: AppDomainVerifyConvertCallbackProxyTest001
140  * @tc.desc: AddTask test
141  * @tc.type: FUNC
142  */
143 HWTEST_F(AppDomainVerifyConvertCallbackTest, AppDomainVerifyConvertCallbackProxyTest001, TestSize.Level0)
144 {
145     sptr<ConvertCallbackProxy> proxy = new ConvertCallbackProxy(nullptr);
146     ASSERT_TRUE(proxy != nullptr);
147     int resCode = 0;
148     TargetInfo targetInfo;
149     proxy->OnConvert(resCode, targetInfo);
150 }
151 
152 /**
153  * @tc.name: AppDomainVerifyTargetInfoTest001
154  * @tc.desc: AddTask test
155  * @tc.type: FUNC
156  */
157 HWTEST_F(AppDomainVerifyConvertCallbackTest, AppDomainVerifyTargetInfoTest001, TestSize.Level0)
158 {
159     TargetInfo targetInfo;
160     OHOS::AAFwk::Want want;
161     want.SetUri("hello");
162     targetInfo.targetType = TargetType::ATOMIC_SERVICE;
163     targetInfo.targetWant = want;
164 
165     Parcel parcel;
166     targetInfo.Marshalling(parcel);
167     auto unmarshalling = TargetInfo::Unmarshalling(parcel);
168     ASSERT_TRUE(unmarshalling->targetType == TargetType::ATOMIC_SERVICE);
169     ASSERT_TRUE(unmarshalling->targetWant.GetUri() == Uri("hello"));
170 
171     Parcel parcel1;
172     unmarshalling = TargetInfo::Unmarshalling(parcel1);
173     ASSERT_TRUE(unmarshalling == nullptr);
174     parcel1.WriteInt32(TargetType::ATOMIC_SERVICE);
175     unmarshalling = TargetInfo::Unmarshalling(parcel1);
176     ASSERT_TRUE(unmarshalling == nullptr);
177 }
178 }