Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/Zonemaster/Engine/DNSName.pm
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ sub prepend {
return $self->new( { labels => \@labels } );
}

sub TO_JSON {
my ( $self ) = @_;

return $self->string;
}

1;

=head1 NAME
Expand Down Expand Up @@ -234,6 +240,10 @@ See also L<https://tools.ietf.org/html/rfc7719#section-6>.

Returns a new L<Zonemaster::Engine::DNSName> object, representing the called one with the given label prepended.

=item TO_JSON

Helper method for JSON encoding.

=back

=cut
10 changes: 10 additions & 0 deletions lib/Zonemaster/Engine/Packet.pm
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ sub answerfrom {
return $from;
}

sub TO_JSON {
my ( $self ) = @_;

return { 'Zonemaster::Engine::Packet' => $self->packet };
}

1;

=head1 NAME
Expand Down Expand Up @@ -226,6 +232,10 @@ C<authority> and C<additional>, only RRs from those sections are returned.

Wrapper for the underlying packet method, that replaces undefined values with the string C<E<lt>unknownE<gt>>.

=item TO_JSON

Support method for L<JSON> to be able to serialize these objects.

=back

=head1 METHODS PASSED THROUGH
Expand Down
34 changes: 32 additions & 2 deletions t/logger.t
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use Test::More;
use Test::Fatal;
use File::Slurp;
use JSON::PP;
use Test::Fatal;
use Test::More;

BEGIN {
use_ok( 'Zonemaster::Engine' );
Expand Down Expand Up @@ -36,6 +37,35 @@ ok( scalar( @{ Zonemaster::Engine->logger->entries } ) >= 2, 'expected number of

like( "$entry", qr/System:Unspecified:TEST an=argument/, 'stringification overload' );

$entry = info( 'TEST2', { an => 'argument', domain => Zonemaster::Engine::Util::name( 'logger.test.example' ) } );

# FIXME: Ideally, we really should instead have domain=logger.test.example
# without the double quotes around the domain name.
#
# Providing the domain name as a regular string gives us no double quotes
# around the domain name. Providing it directly as a
# Zonemaster::Engine::DNSName does give us double quotes, however. That’s
# because blessed objects get turned into their JSON representations, while
# regular strings are interpolated as they are. The TO_JSON method for DNSName
# objects returns a simple string, which is then converted into JSON in the
# usual double-quoted notation.
#
# It’s an annoying inconsistency, but any attempt at a fix might cause
# repercussions when translating a log entry to a human language, a string, or
# JSON. For now, we expect the broken behavior, but that should be fixed
# someday.
is(
"$entry",
'System:Unspecified:TEST2 an=argument; domain="logger.test.example"',
'stringification overload works with DNS names' );
is_deeply(
(decode_json $log->json)->[-1]->{args},
{
an => 'argument',
domain => 'logger.test.example'
},
'entry arguments serialize to JSON' );

is( $entry->level, 'DEBUG', 'right level' );
my $example = Zonemaster::Engine::Logger::Entry->new({ module => 'Basic', tag => 'B02_NS_BROKEN', testcase => 'Basic02' } );
is( $example->level, 'ERROR', 'expected level' );
Expand Down
Loading