aboutsummaryrefslogtreecommitdiff
path: root/games/minimal/mods/default/textures/heart.png
blob: 6bc183e04f21cba17b52009e89bb52072d5a0afd (plain)
ofshex dumpascii
0000 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 00 10 00 00 00 10 08 06 00 00 00 1f f3 ff .PNG........IHDR................
0020 61 00 00 00 04 73 42 49 54 08 08 08 08 7c 08 64 88 00 00 00 09 70 48 59 73 00 00 0e c4 00 00 0e a....sBIT....|.d.....pHYs.......
0040 c4 01 95 2b 0e 1b 00 00 00 d6 49 44 41 54 38 8d cd d2 3d 4e 03 31 10 86 e1 c7 08 09 ca a4 a2 e5 ...+......IDAT8...=N.1..........
0060 06 70 14 6e 00 1d 29 c9 0d b8 01 25 74 09 27 c8 52 a5 e5 08 b9 01 54 48 a9 08 55 a8 18 0a bc c1 .p.n..)....%t.'.R.....TH..U.....
0080 2c de a4 84 57 1a 79 ec d1 f7 79 fc c3 5f 93 ca c9 0d 31 c8 f9 0a e3 5c bf 26 8e 70 88 47 dc 17 ,...W.y...y.._....1....\.&.p.G..
00a0 ba fd 52 7c 86 e3 3c 7f fe 1a 02 4e 71 82 61 ae bf 11 4d 67 73 13 22 3a 31 21 5e 3a 6b af c4 6d ..R|..<....Nq.a...Mgs.":1!^:k..m
00c0 36 fe d1 41 8d f3 ca da 20 1f a5 65 af 4d 56 df 6d 6f 48 ba 7d fe 66 63 30 26 75 0d 6a 2c b0 ec 6..A.......e.MV.moH.}.fc0&u.j,..
00e0 2b 5e 11 4f 95 bb 68 63 4d cc 8a f3 57 99 11 1f 3d 06 0d 71 b1 cb e0 92 98 57 c4 73 62 b4 4b dc +^.O..hcM...W...=..q.....W.sb.K.
0100 32 ca ad ae 73 34 5b c4 d5 67 bc 23 bd 17 82 07 4c 7b 1e a4 f7 1f 4c 49 07 44 da 22 fe 1f 7c 02 2...s4[..g.#....L{....LI.D."..|.
0120 15 56 6b 16 d4 cf a8 2c 00 00 00 00 49 45 4e 44 ae 42 60 82 .Vk....,....IEND.B`.
ef='#n93'>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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
/*
Minetest
Copyright (C) 2018 bendeutsch, Ben Deutsch <ben@bendeutsch.de>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

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 Lesser General Public License for more details.

You should have received a copy of the GNU Lesser 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 "test.h"

#include <algorithm>
#include "database/database-files.h"
#include "database/database-sqlite3.h"
#include "util/string.h"
#include "filesys.h"

namespace
{
// Anonymous namespace to create classes that are only
// visible to this file
//
// These are helpers that return a *AuthDatabase and
// allow us to run the same tests on different databases and
// database acquisition strategies.

class AuthDatabaseProvider
{
public:
	virtual ~AuthDatabaseProvider() = default;
	virtual AuthDatabase *getAuthDatabase() = 0;
};

class FixedProvider : public AuthDatabaseProvider
{
public:
	FixedProvider(AuthDatabase *auth_db) : auth_db(auth_db){};
	virtual ~FixedProvider(){};
	virtual AuthDatabase *getAuthDatabase() { return auth_db; };

private:
	AuthDatabase *auth_db;
};

class FilesProvider : public AuthDatabaseProvider
{
public:
	FilesProvider(const std::string &dir) : dir(dir){};
	virtual ~FilesProvider() { delete auth_db; };
	virtual AuthDatabase *getAuthDatabase()
	{
		delete auth_db;
		auth_db = new AuthDatabaseFiles(dir);
		return auth_db;
	};

private:
	std::string dir;
	AuthDatabase *auth_db = nullptr;
};

class SQLite3Provider : public AuthDatabaseProvider
{
public:
	SQLite3Provider(const std::string &dir) : dir(dir){};
	virtual ~SQLite3Provider() { delete auth_db; };
	virtual AuthDatabase *getAuthDatabase()
	{
		delete auth_db;
		auth_db = new AuthDatabaseSQLite3(dir);
		return auth_db;
	};

private:
	std::string dir;
	AuthDatabase *auth_db = nullptr;
};
}

class TestAuthDatabase : public TestBase
{
public:
	TestAuthDatabase() { TestManager::registerTestModule(this); }
	const char *getName() { return "TestAuthDatabase"; }

	void runTests(IGameDef *gamedef);
	void runTestsForCurrentDB();

	void testRecallFail();
	void testCreate();
	void testRecall();
	void testChange();
	void testRecallChanged();
	void testChangePrivileges();
	void testRecallChangedPrivileges();
	void testListNames();
	void testDelete();

private:
	AuthDatabaseProvider *auth_provider;
};

static TestAuthDatabase g_test_instance;

void TestAuthDatabase::runTests(IGameDef *gamedef)
{
	// fixed directory, for persistence
	thread_local const std::string test_dir = getTestTempDirectory();

	// Each set of tests is run twice for each database type:
	// one where we reuse the same AuthDatabase object (to test local caching),
	// and one where we create a new AuthDatabase object for each call
	// (to test actual persistence).

	rawstream << "-------- Files database (same object)" << std::endl;

	AuthDatabase *auth_db = new AuthDatabaseFiles(test_dir);
	auth_provider = new FixedProvider(auth_db);

	runTestsForCurrentDB();

	delete auth_db;
	delete auth_provider;

	// reset database
	fs::DeleteSingleFileOrEmptyDirectory(test_dir + DIR_DELIM + "auth.txt");

	rawstream << "-------- Files database (new objects)" << std::endl;

	auth_provider = new FilesProvider(test_dir);

	runTestsForCurrentDB();

	delete auth_provider;

	rawstream << "-------- SQLite3 database (same object)" << std::endl;

	auth_db = new AuthDatabaseSQLite3(test_dir);
	auth_provider = new FixedProvider(auth_db);

	runTestsForCurrentDB();

	delete auth_db;
	delete auth_provider;

	// reset database
	fs::DeleteSingleFileOrEmptyDirectory(test_dir + DIR_DELIM + "auth.sqlite");

	rawstream << "-------- SQLite3 database (new objects)" << std::endl;

	auth_provider = new SQLite3Provider(test_dir);

	runTestsForCurrentDB();

	delete auth_provider;
}

////////////////////////////////////////////////////////////////////////////////

void TestAuthDatabase::runTestsForCurrentDB()
{
	TEST(testRecallFail);
	TEST(testCreate);
	TEST(testRecall);
	TEST(testChange);
	TEST(testRecallChanged);
	TEST(testChangePrivileges);
	TEST(testRecallChangedPrivileges);
	TEST(testListNames);
	TEST(testDelete);
	TEST(testRecallFail);
}

void TestAuthDatabase::testRecallFail()
{
	AuthDatabase *auth_db = auth_provider->getAuthDatabase();
	AuthEntry authEntry;

	// no such user yet
	UASSERT(!auth_db->getAuth("TestName", authEntry));
}

void TestAuthDatabase::testCreate()
{
	AuthDatabase *auth_db = auth_provider->getAuthDatabase();
	AuthEntry authEntry;

	authEntry.name = "TestName";
	authEntry.password = "TestPassword";
	authEntry.privileges.emplace_back("shout");
	authEntry.privileges.emplace_back("interact");
	authEntry.last_login = 1000;
	UASSERT(auth_db->createAuth(authEntry));
}

void TestAuthDatabase::testRecall()
{
	AuthDatabase *auth_db = auth_provider->getAuthDatabase();
	AuthEntry authEntry;

	UASSERT(auth_db->getAuth("TestName", authEntry));
	UASSERTEQ(std::string, authEntry.name, "TestName");
	UASSERTEQ(std::string, authEntry.password, "TestPassword");
	// the order of privileges is unimportant
	std::sort(authEntry.privileges.begin(), authEntry.privileges.end());
	UASSERTEQ(std::string, str_join(authEntry.privileges, ","), "interact,shout");
}

void TestAuthDatabase::testChange()
{
	AuthDatabase *auth_db = auth_provider->getAuthDatabase();
	AuthEntry authEntry;

	UASSERT(auth_db->getAuth("TestName", authEntry));
	authEntry.password = "NewPassword";
	authEntry.last_login = 1002;
	UASSERT(auth_db->saveAuth(authEntry));
}

void TestAuthDatabase::testRecallChanged()
{
	AuthDatabase *auth_db = auth_provider->getAuthDatabase();
	AuthEntry authEntry;

	UASSERT(auth_db->getAuth("TestName", authEntry));
	UASSERTEQ(std::string, authEntry.password, "NewPassword");
	// the order of privileges is unimportant
	std::sort(authEntry.privileges.begin(), authEntry.privileges.end());
	UASSERTEQ(std::string, str_join(authEntry.privileges, ","), "interact,shout");
	UASSERTEQ(u64, authEntry.last_login, 1002);
}

void TestAuthDatabase::testChangePrivileges()
{
	AuthDatabase *auth_db = auth_provider->getAuthDatabase();
	AuthEntry authEntry;

	UASSERT(auth_db->getAuth("TestName", authEntry));
	authEntry.privileges.clear();
	authEntry.privileges.emplace_back("interact");
	authEntry.privileges.emplace_back("fly");
	authEntry.privileges.emplace_back("dig");
	UASSERT(auth_db->saveAuth(authEntry));
}

void TestAuthDatabase::testRecallChangedPrivileges()
{
	AuthDatabase *auth_db = auth_provider->getAuthDatabase();
	AuthEntry authEntry;

	UASSERT(auth_db->getAuth("TestName", authEntry));
	// the order of privileges is unimportant
	std::sort(authEntry.privileges.begin(), authEntry.privileges.end());
	UASSERTEQ(std::string, str_join(authEntry.privileges, ","), "dig,fly,interact");
}

void TestAuthDatabase::testListNames()
{

	AuthDatabase *auth_db = auth_provider->getAuthDatabase();
	std::vector<std::string> list;

	AuthEntry authEntry;

	authEntry.name = "SecondName";
	authEntry.password = "SecondPassword";
	authEntry.privileges.emplace_back("shout");
	authEntry.privileges.emplace_back("interact");
	authEntry.last_login = 1003;