2 --[[###########################################################################
4 # libloc - A library to determine the location of someone on the Internet #
6 # Copyright (C) 2024 IPFire Development Team <info@ipfire.org> #
8 # This library is free software; you can redistribute it and/or #
9 # modify it under the terms of the GNU Lesser General Public #
10 # License as published by the Free Software Foundation; either #
11 # version 2.1 of the License, or (at your option) any later version. #
13 # This library is distributed in the hope that it will be useful, #
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
16 # Lesser General Public License for more details. #
18 ############################################################################--]]
20 luaunit = require("luaunit")
22 ENV_TEST_DATABASE = os.getenv("TEST_DATABASE")
23 ENV_TEST_SIGNING_KEY = os.getenv("TEST_SIGNING_KEY")
26 -- Try loading the module
27 location = require("location")
30 print(location.version())
33 function test_open_database()
34 location = require("location")
37 db = location.Database.open(ENV_TEST_DATABASE)
40 luaunit.assertIsTrue(db:verify(ENV_TEST_SIGNING_KEY))
43 luaunit.assertIsString(db:get_description())
46 luaunit.assertIsString(db:get_license())
47 luaunit.assertEquals(db:get_license(), "CC BY-SA 4.0")
50 luaunit.assertIsString(db:get_vendor())
51 luaunit.assertEquals(db:get_vendor(), "IPFire Project")
54 function test_lookup()
55 location = require("location")
58 db = location.Database.open(ENV_TEST_DATABASE)
61 network1 = db:lookup("81.3.27.32")
63 luaunit.assertEquals(network1:get_family(), 2) -- AF_INET
64 luaunit.assertEquals(network1:get_country_code(), "DE")
65 luaunit.assertEquals(network1:get_asn(), 24679)
67 -- Lookup something else
68 network2 = db:lookup("8.8.8.8")
69 luaunit.assertIsTrue(network2:has_flag(location.NETWORK_FLAG_ANYCAST))
70 luaunit.assertIsFalse(network2:has_flag(location.NETWORK_FLAG_DROP))
73 function test_network()
74 location = require("location")
76 n1 = location.Network.new("10.0.0.0/8")
78 -- The ASN should be nul
79 luaunit.assertNil(n1:get_asn())
81 -- The family should be IPv4
82 luaunit.assertEquals(n1:get_family(), 2) -- AF_INET
84 -- The country code should be empty
85 luaunit.assertNil(n1:get_country_code())
89 location = require("location")
92 as = location.AS.new(12345)
93 luaunit.assertEquals(as:get_number(), 12345)
94 luaunit.assertNil(as:get_name())
100 function test_fetch_as()
101 location = require("location")
104 db = location.Database.open(ENV_TEST_DATABASE)
109 -- This should not exist
110 luaunit.assertNil(as)
112 -- Fetch something that exists
113 as = db:get_as(204867)
114 luaunit.assertEquals(as:get_number(), 204867)
115 luaunit.assertEquals(as:get_name(), "Lightning Wire Labs GmbH")
118 function test_country()
119 location = require("location")
121 c1 = location.Country.new("DE")
122 luaunit.assertEquals(c1:get_code(), "DE")
123 luaunit.assertNil(c1:get_name())
124 luaunit.assertNil(c1:get_continent_code())
126 c2 = location.Country.new("GB")
127 luaunit.assertNotEquals(c1, c2)
133 function test_fetch_country()
134 location = require("location")
137 db = location.Database.open(ENV_TEST_DATABASE)
139 -- Fetch an invalid country
140 c = db:get_country("XX")
143 -- Fetch something that exists
144 c = db:get_country("DE")
145 luaunit.assertEquals(c:get_code(), "DE")
146 luaunit.assertEquals(c:get_name(), "Germany")
149 -- This test is not very deterministic but should help to test the GC methods
151 print("GC: " .. collectgarbage("collect"))
154 function test_subnets()
155 location = require("location")
158 db = location.Database.open(ENV_TEST_DATABASE)
160 local network = db:lookup("1.1.1.1")
162 local subnets = network:subnets()
164 luaunit.assertIsTable(subnets)
165 luaunit.assertEquals(#subnets, 2)
167 for i, subnet in ipairs(subnets) do
172 function test_list_networks()
173 location = require("location")
176 db = location.Database.open(ENV_TEST_DATABASE)
178 for network in db:list_networks() do
179 print(network, network:reverse_pointer())
183 os.exit(luaunit.LuaUnit.run())