2017-07-22 19:29:12 +00:00
|
|
|
<?php
|
2017-11-17 01:23:18 +00:00
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
2017-07-22 19:29:12 +00:00
|
|
|
declare(strict_types=1);
|
2017-12-22 16:41:54 +00:00
|
|
|
namespace JKingWeb\Arsse\TestCase\Db\SQLite3;
|
2017-08-29 14:50:31 +00:00
|
|
|
|
2017-07-22 19:29:12 +00:00
|
|
|
use JKingWeb\Arsse\Arsse;
|
2017-12-19 17:11:49 +00:00
|
|
|
use JKingWeb\Arsse\Db\SQLite3\Driver;
|
2017-07-22 19:29:12 +00:00
|
|
|
use org\bovigo\vfs\vfsStream;
|
|
|
|
|
2017-08-29 15:16:37 +00:00
|
|
|
/**
|
2017-07-22 19:29:12 +00:00
|
|
|
* @covers \JKingWeb\Arsse\Db\SQLite3\Driver<extended>
|
|
|
|
* @covers \JKingWeb\Arsse\Db\SQLite3\ExceptionBuilder */
|
2017-12-22 16:41:54 +00:00
|
|
|
class TestCreation extends \JKingWeb\Arsse\Test\AbstractTest {
|
2017-07-22 19:29:12 +00:00
|
|
|
protected $data;
|
|
|
|
protected $drv;
|
|
|
|
protected $ch;
|
|
|
|
|
2019-10-16 18:42:43 +00:00
|
|
|
public function setUp(): void {
|
2017-12-19 17:11:49 +00:00
|
|
|
if (!Driver::requirementsMet()) {
|
2017-07-22 19:29:12 +00:00
|
|
|
$this->markTestSkipped("SQLite extension not loaded");
|
|
|
|
}
|
2021-02-27 20:24:02 +00:00
|
|
|
parent::setUp();
|
2017-07-22 19:29:12 +00:00
|
|
|
// test files
|
|
|
|
$this->files = [
|
|
|
|
// cannot create files
|
|
|
|
'Cmain' => [],
|
2020-03-01 20:16:50 +00:00
|
|
|
'Cshm' => [
|
|
|
|
'arsse.db' => "",
|
2017-07-22 19:29:12 +00:00
|
|
|
'arsse.db-wal' => "",
|
|
|
|
],
|
|
|
|
'Cwal' => [
|
|
|
|
'arsse.db' => "",
|
|
|
|
],
|
|
|
|
// cannot write to files
|
|
|
|
'Wmain' => [
|
2020-03-01 20:16:50 +00:00
|
|
|
'arsse.db' => "",
|
2017-07-22 19:29:12 +00:00
|
|
|
'arsse.db-wal' => "",
|
|
|
|
'arsse.db-shm' => "",
|
|
|
|
],
|
|
|
|
'Wwal' => [
|
2020-03-01 20:16:50 +00:00
|
|
|
'arsse.db' => "",
|
2017-07-22 19:29:12 +00:00
|
|
|
'arsse.db-wal' => "",
|
|
|
|
'arsse.db-shm' => "",
|
|
|
|
],
|
|
|
|
'Wshm' => [
|
2020-03-01 20:16:50 +00:00
|
|
|
'arsse.db' => "",
|
2017-07-22 19:29:12 +00:00
|
|
|
'arsse.db-wal' => "",
|
|
|
|
'arsse.db-shm' => "",
|
|
|
|
],
|
|
|
|
// cannot read from files
|
|
|
|
'Rmain' => [
|
2020-03-01 20:16:50 +00:00
|
|
|
'arsse.db' => "",
|
2017-07-22 19:29:12 +00:00
|
|
|
'arsse.db-wal' => "",
|
|
|
|
'arsse.db-shm' => "",
|
|
|
|
],
|
|
|
|
'Rwal' => [
|
2020-03-01 20:16:50 +00:00
|
|
|
'arsse.db' => "",
|
2017-07-22 19:29:12 +00:00
|
|
|
'arsse.db-wal' => "",
|
|
|
|
'arsse.db-shm' => "",
|
|
|
|
],
|
|
|
|
'Rshm' => [
|
2020-03-01 20:16:50 +00:00
|
|
|
'arsse.db' => "",
|
2017-07-22 19:29:12 +00:00
|
|
|
'arsse.db-wal' => "",
|
|
|
|
'arsse.db-shm' => "",
|
|
|
|
],
|
|
|
|
// can neither read from or write to files
|
|
|
|
'Amain' => [
|
2020-03-01 20:16:50 +00:00
|
|
|
'arsse.db' => "",
|
2017-07-22 19:29:12 +00:00
|
|
|
'arsse.db-wal' => "",
|
|
|
|
'arsse.db-shm' => "",
|
|
|
|
],
|
|
|
|
'Awal' => [
|
2020-03-01 20:16:50 +00:00
|
|
|
'arsse.db' => "",
|
2017-07-22 19:29:12 +00:00
|
|
|
'arsse.db-wal' => "",
|
|
|
|
'arsse.db-shm' => "",
|
|
|
|
],
|
|
|
|
'Ashm' => [
|
2020-03-01 20:16:50 +00:00
|
|
|
'arsse.db' => "",
|
2017-07-22 19:29:12 +00:00
|
|
|
'arsse.db-wal' => "",
|
|
|
|
'arsse.db-shm' => "",
|
|
|
|
],
|
|
|
|
// non-filesystem errors
|
|
|
|
'corrupt' => [
|
2020-03-01 20:16:50 +00:00
|
|
|
'arsse.db' => "",
|
2017-07-22 19:29:12 +00:00
|
|
|
'arsse.db-wal' => "",
|
|
|
|
'arsse.db-shm' => "",
|
|
|
|
],
|
|
|
|
];
|
|
|
|
$vfs = vfsStream::setup("dbtest", 0777, $this->files);
|
|
|
|
$this->path = $path = $vfs->url()."/";
|
|
|
|
// set up access blocks
|
|
|
|
chmod($path."Cmain", 0555);
|
2017-08-29 14:50:31 +00:00
|
|
|
chmod($path."Cwal", 0555);
|
|
|
|
chmod($path."Cshm", 0555);
|
|
|
|
chmod($path."Rmain/arsse.db", 0333);
|
2017-07-22 19:29:12 +00:00
|
|
|
chmod($path."Rwal/arsse.db-wal", 0333);
|
|
|
|
chmod($path."Rshm/arsse.db-shm", 0333);
|
2017-08-29 14:50:31 +00:00
|
|
|
chmod($path."Wmain/arsse.db", 0555);
|
2017-07-22 19:29:12 +00:00
|
|
|
chmod($path."Wwal/arsse.db-wal", 0555);
|
|
|
|
chmod($path."Wshm/arsse.db-shm", 0555);
|
2017-08-29 14:50:31 +00:00
|
|
|
chmod($path."Amain/arsse.db", 0111);
|
2017-07-22 19:29:12 +00:00
|
|
|
chmod($path."Awal/arsse.db-wal", 0111);
|
|
|
|
chmod($path."Ashm/arsse.db-shm", 0111);
|
|
|
|
// set up configuration
|
2018-11-23 00:55:54 +00:00
|
|
|
self::setConf();
|
2017-07-22 19:29:12 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testFailToCreateDatabase(): void {
|
2017-07-22 19:29:12 +00:00
|
|
|
Arsse::$conf->dbSQLite3File = $this->path."Cmain/arsse.db";
|
|
|
|
$this->assertException("fileUncreatable", "Db");
|
2017-12-19 17:11:49 +00:00
|
|
|
new Driver;
|
2017-07-22 19:29:12 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testFailToCreateJournal(): void {
|
2017-07-22 19:29:12 +00:00
|
|
|
Arsse::$conf->dbSQLite3File = $this->path."Cwal/arsse.db";
|
|
|
|
$this->assertException("fileUncreatable", "Db");
|
2017-12-19 17:11:49 +00:00
|
|
|
new Driver;
|
2017-07-22 19:29:12 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testFailToCreateSharedMmeory(): void {
|
2017-07-22 19:29:12 +00:00
|
|
|
Arsse::$conf->dbSQLite3File = $this->path."Cshm/arsse.db";
|
|
|
|
$this->assertException("fileUncreatable", "Db");
|
2017-12-19 17:11:49 +00:00
|
|
|
new Driver;
|
2017-07-22 19:29:12 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testFailToReadDatabase(): void {
|
2017-07-22 19:29:12 +00:00
|
|
|
Arsse::$conf->dbSQLite3File = $this->path."Rmain/arsse.db";
|
|
|
|
$this->assertException("fileUnreadable", "Db");
|
2017-12-19 17:11:49 +00:00
|
|
|
new Driver;
|
2017-07-22 19:29:12 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testFailToReadJournal(): void {
|
2017-07-22 19:29:12 +00:00
|
|
|
Arsse::$conf->dbSQLite3File = $this->path."Rwal/arsse.db";
|
|
|
|
$this->assertException("fileUnreadable", "Db");
|
2017-12-19 17:11:49 +00:00
|
|
|
new Driver;
|
2017-07-22 19:29:12 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testFailToReadSharedMmeory(): void {
|
2017-07-22 19:29:12 +00:00
|
|
|
Arsse::$conf->dbSQLite3File = $this->path."Rshm/arsse.db";
|
|
|
|
$this->assertException("fileUnreadable", "Db");
|
2017-12-19 17:11:49 +00:00
|
|
|
new Driver;
|
2017-07-22 19:29:12 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testFailToWriteToDatabase(): void {
|
2017-07-22 19:29:12 +00:00
|
|
|
Arsse::$conf->dbSQLite3File = $this->path."Wmain/arsse.db";
|
|
|
|
$this->assertException("fileUnwritable", "Db");
|
2017-12-19 17:11:49 +00:00
|
|
|
new Driver;
|
2017-07-22 19:29:12 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testFailToWriteToJournal(): void {
|
2017-07-22 19:29:12 +00:00
|
|
|
Arsse::$conf->dbSQLite3File = $this->path."Wwal/arsse.db";
|
|
|
|
$this->assertException("fileUnwritable", "Db");
|
2017-12-19 17:11:49 +00:00
|
|
|
new Driver;
|
2017-07-22 19:29:12 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testFailToWriteToSharedMmeory(): void {
|
2017-07-22 19:29:12 +00:00
|
|
|
Arsse::$conf->dbSQLite3File = $this->path."Wshm/arsse.db";
|
|
|
|
$this->assertException("fileUnwritable", "Db");
|
2017-12-19 17:11:49 +00:00
|
|
|
new Driver;
|
2017-07-22 19:29:12 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testFailToAccessDatabase(): void {
|
2017-07-22 19:29:12 +00:00
|
|
|
Arsse::$conf->dbSQLite3File = $this->path."Amain/arsse.db";
|
|
|
|
$this->assertException("fileUnusable", "Db");
|
2017-12-19 17:11:49 +00:00
|
|
|
new Driver;
|
2017-07-22 19:29:12 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testFailToAccessJournal(): void {
|
2017-07-22 19:29:12 +00:00
|
|
|
Arsse::$conf->dbSQLite3File = $this->path."Awal/arsse.db";
|
|
|
|
$this->assertException("fileUnusable", "Db");
|
2017-12-19 17:11:49 +00:00
|
|
|
new Driver;
|
2017-07-22 19:29:12 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testFailToAccessSharedMmeory(): void {
|
2017-07-22 19:29:12 +00:00
|
|
|
Arsse::$conf->dbSQLite3File = $this->path."Ashm/arsse.db";
|
|
|
|
$this->assertException("fileUnusable", "Db");
|
2017-12-19 17:11:49 +00:00
|
|
|
new Driver;
|
2017-07-22 19:29:12 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testAssumeDatabaseCorruption(): void {
|
2017-07-22 19:29:12 +00:00
|
|
|
Arsse::$conf->dbSQLite3File = $this->path."corrupt/arsse.db";
|
|
|
|
$this->assertException("fileCorrupt", "Db");
|
2017-12-19 17:11:49 +00:00
|
|
|
new Driver;
|
2017-07-22 19:29:12 +00:00
|
|
|
}
|
2021-05-18 22:42:42 +00:00
|
|
|
|
|
|
|
public function testSetFileMode(): void {
|
|
|
|
$f = tempnam(sys_get_temp_dir(), "arsse");
|
|
|
|
Arsse::$conf->dbSQLite3File = $f;
|
|
|
|
// delete the file PHP just created
|
|
|
|
unlink($f);
|
|
|
|
// recreate the file
|
|
|
|
new Driver;
|
|
|
|
// check the mode
|
|
|
|
clearstatcache();
|
|
|
|
$mode = base_convert((string) stat($f)['mode'], 10, 8);
|
|
|
|
$this->assertMatchesRegularExpression("/640$/", $mode);
|
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
}
|