/* Minetest-c55 Copyright (C) 2010 celeron55, Perttu Ahola 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; either version 2 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 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 "test.h" #include "common_irrlicht.h" #include "debug.h" #include "map.h" #include "player.h" #include "main.h" #include "socket.h" #include "connection.h" #include "utility.h" #include "serialization.h" #include "voxel.h" #include #include "porting.h" #include "content_mapnode.h" #include "nodedef.h" #include "mapsector.h" #include "settings.h" #include "log.h" #include "utility_string.h" #include "voxelalgorithms.h" /* Asserts that the exception occurs */ #define EXCEPTION_CHECK(EType, code)\ {\ bool exception_thrown = false;\ try{ code; }\ catch(EType &e) { exception_thrown = true; }\ assert(exception_thrown);\ } /* A few item and node definitions for those tests that need them */ #define CONTENT_STONE 0 #define CONTENT_GRASS 0x800 #define CONTENT_TORCH 100 void define_some_nodes(IWritableItemDefManager *idef, IWritableNodeDefManager *ndef) { content_t i; ItemDefinition itemdef; ContentFeatures f; /* Stone */ i = CONTENT_STONE; itemdef = ItemDefinition(); itemdef.type = ITEM_NODE; itemdef.name = "default:stone"; itemdef.description = "Stone"; itemdef.groups["cracky"] = 3; itemdef.inventory_image = "[inventorycube" "{default_stone.png" "{default_stone.png" "{default_stone.png"; f = ContentFeatures(); f.name = itemdef.name; for(int i = 0; i < 6; i++) f.tname_tiles[i] = "default_stone.png"; f.is_ground_content = true; idef->registerItem(itemdef); ndef->set(i, f); /* Grass */ i = CONTENT_GRASS; itemdef = ItemDefinition(); itemdef.type = ITEM_NODE; itemdef.name = "default:dirt_with_grass"; itemdef.description = "Dirt with grass"; itemdef.groups["crumbly"] = 3; itemdef.inventory_image = "[inventorycube" "{default_grass.png" "{default_dirt.png&default_grass_side.png" "{default_dirt.png&default_grass_side.png"; f = ContentFeatures(); f.name = itemdef.name; f.tname_tiles[0] = "default_grass.png"; f.tname_tiles[1] = "default_dirt.png"; for(int i = 2; i < 6; i++) f.tname_tiles[i] = "default_dirt.png^default_grass_side.png"; f.is_ground_content = true; idef->registerItem(itemdef); ndef->set(i, f); /* Torch (minimal definition for lighting tests) */ i = CONTENT_TORCH; itemdef = ItemDefinition(); itemdef.type = ITEM_NODE; itemdef.name = "default:torch"; f = ContentFeatures(); f.name = itemdef.name; f.param_type = CPT_LIGHT; f.light_propagates = true; f.sunlight_propagates = true; f.light_source = LIGHT_MAX-1; idef->registerItem(itemdef); ndef->set(i, f); } struct TestUtilities { void Run() { /*infostream<<"wrapDegrees(100.0) = "< std::string mkstr(const char (&s)[N]) { return std::string(s, N - 1); } void Run() { // Tests some serialization primitives assert(serializeString("") == mkstr("\0\0")); assert(serializeWideString(L"") == mkstr("\0\0")); assert(serializeLongString("") == mkstr("\0\0\0\0")); assert(serializeJsonString("") == "\"\""); std::string teststring = "Hello world!"; assert(serializeString(teststring) == mkstr("\0\14Hello world!")); assert(serializeWideString(narrow_to_wide(teststring)) == mkstr("\0\14\0H\0e\0l\0l\0o\0 \0w\0o\0r\0l\0d\0!")); assert(serializeLongString(teststring) == mkstr("\0\0\0\14Hello world!")); assert(serializeJsonString(teststring) == "\"Hello world!\""); std::string teststring2; std::wstring teststring2_w; std::string teststring2_w_encoded; { std::ostringstream tmp_os; std::wostringstream tmp_os_w; std::ostringstream tmp_os_w_encoded; for(int i = 0; i < 256; i++) { tmp_os<<(char)i; tmp_os_w<<(wchar_t)i; tmp_os_w_encoded<<(char)0<<(char)i; } teststring2 = tmp_os.str(); teststring2_w = tmp_os_w.str(); teststring2_w_encoded = tmp_os_w_encoded.str(); } assert(serializeString(teststring2) == mkstr("\1\0") + teststring2); assert(serializeWideString(teststring2_w) == mkstr("\1\0") + teststring2_w_encoded); assert(serializeLongString(teststring2) == mkstr("\0\0\1\0") + teststring2); // MSVC fails when directly using "\\\\" std::string backslash = "\\"; assert(serializeJsonString(teststring2) == mkstr("\"") + "\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007" + "\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f" + "\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017" + "\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f" + " !\\\"" + teststring2.substr(0x23, 0x2f-0x23) + "\\/" + teststring2.substr(0x30, 0x5c-0x30) + backslash + backslash + teststring2.substr(0x5d, 0x7f-0x5d) + "\\u007f" + "\\u0080\\u0081\\u0082\\u0083\\u0084\\u0085\\u0086\\u0087" + "\\u0088\\u0089\\u008a\\u008b\\u008c\\u008d\\u008e\\u008f" + "\\u0090\\u0091\\u0092\\u0093\\u0094\\u0095\\u0096\\u0097" + "\\u0098\\u0099\\u009a\\u009b\\u009c\\u009d\\u009e\\u009f" + "\\u00a0\\u00a1\\u00a2\\u00a3\\u00a4\\u00a5\\u00a6\\u00a7" + "\\u00a8\\u00a9\\u00aa\\u00ab\\u00ac\\u00ad\\u00ae\\u00af" + "\\u00b0\\u00b1\\u00b2\\u00b3\\u00b4\\u00b5\\u00b6\\u00b7" + "\\u00b8\\u00b9\\u00ba\\u00bb\\u00bc\\u00bd\\u00be\\u00bf" + "\\u00c0\\u00c1\\u00c2\\u00c3\\u00c4\\u00c5\\u00c6\\u00c7" + "\\u00c8\\u00c9\\u00ca\\u00cb\\u00cc\\u00cd\\u00ce\\u00cf" + "\\u00d0\\u00d1\\u00d2\\u00d3\\u00d4\\u00d5\\u00d6\\u00d7" + "\\u00d8\\u00d9\\u00da\\u00db\\u00dc\\u00dd\\u00de\\u00df" + "\\u00e0\\u00e1\\u00e2\\u00e3\\u00e4\\u00e5\\u00e6\\u00e7" + "\\u00e8\\u00e9\\u00ea\\u00eb\\u00ec\\u00ed\\u00ee\\u00ef" + "\\u00f0\\u00f1\\u00f2\\u00f3\\u00f4\\u00f5\\u00f6\\u00f7" + "\\u00f8\\u00f9\\u00fa\\u00fb\\u00fc\\u00fd\\u00fe\\u00ff" + "\""); { std::istringstream is(serializeString(teststring2), std::ios::binary); assert(deSerializeString(is) == teststring2); assert(!is.eof()); is.get(); assert(is.eof()); } { std::istringstream is(serializeWideString(teststring2_w), std::ios::binary); assert(deSerializeWideString(is) == teststring2_w); assert(!is.eof()); is.get(); assert(is.eof()); } { std::istringstream is(serializeLongString(teststring2), std::ios::binary); assert(deSerializeLongString(is) == teststring2); assert(!is.eof()); is.get(); assert(is.eof()); } { std::istringstream is(serializeJsonString(teststring2), std::ios::binary); //dstream</* Minetest Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com> 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. */ #pragma once #include <cstring> #include <string> #include "database.h" #include "exceptions.h" extern "C" { #include "sqlite3.h" } class Database_SQLite3 : public Database { public: virtual ~Database_SQLite3(); void beginSave(); void endSave(); bool initialized() const { return m_initialized; } protected: Database_SQLite3(const std::string &savedir, const std::string &dbname); // Open and initialize the database if needed void verifyDatabase(); // Convertors inline void str_to_sqlite(sqlite3_stmt *s, int iCol, const std::string &str) const { sqlite3_vrfy(sqlite3_bind_text(s, iCol, str.c_str(), str.size(), NULL)); } inline void str_to_sqlite(sqlite3_stmt *s, int iCol, const char *str) const { sqlite3_vrfy(sqlite3_bind_text(s, iCol, str, strlen(str), NULL)); } inline void int_to_sqlite(sqlite3_stmt *s, int iCol, int val) const { sqlite3_vrfy(sqlite3_bind_int(s, iCol, val)); } inline void int64_to_sqlite(sqlite3_stmt *s, int iCol, s64 val) const { sqlite3_vrfy(sqlite3_bind_int64(s, iCol, (sqlite3_int64) val)); } inline void double_to_sqlite(sqlite3_stmt *s, int iCol, double val) const { sqlite3_vrfy(sqlite3_bind_double(s, iCol, val)); } inline std::string sqlite_to_string(sqlite3_stmt *s, int iCol) { const char* text = reinterpret_cast<const char*>(sqlite3_column_text(s, iCol)); return std::string(text ? text : ""); } inline s32 sqlite_to_int(sqlite3_stmt *s, int iCol) { return sqlite3_column_int(s, iCol); } inline u32 sqlite_to_uint(sqlite3_stmt *s, int iCol) { return (u32) sqlite3_column_int(s, iCol); } inline float sqlite_to_float(sqlite3_stmt *s, int iCol) { return (float) sqlite3_column_double(s, iCol); } inline const v3f sqlite_to_v3f(sqlite3_stmt *s, int iCol) { return v3f(sqlite_to_float(s, iCol), sqlite_to_float(s, iCol + 1), sqlite_to_float(s, iCol + 2)); } // Query verifiers helpers inline void sqlite3_vrfy(int s, const std::string &m = "", int r = SQLITE_OK) const { if (s != r) throw DatabaseException(m + ": " + sqlite3_errmsg(m_database)); } inline void sqlite3_vrfy(const int s, const int r, const std::string &m = "") const { sqlite3_vrfy(s, m, r); } // Create the database structure virtual void createDatabase() = 0; virtual void initStatements() = 0; sqlite3 *m_database = nullptr; private: // Open the database void openDatabase(); bool m_initialized = false; std::string m_savedir = ""; std::string m_dbname = ""; sqlite3_stmt *m_stmt_begin = nullptr; sqlite3_stmt *m_stmt_end = nullptr; s64 m_busy_handler_data[2]; static int busyHandler(void *data, int count); }; class MapDatabaseSQLite3 : private Database_SQLite3, public MapDatabase { public: MapDatabaseSQLite3(const std::string &savedir); virtual ~MapDatabaseSQLite3(); bool saveBlock(const v3s16 &pos, const std::string &data); void loadBlock(const v3s16 &pos, std::string *block); bool deleteBlock(const v3s16 &pos); void listAllLoadableBlocks(std::vector<v3s16> &dst); void beginSave() { Database_SQLite3::beginSave(); } void endSave() { Database_SQLite3::endSave(); } protected: virtual void createDatabase(); virtual void initStatements(); private: void bindPos(sqlite3_stmt *stmt, const v3s16 &pos, int index = 1); // Map sqlite3_stmt *m_stmt_read = nullptr; sqlite3_stmt *m_stmt_write = nullptr; sqlite3_stmt *m_stmt_list = nullptr; sqlite3_stmt *m_stmt_delete = nullptr; }; class PlayerDatabaseSQLite3 : private Database_SQLite3, public PlayerDatabase { public: PlayerDatabaseSQLite3(const std::string &savedir); virtual ~PlayerDatabaseSQLite3(); void savePlayer(RemotePlayer *player); bool loadPlayer(RemotePlayer *player, PlayerSAO *sao); bool removePlayer(const std::string &name); void listPlayers(std::vector<std::string> &res); protected: virtual void createDatabase(); virtual void initStatements(); private: bool playerDataExists(const std::string &name); // Players sqlite3_stmt *m_stmt_player_load = nullptr; sqlite3_stmt *m_stmt_player_add = nullptr; sqlite3_stmt *m_stmt_player_update = nullptr; sqlite3_stmt *m_stmt_player_remove = nullptr; sqlite3_stmt *m_stmt_player_list = nullptr; sqlite3_stmt *m_stmt_player_load_inventory = nullptr; sqlite3_stmt *m_stmt_player_load_inventory_items = nullptr; sqlite3_stmt *m_stmt_player_add_inventory = nullptr; sqlite3_stmt *m_stmt_player_add_inventory_items = nullptr; sqlite3_stmt *m_stmt_player_remove_inventory = nullptr; sqlite3_stmt *m_stmt_player_remove_inventory_items = nullptr; sqlite3_stmt *m_stmt_player_metadata_load = nullptr; sqlite3_stmt *m_stmt_player_metadata_remove = nullptr; sqlite3_stmt *m_stmt_player_metadata_add = nullptr; }; recvdata; infostream<<"** running server.Receive()"< data1 = SharedBufferFromString("hello1"); SharedBuffer data2 = SharedBufferFromString("Hello2"); Address client_address = server.GetPeerAddress(peer_id_client); infostream<<"*** Sending packets in wrong order (2,1,2)" <channels[chn]; u16 sn = ch->next_outgoing_seqnum; ch->next_outgoing_seqnum = sn+1; server.Send(peer_id_client, chn, data2, true); ch->next_outgoing_seqnum = sn; server.Send(peer_id_client, chn, data1, true); ch->next_outgoing_seqnum = sn+1; server.Send(peer_id_client, chn, data2, true); sleep_ms(50); infostream<<"*** Receiving the packets"< recvdata; u32 size; infostream<<"** running client.Receive()"<20) infostream<<"..."; infostream< recvdata; infostream<<"** running client.Receive()"< 5000 || received) break; try{ size = client.Receive(peer_id, recvdata); received = true; }catch(con::NoIncomingDataException &e){ } sleep_ms(10); } assert(received); infostream<<"** Client received: peer_id="<20) infostream<<"..."; infostream<