Debian Codegraph @

tests/ut_dsysinfo.cpp

raw
1 // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4 #include <gtest/gtest.h>
5
6 #include <QFile>
7 #include <QDebug>
8 #include <QRandomGenerator>
9
10 #include "dsysinfo.h"
11 #include "ddesktopentry.h"
12 #include "test_helper.hpp"
13
14 DCORE_USE_NAMESPACE
15
16 class ut_DSysInfo : public testing::Test
17 {
18 protected:
19 void SetUp() override {
20 if (QLatin1String(DSYSINFO_PREFIX).isEmpty()) {
21 GTEST_SKIP_("DSYSINFO_PREFIX not defined...");
22 }
23 }
24 void TearDown() override {
25
26 }
27 };
28
29 TEST_F(ut_DSysInfo, testOsVersion)
30 {
31 FileGuard guard("/tmp/etc/os-version");
32 DDesktopEntry entry(guard.fileName());
33 entry.setStringValue("UnionTech OS Desktop", "SystemName", "Version");
34 entry.setStringValue("统信桌面操作系统", "SystemName[zh_CN]", "Version");
35 entry.setStringValue("Desktop", "ProductType", "Version");
36 entry.setStringValue("桌面", "ProductType[zh_CN]", "Version");
37 entry.setStringValue("Professional", "EditionName", "Version");
38 entry.setStringValue("专业版", "EditionName[zh_CN]", "Version");
39 entry.setStringValue("20", "MajorVersion", "Version");
40 entry.setStringValue("100A", "MinorVersion", "Version");
41 entry.setStringValue("11Z18.107.109", "OsBuild", "Version");
42 ASSERT_TRUE(entry.save());
43
44 ASSERT_TRUE(DSysInfo::uosSystemName(QLocale("C")) == "UnionTech OS Desktop");
45 ASSERT_TRUE(DSysInfo::uosSystemName(QLocale("zh_CN")) == "统信桌面操作系统");
46 ASSERT_TRUE(DSysInfo::uosProductTypeName(QLocale("zh_CN")) == "桌面");
47 ASSERT_TRUE(DSysInfo::uosProductTypeName(QLocale("C")) == "Desktop");
48 ASSERT_TRUE(DSysInfo::uosEditionName(QLocale("zh_CN")) == "专业版");
49 ASSERT_TRUE(DSysInfo::uosEditionName(QLocale("C")) == "Professional");
50 ASSERT_TRUE(DSysInfo::majorVersion() == "20");
51 ASSERT_TRUE(DSysInfo::minorVersion() == "100A");
52 ASSERT_TRUE(DSysInfo::buildVersion() == "107.109");
53
54 // test minVersion.BC SP1….SP99
55 for (int i = 0; i < 3; ++i) {
56 int sp = QRandomGenerator::global()->generate() % 100;
57 entry.setStringValue(QString("%1").arg(1001 + sp * 10), "MinorVersion", "Version");
58 ASSERT_TRUE(entry.save());
59 ASSERT_TRUE(DSysInfo::spVersion() == (sp ? QString("SP%1").arg(sp) : QString()));
60 }
61
62 // test minVersion.D udpate1~udpate9 updateA~udpateZ
63 for (int i = 0; i < 10; ++i) {
64 entry.setStringValue(QString("%1").arg(1000 + i), "MinorVersion", "Version");
65 ASSERT_TRUE(entry.save());
66 ASSERT_TRUE(DSysInfo::udpateVersion() == (i ? QString("update%1").arg(i) : QString()));
67 }
68
69 for (char c = 'A'; c <= 'Z'; ++c) {
70 entry.setStringValue(QString("100").append(c), "MinorVersion", "Version");
71 ASSERT_TRUE(entry.save());
72 ASSERT_TRUE(DSysInfo::udpateVersion() == QString("update%1").arg(c));
73 }
74
75 // test incalide MinorVersion
76 entry.setStringValue(QString("100?"), "MinorVersion", "Version");
77 ASSERT_TRUE(entry.save());
78 ASSERT_TRUE(DSysInfo::udpateVersion() == QString());
79 // restore MinorVersion
80 entry.setStringValue(QString("1000"), "MinorVersion", "Version");
81 ASSERT_TRUE(entry.save());
82
83 // test OsBuild.B == 1 && OsBuild.D = [1, 6]
84 ASSERT_TRUE(DSysInfo::uosType() == DSysInfo::UosDesktop);
85 for (int i = 1; i <= 6; ++i) {
86 entry.setStringValue(QString("%1").arg(11008.107 + i * 10), "OsBuild", "Version");
87 ASSERT_TRUE(entry.save());
88 switch (i) {
89 case 1:
90 ASSERT_TRUE(DSysInfo::uosEditionType() == DSysInfo::UosProfessional);
91 break;
92 case 2:
93 ASSERT_TRUE(DSysInfo::uosEditionType() == DSysInfo::UosHome);
94 break;
95 case 4:
96 ASSERT_TRUE(DSysInfo::uosEditionType() == DSysInfo::UosMilitary);
97 break;
98 case 5:
99 ASSERT_TRUE(DSysInfo::uosEditionType() == DSysInfo::UosDeviceEdition);
100 break;
101 case 6:
102 ASSERT_TRUE(DSysInfo::uosEditionType() == DSysInfo::UosEducation);
103 break;
104 default:
105 break;
106 }
107 }
108
109 // test OsBuild.B == 2 && OsBuild.D = [1, 5]
110 entry.setStringValue("12018.107", "OsBuild", "Version");
111 ASSERT_TRUE(entry.save());
112 ASSERT_TRUE(DSysInfo::uosType() == DSysInfo::UosServer);
113 for (int i = 1; i <= 5; ++i) {
114 entry.setStringValue(QString("%1").arg(12008.107 + i * 10), "OsBuild", "Version");
115 ASSERT_TRUE(entry.save());
116 switch (i) {
117 case 1:
118 ASSERT_TRUE(DSysInfo::uosEditionType() == DSysInfo::UosEnterprise);
119 break;
120 case 2:
121 ASSERT_TRUE(DSysInfo::uosEditionType() == DSysInfo::UosEnterpriseC);
122 break;
123 case 3:
124 ASSERT_TRUE(DSysInfo::uosEditionType() == DSysInfo::UosEuler);
125 break;
126 case 4:
127 ASSERT_TRUE(DSysInfo::uosEditionType() == DSysInfo::UosMilitaryS);
128 break;
129 case 5:
130 ASSERT_TRUE(DSysInfo::uosEditionType() == DSysInfo::UosDeviceEdition);
131 break;
132 default:
133 break;
134 }
135 }
136
137 // test OsBuild.B == 3
138 entry.setStringValue("13018.107", "OsBuild", "Version");
139 ASSERT_TRUE(entry.save());
140 ASSERT_TRUE(DSysInfo::uosType() == DSysInfo::UosDevice);
141 ASSERT_TRUE(DSysInfo::uosEditionType() == DSysInfo::UosEnterprise);
142
143 // test invalid OsBuild.B
144 entry.setStringValue("10018.107", "OsBuild", "Version");
145 ASSERT_TRUE(entry.save());
146 ASSERT_TRUE(DSysInfo::uosType() == DSysInfo::UosTypeUnknown);
147
148 // 社区版测试
149 entry.setStringValue("Community", "EditionName", "Version");
150 entry.setStringValue("社区版", "EditionName[zh_CN]", "Version");
151 entry.setStringValue("21.1.2", "MinorVersion", "Version");
152 entry.setStringValue("11038.107", "OsBuild", "Version");
153 ASSERT_TRUE(entry.save());
154
155 ASSERT_TRUE(DSysInfo::uosEditionName(QLocale("zh_CN")) == "社区版");
156 ASSERT_TRUE(DSysInfo::uosEditionName(QLocale("C")) == "Community");
157 ASSERT_TRUE(DSysInfo::minorVersion() == "21.1.2");
158 ASSERT_TRUE(DSysInfo::buildVersion() == "107");
159
160 //社区版A_BC_D模式 test minVersion.BC SP1….SP99
161 for (int i = 0; i < 3; ++i) {
162 int sp = QRandomGenerator::global()->generate() % 100;
163 entry.setStringValue(QString("%1").arg(1001 + sp * 10), "MinorVersion", "Version");
164 ASSERT_TRUE(entry.save());
165 ASSERT_TRUE(DSysInfo::spVersion() == (sp ? QString("SP%1").arg(sp) : QString()));
166 }
167
168 //社区版A_BC_D模式 test minVersion.D udpate1~udpate9 updateA~udpateZ
169 for (int i = 0; i < 10; ++i) {
170 entry.setStringValue(QString("%1").arg(1000 + i), "MinorVersion", "Version");
171 ASSERT_TRUE(entry.save());
172 ASSERT_TRUE(DSysInfo::udpateVersion() == (i ? QString("update%1").arg(i) : QString()));
173 }
174
175 auto dmax = [](int x, int y){
176 return x > y ? x : y;
177 };
178 //社区版A_B_C模式 test minVersion.BC SP1….SP99
179 const QString &defalutSP("21.%1");
180 for (int i = 1; i < 3; ++i) {
181 int sp = dmax(QRandomGenerator::global()->generate() % 100, 1);
182 entry.setStringValue(defalutSP.arg(sp), "MinorVersion", "Version");
183 ASSERT_TRUE(entry.save());
184 ASSERT_TRUE(DSysInfo::spVersion() == QString("SP%1").arg(sp));
185 }
186
187 //社区版A_B_C模式 test minVersion.D udpate1~udpate9 updateA~udpateZ
188 const QString &defalutUpdate("21.1.%1");
189 for (int i = 1; i < 3; ++i) {
190 int sp = dmax(QRandomGenerator::global()->generate() % 100, 1);
191 entry.setStringValue(defalutUpdate.arg(sp), "MinorVersion", "Version");
192 ASSERT_TRUE(entry.save());
193 ASSERT_TRUE(DSysInfo::udpateVersion() == QString("update%1").arg(sp));
194 }
195
196 // 家庭版测试
197 entry.setStringValue("Home", "EditionName", "Version");
198 entry.setStringValue("家庭版", "EditionName[zh_CN]", "Version");
199 entry.setStringValue("21.0", "MinorVersion", "Version");
200 entry.setStringValue("11078.107", "OsBuild", "Version");
201 ASSERT_TRUE(entry.save());
202
203 ASSERT_TRUE(DSysInfo::uosEditionName(QLocale("zh_CN")) == "家庭版");
204 ASSERT_TRUE(DSysInfo::uosEditionName(QLocale("C")) == "Home");
205 ASSERT_TRUE(DSysInfo::minorVersion() == "21.0");
206 ASSERT_TRUE(DSysInfo::buildVersion() == "107");
207 ASSERT_TRUE(DSysInfo::spVersion() == QStringLiteral(""));
208 ASSERT_TRUE(DSysInfo::udpateVersion() == QStringLiteral(""));
209 }
210
211 TEST_F(ut_DSysInfo, testdistributionInfo)
212 {
213 FileGuard fg("/tmp/share/deepin/distribution.info");
214 DDesktopEntry entry(fg.fileName());
215 entry.setStringValue("Deepin", "Name", "Distribution");
216 entry.setStringValue("www.deepin.org", "WebsiteName", "Distribution");
217 entry.setStringValue("https://www.deepin.org", "Website", "Distribution");
218 entry.setStringValue("Logo.svg", "Logo", "Distribution");
219 entry.setStringValue("LogoLight.svg", "LogoLight", "Distribution");
220 entry.setStringValue("LogoTransparent.svg", "LogoTransparent", "Distribution");
221
222 entry.setStringValue("Deepin-Manufacturer", "Name", "Manufacturer");
223 entry.setStringValue("Deepin-Distributor", "Name", "Distributor");
224 ASSERT_TRUE(entry.save());
225
226 EnvGuard guard;
227 guard.set("XDG_DATA_HOME", "/tmp/share");
228 ASSERT_EQ(DSysInfo::distributionInfoPath(), fg.fileName());
229 auto website = DSysInfo::distributionOrgWebsite();
230 ASSERT_EQ(website.first, "www.deepin.org");
231 ASSERT_EQ(website.second, "https://www.deepin.org");
232 ASSERT_EQ(DSysInfo::distributionOrgLogo(DSysInfo::Distribution, DSysInfo::Normal), "Logo.svg");
233 ASSERT_EQ(DSysInfo::distributionOrgLogo(DSysInfo::Distribution, DSysInfo::Light), "LogoLight.svg");
234 ASSERT_EQ(DSysInfo::distributionOrgLogo(DSysInfo::Distribution, DSysInfo::Symbolic), ""); // not set
235 ASSERT_EQ(DSysInfo::distributionOrgLogo(DSysInfo::Distribution, DSysInfo::Transparent), "LogoTransparent.svg");
236
237 ASSERT_EQ(DSysInfo::distributionOrgName(DSysInfo::Distribution), "Deepin");
238 ASSERT_EQ(DSysInfo::distributionOrgName(DSysInfo::Distributor), "Deepin-Distributor");
239 ASSERT_EQ(DSysInfo::distributionOrgName(DSysInfo::Manufacturer), "Deepin-Manufacturer");
240
241 guard.restore();
242 }
243
244 TEST_F(ut_DSysInfo, osRelease)
245 {
246 FileGuard fg("/tmp/etc/os-release");
247 DDesktopEntry entry(fg.fileName());
248 entry.setStringValue("Deepin", "ID", "Release");
249 entry.setStringValue("20.9", "VERSION_ID", "Release");
250 entry.setStringValue("Deepin 20.9", "PRETTY_NAME", "Release");
251 ASSERT_TRUE(entry.save());
252
253 ASSERT_EQ(DSysInfo::operatingSystemName(), "Deepin 20.9");
254 ASSERT_EQ(DSysInfo::productType(), DSysInfo::Deepin);
255 ASSERT_EQ(DSysInfo::productTypeString(), "Deepin");
256 ASSERT_EQ(DSysInfo::productVersion(), "20.9");
257
258 // isDeepin
259 ASSERT_TRUE(DSysInfo::isDeepin());
260 entry.setStringValue("Uos", "ID", "Release");
261 ASSERT_TRUE(entry.save());
262 ASSERT_TRUE(DSysInfo::isDeepin());
263
264 entry.setStringValue("arch", "ID", "Release");
265 ASSERT_TRUE(entry.save());
266 ASSERT_FALSE(DSysInfo::isDeepin());
267
268 QString types[] = {"UnknownType", "Deepin", "Arch", "CentOS", "Debian",
269 "Fedora", "LinuxMint", "Manjaro", "openSUSE","SailfishOS",
270 "Ubuntu", "Uos", "Gentoo", "NixOS"};
271 for (int i = DSysInfo::UnknownType; i <= DSysInfo::NixOS; ++i) {
272 entry.setStringValue(types[i], "ID", "Release");
273 ASSERT_TRUE(entry.save());
274 ASSERT_EQ(DSysInfo::productType(), DSysInfo::ProductType(i));
275 }
276 }
277
278 TEST_F(ut_DSysInfo, isDDE)
279 {
280 FileGuard fg("/tmp/etc/os-release");
281 DDesktopEntry entry(fg.fileName());
282 entry.setStringValue("Deepin 20.9", "PRETTY_NAME", "Release");
283 entry.setStringValue("Deepin", "ID", "Release");
284 entry.setStringValue("20.9", "VERSION_ID", "Release");
285 ASSERT_TRUE(entry.save());
286
287 FileGuard fg2("/tmp/etc/deepin-version");
288 DDesktopEntry entry2(fg2.fileName());
289 entry2.setStringValue("20.9", "Version", "Release");
290 entry2.setStringValue("Desktop", "Type", "Release");
291 ASSERT_TRUE(entry2.save());
292
293 // isDeepin && deepinType valid
294 ASSERT_TRUE(DSysInfo::isDeepin());
295 ASSERT_TRUE(DSysInfo::isDDE());
296
297 // isDeepin but deepinType unknow
298 entry2.setStringValue("Unknown", "Type", "Release");
299 ASSERT_TRUE(entry2.save());
300 ASSERT_TRUE(DSysInfo::isDeepin());
301 ASSERT_FALSE(DSysInfo::isDDE());
302
303 // !isDeepin && XDG_SESSION_DESKTOP == dde or deepin
304 {
305 entry.setStringValue("Unknown", "ID", "Release");
306 ASSERT_TRUE(entry.save());
307 ASSERT_FALSE(DSysInfo::isDeepin());
308 EnvGuard guard;
309
310 guard.set("XDG_SESSION_DESKTOP", "dde");
311 ASSERT_TRUE(DSysInfo::isDDE());
312 guard.restore();
313
314 guard.set("XDG_SESSION_DESKTOP", "deepin");
315 ASSERT_TRUE(DSysInfo::isDDE());
316 guard.restore();
317
318 guard.set("XDG_SESSION_DESKTOP", "Unknown");
319 ASSERT_FALSE(DSysInfo::isDDE());
320 guard.restore();
321 }
322
323 }
324
325 TEST_F(ut_DSysInfo, deepinVersion)
326 {
327 FileGuard fg("/tmp/etc/deepin-version");
328 DDesktopEntry entry(fg.fileName());
329 entry.setStringValue("20.9", "Version", "Release");
330 entry.setStringValue("Desktop", "Type", "Release");
331 entry.setStringValue("社区版", "Type[zh_CN]", "Release");
332 entry.setStringValue("Y2020E0001", "Edition", "Release");
333 entry.setStringValue("Y2020CR001", "Copyright", "Release");
334 entry.setStringValue("build1", "Buildid", "Addition");
335 //entry.setStringValue("", "Milestone", "Addition");
336 ASSERT_TRUE(entry.save());
337
338 ASSERT_EQ(DSysInfo::deepinTypeDisplayName(QLocale("C")), "Desktop");
339 ASSERT_EQ(DSysInfo::deepinTypeDisplayName(QLocale("zh_CN")), "社区版");
340 ASSERT_EQ(DSysInfo::deepinVersion(), "20.9");
341 ASSERT_EQ(DSysInfo::deepinEdition(), "Y2020E0001");
342 ASSERT_EQ(DSysInfo::deepinCopyright(), "Y2020CR001");
343
344 qInfo() << "DSysInfo::deepinType()" << DSysInfo::deepinType();
345 ASSERT_EQ(DSysInfo::deepinType(), DSysInfo::DeepinDesktop);
346 {
347 // isCommunityEdition Not Uos
348 FileGuard fg("/tmp/etc/os-release");
349 DDesktopEntry entry(fg.fileName());
350 entry.setStringValue("Deepin", "ID", "Release");
351 ASSERT_TRUE(entry.save());
352 ASSERT_TRUE(DSysInfo::isCommunityEdition());
353
354 entry.setStringValue("Uos", "ID", "Release");
355 ASSERT_TRUE(entry.save());
356 ASSERT_FALSE(DSysInfo::isCommunityEdition());
357 }
358
359 entry.setStringValue("Professional", "Type", "Release");
360 ASSERT_TRUE(entry.save());
361 ASSERT_EQ(DSysInfo::deepinType(), DSysInfo::DeepinProfessional);
362 ASSERT_FALSE(DSysInfo::isCommunityEdition());
363
364 entry.setStringValue("Server", "Type", "Release");
365 ASSERT_TRUE(entry.save());
366 ASSERT_EQ(DSysInfo::deepinType(), DSysInfo::DeepinServer);
367 ASSERT_FALSE(DSysInfo::isCommunityEdition());
368
369 entry.setStringValue("Personal", "Type", "Release");
370 ASSERT_TRUE(entry.save());
371 ASSERT_EQ(DSysInfo::deepinType(), DSysInfo::DeepinPersonal);
372 ASSERT_FALSE(DSysInfo::isCommunityEdition());
373 }
374
375 TEST_F(ut_DSysInfo, other)
376 {
377 qDebug() << DSysInfo::computerName();
378 qDebug() << DSysInfo::memoryInstalledSize();
379 qDebug() << DSysInfo::memoryTotalSize();
380 qDebug() << DSysInfo::systemDiskSize();
381 }