Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow construction of empty Zonemaster::LDNS::RRList objects #209

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions lib/Zonemaster/LDNS/RRList.pm
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ Zonemaster::LDNS::RRList - class representing lists of resource records.

=over

=item new()

Creates a new empty L<Zonemaster::LDNS::RRList> object.

=back

=item new($rrs)

Creates a new L<Zonemaster::LDNS::RRList> object for the given resource records.
Expand Down
45 changes: 22 additions & 23 deletions src/LDNS.xs
Original file line number Diff line number Diff line change
Expand Up @@ -1553,35 +1553,34 @@ packet_CLONE(class)
MODULE = Zonemaster::LDNS PACKAGE = Zonemaster::LDNS::RRList PREFIX=rrlist_

SV *
rrlist_new(objclass,rrs_in)
rrlist_new(objclass, ...)
char* objclass;
AV *rrs_in;
CODE:
{
size_t i;
AV *rrs_in = NULL;
ldns_rr_list *rrs = ldns_rr_list_new();

if(av_len(rrs_in)==-1)
{
croak("List is empty");
}

/* Take RRs out of the array and stick them in a list */
for(i = 0; i <= av_len(rrs_in); ++i)
{
ldns_rr *rr;
SV **rrsv = av_fetch(rrs_in,i,1);
if (rrsv != NULL && sv_isobject(*rrsv) && sv_derived_from(*rrsv, "Zonemaster::LDNS::RR")) {
SvGETMAGIC(*rrsv);
IV tmp = SvIV((SV*)SvRV(*rrsv));
rr = INT2PTR(ldns_rr *,tmp);
if(rr != NULL)
{
ldns_rr_list_push_rr(rrs, ldns_rr_clone(rr));
if (items > 1) {
SSize_t i;
rrs_in = (AV *) SvRV(ST(1));

for(i = 0; i <= av_len(rrs_in); ++i)
{
ldns_rr *rr;
SV **rrsv = av_fetch(rrs_in,i,1);
if (rrsv != NULL && sv_isobject(*rrsv) && sv_derived_from(*rrsv, "Zonemaster::LDNS::RR")) {
SvGETMAGIC(*rrsv);
IV tmp = SvIV((SV*)SvRV(*rrsv));
rr = INT2PTR(ldns_rr *,tmp);
if(rr != NULL)
{
ldns_rr_list_push_rr(rrs, ldns_rr_clone(rr));
}
}
else {
croak("Incorrect type in list");
}
}
else {
croak("Incorrect type in list");
}
}

Expand Down Expand Up @@ -1675,7 +1674,7 @@ rrlist_string(obj)
Zonemaster::LDNS::RRList obj;
CODE:
RETVAL = ldns_rr_list2str(obj);
if(RETVAL == NULL || RETVAL[0] == '\0')
if(RETVAL == NULL)
{
croak("Failed to convert RRList to string");
}
Expand Down
40 changes: 39 additions & 1 deletion t/rrlist.t
Original file line number Diff line number Diff line change
@@ -1,10 +1,48 @@
use strict;
use warnings;

use Test::More;
use Test::Exception;
use Test::Differences;
use Devel::Peek;

use Zonemaster::LDNS;

subtest "Empty RRList" => sub {
my $empty_impl = Zonemaster::LDNS::RRList->new();
my $empty_expl = Zonemaster::LDNS::RRList->new([]);
my $singleton = Zonemaster::LDNS::RRList->new([
Zonemaster::LDNS::RR->new_from_string('test. 0 IN TXT "hello"')
]);

isa_ok($empty_impl, 'Zonemaster::LDNS::RRList');
isa_ok($empty_expl, 'Zonemaster::LDNS::RRList');
isa_ok($singleton, 'Zonemaster::LDNS::RRList');

# We really want to make sure that new() and new([]) have the same
# semantics.

ok( $empty_impl eq $empty_expl, "two distinct empty RRLists are equal to each other" );
ok( $empty_expl eq $empty_impl, "eq on two empty lists is commutative" );

eq_or_diff( $empty_impl->string, '', "stringifying an implicitly empty list gives empty string" );
eq_or_diff( $empty_expl->string, '', "stringifying an explicitly empty list gives empty string" );

ok( $empty_impl ne $singleton, "the implicitly empty list isn’t equal to a non-empty one" );
ok( $empty_expl ne $singleton, "the explicitly empty list isn’t equal to a non-empty one" );

$singleton->pop();
ok( $empty_impl eq $singleton, "now both lists are empty" );

is( $empty_impl->count(), 0, "count() on implicitly empty list is 0" );
is( $empty_expl->count(), 0, "count() on explicitly empty list is 0" );

is( $empty_impl->get(0), undef, "get(0) on empty list gives undef" );
is( $empty_impl->get(42), undef, "get(42) on empty list also gives undef" );

ok( !$empty_impl->is_rrset(), "an empty list is not an RRset" );
ok( !$empty_expl->is_rrset(), "an empty list is not an RRset" );
};

subtest "Good RRList" => sub {
my $rr1 = Zonemaster::LDNS::RR->new_from_string( 'example. 10 IN NS ns1.example.' );
my $rr2 = Zonemaster::LDNS::RR->new_from_string( 'example. 10 IN NS ns2.example.' );
Expand Down