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 <gtest/gtest.h>
17
18 #include "account_log_wrapper.h"
19 #include "app_account_common.h"
20 #define private public
21 #ifdef HAS_CES_PART
22 #include "common_event_manager.h"
23 #include "common_event_support.h"
24 #endif // HAS_CES_PART
25 #include "app_account_common_event_observer.h"
26 #include "app_account_event_stub.h"
27 #include "app_account_event_proxy.h"
28 #undef private
29
30 using namespace testing::ext;
31 using namespace OHOS;
32 using namespace OHOS::AccountSA;
33
34 bool g_status = false;
35 namespace {
36 uint32_t INVALID_CODE = -1;
37 }
38
39 class MockAppAccountEventStub : public AppAccountEventStub {
40 public:
OnAccountsChanged(const std::vector<AppAccountInfo> & accounts)41 void OnAccountsChanged(const std::vector<AppAccountInfo> &accounts)
42 {
43 g_status = true;
44 return;
45 }
46 };
47
48 class AppAccountEventModuleTest : public testing::Test {
49 public:
50 static void SetUpTestCase(void);
51 static void TearDownTestCase(void);
52 void SetUp(void) override;
53 void TearDown(void) override;
54 };
55
SetUpTestCase(void)56 void AppAccountEventModuleTest::SetUpTestCase(void)
57 {}
58
TearDownTestCase(void)59 void AppAccountEventModuleTest::TearDownTestCase(void)
60 {}
61
SetUp(void)62 void AppAccountEventModuleTest::SetUp(void) __attribute__((no_sanitize("cfi")))
63 {
64 testing::UnitTest *test = testing::UnitTest::GetInstance();
65 ASSERT_NE(test, nullptr);
66 const testing::TestInfo *testinfo = test->current_test_info();
67 ASSERT_NE(testinfo, nullptr);
68 string testCaseName = string(testinfo->name());
69 ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
70 }
71
TearDown(void)72 void AppAccountEventModuleTest::TearDown(void)
73 {}
74
75 /**
76 * @tc.name: AppAccountAuthenticateTest_CreateAccountImplicitly_0100
77 * @tc.desc: test event proxy cpp file.
78 * @tc.type: FUNC
79 * @tc.require:
80 */
81 HWTEST_F(AppAccountEventModuleTest, AppAccountEventTest_OnAccountsChanged_0100, TestSize.Level1)
82 {
83 ACCOUNT_LOGI("AppAccountAuthenticatorCallbackTest_OnAccountsChanged_0100");
84
85 sptr<IAppAccountEvent> eventCallbackPtr = new (std::nothrow) MockAppAccountEventStub();
86 ASSERT_NE(eventCallbackPtr, nullptr);
87 sptr<IRemoteObject> callback = eventCallbackPtr->AsObject();
88 AppAccountEventProxy testCallbackProxy(callback);
89 std::vector<AppAccountInfo> accounts;
90 testCallbackProxy.OnAccountsChanged(accounts);
91 EXPECT_EQ(g_status, true);
92 g_status = false;
93 }
94
95 /**
96 * @tc.name: AppAccountEventTest_OnRemoteRequest_0100
97 * @tc.desc: test event stub func OnRemoteRequest with invalid parcel.
98 * @tc.type: FUNC
99 * @tc.require:
100 */
101 HWTEST_F(AppAccountEventModuleTest, AppAccountEventTest_OnRemoteRequest_0100, TestSize.Level1)
102 {
103 auto appAccountEventStubPtr = std::make_shared<MockAppAccountEventStub>();
104 ASSERT_NE(appAccountEventStubPtr, nullptr);
105 MessageParcel data;
106 MessageParcel reply;
107 MessageOption option;
108 int result = appAccountEventStubPtr->OnRemoteRequest(INVALID_CODE, data, reply, option);
109 ASSERT_EQ(result, ERR_ACCOUNT_COMMON_CHECK_DESCRIPTOR_ERROR);
110 }
111
112 /**
113 * @tc.name: AppAccountEventTest_ProcOnAccountsChanged_0100
114 * @tc.desc: test event stub func ProcOnAccountsChanged with invalid data.
115 * @tc.type: FUNC
116 * @tc.require:
117 */
118 HWTEST_F(AppAccountEventModuleTest, AppAccountEventTest_ProcOnAccountsChanged_0100, TestSize.Level1)
119 {
120 auto appAccountEventStubPtr = std::make_shared<MockAppAccountEventStub>();
121 ASSERT_NE(appAccountEventStubPtr, nullptr);
122 MessageParcel data;
123 ASSERT_EQ(appAccountEventStubPtr->ProcOnAccountsChanged(data), ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR);
124 }
125
126 /**
127 * @tc.name: AppAccountEventTest_ProcOnAccountsChanged_0200
128 * @tc.desc: test event stub func ProcOnAccountsChanged with invalid data.
129 * @tc.type: FUNC
130 * @tc.require:
131 */
132 HWTEST_F(AppAccountEventModuleTest, AppAccountEventTest_ProcOnAccountsChanged_0200, TestSize.Level1)
133 {
134 auto appAccountEventStubPtr = std::make_shared<MockAppAccountEventStub>();
135 ASSERT_NE(appAccountEventStubPtr, nullptr);
136 MessageParcel data;
137 data.WriteUint32(INVALID_CODE);
138 ASSERT_EQ(appAccountEventStubPtr->ProcOnAccountsChanged(data), ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR);
139 }
140
141 /**
142 * @tc.name: AppAccountEventTest_ProcOnAccountsChanged_0300
143 * @tc.desc: test event stub func ProcOnAccountsChanged success.
144 * @tc.type: FUNC
145 * @tc.require:
146 */
147 HWTEST_F(AppAccountEventModuleTest, AppAccountEventTest_ProcOnAccountsChanged_0300, TestSize.Level1)
148 {
149 auto appAccountEventStubPtr = std::make_shared<MockAppAccountEventStub>();
150 ASSERT_NE(appAccountEventStubPtr, nullptr);
151 MessageParcel data;
152 std::vector<AppAccountInfo> accounts;
153 AppAccountInfo testAppAccountInfo;
154 accounts.emplace_back(testAppAccountInfo);
155 data.WriteUint32(accounts.size());
156 for (const auto &parcelable : accounts) {
157 bool result = data.WriteParcelable(&parcelable);
158 ASSERT_EQ(result, true);
159 }
160 ASSERT_EQ(appAccountEventStubPtr->ProcOnAccountsChanged(data), ERR_NONE);
161 }
162