forked from metowolf/ipdb-old
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
47 lines (41 loc) · 2.31 KB
/
Copy pathtest.js
File metadata and controls
47 lines (41 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
import net from 'net'
import test from 'ava'
import database from '@ipdb/database'
import IPDB from '.'
const ipdb = new IPDB(database)
const countryTest = (t, input, expected) => {
const result = ipdb.find(input)
if (result.code === 0) {
t.deepEqual([
result.data.country_name,
result.data.city_name,
result.data.region_name
], expected)
} else {
t.is(result.message, expected)
}
}
// Normal IPv4
test('127.0.0.1', countryTest, '127.0.0.1', ['本机地址', '', '本机地址'])
test('192.168.1.1', countryTest, '192.168.1.1', ['局域网', '', '局域网'])
test('10.10.200.59', countryTest, '10.10.200.59', ['局域网', '', '局域网'])
test('1.2.3.4', countryTest, '1.2.3.4', ['APNIC.NET', '', 'APNIC.NET'])
test('201.123.244.2', countryTest, '201.123.244.2', ['墨西哥', '', '墨西哥'])
test('1.0.0.1', countryTest, '1.0.0.1', ['CLOUDFLARE.COM', '', 'CLOUDFLARE.COM'])
// Error format
test('1.0.0', countryTest, '1.0.0', 'ip format error')
test('1.0.0.256', countryTest, '1.0.0.256', 'ip format error')
test('1.0.0.0.1', countryTest, '1.0.0.0.1', 'ip format error')
test('1.1.1.-1', countryTest, '1.1.1.-1', 'ip format error')
const bitsTest = (t, input, expected) => {
const version = net.isIP(input)
const result = version === 4 ? ipdb._toBits4(input) : ipdb._toBits6(input)
t.deepEqual(result.join(''), expected)
}
// Bits
test('bits/127.0.0.1', bitsTest, '127.0.0.1', '01111111000000000000000000000001')
test('bits/192.168.1.1', bitsTest, '192.168.1.1', '11000000101010000000000100000001')
test('bits/::ffff:0102:fed8', bitsTest, '::ffff:0102:fed8', '00000000000000000000000000000000000000000000000000000000000000000000000000000000111111111111111100000001000000101111111011011000')
test('bits/::ffff:102:fed8', bitsTest, '::ffff:102:fed8', '00000000000000000000000000000000000000000000000000000000000000000000000000000000111111111111111100000001000000101111111011011000')
test('bits/0::ffff:102:fed8', bitsTest, '0::ffff:102:fed8', '00000000000000000000000000000000000000000000000000000000000000000000000000000000111111111111111100000001000000101111111011011000')
test('bits/2001:0db8:85a3:0000:0000:8a2e:0370:7334', bitsTest, '2001:0db8:85a3:0000:0000:8a2e:0370:7334', '00100000000000010000110110111000100001011010001100000000000000000000000000000000100010100010111000000011011100000111001100110100')