1 /*
2  * Copyright (c) 2022-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 <cerrno>
17 #include <gtest/gtest.h>
18 #include "account_log_wrapper.h"
19 #define private public
20 #include "domain_account_common.h"
21 #undef private
22 #include "domain_account_client.h"
23 #include "parcel.h"
24 #include "want.h"
25 using namespace testing;
26 using namespace testing::ext;
27 using namespace OHOS;
28 using namespace OHOS::AccountSA;
29 
30 namespace {
31 const int32_t CALLING_UID = 100;
32 const int32_t REMAINING_TIMES = 100;
33 const std::string STRING_NAME_TWO = "zhangsan666";
34 const std::string STRING_DOMAIN_NEW = "test.example.com";
35 const std::string STRING_NAME_NEW = "zhangsan777";
36 const std::string STRING_ACCOUNTID_NEW = "3333";
37 const std::vector<uint8_t> TOKEN = {1, 2, 3, 4, 5};
38 } // namespace
39 
40 class DomainAccountCommonModuleTest : public testing::Test {
41 public:
42     static void SetUpTestCase(void);
43     static void TearDownTestCase(void);
44     void SetUp();
45     void TearDown();
46 };
47 
SetUpTestCase(void)48 void DomainAccountCommonModuleTest::SetUpTestCase(void)
49 {
50     GTEST_LOG_(INFO) << "SetUpTestCase enter";
51 }
52 
TearDownTestCase(void)53 void DomainAccountCommonModuleTest::TearDownTestCase(void)
54 {
55     GTEST_LOG_(INFO) << "TearDownTestCase";
56 }
57 
SetUp(void)58 void DomainAccountCommonModuleTest::SetUp(void) __attribute__((no_sanitize("cfi")))
59 {
60     testing::UnitTest *test = testing::UnitTest::GetInstance();
61     ASSERT_NE(test, nullptr);
62     const testing::TestInfo *testinfo = test->current_test_info();
63     ASSERT_NE(testinfo, nullptr);
64     string testCaseName = string(testinfo->name());
65     ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
66 }
67 
TearDown(void)68 void DomainAccountCommonModuleTest::TearDown(void)
69 {}
70 
71 /**
72  * @tc.name: DomainAccountCommonModuleTest_GetAccessTokenOptions_001
73  * @tc.desc: GetAccessTokenOptions Marshalling successfully.
74  * @tc.type: FUNC
75  * @tc.require:
76  */
77 HWTEST_F(DomainAccountCommonModuleTest, DomainAccountCommonModuleTest_GetAccessTokenOptions_001, TestSize.Level0)
78 {
79     AAFwk::WantParams parameters;
80     GetAccessTokenOptions option(CALLING_UID, parameters);
81     Parcel parcel;
82     option.Marshalling(parcel);
83     GetAccessTokenOptions *getAccessTokenOptions = option.Unmarshalling(parcel);
84     std::shared_ptr<GetAccessTokenOptions> optionPtr(getAccessTokenOptions);
85     EXPECT_EQ(getAccessTokenOptions->callingUid_, CALLING_UID);
86 }
87 
88 /**
89  * @tc.name: DomainAccountCommonModuleTest_DomainAuthResult_001
90  * @tc.desc: DomainAuthResult Marshalling successfully.
91  * @tc.type: FUNC
92  * @tc.require:
93  */
94 HWTEST_F(DomainAccountCommonModuleTest, DomainAccountCommonModuleTest_DomainAuthResult_001, TestSize.Level0)
95 {
96     DomainAuthResult domainAuthResult;
97     domainAuthResult.token = TOKEN;
98     Parcel parcel;
99     domainAuthResult.Marshalling(parcel);
100     DomainAuthResult *result = domainAuthResult.Unmarshalling(parcel);
101     std::shared_ptr<DomainAuthResult> domainAuthResultPtr(result);
102     for (size_t index = 0; index < result->token.size(); index++) {
103         EXPECT_EQ(result->token[index], TOKEN[index]);
104     }
105 }
106 
107 /**
108  * @tc.name: DomainAccountCommonModuleTest_AuthStatusInfo_001
109  * @tc.desc: AuthStatusInfo Marshalling successfully.
110  * @tc.type: FUNC
111  * @tc.require:
112  */
113 HWTEST_F(DomainAccountCommonModuleTest, DomainAccountCommonModuleTest_AuthStatusInfo_001, TestSize.Level0)
114 {
115     AuthStatusInfo authStatusInfo;
116     authStatusInfo.remainingTimes = REMAINING_TIMES;
117     Parcel parcel;
118     authStatusInfo.Marshalling(parcel);
119     AuthStatusInfo *result = authStatusInfo.Unmarshalling(parcel);
120     std::shared_ptr<AuthStatusInfo> authStatusInfoPtr(result);
121     EXPECT_EQ(authStatusInfoPtr->remainingTimes, REMAINING_TIMES);
122 }
123 
124 /**
125  * @tc.name: DomainAccountCommonModuleTest_DomainServerConfig_001
126  * @tc.desc: DomainServerConfig Marshalling successfully.
127  * @tc.type: FUNC
128  * @tc.require:
129  */
130 HWTEST_F(DomainAccountCommonModuleTest, DomainAccountCommonModuleTest_DomainServerConfig_001, TestSize.Level0)
131 {
132     std::string parameters;
133     string id = STRING_DOMAIN_NEW;
134     DomainServerConfig config(parameters, id);
135     Parcel parcel;
136     config.Marshalling(parcel);
137     DomainServerConfig *result = config.Unmarshalling(parcel);
138     std::shared_ptr<DomainServerConfig> infoPtr(result);
139     EXPECT_EQ(infoPtr->id_, STRING_DOMAIN_NEW);
140 }