Fix building on perls with no . in @INC
[dbsrgits/DBIx-Class.git] / xt / extra / taint.t
CommitLineData
c0329273 1BEGIN { $ENV{DBICTEST_ANFANG_DEFANG} = 1 }
2
2ff02982 3# When in taint mode, PERL5LIB is ignored (but *not* unset)
4# Put it back in INC so that local-lib users can actually
5# run this test. Use lib.pm instead of an @INC unshift as
6# it will correctly add any arch subdirs encountered
7#
8# Yes, this is a lazy solution: adding -I args in the exec below is the
9# more sensible approach, but no time to properly do it at present
10use Config;
11use lib (
12 grep { length }
13 map { split /\Q$Config{path_sep}\E/, (/^(.*)$/)[0] } # untainting regex
14 grep { defined }
15 @ENV{qw(PERL5LIB PERLLIB)} # precedence preserved by lib
16);
17
83542a7d 18use strict;
19use warnings;
ade96c1f 20
21# there is talk of possible perl compilations where -T is fatal or just
22# doesn't work. We don't want to have the user deal with that.
8aae7940 23BEGIN { unless ($INC{'DBICTest/WithTaint.pm'}) {
ade96c1f 24
a5741f5c 25 if ( $^O eq 'MSWin32' and $^X =~ /\x20/ ) {
26 print "1..0 # SKIP Running this test on Windows with spaces within the perl executable path (\$^X) is not possible due to https://rt.perl.org/Ticket/Display.html?id=123907\n";
27 exit 0;
28 }
29
ade96c1f 30 # it is possible the test itself is initially invoked in taint mode
31 # and with relative paths *and* with a relative $^X and some other
32 # craziness... in short: just be proactive
33 require File::Spec;
66a65d81 34
35 if (length $ENV{PATH}) {
36 ( $ENV{PATH} ) = join ( $Config{path_sep},
37 map { length($_) ? File::Spec->rel2abs($_) : () }
38 split /\Q$Config{path_sep}/, $ENV{PATH}
39 ) =~ /\A(.+)\z/;
40 }
41
42 my ($perl) = $^X =~ /\A(.+)\z/;
ade96c1f 43
44 {
45 local $ENV{PATH} = "/nosuchrootbindir";
46 system( $perl => -T => -e => '
47 use warnings;
48 use strict;
49 eval { my $x = $ENV{PATH} . (kill (0)); 1 } or exit 42;
50 exit 0;
51 ');
52 }
53
54 if ( ($? >> 8) != 42 ) {
55 print "1..0 # SKIP Your perl does not seem to like/support -T...\n";
56 exit 0;
57 }
58
8aae7940 59 exec( $perl, qw( -It/lib -MDBICTest::WithTaint -T ), __FILE__ );
ade96c1f 60}}
83542a7d 61
6fc6d60c 62# We need to specify 'lib' here as well because even if it was already in
63# @INC, the above will have put our local::lib in front of it, so now an
64# installed DBIx::Class will take precedence over the one we're trying to test.
65# In some cases, prove will have supplied ./lib as an absolute path so it
66# doesn't seem worth trying to remove the second copy since it won't hurt
67# anything.
68use lib qw(t/lib lib);
69
91b0ad0b 70use Test::More;
f54428ab 71use Test::Exception;
8d6b1478 72use DBICTest;
83542a7d 73
f54428ab 74throws_ok (
75 sub { $ENV{PATH} . (kill (0)) },
76 qr/Insecure dependency in kill/,
77 'taint mode active'
66a65d81 78) if length $ENV{PATH};
83542a7d 79
f54428ab 80{
81 package DBICTest::Taint::Classes;
83542a7d 82
f54428ab 83 use Test::More;
84 use Test::Exception;
91b0ad0b 85
f54428ab 86 use base qw/DBIx::Class::Schema/;
91b0ad0b 87
f54428ab 88 lives_ok (sub {
89 __PACKAGE__->load_classes(qw/Manual/);
90 ok( __PACKAGE__->source('Manual'), 'The Classes::Manual source has been registered' );
91 __PACKAGE__->_unregister_source (qw/Manual/);
92 }, 'Loading classes with explicit load_classes worked in taint mode' );
91b0ad0b 93
f54428ab 94 lives_ok (sub {
95 __PACKAGE__->load_classes();
96 ok( __PACKAGE__->source('Auto'), 'The Classes::Auto source has been registered' );
97 ok( __PACKAGE__->source('Auto'), 'The Classes::Manual source has been re-registered' );
98 }, 'Loading classes with Module::Find/load_classes worked in taint mode' );
99}
83542a7d 100
f54428ab 101{
102 package DBICTest::Taint::Namespaces;
83542a7d 103
f54428ab 104 use Test::More;
105 use Test::Exception;
91b0ad0b 106
f54428ab 107 use base qw/DBIx::Class::Schema/;
83542a7d 108
f54428ab 109 lives_ok (sub {
110 __PACKAGE__->load_namespaces();
111 ok( __PACKAGE__->source('Test'), 'The Namespaces::Test source has been registered' );
112 }, 'Loading classes with Module::Find/load_namespaces worked in taint mode' );
113}
91b0ad0b 114
f2d11f4a 115# check that we can create a database and all
116{
117 my $s = DBICTest->init_schema( sqlite_use_file => 1 );
118 my $art = $s->resultset('Artist')->search({}, {
119 prefetch => 'cds', order_by => 'artistid',
120 })->next;
121 is ($art->artistid, 1, 'got artist');
122}
123
f54428ab 124done_testing;