summaryrefslogtreecommitdiff
path: root/archived/projt-launcher/tests/INIFile_test.cpp
blob: dba12baaf3fcec2d0b10b025031f28371826aea6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: 2026 Project Tick
// SPDX-FileContributor: Project Tick Team
/*
 *  ProjT Launcher - Minecraft Launcher
 *  Copyright (C) 2026 Project Tick
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, version 3.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, write to the Free Software Foundation,
 *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <QTest>

#include <settings/INIFile.h>
#include <QList>
#include <QSettings>
#include <QTemporaryFile>
#include <QVariant>
#include "FileSystem.h"

#include <QVariantUtils.h>

class IniFileTest : public QObject
{
	Q_OBJECT
  private slots:
	void initTestCase()
	{}
	void cleanupTestCase()
	{}

	void test_Escape_data()
	{
		QTest::addColumn<QString>("through");

		QTest::newRow("unix path") << "/abc/def/ghi/jkl";
		QTest::newRow("windows path") << "C:\\Program files\\terrible\\name\\of something\\";
		QTest::newRow("Plain text") << "Lorem ipsum dolor sit amet.";
		QTest::newRow("Escape sequences") << "Lorem\n\t\n\\n\\tAAZ\nipsum dolor\n\nsit amet.";
		QTest::newRow("Escape sequences 2") << "\"\n\n\"";
		QTest::newRow("Hashtags") << "some data#something";
	}

	void test_SaveLoad()
	{
		QString a		 = "a";
		QString b		 = "a\nb\t\n\\\\\\C:\\Program files\\terrible\\name\\of something\\#thisIsNotAComment";
		QString filename = "test_SaveLoad.ini";

		// save
		INIFile f;
		f.set("a", a);
		f.set("b", b);
		f.saveFile(filename);

		// load
		INIFile f2;
		f2.loadFile(filename);
		QCOMPARE(f2.get("a", "NOT SET").toString(), a);
		QCOMPARE(f2.get("b", "NOT SET").toString(), b);
	}

	void test_SaveLoadLists()
	{
		QString slist_strings	 = "(\"a\",\"b\",\"c\")";
		QStringList list_strings = { "a", "b", "c" };

		QString slist_numbers	= "(1,2,3,10)";
		QList<int> list_numbers = { 1, 2, 3, 10 };

		QString filename = "test_SaveLoadLists.ini";

		INIFile f;
		f.set("list_strings", list_strings);
		f.set("list_numbers", QVariantUtils::fromList(list_numbers));
		f.saveFile(filename);

		// load
		INIFile f2;
		f2.loadFile(filename);

		QStringList out_list_strings = f2.get("list_strings", QStringList()).toStringList();
		qDebug() << "OutStringList" << out_list_strings;

		QList<int> out_list_numbers =
			QVariantUtils::toList<int>(f2.get("list_numbers", QVariantUtils::fromList(QList<int>())));
		qDebug() << "OutNumbersList" << out_list_numbers;

		QCOMPARE(out_list_strings, list_strings);
		QCOMPARE(out_list_numbers, list_numbers);
	}

	void test_SaveAlreadyExistingFile()
	{
		QString fileContent = R"(InstanceType=OneSix
iconKey=vanillia_icon
name=Minecraft Vanillia
OverrideCommands=true
PreLaunchCommand="$INST_JAVA" -jar packwiz-installer-bootstrap.jar link
Wrapperommand=)";
		fileContent += "\"";
		fileContent += +R"(\"$INST_JAVA\" -jar packwiz-installer-bootstrap.jar link =)";
		fileContent += "\"\n";
#if defined(Q_OS_WIN)
		QString fileName = "test_SaveAlreadyExistingFile.ini";
		QFile file(fileName);
		QCOMPARE(file.open(QFile::WriteOnly | QFile::Text), true);
#else
		QTemporaryFile file;
		QCOMPARE(file.open(), true);
		QCOMPARE(file.fileName().isEmpty(), false);
		QString fileName = file.fileName();
#endif
		QTextStream stream(&file);
		stream << fileContent;
		file.close();

		// load
		INIFile f1;
		f1.loadFile(fileName);
		QCOMPARE(f1.get("PreLaunchCommand", "NOT SET").toString(),
				 "\"$INST_JAVA\" -jar packwiz-installer-bootstrap.jar link");
		QCOMPARE(f1.get("Wrapperommand", "NOT SET").toString(),
				 "\"$INST_JAVA\" -jar packwiz-installer-bootstrap.jar link =");
		f1.saveFile(fileName);
		INIFile f2;
		f2.loadFile(fileName);
		QCOMPARE(f2.get("PreLaunchCommand", "NOT SET").toString(),
				 "\"$INST_JAVA\" -jar packwiz-installer-bootstrap.jar link");
		QCOMPARE(f2.get("Wrapperommand", "NOT SET").toString(),
				 "\"$INST_JAVA\" -jar packwiz-installer-bootstrap.jar link =");
		QCOMPARE(f2.get("ConfigVersion", "NOT SET").toString(), "1.3");
#if defined(Q_OS_WIN)
		FS::deletePath(fileName);
#endif
	}

	void test_SaveAlreadyExistingFileWithSpecialChars()
	{
#if defined(Q_OS_WIN)
		QString fileName = "test_SaveAlreadyExistingFileWithSpecialChars.ini";
#else
		QTemporaryFile file;
		QCOMPARE(file.open(), true);
		QCOMPARE(file.fileName().isEmpty(), false);
		QString fileName = file.fileName();
		file.close();
#endif
		QSettings settings{ fileName, QSettings::Format::IniFormat };
		settings.setFallbacksEnabled(false);

		settings.setValue("simple", "value1");
		settings.setValue("withQuotes", R"("value2" with quotes)");
		settings.setValue("withSpecialCharacters", "env mesa=true");
		settings.setValue("withSpecialCharacters2", "1,2,3,4");
		settings.setValue("withSpecialCharacters2", "1;2;3;4");
		settings.setValue("withAll", "val=\"$INST_JAVA\" -jar; ls ");

		settings.sync();

		QCOMPARE(settings.status(), QSettings::Status::NoError);

		// load
		INIFile f1;
		f1.loadFile(fileName);
		for (auto key : settings.allKeys())
			QCOMPARE(f1.get(key, "NOT SET").toString(), settings.value(key).toString());
		f1.saveFile(fileName);
		INIFile f2;
		f2.loadFile(fileName);
		for (auto key : settings.allKeys())
			QCOMPARE(f2.get(key, "NOT SET").toString(), settings.value(key).toString());
		QCOMPARE(f2.get("ConfigVersion", "NOT SET").toString(), "1.3");
#if defined(Q_OS_WIN)
		FS::deletePath(fileName);
#endif
	}

	void test_SaveAlreadyExistingFileWithSpecialCharsV1()
	{
		QString fileContent = R"(InstanceType=OneSix
ConfigVersion=1.1
iconKey=vanillia_icon
name=Minecraft Vanillia
OverrideCommands=true
PreLaunchCommand=)";
		fileContent += "\"\\\"env mesa=true\\\"\"\n";

#if defined(Q_OS_WIN)
		QString fileName = "test_SaveAlreadyExistingFileWithSpecialCharsV1.ini";
		QFile file(fileName);
		QCOMPARE(file.open(QFile::WriteOnly | QFile::Text), true);
#else
		QTemporaryFile file;
		QCOMPARE(file.open(), true);
		QCOMPARE(file.fileName().isEmpty(), false);
		QString fileName = file.fileName();
#endif
		QTextStream stream(&file);
		stream << fileContent;
		file.close();

		// load
		INIFile f1;
		f1.loadFile(fileName);
		QCOMPARE(f1.get("PreLaunchCommand", "NOT SET").toString(), "env mesa=true");
		QCOMPARE(f1.get("ConfigVersion", "NOT SET").toString(), "1.3");
#if defined(Q_OS_WIN)
		FS::deletePath(fileName);
#endif
	}
};

QTEST_GUILESS_MAIN(IniFileTest)

#include "INIFile_test.moc"