• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "version_info_manager_test.h"
17 
18 #include <cerrno>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 
22 #define private public
23 #include "version_info_manager.h"
24 #include "version_manager.h"
25 #undef private
26 #include "dh_context.h"
27 #include "distributed_hardware_errno.h"
28 #include "distributed_hardware_log.h"
29 
30 using namespace testing::ext;
31 using namespace std;
32 
33 namespace OHOS {
34 namespace DistributedHardware {
35 #undef DH_LOG_TAG
36 #define DH_LOG_TAG "VersionInfoManagerTest"
37 
38 namespace {
39 const string DATABASE_DIR = "/data/service/el1/public/database/dtbhardware_manager_service/";
40 const string DEV_ID_1 = "bb536a637105409e904d4da83790a4a7";
41 const string DEV_ID_2 = "bb536a637105409e904d4da83790a4a8";
42 const string DEV_ID_3 = "bb536a637105409e904d4da83790a4a9";
43 const string NAME_SCREEN = "distributed_screen";
44 const string NAME_CAMERA = "distributed_camera";
45 const string NAME_INPUT  = "distributed_input";
46 const string VERSION_1 = "1.0";
47 const string VERSION_2 = "2.0";
48 const string VERSION_3 = "3.0";
49 std::vector<VersionInfo> g_versionInfos;
50 }
51 
CreateVersionInfos()52 std::vector<VersionInfo> CreateVersionInfos()
53 {
54     CompVersion compVersions1 = {
55         .name = NAME_CAMERA,
56         .dhType = DHType::CAMERA,
57         .handlerVersion = VERSION_1,
58         .sourceVersion = VERSION_1,
59         .sinkVersion = VERSION_1
60     };
61 
62     CompVersion compVersions2 = {
63         .name = NAME_SCREEN,
64         .dhType = DHType::SCREEN,
65         .handlerVersion = VERSION_2,
66         .sourceVersion = VERSION_2,
67         .sinkVersion = VERSION_2
68     };
69 
70     CompVersion compVersions3 = {
71         .name = NAME_INPUT,
72         .dhType = DHType::INPUT,
73         .handlerVersion = VERSION_3,
74         .sourceVersion = VERSION_3,
75         .sinkVersion = VERSION_3
76     };
77 
78     VersionInfo verInfo1;
79     verInfo1.deviceId = DEV_ID_1;
80     verInfo1.dhVersion = VERSION_1;
81     verInfo1.compVersions.insert(std::pair<DHType, CompVersion>(compVersions1.dhType, compVersions1));
82     verInfo1.compVersions.insert(std::pair<DHType, CompVersion>(compVersions1.dhType, compVersions1));
83     verInfo1.compVersions.insert(std::pair<DHType, CompVersion>(compVersions1.dhType, compVersions1));
84 
85     VersionInfo verInfo2;
86     verInfo2.deviceId = DEV_ID_2;
87     verInfo1.dhVersion = VERSION_2;
88     verInfo1.compVersions.insert(std::pair<DHType, CompVersion>(compVersions2.dhType, compVersions2));
89     verInfo1.compVersions.insert(std::pair<DHType, CompVersion>(compVersions2.dhType, compVersions2));
90     verInfo1.compVersions.insert(std::pair<DHType, CompVersion>(compVersions2.dhType, compVersions2));
91 
92     VersionInfo verInfo3;
93     verInfo3.deviceId = DEV_ID_3;
94     verInfo1.dhVersion = VERSION_3;
95     verInfo1.compVersions.insert(std::pair<DHType, CompVersion>(compVersions3.dhType, compVersions3));
96     verInfo1.compVersions.insert(std::pair<DHType, CompVersion>(compVersions3.dhType, compVersions3));
97     verInfo1.compVersions.insert(std::pair<DHType, CompVersion>(compVersions3.dhType, compVersions3));
98 
99     return std::vector<VersionInfo> { verInfo1, verInfo2, verInfo3 };
100 }
101 
SetUpTestCase(void)102 void VersionInfoManagerTest::SetUpTestCase(void)
103 {
104     auto ret = mkdir(DATABASE_DIR.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
105     if (ret != 0) {
106         DHLOGE("mkdir failed, path: %s, errno : %d", DATABASE_DIR.c_str(), errno);
107     }
108 
109     g_versionInfos = CreateVersionInfos();
110 }
111 
TearDownTestCase(void)112 void VersionInfoManagerTest::TearDownTestCase(void)
113 {
114     auto ret = remove(DATABASE_DIR.c_str());
115     if (ret != 0) {
116         DHLOGE("remove dir failed, path: %s, errno : %d", DATABASE_DIR.c_str(), errno);
117     }
118 }
119 
SetUp()120 void VersionInfoManagerTest::SetUp()
121 {
122 }
123 
TearDown()124 void VersionInfoManagerTest::TearDown()
125 {
126 }
127 
128 /**
129  * @tc.name:version_info_manager_test_001
130  * @tc.desc: Verify the VersionInfoManager Init function.
131  * @tc.type: FUNC
132  * @tc.require: AR000GHSCV
133  */
134 HWTEST_F(VersionInfoManagerTest, version_info_manager_test_001, TestSize.Level0)
135 {
136     EXPECT_EQ(VersionInfoManager::GetInstance()->Init(), DH_FWK_SUCCESS);
137 }
138 
139 /**
140  * @tc.name:version_info_manager_test_002
141  * @tc.desc: Verify the VersionInfoManager SyncRemoteVersionInfos function.
142  * @tc.type: FUNC
143  * @tc.require: AR000GHSJE
144  */
145 HWTEST_F(VersionInfoManagerTest, version_info_manager_test_002, TestSize.Level0)
146 {
147     EXPECT_EQ(VersionInfoManager::GetInstance()->SyncRemoteVersionInfos(), DH_FWK_SUCCESS);
148 }
149 
150 /**
151  * @tc.name:version_info_manager_test_003
152  * @tc.desc: Verify the VersionInfoManager AddVersion function.
153  * @tc.type: FUNC
154  * @tc.require: AR000GHSCV
155  */
156 HWTEST_F(VersionInfoManagerTest, version_info_manager_test_004, TestSize.Level0)
157 {
158     for (const auto& verInfo : g_versionInfos) {
159         EXPECT_EQ(VersionInfoManager::GetInstance()->AddVersion(verInfo), DH_FWK_SUCCESS);
160     }
161 }
162 
163 /**
164  * @tc.name:version_info_manager_test_005
165  * @tc.desc: Verify the VersionInfoManager GetVersionInfoByDeviceId function.
166  * @tc.type: FUNC
167  * @tc.require: AR000GHSJE
168  */
169 HWTEST_F(VersionInfoManagerTest, version_info_manager_test_005, TestSize.Level0)
170 {
171     VersionInfo versionInfo;
172     for (const auto& verInfo : g_versionInfos) {
173         EXPECT_EQ(VersionInfoManager::GetInstance()->GetVersionInfoByDeviceId(verInfo.deviceId, versionInfo),
174             DH_FWK_SUCCESS);
175     }
176 }
177 
178 /**
179  * @tc.name:version_info_manager_test_006
180  * @tc.desc: Verify the VersionInfoManager SyncVersionInfoFromDB function.
181  * @tc.type: FUNC
182  * @tc.require: AR000GHSJE
183  */
184 HWTEST_F(VersionInfoManagerTest, version_info_manager_test_006, TestSize.Level0)
185 {
186     EXPECT_EQ(VersionInfoManager::GetInstance()->SyncVersionInfoFromDB(DEV_ID_1), DH_FWK_SUCCESS);
187 }
188 
189 /**
190  * @tc.name:version_info_manager_test_007
191  * @tc.desc: Verify the VersionInfoManager UnInit function.
192  * @tc.type: FUNC
193  * @tc.require: AR000GHSJE
194  */
195 HWTEST_F(VersionInfoManagerTest, version_info_manager_test_007, TestSize.Level0)
196 {
197     EXPECT_EQ(VersionInfoManager::GetInstance()->UnInit(), DH_FWK_SUCCESS);
198 }
199 } // namespace DistributedHardware
200 } // namespace OHOS
201