Skip to content

Commit

Permalink
Item449: made the Sandbox static. Minor refactoring, but needed doing…
Browse files Browse the repository at this point in the history
… before release because the API is exposed

git-svn-id: http://svn.foswiki.org/trunk@1322 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
CrawfordCurrie authored and CrawfordCurrie committed Dec 13, 2008
1 parent c5da12b commit 82b0b8f
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 178 deletions.
5 changes: 4 additions & 1 deletion TWikiCompatibilityPlugin/lib/TWiki.pm
Expand Up @@ -4,7 +4,10 @@ use Foswiki;

sub TWiki::new {
shift;
return new Foswiki(@_);
my $fatwilly = new Foswiki(@_);
require TWiki::Sandbox;
$fatwilly->{sandbox} = new TWiki::Sandbox();
return $fatwilly;
}

%TWiki::regex = %Foswiki::regex;
Expand Down
11 changes: 7 additions & 4 deletions TWikiCompatibilityPlugin/lib/TWiki/Sandbox.pm
Expand Up @@ -6,12 +6,15 @@ use strict;

use Foswiki::Sandbox;

sub TRACE { 0 }
sub new { Foswiki::Sandbox::new(@_) }
sub finish { Foswiki::Sandbox::finish(@_) }
# Required because TWiki sysCommand is invoked as an object method.
sub new {
my $class = shift;
return bless( {}, $class);
}

sub untaintUnchecked { Foswiki::Sandbox::untaintUnchecked(@_) }
sub normalizeFileName { Foswiki::Sandbox::normalizeFileName(@_) }
sub sanitizeAttachmentName { Foswiki::Sandbox::sanitizeAttachmentName(@_) }
sub sysCommand { Foswiki::Sandbox::sysCommand(@_) }
sub sysCommand { return Foswiki::Sandbox->sysCommand(@_) }

1;
1 change: 0 additions & 1 deletion TWikiCompatibilityPlugin/lib/TWiki/UI.pm
Expand Up @@ -12,7 +12,6 @@ sub execute { Foswiki::UI::execute(@_) }
sub logon { Foswiki::UI::logon(@_) }
sub checkWebExists { Foswiki::UI::checkWebExists(@_) }
sub checkTopicExists { Foswiki::UI::checkTopicExists(@_) }
sub checkMirror { Foswiki::UI::checkMirror(@_) }
sub checkAccess { Foswiki::UI::checkAccess(@_) }
sub readTemplateTopic { Foswiki::UI::readTemplateTopic(@_) }
sub run { Foswiki::UI::run(@_) }
Expand Down
@@ -1,6 +1,6 @@
package TWikiCompatibilityPluginSuite;
use base 'Unit::TestSuite';

sub include_tests { return 'FuncTests' };
sub include_tests { return 'TWikiFuncTests' };

1;
Expand Up @@ -4,7 +4,7 @@ use strict;
# Unit tests for TWiki::Func
#

package FuncTests;
package TWikiFuncTests;
use base qw(FoswikiFnTestCase);

