Skip to content

Commit

Permalink
Merge pull request #17 from thornag/1.1.x-windows
Browse files Browse the repository at this point in the history
Windows support
  • Loading branch information
michael-donat committed Apr 29, 2014
2 parents 76c3784 + b24d07e commit 9a4524b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/VirtualFileSystem/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function root()
*/
public function fileAt($path)
{
$pathParts = array_filter(explode('/', $path), 'strlen');
$pathParts = array_filter(explode('/', str_replace('\\', '/', $path)), 'strlen');

$node = $this->root();

Expand Down
4 changes: 2 additions & 2 deletions src/VirtualFileSystem/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class Factory
*/
public function __construct()
{
$this->userid = posix_getuid();
$this->groupid = posix_getgid();
$this->userid = function_exists('posix_getuid') ? posix_getuid() : 0;
$this->groupid = function_exists('posix_getgid') ? posix_getgid() : 0;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/VirtualFileSystem/Wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ public function stream_metadata($path, $option, $value)
);
return false;
}
$uid = posix_getpwnam($value)['uid'];
$uid = function_exists('posix_getpwnam') ? posix_getpwnam($value)['uid'] : 0;
$node->chown($uid);
$node->setChangeTime(time());
break;
Expand All @@ -449,7 +449,7 @@ public function stream_metadata($path, $option, $value)
);
return false;
}
$gid = posix_getgrnam($value)['gid'];
$gid = function_exists('posix_getgrnam') ? posix_getgrnam($value)['gid'] : 0;
$node->chgrp($gid);
$node->setChangeTime(time());
break;
Expand Down
4 changes: 2 additions & 2 deletions src/VirtualFileSystem/Wrapper/PermissionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class PermissionHelper
*/
public function __construct($uid = null, $gid = null)
{
$this->userid = is_null($uid) ? posix_getuid() : $uid;
$this->groupid = is_null($gid) ? posix_getgid() : $gid;
$this->userid = is_null($uid) ? (function_exists('posix_getuid') ? posix_getuid() : 0) : $uid;
$this->groupid = is_null($gid) ? ((function_exists('posix_getgid') ? posix_getgid() : 0)) : $gid;
}

