Skip to content

Commit 70bbec2

Browse files
committed
Added tests for SimpleSAMLUtilsSystem
1 parent c281075 commit 70bbec2

2 files changed

Lines changed: 252 additions & 1 deletion

File tree

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
"require-dev": {
4747
"ext-pdo_sqlite": "*",
4848
"phpunit/phpunit": "~4.8",
49-
"satooshi/php-coveralls": "^1.0"
49+
"satooshi/php-coveralls": "^1.0",
50+
"mikey179/vfsStream": "~1.6"
5051
},
5152
"suggests": {
5253
"predis/predis": "1.1.1"
Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
<?php
2+
3+
namespace SimpleSAML\Test\Utils;
4+
5+
use \SimpleSAML_Configuration as Configuration;
6+
use \SimpleSAML\Utils\System;
7+
8+
use \org\bovigo\vfs\vfsStream;
9+
10+
/**
11+
* Tests for SimpleSAML\Utils\System.
12+
*/
13+
class SystemTest extends \PHPUnit_Framework_TestCase
14+
{
15+
const ROOTDIRNAME = 'testdir';
16+
const DEFAULTTEMPDIR = 'tempdir';
17+
18+
public function setUp()
19+
{
20+
$this->root = vfsStream::setup(
21+
self::ROOTDIRNAME,
22+
null,
23+
array(
24+
self::DEFAULTTEMPDIR => array(),
25+
)
26+
);
27+
$this->root_directory = vfsStream::url(self::ROOTDIRNAME);
28+
}
29+
30+
/**
31+
* @covers \SimpleSAML\Utils\System::getOS
32+
* @test
33+
*/
34+
public function testGetOSBasic()
35+
{
36+
$res = System::getOS();
37+
38+
$this->assertInternalType("int", $res);
39+
}
40+
41+
/**
42+
* @covers \SimpleSAML\Utils\System::resolvePath
43+
* @test
44+
*/
45+
public function testResolvePathRemoveTrailingSlashes()
46+
{
47+
$base = "/base////";
48+
$path = "test";
49+
50+
$res = System::resolvePath($path, $base);
51+
$expected = "/base/test";
52+
53+
$this->assertEquals($expected, $res);
54+
}
55+
56+
/**
57+
* @covers \SimpleSAML\Utils\System::resolvePath
58+
* @test
59+
*/
60+
public function testResolvePathPreferAbsolutePathToBase()
61+
{
62+
$base = "/base/";
63+
$path = "/test";
64+
65+
$res = System::resolvePath($path, $base);
66+
$expected = "/test";
67+
68+
$this->assertEquals($expected, $res);
69+
}
70+
71+
/**
72+
* @covers \SimpleSAML\Utils\System::resolvePath
73+
* @test
74+
*/
75+
public function testResolvePathCurDirPath()
76+
{
77+
$base = "/base/";
78+
$path = "/test/.";
79+
80+
$res = System::resolvePath($path, $base);
81+
$expected = "/test";
82+
83+
$this->assertEquals($expected, $res);
84+
}
85+
86+
/**
87+
* @covers \SimpleSAML\Utils\System::resolvePath
88+
* @test
89+
*/
90+
public function testResolvePathParentPath()
91+
{
92+
$base = "/base/";
93+
$path = "/test/child/..";
94+
95+
$res = System::resolvePath($path, $base);
96+
$expected = "/test";
97+
98+
$this->assertEquals($expected, $res);
99+
}
100+
101+
/**
102+
* @covers \SimpleSAML\Utils\System::writeFile
103+
* @test
104+
*/
105+
public function testWriteFileInvalidArguments()
106+
{
107+
$this->setExpectedException('\InvalidArgumentException');
108+
System::writeFile(null, null, null);
109+
}
110+
111+
/**
112+
* @requires PHP 5.4.0
113+
* @covers \SimpleSAML\Utils\System::writeFile
114+
* @test
115+
*/
116+
public function testWriteFileBasic()
117+
{
118+
$tempdir = $this->root_directory . DIRECTORY_SEPARATOR . self::DEFAULTTEMPDIR;
119+
$config = $this->setConfigurationTempDir($tempdir);
120+
121+
$filename = $this->root_directory . DIRECTORY_SEPARATOR . 'test';
122+
123+
System::writeFile($filename, '');
124+
125+
$this->assertFileExists($filename);
126+
127+
$this->clearInstance($config, '\SimpleSAML_Configuration');
128+
}
129+
130+
/**
131+
* @requires PHP 5.4.0
132+
* @covers \SimpleSAML\Utils\System::writeFile
133+
* @test
134+
*/
135+
public function testWriteFileContents()
136+
{
137+
$tempdir = $this->root_directory . DIRECTORY_SEPARATOR . self::DEFAULTTEMPDIR;
138+
$config = $this->setConfigurationTempDir($tempdir);
139+
140+
$filename = $this->root_directory . DIRECTORY_SEPARATOR . 'test';
141+
$contents = 'TEST';
142+
143+
System::writeFile($filename, $contents);
144+
145+
$res = file_get_contents($filename);
146+
$expected = $contents;
147+
148+
$this->assertEquals($expected, $res);
149+
150+
$this->clearInstance($config, '\SimpleSAML_Configuration');
151+
}
152+
153+
/**
154+
* @requires PHP 5.4.0
155+
* @covers \SimpleSAML\Utils\System::writeFile
156+
* @test
157+
*/
158+
public function testWriteFileMode()
159+
{
160+
$tempdir = $this->root_directory . DIRECTORY_SEPARATOR . self::DEFAULTTEMPDIR;
161+
$config = $this->setConfigurationTempDir($tempdir);
162+
163+
$filename = $this->root_directory . DIRECTORY_SEPARATOR . 'test';
164+
$mode = 0666;
165+
166+
System::writeFile($filename, '', $mode);
167+
168+
$res = $this->root->getChild('test')->getPermissions();
169+
$expected = $mode;
170+
171+
$this->assertEquals($expected, $res);
172+
173+
$this->clearInstance($config, '\SimpleSAML_Configuration');
174+
}
175+
176+
/**
177+
* @covers \SimpleSAML\Utils\System::getTempDir
178+
* @test
179+
*/
180+
public function testGetTempDirBasic()
181+
{
182+
$tempdir = $this->root_directory . DIRECTORY_SEPARATOR . self::DEFAULTTEMPDIR;
183+
$config = $this->setConfigurationTempDir($tempdir);
184+
185+
$res = System::getTempDir();
186+
$expected = $tempdir;
187+
188+
$this->assertEquals($expected, $res);
189+
$this->assertFileExists($res);
190+
191+
$this->clearInstance($config, '\SimpleSAML_Configuration');
192+
}
193+
194+
/**
195+
* @covers \SimpleSAML\Utils\System::getTempDir
196+
* @test
197+
*/
198+
public function testGetTempDirNonExistant()
199+
{
200+
$tempdir = $this->root_directory . DIRECTORY_SEPARATOR . 'nonexistant';
201+
$config = $this->setConfigurationTempDir($tempdir);
202+
203+
$res = System::getTempDir();
204+
$expected = $tempdir;
205+
206+
$this->assertEquals($expected, $res);
207+
$this->assertFileExists($res);
208+
209+
$this->clearInstance($config, '\SimpleSAML_Configuration');
210+
}
211+
212+
/**
213+
* @requires PHP 5.4.0
214+
* @requires OS Linux
215+
* @covers \SimpleSAML\Utils\System::getTempDir
216+
* @test
217+
*/
218+
public function testGetTempDirBadOwner()
219+
{
220+
$bad_uid = posix_getuid() + 1;
221+
222+
$tempdir = $this->root_directory . DIRECTORY_SEPARATOR . self::DEFAULTTEMPDIR;
223+
$config = $this->setConfigurationTempDir($tempdir);
224+
225+
chown($tempdir, $bad_uid);
226+
227+
$this->setExpectedException('\SimpleSAML_Error_Exception');
228+
$res = System::getTempDir();
229+
230+
$this->clearInstance($config, '\SimpleSAML_Configuration');
231+
}
232+
233+
private function setConfigurationTempDir($directory)
234+
{
235+
$config = Configuration::loadFromArray(array(
236+
'tempdir' => $directory,
237+
), '[ARRAY]', 'simplesaml');
238+
239+
return $config;
240+
}
241+
242+
protected function clearInstance($service, $className)
243+
{
244+
$reflectedClass = new \ReflectionClass($className);
245+
$reflectedInstance = $reflectedClass->getProperty('instance');
246+
$reflectedInstance->setAccessible(true);
247+
$reflectedInstance->setValue($service, null);
248+
$reflectedInstance->setAccessible(false);
249+
}
250+
}

0 commit comments

Comments
 (0)