1 /*
2  * Copyright (c) 2021 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 #define private public
20 #include "app_account_subscribe_info.h"
21 #undef private
22 
23 using namespace testing::ext;
24 using namespace OHOS;
25 using namespace OHOS::AccountSA;
26 
27 namespace {
28 const std::string STRING_OWNER = "com.example.owner";
29 }  // namespace
30 
31 class AppAccountSubscribeInfoTest : public testing::Test {
32 public:
33     static void SetUpTestCase(void);
34     static void TearDownTestCase(void);
35     void SetUp(void) override;
36     void TearDown(void) override;
37 };
38 
SetUpTestCase(void)39 void AppAccountSubscribeInfoTest::SetUpTestCase(void)
40 {}
41 
TearDownTestCase(void)42 void AppAccountSubscribeInfoTest::TearDownTestCase(void)
43 {}
44 
SetUp(void)45 void AppAccountSubscribeInfoTest::SetUp(void) __attribute__((no_sanitize("cfi")))
46 {
47     testing::UnitTest *test = testing::UnitTest::GetInstance();
48     ASSERT_NE(test, nullptr);
49     const testing::TestInfo *testinfo = test->current_test_info();
50     ASSERT_NE(testinfo, nullptr);
51     string testCaseName = string(testinfo->name());
52     ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
53 }
54 
TearDown(void)55 void AppAccountSubscribeInfoTest::TearDown(void)
56 {}
57 
58 /**
59  * @tc.name: AppAccountSubscribeInfo_GetOwners_0100
60  * @tc.desc: Get owners with valid data.
61  * @tc.type: FUNC
62  * @tc.require: SR000GGVFT
63  */
64 HWTEST_F(AppAccountSubscribeInfoTest, AppAccountSubscribeInfo_GetOwners_0100, TestSize.Level1)
65 {
66     // make owners
67     std::vector<std::string> owners;
68     owners.emplace_back(STRING_OWNER);
69 
70     // make subscribe info with owners
71     AppAccountSubscribeInfo subscribeInfo(owners);
72 
73     // get owners
74     std::vector<std::string> ownersFromSubscribeInfo;
75     ErrCode result = subscribeInfo.GetOwners(ownersFromSubscribeInfo);
76     EXPECT_EQ(result, ERR_OK);
77 
78     // check size of owners
79     EXPECT_EQ(owners.size(), ownersFromSubscribeInfo.size());
80     // check the first owner name
81     EXPECT_EQ(owners.front(), ownersFromSubscribeInfo.front());
82 }
83 
84 /**
85  * @tc.name: AppAccountSubscribeInfo_SetOwners_0100
86  * @tc.desc: Set owners with valid data.
87  * @tc.type: FUNC
88  * @tc.require: SR000GGVFT
89  */
90 HWTEST_F(AppAccountSubscribeInfoTest, AppAccountSubscribeInfo_SetOwners_0100, TestSize.Level1)
91 {
92     // make owners
93     std::vector<std::string> owners;
94     owners.emplace_back(STRING_OWNER);
95 
96     // make subscribe info
97     AppAccountSubscribeInfo subscribeInfo;
98 
99     // set owners
100     ErrCode result = subscribeInfo.SetOwners(owners);
101     EXPECT_EQ(result, ERR_OK);
102 
103     // check size of owners
104     EXPECT_EQ(owners.size(), subscribeInfo.owners_.size());
105     // check the first owner name
106     EXPECT_EQ(owners.front(), subscribeInfo.owners_.front());
107 }
108 
109 /**
110  * @tc.name: AppAccountSubscribeInfo_Marshalling_0100
111  * @tc.desc: Marshalling Unmarshalling with valid data.
112  * @tc.type: FUNC
113  * @tc.require: SR000GGVFT
114  */
115 HWTEST_F(AppAccountSubscribeInfoTest, AppAccountSubscribeInfo_Marshalling_0100, TestSize.Level0)
116 {
117     // make owners
118     std::vector<std::string> owners;
119     owners.emplace_back(STRING_OWNER);
120 
121     // make subscribe info with owners
122     AppAccountSubscribeInfo subscribeInfo(owners);
123 
124     Parcel parcel;
125     // marshalling
126     EXPECT_EQ(subscribeInfo.Marshalling(parcel), true);
127 
128     // unmarshalling
129     auto subscribeInfoPtr = AppAccountSubscribeInfo::Unmarshalling(parcel);
130     EXPECT_NE(subscribeInfoPtr, nullptr);
131 
132     // get owners
133     std::vector<std::string> ownersFromSubscribeInfo;
134     ErrCode result = subscribeInfoPtr->GetOwners(ownersFromSubscribeInfo);
135     EXPECT_EQ(result, ERR_OK);
136 
137     // check size of owners
138     EXPECT_EQ(owners.size(), ownersFromSubscribeInfo.size());
139     // check the first owner name
140     EXPECT_EQ(owners.front(), ownersFromSubscribeInfo.front());
141 }
142