diff --git a/binding/javascript/tests/buffer.test.js b/binding/javascript/tests/buffer.test.js deleted file mode 100644 index 9143f55..0000000 --- a/binding/javascript/tests/buffer.test.js +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright 2022 The Ip2Region Authors. All rights reserved. -// Use of this source code is governed by a Apache2.0-style -// license that can be found in the LICENSE file. - -// util test script -// @Author Lion - -import * as util from '../util.js'; -import path from 'node:path'; -import { fileURLToPath } from 'url'; - -const __dirname = path.dirname(fileURLToPath(import.meta.url)); -const dbPath = path.join(__dirname, '..', '..', '..', 'data', 'ip2region_v4.xdb') - -test('test load header', () => { - let header = util.loadHeaderFromFile(dbPath); - console.log(`dbPath: ${dbPath}, header: ${header.toString()}}`); -}); - -test('test load vector index', () => { - let vIndex = util.loadVectorIndexFromFile(dbPath); - console.log(`dbPath: ${dbPath}, vIndex: ${vIndex.length}}`); -}); - -test('test load content', () => { - let content = util.loadContentFromFile(dbPath); - console.log(`dbPath: ${dbPath}, content: ${content.length}}`); -}); \ No newline at end of file diff --git a/binding/javascript/tests/ip.test.js b/binding/javascript/tests/ip.test.js deleted file mode 100644 index 66f43c0..0000000 --- a/binding/javascript/tests/ip.test.js +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright 2022 The Ip2Region Authors. All rights reserved. -// Use of this source code is governed by a Apache2.0-style -// license that can be found in the LICENSE file. - -// util test script -// @Author Lion - -import * as util from '../util.js'; - -test('parse ip address', () => { - let ip_list = [ - "1.0.0.0", "58.251.30.115", "192.168.1.100", "126.255.32.255", "219.xx.xx.11", - "::", "::1", "fffe::", "2c0f:fff0::", "2c0f:fff0::1", "2a02:26f7:c409:4001::", - "2fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", "240e:982:e617:ffff:ffff:ffff:ffff:ffff", "::xx:ffff" - ]; - - ip_list.forEach(ipString => { - let ipBytes = null; - try { - ipBytes = util.parseIP(ipString); - } catch (e) { - console.log(`failed to parse ip '${ipString}': ${e.message}`); - return; - } - - let to_Str = util.ipToString(ipBytes, true); - let toByte = util.ipBytesString(ipBytes); - console.log(`parseIP(${ipString}): {Bytes: ${toByte}, String: ${to_Str}}`); - expect(ipString).toBe(to_Str); - }); -}); - -test('ip compare', () => { - let ip_list = [ - ["1.0.0.0", "1.0.0.1", -1], - ["192.168.1.101", "192.168.1.90", 1], - ["219.133.111.87", "114.114.114.114", 1], - ["2000::", "2000:ffff:ffff:ffff:ffff:ffff:ffff:ffff", -1], - ["2001:4:112::", "2001:4:112:ffff:ffff:ffff:ffff:ffff", -1], - ["ffff::", "2001:4:ffff:ffff:ffff:ffff:ffff:ffff", 1] - ]; - - ip_list.forEach(ips => { - const ip1 = util.parseIP(ips[0]); - const ip2 = util.parseIP(ips[1]); - const cmp = util.ipCompare(ip1, ip2); - expect(cmp).toBe(ips[2]); - console.log(`compare(${ips[0]}, ${ips[1]}): ${cmp}`); - }); -}); \ No newline at end of file diff --git a/binding/javascript/tests/search.test.js b/binding/javascript/tests/search.test.js deleted file mode 100644 index d5613de..0000000 --- a/binding/javascript/tests/search.test.js +++ /dev/null @@ -1,85 +0,0 @@ -// Copyright 2022 The Ip2Region Authors. All rights reserved. -// Use of this source code is governed by a Apache2.0-style -// license that can be found in the LICENSE file. - -// searcher search tester -// @Author Lion - -import {IPv4, IPv6, parseIP, ipToString, newWithFileOnly, verifyFromFile} from '../index.js'; -import path from 'path'; -import { fileURLToPath } from 'url'; - -const __dirname = path.dirname(fileURLToPath(import.meta.url)); -const dbPath = { - v4: path.join(__dirname, '..', '..', '..', 'data', 'ip2region_v4.xdb'), - v6: path.join(__dirname, '..', '..', '..', 'data', 'ip2region_v6.xdb') -} - -test('ipv4 search test', async () => { - // verify the xdb file - // @Note: do NOT call it every time you create a searcher since this will slow - // down the search response. - // @see the verify function for details. - try { - verifyFromFile(dbPath.v4); - console.log(`xdb file '${dbPath.v4}' verified`); - } catch (e) { - console.log(`binding is not applicable for xdb file '${dbPath.v4}': ${e.message}`); - return; - } - - let searcher = newWithFileOnly(IPv4, dbPath.v4); - let ip_list = [ - '1.0.0.0', - parseIP('113.118.112.93'), - '240e:3b7::' - ]; - - for (var i = 0; i < ip_list.length; i++) { - let ip = ip_list[i]; - searcher.search(ip).then((region)=>{ - let ipStr = Buffer.isBuffer(ip) ? ipToString(ip) : ip; - console.log(`search(${ipStr}): {region: ${region}, ioCount: ${searcher.getIOCount()}}`); - }).catch((err) => { - console.log(`${err.message}`); - }); - } - - // close searcher - searcher.close(); -}); - -test('ipv6 search test', async () => { - // verify the xdb file - // @Note: do NOT call it every time you create a searcher since this will slow - // down the search response. - // @see the verify function for details. - try { - verifyFromFile(dbPath.v6); - console.log(`xdb file '${dbPath.v6}' verified`); - } catch (e) { - console.log(`binding is not applicable for xdb file '${dbPath.v6}': ${e.message}`); - return; - } - - let searcher = newWithFileOnly(IPv6, dbPath.v6); - let ip_list = [ - '2a02:26f7:c409:4001::', - parseIP('2a11:8080:200::a:a05c'), - '240e:3b7::', - '120.229.45.92' - ]; - - for (var i = 0; i < ip_list.length; i++) { - let ip = ip_list[i]; - searcher.search(ip).then((region)=>{ - let ipStr = Buffer.isBuffer(ip) ? ipToString(ip) : ip; - console.log(`search(${ipStr}): {region: ${region}, ioCount: ${searcher.getIOCount()}}`); - }).catch((err) => { - console.log(`${err.message}`); - }); - } - - // close searcher - searcher.close(); -}); \ No newline at end of file diff --git a/binding/javascript/tests/searcher.test.js b/binding/javascript/tests/searcher.test.js index 05f401f..b001d14 100644 --- a/binding/javascript/tests/searcher.test.js +++ b/binding/javascript/tests/searcher.test.js @@ -9,15 +9,87 @@ import path from 'path'; import { fileURLToPath } from 'url'; import { IPv4, IPv6, XdbIPv4Id, + parseIP, ipToString, verifyFromFile, loadVectorIndexFromFile, loadContentFromFile, newWithFileOnly, newWithVectorIndex, newWithBuffer } from '../index.js'; +import { fail } from 'assert'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const dbPath = { v4: path.join(__dirname, '..', '..', '..', 'data', 'ip2region_v4.xdb'), v6: path.join(__dirname, '..', '..', '..', 'data', 'ip2region_v6.xdb') } +test('xdb file verify', () => { + // verify the xdb file + // @Note: do NOT call it every time you create a searcher since this will slow + // down the search response. + // @see the verify function for details. + for (k in dbPath) { + if (!dbPath.hasOwnProperty(k)) { + continue; + } + + try { + verifyFromFile(dbPath[k]); + console.log(`xdb file '${dbPath[k]}' verified`); + } catch (e) { + throw new Error(`binding is not applicable for xdb file '${dbPath[k]}': ${e.message}`); + } + } +}); + +// --- +// search api testing + +test('ipv4 search test', async () => { + let searcher = newWithFileOnly(IPv4, dbPath.v4); + let ip_list = [ + '1.0.0.0', + parseIP('113.118.112.93'), + '240e:3b7::' + ]; + + for (var i = 0; i < ip_list.length; i++) { + let ip = ip_list[i]; + searcher.search(ip).then((region)=>{ + let ipStr = Buffer.isBuffer(ip) ? ipToString(ip) : ip; + console.log(`search(${ipStr}): {region: ${region}, ioCount: ${searcher.getIOCount()}}`); + }).catch((err) => { + console.log(`${err.message}`); + }); + } + + // close searcher + searcher.close(); +}); + +test('ipv6 search test', async () => { + let searcher = newWithFileOnly(IPv6, dbPath.v6); + let ip_list = [ + '2a02:26f7:c409:4001::', + parseIP('2a11:8080:200::a:a05c'), + '240e:3b7::', + '120.229.45.92' + ]; + + for (var i = 0; i < ip_list.length; i++) { + let ip = ip_list[i]; + searcher.search(ip).then((region)=>{ + let ipStr = Buffer.isBuffer(ip) ? ipToString(ip) : ip; + console.log(`search(${ipStr}): {region: ${region}, ioCount: ${searcher.getIOCount()}}`); + }).catch((err) => { + console.log(`${err.message}`); + }); + } + + // close searcher + searcher.close(); +}); + + +// --- +// searcher with different cache policy testing function _get_creater_list(version) { let dbFile = version.id == XdbIPv4Id ? dbPath.v4 : dbPath.v6; diff --git a/binding/javascript/tests/util.test.js b/binding/javascript/tests/util.test.js new file mode 100644 index 0000000..e3257d1 --- /dev/null +++ b/binding/javascript/tests/util.test.js @@ -0,0 +1,114 @@ +// Copyright 2022 The Ip2Region Authors. All rights reserved. +// Use of this source code is governed by a Apache2.0-style +// license that can be found in the LICENSE file. + +// util test script +// @Author Lion + +import * as util from '../util.js'; +import path from 'node:path'; +import { fileURLToPath } from 'url'; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); +const dbPath = path.join(__dirname, '..', '..', '..', 'data', 'ip2region_v4.xdb') + +test('const print', () => { + console.log("IPv4: ", util.IPv4.toString()); + console.log("IPv6: ", util.IPv6.toString()); +}); + +test("test version from name", () => { + let vs = ["v4", "ipv4", "v4x", "v6", "ipv6", "v6x"]; + vs.forEach(ele => { + let v = util.versionFromName(ele); + if (v == null) { + console.log(`invalid version name ${ele}`); + return; + } + + console.log(`versionFrom(${ele}): ${v.toString()}, id=${v.id}, name=${v.name}`); + }); +}); + +test("test version ip compare", () => { + let ip_list = [ + ["1.0.0.0", "0.0.1.2", 1], + ["192.168.1.101", "192.168.1.90", 1], + ["219.133.111.87", "114.114.114.114", 1], + ["2000::", "2000:ffff:ffff:ffff:ffff:ffff:ffff:ffff", -1], + ["2001:4:112::", "2001:4:112:ffff:ffff:ffff:ffff:ffff", -1], + ["ffff::", "2001:4:ffff:ffff:ffff:ffff:ffff:ffff", 1] + ]; + + ip_list.forEach(ips => { + const ip1 = util.parseIP(ips[0]); + const ip2 = util.parseIP(ips[1]); + if (ip1.length != ip2.length) { + fail(`ip1 and ip2 are not the same ip type`); + } + + const version = ip1.length == 4 ? util.IPv4 : util.IPv6; + const cmp = version.ipSubCompare(ip1, ip1.length == 4 ? ip2.reverse() : ip2, 0); + expect(cmp).toBe(ips[2]); + console.log(`compare(${ips[0]}, ${ips[1]}): ${cmp}`); + }); +}); + + + +test('parse ip address', () => { + let ip_list = [ + "1.0.0.0", "58.251.30.115", "192.168.1.100", "126.255.32.255", "219.xx.xx.11", + "::", "::1", "fffe::", "2c0f:fff0::", "2c0f:fff0::1", "2a02:26f7:c409:4001::", + "2fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", "240e:982:e617:ffff:ffff:ffff:ffff:ffff", "::xx:ffff" + ]; + + ip_list.forEach(ipString => { + let ipBytes = null; + try { + ipBytes = util.parseIP(ipString); + } catch (e) { + console.log(`failed to parse ip '${ipString}': ${e.message}`); + return; + } + + let to_Str = util.ipToString(ipBytes, true); + let toByte = util.ipBytesString(ipBytes); + console.log(`parseIP(${ipString}): {Bytes: ${toByte}, String: ${to_Str}}`); + expect(ipString).toBe(to_Str); + }); +}); + +test('ip compare', () => { + let ip_list = [ + ["1.0.0.0", "1.0.0.1", -1], + ["192.168.1.101", "192.168.1.90", 1], + ["219.133.111.87", "114.114.114.114", 1], + ["2000::", "2000:ffff:ffff:ffff:ffff:ffff:ffff:ffff", -1], + ["2001:4:112::", "2001:4:112:ffff:ffff:ffff:ffff:ffff", -1], + ["ffff::", "2001:4:ffff:ffff:ffff:ffff:ffff:ffff", 1] + ]; + + ip_list.forEach(ips => { + const ip1 = util.parseIP(ips[0]); + const ip2 = util.parseIP(ips[1]); + const cmp = util.ipCompare(ip1, ip2); + expect(cmp).toBe(ips[2]); + console.log(`compare(${ips[0]}, ${ips[1]}): ${cmp}`); + }); +}); + +test('test load header', () => { + let header = util.loadHeaderFromFile(dbPath); + console.log(`dbPath: ${dbPath}, header: ${header.toString()}}`); +}); + +test('test load vector index', () => { + let vIndex = util.loadVectorIndexFromFile(dbPath); + console.log(`dbPath: ${dbPath}, vIndex: ${vIndex.length}}`); +}); + +test('test load content', () => { + let content = util.loadContentFromFile(dbPath); + console.log(`dbPath: ${dbPath}, content: ${content.length}}`); +}); \ No newline at end of file diff --git a/binding/javascript/tests/version.test.js b/binding/javascript/tests/version.test.js deleted file mode 100644 index 4b7b948..0000000 --- a/binding/javascript/tests/version.test.js +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright 2022 The Ip2Region Authors. All rights reserved. -// Use of this source code is governed by a Apache2.0-style -// license that can be found in the LICENSE file. - -// version test script -// @Author Lion - -import * as util from '../util.js'; - -test('const print', () => { - console.log("IPv4: ", util.IPv4.toString()); - console.log("IPv6: ", util.IPv6.toString()); -}); - -test("test version from name", () => { - let vs = ["v4", "ipv4", "v4x", "v6", "ipv6", "v6x"]; - vs.forEach(ele => { - let v = util.versionFromName(ele); - if (v == null) { - console.log(`invalid version name ${ele}`); - return; - } - - console.log(`versionFrom(${ele})`, v.toString()); - console.log(`version.id=${v.id}, version.name=${v.name}`); - }); -}); - -test("test version ip compare", () => { - let ip_list = [ - ["1.0.0.0", "0.0.1.2", 1], - ["192.168.1.101", "192.168.1.90", 1], - ["219.133.111.87", "114.114.114.114", 1], - ["2000::", "2000:ffff:ffff:ffff:ffff:ffff:ffff:ffff", -1], - ["2001:4:112::", "2001:4:112:ffff:ffff:ffff:ffff:ffff", -1], - ["ffff::", "2001:4:ffff:ffff:ffff:ffff:ffff:ffff", 1] - ]; - - ip_list.forEach(ips => { - const ip1 = util.parseIP(ips[0]); - const ip2 = util.parseIP(ips[1]); - if (ip1.length != ip2.length) { - fail(`ip1 and ip2 are not the same ip type`); - } - - const version = ip1.length == 4 ? util.IPv4 : util.IPv6; - const cmp = version.ipSubCompare(ip1, ip1.length == 4 ? ip2.reverse() : ip2, 0); - expect(cmp).toBe(ips[2]); - console.log(`compare(${ips[0]}, ${ips[1]}): ${cmp}`); - }); -});