use TWiki;
Expand Down Expand Up @@ -449,7 +449,7 @@ END
$topic, $this->{test_web});
$this->assert($access);
# make sure meta overrides text, as documented - Item2953
my $meta = new TWiki::Meta($this->{twiki}, $this->{test_web}, $topic);
my $meta = new Foswiki::Meta($this->{twiki}, $this->{test_web}, $topic);
$meta->putKeyed('PREFERENCE', {
name => 'ALLOWTOPICVIEW',
title => 'ALLOWTOPICVIEW',
Expand All @@ -461,7 +461,7 @@ END
" * Set ALLOWTOPICVIEW = NotASoul\n",
$topic, $this->{test_web}, $meta);
$this->assert($access);
$meta = new TWiki::Meta($this->{twiki}, $this->{test_web}, $topic);
$meta = new Foswiki::Meta($this->{twiki}, $this->{test_web}, $topic);
$meta->putKeyed('PREFERENCE', {
name => 'DENYTOPICVIEW',
title => 'DENYTOPICVIEW',
Expand Down Expand Up @@ -501,7 +501,7 @@ END
$topic, $this->{test_web});
$this->assert($access);
# make sure meta overrides text, as documented - Item2953
my $meta = new TWiki::Meta($this->{twiki}, $this->{test_web}, $topic);
my $meta = new Foswiki::Meta($this->{twiki}, $this->{test_web}, $topic);
$meta->putKeyed('PREFERENCE', {
name => 'ALLOWTOPICVIEW',
title => 'ALLOWTOPICVIEW',
Expand All @@ -513,7 +513,7 @@ END
" * Set ALLOWTOPICVIEW = NotASoul\n",
$topic, $this->{test_web}, $meta);
$this->assert($access);
$meta = new TWiki::Meta($this->{twiki}, $this->{test_web}, $topic);
$meta = new Foswiki::Meta($this->{twiki}, $this->{test_web}, $topic);
$meta->putKeyed('PREFERENCE', {
name => 'DENYTOPICVIEW',
title => 'DENYTOPICVIEW',
Expand Down
8 changes: 5 additions & 3 deletions UnitTestContrib/test/unit/RcsTests.pm
Expand Up @@ -54,14 +54,16 @@ sub set_up {
die unless (defined $Foswiki::cfg{ScriptSuffix});
$Foswiki::cfg{Register}{AllowLoginName} = 1;
$twiki = new Foswiki();
$twiki->{sandbox}->{TRACE} = 0;
no warnings 'redefine';
*Foswiki::Sandbox::TRACE = sub {};
use warnings 'redefine';
# Switch off pipes to maximise debug opportunities
# The following setting is for debugging and disabled
# since it makes so much noise that normal tests drown
# Note enabling these makes later test cases fail when
# run as FoswikiSuite
#$twiki->{sandbox}->{REAL_SAFE_PIPE_OPEN} = 0;
#$twiki->{sandbox}->{EMULATED_SAFE_PIPE_OPEN} = 0;
#$Foswiki::Sandbox::REAL_SAFE_PIPE_OPEN = 0;
#$Foswiki::Sandbox::EMULATED_SAFE_PIPE_OPEN = 0;

$Foswiki::cfg{WarningFileName} = "$Foswiki::cfg{TempfileDir}/junk";
die unless $twiki;
Expand Down
66 changes: 30 additions & 36 deletions UnitTestContrib/test/unit/RobustnessTests.pm
Expand Up @@ -2,7 +2,7 @@
package RobustnessTests;

use base qw(FoswikiTestCase);
require 5.006;
require 5.008;

use Foswiki;
use Foswiki::Sandbox;
Expand All @@ -14,23 +14,17 @@ my $slash;
sub set_up {
my $this = shift;
$this->SUPER::set_up();
$this->{twiki} = new Foswiki();
$this->{session} = new Foswiki();
$slash = ($Foswiki::cfg{OS} eq 'WINDOWS') ? '\\' : '/';
# NOTE: this test pokes the *shared* sandbox, so we have to be extra
# careful about restoring state. We store the state, rather than
# just destroying the sandbox object, so that we can
# still pick up on potential mod_perl problems in the tests.
$this->{RSPO} = $Foswiki::sandbox->{REAL_SAFE_PIPE_OPEN};
$this->{ESPO} = $Foswiki::sandbox->{EMULATED_SAFE_PIPE_OPEN};
Foswiki::Sandbox::_assessPipeSupport();
}

sub tear_down {
my $this = shift;
# NOTE: this test pokes the *shared* sandbox, so we have to be extra
# NOTE: this test pokes globals in the sandbox, so we have to be extra
# careful about restoring state.
$Foswiki::sandbox->{REAL_SAFE_PIPE_OPEN} = $this->{RSPO};
$Foswiki::sandbox->{EMULATED_SAFE_PIPE_OPEN} = $this->{ESPO};
$this->{twiki}->finish();
Foswiki::Sandbox::_assessPipeSupport();
$this->{session}->finish();
$this->SUPER::tear_down();
}

Expand Down Expand Up @@ -119,54 +113,54 @@ sub test_sanitizeAttachmentName {
sub test_buildCommandLine {
my $this = shift;
$this->assert_deep_equals(["a", "b", "c"],
[$Foswiki::sandbox->_buildCommandLine("a b c", ())]);
[Foswiki::Sandbox::_buildCommandLine("a b c", ())]);
$this->assert_deep_equals(["a", "b", "c"],
[$Foswiki::sandbox->_buildCommandLine(" a b c ", ())]);
[Foswiki::Sandbox::_buildCommandLine(" a b c ", ())]);
$this->assert_deep_equals([1, 2, 3],
[$Foswiki::sandbox->_buildCommandLine(" %A% %B% %C% ", (A => 1, B => 2, C => 3))]);
[Foswiki::Sandbox::_buildCommandLine(" %A% %B% %C% ", (A => 1, B => 2, C => 3))]);
$this->assert_deep_equals([1, "./-..", "a${slash}b"],
[$Foswiki::sandbox->_buildCommandLine(" %A|U% %B|F% %C|F% ", (A => 1, B => "-..", C => "a/b"))]);
[Foswiki::Sandbox::_buildCommandLine(" %A|U% %B|F% %C|F% ", (A => 1, B => "-..", C => "a/b"))]);
$this->assert_deep_equals([1, "2:3"],
[$Foswiki::sandbox->_buildCommandLine(" %A% %B%:%C% ", (A => 1, B => 2, C => 3))]);
[Foswiki::Sandbox::_buildCommandLine(" %A% %B%:%C% ", (A => 1, B => 2, C => 3))]);
$this->assert_deep_equals([1, "-n2:3"],
[$Foswiki::sandbox->_buildCommandLine(" %A% -n%B%:%C% ", (A => 1, B => 2, C => 3))]);
[Foswiki::Sandbox::_buildCommandLine(" %A% -n%B%:%C% ", (A => 1, B => 2, C => 3))]);
$this->assert_deep_equals([1, "-r2:HEAD", 3],
[$Foswiki::sandbox->_buildCommandLine(" %A% -r%B%:HEAD %C% ", (A => 1, B => 2, C => 3))]);
[Foswiki::Sandbox::_buildCommandLine(" %A% -r%B%:HEAD %C% ", (A => 1, B => 2, C => 3))]);
$this->assert_deep_equals(["a", "b", "${slash}c"],
[$Foswiki::sandbox->_buildCommandLine(" %A|F% ", (A => ["a", "b", "/c"]))]);
[Foswiki::Sandbox::_buildCommandLine(" %A|F% ", (A => ["a", "b", "/c"]))]);

