summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--plugins-scripts/t/utils.t21
-rw-r--r--plugins-scripts/utils.pm.in11
2 files changed, 27 insertions, 5 deletions
diff --git a/plugins-scripts/t/utils.t b/plugins-scripts/t/utils.t
index 469988c9..4df5606b 100644
--- a/plugins-scripts/t/utils.t
+++ b/plugins-scripts/t/utils.t
@@ -4,8 +4,10 @@
4# 4#
5# $Id$ 5# $Id$
6# 6#
7# Run with perl t/utils.t
7 8
8#use strict; 9use warnings;
10use strict;
9use Test::More; 11use Test::More;
10use NPTest; 12use NPTest;
11 13
@@ -19,16 +21,29 @@ my $hostname_checks = {
19 "host-hyphened.com" => 1, 21 "host-hyphened.com" => 1,
20 "rubbish" => 1, 22 "rubbish" => 1,
21 "-start.com" => 0, 23 "-start.com" => 0,
22 "endsindot." => 0, 24 "nonfqdn-but-endsindot." => 1,
25 "fqdn.and.endsindot." => 1,
23 "lots.of.dots.dot.org" => 1, 26 "lots.of.dots.dot.org" => 1,
27 "endingwithdoubledots.." => 0,
28 "toomany..dots" => 0,
29 ".start.with.dot" => 0,
24 "10.20.30.40" => 1, 30 "10.20.30.40" => 1,
25 "10.20.30.40.50" => 0, 31 "10.20.30.40.50" => 0,
26 "10.20.30" => 0, 32 "10.20.30" => 0,
33 "10.20.30.40." => 1, # This is considered a hostname because of trailing dot. It probably won't exist though...
34 "888." => 1, # This is because it could be a domain
35 "host.888." => 1,
36 "where.did.that.!.come.from." => 0,
37 "no.underscores_.com" => 0,
27 }; 38 };
28 39
29plan tests => scalar keys %$hostname_checks; 40plan tests => ((scalar keys %$hostname_checks) + 4);
30 41
31foreach my $h (sort keys %$hostname_checks) { 42foreach my $h (sort keys %$hostname_checks) {
32 is (utils::is_hostname($h), $hostname_checks->{$h}, "$h should return ".$hostname_checks->{$h}); 43 is (utils::is_hostname($h), $hostname_checks->{$h}, "$h should return ".$hostname_checks->{$h});
33} 44}
34 45
46is(utils::is_hostname(), 0, "No parameter errors");
47is(utils::is_hostname(""), 0, "Empty string errors");
48is(utils::is_hostname(0), 0, "0 also errors");
49is(utils::is_hostname(1), 0, "1 also errors");
diff --git a/plugins-scripts/utils.pm.in b/plugins-scripts/utils.pm.in
index e2458359..8449b544 100644
--- a/plugins-scripts/utils.pm.in
+++ b/plugins-scripts/utils.pm.in
@@ -53,9 +53,16 @@ sub usage {
53 53
54sub is_hostname { 54sub is_hostname {
55 my $host1 = shift; 55 my $host1 = shift;
56 if ($host1 && $host1 =~ m/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+|[a-zA-Z0-9][-a-zA-Z0-9]+(\.[a-zA-Z0-9][-a-zA-Z0-9]+)*)$/) { 56 return 0 unless defined $host1;
57 if ($host1 =~ m/^[\d\.]+$/ && $host1 !~ /\.$/) {
58 if ($host1 =~ m/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/) {
59 return 1;
60 } else {
61 return 0;
62 }
63 } elsif ($host1 =~ m/^[a-zA-Z0-9][-a-zA-Z0-9]+(\.[a-zA-Z0-9][-a-zA-Z0-9]+)*\.?$/) {
57 return 1; 64 return 1;
58 }else{ 65 } else {
59 return 0; 66 return 0;
60 } 67 }
61} 68}