]> git.ipfire.org Git - location/libloc.git/blob - tests/lua/main.lua
importer: Update a few AWS locations
[location/libloc.git] / tests / lua / main.lua
1 #!/usr/bin/lua
2 --[[###########################################################################
3 # #
4 # libloc - A library to determine the location of someone on the Internet #
5 # #
6 # Copyright (C) 2024 IPFire Development Team <info@ipfire.org> #
7 # #
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. #
12 # #
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. #
17 # #
18 ############################################################################--]]
19
20 luaunit = require("luaunit")
21
22 ENV_TEST_DATABASE = os.getenv("TEST_DATABASE")
23 ENV_TEST_SIGNING_KEY = os.getenv("TEST_SIGNING_KEY")
24
25 function test_load()
26 -- Try loading the module
27 location = require("location")
28
29 -- Print the version
30 print(location.version())
31 end
32
33 function test_open_database()
34 location = require("location")
35
36 -- Open the database
37 db = location.Database.open(ENV_TEST_DATABASE)
38
39 -- Verify
40 luaunit.assertIsTrue(db:verify(ENV_TEST_SIGNING_KEY))
41
42 -- Description
43 luaunit.assertIsString(db:get_description())
44
45 -- License
46 luaunit.assertIsString(db:get_license())
47 luaunit.assertEquals(db:get_license(), "CC BY-SA 4.0")
48
49 -- Vendor
50 luaunit.assertIsString(db:get_vendor())
51 luaunit.assertEquals(db:get_vendor(), "IPFire Project")
52 end
53
54 function test_lookup()
55 location = require("location")
56
57 -- Open the database
58 db = location.Database.open(ENV_TEST_DATABASE)
59
60 -- Perform a lookup
61 network1 = db:lookup("81.3.27.32")
62
63 luaunit.assertEquals(network1:get_family(), 2) -- AF_INET
64 luaunit.assertEquals(network1:get_country_code(), "DE")
65 luaunit.assertEquals(network1:get_asn(), 24679)
66
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))
71 end
72
73 function test_network()
74 location = require("location")
75
76 n1 = location.Network.new("10.0.0.0/8")
77
78 -- The ASN should be nul
79 luaunit.assertNil(n1:get_asn())
80
81 -- The family should be IPv4
82 luaunit.assertEquals(n1:get_family(), 2) -- AF_INET
83
84 -- The country code should be empty
85 luaunit.assertNil(n1:get_country_code())
86 end
87
88 function test_as()
89 location = require("location")
90
91 -- Create a new AS
92 as = location.AS.new(12345)
93 luaunit.assertEquals(as:get_number(), 12345)
94 luaunit.assertNil(as:get_name())
95
96 -- Reset
97 as = nil
98 end
99
100 function test_fetch_as()
101 location = require("location")
102
103 -- Open the database
104 db = location.Database.open(ENV_TEST_DATABASE)
105
106 -- Fetch an AS
107 as = db:get_as(0)
108
109 -- This should not exist
110 luaunit.assertNil(as)
111
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")
116 end
117
118 function test_country()
119 location = require("location")
120
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())
125
126 c2 = location.Country.new("GB")
127 luaunit.assertNotEquals(c1, c2)
128
129 c1 = nil
130 c2 = nil
131 end
132
133 function test_fetch_country()
134 location = require("location")
135
136 -- Open the database
137 db = location.Database.open(ENV_TEST_DATABASE)
138
139 -- Fetch an invalid country
140 c = db:get_country("XX")
141 luaunit.assertNil(c)
142
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")
147 end
148
149 -- This test is not very deterministic but should help to test the GC methods
150 function test_gc()
151 print("GC: " .. collectgarbage("collect"))
152 end
153
154 function test_subnets()
155 location = require("location")
156
157 -- Open the database
158 db = location.Database.open(ENV_TEST_DATABASE)
159
160 local network = db:lookup("1.1.1.1")
161
162 local subnets = network:subnets()
163
164 luaunit.assertIsTable(subnets)
165 luaunit.assertEquals(#subnets, 2)
166
167 for i, subnet in ipairs(subnets) do
168 print(subnet)
169 end
170 end
171
172 function test_list_networks()
173 location = require("location")
174
175 -- Open the database
176 db = location.Database.open(ENV_TEST_DATABASE)
177
178 for network in db:list_networks() do
179 print(network, network:reverse_pointer())
180 end
181 end
182
183 os.exit(luaunit.LuaUnit.run())