Fix building on perls with no . in @INC
[dbsrgits/DBIx-Class.git] / xt / extra / taint.t
1 BEGIN { $ENV{DBICTEST_ANFANG_DEFANG} = 1 }
2
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
10 use Config;
11 use 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
18 use strict;
19 use warnings;
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.
23 BEGIN { unless ($INC{'DBICTest/WithTaint.pm'}) {
24
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
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;
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/;
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
59   exec( $perl, qw( -It/lib -MDBICTest::WithTaint -T ), __FILE__ );
60 }}
61
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.
68 use lib qw(t/lib lib);
69
70 use Test::More;
71 use Test::Exception;
72 use DBICTest;
73
74 throws_ok (
75   sub { $ENV{PATH} . (kill (0)) },
76   qr/Insecure dependency in kill/,
77   'taint mode active'
78 ) if length $ENV{PATH};
79
80 {
81   package DBICTest::Taint::Classes;
82
83   use Test::More;
84   use Test::Exception;
85
86   use base qw/DBIx::Class::Schema/;
87
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' );
93
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 }
100
101 {
102   package DBICTest::Taint::Namespaces;
103
104   use Test::More;
105   use Test::Exception;
106
107   use base qw/DBIx::Class::Schema/;
108
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 }
114
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
124 done_testing;