File: main.lua.in

package info (click to toggle)
libloc 0.9.18-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 42,824 kB
  • sloc: ansic: 7,602; python: 2,893; makefile: 511; sh: 39; perl: 13
file content (216 lines) | stat: -rwxr-xr-x 5,757 bytes parent folder | download
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/lua@LUA_VERSION@
--[[###########################################################################
#                                                                             #
# libloc - A library to determine the location of someone on the Internet     #
#                                                                             #
# Copyright (C) 2024 IPFire Development Team <info@ipfire.org>                #
#                                                                             #
# This library is free software; you can redistribute it and/or               #
# modify it under the terms of the GNU Lesser General Public                  #
# License as published by the Free Software Foundation; either                #
# version 2.1 of the License, or (at your option) any later version.          #
#                                                                             #
# This library is distributed in the hope that it will be useful,             #
# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU           #
# Lesser General Public License for more details.                             #
#                                                                             #
############################################################################--]]

luaunit = require("luaunit")

ENV_TEST_DATABASE    = os.getenv("TEST_DATABASE")
ENV_TEST_SIGNING_KEY = os.getenv("TEST_SIGNING_KEY")

function test_load()
	-- Try loading the module
	location = require("location")

	-- Print the version
	print(location.version())
end

log_callback_called = 0

function log_callback(level, message)
	log_callback_called = true
	print("LOG " .. message)
end

function test_log_callback()
	location = require("location")

	-- Set the callback
	location.set_log_callback(log_callback)

	-- Enable debugging
	location.set_log_level(7)

	-- Perform some random operation
	local db = location.Database.open(ENV_TEST_DATABASE)
	luaunit.assertNotNil(db)

	luaunit.assertIsTrue(log_callback_called)
end

function test_open_database()
	location = require("location")

	-- Open the database
	db = location.Database.open(ENV_TEST_DATABASE)
	luaunit.assertNotNil(db)

	-- Verify
	luaunit.assertIsTrue(db:verify(ENV_TEST_SIGNING_KEY))

	-- Description
	luaunit.assertIsString(db:get_description())

	-- License
	luaunit.assertIsString(db:get_license())
	luaunit.assertEquals(db:get_license(), "CC BY-SA 4.0")

	-- Vendor
	luaunit.assertIsString(db:get_vendor())
	luaunit.assertEquals(db:get_vendor(), "IPFire Project")
end

function test_lookup()
	location = require("location")

	-- Open the database
	db = location.Database.open(ENV_TEST_DATABASE)
	luaunit.assertNotNil(db)

	-- Perform a lookup
	network1 = db:lookup("81.3.27.32")

	luaunit.assertEquals(network1:get_family(), 2) -- AF_INET
	luaunit.assertEquals(network1:get_country_code(), "DE")
	luaunit.assertEquals(network1:get_asn(), 24679)

	-- Lookup something else
	network2 = db:lookup("8.8.8.8")
	luaunit.assertIsTrue(network2:has_flag(location.NETWORK_FLAG_ANYCAST))
	luaunit.assertIsFalse(network2:has_flag(location.NETWORK_FLAG_DROP))
end

function test_network()
	location = require("location")

	n1 = location.Network.new("10.0.0.0/8")
	luaunit.assertNotNil(n1)

	-- The ASN should be nul
	luaunit.assertNil(n1:get_asn())

	-- The family should be IPv4
	luaunit.assertEquals(n1:get_family(), 2) -- AF_INET

	-- The country code should be empty
	luaunit.assertNil(n1:get_country_code())
end

function test_as()
	location = require("location")
	luaunit.assertNotNil(location)

	-- Create a new AS
	as = location.AS.new(12345)
	luaunit.assertEquals(as:get_number(), 12345)
	luaunit.assertNil(as:get_name())

	-- Reset
	as = nil
end

function test_fetch_as()
	location = require("location")

	-- Open the database
	db = location.Database.open(ENV_TEST_DATABASE)
	luaunit.assertNotNil(db)

	-- Fetch an AS
	as = db:get_as(0)

	-- This should not exist
	luaunit.assertNil(as)

	-- Fetch something that exists
	as = db:get_as(204867)
	luaunit.assertEquals(as:get_number(), 204867)
	luaunit.assertEquals(as:get_name(), "Lightning Wire Labs GmbH")
end

function test_country()
	location = require("location")

	c1 = location.Country.new("DE")
	luaunit.assertNotNil(c1)
	luaunit.assertEquals(c1:get_code(), "DE")
	luaunit.assertNil(c1:get_name())
	luaunit.assertNil(c1:get_continent_code())

	c2 = location.Country.new("GB")
	luaunit.assertNotNil(c2)
	luaunit.assertNotEquals(c1, c2)

	c1 = nil
	c2 = nil
end

function test_fetch_country()
	location = require("location")

	-- Open the database
	db = location.Database.open(ENV_TEST_DATABASE)
	luaunit.assertNotNil(db)

	-- Fetch an invalid country
	c = db:get_country("XX")
	luaunit.assertNil(c)

	-- Fetch something that exists
	c = db:get_country("DE")
	luaunit.assertEquals(c:get_code(), "DE")
	luaunit.assertEquals(c:get_name(), "Germany")
end

-- This test is not very deterministic but should help to test the GC methods
function test_gc()
	print("GC: " .. collectgarbage("collect"))
end

function test_subnets()
	location = require("location")

	-- Open the database
	db = location.Database.open(ENV_TEST_DATABASE)
	luaunit.assertNotNil(db)

	local network = db:lookup("1.1.1.1")

	local subnets = network:subnets()

	luaunit.assertIsTable(subnets)
	luaunit.assertEquals(#subnets, 2)

	for i, subnet in ipairs(subnets) do
		print(subnet)
	end
end

function test_list_networks()
	location = require("location")

	-- Open the database
	db = location.Database.open(ENV_TEST_DATABASE)
	luaunit.assertNotNil(db)

	for network in db:list_networks() do
		print(network, network:reverse_pointer())
	end
end

os.exit(luaunit.LuaUnit.run())