#!/usr/bin/perl
#
# Copyright 2004-2012 SPARTA, Inc.  All rights reserved.  See the COPYING
# file distributed with this software for details
#
#
# This script performs simple tests of the DNSSEC-Tools tooloptions module.
# It calls tooloptions() to retrieve various configurations of options:
# conf, keyrec, cmdopts; conf, cmdopts; conf, keyrec.  Command-line option
# inclusion is turned off for some tests, on for other tests, and dropped
# for the final test.  A keyrec file will be created in the current directory
# for testing.
#
# These are simple tests in that the results are displayed to the screen
# and the tester is left the responsibility of ensuring that all worked
# as expected.  Automatic testing would be great, and it may come RSN.
#

use strict;

use Net::DNS::SEC::Tools::tooloptions;

my @locopts =
(
	"froofy=s",
	"woofers=s",
);

my $ropts;
my %opts;

my $krf;
my $krname;

$krf = "portrigh.keyrec";
$krname = "Kportrigh.com.+005+52000";

buildkrf($krf);

#####################################################################
#
# Test a call with keyrec file, keyrec name, and local options.
#
$ropts = tooloptions($krf,$krname,@locopts);
%opts = %$ropts;

print "options (with keyrec):\n";
foreach my $k (sort(keys(%opts)))
{
	print "\t<$k>	<$opts{$k}>\n";
}
print "\n";

#####################################################################
#
# Test a call with keyrec file, keyrec name, and local options.
#
$ropts = tooloptions("",@locopts);
%opts = %$ropts;

print "options (no keyrec):\n";
foreach my $k (sort(keys(%opts)))
{
	print "\t<$k>	<$opts{$k}>\n";
}
print "\n";

#####################################################################
#
# Test a call with keyrec file, keyrec name, and local options.
# Use of command line options is suspended.
#
opts_suspend();
$ropts = tooloptions($krf,$krname,@locopts);
%opts = %$ropts;

print "options (with suspended options):\n";
foreach my $k (sort(keys(%opts)))
{
	print "\t<$k>	<$opts{$k}>\n";
}
print "\n";


#####################################################################
#
# Test a call with keyrec file, keyrec name, and local options.
# Use of command line options is restored.
#
opts_restore();
$ropts = tooloptions($krf,$krname,@locopts);
%opts = %$ropts;

print "options (with restored options):\n";
foreach my $k (sort(keys(%opts)))
{
	print "\t<$k>	<$opts{$k}>\n";
}
print "\n";


#####################################################################
#
# Test a call with keyrec file, keyrec name, and local options.
# Use of command line options is dropped.
#
opts_drop();
$ropts = tooloptions($krf,$krname,@locopts);
%opts = %$ropts;

print "options (with dropped options):\n";
foreach my $k (sort(keys(%opts)))
{
	print "\t<$k>	<$opts{$k}>\n";
}
print "\n";


#####################################################################
#
# Test a call with keyrec file, keyrec name, and local options.
# Use of command line options is restored, but inaccessible.
#
opts_restore();
$ropts = tooloptions($krf,$krname,@locopts);
%opts = %$ropts;

print "options (with restored options, but post-drop):\n";
foreach my $k (sort(keys(%opts)))
{
	print "\t<$k>	<$opts{$k}>\n";
}
print "\n";


exit(0);


#####################################################################
#
# buildkrf()
#
sub buildkrf
{
	my $krf = shift;

        open(KRF,"> $krf");
	print KRF <<EOF;

#
# key management database
#

zone "portrigh.com"
	zonefile	"db.portrigh.com"
	kskkey		"Kportrigh.com.+005+26000.key"
	kskpath		"./Kportrigh.com.+005+26000.key"
	zskkey		"Kportrigh.com.+005+52000.key"
	zskpath		"./Kportrigh.com.+005+52000.key"
	endtime		"+2592000"		# good for 30 days
	keyrec_signsecs	"1101183759"
	keyrec_signdate	"Tue Nov 23 04:22:39 2004-2006"

key "Kportrigh.com.+005+26000"
	zonename	"portrigh.com"
	keyrec_type	"ksk"
	algorithm	"rsasha1"
	ksklength	"1024"
	random		"-r /dev/urandom"
	keyrec_gensecs	"1101183759"
	keyrec_gendate	"Tue Nov 23 04:22:39 2004-2006"

key "Kportrigh.com.+005+52000"
	zonename	"portrigh.com"
	keyrec_type	"zsk"
	algorithm	"rsasha1"
	zsklength	"512"
	random		"-r /dev/urandom"
	keyrec_gensecs	"1101183759"
	keyrec_gendate	"Tue Nov 23 04:22:39 2004-2006"
EOF

	close(KRF);
}