$this->assert_deep_equals(
["1", "2.3", "4", 'str-.+_ing', "-09AZaz.+_"],
[$Foswiki::sandbox->_buildCommandLine(" %A|N% %B|S% %C|S%",
[Foswiki::Sandbox::_buildCommandLine(" %A|N% %B|S% %C|S%",
(A => [1, 2.3, 4], B => 'str-.+_ing', C => "-09AZaz.+_"))]);

$this->assert_deep_equals(["2004/11/20 09:57:41"],
[$Foswiki::sandbox->_buildCommandLine("%A|D%", A => Foswiki::Time::formatTime (1100944661, '$rcs', 'gmtime'))]);
eval { $Foswiki::sandbox->_buildCommandLine('%A|%') };
[Foswiki::Sandbox::_buildCommandLine("%A|D%", A => Foswiki::Time::formatTime (1100944661, '$rcs', 'gmtime'))]);
eval { Foswiki::Sandbox::_buildCommandLine('%A|%') };
$this->assert_not_null($@, '');
eval { $Foswiki::sandbox->_buildCommandLine('%A|X%') };
eval { Foswiki::Sandbox::_buildCommandLine('%A|X%') };
$this->assert_not_null($@, '');
eval { $Foswiki::sandbox->_buildCommandLine(' %A|N% ', A => '2/3') };
eval { Foswiki::Sandbox::_buildCommandLine(' %A|N% ', A => '2/3') };
$this->assert_not_null($@, '');
eval { $Foswiki::sandbox->_buildCommandLine(' %A|S% ', A => '2/3') };
eval { Foswiki::Sandbox::_buildCommandLine(' %A|S% ', A => '2/3') };
$this->assert_not_null($@, '');
}

