maxmind 2.10.0 → 2.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/.github/main.workflow +16 -0
  2. package/LICENSE +1 -1
  3. package/README.md +45 -2
  4. package/examples/README.md +2 -0
  5. package/index.d.ts +118 -114
  6. package/lib/bigint.js +2 -2
  7. package/lib/metadata.js +1 -1
  8. package/package.json +6 -5
  9. package/tslint.json +4 -0
  10. package/.nyc_output/b65d0aee5ae72eefe674695f0b3cc653.json +0 -1
  11. package/.nyc_output/c194a11003d542e533dda0e03e1c0249.json +0 -1
  12. package/.nyc_output/e5178fde26f4197a251893de3577725e.json +0 -1
  13. package/scripts/lint +0 -20
  14. package/scripts/release +0 -17
  15. package/scripts/test +0 -24
  16. package/test/data/.git +0 -1
  17. package/test/data/.gitattributes +0 -1
  18. package/test/data/.gitconfig +0 -2
  19. package/test/data/.perltidyallrc +0 -11
  20. package/test/data/.tidyallrc +0 -7
  21. package/test/data/LICENSE +0 -4
  22. package/test/data/MaxMind-DB-spec.md +0 -558
  23. package/test/data/README.md +0 -4
  24. package/test/data/perltidyrc +0 -12
  25. package/test/data/source-data/GeoIP2-Anonymous-IP-Test.json +0 -32
  26. package/test/data/source-data/GeoIP2-City-Test.json +0 -12376
  27. package/test/data/source-data/GeoIP2-Connection-Type-Test.json +0 -102
  28. package/test/data/source-data/GeoIP2-Country-Test.json +0 -10975
  29. package/test/data/source-data/GeoIP2-Domain-Test.json +0 -452
  30. package/test/data/source-data/GeoIP2-Enterprise-Test.json +0 -96
  31. package/test/data/source-data/GeoIP2-ISP-Test.json +0 -12585
  32. package/test/data/source-data/GeoIP2-Precision-City-Test.json +0 -12456
  33. package/test/data/source-data/GeoIP2-Precision-ISP-Test.json +0 -12593
  34. package/test/data/source-data/README +0 -13
  35. package/test/data/test-data/GeoIP2-Anonymous-IP-Test.mmdb +0 -0
  36. package/test/data/test-data/GeoIP2-City-Test-Broken-Double-Format.mmdb +0 -0
  37. package/test/data/test-data/GeoIP2-City-Test.mmdb +0 -0
  38. package/test/data/test-data/GeoIP2-Connection-Type-Test.mmdb +0 -0
  39. package/test/data/test-data/GeoIP2-Country-Test.mmdb +0 -0
  40. package/test/data/test-data/GeoIP2-Domain-Test.mmdb +0 -0
  41. package/test/data/test-data/GeoIP2-Enterprise-Test.mmdb +0 -0
  42. package/test/data/test-data/GeoIP2-ISP-Test.mmdb +0 -0
  43. package/test/data/test-data/GeoIP2-Precision-City-Test.mmdb +0 -0
  44. package/test/data/test-data/GeoIP2-Precision-ISP-Test.mmdb +0 -0
  45. package/test/data/test-data/MaxMind-DB-no-ipv4-search-tree.mmdb +0 -0
  46. package/test/data/test-data/MaxMind-DB-string-value-entries.mmdb +0 -0
  47. package/test/data/test-data/MaxMind-DB-test-broken-pointers-24.mmdb +0 -0
  48. package/test/data/test-data/MaxMind-DB-test-broken-search-tree-24.mmdb +0 -0
  49. package/test/data/test-data/MaxMind-DB-test-decoder.mmdb +0 -0
  50. package/test/data/test-data/MaxMind-DB-test-ipv4-24.mmdb +0 -0
  51. package/test/data/test-data/MaxMind-DB-test-ipv4-28.mmdb +0 -0
  52. package/test/data/test-data/MaxMind-DB-test-ipv4-32.mmdb +0 -0
  53. package/test/data/test-data/MaxMind-DB-test-ipv6-24.mmdb +0 -0
  54. package/test/data/test-data/MaxMind-DB-test-ipv6-28.mmdb +0 -0
  55. package/test/data/test-data/MaxMind-DB-test-ipv6-32.mmdb +0 -0
  56. package/test/data/test-data/MaxMind-DB-test-metadata-pointers.mmdb +0 -0
  57. package/test/data/test-data/MaxMind-DB-test-mixed-24.mmdb +0 -0
  58. package/test/data/test-data/MaxMind-DB-test-mixed-28.mmdb +0 -0
  59. package/test/data/test-data/MaxMind-DB-test-mixed-32.mmdb +0 -0
  60. package/test/data/test-data/MaxMind-DB-test-nested.mmdb +0 -0
  61. package/test/data/test-data/README.md +0 -26
  62. package/test/data/test-data/maps-with-pointers.raw +0 -0
  63. package/test/data/test-data/write-test-data.pl +0 -561
  64. package/test/data/tidyall.ini +0 -5
  65. package/test/databases/GeoIP2-City-Test.mmdb.gz +0 -0
  66. package/test/databases/broken.dat +0 -0
  67. package/test/databases/legacy.dat +0 -0
  68. package/test/decoder.js +0 -78
  69. package/test/e2e.js +0 -146
  70. package/test/index.js +0 -176
  71. package/test/ip.js +0 -89
  72. package/test/is-gzip.js +0 -21
  73. package/test/reader.js +0 -124
