update v 1.0.7.5

This commit is contained in:
Sujit Prasad
2016-06-13 20:41:55 +05:30
parent aa9786d829
commit 283d97e3ea
5078 changed files with 339851 additions and 175995 deletions

View File

@@ -0,0 +1,369 @@
<?php
namespace MaxMind\Db\Test\Reader;
use MaxMind\Db\Reader\Decoder;
class DecoderTest extends \PHPUnit_Framework_TestCase
{
private $arrays = array(
array(
'expected' => array(),
'input' => array(0x0, 0x4),
'name' => 'empty',
),
array(
'expected' => array('Foo'),
'input' => array(0x1, 0x4, /* Foo */
0x43, 0x46, 0x6f, 0x6f),
'name' => 'one element',
),
array(
'expected' => array('Foo', '人'),
'input' => array(
0x2, 0x4,
/* Foo */
0x43, 0x46, 0x6f, 0x6f,
/* 人 */
0x43, 0xe4, 0xba, 0xba
),
'name' => 'two elements',
),
);
private $booleans = array(
false => array(0x0, 0x7),
true => array(0x1, 0x7),
);
private $doubles = array(
'0.0' => array(0x68, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0),
'0.5' => array(0x68, 0x3F, 0xE0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0),
'3.14159265359' => array(0x68, 0x40, 0x9, 0x21, 0xFB, 0x54, 0x44,
0x2E, 0xEA),
'123.0' => array(0x68, 0x40, 0x5E, 0xC0, 0x0, 0x0, 0x0, 0x0, 0x0),
'1073741824.12457' => array(0x68, 0x41, 0xD0, 0x0, 0x0, 0x0, 0x7,
0xF8, 0xF4),
'-0.5' => array(0x68, 0xBF, 0xE0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0),
'-3.14159265359' => array(0x68, 0xC0, 0x9, 0x21, 0xFB, 0x54, 0x44,
0x2E, 0xEA),
'-1073741824.12457' => array(0x68, 0xC1, 0xD0, 0x0, 0x0, 0x0, 0x7,
0xF8, 0xF4),
);
private $floats = array(
'0.0' => array(0x4, 0x8, 0x0, 0x0, 0x0, 0x0),
'1.0' => array(0x4, 0x8, 0x3F, 0x80, 0x0, 0x0),
'1.1' => array(0x4, 0x8, 0x3F, 0x8C, 0xCC, 0xCD),
'3.14' => array(0x4, 0x8, 0x40, 0x48, 0xF5, 0xC3),
'9999.99' => array(0x4, 0x8, 0x46, 0x1C, 0x3F, 0xF6),
'-1.0' => array(0x4, 0x8, 0xBF, 0x80, 0x0, 0x0),
'-1.1' => array(0x4, 0x8, 0xBF, 0x8C, 0xCC, 0xCD),
'-3.14' => array(0x4, 0x8, 0xC0, 0x48, 0xF5, 0xC3),
'-9999.99' => array(0x4, 0x8, 0xC6, 0x1C, 0x3F, 0xF6)
);
// PHP can't have arrays/objects as keys. Maybe redo all of the tests
// this way so that we can use one test runner
private $maps = array(
array(
'expected' => array(),
'input' => array(0xe0),
'name' => 'empty',
),
array(
'expected' => array('en' => 'Foo'),
'input' => array(0xe1, /* en */
0x42, 0x65, 0x6e,
/* Foo */
0x43, 0x46, 0x6f, 0x6f),
'name' => 'one key',
),
array(
'expected' => array('en' => 'Foo', 'zh' => '人'),
'input' => array(
0xe2,
/* en */
0x42, 0x65, 0x6e,
/* Foo */
0x43, 0x46, 0x6f, 0x6f,
/* zh */
0x42, 0x7a, 0x68,
/* 人 */
0x43, 0xe4, 0xba, 0xba
),
'name' => 'two keys',
),
array(
'expected' => array('name' => array('en' => 'Foo', 'zh' => '人')),
'input' => array(
0xe1,
/* name */
0x44, 0x6e, 0x61, 0x6d, 0x65, 0xe2,
/* en */
0x42, 0x65, 0x6e,
/* Foo */
0x43, 0x46, 0x6f, 0x6f,
/* zh */
0x42, 0x7a, 0x68,
/* 人 */
0x43, 0xe4, 0xba, 0xba
),
'name' => 'nested',
),
array(
'expected' => array('languages' => array('en', 'zh')),
'input' => array(
0xe1,
/* languages */
0x49, 0x6c, 0x61, 0x6e, 0x67, 0x75, 0x61,
0x67, 0x65, 0x73,
/* array */
0x2, 0x4,
/* en */
0x42, 0x65, 0x6e,
/* zh */
0x42, 0x7a, 0x68
),
'name' => 'map with array in it'
),
);
private $pointers = array(
0 => array(0x20, 0x0),
5 => array(0x20, 0x5),
10 => array(0x20, 0xa),
1023 => array(0x23, 0xff,),
3017 => array(0x28, 0x3, 0xc9),
524283 => array(0x2f, 0xf7, 0xfb),
526335 => array(0x2f, 0xff, 0xff),
134217726 => array(0x37, 0xf7, 0xf7, 0xfe),
134744063 => array(0x37, 0xff, 0xff, 0xff),
2147483647 => array(0x38, 0x7f, 0xff, 0xff, 0xff),
4294967295 => array(0x38, 0xff, 0xff, 0xff, 0xff),
);
private $uint16 = array(
0 => array(0xa0),
255 => array(0xa1, 0xff),
500 => array(0xa2, 0x1, 0xf4),
10872 => array(0xa2, 0x2a, 0x78),
65535 => array(0xa2, 0xff, 0xff),
);
private $int32 = array(
'0' => array(0x0, 0x1),
'-1' => array(0x4, 0x1, 0xff, 0xff, 0xff, 0xff),
'255' => array(0x1, 0x1, 0xff),
'-255' => array(0x4, 0x1, 0xff, 0xff, 0xff, 0x1),
'500' => array(0x2, 0x1, 0x1, 0xf4),
'-500' => array(0x4, 0x1, 0xff, 0xff, 0xfe, 0xc),
'65535' => array(0x2, 0x1, 0xff, 0xff),
'-65535' => array(0x4, 0x1, 0xff, 0xff, 0x0, 0x1),
'16777215' => array(0x3, 0x1, 0xff, 0xff, 0xff),
'-16777215' => array(0x4, 0x1, 0xff, 0x0, 0x0, 0x1),
'2147483647' => array(0x4, 0x1, 0x7f, 0xff, 0xff, 0xff),
'-2147483647' => array(0x4, 0x1, 0x80, 0x0, 0x0, 0x1),
);
private function strings()
{
$strings = array(
'' => array(0x40),
1 => array(0x41, 0x31),
'人' => array(0x43, 0xE4, 0xBA, 0xBA),
'123' => array(0x43, 0x31, 0x32, 0x33),
'123456789012345678901234567' => array(0x5b, 0x31, 0x32, 0x33, 0x34,
0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
0x37),
'1234567890123456789012345678' => array(0x5c, 0x31, 0x32, 0x33, 0x34,
0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
0x37, 0x38),
'12345678901234567890123456789' => array(0x5d, 0x0, 0x31, 0x32, 0x33,
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34,
0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
0x36, 0x37, 0x38, 0x39),
'123456789012345678901234567890' => array(0x5d, 0x1, 0x31, 0x32, 0x33,
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34,
0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
0x36, 0x37, 0x38, 0x39, 0x30),
);
$strings[str_repeat('x', 500)] =
array_pad(
array(0x5e, 0x0, 0xd7),
503,
0x78
);
$strings[str_repeat('x', 2000)] =
array_pad(
array(0x5e, 0x6, 0xb3),
2003,
0x78
);
$strings[str_repeat('x', 70000)] =
array_pad(
array(0x5f, 0x0, 0x10, 0x53),
70004,
0x78
);
return $strings;
}
private $uint32 = array(
0 => array(0xc0),
255 => array(0xc1, 0xff),
500 => array(0xc2, 0x1, 0xf4),
10872 => array(0xc2, 0x2a, 0x78),
65535 => array(0xc2, 0xff, 0xff),
16777215 => array(0xc3, 0xff, 0xff, 0xff),
4294967295 => array(0xc4, 0xff, 0xff, 0xff, 0xff),
);
private function bytes()
{
// ugly deep clone
$bytes = unserialize(serialize($this->strings()));
foreach ($bytes as $key => $byte_array) {
$byte_array[0] ^= 0xc0;
$bytes[$key] = $byte_array;
}
return $bytes;
}
public function generateLargeUint($bits)
{
$ctrlByte = $bits == 64 ? 0x2 : 0x3;
$uints = array(
0 => array(0x0, $ctrlByte),
500 => array(0x2, $ctrlByte, 0x1, 0xf4),
10872 => array(0x2, $ctrlByte, 0x2a, 0x78),
);
for ($power = 1; $power <= $bits / 8; $power++) {
$expected = bcsub(bcpow(2, 8 * $power), 1);
$input = array($power, $ctrlByte);
for ($i = 2; $i < 2 + $power; $i++) {
$input[$i] = 0xff;
}
$uints[$expected] = $input;
}
return $uints;
}
public function testArrays()
{
$this->validateTypeDecodingList('array', $this->arrays);
}
public function testBooleans()
{
$this->validateTypeDecoding('boolean', $this->booleans);
}
public function testBytes()
{
$this->validateTypeDecoding('byte', $this->bytes());
}
public function testDoubles()
{
$this->validateTypeDecoding('double', $this->doubles);
}
public function testFloats()
{
$this->validateTypeDecoding('float', $this->floats);
}
public function testInt32()
{
$this->validateTypeDecoding('int32', $this->int32);
}
public function testMaps()
{
$this->validateTypeDecodingList('map', $this->maps);
}
public function testPointers()
{
$this->validateTypeDecoding('pointers', $this->pointers);
}
public function testStrings()
{
$this->validateTypeDecoding('utf8_string', $this->strings());
}
public function testUint16()
{
$this->validateTypeDecoding('uint16', $this->uint16);
}
public function testUint32()
{
$this->validateTypeDecoding('uint32', $this->uint32);
}
public function testUint64()
{
$this->validateTypeDecoding('uint64', $this->generateLargeUint(64));
}
public function testUint128()
{
$this->validateTypeDecoding('uint128', $this->generateLargeUint(128));
}
private function validateTypeDecoding($type, $tests)
{
foreach ($tests as $expected => $input) {
$this->checkDecoding($type, $input, $expected);
}
}
private function validateTypeDecodingList($type, $tests)
{
foreach ($tests as $test) {
$this->checkDecoding(
$type,
$test['input'],
$test['expected'],
$test['name']
);
}
}
private function checkDecoding($type, $input, $expected, $name = null)
{
$name = $name || $expected;
$description = "decoded $type - $name";
$handle = fopen('php://memory', 'rw');
foreach ($input as $byte) {
fwrite($handle, pack('C', $byte));
}
fseek($handle, 0);
$decoder = new Decoder($handle, 0, true);
list($actual) = $decoder->decode(0);
if ($type == 'float') {
$actual = round($actual, 2);
}
$this->assertEquals($expected, $actual, $description);
}
}

