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 3 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
31 changes: 30 additions & 1 deletion t/rrlist.t
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
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_a = Zonemaster::LDNS::RRList->new();
my $empty_b = Zonemaster::LDNS::RRList->new([]);
my $nonempty = Zonemaster::LDNS::RRList->new([
Zonemaster::LDNS::RR->new_from_string('test. 0 IN TXT "hello"')
]);

isa_ok($empty_a, 'Zonemaster::LDNS::RRList');
isa_ok($empty_b, 'Zonemaster::LDNS::RRList');
isa_ok($nonempty, 'Zonemaster::LDNS::RRList');

eq_or_diff( $empty_a->string, '', "stringifying an empty list gives empty string" );
ok( $empty_a eq $empty_b, "two distinct empty RRLists are equal to each other" );
ok( $empty_a ne $nonempty, "an empty RRlist is not equal to a non-empty RRlist" );

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

is( $empty_a->count(), 0, "count() on empty list is 0" );

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

ok( !$empty_a->is_rrset(), "an empty list is not an RRset" );
ok( !$empty_b->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