#!/usr/bin/perl
#
# Copyright 2004-2013 SPARTA, Inc.  All rights reserved.  See the COPYING
# file distributed with this software for details
#
#
# DNSSEC-Tools
#
# Test script for the DNSSEC-Tools tooloptions module.  This script tests
# the opts_krfile() and opts_getkeys() interfaces.  Upon being given a
# keyrec file and the name of a zone's keyrec, the keyrecs of the zone's
# KSK and ZSK are printed.
#
# Executing with the following commands line should yield success:
#
#	test-toolopts2 -krf portrigh.keyrec -keyrec portrigh.com
#	test-toolopts2 -krf portrigh.keyrec -keyrec triharpskel.com
#
# Executing with any other -keyrec option should fail.
#
# Some of the data in the test keyrec file is nonsense.  However, it is
# useful to distinguish between keyrecs, thus allowing one to determine
# if the test passed or failed.
#

use strict;

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

my $ropts;			# Reference to option table.
my %opts;			# Option hash table.

my $krf;			# Keyrec file.
my $dum;			# Dummy return value.

my $rkh;			# Reference to the KSK keyrec.
my $rzh;			# Reference to the ZSK keyrec.
my %kskrec;			# The KSK keyrec itself.
my %zskrec;			# The ZSK keyrec itself.

my $argc = @ARGV;		# Argument count.

########################################################
#
# Build a keyrec file for use by these tests.
#
$krf = "portrigh.keyrec";
buildkrf($krf);

########################################################
#
# Check for required arguments.  We'll do this *after* the
# keyrec file was built so that the tester can examine the
# keyrecs for useful info.
#
if($argc == 0)
{
	print STDERR "usage:  test-toolopts2 -krfile $krf -keyrec <zone>\n";
}

########################################################
#
# Get the options keyrec names of the KSK and ZSK for the
# zone specified in our command line.
#
($dum,$dum,$ropts) = opts_krfile("","");
if($ropts == undef)
{
	print "opts_krfile() returned an undefined option reference\n";
}
else
{
	%opts = %$ropts;

	print "kskkey\t\t$opts{'kskkey'}\n";
	print "zskkey\t\t$opts{'zskkey'}\n";
	print "\n";
}

########################################################
#
# Get the KSK and ZSK keyrecs for the zone specified in our command line.
#
($rkh,$rzh) = opts_getkeys();
if($rkh == undef)
{
	print "opts_getkeys() returned an undefined KSK option reference\n";
	exit(1);
}
if($rzh == undef)
{
	print "opts_getkeys() returned an undefined ZSK option reference\n";
	exit(1);
}

%kskrec = %$rkh;
%zskrec = %$rzh;

print "kskrec:\n";
foreach my $k (sort(keys(%kskrec)))
{
	print "\t$k\t\t$kskrec{$k}\n";
}
print "\n\n";

print "zskrec:\n";
foreach my $k (sort(keys(%zskrec)))
{
	print "\t$k\t\t$zskrec{$k}\n";
}
print "\n\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"
	kskpath		"./Kportrigh.com.+005+26000.key"
	zskkey		"Kportrigh.com.+005+52000"
	zskpath		"./Kportrigh.com.+005+52000.key"
	endtime		"+8888888"		# good for 102 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	"rsasha2"
	ksklength	"1024"
	random		"-r /dev/urandom1"
	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	"rsasha3"
	zsklength	"768"
	random		"-r /dev/urandom2"
	keyrec_gensecs	"1101183759"
	keyrec_gendate	"Tue Nov 23 04:22:39 2004-2006"

zone "triharpskel.com"
	zonefile	"db.triharpskel.com"
	kskkey		"Ktriharpskel.com.+005+32000"
	kskpath		"keys.ksk/Ktriharpskel.com.+005+32000.key"
	zskkey		"Ktriharpskel.com.+005+64000"
	zskpath		"keys.zsk/Ktriharpskel.com.+005+64000.key"
	endtime		"+7800000"		# good for 90 days
	kskkey		"Ktriharpskel.com.+005+32000"
	zskkey		"Ktriharpskel.com.+005+64000"
	keyrec_signsecs	"1101183759"
	keyrec_signdate	"Tue Nov 23 04:22:39 2004-2006"

key "Ktriharpskel.com.+005+32000"
	zonename	"triharpskel.com"
	keyrec_type	"ksk"
	algorithm	"rsasha4"
	ksklength	"2048"
	random		"-r /dev/urandom3"
	keyrec_gensecs	"1101183759"
	keyrec_gendate	"Tue Nov 23 04:22:39 2004-2006"

key "Ktriharpskel.com.+005+64000"
	zonename	"triharpskel.com"
	keyrec_type	"zsk"
	algorithm	"rsasha5"
	zsklength	"2008"
	random		"-r /dev/urandom4"
	keyrec_gensecs	"1101183759"
	keyrec_gendate	"Tue Nov 23 04:22:39 2004-2006"

EOF

	close(KRF);
}
