#!/usr/bin/perl
my
$SeriaPort
=
''
;
my
$TestMultiple
= 1;
my
$ConnectionType
;
my
$Timeout
= 10;
my
$ConnectionTimeout
= 15;
my
$ErrorMode
=
'return'
;
my
$InputLog
;
my
$OutputLog
;
my
$DumpLog
;
my
$Host
;
my
$TcpPort
;
my
$Username
;
my
$Password
;
my
$PublicKeyPath
;
my
$PrivateKeyPath
;
my
$Passphrase
;
my
$Baudrate
;
my
$Databits
= 8;
my
$Parity
=
'none'
;
my
$Stopbits
= 1;
my
$Handshake
=
'none'
;
my
$PromptCredentials
= 1;
my
$Debug
= 0;
if
( !
$SeriaPort
&&
eval
{
require
DefaultPort } &&
$DefaultPort::Serial_Test_Port
) {
$SeriaPort
=
$DefaultPort::Serial_Test_Port
;
}
sub
prompt {
my
$varRef
=
shift
;
my
$message
=
shift
;
my
$default
=
shift
;
my
$userInput
;
return
if
defined
$$varRef
;
print
"\n"
,
$message
;
chomp
(
$$varRef
= <STDIN>);
print
"\n"
;
unless
(
length
$$varRef
) {
if
(
defined
$default
) {
$$varRef
=
$default
;
return
;
}
done_testing();
exit
;
}
}
BEGIN {
use_ok(
'Control::CLI'
) ||
die
"Bail out!"
;
}
my
$modules
= ((Control::CLI::useTelnet) ?
"Net::Telnet $Net::Telnet::VERSION, "
:
''
).
((Control::CLI::useSsh) ?
"Net::SSH2 $Net::SSH2::VERSION, "
:
''
).
((Control::CLI::useSerial) ? ($^O eq
'MSWin32'
?
"Win32::SerialPort $Win32::SerialPort::VERSION, "
:
"Device::SerialPort $Device::SerialPort::VERSION, "
):
''
);
chop
$modules
;
chop
$modules
;
diag
"Testing Control::CLI $Control::CLI::VERSION"
;
diag
"Available modules to test with: $modules"
;
if
(Control::CLI::useTelnet || Control::CLI::useSsh) {
if
(Control::CLI::useIPv6) {
diag
"Using IO::Socket::IP ==> IPv4 and IPv6 support"
;
}
else
{
diag
"Using IO::Socket::INET ==> IPv4 only (install IO::Socket::IP for IPv6 support)"
;
}
}
unless
(IO::Interactive::is_interactive) {
my
(
$cli
,
$testcli
,
$serialPortUndetected
);
SKIP: {
skip
"Net::Telnet not installed, skipping Telnet constructor test"
, 1
unless
Control::CLI::useTelnet;
$testcli
= new Control::CLI(
Use
=>
'TELNET'
,
Errmode
=>
'return'
);
ok(
defined
$testcli
,
"Testing constructor for Telnet"
);
$cli
=
$testcli
if
defined
$testcli
;
}
SKIP: {
skip
"Net::SSH not installed, skipping SSH constructor test"
, 1
unless
Control::CLI::useSsh;
$testcli
= new Control::CLI(
Use
=>
'SSH'
,
Errmode
=>
'return'
);
ok(
defined
$testcli
,
"Testing constructor for SSH"
);
$cli
=
$testcli
if
defined
$testcli
;
}
SKIP: {
skip
"Win32::SerialPort not installed, skipping Serial constructor test"
, 1
unless
Control::CLI::useSerial;
unless
(
$SeriaPort
) {
if
($^O eq
'MSWin32'
) {
$serialPortUndetected
= 1;
skip
"Cannot make out available serial ports for Serial constructor test"
, 1;
}
import
Win32::TieRegistry;
$Win32::TieRegistry::Registry
->Delimiter(
"/"
);
my
$comports
=
$Win32::TieRegistry::Registry
->{
"HKEY_LOCAL_MACHINE/HARDWARE/DEVICEMAP/SERIALCOMM"
};
unless
(
defined
$comports
) {
$serialPortUndetected
= 1;
skip
"Cannot make out available serial ports for Serial constructor test"
, 1;
}
foreach
(
keys
%$comports
) {
$SeriaPort
=
$comports
->{
$_
}
if
$comports
->{
$_
} =~ /^COM\d$/;
last
;
}
}
else
{
my
@devttys
=
glob
'/dev/ttyS?'
;
if
(
@devttys
&&
eval
{
require
POSIX}) {
foreach
my
$port
(
@devttys
) {
if
(
$port
=~ /^(\/dev\/ttyS\d)$/) {
my
$tryport
= $1;
my
$fd
= POSIX::
open
(
$tryport
,
&POSIX::O_RDWR
|
&POSIX::O_NOCTTY
|
&POSIX::O_NONBLOCK
);
my
$to
= POSIX::Termios->new();
if
(
$to
&&
$fd
&&
$to
->getattr(
$fd
) ) {
$SeriaPort
=
$tryport
;
last
;
}
}
}
}
unless
(
$SeriaPort
) {
$serialPortUndetected
= 1;
skip
"Cannot make out available serial ports for Serial constructor test"
, 1;
}
}
diag
"Serial Port detected for testing Serial constructor with: $SeriaPort"
;
}
$testcli
= new Control::CLI(
Use
=>
$SeriaPort
,
Errmode
=>
'return'
);
ok(
defined
$testcli
,
"Testing constructor for Serial Port (using $SeriaPort)"
);
$cli
=
$testcli
if
defined
$testcli
;
}
if
(
$serialPortUndetected
) {
diag
"Skipped serial port constructor test as no serial port detected"
;
diag
"- can manually set one with 'perl <Build.PL|Makefile.PL> TESTPORT=<DEVICE>'"
;
}
ok(
defined
$cli
,
"Testing constructor for either Telnet/SSH/Serial"
);
isa_ok(
$cli
,
'Control::CLI'
);
diag
"Once installed, to test connection to a device, please run test script control-cli.t manually and follow interactive prompts"
;
done_testing();
exit
;
}
do
{{
my
(
$cli
,
$eof
,
$returnValue
,
$cmd
);
my
(
$connectionType
,
$username
,
$password
,
$host
,
$tcpPort
,
$baudrate
)
= (
$ConnectionType
,
$Username
,
$Password
,
$Host
,
$TcpPort
,
$Baudrate
);
prompt(\
$connectionType
,
"Select connection type to test\n [enter string: telnet|ssh|<COM-port-name>; or just ENTER to end test]\n : "
);
$cli
= new Control::CLI(
Use
=>
$connectionType
,
Timeout
=>
$Timeout
,
Connection_timeout
=>
$ConnectionTimeout
,
Errmode
=>
$ErrorMode
,
Input_log
=>
$InputLog
,
Output_log
=>
$OutputLog
,
Dump_log
=>
$DumpLog
,
Debug
=>
$Debug
,
);
ok(
defined
$cli
,
"Testing constructor for '$connectionType'"
);
if
(!
defined
$cli
&&
$connectionType
!~ /^(?i:TELNET|SSH)$/) {
diag
"Cannot open serial port provided"
;
redo
;
}
isa_ok(
$cli
,
'Control::CLI'
);
$connectionType
=
$cli
->connection_type;
ok(
$connectionType
,
"Testing connection type = $connectionType"
);
$eof
=
$cli
->
eof
;
ok(
$eof
,
"Testing eof is true before connecting"
);
if
(
$connectionType
=~ /^(?i:TELNET|SSH)$/) {
if
(!
defined
$host
) {
my
$complexInput
;
prompt(\
$host
,
"Provide an IP|hostname to test with (you will be prompted for commands to execute);\n [[username][:password]@]<host|IP> [port]; ENTER to end test]\n : "
);
if
(
$host
=~ s/^(.+)@//) {
(
$username
,
$password
) =
split
(
':'
, $1);
undef
$username
unless
length
$username
;
undef
$password
unless
length
$password
;
print
"Username = "
,
$username
,
"\n"
if
defined
$username
;
print
"Password = "
,
$password
,
"\n"
if
defined
$password
;
$complexInput
= 1;
}
if
(
$host
=~ /^(\S+)\s+(\d+)$/) {
(
$host
,
$tcpPort
) = ($1, $2);
$complexInput
= 1;
}
if
(
$complexInput
) {
print
"Host = "
,
$host
,
"\n"
if
defined
$host
;
print
"Port = "
,
$tcpPort
,
"\n"
if
defined
$tcpPort
;
print
"\n"
;
}
}
}
else
{
prompt(\
$baudrate
,
"Specify baudrate to use [just ENTER for 9600 baud]: "
, 9600);
}
$returnValue
=
$cli
->
connect
(
Host
=>
$host
,
Port
=>
$tcpPort
,
Username
=>
$username
,
Password
=>
$password
,
PublicKey
=>
$PublicKeyPath
,
PrivateKey
=>
$PrivateKeyPath
,
Passphrase
=>
$Passphrase
,
BaudRate
=>
$baudrate
,
DataBits
=>
$Databits
,
Parity
=>
$Parity
,
StopBits
=>
$Stopbits
,
Handshake
=>
$Handshake
,
Prompt_Credentials
=>
$PromptCredentials
,
);
ok(
$returnValue
,
"Testing connection"
);
unless
(
$returnValue
) {
diag
$cli
->errmsg;
redo
;
}
$eof
=
$cli
->
eof
;
ok( !
$eof
,
"Testing eof is false after connecting"
);
$cli
->
print
if
$connectionType
eq
'SERIAL'
;
$returnValue
=
$cli
->login(
Username
=>
$username
,
Password
=>
$password
,
Prompt_Credentials
=>
$PromptCredentials
,
);
ok(
$returnValue
,
"Testing login"
);
unless
(
$returnValue
) {
diag
$cli
->errmsg;
$cli
->disconnect;
redo
;
}
prompt(\
$cmd
,
"Specify a command to send, which generates some output: "
);
$returnValue
=
$cli
->cmd(
Command
=>
$cmd
,
Return_reference
=> 0,
);
ok(
$returnValue
,
"Testing cmd() method"
);
if
(
$returnValue
) { diag
"Obtained output of command '$cmd':\n$returnValue"
}
else
{ diag
$cli
->errmsg }
$cli
->disconnect;
$eof
=
$cli
->
eof
;
ok(
$eof
,
"Testing eof is true after disconnecting"
);
}}
while
(
$TestMultiple
);
done_testing();