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 
16 #include "mock_domain_create_domain_account_callback.h"
17 #include "mock_domain_plugin.h"
18 
19 #include "account_log_wrapper.h"
20 #include "condition_variable"
21 #include "os_account_manager.h"
22 #include "parcel.h"
23 
24 namespace OHOS {
25 namespace AccountSA {
26 namespace {
27 }
28 
TestCreateDomainAccountCallback(const std::shared_ptr<MockDomainCreateDomainAccountCallback> & callback)29 TestCreateDomainAccountCallback::TestCreateDomainAccountCallback(
30     const std::shared_ptr<MockDomainCreateDomainAccountCallback> &callback)
31     : callback_(callback)
32 {}
33 
~TestCreateDomainAccountCallback()34 TestCreateDomainAccountCallback::~TestCreateDomainAccountCallback()
35 {}
36 
OnResult(const int32_t errCode,Parcel & parcel)37 void TestCreateDomainAccountCallback::OnResult(const int32_t errCode, Parcel &parcel)
38 {
39     if (callback_ == nullptr) {
40         std::unique_lock<std::mutex> lock(mutex);
41         isReady = true;
42         cv.notify_one();
43         return;
44     }
45 
46     DomainAccountInfo newDomainInfo;
47     OsAccountInfo *osAccountInfo = OsAccountInfo::Unmarshalling(parcel);
48     if (osAccountInfo == nullptr || errCode != ERR_OK) {
49         callback_->OnResult(errCode, newDomainInfo.accountName_, newDomainInfo.domain_, newDomainInfo.accountId_);
50         std::unique_lock<std::mutex> lock(mutex);
51         isReady = true;
52         cv.notify_one();
53         return;
54     }
55     osAccountInfo->GetDomainInfo(newDomainInfo);
56     callback_->OnResult(errCode, newDomainInfo.accountName_, newDomainInfo.domain_, newDomainInfo.accountId_);
57     localId_ = osAccountInfo->GetLocalId();
58     if ((newDomainInfo.accountName_ == "zhangsan") || (newDomainInfo.accountName_ == "zhangsan777") ||
59         (newDomainInfo.accountName_ == "zhangsan666")) {
60         std::unique_lock<std::mutex> lock(mutex);
61         isReady = true;
62         cv.notify_one();
63         return;
64     }
65     OsAccountManager::RemoveOsAccount(osAccountInfo->GetLocalId());
66     std::unique_lock<std::mutex> lock(mutex);
67     isReady = true;
68     cv.notify_one();
69     return;
70 }
71 
GetLocalId(void)72 int32_t TestCreateDomainAccountCallback::GetLocalId(void)
73 {
74     return localId_;
75 }
76 }  // AccountSA
77 }  // OHOS