sub verify {
my $this = shift;
my($out, $exit) = $Foswiki::sandbox->sysCommand(
my($out, $exit) = Foswiki::Sandbox->sysCommand(
'sh -c %A%', A => 'echo OK; echo BOSS');
$this->assert_str_equals("OK\nBOSS\n", $out);
$this->assert_equals(0, $exit);
($out, $exit) = $Foswiki::sandbox->sysCommand(
($out, $exit) = Foswiki::Sandbox->sysCommand(
'sh -c %A%', A => 'echo JUNK ON STDERR 1>&2');
$this->assert_equals(0, $exit);
$this->assert_str_equals("", $out);
($out, $exit) = $Foswiki::sandbox->sysCommand(
($out, $exit) = Foswiki::Sandbox->sysCommand(
'test %A% %B% %C%', A => '1', B=>'-eq', C=>'2');
$this->assert_equals(1, $exit, $exit.' '.$out);
$this->assert_str_equals("", $out);
( $out, $exit) = $Foswiki::sandbox->sysCommand(
( $out, $exit) = Foswiki::Sandbox->sysCommand(
'sh -c %A%', A => 'echo urmf; exit 7');
$this->assert($exit != 0);
$this->assert_str_equals("urmf\n", $out);
Expand All @@ -175,24 +169,24 @@ sub verify {
sub test_executeRSP {
my $this = shift;
return if $Foswiki::cfg{OS} eq 'WINDOWS';
$Foswiki::sandbox->{REAL_SAFE_PIPE_OPEN} = 1;
$Foswiki::sandbox->{EMULATED_SAFE_PIPE_OPEN} = 0;
$Foswiki::Sandbox::REAL_SAFE_PIPE_OPEN = 1;
$Foswiki::Sandbox::EMULATED_SAFE_PIPE_OPEN = 0;
$this->verify();
}

sub test_executeESP {
my $this = shift;
return if $Foswiki::cfg{OS} eq 'WINDOWS';
$Foswiki::sandbox->{REAL_SAFE_PIPE_OPEN} = 0;
$Foswiki::sandbox->{EMULATED_SAFE_PIPE_OPEN} = 1;
$Foswiki::Sandbox::REAL_SAFE_PIPE_OPEN = 0;
$Foswiki::Sandbox::EMULATED_SAFE_PIPE_OPEN = 1;
$this->verify();
}

sub test_executeNSP {
my $this = shift;
return if $Foswiki::cfg{OS} eq 'WINDOWS';
$Foswiki::sandbox->{REAL_SAFE_PIPE_OPEN} = 0;
$Foswiki::sandbox->{EMULATED_SAFE_PIPE_OPEN} = 0;
$Foswiki::Sandbox::REAL_SAFE_PIPE_OPEN = 0;
$Foswiki::Sandbox::EMULATED_SAFE_PIPE_OPEN = 0;
$this->verify();
}

Expand Down
10 changes: 1 addition & 9 deletions core/lib/Foswiki.pm
Expand Up @@ -24,7 +24,6 @@ with CGI accelerators such as mod_perl.
* =remoteUser= Login ID when using ApacheLogin. Maintained for
compatibility only, do not use.
* =requestedWebName= Name of web found in URL path or =web= URL parameter
* =sandbox= Foswiki::Sandbox singleton
* =scriptUrlPath= URL path to the current script. May be dynamically
extracted from the URL path if {GetScriptUrlFromCgi}.
Only required to support {GetScriptUrlFromCgi} and
Expand Down Expand Up @@ -1290,7 +1289,7 @@ sub new {
$initialContext ||= defined($query) ? {} : { command_line => 1 };

$query ||= new Foswiki::Request();
my $this = bless( {}, $class );
my $this = bless( { sandbox => 'Foswiki::Sandbox' }, $class );
$this->{request} = $query;
$this->{cgiQuery} = $query; # for backwards compatibility in contribs
$this->{response} = new Foswiki::Response();
Expand All @@ -1305,13 +1304,6 @@ sub new {
$this->{_HTMLHEADERS} = {};
$this->{context} = $initialContext;

# create the various sub-objects
unless ($sandbox) {

# "shared" between mod_perl instances
$sandbox =
new Foswiki::Sandbox( $Foswiki::cfg{OS}, $Foswiki::cfg{DetailedOS} );
}
require Foswiki::Plugins;
$this->{plugins} = new Foswiki::Plugins($this);
require Foswiki::Store;
Expand Down
9 changes: 3 additions & 6 deletions core/lib/Foswiki/Render.pm
Expand Up @@ -443,7 +443,7 @@ sub makeAnchorName {
$anchorName =~ s/<\/?[a-zA-Z][^>]*>//gi; # remove HTML tags
$anchorName =~ s/&#?[a-zA-Z0-9]+;//g; # remove HTML entities
$anchorName =~ s/&//g; # remove &
# filter TOC excludes if not at beginning
# filter TOC excludes if not at beginning
$anchorName =~ s/^(.+?)\s*$Foswiki::regex{headerPatternNoTOC}.*/$1/o;

# filter '!!', '%NOTOC%'
Expand All @@ -460,15 +460,12 @@ sub makeAnchorName {
if ( !$compatibilityMode ) {
$anchorName =~ s/^[\s#_]+//; # no leading space nor '#', '_'
}
$anchorName =~ s/^(.{32})(.*)$/$1/
; # limit to 32 chars - FIXME: Use Unicode chars before truncate
# limit to 32 chars - FIXME: Use Unicode chars before truncate
$anchorName =~ s/^(.{32})(.*)$/$1/;
if ( !$compatibilityMode ) {
$anchorName =~ s/[\s_]+$//; # no trailing space, nor '_'
}

# There should be no need to encode 8-bit characters in anchor
# due to UTF-8 URL support. However encoding apparently cures Item5962

return Foswiki::urlEncode( $anchorName );
}

Expand Down

0 comments on commit 82b0b8f

Please sign in to comment.