| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Logger\LoggerFactory; |
| 4 |
use Wikimedia\FileBackend\FileBackend; |
| 5 |
use Wikimedia\FileBackend\FileBackendError; |
| 6 |
use Wikimedia\FileBackend\SwiftFileBackend; |
| 7 |
use Wikimedia\TestingAccessWrapper; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
class SwiftFileBackendTest extends MediaWikiIntegrationTestCase { |
| 20 |
|
| 21 |
private $backend; |
| 22 |
|
| 23 |
protected function setUp(): void { |
| 24 |
parent::setUp(); |
| 25 |
|
| 26 |
$this->backend = TestingAccessWrapper::newFromObject( |
| 27 |
new SwiftFileBackend( [ |
| 28 |
'name' => 'local-swift-testing', |
| 29 |
'class' => SwiftFileBackend::class, |
| 30 |
'wikiId' => 'unit-testing', |
| 31 |
'lockManager' => $this->getServiceContainer()->getLockManagerGroupFactory() |
| 32 |
->getLockManagerGroup()->get( 'fsLockManager' ), |
| 33 |
'swiftAuthUrl' => 'http://127.0.0.1:8080/auth', |
| 34 |
'swiftUser' => 'test:tester', |
| 35 |
'swiftKey' => 'testing', |
| 36 |
'swiftTempUrlKey' => 'b3968d0207b54ece87cccc06515a89d4', |
| 37 |
'logger' => LoggerFactory::getInstance( 'FileOperation' ) |
| 38 |
] ) |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public function testExtractPostableContentHeaders( $raw, $sanitized ) { |
| 46 |
$hdrs = $this->backend->extractMutableContentHeaders( $raw ); |
| 47 |
|
| 48 |
$this->assertEquals( $sanitized, $hdrs, 'Correct extractPostableContentHeaders() result' ); |
| 49 |
} |
| 50 |
|
| 51 |
public static function provider_testExtractPostableContentHeaders() { |
| 52 |
return [ |
| 53 |
'empty' => [ |
| 54 |
[], |
| 55 |
[] |
| 56 |
], |
| 57 |
[ |
| 58 |
[ |
| 59 |
'content-length' => 345, |
| 60 |
'content-type' => 'image+bitmap/jpeg', |
| 61 |
'content-disposition' => 'inline', |
| 62 |
'content-duration' => 35.6363, |
| 63 |
'content-Custom' => 'hello', |
| 64 |
'x-content-custom' => 'hello' |
| 65 |
], |
| 66 |
[ |
| 67 |
'content-type' => 'image+bitmap/jpeg', |
| 68 |
'content-disposition' => 'inline', |
| 69 |
'content-duration' => 35.6363, |
| 70 |
'content-custom' => 'hello', |
| 71 |
'x-content-custom' => 'hello' |
| 72 |
] |
| 73 |
], |
| 74 |
[ |
| 75 |
[ |
| 76 |
'content-length' => 345, |
| 77 |
'content-type' => 'image+bitmap/jpeg', |
| 78 |
'content-Disposition' => 'inline; filename=xxx; ' . str_repeat( 'o', 1024 ), |
| 79 |
'content-duration' => 35.6363, |
| 80 |
'content-custom' => 'hello', |
| 81 |
'x-content-custom' => 'hello' |
| 82 |
], |
| 83 |
[ |
| 84 |
'content-type' => 'image+bitmap/jpeg', |
| 85 |
'content-disposition' => 'inline; filename=xxx', |
| 86 |
'content-duration' => 35.6363, |
| 87 |
'content-custom' => 'hello', |
| 88 |
'x-content-custom' => 'hello' |
| 89 |
] |
| 90 |
], |
| 91 |
[ |
| 92 |
[ |
| 93 |
'content-length' => 345, |
| 94 |
'content-type' => 'image+bitmap/jpeg', |
| 95 |
'content-disposition' => 'filename=' . str_repeat( 'o', 1024 ) . ';inline', |
| 96 |
'content-duration' => 35.6363, |
| 97 |
'content-custom' => 'hello', |
| 98 |
'x-content-custom' => 'hello' |
| 99 |
], |
| 100 |
[ |
| 101 |
'content-type' => 'image+bitmap/jpeg', |
| 102 |
'content-disposition' => '', |
| 103 |
'content-duration' => 35.6363, |
| 104 |
'content-custom' => 'hello', |
| 105 |
'x-content-custom' => 'hello' |
| 106 |
] |
| 107 |
], |
| 108 |
[ |
| 109 |
[ |
| 110 |
'x-delete-at' => 'non numeric', |
| 111 |
'x-delete-after' => 'non numeric', |
| 112 |
'x-content-custom' => 'hello' |
| 113 |
], |
| 114 |
[ |
| 115 |
'x-content-custom' => 'hello' |
| 116 |
] |
| 117 |
], |
| 118 |
[ |
| 119 |
[ |
| 120 |
'x-delete-at' => '12345', |
| 121 |
'x-delete-after' => '12345' |
| 122 |
], |
| 123 |
[ |
| 124 |
'x-delete-at' => '12345', |
| 125 |
'x-delete-after' => '12345' |
| 126 |
] |
| 127 |
], |
| 128 |
[ |
| 129 |
[ |
| 130 |
'x-delete-at' => 12345, |
| 131 |
'x-delete-after' => 12345 |
| 132 |
], |
| 133 |
[ |
| 134 |
'x-delete-at' => 12345, |
| 135 |
'x-delete-after' => 12345 |
| 136 |
] |
| 137 |
] |
| 138 |
]; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public function testGetMetadataHeaders( $raw, $sanitized ) { |
| 145 |
$hdrs = $this->backend->extractMetadataHeaders( $raw ); |
| 146 |
|
| 147 |
$this->assertEquals( $sanitized, $hdrs, 'getMetadataHeaders() has unexpected result' ); |
| 148 |
} |
| 149 |
|
| 150 |
public static function provider_testGetMetadataHeaders() { |
| 151 |
return [ |
| 152 |
[ |
| 153 |
[ |
| 154 |
'content-length' => 345, |
| 155 |
'content-custom' => 'hello', |
| 156 |
'x-content-custom' => 'hello', |
| 157 |
'x-object-meta-custom' => 5, |
| 158 |
'x-object-meta-sha1Base36' => 'a3deadfg...', |
| 159 |
], |
| 160 |
[ |
| 161 |
'x-object-meta-custom' => 5, |
| 162 |
'x-object-meta-sha1base36' => 'a3deadfg...', |
| 163 |
] |
| 164 |
] |
| 165 |
]; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public function testGetMetadata( $raw, $sanitized ) { |
| 172 |
$hdrs = $this->backend->getMetadataFromHeaders( $raw ); |
| 173 |
|
| 174 |
$this->assertEquals( $sanitized, $hdrs, 'getMetadata() has unexpected result' ); |
| 175 |
} |
| 176 |
|
| 177 |
public static function provider_testGetMetadata() { |
| 178 |
return [ |
| 179 |
[ |
| 180 |
[ |
| 181 |
'content-length' => 345, |
| 182 |
'content-custom' => 'hello', |
| 183 |
'x-content-custom' => 'hello', |
| 184 |
'x-object-meta-custom' => 5, |
| 185 |
'x-object-meta-sha1Base36' => 'a3deadfg...', |
| 186 |
], |
| 187 |
[ |
| 188 |
'custom' => 5, |
| 189 |
'sha1base36' => 'a3deadfg...', |
| 190 |
] |
| 191 |
] |
| 192 |
]; |
| 193 |
} |
| 194 |
|
| 195 |
private function setupAuthFailure() { |
| 196 |
$this->backend->authErrorTimestamp = time(); |
| 197 |
$this->backend->http = null; |
| 198 |
} |
| 199 |
|
| 200 |
public function testGetFileStatAuthFail() { |
| 201 |
$this->setupAuthFailure(); |
| 202 |
$result = $this->backend->getFileStat( [ |
| 203 |
'src' => 'mwstore://local-swift-testing/c/test.txt' |
| 204 |
] ); |
| 205 |
$this->assertSame( FileBackend::STAT_ERROR, $result ); |
| 206 |
} |
| 207 |
|
| 208 |
public function testGetFileContentsAuthFail() { |
| 209 |
$this->setupAuthFailure(); |
| 210 |
$result = $this->backend->getFileContents( [ |
| 211 |
'src' => 'mwstore://local-swift-testing/c/test.txt' |
| 212 |
] ); |
| 213 |
$this->assertFalse( $result ); |
| 214 |
} |
| 215 |
|
| 216 |
public function testGetLocalCopyAuthFail() { |
| 217 |
$this->setupAuthFailure(); |
| 218 |
$result = $this->backend->getLocalCopy( [ |
| 219 |
'src' => 'mwstore://local-swift-testing/c/test.txt' |
| 220 |
] ); |
| 221 |
$this->assertNull( $result ); |
| 222 |
} |
| 223 |
|
| 224 |
public function testCreateAuthFail() { |
| 225 |
$this->setupAuthFailure(); |
| 226 |
$status = $this->backend->create( [ |
| 227 |
'dst' => 'mwstore://local-swift-testing/c/test.txt', |
| 228 |
'content' => '', |
| 229 |
] ); |
| 230 |
|
| 231 |
|
| 232 |
$this->assertStatusError( 'backend-fail-internal', $status ); |
| 233 |
} |
| 234 |
|
| 235 |
public function testSecureAuthFail() { |
| 236 |
$this->setupAuthFailure(); |
| 237 |
$status = $this->backend->secure( [ |
| 238 |
'dir' => 'mwstore://local-swift-testing/c', |
| 239 |
'noAccess' => true, |
| 240 |
] ); |
| 241 |
$this->assertStatusError( 'backend-fail-internal', $status ); |
| 242 |
} |
| 243 |
|
| 244 |
public function testPrepareAuthFail() { |
| 245 |
$this->setupAuthFailure(); |
| 246 |
$status = $this->backend->prepare( [ |
| 247 |
'dir' => 'mwstore://local-swift-testing/c', |
| 248 |
'noAccess' => true, |
| 249 |
] ); |
| 250 |
$this->assertStatusError( 'backend-fail-internal', $status ); |
| 251 |
} |
| 252 |
|
| 253 |
public function testCleanAuthFail() { |
| 254 |
$this->setupAuthFailure(); |
| 255 |
$status = $this->backend->clean( [ |
| 256 |
'dir' => 'mwstore://local-swift-testing/c', |
| 257 |
] ); |
| 258 |
$this->assertStatusError( 'backend-fail-internal', $status ); |
| 259 |
} |
| 260 |
|
| 261 |
public function testGetFileListAuthFail() { |
| 262 |
$this->setupAuthFailure(); |
| 263 |
$result = $this->backend->getFileList( [ |
| 264 |
'dir' => 'mwstore://local-swift-testing/c', |
| 265 |
] ); |
| 266 |
$this->expectException( FileBackendError::class ); |
| 267 |
iterator_to_array( $result ); |
| 268 |
} |
| 269 |
} |