Advanced-Config

 view release on metacpan or  search on metacpan

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


   # 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
{
   DBUG_ENTER_FUNC (@_);
   my $cfg   = shift;   # The source config file.
   my $dcfg  = shift;   # The config file to comare it to.
   my $lbl   = shift;   # The label to use ...
   my $data  = shift;   # The stats on the source cfg file.
   my $fail  = shift;   # 1-Decrypt should fail.  0-Decrypt should succeed.
   my $which = shift;   # Which tags were decrypted!

   my @sect = $dcfg->find_sections ();
   my $cnt = keys %{$data};
   my $dcnt = @sect;

   dbug_ok (1, "-"x30);
   dbug_is ($cnt, $dcnt, "The ${lbl} config file has the right number of sections.");

   $cnt = 0;
   foreach my $s ( @sect ) {
      unless ( exists $data->{$s} ) {
         dbug_ok (0, "Section '$s' exists in the original config file.");
         next;
      }

      my @tag_list = $dcfg->get_section ($s)->find_tags ();
      my $tcnt = @tag_list;
      ok ( $tcnt == $data->{$s}->{CNT}, "Section '$s' in the ${lbl} cfg file has the right number of tags ($tcnt)" );

      foreach my $t ( @tag_list ) {
         my $stag = $cfg->get_section ($s)->get_value ($t);
         my $dtag = $dcfg->get_section ($s)->get_value ($t);
         unless ( $stag ) {
            dbug_ok (0, "Tag \"${t}\" exists in both config files.");
         } elsif ( ! $which->{$t} ) {
            dbug_cmp_ok ( $stag, 'eq', $dtag, "Tag \"${t}\" has the same value in both config files! ($dtag)" );
         } elsif ( $fail ) {
            dbug_cmp_ok ( $stag, 'ne', $dtag, "Tag \"${t}\" had issues decrypting this value from the config file. ($dtag)" );
         } else {
            dbug_cmp_ok ( $stag, 'eq', $dtag, "Tag \"${t}\" has the same value in both config files. ($dtag)" );
         }
      }
   }

   DBUG_VOID_RETURN ();
}

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

sub init_cfg_file
{



( run in 0.625 second using v1.01-cache-2.11-cpan-39bf76dae61 )