| 1 |
// SPDX-FileCopyrightText: 2021 - 2023 UnionTech Software Technology Co., Ltd. |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 |
|
| 5 |
#include <DConfigFile> |
| 6 |
#include <DStandardPaths> |
| 7 |
#include <QBuffer> |
| 8 |
#include <QDir> |
| 9 |
|
| 10 |
#include <gtest/gtest.h> |
| 11 |
#include "test_helper.hpp" |
| 12 |
|
| 13 |
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0) |
| 14 |
#define type typeId // In qt6 type is deprecated and typeId should be used, this macro is for more convenient compatibility with qt6 |
| 15 |
#endif |
| 16 |
|
| 17 |
DCORE_USE_NAMESPACE |
| 18 |
|
| 19 |
static constexpr char const *LocalPrefix = "/tmp/example"; |
| 20 |
static QString NoAppId; |
| 21 |
|
| 22 |
class ut_DConfigFile : public testing::Test |
| 23 |
{ |
| 24 |
protected: |
| 25 |
static void SetUpTestCase() { |
| 26 |
home.set("HOME", "/tmp/home"); |
| 27 |
dsgDataDir.set("DSG_DATA_DIRS", PREFIX"/share/dsg"); |
| 28 |
} |
| 29 |
static void TearDownTestCase() { |
| 30 |
dsgDataDir.restore(); |
| 31 |
home.restore(); |
| 32 |
} |
| 33 |
virtual void TearDown() override; |
| 34 |
|
| 35 |
const char *APP_ID = "org.foo.appid"; |
| 36 |
const char *FILE_NAME = "org.foo.name"; |
| 37 |
QString metaPath = QString("%1" PREFIX"/share/dsg/configs/%2").arg(LocalPrefix, APP_ID); |
| 38 |
QString metaGlobalPath = QString("%1" PREFIX"/share/dsg/configs").arg(LocalPrefix); |
| 39 |
QString overridePath = QString("%1" PREFIX"/share/dsg/configs/overrides/%2/%3").arg(LocalPrefix, APP_ID, FILE_NAME); |
| 40 |
const char *OTHER_APP_ID = "tests_other"; |
| 41 |
QString noAppidMetaPath = QString("%1" PREFIX"/share/dsg/configs").arg(LocalPrefix); |
| 42 |
QString noAppidOverridePath = QString("%1" PREFIX"/share/dsg/configs/overrides/%2").arg(LocalPrefix, FILE_NAME); |
| 43 |
uint uid = getuid(); |
| 44 |
static EnvGuard dsgDataDir; |
| 45 |
static EnvGuard home; |
| 46 |
}; |
| 47 |
EnvGuard ut_DConfigFile::dsgDataDir; |
| 48 |
EnvGuard ut_DConfigFile::home; |
| 49 |
|
| 50 |
|
| 51 |
void ut_DConfigFile::TearDown() { |
| 52 |
QDir(LocalPrefix).removeRecursively(); |
| 53 |
} |
| 54 |
|
| 55 |
TEST_F(ut_DConfigFile, testLoad) { |
| 56 |
QByteArray meta = R"delimiter( |
| 57 |
{ |
| 58 |
"magic": "dsg.config.meta", |
| 59 |
"version": "1.0", |
| 60 |
"contents": { |
| 61 |
"canExit": { |
| 62 |
"value": false, |
| 63 |
"serial": 0, |
| 64 |
"name": "I am name", |
| 65 |
"name[zh_CN]": "我是名字", |
| 66 |
"description": "我是描述", |
| 67 |
"description[en_US]": "I am description", |
| 68 |
"permissions": "readwrite", |
| 69 |
"visibility": "private" |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
)delimiter"; |
| 74 |
|
| 75 |
QBuffer buffer; |
| 76 |
buffer.setData(meta); |
| 77 |
|
| 78 |
DConfigFile config(APP_ID, FILE_NAME); |
| 79 |
ASSERT_TRUE(config.load(&buffer, {})); |
| 80 |
ASSERT_EQ(config.meta()->keyList(), QStringList{QLatin1String("canExit")}); |
| 81 |
|
| 82 |
QScopedPointer<DConfigCache> userCache(config.createUserCache(uid)); |
| 83 |
ASSERT_EQ(config.value("canExit", userCache.get()).toBool(), false); |
| 84 |
|
| 85 |
ASSERT_EQ(config.meta()->version().major, 1); |
| 86 |
ASSERT_EQ(config.meta()->version().minor, 0); |
| 87 |
|
| 88 |
ASSERT_EQ(config.meta()->visibility("canExit"), DConfigFile::Private); |
| 89 |
|
| 90 |
ASSERT_EQ(config.meta()->displayName("canExit", QLocale::AnyLanguage), "I am name"); |
| 91 |
ASSERT_EQ(config.meta()->displayName("canExit", QLocale::Chinese), QString("我是名字")); |
| 92 |
|
| 93 |
ASSERT_EQ(config.meta()->description("canExit", QLocale::AnyLanguage), "我是描述"); |
| 94 |
ASSERT_EQ(config.meta()->description("canExit", QLocale::English), "I am description"); |
| 95 |
|
| 96 |
ASSERT_EQ(config.meta()->permissions("canExit"), DConfigFile::ReadWrite); |
| 97 |
} |
| 98 |
|
| 99 |
TEST_F(ut_DConfigFile, setValueTypeCheck) { |
| 100 |
|
| 101 |
FileCopyGuard guard(":/data/dconf-example.meta.json", QString("%1/%2.json").arg(metaPath, FILE_NAME)); |
| 102 |
DConfigFile config(APP_ID, FILE_NAME); |
| 103 |
config.load(LocalPrefix); |
| 104 |
QScopedPointer<DConfigCache> userCache(config.createUserCache(uid)); |
| 105 |
userCache->load(LocalPrefix); |
| 106 |
{ |
| 107 |
const auto type = config.value("canExit", userCache.get()).type(); |
| 108 |
ASSERT_TRUE(config.setValue("canExit", false, "test", userCache.get())); |
| 109 |
ASSERT_TRUE(config.setValue("canExit", "true", "test", userCache.get())); |
| 110 |
ASSERT_FALSE(config.setValue("canExit", "true2", "test", userCache.get())); |
| 111 |
ASSERT_EQ(config.value("canExit", userCache.get()).type(), type); |
| 112 |
} |
| 113 |
{ |
| 114 |
const auto type = config.value("key2", userCache.get()).type(); |
| 115 |
ASSERT_TRUE(config.setValue("key2", "121", "test", userCache.get())); |
| 116 |
ASSERT_FALSE(config.setValue("key2", 121, "test", userCache.get())); |
| 117 |
ASSERT_EQ(config.value("key2", userCache.get()).type(), type); |
| 118 |
} |
| 119 |
{ |
| 120 |
const auto type = config.value("number", userCache.get()).type(); |
| 121 |
ASSERT_TRUE(config.setValue("number", 1, "test", userCache.get())); |
| 122 |
ASSERT_TRUE(config.setValue("number", 2.0, "test", userCache.get())); |
| 123 |
ASSERT_TRUE(config.setValue("number", "3", "test", userCache.get())); |
| 124 |
ASSERT_FALSE(config.setValue("number", "1ab", "test", userCache.get())); |
| 125 |
ASSERT_EQ(config.value("number", userCache.get()).type(), type); |
| 126 |
} |
| 127 |
{ |
| 128 |
const auto type = config.value("numberDouble", userCache.get()).type(); |
| 129 |
ASSERT_TRUE(config.setValue("numberDouble", 1.2, "test", userCache.get())); |
| 130 |
ASSERT_EQ(config.value("numberDouble", userCache.get()), 1.2); |
| 131 |
} |
| 132 |
{ |
| 133 |
const auto type = config.value("array", userCache.get()).type(); |
| 134 |
const QStringList array{"value1", "value2"}; |
| 135 |
ASSERT_TRUE(config.setValue("array", QStringList(), "test", userCache.get())); |
| 136 |
ASSERT_TRUE(config.setValue("array", array, "test", userCache.get())); |
| 137 |
ASSERT_TRUE(config.setValue("array", QJsonDocument::fromJson("[]").toVariant(), "test", userCache.get())); |
| 138 |
ASSERT_FALSE(config.setValue("array", "", "test", userCache.get())); |
| 139 |
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) |
| 140 |
ASSERT_FALSE(config.setValue("array", "value1", "test", userCache.get())); |
| 141 |
#else |
| 142 |
ASSERT_TRUE(config.setValue("array", "value1", "test", userCache.get())); |
| 143 |
#endif |
| 144 |
ASSERT_EQ(config.value("array", userCache.get()).type(), type); |
| 145 |
} |
| 146 |
{ |
| 147 |
const auto type = config.value("array_map", userCache.get()).type(); |
| 148 |
QVariantList array; |
| 149 |
QVariantMap map1; |
| 150 |
map1["key1"] = "value1"; |
| 151 |
map1["key2"] = "value2"; |
| 152 |
array.append(map1); |
| 153 |
ASSERT_EQ(config.value("array_map", userCache.get()).toList(), array); |
| 154 |
ASSERT_TRUE(config.setValue("array_map", QVariantList(), "test", userCache.get())); |
| 155 |
ASSERT_TRUE(config.setValue("array_map", array, "test", userCache.get())); |
| 156 |
ASSERT_TRUE(config.setValue("array_map", QJsonDocument::fromJson("[]").toVariant(), "test", userCache.get())); |
| 157 |
ASSERT_FALSE(config.setValue("array_map", "", "test", userCache.get())); |
| 158 |
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) |
| 159 |
ASSERT_FALSE(config.setValue("array_map", "value1", "test", userCache.get())); |
| 160 |
#else |
| 161 |
ASSERT_TRUE(config.setValue("array_map", "value1", "test", userCache.get())); |
| 162 |
#endif |
| 163 |
ASSERT_EQ(config.value("array_map", userCache.get()).type(), type); |
| 164 |
} |
| 165 |
{ |
| 166 |
const auto type = config.value("map", userCache.get()).type(); |
| 167 |
QVariantMap map; |
| 168 |
map.insert("key1", "value1"); |
| 169 |
map.insert("key2", "value2"); |
| 170 |
ASSERT_TRUE(config.setValue("map", QVariantMap(), "test", userCache.get())); |
| 171 |
ASSERT_TRUE(config.setValue("map", map, "test", userCache.get())); |
| 172 |
ASSERT_TRUE(config.setValue("map", QJsonDocument::fromJson("{}").toVariant(), "test", userCache.get())); |
| 173 |
ASSERT_FALSE(config.setValue("map", QJsonDocument::fromJson("[]").toVariant(), "test", userCache.get())); |
| 174 |
ASSERT_FALSE(config.setValue("map", "key1", "test", userCache.get())); |
| 175 |
ASSERT_EQ(config.value("map", userCache.get()).type(), type); |
| 176 |
} |
| 177 |
{ |
| 178 |
const auto type = config.value("map_array", userCache.get()).type(); |
| 179 |
QVariantMap map; |
| 180 |
map.insert("key1", QStringList{"value1"}); |
| 181 |
map.insert("key2", QStringList{"value2"}); |
| 182 |
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) |
| 183 |
ASSERT_EQ(config.value("map_array", userCache.get()).toMap(), map); |
| 184 |
#else |
| 185 |
auto ret = config.value("map_array", userCache.get()).toMap(); |
| 186 |
ASSERT_EQ(ret.keys(), map.keys()); |
| 187 |
auto value1 = ret.values(); |
| 188 |
auto value2 = map.values(); |
| 189 |
ASSERT_EQ(value1.size(), value2.size()); |
| 190 |
for(std::size_t i = 0; i < value1.size(); ++i){ |
| 191 |
ASSERT_EQ(value1[i].toStringList(), value2[i].toStringList()); |
| 192 |
} |
| 193 |
#endif |
| 194 |
ASSERT_TRUE(config.setValue("map_array", QVariantMap(), "test", userCache.get())); |
| 195 |
ASSERT_TRUE(config.setValue("map_array", map, "test", userCache.get())); |
| 196 |
ASSERT_TRUE(config.setValue("map_array", QJsonDocument::fromJson("{}").toVariant(), "test", userCache.get())); |
| 197 |
ASSERT_FALSE(config.setValue("map_array", "", "test", userCache.get())); |
| 198 |
ASSERT_FALSE(config.setValue("map_array", "value1", "test", userCache.get())); |
| 199 |
ASSERT_EQ(config.value("map_array", userCache.get()).type(), type); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
TEST_F(ut_DConfigFile, fileIODevice) { |
| 204 |
|
| 205 |
FileCopyGuard guard(":/data/dconf-example.meta.json", QString("%1/%2.json").arg(metaPath, FILE_NAME)); |
| 206 |
{ |
| 207 |
DConfigFile config(APP_ID, FILE_NAME); |
| 208 |
ASSERT_TRUE(config.load(LocalPrefix)); |
| 209 |
QScopedPointer<DConfigCache> userCache(config.createUserCache(uid)); |
| 210 |
ASSERT_TRUE(userCache->load(LocalPrefix)); |
| 211 |
} |
| 212 |
{ |
| 213 |
DConfigFile config(APP_ID, FILE_NAME); |
| 214 |
config.load(LocalPrefix); |
| 215 |
QScopedPointer<DConfigCache> userCache(config.createUserCache(uid)); |
| 216 |
userCache->load(LocalPrefix); |
| 217 |
|
| 218 |
config.setValue("canExit", false, "test", userCache.get()); |
| 219 |
config.setValue("key2", QString("128"), "test", userCache.get()); |
| 220 |
|
| 221 |
ASSERT_TRUE(config.save(LocalPrefix)); |
| 222 |
ASSERT_TRUE(userCache->save(LocalPrefix)); |
| 223 |
} |
| 224 |
{ |
| 225 |
DConfigFile config(APP_ID, FILE_NAME); |
| 226 |
ASSERT_TRUE(config.load(LocalPrefix)); |
| 227 |
QScopedPointer<DConfigCache> userCache(config.createUserCache(uid)); |
| 228 |
ASSERT_TRUE(userCache->load(LocalPrefix)); |
| 229 |
ASSERT_EQ(config.value("canExit", userCache.get()), false); |
| 230 |
ASSERT_EQ(config.value("key2", userCache.get()).toString(), QString("128")); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
TEST_F(ut_DConfigFile, appmeta) { |
| 235 |
|
| 236 |
FileCopyGuard guard(":/data/dconf-example.meta.json", QString("%1/%2.json").arg(metaPath, FILE_NAME)); |
| 237 |
{ |
| 238 |
DConfigFile config(APP_ID, FILE_NAME); |
| 239 |
|
| 240 |
config.load(LocalPrefix); |
| 241 |
ASSERT_TRUE(config.load(LocalPrefix)); |
| 242 |
QScopedPointer<DConfigCache> userCache(config.createUserCache(uid)); |
| 243 |
ASSERT_TRUE(userCache->load(LocalPrefix)); |
| 244 |
ASSERT_EQ(config.value("key3", userCache.get()).toString(), QString("application")); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
TEST_F(ut_DConfigFile, globalmeta) { |
| 249 |
|
| 250 |
FileCopyGuard guard(":/data/dconf-global.meta.json", QString("%1/%2.json").arg(metaPath, FILE_NAME)); |
| 251 |
DConfigFile config(APP_ID, FILE_NAME); |
| 252 |
ASSERT_TRUE(config.load(LocalPrefix)); |
| 253 |
QScopedPointer<DConfigCache> userCache(config.createUserCache(uid)); |
| 254 |
ASSERT_TRUE(userCache->load(LocalPrefix)); |
| 255 |
ASSERT_EQ(config.value("key3", userCache.get()), QString("global")); |
| 256 |
} |
| 257 |
|
| 258 |
TEST_F(ut_DConfigFile, meta) { |
| 259 |
|
| 260 |
FileCopyGuard guard(":/data/dconf-example.meta.json", QString("%1/%2.json").arg(metaPath, FILE_NAME)); |
| 261 |
FileCopyGuard guard2(":/data/dconf-global.meta.json", QString("%1/%2.json").arg(metaGlobalPath, FILE_NAME)); |
| 262 |
DConfigFile config(APP_ID, FILE_NAME); |
| 263 |
ASSERT_TRUE(config.load(LocalPrefix)); |
| 264 |
QScopedPointer<DConfigCache> userCache(config.createUserCache(uid)); |
| 265 |
ASSERT_TRUE(userCache->load(LocalPrefix)); |
| 266 |
ASSERT_EQ(config.value("key3", userCache.get()), QString("application")); |
| 267 |
const QStringList array{"value1", "value2"}; |
| 268 |
ASSERT_EQ(config.value("array", userCache.get()).toStringList(), array); |
| 269 |
QVariantMap map; |
| 270 |
map.insert("key1", "value1"); |
| 271 |
map.insert("key2", "value2"); |
| 272 |
ASSERT_EQ(config.value("map", userCache.get()).toMap(), map); |
| 273 |
} |
| 274 |
|
| 275 |
TEST_F(ut_DConfigFile, fileOverride) { |
| 276 |
|
| 277 |
FileCopyGuard guard(":/data/dconf-example.meta.json", QString("%1/%2.json").arg(metaPath, FILE_NAME)); |
| 278 |
{ |
| 279 |
DConfigFile config(APP_ID, FILE_NAME); |
| 280 |
config.load(LocalPrefix); |
| 281 |
QScopedPointer<DConfigCache> userCache(config.createUserCache(uid)); |
| 282 |
ASSERT_TRUE(userCache->load(LocalPrefix)); |
| 283 |
ASSERT_EQ(config.value("key3", userCache.get()), QString("application")); |
| 284 |
} |
| 285 |
|
| 286 |
FileCopyGuard guard1(":/data/dconf-example.override.json", QString("%1/%2.json").arg(overridePath, FILE_NAME)); |
| 287 |
{ |
| 288 |
DConfigFile config(APP_ID, FILE_NAME); |
| 289 |
config.load(LocalPrefix); |
| 290 |
QScopedPointer<DConfigCache> userCache(config.createUserCache(uid)); |
| 291 |
ASSERT_TRUE(userCache->load(LocalPrefix)); |
| 292 |
ASSERT_EQ(config.value("key3", userCache.get()), QString("override")); |
| 293 |
} |
| 294 |
|
| 295 |
FileCopyGuard guard2(":/data/dconf-override/dconf-example.override.a.json", QString("%1/a/%2.json").arg(overridePath, FILE_NAME)); |
| 296 |
{ |
| 297 |
{ |
| 298 |
DConfigFile config(APP_ID, FILE_NAME, "/a"); |
| 299 |
config.load(LocalPrefix); |
| 300 |
QScopedPointer<DConfigCache> userCache(config.createUserCache(uid)); |
| 301 |
ASSERT_TRUE(userCache->load(LocalPrefix)); |
| 302 |
ASSERT_EQ(config.value("key3", userCache.get()).toString(), QString("override /a")); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
FileCopyGuard guard3(":/data/dconf-override/dconf-example.override.a.b.json", QString("%1/a/b/%2.json").arg(overridePath, FILE_NAME)); |
| 307 |
{ |
| 308 |
{ |
| 309 |
DConfigFile config(APP_ID, FILE_NAME, "/a/b"); |
| 310 |
config.load(LocalPrefix); |
| 311 |
QScopedPointer<DConfigCache> userCache(config.createUserCache(uid)); |
| 312 |
ASSERT_TRUE(userCache->load(LocalPrefix)); |
| 313 |
ASSERT_EQ(config.value("key3", userCache.get()).toString(), QString("override /a/b")); |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
TEST_F(ut_DConfigFile, fileOverrideNoExistItem) { |
| 319 |
|
| 320 |
FileCopyGuard guard(":/data/dconf-example.meta.json", QString("%1/%2.json").arg(metaPath, FILE_NAME)); |
| 321 |
FileCopyGuard guard2(":/data/dconf-example.override.noexistitem.json", QString("%1/%2.json").arg(overridePath, FILE_NAME)); |
| 322 |
{ |
| 323 |
DConfigFile config(APP_ID, FILE_NAME); |
| 324 |
ASSERT_TRUE(config.load(LocalPrefix)); |
| 325 |
ASSERT_FALSE(config.meta()->keyList().contains("noexistitem")); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
TEST_F(ut_DConfigFile, noAppIdWithGlobalConfiguration) { |
| 330 |
|
| 331 |
FileCopyGuard guard(":/data/dconf-example.meta.json", QString("%1/%2.json").arg(noAppidMetaPath, FILE_NAME)); |
| 332 |
{ |
| 333 |
DConfigFile config(NoAppId, FILE_NAME); |
| 334 |
ASSERT_TRUE(config.load(LocalPrefix)); |
| 335 |
ASSERT_EQ(config.value("key3"), QString("application")); |
| 336 |
config.setValue("key3", "global-with-no-appid", "test"); |
| 337 |
config.save(LocalPrefix); |
| 338 |
} |
| 339 |
{ |
| 340 |
DConfigFile config(NoAppId, FILE_NAME); |
| 341 |
config.load(LocalPrefix); |
| 342 |
ASSERT_EQ(config.value("key3"), "global-with-no-appid"); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
TEST_F(ut_DConfigFile, noAppIdWithUserConfiguration) { |
| 347 |
|
| 348 |
FileCopyGuard guard(":/data/dconf-example.meta.json", QString("%1/%2.json").arg(noAppidMetaPath, FILE_NAME)); |
| 349 |
{ |
| 350 |
DConfigFile config(NoAppId, FILE_NAME); |
| 351 |
ASSERT_TRUE(config.load(LocalPrefix)); |
| 352 |
QScopedPointer<DConfigCache> userCache(config.createUserCache(uid)); |
| 353 |
ASSERT_TRUE(userCache->load(LocalPrefix)); |
| 354 |
ASSERT_EQ(config.value("key2", userCache.get()), QString("125")); |
| 355 |
config.setValue("key2", "user-with-no-appid", "test", userCache.get()); |
| 356 |
ASSERT_TRUE(userCache->save(LocalPrefix)); |
| 357 |
} |
| 358 |
{ |
| 359 |
DConfigFile config(NoAppId, FILE_NAME); |
| 360 |
config.load(LocalPrefix); |
| 361 |
QScopedPointer<DConfigCache> userCache(config.createUserCache(uid)); |
| 362 |
userCache->load(LocalPrefix); |
| 363 |
ASSERT_EQ(config.value("key2", userCache.get()), "user-with-no-appid"); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
TEST_F(ut_DConfigFile, noAppIdUserConfiguration) { |
| 368 |
|
| 369 |
// meta安装在公共目录,使用空的appid设置配置项, 则无论是否传入appid,获取的是均为空appid的cache值 |
| 370 |
FileCopyGuard guard(":/data/dconf-example.meta.json", QString("%1/%2.json").arg(noAppidMetaPath, FILE_NAME)); |
| 371 |
{ |
| 372 |
// 不传入appid设置配置项 |
| 373 |
DConfigFile config(NoAppId, FILE_NAME); |
| 374 |
config.load(LocalPrefix); |
| 375 |
QScopedPointer<DConfigCache> userCache(config.createUserCache(uid)); |
| 376 |
userCache->load(LocalPrefix); |
| 377 |
config.setValue("key2", "user-with-no-appid", "test", userCache.get()); |
| 378 |
userCache->save(LocalPrefix); |
| 379 |
} |
| 380 |
{ |
| 381 |
// 不传入appid,获取的为空appid的值 |
| 382 |
DConfigFile config(NoAppId, FILE_NAME); |
| 383 |
config.load(LocalPrefix); |
| 384 |
QScopedPointer<DConfigCache> userCache(config.createUserCache(uid)); |
| 385 |
userCache->load(LocalPrefix); |
| 386 |
ASSERT_EQ(config.value("key2", userCache.get()), "user-with-no-appid"); |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
TEST_F(ut_DConfigFile, appIdOverrideNoAppIdUserConfiguration) { |
| 391 |
|
| 392 |
// meta安装在公共目录,appid覆盖了meta文件,使用空的appid设置配置项, 则无论是否传入appid,获取的是均为空appid的cache值 |
| 393 |
FileCopyGuard guard(":/data/dconf-example.meta.json", QString("%1/%2.json").arg(noAppidMetaPath, FILE_NAME)); |
| 394 |
FileCopyGuard guard2(":/data/dconf-example.meta.json", QString("%1/%2.json").arg(metaPath, FILE_NAME)); |
| 395 |
{ |
| 396 |
DConfigFile config(NoAppId, FILE_NAME); |
| 397 |
config.load(LocalPrefix); |
| 398 |
QScopedPointer<DConfigCache> userCache(config.createUserCache(uid)); |
| 399 |
userCache->load(LocalPrefix); |
| 400 |
config.setValue("key2", "user-with-no-appid", "test", userCache.get()); |
| 401 |
userCache->save(LocalPrefix); |
| 402 |
} |
| 403 |
{ |
| 404 |
// 不传入appid,获取的为空appid的值 |
| 405 |
DConfigFile config(NoAppId, FILE_NAME); |
| 406 |
config.load(LocalPrefix); |
| 407 |
QScopedPointer<DConfigCache> userCache(config.createUserCache(uid)); |
| 408 |
userCache->load(LocalPrefix); |
| 409 |
ASSERT_EQ(config.value("key2", userCache.get()), "user-with-no-appid"); |
| 410 |
} |
| 411 |
{ |
| 412 |
// 传入appid设置配置项 |
| 413 |
DConfigFile config(APP_ID, FILE_NAME); |
| 414 |
config.load(LocalPrefix); |
| 415 |
QScopedPointer<DConfigCache> userCache(config.createUserCache(uid)); |
| 416 |
userCache->load(LocalPrefix); |
| 417 |
config.setValue("key2", "user-with-appid", "test", userCache.get()); |
| 418 |
userCache->save(LocalPrefix); |
| 419 |
} |
| 420 |
{ |
| 421 |
// 不传入appid,获取的是空appid的值 |
| 422 |
DConfigFile config(NoAppId, FILE_NAME); |
| 423 |
config.load(LocalPrefix); |
| 424 |
QScopedPointer<DConfigCache> userCache(config.createUserCache(uid)); |
| 425 |
userCache->load(LocalPrefix); |
| 426 |
ASSERT_EQ(config.value("key2", userCache.get()), "user-with-no-appid"); |
| 427 |
} |
| 428 |
{ |
| 429 |
// 传入appid,获取的是含appid的值 |
| 430 |
DConfigFile config(APP_ID, FILE_NAME); |
| 431 |
config.load(LocalPrefix); |
| 432 |
QScopedPointer<DConfigCache> userCache(config.createUserCache(uid)); |
| 433 |
userCache->load(LocalPrefix); |
| 434 |
ASSERT_EQ(config.value("key2", userCache.get()).toString().toStdString(), "user-with-appid"); |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
TEST_F(ut_DConfigFile, setCachePathPrefix) { |
| 439 |
|
| 440 |
FileCopyGuard guard(":/data/dconf-example.meta.json", QString("%1/%2.json").arg(metaPath, FILE_NAME)); |
| 441 |
{ |
| 442 |
DConfigFile config(APP_ID, FILE_NAME); |
| 443 |
config.globalCache()->setCachePathPrefix("/configs-global"); |
| 444 |
ASSERT_TRUE(config.load(LocalPrefix)); |
| 445 |
QScopedPointer<DConfigCache> userCache(config.createUserCache(uid)); |
| 446 |
userCache->setCachePathPrefix("/configs-user"); |
| 447 |
ASSERT_TRUE(userCache->load(LocalPrefix)); |
| 448 |
ASSERT_EQ(config.value("key2", userCache.get()), QString("125")); |
| 449 |
ASSERT_EQ(config.value("key3", userCache.get()), QString("application")); |
| 450 |
|
| 451 |
config.setValue("key2", QString("user-config"), "test", userCache.get()); |
| 452 |
config.setValue("key3", "global-config", "test", userCache.get()); |
| 453 |
config.save(LocalPrefix); |
| 454 |
userCache->save(LocalPrefix); |
| 455 |
} |
| 456 |
{ |
| 457 |
DConfigFile config(APP_ID, FILE_NAME); |
| 458 |
config.globalCache()->setCachePathPrefix("/configs-global"); |
| 459 |
ASSERT_TRUE(config.load(LocalPrefix)); |
| 460 |
QScopedPointer<DConfigCache> userCache(config.createUserCache(uid)); |
| 461 |
userCache->setCachePathPrefix("/configs-user"); |
| 462 |
ASSERT_TRUE(userCache->load(LocalPrefix)); |
| 463 |
|
| 464 |
ASSERT_EQ(config.value("key2", userCache.get()), QString("user-config")); |
| 465 |
ASSERT_EQ(config.value("key3", userCache.get()), QString("global-config")); |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
TEST_F(ut_DConfigFile, setSubpath) { |
| 470 |
|
| 471 |
FileCopyGuard guard(":/data/dconf-example.meta.json", QString("%1/%2.json").arg(metaPath, FILE_NAME)); |
| 472 |
{ |
| 473 |
DConfigFile config(APP_ID, FILE_NAME); |
| 474 |
ASSERT_TRUE(config.load(LocalPrefix)); |
| 475 |
} |
| 476 |
{ |
| 477 |
DConfigFile config(APP_ID, FILE_NAME, "/a/b"); |
| 478 |
ASSERT_TRUE(config.load(LocalPrefix)); |
| 479 |
} |
| 480 |
{ |
| 481 |
DConfigFile config(APP_ID, FILE_NAME, "/a/b/.."); |
| 482 |
ASSERT_TRUE(config.load(LocalPrefix)); |
| 483 |
} |
| 484 |
{ |
| 485 |
DConfigFile config(APP_ID, FILE_NAME, "/../a/b"); |
| 486 |
ASSERT_FALSE(config.load(LocalPrefix)); |
| 487 |
} |
| 488 |
{ |
| 489 |
DConfigFile config(APP_ID, FILE_NAME, "/a/b/../../.."); |
| 490 |
ASSERT_FALSE(config.load(LocalPrefix)); |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
TEST_F(ut_DConfigFile, userPublic) { |
| 495 |
|
| 496 |
FileCopyGuard guard(":/data/dconf-example.meta.json", QString("%1/%2.json").arg(metaPath, FILE_NAME)); |
| 497 |
{ |
| 498 |
DConfigFile config(APP_ID, FILE_NAME); |
| 499 |
ASSERT_TRUE(config.load(LocalPrefix)); |
| 500 |
ASSERT_TRUE(config.meta()->flags("publicConfig").testFlag(DConfigFile::UserPublic)); |
| 501 |
ASSERT_FALSE(config.meta()->flags("canExit").testFlag(DConfigFile::UserPublic)); |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
class ut_DConfigFileCheckName : public ut_DConfigFile, public ::testing::WithParamInterface<std::tuple<QString, bool>> |
| 506 |
{ |
| 507 |
|
| 508 |
}; |
| 509 |
|
| 510 |
TEST_P(ut_DConfigFileCheckName, checkName) |
| 511 |
{ |
| 512 |
const auto [fileName, isValid] = GetParam(); |
| 513 |
FileCopyGuard guard(":/data/dconf-example.meta.json", QString("%1/%2.json").arg(metaPath, fileName)); |
| 514 |
DConfigFile config(APP_ID, fileName); |
| 515 |
ASSERT_EQ(config.load(LocalPrefix), isValid); |
| 516 |
} |
| 517 |
INSTANTIATE_TEST_SUITE_P(checkName, ut_DConfigFileCheckName, |
| 518 |
::testing::Values( |
| 519 |
std::tuple{QString("org-foo"), true}, |
| 520 |
std::tuple{QString("org foo"), true}, |
| 521 |
std::tuple{QString("org.foo2"), true}, |
| 522 |
std::tuple{QString("org/foo"), false}, |
| 523 |
std::tuple{QString("./org-foo"), false}, |
| 524 |
std::tuple{QString("../configs/org-foo"), false})); |
| 525 |
|
| 526 |
class ut_DConfigFileCheckAppId : public ut_DConfigFile, public ::testing::WithParamInterface<std::tuple<QString, bool>> |
| 527 |
{ |
| 528 |
|
| 529 |
}; |
| 530 |
|
| 531 |
TEST_P(ut_DConfigFileCheckAppId, checkAppId) |
| 532 |
{ |
| 533 |
const auto [appId, isValid] = GetParam(); |
| 534 |
FileCopyGuard guard(":/data/dconf-example.meta.json", QString("%1/%2/%3.json").arg(noAppidMetaPath, appId, FILE_NAME)); |
| 535 |
DConfigFile config(appId, FILE_NAME); |
| 536 |
ASSERT_EQ(config.load(LocalPrefix), isValid); |
| 537 |
} |
| 538 |
INSTANTIATE_TEST_SUITE_P(checkAppId, ut_DConfigFileCheckAppId, |
| 539 |
::testing::Values( |
| 540 |
std::tuple{NoAppId, true}, |
| 541 |
std::tuple{QString("org-foo"), true}, |
| 542 |
std::tuple{QString("org foo"), false}, |
| 543 |
std::tuple{QString("org.foo2"), true}, |
| 544 |
std::tuple{QString("org/foo"), false}, |
| 545 |
std::tuple{QString("./org-foo"), false}, |
| 546 |
std::tuple{QString("../configs/org-foo"), false})); |