]> git.ipfire.org Git - people/ms/libloc.git/blob - src/perl/t/Location.t
perl: Add test for get_continent_code() function.
[people/ms/libloc.git] / src / perl / t / Location.t
1 # Before 'make install' is performed this script should be runnable with
2 # 'make test'. After 'make install' it should work as 'perl Location.t'
3
4 #########################
5
6 # change 'tests => 1' to 'tests => last_test_to_print';
7
8 use strict;
9 use warnings;
10
11 # Where to find the test database.
12 my $testdb = $ENV{'database'};
13 my $keyfile = $ENV{'keyfile'};
14
15 use Test::More tests => 12;
16 BEGIN { use_ok('Location') };
17
18 #########################
19
20 # Insert your test code below, the Test::More module is use()ed here so read
21 # its man page ( perldoc Test::More ) for help writing this test script.
22
23 # Address which should be used for database lookup.
24 my $address = "2a07:1c44:5800::1";
25
26 # Connect to the database.
27 my $db = &Location::init("$testdb");
28
29 # Verify
30 my $status = &Location::verify($db, $keyfile);
31 ok($status, "This database is valid");
32
33 my $vendor = &Location::get_vendor($db);
34 ok($vendor eq "IPFire Project", "Test 1 - Get Database Vendor");
35
36 my $license = &Location::get_license($db);
37 ok($license eq "CC", "Test 2 - Get Database license");
38
39 my $description = &Location::get_description($db);
40 ok($description eq "This is a geo location database", "Test 3 - Get Database Description");
41
42 my $country_code = &Location::lookup_country_code($db, $address);
43 ok($country_code eq "DE", "Test 4 - Lookup country code for $address");
44
45 $country_code = &Location::lookup_country_code($db, "1.1.1.1");
46 if(defined($country_code)) { fail("Test 5 - Lookup country code for address not in Database."); }
47
48 $country_code = &Location::lookup_country_code($db, "a.b.c.d");
49 if(defined($country_code)) { fail("Test 6 - Lookup country code for invalid address.") }
50
51 my $as_number = &Location::lookup_asn($db, $address);
52 ok($as_number eq "204867", "Test 7 - Lookup Autonomous System Number for $address.");
53
54 $as_number = &Location::lookup_asn($db, "1.1.1.1");
55 if(defined($as_number)) { fail("Test 8 - Lookup Autonomous System Number for address not in Database.") }
56
57 $as_number = &Location::lookup_asn($db, "a.b.c.d");
58 if(defined($as_number)) { fail("Test 9 - Lookup Autonomous System Number for invalid address.") }
59
60 my $as_name = &Location::get_as_name($db, "204867");
61 ok($as_name eq "Lightning Wire Labs GmbH", "Test 10 - Get name for AS204867.");
62
63 my @locations = &Location::database_countries($db);
64 ok(@locations != 0, "Test 11 - Get database countries.");
65
66 my $network_flag_anycast = &Location::lookup_network_has_flag($db, $address, "LOC_NETWORK_FLAG_ANYCAST");
67 ok($network_flag_anycast, "Network has Anycast flag.");
68
69 my $country_name = &Location::get_country_name($db, "DE");
70 ok($country_name eq "Germany", "Test 13 - Got country name: $country_name");
71
72 my $continent_code = &Location::get_continent_code($db, "DE");
73 ok($continent_code eq "EU", "Test 14 - Got continent code $continent_code for country code 'DE'");