summaryrefslogtreecommitdiff
path: root/src/ban.h
Commit message (Expand)AuthorAge
* Fixed/extended/modified ban stuff to be good for inclusionPerttu Ahola2011-08-12
* added ipban supportConstantin Wenger2011-08-12
ef='#n20'>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 227 228 229 230 231 232 233 234 235
/*
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.
*/

#include "test.h"

#include <sstream>

#include "irrlichttypes_extrabloated.h"
#include "log.h"
#include "serialization.h"
#include "nodedef.h"
#include "noise.h"

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

	void runTests(IGameDef *gamedef);

	void testRLECompression();
	void testZlibCompression();
	void testZlibLargeData();
	void testZlibLimit();
	void _testZlibLimit(u32 size, u32 limit);
};

static TestCompression g_test_instance;

void TestCompression::runTests(IGameDef *gamedef)
{
	TEST(testRLECompression);
	TEST(testZlibCompression);
	TEST(testZlibLargeData);
	TEST(testZlibLimit);
}

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

void TestCompression::testRLECompression()
{
	SharedBuffer<u8> fromdata(4);
	fromdata[0]=1;
	fromdata[1]=5;
	fromdata[2]=5;
	fromdata[3]=1;

	std::ostringstream os(std::ios_base::binary);
	compress(fromdata, os, 0);

	std::string str_out = os.str();

	infostream << "str_out.size()="<<str_out.size()<<std::endl;
	infostream << "TestCompress: 1,5,5,1 -> ";
	for (char i : str_out)
		infostream << (u32) i << ",";
	infostream << std::endl;

	UASSERT(str_out.size() == 10);

	UASSERT(str_out[0] == 0);
	UASSERT(str_out[1] == 0);
	UASSERT(str_out[2] == 0);
	UASSERT(str_out[3] == 4);
	UASSERT(str_out[4] == 0);
	UASSERT(str_out[5] == 1);
	UASSERT(str_out[6] == 1);
	UASSERT(str_out[7] == 5);
	UASSERT(str_out[8] == 0);
	UASSERT(str_out[9] == 1);

	std::istringstream is(str_out, std::ios_base::binary);
	std::ostringstream os2(std::ios_base::binary);

	decompress(is, os2, 0);
	std::string str_out2 = os2.str();

	infostream << "decompress: ";
	for (char i : str_out2)
		infostream << (u32) i << ",";
	infostream << std::endl;

	UASSERTEQ(size_t, str_out2.size(), fromdata.getSize());

	for (u32 i = 0; i < str_out2.size(); i++)
		UASSERT(str_out2[i] == fromdata[i]);
}

void TestCompression::testZlibCompression()
{
	SharedBuffer<u8> fromdata(4);
	fromdata[0]=1;
	fromdata[1]=5;
	fromdata[2]=5;
	fromdata[3]=1;

	std::ostringstream os(std::ios_base::binary);
	compress(fromdata, os, SER_FMT_VER_HIGHEST_READ);

	std::string str_out = os.str();

	infostream << "str_out.size()=" << str_out.size() <<std::endl;
	infostream << "TestCompress: 1,5,5,1 -> ";
	for (char i : str_out)
		infostream << (u32) i << ",";
	infostream << std::endl;

	std::istringstream is(str_out, std::ios_base::binary);
	std::ostringstream os2(std::ios_base::binary);

	decompress(is, os2, SER_FMT_VER_HIGHEST_READ);
	std::string str_out2 = os2.str();

	infostream << "decompress: ";
	for (char i : str_out2)
		infostream << (u32) i << ",";
	infostream << std::endl;

	UASSERTEQ(size_t, str_out2.size(), fromdata.getSize());

	for (u32 i = 0; i < str_out2.size(); i++)
		UASSERT(str_out2[i] == fromdata[i]);
}

void TestCompression::testZlibLargeData()
{
	infostream << "Test: Testing zlib wrappers with a large amount "
		"of pseudorandom data" << std::endl;

	u32 size = 50000;
	infostream << "Test: Input size of large compressZlib is "
		<< size << std::endl;

	std::string data_in;
	data_in.resize(size);
	PseudoRandom pseudorandom(9420);
	for (u32 i = 0; i < size; i++)
		data_in[i] = pseudorandom.range(0, 255);

	std::ostringstream os_compressed(std::ios::binary);
	compressZlib(data_in, os_compressed);
	infostream << "Test: Output size of large compressZlib is "
		<< os_compressed.str().size()<<std::endl;

	std::istringstream is_compressed(os_compressed.str(), std::ios::binary);
	std::ostringstream os_decompressed(std::ios::binary);
	decompressZlib(is_compressed, os_decompressed);
	infostream << "Test: Output size of large decompressZlib is "
		<< os_decompressed.str().size() << std::endl;

	std::string str_decompressed = os_decompressed.str();
	UASSERTEQ(size_t, str_decompressed.size(), data_in.size());

	for (u32 i = 0; i < size && i < str_decompressed.size(); i++) {
		UTEST(str_decompressed[i] == data_in[i],
				"index out[%i]=%i differs from in[%i]=%i",
				i, str_decompressed[i], i, data_in[i]);
	}
}

void TestCompression::testZlibLimit()
{
	// edge cases
	_testZlibLimit(1024, 1023);
	_testZlibLimit(1024, 1024);
	_testZlibLimit(1024, 1025);

	// test around buffer borders
	u32 bufsize = 16384; // as in implementation
	for (int s = -1; s <= 1; s++)
	{
		for (int l = -1; l <= 1; l++)
		{
			_testZlibLimit(bufsize + s, bufsize + l);
		}
	}
	// span multiple buffers
	_testZlibLimit(35000, 22000);
	_testZlibLimit(22000, 35000);
}

void TestCompression::_testZlibLimit(u32 size, u32 limit)
{
	infostream << "Test: Testing zlib wrappers with a decompression "
		"memory limit of " << limit << std::endl;

	infostream << "Test: Input size of compressZlib for limit is "
		<< size << std::endl;

	// how much data we expect to get
	u32 expected = size < limit ? size : limit;

	// create recognizable data
	std::string data_in;
	data_in.resize(size);
	for (u32 i = 0; i < size; i++)
		data_in[i] = (u8)(i % 256);

	std::ostringstream os_compressed(std::ios::binary);
	compressZlib(data_in, os_compressed);
	infostream << "Test: Output size of compressZlib for limit is "
		<< os_compressed.str().size()<<std::endl;

	std::istringstream is_compressed(os_compressed.str(), std::ios::binary);
	std::ostringstream os_decompressed(std::ios::binary);
	decompressZlib(is_compressed, os_decompressed, limit);
	infostream << "Test: Output size of decompressZlib with limit is "
		<< os_decompressed.str().size() << std::endl;

	std::string str_decompressed = os_decompressed.str();
	UASSERTEQ(size_t, str_decompressed.size(), expected);

	for (u32 i = 0; i < size && i < str_decompressed.size(); i++) {
		UTEST(str_decompressed[i] == data_in[i],
				"index out[%i]=%i differs from in[%i]=%i",
				i, str_decompressed[i], i, data_in[i]);
	}
}