Advanced-Config

 view release on metacpan or  search on metacpan

t/28-sensitive_tests.t  view on Meta::CPAN

{
   # Turn fish on ...
   DBUG_PUSH ( $fish );

   DBUG_ENTER_FUNC (@ARGV);

   dbug_ok (1, "In the MAIN program ...");  # Test # 2 ...

   # Make this extra pattern sensitive!
   Advanced::Config::Options::make_it_sensitive ("1111", "zork", "22222");

   my $file = File::Spec->catfile ("t", "config", "28-sensitive.cfg");
   my $cfg1 = make_object ( $file );
   my $cfg2 = make_object ( $file, "disable_variables" => 1 );
   my $cfg3 = make_object ( $file, "disable_decryption" => 1 );

   # Doesn't work after the config files are loaded ..
   Advanced::Config::Options::make_it_sensitive ("pork");

   my @sections = $cfg1->find_sections ();
   my $cnt = @sections;
   dbug_is ($cnt, 3, "All 3 sections were accounted for!");

   my @tag_list = $cfg1->find_tags ();
   $cnt = @tag_list;

   # --------------------------------------------------------------
   # Section # 1: Normal operation, validating the sensitive logic.
   #              All tags considered sensitive end in "_y" for this test.
   #              Or if a section name is sensitive everything in
   #              it is sensitive!
   # --------------------------------------------------------------
   foreach my $s ( @sections ) {
      dbug_ok (1, "-"x50);
      my $lCfg = $cfg1->get_section ( $s );
      my @tags = $lCfg->find_tags ();
      my $tlt = @tags;

      my $sens = Advanced::Config::Options::should_we_hide_sensitive_data ( $s );
      if ( $sens ) {
         dbug_ok ( 1, "Processing sensitive section '$s'.");
      } else {
         dbug_ok ( 1, "Processing normal section '$s'.");
      }
      dbug_is ( $tlt, $cnt, "Found ${cnt} tags in this section.");

      foreach my $t (@tags) {
         my $ans = ($sens || ( $t =~ m/_y$/i )) ? 1 : 0;
         my $chk = $lCfg->chk_if_sensitive ( $t );
         my $v = $lCfg->get_value ($t);

         if ( $ans ) {
            dbug_is ( $chk, 1, "Tag '$t' is considered sensitive!  ($v)" );
         } else {
            dbug_is ( $chk, 0, "Tag '$t' is NOT considered sensitive!  ($v)" );
         }
      }
   }

   # --------------------------------------------------------------
   # Section # 2: Now testing which tags have unexpanded variables ...
   #              Only tests tags listed in '00_has_variables'
   # --------------------------------------------------------------
   foreach my $s ( @sections ) {
      dbug_ok (1, "="x50);
      my $lCfg = $cfg2->get_section ( $s );
      dbug_ok (1, "Disabled Variable test for section: " . $s);

      # Get from "main" section, not individual sections
      my $hash_ref2 = $cfg2->get_hash_values ( "00_has_variables" );

      foreach my $t ( $lCfg->find_tags () ) {
         my $v = $lCfg->get_value ($t);
         my $bool = $lCfg->chk_if_still_uses_variables ($t);
         my $ans = ( $v =~ m/[$][{][^}]+[}]/ ) ? 1 : 0;  # Check for a variable definition.
         my $agree = ($bool == $ans) ? 1 : 0;

         if ( exists $hash_ref2->{$t} ) {
            dbug_ok ( $agree && $bool, "Tag '$t' has unresolved variables in it!  ($v)" );
         } elsif ( $ans || $bool ) {
            dbug_ok ( 0, "Tag '$t' has NO variables in it!  ($v)  [$ans, $bool]" );
         }
      }
   }

   # --------------------------------------------------------------
   # Section # 3: Now testing which tags failed to decrypt!
   #              Only tests tags listed in '00_has_decryption'
   # --------------------------------------------------------------
   foreach my $s ( @sections ) {
      dbug_ok (1, "~"x50);
      my $lCfg = $cfg3->get_section ( $s );
      dbug_ok (1, "Disabled Decryption test for section: " . $s);

      # Get from "main" section, not individual sections
      my $hash_ref3 = $cfg3->get_hash_values ( "00_has_decryption" );

      foreach my $t ( $lCfg->find_tags () ) {
         my $v = $lCfg->get_value ($t);
         my $chk = $lCfg->chk_if_still_encrypted ($t);

         my $bool = $lCfg->chk_if_still_uses_variables ($t);
         my $ans = ( $v =~ m/[$][{][^}]+[}]/ ) ? 1 : 0;  # Check for a variable definition.

         if ( exists $hash_ref3->{$t} ) {
            if ( $bool || $ans ) {
               dbug_ok ( 0, "Tag '$t' references an encrypted value ($v)" );
            } else {
               dbug_ok ( $chk, "Tag '$t' references an encrypted value ($v)" );
            }
         } elsif ( $chk ) {
            dbug_ok ( 0, "Tag '$t' is correctly marked as encrypted ($v)" );
         } elsif ( $bool != $ans ) {
            my $v = $bool ? "" : "non-";
            dbug_ok ( 0, "Tag '$t' is correctly marked as a ${v}variable ($v)" );
         } elsif ( $bool ) {
            dbug_ok ( 1, "Tag '$t' references an encrypted variable ($v)" );
         }
      }
   }



( run in 0.916 second using v1.01-cache-2.11-cpan-5b529ec07f3 )