Advanced-Config
view release on metacpan or search on metacpan
t/20-validate_encrypt_decrypt.t view on Meta::CPAN
#!/usr/bin/perl
use strict;
use warnings;
use Test::More;
use File::Basename;
use File::Spec;
use Sys::Hostname;
use Fred::Fish::DBUG 2.09 qw / on /;
use Fred::Fish::DBUG::Test 2.09;
# How to find the helper module ...
BEGIN { push (@INC, File::Spec->catdir (".", "t", "test-helper")); }
use helper1234;
my $fish;
# =================================================================
# Tests the encryption/decryption logic.
# =================================================================
# Assumptions about the config files made by this test program:
# If any assumtions are false, you will see test failures.
# 1) No tag appeears in multiple sections for this test.
# 2) All tags starting with "join" are assumed to reference
# encrypted variables/tags.
# =================================================================
BEGIN {
$fish = turn_fish_on_off_for_advanced_config ();
unlink ( $fish );
DBUG_ENTER_FUNC ();
use_ok ("Advanced::Config");
DBUG_VOID_RETURN ();
}
END {
DBUG_ENTER_FUNC ();
# Don't do any tests in the END block ...
DBUG_VOID_RETURN ();
}
# =================================================================
my %decrypt_callback_tags;
my %encrypt_callback_tags;
sub my_security_callback
{
DBUG_ENTER_FUNC (@_);
my $mode = shift; # 0 = Decrypt / 1 = Encrypt
my $tag = shift;
my $value = shift;
my $file = shift;
my $workArea = shift;
if ( $mode == 0 ) {
# Decryption ...
$decrypt_callback_tags{$tag} = 1;
if ( $value =~ m/^XX(.*)YY$/ ) {
$value = $1;
} else {
$value = "Bogus decryption ...";
}
} else {
# Encryption ...
$encrypt_callback_tags{$tag} = 1;
$value = "XX" . $value . "YY";
}
DBUG_RETURN ( $value );
}
sub my_source_callback
{
t/20-validate_encrypt_decrypt.t view on Meta::CPAN
my $ccc = $cfg->get_value($cTag);
my $ddd = $cfg->get_value($dTag);
my $zzz = $cfg->get_value($zTag);
dbug_ok ( 1, "Validating tag ${aTag}: [$aaa]");
dbug_cmp_ok ( $bbb, 'eq', $aaa, "Validating tag ${bTag}: [$bbb]");
dbug_cmp_ok ( $ccc, 'eq', $aaa, "Validating tag ${cTag}: [$ccc]");
dbug_cmp_ok ( $ddd, 'eq', $aaa, "Validating tag ${dTag}: [$ddd]");
dbug_cmp_ok ( $zzz, 'eq', $aaa, "Validating tag ${zTag}: [$zzz]");
DBUG_VOID_RETURN ();
}
# =================================================================
sub run_all_tests
{
DBUG_ENTER_FUNC (@_);
my $alias = shift;
my $rOpts = shift;
dbug_ok (1, "x"x50);
dbug_ok (1, "?"x10 . " $alias " . "?"x10);
# my $emptyCfg = Advanced::Config->new (undef, { assign => "?", quote_left => 'x', quote_right => 'x' } );
my $emptyCfg = Advanced::Config->new (undef, $rOpts);
dbug_isa_ok ($emptyCfg, 'Advanced::Config');
# Options to use in decrypting an encrypted file ...
my %aOpts;
%aOpts = %{$rOpts} if ( defined $rOpts );
$aOpts{alias} = $alias;
my ($orig_file, $encrypt_file, $file_decrypt, $fail_file);
$orig_file = $encrypt_file = $file_decrypt = $fail_file =
File::Spec->catfile ("t", "config", $alias);
# Add a prefix ...
$encrypt_file =~ s/-0-/-1-/;
$file_decrypt =~ s/-0-/-2-/;
$fail_file =~ s/-0-/-3-/;
# Add a postfix ...
$encrypt_file =~ s/[.]cfg$/.encrypted.cfg/;
$file_decrypt =~ s/[.]cfg$/.decrypted.cfg/;
$fail_file =~ s/[.]cfg$/.failure.cfg/;
my $cfg = init_cfg_file ( $orig_file, $rOpts );
# Encrypting the file ...
DBUG_PRINT ("====", "%s", "="x50);
my $status = $emptyCfg->encrypt_config_file ($orig_file, $encrypt_file, $rOpts);
dbug_is ($status, 1, "Encryption Succeeded!");
# Reload the encrypted file back into memory ...
DBUG_PRINT ("====", "%s", "="x50);
my $ecfg = init_cfg_file ( $encrypt_file, \%aOpts );
# Saves a list of tags to be decrypted ...
# Set via the callback function for the encrypt/decrypt logic.
my %save = %decrypt_callback_tags;
# Loading using a bad alias ...
DBUG_PRINT ("====", "%s", "="x50);
my $ecfg2 = init_cfg_file ( $encrypt_file, $rOpts );
# Decrypting the file correctly ...
DBUG_PRINT ("====", "%s", "="x50);
$status = $emptyCfg->decrypt_config_file ($encrypt_file, $file_decrypt, \%aOpts);
dbug_is ($status, 1, "Decryption Succeeded!");
my $dcfg = init_cfg_file ( $file_decrypt, $rOpts );
# Decrypting the file incorrectly ...
DBUG_PRINT ("====", "%s", "f"x50);
$status = $emptyCfg->decrypt_config_file ($encrypt_file, $fail_file, $rOpts);
dbug_is ($status, 1, "Bad Decryption Succeeded!");
my $fcfg = init_cfg_file ( $fail_file, $rOpts );
DBUG_PRINT ("====", "%s", "="x50);
my @sections = $cfg->find_sections ();
my $cnt = @sections;
dbug_ok ($cnt, "The config file has ${cnt} section(s) in it!");
my %data;
# Get the stats for the main file ...
foreach ( @sections ) {
my %parts;
my @tag_list = $cfg->get_section ($_)->find_tags ();
my $tcnt = @tag_list;
dbug_ok ( $tcnt, "Found ${tcnt} tags in section $_");
$parts{CNT} = $tcnt;
$parts{TAGS} = \@tag_list;
$data{$_} = \%parts;
# All variables begining with "join..." reference encrypted variables.
# So put in %save as well.
foreach my $t ( @tag_list ) {
$save{$t} = 1 if ( $t =~ m/^join/ );
}
}
DBUG_PRINT ("----", "%s", "-"x50);
# Validating the encrypted file ...
compare_cfg ( $cfg, $ecfg, "encrypted", \%data, 0, \%save);
compare_cfg ( $cfg, $dcfg, "decrypted", \%data, 0, \%save);
# These compares should fail the decryption process!
compare_cfg ( $cfg, $ecfg2, "no alias failure", \%data, 1, \%save);
compare_cfg ( $cfg, $fcfg, "clear failure", \%data, 1, \%save);
# unlink ($encrypt_file, $file_decrypt, $fail_file);
DBUG_VOID_RETURN ();
}
# =================================================================
sub compare_cfg
( run in 1.100 second using v1.01-cache-2.11-cpan-e1769b4cff6 )