package/test/decoder.js DELETED
@@ -1,78 +0,0 @@
1
- 'use strict';
2
-
3
- var assert = require('assert');
4
- var fs = require('fs');
5
- var path = require('path');
6
- var Decoder = require('../lib/decoder');
7
-
8
-
9
- describe('lib/decoder', function() {
10
-
11
- var decoder = new Decoder(
12
- fs.readFileSync(path.join(__dirname, 'data/test-data/GeoIP2-City-Test.mmdb')),
13
- 1
14
- );
15
-
16
- describe('decodeByType()', function() {
17
- it('should fail for unknown type', function() {
18
- assert.throws(function() {
19
- decoder.decodeByType('kraken');
20
- }, /Unknown type/);
21
- });
22
- });
23
-
24
- describe('decodeUint()', function() {
25
- it('should return zero for unsupported int size', function() {
26
- assert.equal(decoder.decodeUint(1, 32), 0);
27
- });
28
- });
29
-
30
- describe('decode()', function() {
31
- it('should throw when extended type has wrong size', function() {
32
- var test = new Decoder(new Buffer([0x00, 0x00]));
33
- assert.throws(function() {
34
- test.decode(0);
35
- }, /Invalid Extended Type at offset 1 val 7/);
36
- });
37
- });
38
-
39
- describe('sizeFromCtrlByte()', function() {
40
- var decoder = new Decoder(new Buffer([0x01, 0x02, 0x03, 0x04]));
41
-
42
- it('should return correct value (size <29)', function() {
43
- assert.deepEqual(decoder.sizeFromCtrlByte(60, 0), { value: 28, offset: 0 });
44
- });
45
-
46
- it('should return correct value (size = 29)', function() {
47
- assert.deepEqual(decoder.sizeFromCtrlByte(61, 0), { value: 30, offset: 1 });
48
- });
49
-
50
- it('should return correct value (size = 30)', function() {
51
- assert.deepEqual(decoder.sizeFromCtrlByte(62, 0), { value: 543, offset: 2 });
52
- });
53
-
54
- it('should return correct value (size = 31)', function() {
55
- assert.deepEqual(decoder.sizeFromCtrlByte(63, 0), { value: 131872, offset: 3 });
56
- });
57
- });
58
-
59
- describe('decodePointer()', function() {
60
- var decoder = new Decoder(new Buffer([0x01, 0x02, 0x03, 0x04]));
61
-
62
- it('should return correct value (pointer size = 0)', function() {
63
- assert.deepEqual(decoder.decodePointer(39, 0), { value: 1793, offset: 1 });
64
- });
65
-
66
- it('should return correct value (pointer size = 1)', function() {
67
- assert.deepEqual(decoder.decodePointer(45, 0), { value: 329986, offset: 2 });
68
- });
69
-
70
- it('should return correct value (pointer size = 2)', function() {
71
- assert.deepEqual(decoder.decodePointer(48, 0), { value: 592387, offset: 3 });
72
- });
73
-
74
- it('should return correct value (pointer size = 3)', function() {
75
- assert.deepEqual(decoder.decodePointer(56, 0), { value: 16909060, offset: 4 });
76
- });
77
- });
78
- });
package/test/e2e.js DELETED
@@ -1,146 +0,0 @@
1
- 'use strict';
2
-
3
- var path = require('path');
4
- var assert = require('assert');
5
- var maxmind = require('../index');
6
- var ipaddr = require('ip-address');
7
-
8
-
9
- var actual = function(file) {
10
- var data = require('./data/source-data/' + file);
11
- var hash = {};
12
- data.forEach(function(item) {
13
- for (var key in item)
14
- hash[key] = item[key];
15
- });
16
-
17
- return {
18
- hash: hash,
19
- get: function(subnet) {
20
- var item = hash[subnet];
21
- assert(item);
22
- return item;
23
- }
24
- };
25
- };
26
-
27
- describe('maxmind', function() {
28
-
29
- var dataDir = path.join(__dirname, 'data/test-data');
30
-
31
- describe('basic functionality', function() {
32
-
33
- it('should successfully handle database', function() {
34
- assert(maxmind.openSync(path.join(dataDir, 'GeoIP2-City-Test.mmdb')));
35
- });
36
-
37
- it('should fetch geo ip', function() {
38
- var geoIp = maxmind.openSync(path.join(dataDir, 'GeoIP2-City-Test.mmdb'));
39
- var data = actual('GeoIP2-City-Test.json');
40
- assert.deepEqual(geoIp.get('1.1.1.1'), null);
41
-
42
- assert.deepEqual(geoIp.get('175.16.198.255'), null);
43
- assert.deepEqual(geoIp.get('175.16.199.1'), data.get('::175.16.199.0/120'));
44
- assert.deepEqual(geoIp.get('175.16.199.255'), data.get('::175.16.199.0/120'));
45
- assert.deepEqual(geoIp.get('::175.16.199.255'), data.get('::175.16.199.0/120'));
46
- assert.deepEqual(geoIp.get('175.16.200.1'), null);
47
-
48
- assert.deepEqual(geoIp.get('2a02:cf40:ffff::'), data.get('2a02:cf40::/29'));
49
- assert.deepEqual(geoIp.get('2a02:cf47:0000::'), data.get('2a02:cf40::/29'));
50
- assert.deepEqual(geoIp.get('2a02:cf48:0000::'), null);
51
- });
52
-
53
- it('should handle corrupt database', function() {
54
- assert.throws(function verify() {
55
- maxmind.openSync('./data/README.md');
56
- });
57
- });
58
-
59
- it('should accept cache options', function() {
60
- assert(maxmind.openSync(path.join(dataDir, 'GeoIP2-City-Test.mmdb'), {
61
- cache: { max: 1000 }
62
- }));
63
- });
64
- });
65
-
66
- describe('section: data', function() {
67
- it('should decode all possible types - complex', function() {
68
- var geoIp = maxmind.openSync(path.join(dataDir, 'MaxMind-DB-test-decoder.mmdb'));
69
- assert.deepEqual(geoIp.get('::1.1.1.1'), {
70
- array: [1, 2, 3],
71
- boolean: true,
72
- bytes: new Buffer([0, 0, 0, 42]),
73
- double: 42.123456,
74
- // It should be 1.1, but there's some issue with rounding in v8
75
- float: 1.100000023841858,
76
- int32: -268435456,
77
- map: { mapX: { arrayX: [7, 8, 9], utf8_stringX: 'hello' } },
78
- uint128: '1329227995784915872903807060280344576',
79
- uint16: 100,
80
- uint32: 268435456,
81
- uint64: '1152921504606846976',
82
- utf8_string: 'unicode! ☯ - ♫'
83
- });
84
- });
85
-
86
- it('should decode all possible types - zero/empty values', function() {
87
- var geoIp = maxmind.openSync(path.join(dataDir, 'MaxMind-DB-test-decoder.mmdb'));
88
- assert.deepEqual(geoIp.get('::0.0.0.0'), {
89
- array: [],
90
- boolean: false,
91
- bytes: new Buffer([]),
92
- double: 0,
93
- float: 0,
94
- int32: 0,
95
- map: {},
96
- uint128: '0',
97
- uint16: 0,
98
- uint32: 0,
99
- uint64: '0',
100
- utf8_string: ''
101
- });
102
- });
103
-
104
- it('should return correct value: string entries', function() {
105
- var geoIp = maxmind.openSync(path.join(dataDir, 'MaxMind-DB-string-value-entries.mmdb'));
106
- assert.equal(geoIp.get('1.1.1.1'), '1.1.1.1/32');
107
- assert.equal(geoIp.get('1.1.1.2'), '1.1.1.2/31');
108
- assert.equal(geoIp.get('175.2.1.1'), null);
109
- });
110
- });
111
-
112
- describe('section: binary search tree', function() {
113
-
114
- var files = [
115
- 'GeoIP2-Anonymous-IP-Test',
116
- 'GeoIP2-City-Test',
117
- 'GeoIP2-Connection-Type-Test',
118
- 'GeoIP2-Country-Test',
119
- 'GeoIP2-Domain-Test',
120
- 'GeoIP2-Enterprise-Test',
121
- 'GeoIP2-ISP-Test',
122
- 'GeoIP2-Precision-City-Test',
123
- 'GeoIP2-Precision-ISP-Test'
124
- ];
125
-
126
- var tester = function(geoIp, data) {
127
- for (var subnet in data.hash) {
128
- var ip = new ipaddr.Address6(subnet);
129
- // TODO: check random address from the subnet?
130
- // see http://ip-address.js.org/#address4/biginteger
131
- // see https://github.com/andyperlitch/jsbn
132
- assert.deepEqual(geoIp.get(ip.startAddress().address), data.hash[subnet], subnet);
133
- assert.deepEqual(geoIp.get(ip.endAddress().address), data.hash[subnet], subnet);
134
- }
135
- };
136
-
137
- files.forEach(function(file) {
138
- it('should test everything: ' + file, function() {
139
- var geoIp = maxmind.openSync(path.join(dataDir, '/' + file + '.mmdb'));
140
- var data = actual(file + '.json');
141
- tester(geoIp, data);
142
- });
143
- });
144
- });
145
-
146
- });
package/test/index.js DELETED
@@ -1,176 +0,0 @@
1
- 'use strict';
2
-
3
- var sinon = require('sinon');
4
- var assert = require('assert');
5
- var path = require('path');
6
- var fs = require('fs');
7
- var maxmind = require('../index');
8
- var Reader = require('../lib/reader');
9
-
10
- describe('index', function() {
11
- var dataDir = path.join(__dirname, 'data/test-data');
12
- var dbPath = path.join(dataDir, 'GeoIP2-City-Test.mmdb');
13
-
14
- var sandbox;
15
- var watchHandler;
16
- beforeEach(function() {
17
- sandbox = sinon.createSandbox();
18
- sandbox.stub(fs, 'watch').callsFake(function(path, options, cb) { watchHandler = cb; });
19
- sandbox.spy(fs, 'readFile');
20
- sandbox.spy(fs, 'readFileSync');
21
- });
22
- afterEach(function() { sandbox.restore(); });
23
-
24
-
25
- describe('validate()', function() {
26
- it('should work fine for both IPv4 and IPv6', function() {
27
- assert.equal(maxmind.validate('64.4.4.4'), true);
28
- assert.equal(maxmind.validate('2001:4860:0:1001::3004:ef68'), true);
29
- assert.equal(maxmind.validate('whhaaaazza'), false);
30
- });
31
- });
32
-
33
- describe('init()', function() {
34
- it('should fail when someone tries to use legacy api', function() {
35
- assert.throws(function() {
36
- maxmind.init();
37
- }, /Maxmind v1 module has changed API/);
38
- });
39
- });
40
-
41
- describe('open()', function() {
42
- it('should work with most basic usage', function(done) {
43
- maxmind.open(dbPath, function(err, lookup) {
44
- if (err) return done(err);
45
- assert(lookup.get('2001:230::'));
46
- done();
47
- });
48
- });
49
-
50
- it('should successfully handle database, with opts', function(done) {
51
- var options = { cache: { max: 1000 }, watchForUpdates: true };
52
- maxmind.open(dbPath, options, function(err, lookup) {
53
- if (err) return done(err);
54
- assert(lookup.get('2001:230::'));
55
- assert(fs.watch.calledOnce);
56
- assert(fs.readFile.calledOnce);
57
- done();
58
- });
59
- });
60
-
61
- it('should work with auto updates', function(done) {
62
- var options = { watchForUpdates: true };
63
- maxmind.open(dbPath, options, function(err, lookup) {
64
- if (err) return done(err);
65
- assert(lookup.get('2001:230::'));
66
- assert(fs.watch.calledOnce);
67
- assert(fs.readFile.calledOnce);
68
- watchHandler();
69
- assert(fs.readFile.calledTwice);
70
- done();
71
- });
72
- });
73
-
74
- it('should work with auto updates and call specified hook', function(done) {
75
- var hook = sinon.spy();
76
- var options = {
77
- watchForUpdates: true,
78
- watchForUpdatesHook: hook,
79
- watchForUpdatesNonPersistent: false,
80
- };
81
- maxmind.open(dbPath, options, function(err, lookup) {
82
- if (err) return done(err);
83
- assert(lookup.get('2001:230::'));
84
- assert(hook.notCalled);
85
- watchHandler();
86
- assert(hook.calledOnce);
87
- done();
88
- });
89
- });
90
-
91
- it('should successfully handle errors while opening a db', function(done) {
92
- maxmind.open('/foo/bar', function(err) {
93
- assert.equal(err.code, 'ENOENT');
94
- done();
95
- });
96
- });
97
-
98
- it('should throw an error when no callback provided', function() {
99
- assert.throws(function() {
100
- maxmind.open(dbPath);
101
- }, /Callback function must be provided/);
102
- });
103
-
104
- it('should return an error when gzip file attempted', function(done) {
105
- var dbPath = path.join(__dirname, 'databases/GeoIP2-City-Test.mmdb.gz');
106
- maxmind.open(dbPath, function(err) {
107
- assert.equal(err.message,
108
- 'Looks like you are passing in a file in gzip format, please use mmdb database instead.');
109
- done();
110
- });
111
- });
112
-
113
- it('should check for an error when cannot read database on update', function(done) {
114
- var counter = 0;
115
- var cb = function(err, reader) {
116
- // Indeed couter is kinda gross.
117
- switch (counter++) {
118
- case 0:
119
- assert.equal(err, null);
120
- assert(reader instanceof Reader);
121
- assert(fs.readFile.calledOnce);
122
- fs.readFile.restore();
123
- sandbox.stub(fs, 'readFile').callsFake(function(path, cb) { cb(new Error('Crazy shit')); });
124
- watchHandler();
125
- break;
126
-
127
- case 1:
128
- assert.equal(err.message, 'Crazy shit');
129
- done();
130
- break;
131
-
132
- default:
133
- done(new Error('Only two calls should happen'));
134
- }
135
- };
136
- maxmind.open(dbPath, { watchForUpdates: true }, cb);
137
- });
138
-
139
- it('should handler reader errors', function(done) {
140
- maxmind.open(path.join(__dirname, 'databases/broken.dat'), function(err) {
141
- assert.equal(err.message, 'Cannot parse binary database');
142
- done();
143
- });
144
- });
145
- });
146
-
147
- describe('openSync()', function() {
148
- it('should successfully handle database', function() {
149
- var lookup = maxmind.openSync(dbPath);
150
- assert(lookup.get('2001:230::'));
151
- });
152
-
153
- it('should successfully handle database updates', function() {
154
- var lookup = maxmind.openSync(dbPath, { watchForUpdates: true });
155
- assert(lookup.get('2001:230::'));
156
- assert(fs.watch.calledOnce);
157
- assert(fs.readFileSync.calledOnce);
158
- watchHandler();
159
- assert(fs.readFileSync.calledTwice);
160
- });
161
-
162
- it('should successfully handle database updates and call specified hook', function() {
163
- var hook = sinon.spy();
164
- var opts = {
165
- watchForUpdates: true,
166
- watchForUpdatesHook: hook,
167
- };
168
- var lookup = maxmind.openSync(dbPath, opts);
169
- assert(lookup.get('2001:230::'));
170
- assert(hook.notCalled);
171
- watchHandler();
172
- assert(hook.calledOnce);
173
- });
174
-
175
- });
176
- });
package/test/ip.js DELETED
@@ -1,89 +0,0 @@
1
- 'use strict';
2
-
3
- var assert = require('assert');
4
- var ip = require('../lib/ip');
5
-
6
-
7
- describe('lib/ip', function() {
8
-
9
- describe('parse()', function() {
10
- describe('ipv4', function() {
11
- it('should successfully parse v4', function() {
12
- assert.deepEqual(ip.parse('127.0.0.1'), [0x7f, 0x00, 0x00, 0x01]);
13
- assert.deepEqual(ip.parse('10.10.200.59'), [0x0a, 0x0a, 0xc8, 0x3b]);
14
- });
15
- });
16
-
17
- describe('ipv6', function() {
18
- it('should parse complete address', function() {
19
- assert.deepEqual(
20
- ip.parse('2001:0db8:85a3:0042:1000:8a2e:0370:7334'),
21
- [0x20, 0x1, 0xd, 0xb8, 0x85, 0xa3, 0, 0x42, 0x10, 0, 0x8a, 0x2e, 0x03, 0x70, 0x73, 0x34]
22
- );
23
- assert.deepEqual(
24
- ip.parse('2001:0db8:85a3:0000:0000:8a2e:0370:7334'),
25
- [0x20, 0x01, 0x0d, 0xb8, 0x85, 0xa3, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x2e, 0x03, 0x70, 0x73, 0x34]
26
- );
27
- });
28
-
29
- it('should parse two-part address', function() {
30
- assert.deepEqual(
31
- ip.parse('2001:4860:0:1001::3004:ef68'),
32
- [0x20, 0x01, 0x48, 0x60, 0, 0, 0x10, 0x01, 0, 0, 0, 0, 0x30, 0x04, 0xef, 0x68]
33
- );
34
- assert.deepEqual(
35
- ip.parse('2001:db8:85a3::8a2e:370:7334'),
36
- [0x20, 0x01, 0x0d, 0xb8, 0x85, 0xa3, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x2e, 0x03, 0x70, 0x73, 0x34]
37
- );
38
- });
39
-
40
- it('should parse `::` in the end of address', function() {
41
- var expected = [0x20, 0x01, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
42
- assert.deepEqual(ip.parse('2001:200::'), expected);
43
- assert.deepEqual(ip.parse('2001:0200:0000:0000:0000:0000:0000:0000'), expected);
44
- });
45
-
46
- it('should parse ipv4 with `::ffff`', function() {
47
- var expected = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 1, 2, 254, 216];
48
- assert.deepEqual(ip.parse('::ffff:1.2.254.216'), expected);
49
- assert.deepEqual(ip.parse('::ffff:0102:fed8'), expected);
50
- assert.deepEqual(ip.parse('::ffff:102:fed8'), expected);
51
- assert.deepEqual(ip.parse('0000:0000:0000:0000:0000:ffff:0102:fed8'), expected);
52
- assert.deepEqual(ip.parse('0000:0000:0000:0000:0000:ffff:102:fed8'), expected);
53
- });
54
-
55
- it('should parse ipv4 with `::`', function() {
56
- var expected = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 17, 254, 216];
57
- assert.deepEqual(ip.parse('::64.17.254.216'), expected);
58
- assert.deepEqual(ip.parse('::4011:fed8'), expected);
59
- assert.deepEqual(ip.parse('0000:0000:0000:0000:0000:0000:4011:fed8'), expected);
60
- });
61
- });
62
- });
63
-
64
- describe('bitAt()', function() {
65
- it('should return correct bit for given offset', function() {
66
- var address = new Buffer([0x0a, 0x0a, 0xc8, 0x3b]);
67
- assert.strictEqual(ip.bitAt(address, 1), 0);
68
- assert.strictEqual(ip.bitAt(address, 10), 0);
69
- assert.strictEqual(ip.bitAt(address, 23), 0);
70
- assert.strictEqual(ip.bitAt(address, 31), 1);
71
- assert.strictEqual(ip.bitAt(address, 999), 0);
72
- });
73
- });
74
-
75
- describe('validate()', function() {
76
- it('should work fine for IPv4', function() {
77
- assert.equal(ip.validate('64.4.4.4'), true);
78
- assert.equal(ip.validate('64.4.4.boom!'), false);
79
- assert.equal(ip.validate(undefined), false);
80
- assert.equal(ip.validate('kraken'), false);
81
- });
82
-
83
- it('should work fine for IPv6', function() {
84
- assert.equal(ip.validate('2001:4860:0:1001::3004:ef68'), true);
85
- assert.equal(ip.validate('::64.17.254.216'), true);
86
- assert.equal(ip.validate('2001:4860:0:1001::3004:boom!'), false);
87
- });
88
- });
89
- });
package/test/is-gzip.js DELETED
@@ -1,21 +0,0 @@
1
- 'use strict';
2
-
3
- var assert = require('assert');
4
- var isGzip = require('../lib/is-gzip');
5
-
6
- describe('lib/is-gzip', function() {
7
- it('should return false for short buffers', function() {
8
- assert.equal(isGzip(new Buffer([1, 2])), false);
9
- });
10
-
11
- it('should return false for string buffer', function() {
12
- assert.equal(isGzip(new Buffer('kraken')), false);
13
- });
14
-
15
- it('should return false for string buffer', function() {
16
- // gzipped "kraken" string
17
- // shell: `echo "kraken" | gzip | base64`
18
- var buffer = new Buffer('H4sIAGBDv1gAA8suSsxOzeMCAKjj9U8HAAAA', 'base64');
19
- assert.equal(isGzip(buffer), true);
20
- });
21
- });
package/test/reader.js DELETED
@@ -1,124 +0,0 @@
1
- 'use strict';
2
-
3
- var fs = require('fs');
4
- var path = require('path');
5
- var assert = require('assert');
6
- var Reader = require('../lib/reader');
7
-
8
-
9
- describe('lib/reader', function() {
10
-
11
- var dataDir = path.join(__dirname, 'data/test-data');
12
- var read = function(dir, filepath) {
13
- return fs.readFileSync(path.join(dir, filepath));
14
- };
15
-
16
- describe('findAddressInTree()', function() {
17
-
18
- it('should work for most basic case', function() {
19
- var reader = new Reader(read(dataDir, 'GeoIP2-City-Test.mmdb'));
20
- assert.equal(reader.findAddressInTree('1.1.1.1'), null);
21
- });
22
-
23
- it('should return correct value: city database', function() {
24
- var reader = new Reader(read(dataDir, 'GeoIP2-City-Test.mmdb'));
25
- assert.equal(reader.findAddressInTree('1.1.1.1'), null);
26
- assert.equal(reader.findAddressInTree('175.16.199.1'), 3042);
27
- assert.equal(reader.findAddressInTree('175.16.199.88'), 3042);
28
- assert.equal(reader.findAddressInTree('175.16.199.255'), 3042);
29
- assert.equal(reader.findAddressInTree('::175.16.199.255'), 3042);
30
- assert.equal(reader.findAddressInTree('::ffff:175.16.199.255'), 3042);
31
- assert.equal(reader.findAddressInTree('2a02:cf40:ffff::'), 4735);
32
- assert.equal(reader.findAddressInTree('2a02:cf47:0000::'), 4735);
33
- assert.equal(reader.findAddressInTree('2a02:cf47:0000:fff0:ffff::'), 4735);
34
- assert.equal(reader.findAddressInTree('2a02:cf48:0000::'), null);
35
- });
36
-
37
- it('should return correct value: string entries', function() {
38
- var reader = new Reader(read(dataDir, 'MaxMind-DB-string-value-entries.mmdb'));
39
- assert.equal(reader.findAddressInTree('1.1.1.1'), 98);
40
- assert.equal(reader.findAddressInTree('1.1.1.2'), 87);
41
- assert.equal(reader.findAddressInTree('175.2.1.1'), null);
42
- });
43
-
44
- describe('various record sizes and ip versions', function() {
45
- var ips = {
46
- v4: {
47
- '1.1.1.1': 102,
48
- '1.1.1.2': 90,
49
- '1.1.1.32': 114,
50
- '1.1.1.33': null
51
- },
52
- v6: {
53
- '::1:ffff:fffa': null,
54
- '::1:ffff:ffff': 176,
55
- '::2:0000:0000': 194,
56
- '::2:0000:0060': null
57
- },
58
- mix: {
59
- '1.1.1.1': 315,
60
- '1.1.1.2': 301,
61
- '1.1.1.32': 329,
62
- '1.1.1.33': null,
63
- '::1:ffff:fffa': null,
64
- '::1:ffff:ffff': 344,
65
- '::2:0000:0000': 362,
66
- '::2:0000:0060': null
67
- }
68
- };
69
-
70
- var scenarios = {
71
- 'MaxMind-DB-test-ipv4-24.mmdb': ips.v4,
72
- 'MaxMind-DB-test-ipv4-28.mmdb': ips.v4,
73
- 'MaxMind-DB-test-ipv4-32.mmdb': ips.v4,
74
- 'MaxMind-DB-test-ipv6-24.mmdb': ips.v6,
75
- 'MaxMind-DB-test-ipv6-28.mmdb': ips.v6,
76
- 'MaxMind-DB-test-ipv6-32.mmdb': ips.v6,
77
- 'MaxMind-DB-test-mixed-24.mmdb': ips.mix,
78
- 'MaxMind-DB-test-mixed-28.mmdb': ips.mix,
79
- 'MaxMind-DB-test-mixed-32.mmdb': ips.mix
80
- };
81
-
82
- for (var file in scenarios) {
83
- (function(file, ips) {
84
- it('should return correct value: ' + file, function() {
85
- var reader = new Reader(read(dataDir, '' + file));
86
- for (var ip in ips) {
87
- assert.equal(reader.findAddressInTree(ip), ips[ip], 'IP: ' + ip);
88
- }
89
- });
90
- })(file, scenarios[file]);
91
- }
92
- });
93
-
94
- describe('broken files and search trees', function() {
95
- it('should behave fine when there is no ipv4 search tree', function() {
96
- var reader = new Reader(read(dataDir, 'MaxMind-DB-no-ipv4-search-tree.mmdb'));
97
- assert.equal(reader.findAddressInTree('::1:ffff:ffff'), 80);
98
- // TODO: perhaps null should be returned here, note that pointer is larger than file itself
99
- assert.equal(reader.findAddressInTree('1.1.1.1'), 4811873);
100
- });
101
-
102
- it('should behave fine when search tree is broken', function() {
103
- // TODO: find out in what way the file is broken
104
- var reader = new Reader(read(dataDir, 'MaxMind-DB-test-broken-search-tree-24.mmdb'));
105
- assert.equal(reader.findAddressInTree('1.1.1.1'), 102);
106
- assert.equal(reader.findAddressInTree('1.1.1.2'), 90);
107
- });
108
- });
109
-
110
- describe('invalid database format', function() {
111
- it('should provide meaningful message when one tries to use legacy db', function() {
112
- assert.throws(function() {
113
- new Reader(read(path.join(__dirname, 'databases'), 'legacy.dat'));
114
- }, /Maxmind v1 module has changed API/);
115
- });
116
-
117
- it('should provide meaningful message when one tries to use unknown format', function() {
118
- assert.throws(function() {
119
- new Reader(read(path.join(__dirname, 'databases'), 'broken.dat'));
120
- }, /Cannot parse binary database/);
121
- });
122
- });
123
- });
124
- });