Skip to content

Commit

Permalink
fix tests that were accessing the network; closes #32
Browse files Browse the repository at this point in the history
  • Loading branch information
dwarring committed Dec 8, 2019
1 parent 92638b8 commit 56dcc82
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
2 changes: 2 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
- [LibXML::{Pattern|Dtd|Schema|RelaxNg|RegExp}]
Added ACCEPTS() multi methods for more Rakuish matching, e.g.:
$node ~~ $pattern; $doc ~~ $dtd; "abc" ~~ $regexp
- [t/26schema.t][t/43options.t] fix tests that were accessing the network #32
(were failing when http/https was not available).
0.2.3 2019-12-05
- [dom.c][LibXML::Node] take out case conversion from domXPathGetKey()
.xpath-key() to case handling semantics. tags are converted to
Expand Down
2 changes: 1 addition & 1 deletion lib/LibXML/Schema.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ my class Parser::Context {

if $ext-loader-changed {
xmlExternalEntityLoader::set-networked(+!$!network.so);
}
}

self.flush-errors;
$rv;
Expand Down
15 changes: 10 additions & 5 deletions t/26schema.t
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use LibXML::Schema;
use LibXML::InputCallback;
use LibXML::Element;

plan 16;
plan 18;

sub slurp(Str $_) { .IO.slurp }

Expand Down Expand Up @@ -93,9 +93,10 @@ EOF
# 5 check that :network works

# guard against actual network access attempts
my Bool $net-access = False;
my LibXML::InputCallback $input-callbacks .= new: :callbacks{
:match(sub ($f) {return $f.IO.e }),
:open(sub ($f) {$f.IO.open(:r) }),
:match(sub ($f) {True}),
:open(sub ($_) { (/^http[s?]':'/ ?? do { $net-access++; 'test/empty.txt' } !! $_).IO.open(:r); }),
:read(sub ($fh, $n) {$fh.read($n)}),
:close(sub ($fh) {$fh.close}),
};
Expand All @@ -117,9 +118,11 @@ EOF
nok( defined($schema), 'Schema from buffer with external import and !network is not loaded.' );
}

nok $net-access, 'no attempted network access';

{
my $schema = try { LibXML::Schema.new( location => $netfile, :network, :suppress-warnings ); };
ok ! $!.defined, "no exception";
like $!, /'Document is empty'/, 'location :network access';
}
{
my $schema = try { LibXML::Schema.new( string => q:to<EOF>, :network, :suppress-warnings ) };
Expand All @@ -128,7 +131,9 @@ EOF
<xsd:import namespace="http://example.com/namespace" schemaLocation="http://example.com/xml.xsd"/>
</xsd:schema>
EOF
ok ! $!.defined, "no exception";
like $!, /'Document is empty'/, 'string :network access';
}

ok $net-access, 'attempted network access';

$input-callbacks.deactivate;
12 changes: 7 additions & 5 deletions t/43options.t
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use v6;
use Test;
plan 330;
plan 332;

use LibXML;
use LibXML::InputCallback;
Expand Down Expand Up @@ -100,12 +100,13 @@ chomp($sys_line);
"expand_entities is preserved after _clone()/etc."
);
my Bool $net-access = False;
# now check network access
$XML ~~ s,'file://',http://example.com,;
# guard against actual network access attempts
my LibXML::InputCallback $input-callbacks .= new: :callbacks{
:match(sub ($f) {return $f.IO.e }),
:open(sub ($f) {$f.IO.open(:r) }),
:match(sub ($f) {True }),
:open(sub ($_) { (/^http[s?]':'/ ?? do { $net-access++; 'test/empty.txt' } !! $_).IO.open(:r); }),
:read(sub ($fh, $n) {$fh.read($n)}),
:close(sub ($fh) {$fh.close}),
};
Expand All @@ -115,13 +116,14 @@ chomp($sys_line);
is-deeply $parser.network, False;
$parser.load_ext_dtd = True;
$parser.expand_entities = True;
## warn $parser.flags;
is-deeply $parser.network, False;
try { $parser.load: string => $XML };
like( $!, /'I/O error : Attempt to load network entity'/, 'Entity from network location throw error.' );
nok $net-access, 'no attempted network access';
$parser.network = True;
try { $parser.load: string => $XML };
like( $!, /'I/O warning : failed to load HTTP resource'/, 'Entity from network location throw error.' );
ok ! $!.defined, 'attempted network access';
ok $net-access, 'attempted network access';
}
Expand Down
Empty file added test/empty.txt
Empty file.

0 comments on commit 56dcc82

Please sign in to comment.