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 <gtest/gtest.h>
17
18 #include "account_error_no.h"
19 #include "account_log_wrapper.h"
20 #define private public
21 #include "app_account_control_manager.h"
22 #include "app_account_manager_service.h"
23 #undef private
24 #include "mock_inner_app_account_manager.h"
25
26 using namespace testing::ext;
27 using namespace OHOS;
28 using namespace OHOS::AccountSA;
29
30 namespace {
31 const std::string STRING_NAME = "name";
32 const std::string STRING_EXTRA_INFO = "extra_info";
33 const std::string STRING_OWNER = "com.example.owner";
34 sptr<IRemoteObject> g_appAccountManagerService;
35 sptr<IAppAccount> g_appAccountProxy;
36 sptr<AppAccountManagerService> g_servicePtr;
37 } // namespace
38
39 class AppAccountManagerServiceTest : public testing::Test {
40 public:
41 static void SetUpTestCase(void);
42 static void TearDownTestCase(void);
43 void SetUp(void) override;
44 void TearDown(void) override;
45 };
46
SetUpTestCase(void)47 void AppAccountManagerServiceTest::SetUpTestCase(void)
48 {
49 g_servicePtr = new (std::nothrow) AppAccountManagerService();
50 ASSERT_NE(g_servicePtr, nullptr);
51 auto mockInnerManagerPtr = std::make_shared<MockInnerAppAccountManager>();
52 g_servicePtr->innerManager_ = mockInnerManagerPtr;
53
54 g_appAccountManagerService = g_servicePtr->AsObject();
55 g_appAccountProxy = iface_cast<IAppAccount>(g_appAccountManagerService);
56 }
57
TearDownTestCase(void)58 void AppAccountManagerServiceTest::TearDownTestCase(void)
59 {
60 GTEST_LOG_(INFO) << "TearDownTestCase!";
61 }
62
SetUp(void)63 void AppAccountManagerServiceTest::SetUp(void) __attribute__((no_sanitize("cfi")))
64 {
65 testing::UnitTest *test = testing::UnitTest::GetInstance();
66 ASSERT_NE(test, nullptr);
67 const testing::TestInfo *testinfo = test->current_test_info();
68 ASSERT_NE(testinfo, nullptr);
69 string testCaseName = string(testinfo->name());
70 ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
71 }
72
TearDown(void)73 void AppAccountManagerServiceTest::TearDown(void)
74 {}
75
76 /**
77 * @tc.name: AppAccountManagerService_AddAccount_0100
78 * @tc.desc: Add an app account with valid data.
79 * @tc.type: FUNC
80 * @tc.require: SR000GGV11
81 */
82 HWTEST_F(AppAccountManagerServiceTest, AppAccountManagerService_AddAccount_0100, TestSize.Level1)
83 {
84 ErrCode result = g_appAccountProxy->AddAccount(STRING_NAME, STRING_EXTRA_INFO);
85
86 EXPECT_EQ(result, ERR_OK);
87 }
88
89 /**
90 * @tc.name: AppAccountManagerService_DeleteAccount_0100
91 * @tc.desc: Delete an app account with valid data.
92 * @tc.type: FUNC
93 * @tc.require: SR000GGV11
94 */
95 HWTEST_F(AppAccountManagerServiceTest, AppAccountManagerService_DeleteAccount_0100, TestSize.Level1)
96 {
97 ErrCode result = g_appAccountProxy->DeleteAccount(STRING_NAME);
98
99 EXPECT_EQ(result, ERR_OK);
100 }
101
102 /**
103 * @tc.name: AppAccountManagerService_CreateAccount_0100
104 * @tc.desc: Add an app account with valid data.
105 * @tc.type: FUNC
106 * @tc.require: issueI5RWXN
107 */
108 HWTEST_F(AppAccountManagerServiceTest, AppAccountManagerService_CreateAccount_0100, TestSize.Level1)
109 {
110 CreateAccountOptions option;
111 ErrCode result = g_appAccountProxy->CreateAccount(STRING_NAME, option);
112 EXPECT_EQ(result, ERR_OK);
113 result = g_appAccountProxy->DeleteAccount(STRING_NAME);
114 EXPECT_EQ(result, ERR_OK);
115 }
116
117 /**
118 * @tc.name: AppAccountManagerService_SubscribeAppAccount_0100
119 * @tc.desc: Subscribe app accounts with invalid data.
120 * @tc.type: FUNC
121 * @tc.require: SR000GGVFT
122 */
123 HWTEST_F(
124 AppAccountManagerServiceTest, AppAccountManagerService_SubscribeAppAccount_0100, TestSize.Level1)
125 {
126 // make owners
127 std::vector<std::string> owners;
128 owners.emplace_back(STRING_OWNER);
129
130 // make subscribe info
131 AppAccountSubscribeInfo subscribeInfo;
132 subscribeInfo.SetOwners(owners);
133
134 // subscribe app account
135 ErrCode result = g_appAccountProxy->SubscribeAppAccount(subscribeInfo, nullptr);
136
137 EXPECT_EQ(result, ERR_ACCOUNT_COMMON_NULL_PTR_ERROR);
138 }
139
140 /**
141 * @tc.name: AppAccountManagerService_UnsubscribeAppAccount_0100
142 * @tc.desc: Unsubscribe app accounts with invalid data.
143 * @tc.type: FUNC
144 * @tc.require: SR000GGVFT
145 */
146 HWTEST_F(
147 AppAccountManagerServiceTest, AppAccountManagerService_UnsubscribeAppAccount_0100, TestSize.Level1)
148 {
149 // unsubscribe app account
150 ErrCode result = g_appAccountProxy->UnsubscribeAppAccount(nullptr);
151
152 EXPECT_EQ(result, ERR_ACCOUNT_COMMON_NULL_PTR_ERROR);
153 }
154