File-SOPS

 view release on metacpan or  search on metacpan

t/31-negative-zero-integer-cache.t  view on Meta::CPAN

    # Every one of these is an integral float that YAML::XS caches an integer
    # on as well. They are NOT retyped, because their two halves agree about
    # the value: `1e3` is 1000 whichever way it is labelled, and the digest
    # bytes are identical. A guard that read SVf_NOK first would move all of
    # them (measured: 50 further corpus rows) to fix the one that needs it.
    my %expected = (
        '1e3'      => [ int   => '1000' ],
        '1E3'      => [ int   => '1000' ],
        '1e+3'     => [ int   => '1000' ],
        '1.0e0'    => [ int   => '1' ],
        '0755e0'   => [ int   => '755' ],
        '0.0e0'    => [ int   => '0' ],
        '0e0'      => [ int   => '0' ],
        '+0.0e0'   => [ int   => '0' ],
        '-1.0e0'   => [ int   => '-1' ],
        '-1e1'     => [ int   => '-10' ],
        '100.0e0'  => [ int   => '100' ],
        '-0.5e0'   => [ float => '-0.5' ],
        '-0.0'     => [ float => '-0' ],
        '-0.00'    => [ float => '-0' ],
        '1.5'      => [ float => '1.5' ],
        '5432'     => [ int   => '5432' ],
        '007'      => [ int   => '7' ],
        '1e20'     => [ float => '100000000000000000000' ],
        '1e-3'     => [ float => '0.001' ],
    );
    for my $spelling (sort keys %expected) {
        my ($type, $bytes) = @{ $expected{$spelling} };
        my $leaf = parsed($spelling);
        is(File::SOPS::Encrypted->detect_type($leaf), $type,
            "YAML $spelling is still $type");
        is(File::SOPS::Encrypted->value_to_bytes($leaf), $bytes,
            "YAML $spelling still digests $bytes");
    }
};

###############################################################################
# 2. THE MODEL OF GO. _go_scalar_bytes agreed with our own wrong answer, which
#    is why the k86 guard waved the leaf through. It has to answer what Go
#    answers -- and it has to keep answering it on a scalar it is handed twice,
#    because the conversion it used numified its operand in place.
###############################################################################

subtest 'the Go model resolves a negative zero with an exponent as -0' => sub {
    for my $spelling (@NEGATIVE_ZERO_SPELLINGS) {
        is(File::SOPS::Format::YAML::_go_scalar_bytes($spelling), '-0',
            "Go reads $spelling as the float -0");
    }
};

subtest 'the Go model still answers what ADR 0013 measured' => sub {
    my %expected = (
        '0755'                => '493',
        '010'                 => '8',
        '007'                 => '7',
        '1e3'                 => '1000',
        '0755e0'              => '755',
        '1_000'               => '1000',
        '.inf'                => '+Inf',
        '-.inf'               => '-Inf',
        '.nan'                => 'NaN',
        'TRUE'                => 'True',
        '2015-01-01'          => '2015-01-01T00:00:00Z',
        '2015-01-01T12:00:00Z' => '2015-01-01T12:00:00Z',
        '-0.5e0'              => '-0.5',
        '-1.0e0'              => '-1',
        '0.0e0'               => '0',
        '-0'                  => '0',
        'localhost'           => 'localhost',
        '123abc'              => '123abc',
    );
    for my $token (sort keys %expected) {
        is(File::SOPS::Format::YAML::_go_scalar_bytes($token), $expected{$token},
            "Go reads $token as $expected{$token}");
    }
    is(File::SOPS::Format::YAML::_go_scalar_bytes('9223372036854775808'), undef,
        'and still refuses to guess at a uint64');
};

subtest 'the model answers the same on a scalar it is handed three times' => sub {
    # `$p * 1.0` was stable here and wrong; `0 + $p` and `$p * 1` are right on
    # the first call and wrong on every later one, from the same scalar in the
    # same process. Only a pack/unpack copy reads the NV and nothing else.
    for my $token ('-0.0e0', '-0.0', '0755', '1e3', '-0.5e0') {
        my $reused = $token;
        my @rounds = map { File::SOPS::Format::YAML::_go_scalar_bytes($reused) } 1 .. 3;
        is($rounds[1], $rounds[0], "$token: round 2 agrees with round 1");
        is($rounds[2], $rounds[0], "$token: round 3 agrees with round 1");
    }
};

###############################################################################
# 3. END TO END. The document, the binary, and this library's own MAC.
###############################################################################

sub sops_decrypt {
    my ($file, $format) = @_;
    my $out = `$sops_bin -d --input-type $format --output-type $format $file 2>&1`;
    return ($? >> 8, $out);
}

subtest 'an unencrypted YAML negative zero is written and sops reads it back' => sub {
    for my $spelling (@NEGATIVE_ZERO_SPELLINGS) {
        my $document = eval {
            File::SOPS->encrypt(
                data       => { x_unencrypted => parsed($spelling), other => 'kept' },
                recipients => [$public],
                format     => 'yaml',
            );
        };
        my $error = $@;
        ok(defined $document, "$spelling: a document is written")
            or diag("croaked: $error");
        next unless defined $document;

        like($document, qr/^x_unencrypted: \Q$spelling\E$/m,
            "$spelling: the source spelling survives into the document");

        my $file = scratch_file('yaml');
        write_binary($file, $document);
        my ($rc, $out) = sops_decrypt($file, 'yaml');



( run in 2.663 seconds using v1.01-cache-2.11-cpan-60884a8df76 )