/**
Expand Down
28 changes: 18 additions & 10 deletions tests/VirtualFileSystem/Wrapper/PermissionHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@

class PermissionHelperTest extends \PHPUnit_Framework_TestCase
{
protected $uid;
protected $gid;

public function setUp() {
$this->uid = function_exists('posix_getuid') ? posix_getuid() : 0;
$this->gid = function_exists('posix_getgid') ? posix_getgid() : 0;
}

public function testUserPermissionsAreCalculatedCorrectly()
{

$file = new File('file');
$file->chown(posix_getuid());
$file->chown($this->uid);

$ph = new PermissionHelper();
$ph->setNode($file);
Expand Down Expand Up @@ -50,7 +58,7 @@ public function testUserPermissionsAreCalculatedCorrectly()
public function testGroupPermissionsAreCalculatedCorrectly()
{
$file = new File('file');
$file->chgrp(posix_getgid());
$file->chgrp($this->gid);

$ph = new PermissionHelper();
$ph->setNode($file);
Expand Down Expand Up @@ -147,43 +155,43 @@ public function testIsReadable()
$this->assertTrue($ph->isReadable(), 'File is readable root:root 0004');

$file->chmod(0000);
$file->chown(posix_getuid());
$file->chown($this->uid);
$file->chgrp(0);
$this->assertFalse($ph->isReadable(), 'File is not readable user:root 0000');

$file->chmod(0400);
$file->chown(posix_getuid());
$file->chown($this->uid);
$file->chgrp(0);
$this->assertTrue($ph->isReadable(), 'File is readable user:root 0400');

$file->chmod(0040);
$file->chown(posix_getuid());
$file->chown($this->uid);
$file->chgrp(0);
$this->assertFalse($ph->isReadable(), 'File is not readable user:root 0040');

$file->chmod(0004);
$file->chown(posix_getuid());
$file->chown($this->uid);
$file->chgrp(0);
$this->assertTrue($ph->isReadable(), 'File is readable user:root 0004');

$file->chmod(0000);
$file->chown(0);
$file->chgrp(posix_getgid());
$file->chgrp($this->gid);
$this->assertFalse($ph->isReadable(), 'File is not readable root:user 0000');

$file->chmod(0040);
$file->chown(0);
$file->chgrp(posix_getgid());
$file->chgrp($this->gid);
$this->assertTrue($ph->isReadable(), 'File is readable root:user 0040');

$file->chmod(0400);
$file->chown(0);
$file->chgrp(posix_getgid());
$file->chgrp($this->gid);
$this->assertFalse($ph->isReadable(), 'File is not readable root:user 0400');

$file->chmod(0004);
$file->chown(0);
$file->chgrp(posix_getgid());
$file->chgrp($this->gid);
$this->assertTrue($ph->isReadable(), 'File is readable root:user 0004');
}
}
51 changes: 28 additions & 23 deletions tests/VirtualFileSystem/WrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@

class WrapperTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
protected $uid;
protected $gid;

public function setUp() {
$this->uid = function_exists('posix_getuid') ? posix_getuid() : 0;
$this->gid = function_exists('posix_getgid') ? posix_getgid() : 0;

@$na['n/a']; //putting error in known state
}

Expand Down Expand Up @@ -96,9 +101,9 @@ public function testChmod()

public function testChownByName()
{
if (posix_getuid() == 0) {
if ($this->uid == 0) {
$this->markTestSkipped(
'No point testing if user is already root. \Php unit shouldn\'t be run as root user.'
'No point testing if user is already root. \Php unit shouldn\'t be run as root user. (Unless you are a windows user!)'
);
}

Expand All @@ -116,7 +121,7 @@ public function testChownByName()

public function testChownById()
{
if (posix_getuid() == 0) {
if ($this->uid == 0) {
$this->markTestSkipped(
'No point testing if user is already root. Php unit shouldn\'t be run as root user.'
);
Expand All @@ -133,9 +138,9 @@ public function testChownById()

public function testChgrpByName()
{
if (posix_getgid() == 0) {
if ($this->uid == 0) {
$this->markTestSkipped(
'No point testing if group is already root. Php unit shouldn\'t be run as root group.'
'No point testing if group is already root. Php unit shouldn\'t be run as root group. (Unless you are on Windows - then we skip)'
);
}

Expand All @@ -153,9 +158,9 @@ public function testChgrpByName()

public function testChgrpById()
{
if (posix_getgid() == 0) {
if ($this->gid == 0) {
$this->markTestSkipped(
'No point testing if group is already root. Php unit shouldn\'t be run as root group.'
'No point testing if group is already root. Php unit shouldn\'t be run as root group. (Unless you are on Windows - then we skip)'
);
}

Expand Down Expand Up @@ -830,7 +835,7 @@ public function testPermissionsAreCheckedWhenOpeningFiles()
$this->assertFalse($wr->stream_open($fs->path('/file'), 'a+', 0, $path));

$file->chmod(0400);
$file->chown(posix_getuid());
$file->chown($this->uid);
$file->chgrp(0);
$this->assertTrue($wr->stream_open($fs->path('/file'), 'r', 0, $path));
$this->assertFalse($wr->stream_open($fs->path('/file'), 'r+', 0, $path));
Expand All @@ -840,7 +845,7 @@ public function testPermissionsAreCheckedWhenOpeningFiles()
$this->assertFalse($wr->stream_open($fs->path('/file'), 'a+', 0, $path));

$file->chmod(0200);
$file->chown(posix_getuid());
$file->chown($this->uid);
$file->chgrp(0);
$this->assertFalse($wr->stream_open($fs->path('/file'), 'r', 0, $path));
$this->assertFalse($wr->stream_open($fs->path('/file'), 'r+', 0, $path));
Expand All @@ -850,7 +855,7 @@ public function testPermissionsAreCheckedWhenOpeningFiles()
$this->assertFalse($wr->stream_open($fs->path('/file'), 'a+', 0, $path));

$file->chmod(0600);
$file->chown(posix_getuid());
$file->chown($this->uid);
$file->chgrp(0);
$this->assertTrue($wr->stream_open($fs->path('/file'), 'r', 0, $path));
$this->assertTrue($wr->stream_open($fs->path('/file'), 'r+', 0, $path));
Expand Down Expand Up @@ -879,7 +884,7 @@ public function testTemporaryFileCreatedToReadDirectoriesWithStreamOpenInheritsP
$this->assertFalse($wr->stream_open($fs->path('/dir'), 'a+', 0, $path));

$file->chmod(0400);
$file->chown(posix_getuid());
$file->chown($this->uid);
$file->chgrp(0);
$this->assertTrue($wr->stream_open($fs->path('/dir'), 'r', 0, $path));
$this->assertFalse($wr->stream_open($fs->path('/dir'), 'r+', 0, $path));
Expand All @@ -889,7 +894,7 @@ public function testTemporaryFileCreatedToReadDirectoriesWithStreamOpenInheritsP
$this->assertFalse($wr->stream_open($fs->path('/dir'), 'a+', 0, $path));

$file->chmod(0200);
$file->chown(posix_getuid());
$file->chown($this->uid);
$file->chgrp(0);
$this->assertFalse($wr->stream_open($fs->path('/dir'), 'r', 0, $path));
$this->assertFalse($wr->stream_open($fs->path('/dir'), 'r+', 0, $path));
Expand All @@ -899,7 +904,7 @@ public function testTemporaryFileCreatedToReadDirectoriesWithStreamOpenInheritsP
$this->assertFalse($wr->stream_open($fs->path('/dir'), 'a+', 0, $path));

$file->chmod(0600);
$file->chown(posix_getuid());
$file->chown($this->uid);
$file->chgrp(0);
$this->assertTrue($wr->stream_open($fs->path('/dir'), 'r', 0, $path));
$this->assertFalse($wr->stream_open($fs->path('/dir'), 'r+', 0, $path));
Expand All @@ -922,18 +927,18 @@ public function testPermissionsAreCheckedWhenOpeningDirectories()
$this->assertFalse(@$wr->dir_opendir($fs->path('/dir'), 0));

$file->chmod(0200);
$file->chown(posix_getuid());
$file->chown($this->uid);
$file->chgrp(0);
$this->assertFalse(@$wr->dir_opendir($fs->path('/dir'), 0));

$file->chmod(0400);
$file->chown(posix_getuid());
$file->chown($this->uid);
$file->chgrp(0);
$this->assertTrue(@$wr->stream_open($fs->path('/dir'), 'r', 0, $path));

$file->chmod(0040);
$file->chown(0);
$file->chgrp(posix_getgid());
$file->chgrp($this->gid);
$this->assertTrue(@$wr->stream_open($fs->path('/dir'), 'r', 0, $path));
}

Expand Down Expand Up @@ -1088,7 +1093,7 @@ public function testChmodNotAllowedIfNotOwner()
{
$fs = new FileSystem();
$file = $fs->createFile('/file');
$file->chown(posix_getuid() + 1); //set to non current
$file->chown($this->uid + 1); //set to non current

$wr = new Wrapper();

Expand All @@ -1109,7 +1114,7 @@ public function testChownAndChgrpNotAllowedIfNotRoot()
{
$fs = new FileSystem();
$file = $fs->createFile('/file');
$file->chown(posix_getuid() + 1); //set to non current
$file->chown($this->uid + 1); //set to non current

$wr = new Wrapper();

Expand Down Expand Up @@ -1172,7 +1177,7 @@ public function testTouchNotAllowedIfNotOwnerOrNotWritable()
{
$fs = new FileSystem();
$file = $fs->createFile('/file');
$file->chown(posix_getuid() + 1); //set to non current
$file->chown($this->uid + 1); //set to non current
$file->chmod(0000);

$wr = new Wrapper();
Expand All @@ -1189,14 +1194,14 @@ public function testTouchNotAllowedIfNotOwnerOrNotWritable()
$error['message']
);

$file->chown(posix_getuid());
$file->chown($this->uid);

$this->assertTrue(
$wr->stream_metadata($fs->path('/file'), STREAM_META_TOUCH, 0),
'Allowed to touch if owner and no permission'
);

$file->chown(posix_getuid() + 1); //set to non current
$file->chown($this->uid + 1); //set to non current
$file->chmod(0002);

$this->assertTrue(
Expand Down

0 comments on commit 9a4524b

Please sign in to comment.