2021-04-02 21:00:09 +02:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
require_once dirname(__FILE__)."/../lib-htmgem.inc.php";
|
|
|
|
|
|
|
|
final class miscTest extends TestCase {
|
|
|
|
|
2021-04-11 21:35:55 +02:00
|
|
|
public function test_split_path_links(): void {
|
2021-04-02 21:00:09 +02:00
|
|
|
$this->assertSame(
|
2021-04-11 21:35:55 +02:00
|
|
|
array(),
|
|
|
|
\htmgem\split_path_links(""),
|
|
|
|
"empty link"
|
|
|
|
);
|
|
|
|
$this->assertSame(
|
|
|
|
array(
|
|
|
|
"noslash" => "noslash",
|
|
|
|
),
|
|
|
|
\htmgem\split_path_links("noslash"),
|
|
|
|
"no slash"
|
|
|
|
);
|
|
|
|
$this->assertSame(
|
|
|
|
array(),
|
|
|
|
\htmgem\split_path_links("/"),
|
|
|
|
"only a slash"
|
|
|
|
);
|
|
|
|
$this->assertSame(
|
|
|
|
array(
|
|
|
|
"one" => "/one",
|
|
|
|
),
|
|
|
|
\htmgem\split_path_links("/one"),
|
|
|
|
"/one"
|
|
|
|
);
|
|
|
|
$this->assertSame(
|
|
|
|
array(
|
|
|
|
"one" => "one",
|
|
|
|
"two" => "one/two",
|
|
|
|
),
|
|
|
|
\htmgem\split_path_links("one/two"),
|
|
|
|
"one/two"
|
|
|
|
);
|
|
|
|
$this->assertSame(
|
|
|
|
array(
|
|
|
|
"one" => "/one",
|
|
|
|
"two" => "/one/two",
|
|
|
|
"file.ext" => "/one/two/file.ext",
|
|
|
|
),
|
|
|
|
\htmgem\split_path_links("/one/two/file.ext"),
|
|
|
|
"/one/two/file.ext"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_resolve_path(): void {
|
|
|
|
$this->assertSame(
|
|
|
|
\htmgem\resolve_path(""),
|
2021-04-02 21:00:09 +02:00
|
|
|
"",
|
|
|
|
"empty link"
|
|
|
|
);
|
|
|
|
$this->assertSame(
|
2021-04-11 21:35:55 +02:00
|
|
|
\htmgem\resolve_path("test"),
|
2021-04-02 21:00:09 +02:00
|
|
|
"test",
|
|
|
|
"single word"
|
|
|
|
);
|
|
|
|
$this->assertSame(
|
2021-04-11 21:35:55 +02:00
|
|
|
\htmgem\resolve_path(" "),
|
2021-04-02 21:00:09 +02:00
|
|
|
" ",
|
|
|
|
"single space"
|
|
|
|
);
|
|
|
|
$this->assertSame(
|
2021-04-11 21:35:55 +02:00
|
|
|
\htmgem\resolve_path(" A B "),
|
2021-04-02 21:00:09 +02:00
|
|
|
" A B ",
|
|
|
|
"several space"
|
|
|
|
);
|
|
|
|
$this->assertSame(
|
2021-04-11 21:35:55 +02:00
|
|
|
\htmgem\resolve_path("/"),
|
2021-04-02 21:00:09 +02:00
|
|
|
"/",
|
|
|
|
"one slash"
|
|
|
|
);
|
|
|
|
$this->assertSame(
|
2021-04-11 21:35:55 +02:00
|
|
|
\htmgem\resolve_path("//"),
|
2021-04-02 21:00:09 +02:00
|
|
|
"/",
|
|
|
|
"two slashes"
|
|
|
|
);
|
|
|
|
$this->assertSame(
|
2021-04-11 21:35:55 +02:00
|
|
|
\htmgem\resolve_path("/////"),
|
2021-04-02 21:00:09 +02:00
|
|
|
"/",
|
|
|
|
"five slashes"
|
|
|
|
);
|
|
|
|
$this->assertSame(
|
2021-04-11 21:35:55 +02:00
|
|
|
\htmgem\resolve_path("one/"),
|
2021-04-02 21:00:09 +02:00
|
|
|
"one",
|
|
|
|
"strip the last slash"
|
|
|
|
);
|
|
|
|
$this->assertSame(
|
2021-04-11 21:35:55 +02:00
|
|
|
\htmgem\resolve_path("/two"),
|
2021-04-02 21:00:09 +02:00
|
|
|
"/two",
|
|
|
|
"slash at the beginning"
|
|
|
|
);
|
|
|
|
$this->assertSame(
|
2021-04-11 21:35:55 +02:00
|
|
|
\htmgem\resolve_path("/two/"),
|
2021-04-02 21:00:09 +02:00
|
|
|
"/two",
|
|
|
|
"slash at the beginning and the end"
|
|
|
|
);
|
|
|
|
$this->assertSame(
|
2021-04-11 21:35:55 +02:00
|
|
|
\htmgem\resolve_path("one/two/"),
|
2021-04-02 21:00:09 +02:00
|
|
|
"one/two",
|
|
|
|
"only the last slash remains"
|
|
|
|
);
|
|
|
|
$this->assertSame(
|
2021-04-11 21:35:55 +02:00
|
|
|
\htmgem\resolve_path("one/two/three//"),
|
2021-04-02 21:00:09 +02:00
|
|
|
"one/two/three",
|
|
|
|
"strip the last slashes"
|
|
|
|
);
|
|
|
|
$this->assertSame(
|
2021-04-11 21:35:55 +02:00
|
|
|
\htmgem\resolve_path("one/../"),
|
2021-04-02 21:00:09 +02:00
|
|
|
"",
|
|
|
|
"empty one"
|
|
|
|
);
|
|
|
|
$this->assertSame(
|
2021-04-11 21:35:55 +02:00
|
|
|
\htmgem\resolve_path("one/two/../"),
|
2021-04-02 21:00:09 +02:00
|
|
|
"one",
|
|
|
|
"empty one two"
|
|
|
|
);
|
|
|
|
$this->assertSame(
|
2021-04-11 21:35:55 +02:00
|
|
|
\htmgem\resolve_path("one/two/../.."),
|
2021-04-02 21:00:09 +02:00
|
|
|
"",
|
|
|
|
"empty one two twice"
|
|
|
|
);
|
|
|
|
$this->assertSame(
|
2021-04-11 21:35:55 +02:00
|
|
|
\htmgem\resolve_path("one/../two/./../three"),
|
2021-04-02 21:00:09 +02:00
|
|
|
"three",
|
|
|
|
"waltz"
|
|
|
|
);
|
|
|
|
$this->assertSame(
|
2021-04-11 21:35:55 +02:00
|
|
|
\htmgem\resolve_path("one/../.."),
|
2021-04-02 21:00:09 +02:00
|
|
|
"/",
|
|
|
|
"directory traversal"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|