1 /*
2 * Copyright (c) 2024 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 #include "device/device_info.h"
16
17 #include <exception>
18 #include <memory>
19 #include <unistd.h>
20
21 #include "gtest/gtest.h"
22
23 namespace OHOS {
24 namespace Storage {
25 namespace DistributedFile {
26 namespace Test {
27 using namespace testing::ext;
28 using namespace std;
29
30 const string NETWORKID = "testNetWorkId";
31
32 DistributedHardware::DmDeviceInfo deviceInfo = {
33 .deviceId = "testdevid",
34 .deviceName = "testdevname",
35 .deviceTypeId = 1,
36 .networkId = "testNetWorkId",
37 };
38
39 class DeviceInfoTest : public testing::Test {
40 public:
41 static void SetUpTestCase(void);
42 static void TearDownTestCase(void);
43 void SetUp();
44 void TearDown();
45 };
46
SetUpTestCase(void)47 void DeviceInfoTest::SetUpTestCase(void)
48 {
49 // input testsuit setup step,setup invoked before all testcases
50 }
51
TearDownTestCase(void)52 void DeviceInfoTest::TearDownTestCase(void)
53 {
54 // input testsuit teardown step,teardown invoked after all testcases
55 }
56
SetUp(void)57 void DeviceInfoTest::SetUp(void)
58 {
59 // input testcase setup step,setup invoked before each testcases
60 }
61
TearDown(void)62 void DeviceInfoTest::TearDown(void)
63 {
64 // input testcase teardown step,teardown invoked after each testcases
65 }
66
67 /**
68 * @tc.name: DeviceInfoTest_SetCid_0100
69 * @tc.desc: Verify the SetCid function.
70 * @tc.type: FUNC
71 * @tc.require: IAGNEY
72 */
73 HWTEST_F(DeviceInfoTest, DeviceInfoTest_SetCid_0100, TestSize.Level1)
74 {
75 GTEST_LOG_(INFO) << "DeviceInfoTest_SetCid_0100 start";
76 DeviceInfo devInfo(deviceInfo);
77 try {
78 string testCid = "test";
79 devInfo.SetCid(testCid);
80 EXPECT_EQ(devInfo.GetCid(), NETWORKID);
81
82 devInfo.initCidFlag_ = false;
83 devInfo.SetCid(testCid);
84 EXPECT_EQ(devInfo.GetCid(), testCid);
85 } catch (...) {
86 EXPECT_TRUE(false);
87 }
88 GTEST_LOG_(INFO) << "DeviceInfoTest_SetCid_0100 start";
89 }
90
91 /**
92 * @tc.name: DeviceInfoTest_GetCid_0100
93 * @tc.desc: Verify the GetCid function.
94 * @tc.type: FUNC
95 * @tc.require: IAGNEY
96 */
97 HWTEST_F(DeviceInfoTest, DeviceInfoTest_GetCid_0100, TestSize.Level1)
98 {
99 GTEST_LOG_(INFO) << "DeviceInfoTest_GetCid_0100 start";
100 DeviceInfo devInfo(deviceInfo);
101 try {
102 devInfo.initCidFlag_ = false;
103 (void)devInfo.GetCid();
104 EXPECT_TRUE(false);
105 } catch (...) {
106 EXPECT_TRUE(true);
107 }
108 GTEST_LOG_(INFO) << "DeviceInfoTest_GetCid_0100 start";
109 }
110
111 /**
112 * @tc.name: DeviceInfoTest_GetExtraData_0100
113 * @tc.desc: Verify the GetExtraData function.
114 * @tc.type: FUNC
115 * @tc.require: IAGNEY
116 */
117 HWTEST_F(DeviceInfoTest, DeviceInfoTest_GetExtraData_0100, TestSize.Level1)
118 {
119 GTEST_LOG_(INFO) << "DeviceInfoTest_GetExtraData_0100 start";
120 DeviceInfo devInfo(deviceInfo);
121 try {
122 devInfo.extraData_ = "";
123 EXPECT_EQ(devInfo.GetExtraData(), "");
124
125 devInfo.extraData_ = "test";
126 EXPECT_EQ(devInfo.GetExtraData(), "test");
127 EXPECT_TRUE(true);
128 } catch (...) {
129 EXPECT_TRUE(false);
130 }
131 GTEST_LOG_(INFO) << "DeviceInfoTest_GetExtraData_0100 start";
132 }
133
134 /**
135 * @tc.name: DeviceInfoTest_GetDeviceId_0100
136 * @tc.desc: Verify the GetDeviceId function.
137 * @tc.type: FUNC
138 * @tc.require: IAGNEY
139 */
140 HWTEST_F(DeviceInfoTest, DeviceInfoTest_GetDeviceId_0100, TestSize.Level1)
141 {
142 GTEST_LOG_(INFO) << "DeviceInfoTest_GetDeviceId_0100 start";
143 DeviceInfo devInfo(deviceInfo);
144 try {
145 devInfo.deviceId_ = "";
146 EXPECT_EQ(devInfo.GetDeviceId(), "");
147
148 devInfo.deviceId_ = "test";
149 EXPECT_EQ(devInfo.GetDeviceId(), "test");
150 EXPECT_TRUE(true);
151 } catch (...) {
152 EXPECT_TRUE(false);
153 }
154 GTEST_LOG_(INFO) << "DeviceInfoTest_GetDeviceId_0100 start";
155 }
156
157 /**
158 * @tc.name: DeviceInfoTest_OperateEqual_0100
159 * @tc.desc: Verify the operate= function.
160 * @tc.type: FUNC
161 * @tc.require: IAGNEY
162 */
163 HWTEST_F(DeviceInfoTest, DeviceInfoTest_OperateEqual_0100, TestSize.Level1)
164 {
165 GTEST_LOG_(INFO) << "DeviceInfoTest_OperateEqual_0100 start";
166 DeviceInfo devInfo(deviceInfo);
167 DeviceInfo devInfoTwo(devInfo);
168 try {
169 EXPECT_EQ(devInfoTwo.GetDeviceId(), deviceInfo.deviceId);
170 EXPECT_EQ(devInfoTwo.GetCid(), deviceInfo.networkId);
171 EXPECT_EQ(devInfoTwo.initCidFlag_, true);
172 EXPECT_TRUE(true);
173
174 DistributedHardware::DmDeviceInfo dmDeviceInfoTwo = {
175 .deviceId = "testdevid2",
176 .deviceName = "testdevname2",
177 .deviceTypeId = 2,
178 .networkId = "testNetWorkId2",
179 };
180
181 devInfoTwo = dmDeviceInfoTwo;
182 EXPECT_EQ(devInfoTwo.GetDeviceId(), dmDeviceInfoTwo.deviceId);
183 EXPECT_EQ(devInfoTwo.GetCid(), dmDeviceInfoTwo.networkId);
184 EXPECT_EQ(devInfoTwo.initCidFlag_, true);
185 EXPECT_TRUE(true);
186 } catch (...) {
187 EXPECT_TRUE(false);
188 }
189 GTEST_LOG_(INFO) << "DeviceInfoTest_OperateEqual_0100 start";
190 }
191 }
192 } // namespace Test
193 } // namespace DistributedFile
194 }