]>
Commit | Line | Data |
---|---|---|
6ae775f9 MT |
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 | ||
7eaabd10 MT |
22 | ENV_TEST_DATABASE = os.getenv("TEST_DATABASE") |
23 | ||
6ae775f9 MT |
24 | function test_load() |
25 | -- Try loading the module | |
26 | location = require("location") | |
03b9c3a4 MT |
27 | |
28 | -- Print the version | |
29 | print(location.version()) | |
6ae775f9 MT |
30 | end |
31 | ||
7eaabd10 MT |
32 | function test_open_database() |
33 | location = require("location") | |
34 | ||
35 | -- Open the database | |
36 | db = location.Database.open(ENV_TEST_DATABASE) | |
ba81f732 MT |
37 | |
38 | -- Description | |
39 | luaunit.assertIsString(db:get_description()) | |
40 | ||
41 | -- License | |
42 | luaunit.assertIsString(db:get_license()) | |
43 | luaunit.assertEquals(db:get_license(), "CC BY-SA 4.0") | |
44 | ||
45 | -- Vendor | |
46 | luaunit.assertIsString(db:get_vendor()) | |
47 | luaunit.assertEquals(db:get_vendor(), "IPFire Project") | |
7eaabd10 MT |
48 | end |
49 | ||
50 | function test_lookup() | |
51 | location = require("location") | |
52 | ||
53 | -- Open the database | |
54 | db = location.Database.open(ENV_TEST_DATABASE) | |
55 | ||
56 | -- Perform a lookup | |
2ec52cc1 MT |
57 | network = db:lookup("81.3.27.32") |
58 | ||
59 | luaunit.assertEquals(network:get_family(), 2) -- AF_INET | |
60 | luaunit.assertEquals(network:get_country_code(), "DE") | |
61 | luaunit.assertEquals(network:get_asn(), 24679) | |
7eaabd10 MT |
62 | end |
63 | ||
e914e3ca MT |
64 | function test_network() |
65 | location = require("location") | |
66 | ||
67 | n1 = location.Network.new("10.0.0.0/8") | |
68 | ||
69 | -- The ASN should be nul | |
b49c59d2 | 70 | luaunit.assertNil(n1:get_asn()) |
e914e3ca MT |
71 | |
72 | -- The family should be IPv4 | |
2ec52cc1 | 73 | luaunit.assertEquals(n1:get_family(), 2) -- AF_INET |
e914e3ca MT |
74 | |
75 | -- The country code should be empty | |
b49c59d2 | 76 | luaunit.assertNil(n1:get_country_code()) |
e914e3ca MT |
77 | end |
78 | ||
67675dc3 MT |
79 | function test_as() |
80 | location = require("location") | |
81 | ||
82 | -- Create a new AS | |
83 | as = location.AS.new(12345) | |
84 | luaunit.assertEquals(as:get_number(), 12345) | |
85 | luaunit.assertNil(as:get_name()) | |
86 | ||
87 | -- Reset | |
88 | as = nil | |
89 | end | |
90 | ||
77ee9330 MT |
91 | function test_fetch_as() |
92 | location = require("location") | |
93 | ||
94 | -- Open the database | |
95 | db = location.Database.open(ENV_TEST_DATABASE) | |
96 | ||
97 | -- Fetch an AS | |
98 | as = db:get_as(0) | |
99 | ||
100 | -- This should not exist | |
101 | luaunit.assertNil(as) | |
102 | ||
103 | -- Fetch something that exists | |
104 | as = db:get_as(204867) | |
105 | luaunit.assertEquals(as:get_number(), 204867) | |
106 | luaunit.assertEquals(as:get_name(), "Lightning Wire Labs GmbH") | |
107 | end | |
108 | ||
0ddfebb3 MT |
109 | function test_country() |
110 | location = require("location") | |
111 | ||
112 | c1 = location.Country.new("DE") | |
113 | luaunit.assertEquals(c1:get_code(), "DE") | |
b362c2a7 MT |
114 | luaunit.assertNil(c1:get_name()) |
115 | luaunit.assertNil(c1:get_continent_code()) | |
0ddfebb3 MT |
116 | |
117 | c2 = location.Country.new("GB") | |
118 | luaunit.assertNotEquals(c1, c2) | |
d77d3c8d MT |
119 | |
120 | c1 = nil | |
121 | c2 = nil | |
0ddfebb3 MT |
122 | end |
123 | ||
49463375 MT |
124 | function test_fetch_country() |
125 | location = require("location") | |
126 | ||
127 | -- Open the database | |
128 | db = location.Database.open(ENV_TEST_DATABASE) | |
129 | ||
130 | -- Fetch an invalid country | |
131 | c = db:get_country("XX") | |
132 | luaunit.assertNil(c) | |
133 | ||
134 | -- Fetch something that exists | |
135 | c = db:get_country("DE") | |
136 | luaunit.assertEquals(c:get_code(), "DE") | |
137 | luaunit.assertEquals(c:get_name(), "Germany") | |
138 | end | |
139 | ||
9ff91ee2 MT |
140 | -- This test is not very deterministic but should help to test the GC methods |
141 | function test_gc() | |
142 | print("GC: " .. collectgarbage("collect")) | |
143 | end | |
144 | ||
6ae775f9 | 145 | os.exit(luaunit.LuaUnit.run()) |