Merge pull request #2044 from RincewindsHat/fix/fedora-rpm-build
[monitoring-plugins.git] / plugins / t / check_smtp.t
blob1a1ebe3eb7fd23aa9d71bd31e2182ff3cd797df0
1 #! /usr/bin/perl -w -I ..
3 # Simple Mail Transfer Protocol (SMTP) Test via check_smtp
7 use strict;
8 use Test::More;
9 use NPTest;
11 my $host_tcp_smtp = getTestParameter( "NP_HOST_TCP_SMTP",
12 "A host providing an SMTP Service (a mail server)", "mailhost");
13 my $host_tcp_smtp_starttls = getTestParameter( "NP_HOST_TCP_SMTP_STARTTLS",
14 "A host providing SMTP with STARTTLS", $host_tcp_smtp);
15 my $host_tcp_smtp_nostarttls = getTestParameter( "NP_HOST_TCP_SMTP_NOSTARTTLS",
16 "A host providing SMTP without STARTTLS", "");
17 my $host_tcp_smtp_tls = getTestParameter( "NP_HOST_TCP_SMTP_TLS",
18 "A host providing SMTP with TLS", $host_tcp_smtp);
20 my $host_nonresponsive = getTestParameter( "NP_HOST_NONRESPONSIVE",
21 "The hostname of system not responsive to network requests", "10.0.0.1" );
23 my $hostname_invalid = getTestParameter( "NP_HOSTNAME_INVALID",
24 "An invalid (not known to DNS) hostname", "nosuchhost" );
25 my $res;
27 plan tests => 16;
29 SKIP: {
30 skip "No SMTP server defined", 4 unless $host_tcp_smtp;
31 $res = NPTest->testCmd( "./check_smtp $host_tcp_smtp" );
32 is ($res->return_code, 0, "OK");
34 $res = NPTest->testCmd( "./check_smtp -H $host_tcp_smtp -p 25 -w 9 -c 9 -t 10 -e 220" );
35 is ($res->return_code, 0, "OK, within 9 second response");
37 $res = NPTest->testCmd( "./check_smtp -H $host_tcp_smtp -p 25 -wt 9 -ct 9 -to 10 -e 220" );
38 is ($res->return_code, 0, "OK, old syntax");
40 $res = NPTest->testCmd( "./check_smtp -H $host_tcp_smtp -e 221" );
41 is ($res->return_code, 1, "WARNING - got correct error when expecting 221 instead of 220" );
43 TODO: {
44 local $TODO = "Output is over two lines";
45 like ( $res->output, qr/^SMTP WARNING/, "Correct error message" );
48 $res = NPTest->testCmd( "./check_smtp -H $host_tcp_smtp --ssl -p 25" );
49 is ($res->return_code, 2, "Check rc of connecting to $host_tcp_smtp with TLS on standard SMTP port" );
50 like ($res->output, qr/^CRITICAL - Cannot make SSL connection\./, "Check output of connecting to $host_tcp_smtp with TLS on standard SMTP port");
53 SKIP: {
54 skip "No SMTP server with STARTTLS defined", 1 unless $host_tcp_smtp_starttls;
55 # SSL connection for STARTTLS
56 $res = NPTest->testCmd( "./check_smtp -H $host_tcp_smtp_starttls -p 25 -S" );
57 is ($res->return_code, 0, "OK, with STARTTLS" );
60 SKIP: {
61 skip "No SMTP server without STARTTLS defined", 2 unless $host_tcp_smtp_nostarttls;
62 $res = NPTest->testCmd( "./check_smtp -H $host_tcp_smtp_nostarttls -p 25 -S" );
63 is ($res->return_code, 1, "OK, got warning from server without STARTTLS");
64 is ($res->output, "WARNING - TLS not supported by server", "Right error message" );
67 SKIP: {
68 skip "No SMTP server with TLS defined", 1 unless $host_tcp_smtp_tls;
69 $res = NPTest->testCmd( "./check_smtp -H $host_tcp_smtp_tls --ssl" );
70 is ($res->return_code, 0, "Check rc of connecting to $host_tcp_smtp_tls with TLS" );
71 like ($res->output, qr/^SMTP OK - /, "Check output of connecting to $host_tcp_smtp_tls with TLS" );
73 my $unused_port = 4465;
74 $res = NPTest->testCmd( "./check_smtp -H $host_tcp_smtp_tls -p $unused_port --ssl" );
75 is ($res->return_code, 2, "Check rc of connecting to $host_tcp_smtp_tls with TLS on unused port $unused_port" );
76 like ($res->output, qr/^connect to address $host_tcp_smtp_tls and port $unused_port: Connection refused/, "Check output of connecting to $host_tcp_smtp_tls with TLS on unused port $unused_port");
79 $res = NPTest->testCmd( "./check_smtp $host_nonresponsive" );
80 is ($res->return_code, 2, "CRITICAL - host non responding" );
82 $res = NPTest->testCmd( "./check_smtp $hostname_invalid" );
83 is ($res->return_code, 3, "UNKNOWN - hostname invalid" );