Advanced-Config

 view release on metacpan or  search on metacpan

t/20-validate_encrypt_decrypt.t  view on Meta::CPAN

   DBUG_LEAVE (0);
}

# =================================================================

sub run_alt_tests
{
   DBUG_ENTER_FUNC (@_);
   my $cfg = shift;
   my $aTag = shift;
   my $bTag = shift;
   my $cTag = shift;
   my $dTag = shift;
   my $zTag = shift;

   my $aaa = $cfg->get_value($aTag);
   my $bbb = $cfg->get_value($bTag);
   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);



( run in 0.942 second using v1.01-cache-2.11-cpan-5735350b133 )