App-MechaCPAN

 view release on metacpan or  search on metacpan

t/09_checksums.t  view on Meta::CPAN

      $verifier_called = 0;
      $fetch_called    = 0;
      local $@;
      my $ok = eval { App::MechaCPAN::get_cpan_checksums($url); 1 };
      is( $ok, 1, '$CHKSIGS=0 returns cleanly on a valid body' )
        or diag $@;

      is( $fetch_called,    0, 'fetch_file was not invoked when $CHKSIGS=0' );
      is( $verifier_called, 0, 'verifier was not invoked when $CHKSIGS=0' );
    }
  }

  # best-effort ($CHKSIGS undef) + verifier resolves + valid body —
  # verifier closure is invoked with (CHECKSUMS-file-path, keyring-path)
  {
    my @verifier_args;
    my $body_seen;

    local $App::MechaCPAN::CHKSIGS;
    local $body     = clearsign($basic_dd);
    local $verifier = sub
    {
      $verifier_called++;
      @verifier_args = @_;
      open my $fh, '<', $verifier_args[0];
      $body_seen = do { local $/; <$fh> };
      return;
    };

    {
      $verifier_called = 0;
      local $@;
      my $ok    = eval { App::MechaCPAN::get_cpan_checksums($url) };
      my $error = $@;
      is( $error, '', 'get_cpan_checksums ran without error' );
      isnt( $ok, undef, 'get_cpan_checksums ran completely' );
      is( ref $ok, 'HASH', 'get_cpan_checksums results is a hash' );

      is( $verifier_called,      1,          'verifier was called' );
      is( scalar @verifier_args, 2,          'verifier received two arguments' );
      is( $verifier_args[1],     "$keyring", 'second arg is the stubbed keyring path' );
      isnt( $body_seen, undef, 'verifier body was modified' );
      isnt( $body_seen, '',    'verifier could read the CHECKSUMS file off disk' );
      like( $body_seen, qr/cksum/, 'CHECKSUMS file content reached the verifier' );
    }
  }

  # $CHKSIGS undef + no verifier = best-effort; returns cleanly
  {
    local $App::MechaCPAN::CHKSIGS;
    local $body     = clearsign($basic_dd);
    local $verifier = undef;

    {
      $verifier_searched = 0;
      local $@;
      my $ok    = eval { App::MechaCPAN::get_cpan_checksums($url); 1 };
      my $error = $@;

      is( $ok,                1,  'get_cpan_checksums did completely run' );
      is( $error,             '', 'get_cpan_checksums did not throw an error' );
      is( $verifier_searched, 1,  'verifier search was called once' );
    }
  }

  # $CHKSIGS=1 + no verifier available — fatal
  {
    local $App::MechaCPAN::CHKSIGS = 1;
    local $body                    = clearsign($basic_dd);
    local $verifier                = undef;

    {
      $verifier_searched = 0;
      local $@;
      my $ok    = eval { App::MechaCPAN::get_cpan_checksums($url); 1 };
      my $error = $@;

      isnt( $ok,    1,  'get_cpan_checksums did not completely run' );
      isnt( $error, '', 'get_cpan_checksums threw an error' );
      like( $error, qr/verification program/, '$CHKSIGS=1 with no resolvable verifier dies' );
      is( $verifier_searched, 1, 'verifier search was called once' );
    }
  }

  # verifier dies — its failure propagates to the caller
  {
    local $App::MechaCPAN::CHKSIGS = 1;
    local $body                    = clearsign($basic_dd);
    local $verifier                = sub { die "BAD SIGNATURE\n" };

    {
      $verifier_searched = 0;
      local $@;
      my $ok    = eval { App::MechaCPAN::get_cpan_checksums($url); 1 };
      my $error = $@;

      isnt( $ok,    1,  'get_cpan_checksums did not completely run' );
      isnt( $error, '', 'get_cpan_checksums threw an error' );
      is( $verifier_searched, 1, 'verifier search was called once' );
      like( $error, qr/BAD SIGNATURE/, 'verifier failure propagates' );
    }
  }

  # $CHKSIGS undef + missing module = returns nothing
  {
    local $App::MechaCPAN::CHKSIGS;
    local $body     = clearsign($basic_dd);
    local $verifier = sub
    {
      $verifier_called++;
      return;
    };

    {
      $verifier_searched = 0;
      local $@;
      my $ok    = eval { App::MechaCPAN::get_cpan_checksums( $url, 'NoDeps-1.0.tar.gz' ); };
      my $error = $@;

      isnt( $ok, undef, 'get_cpan_checksums with known module did completely run' );
      is( ref $ok,            'HASH', 'get_cpan_checksums returns a hashref' );
      is( $error,             '',     'get_cpan_checksums did not throw an error' );
      is( $verifier_searched, 1,      'verifier search was called once' );
    }
    {
      $verifier_searched = 0;
      local $@;
      my $ok    = eval { App::MechaCPAN::get_cpan_checksums( $url, 'NoDeps-X.0.tar.gz' ); };
      my $error = $@;

      is( $ok,                undef, 'get_cpan_checksums with unknown module did completely run' );
      is( $error,             '',    'get_cpan_checksums did not throw an error' );
      is( $verifier_searched, 1,     'verifier search was called once' );
    }
  }

  # $CHKSIGS=1 + missing module = fatal
  {
    local $App::MechaCPAN::CHKSIGS = 1;
    local $body                    = clearsign($basic_dd);
    local $verifier                = sub
    {
      $verifier_called++;
      return;
    };

    {
      $verifier_searched = 0;
      local $@;
      my $ok    = eval { App::MechaCPAN::get_cpan_checksums( $url, 'NoDeps-1.0.tar.gz' ); };
      my $error = $@;

      isnt( $ok, undef, 'get_cpan_checksums with known module did completely run' );
      is( ref $ok,            'HASH', 'get_cpan_checksums returns a hashref' );
      is( $error,             '',     'get_cpan_checksums did not throw an error' );
      is( $verifier_searched, 1,      'verifier search was called once' );
    }
    {
      $verifier_searched = 0;
      local $@;
      my $ok    = eval { App::MechaCPAN::get_cpan_checksums( $url, 'NoDeps-X.0.tar.gz' ); };
      my $error = $@;

      is( $ok,                undef, 'get_cpan_checksums with unknown module did completely run' );
      isnt( $error,           '', 'get_cpan_checksums throw an error' );
      is( $verifier_searched, 1, 'verifier search was called once' );
    }
  }
}

done_testing;



( run in 0.521 second using v1.01-cache-2.11-cpan-6aa56a78535 )