View File

@@ -0,0 +1,45 @@
<?php
namespace MaxMind\Db\Test\Reader;
use MaxMind\Db\Reader\Decoder;
class PointerTest extends \PHPUnit_Framework_TestCase
{
public function testWithPointers()
{
$handle = fopen('tests/data/test-data/maps-with-pointers.raw', 'r');
$decoder = new Decoder($handle, 0);
$this->assertEquals(
array(array('long_key' => 'long_value1'), 22),
$decoder->decode(0)
);
$this->assertEquals(
array(array('long_key' => 'long_value2'), 37),
$decoder->decode(22)
);
$this->assertEquals(
array(array('long_key2' => 'long_value1'), 50),
$decoder->decode(37)
);
$this->assertEquals(
array(array('long_key2' => 'long_value2'), 55),
$decoder->decode(50)
);
$this->assertEquals(
array(array('long_key' => 'long_value1'), 57),
$decoder->decode(55)
);
$this->assertEquals(
array(array('long_key2' => 'long_value2'), 59),
$decoder->decode(57)
);
}
}

View File

@@ -0,0 +1,391 @@
<?php
namespace MaxMind\Db\Test\Reader;
use MaxMind\Db\Reader;
class ReaderTest extends \PHPUnit_Framework_TestCase
{
public function testReader()
{
foreach (array(24, 28, 32) as $recordSize) {
foreach (array(4, 6) as $ipVersion) {
$fileName = 'tests/data/test-data/MaxMind-DB-test-ipv'
. $ipVersion . '-' . $recordSize . '.mmdb';
$reader = new Reader($fileName);
$this->checkMetadata($reader, $ipVersion, $recordSize);
if ($ipVersion == 4) {
$this->checkIpV4($reader, $fileName);
} else {
$this->checkIpV6($reader, $fileName);
}
}
}
}
public function testDecoder()
{
$reader = new Reader('tests/data/test-data/MaxMind-DB-test-decoder.mmdb');
$record = $reader->get('::1.1.1.0');
$this->assertEquals(true, $record['boolean']);
$this->assertEquals(pack('N', 42), $record['bytes']);
$this->assertEquals('unicode! ☯ - ♫', $record['utf8_string']);
$this->assertEquals(array(1, 2, 3), $record['array']);
$this->assertEquals(
array(
'mapX' => array(
'arrayX' => array(7, 8, 9),
'utf8_stringX' => 'hello'
),
),
$record['map']
);
$this->assertEquals(42.123456, $record['double']);
$this->assertEquals(1.1, $record['float'], 'float', 0.000001);
$this->assertEquals(-268435456, $record['int32']);
$this->assertEquals(100, $record['uint16']);
$this->assertEquals(268435456, $record['uint32']);
$this->assertEquals('1152921504606846976', $record['uint64']);
$uint128 = $record['uint128'];
// For the C extension, which returns a hexadecimal
if (extension_loaded('gmp')) {
$uint128 = gmp_strval($uint128);
} else {
$this->markTestIncomplete('Requires gmp extension to check value of uint128');
}
$this->assertEquals(
'1329227995784915872903807060280344576',
$uint128
);
}
public function testZeros()
{
$reader = new Reader('tests/data/test-data/MaxMind-DB-test-decoder.mmdb');
$record = $reader->get('::');
$this->assertEquals(false, $record['boolean']);
$this->assertEquals('', $record['bytes']);
$this->assertEquals('', $record['utf8_string']);
$this->assertEquals(array(), $record['array']);
$this->assertEquals(array(), $record['map']);
$this->assertEquals(0, $record['double']);
$this->assertEquals(0, $record['float'], 'float', 0.000001);
$this->assertEquals(0, $record['int32']);
$this->assertEquals(0, $record['uint16']);
$this->assertEquals(0, $record['uint32']);
$this->assertEquals(0, $record['uint64']);
$uint128 = $record['uint128'];
if (extension_loaded('gmp')) {
$uint128 = gmp_strval($uint128);
} else {
$this->markTestIncomplete('Requires gmp extension to check value of uint128');
}
$this->assertEquals('0', $uint128);
}
public function testNoIpV4SearchTree()
{
$reader = new Reader(
'tests/data/test-data/MaxMind-DB-no-ipv4-search-tree.mmdb'
);
$this->assertEquals('::0/64', $reader->get('1.1.1.1'));
$this->assertEquals('::0/64', $reader->get('192.1.1.1'));
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Error looking up 2001::. You attempted to look up an IPv6 address in an IPv4-only database
*/
public function testV6AddressV4Database()
{
$reader = new Reader('tests/data/test-data/MaxMind-DB-test-ipv4-24.mmdb');
$reader->get('2001::');
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage The value "not_ip" is not a valid IP address.
*/
public function testIpValidation()
{
$reader = new Reader('tests/data/test-data/MaxMind-DB-test-decoder.mmdb');
$reader->get('not_ip');
}
/**
* @expectedException MaxMind\Db\Reader\InvalidDatabaseException
* @expectedExceptionMessage The MaxMind DB file's data section contains bad data (unknown data type or corrupt data)
*/
public function testBrokenDatabase()
{
$reader = new Reader('tests/data/test-data/GeoIP2-City-Test-Broken-Double-Format.mmdb');
$reader->get('2001:220::');
}
/**
* @expectedException MaxMind\Db\Reader\InvalidDatabaseException
* @expectedExceptionMessage The MaxMind DB file's search tree is corrupt
*/
public function testBrokenSearchTreePointer()
{
$reader = new Reader('tests/data/test-data/MaxMind-DB-test-broken-pointers-24.mmdb');
$reader->get('1.1.1.32');
}
/**
* @expectedException MaxMind\Db\Reader\InvalidDatabaseException
* @expectedExceptionMessage contains bad data
*/
public function testBrokenDataPointer()
{
$reader = new Reader('tests/data/test-data/MaxMind-DB-test-broken-pointers-24.mmdb');
$reader->get('1.1.1.16');
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage The file "file-does-not-exist.mmdb" does not exist or is not readable.
*/
public function testMissingDatabase()
{
new Reader('file-does-not-exist.mmdb');
}
/**
* @expectedException MaxMind\Db\Reader\InvalidDatabaseException
* @expectedExceptionMessage Error opening database file (README.md). Is this a valid MaxMind DB file?
*/
public function testNonDatabase()
{
new Reader('README.md');
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage The constructor takes exactly one argument.
*/
public function testTooManyConstructorArgs()
{
new Reader('README.md', 1);
}
/**
* @expectedException InvalidArgumentException
*
* This test only matters for the extension.
*/
public function testNoConstructorArgs()
{
if (extension_loaded('maxminddb')) {
new Reader();
} else {
throw new \InvalidArgumentException();
}
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Method takes exactly one argument.
*/
public function testTooManyGetAgs()
{
$reader = new Reader(
'tests/data/test-data/MaxMind-DB-test-decoder.mmdb'
);
$reader->get('1.1.1.1', 'blah');
}
/**
* @expectedException InvalidArgumentException
*
* This test only matters for the extension.
*/
public function testNoGetArgs()
{
if (extension_loaded('maxminddb')) {
$reader = new Reader(
'tests/data/test-data/MaxMind-DB-test-decoder.mmdb'
);
$reader->get();
} else {
throw new \InvalidArgumentException();
}
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Method takes no arguments.
*/
public function testMetadataAgs()
{
$reader = new Reader(
'tests/data/test-data/MaxMind-DB-test-decoder.mmdb'
);
$reader->metadata('blah');
}
public function testClose()
{
$reader = new Reader(
'tests/data/test-data/MaxMind-DB-test-decoder.mmdb'
);
$reader->close();
}
/**
* @expectedException BadMethodCallException
* @expectedExceptionMessage Attempt to close a closed MaxMind DB.
*/
public function testDoubleClose()
{
$reader = new Reader(
'tests/data/test-data/MaxMind-DB-test-decoder.mmdb'
);
$reader->close();
$reader->close();
}
/**
* @expectedException BadMethodCallException
* @expectedExceptionMessage Attempt to read from a closed MaxMind DB.
*/
public function testClosedGet()
{
$reader = new Reader(
'tests/data/test-data/MaxMind-DB-test-decoder.mmdb'
);
$reader->close();
$reader->get('1.1.1.1');
}
/**
* @expectedException BadMethodCallException
* @expectedExceptionMessage Attempt to read from a closed MaxMind DB.
*/
public function testClosedMetadata()
{
$reader = new Reader(
'tests/data/test-data/MaxMind-DB-test-decoder.mmdb'
);
$reader->close();
$reader->metadata();
}
private function checkMetadata($reader, $ipVersion, $recordSize)
{
$metadata = $reader->metadata();
$this->assertEquals(
2,
$metadata->binaryFormatMajorVersion,
'major version'
);
$this->assertEquals(0, $metadata->binaryFormatMinorVersion);
$this->assertGreaterThan(1373571901, $metadata->buildEpoch);
$this->assertEquals('Test', $metadata->databaseType);
$this->assertEquals(
array('en' => 'Test Database', 'zh' => 'Test Database Chinese'),
$metadata->description
);
$this->assertEquals($ipVersion, $metadata->ipVersion);
$this->assertEquals(array('en', 'zh'), $metadata->languages);
$this->assertEquals($recordSize / 4, $metadata->nodeByteSize);
$this->assertGreaterThan(36, $metadata->nodeCount);
$this->assertEquals($recordSize, $metadata->recordSize);
$this->assertGreaterThan(200, $metadata->searchTreeSize);
}
private function checkIpV4(Reader $reader, $fileName)
{
for ($i = 0; $i <= 5; $i++) {
$address = '1.1.1.' . pow(2, $i);
$this->assertEquals(
array('ip' => $address),
$reader->get($address),
'found expected data record for '
. $address . ' in ' . $fileName
);
}
$pairs = array(
'1.1.1.3' => '1.1.1.2',
'1.1.1.5' => '1.1.1.4',
'1.1.1.7' => '1.1.1.4',
'1.1.1.9' => '1.1.1.8',
'1.1.1.15' => '1.1.1.8',
'1.1.1.17' => '1.1.1.16',
'1.1.1.31' => '1.1.1.16'
);
foreach ($pairs as $keyAddress => $valueAddress) {
$data = array('ip' => $valueAddress);
$this->assertEquals(
$data,
$reader->get($keyAddress),
'found expected data record for ' . $keyAddress . ' in '
. $fileName
);
}
foreach (array('1.1.1.33', '255.254.253.123') as $ip) {
$this->assertNull($reader->get($ip));
}
}
// XXX - logic could be combined with above
private function checkIpV6(Reader $reader, $fileName)
{
$subnets = array('::1:ffff:ffff', '::2:0:0',
'::2:0:40', '::2:0:50', '::2:0:58');
foreach ($subnets as $address) {
$this->assertEquals(
array('ip' => $address),
$reader->get($address),
'found expected data record for ' . $address . ' in '
. $fileName
);
}
$pairs = array(
'::2:0:1' => '::2:0:0',
'::2:0:33' => '::2:0:0',
'::2:0:39' => '::2:0:0',
'::2:0:41' => '::2:0:40',
'::2:0:49' => '::2:0:40',
'::2:0:52' => '::2:0:50',
'::2:0:57' => '::2:0:50',
'::2:0:59' => '::2:0:58'
);
foreach ($pairs as $keyAddress => $valueAddress) {
$this->assertEquals(
array('ip' => $valueAddress),
$reader->get($keyAddress),
'found expected data record for ' . $keyAddress . ' in '
. $fileName
);
}
foreach (array('1.1.1.33', '255.254.253.123', '89fa::') as $ip) {
$this->assertNull($reader->get($ip));
}
}
}