GoogleCloudStorageTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. <?php
  2. use Google\Auth\HttpHandler\HttpHandlerFactory;
  3. use Google\Cloud\Core\Exception\BadRequestException;
  4. use Google\Cloud\Core\Exception\NotFoundException;
  5. use Google\Cloud\Storage\Bucket;
  6. use Google\Cloud\Storage\Connection\ConnectionInterface;
  7. use Google\Cloud\Storage\StorageClient;
  8. use Google\Cloud\Storage\StorageObject;
  9. use GuzzleHttp\Client;
  10. use PrivateBin\Data\GoogleCloudStorage;
  11. class GoogleCloudStorageTest extends PHPUnit_Framework_TestCase
  12. {
  13. private static $_client;
  14. private static $_bucket;
  15. public static function setUpBeforeClass()
  16. {
  17. $httpClient = new Client(array('debug'=>false));
  18. $handler = HttpHandlerFactory::build($httpClient);
  19. $name = 'pb-';
  20. $alphabet = 'abcdefghijklmnopqrstuvwxyz';
  21. for ($i = 0; $i < 29; ++$i) {
  22. $name .= $alphabet[rand(0, strlen($alphabet) - 1)];
  23. }
  24. self::$_client = new StorageClientStub(array());
  25. self::$_bucket = self::$_client->createBucket($name);
  26. }
  27. public function setUp()
  28. {
  29. // do not report E_NOTICE as fsouza/fake-gcs-server does not return a `generation` value in the response
  30. // which the Google Cloud Storage PHP library expects.
  31. error_reporting(E_ERROR | E_WARNING | E_PARSE);
  32. $this->_model = GoogleCloudStorage::getInstance(array(
  33. 'bucket' => self::$_bucket->name(),
  34. 'prefix' => 'pastes',
  35. 'client' => self::$_client, ));
  36. }
  37. public function tearDown()
  38. {
  39. foreach (self::$_bucket->objects() as $object) {
  40. $object->delete();
  41. }
  42. error_reporting(E_ALL);
  43. }
  44. public static function tearDownAfterClass()
  45. {
  46. self::$_bucket->delete();
  47. }
  48. public function testFileBasedDataStoreWorks()
  49. {
  50. $this->_model->delete(Helper::getPasteId());
  51. // storing pastes
  52. $paste = Helper::getPaste(2, array('expire_date' => 1344803344));
  53. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does not yet exist');
  54. $this->assertTrue($this->_model->create(Helper::getPasteId(), $paste), 'store new paste');
  55. $this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists after storing it');
  56. $this->assertFalse($this->_model->create(Helper::getPasteId(), $paste), 'unable to store the same paste twice');
  57. $this->assertEquals($paste, $this->_model->read(Helper::getPasteId()));
  58. // storing comments
  59. $this->assertFalse($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment does not yet exist');
  60. $this->assertTrue($this->_model->createComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId(), Helper::getComment()), 'store comment');
  61. $this->assertTrue($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment exists after storing it');
  62. $this->assertFalse($this->_model->createComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId(), Helper::getComment()), 'unable to store the same comment twice');
  63. $comment = Helper::getComment();
  64. $comment['id'] = Helper::getCommentId();
  65. $comment['parentid'] = Helper::getPasteId();
  66. $this->assertEquals(
  67. array($comment['meta']['created'] => $comment),
  68. $this->_model->readComments(Helper::getPasteId())
  69. );
  70. // deleting pastes
  71. $this->_model->delete(Helper::getPasteId());
  72. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste successfully deleted');
  73. $this->assertFalse($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment was deleted with paste');
  74. $this->assertFalse($this->_model->read(Helper::getPasteId()), 'paste can no longer be found');
  75. }
  76. /**
  77. * pastes a-g are expired and should get deleted, x never expires and y-z expire in an hour
  78. */
  79. public function testPurge()
  80. {
  81. $expired = Helper::getPaste(2, array('expire_date' => 1344803344));
  82. $paste = Helper::getPaste(2, array('expire_date' => time() + 3600));
  83. $keys = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'x', 'y', 'z');
  84. $ids = array();
  85. foreach ($keys as $key) {
  86. $ids[$key] = hash('fnv164', $key);
  87. $this->assertFalse($this->_model->exists($ids[$key]), "paste $key does not yet exist");
  88. if (in_array($key, array('x', 'y', 'z'))) {
  89. $this->assertTrue($this->_model->create($ids[$key], $paste), "store $key paste");
  90. } elseif ($key === 'x') {
  91. $this->assertTrue($this->_model->create($ids[$key], Helper::getPaste()), "store $key paste");
  92. } else {
  93. $this->assertTrue($this->_model->create($ids[$key], $expired), "store $key paste");
  94. }
  95. $this->assertTrue($this->_model->exists($ids[$key]), "paste $key exists after storing it");
  96. }
  97. $this->_model->purge(10);
  98. foreach ($ids as $key => $id) {
  99. if (in_array($key, array('x', 'y', 'z'))) {
  100. $this->assertTrue($this->_model->exists($id), "paste $key exists after purge");
  101. $this->_model->delete($id);
  102. } else {
  103. $this->assertFalse($this->_model->exists($id), "paste $key was purged");
  104. }
  105. }
  106. }
  107. public function testErrorDetection()
  108. {
  109. $this->_model->delete(Helper::getPasteId());
  110. $paste = Helper::getPaste(2, array('expire' => "Invalid UTF-8 sequence: \xB1\x31"));
  111. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does not yet exist');
  112. $this->assertFalse($this->_model->create(Helper::getPasteId(), $paste), 'unable to store broken paste');
  113. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does still not exist');
  114. }
  115. public function testCommentErrorDetection()
  116. {
  117. $this->_model->delete(Helper::getPasteId());
  118. $comment = Helper::getComment(1, array('nickname' => "Invalid UTF-8 sequence: \xB1\x31"));
  119. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does not yet exist');
  120. $this->assertTrue($this->_model->create(Helper::getPasteId(), Helper::getPaste()), 'store new paste');
  121. $this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists after storing it');
  122. $this->assertFalse($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment does not yet exist');
  123. $this->assertFalse($this->_model->createComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId(), $comment), 'unable to store broken comment');
  124. $this->assertFalse($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment does still not exist');
  125. }
  126. }
  127. /**
  128. * Class StorageClientStub provides a limited stub for performing the unit test
  129. */
  130. class StorageClientStub extends StorageClient
  131. {
  132. private $_config = null;
  133. private $_connection = null;
  134. private $_buckets = array();
  135. public function __construct(array $config = array())
  136. {
  137. $this->_config = $config;
  138. $this->_connection = new ConnectionInterfaceStub();
  139. }
  140. public function bucket($name, $userProject = false)
  141. {
  142. if (!key_exists($name, $this->_buckets)) {
  143. $b = new BucketStub($this->_connection, $name, array(), $this);
  144. $this->_buckets[$name] = $b;
  145. }
  146. return $this->_buckets[$name];
  147. }
  148. /**
  149. * @throws \Google\Cloud\Core\Exception\NotFoundException
  150. */
  151. public function deleteBucket($name)
  152. {
  153. if (key_exists($name, $this->_buckets)) {
  154. unset($this->_buckets[$name]);
  155. } else {
  156. throw new NotFoundException();
  157. }
  158. }
  159. public function buckets(array $options = array())
  160. {
  161. throw new BadMethodCallException('not supported by this stub');
  162. }
  163. public function registerStreamWrapper($protocol = null)
  164. {
  165. throw new BadMethodCallException('not supported by this stub');
  166. }
  167. public function unregisterStreamWrapper($protocol = null)
  168. {
  169. throw new BadMethodCallException('not supported by this stub');
  170. }
  171. public function signedUrlUploader($uri, $data, array $options = array())
  172. {
  173. throw new BadMethodCallException('not supported by this stub');
  174. }
  175. public function timestamp(\DateTimeInterface $timestamp, $nanoSeconds = null)
  176. {
  177. throw new BadMethodCallException('not supported by this stub');
  178. }
  179. public function getServiceAccount(array $options = array())
  180. {
  181. throw new BadMethodCallException('not supported by this stub');
  182. }
  183. public function hmacKeys(array $options = array())
  184. {
  185. throw new BadMethodCallException('not supported by this stub');
  186. }
  187. public function hmacKey($accessId, $projectId = null, array $metadata = array())
  188. {
  189. throw new BadMethodCallException('not supported by this stub');
  190. }
  191. public function createHmacKey($serviceAccountEmail, array $options = array())
  192. {
  193. throw new BadMethodCallException('not supported by this stub');
  194. }
  195. public function createBucket($name, array $options = array())
  196. {
  197. if (key_exists($name, $this->_buckets)) {
  198. throw new BadRequestException('already exists');
  199. }
  200. $b = new BucketStub($this->_connection, $name, array(), $this);
  201. $this->_buckets[$name] = $b;
  202. return $b;
  203. }
  204. }
  205. /**
  206. * Class BucketStub stubs a GCS bucket.
  207. */
  208. class BucketStub extends Bucket
  209. {
  210. public $_objects;
  211. private $_name;
  212. private $_info;
  213. private $_connection;
  214. private $_client;
  215. public function __construct(ConnectionInterface $connection, $name, array $info = array(), $client = null)
  216. {
  217. $this->_name = $name;
  218. $this->_info = $info;
  219. $this->_connection = $connection;
  220. $this->_objects = array();
  221. $this->_client = $client;
  222. }
  223. public function acl()
  224. {
  225. throw new BadMethodCallException('not supported by this stub');
  226. }
  227. public function defaultAcl()
  228. {
  229. throw new BadMethodCallException('not supported by this stub');
  230. }
  231. public function exists()
  232. {
  233. return true;
  234. }
  235. public function upload($data, array $options = array())
  236. {
  237. if (!is_string($data) || !key_exists('name', $options)) {
  238. throw new BadMethodCallException('not supported by this stub');
  239. }
  240. $name = $options['name'];
  241. $generation = '1';
  242. $o = new StorageObjectStub($this->_connection, $name, $this, $generation, $options);
  243. $this->_objects[$options['name']] = $o;
  244. $o->setData($data);
  245. }
  246. public function uploadAsync($data, array $options = array())
  247. {
  248. throw new BadMethodCallException('not supported by this stub');
  249. }
  250. public function getResumableUploader($data, array $options = array())
  251. {
  252. throw new BadMethodCallException('not supported by this stub');
  253. }
  254. public function getStreamableUploader($data, array $options = array())
  255. {
  256. throw new BadMethodCallException('not supported by this stub');
  257. }
  258. public function object($name, array $options = array())
  259. {
  260. if (key_exists($name, $this->_objects)) {
  261. return $this->_objects[$name];
  262. } else {
  263. return new StorageObjectStub($this->_connection, $name, $this, null, $options);
  264. }
  265. }
  266. public function objects(array $options = array())
  267. {
  268. $prefix = key_exists('prefix', $options) ? $options['prefix'] : '';
  269. return new CallbackFilterIterator(
  270. new ArrayIterator($this->_objects),
  271. function ($current, $key, $iterator) use ($prefix) {
  272. return substr($key, 0, strlen($prefix)) == $prefix;
  273. }
  274. );
  275. }
  276. public function createNotification($topic, array $options = array())
  277. {
  278. throw new BadMethodCallException('not supported by this stub');
  279. }
  280. public function notification($id)
  281. {
  282. throw new BadMethodCallException('not supported by this stub');
  283. }
  284. public function notifications(array $options = array())
  285. {
  286. throw new BadMethodCallException('not supported by this stub');
  287. }
  288. public function delete(array $options = array())
  289. {
  290. $this->_client->deleteBucket($this->_name);
  291. }
  292. public function update(array $options = array())
  293. {
  294. throw new BadMethodCallException('not supported by this stub');
  295. }
  296. public function compose(array $sourceObjects, $name, array $options = array())
  297. {
  298. throw new BadMethodCallException('not supported by this stub');
  299. }
  300. public function info(array $options = array())
  301. {
  302. throw new BadMethodCallException('not supported by this stub');
  303. }
  304. public function reload(array $options = array())
  305. {
  306. throw new BadMethodCallException('not supported by this stub');
  307. }
  308. public function name()
  309. {
  310. return $this->_name;
  311. }
  312. public static function lifecycle(array $lifecycle = array())
  313. {
  314. throw new BadMethodCallException('not supported by this stub');
  315. }
  316. public function currentLifecycle(array $options = array())
  317. {
  318. throw new BadMethodCallException('not supported by this stub');
  319. }
  320. public function isWritable($file = null)
  321. {
  322. throw new BadMethodCallException('not supported by this stub');
  323. }
  324. public function iam()
  325. {
  326. throw new BadMethodCallException('not supported by this stub');
  327. }
  328. public function lockRetentionPolicy(array $options = array())
  329. {
  330. throw new BadMethodCallException('not supported by this stub');
  331. }
  332. public function signedUrl($expires, array $options = array())
  333. {
  334. throw new BadMethodCallException('not supported by this stub');
  335. }
  336. public function generateSignedPostPolicyV4($objectName, $expires, array $options = array())
  337. {
  338. throw new BadMethodCallException('not supported by this stub');
  339. }
  340. }
  341. /**
  342. * Class StorageObjectStub stubs a GCS storage object.
  343. */
  344. class StorageObjectStub extends StorageObject
  345. {
  346. private $_name;
  347. private $_data;
  348. private $_info;
  349. private $_bucket;
  350. private $_generation;
  351. private $_exists = false;
  352. private $_connection;
  353. public function __construct(ConnectionInterface $connection, $name, $bucket, $generation = null, array $info = array(), $encryptionKey = null, $encryptionKeySHA256 = null)
  354. {
  355. $this->_name = $name;
  356. $this->_bucket = $bucket;
  357. $this->_generation = $generation;
  358. $this->_info = $info;
  359. $this->_connection = $connection;
  360. }
  361. public function acl()
  362. {
  363. throw new BadMethodCallException('not supported by this stub');
  364. }
  365. public function exists(array $options = array())
  366. {
  367. return key_exists($this->_name, $this->_bucket->_objects);
  368. }
  369. /**
  370. * @throws NotFoundException
  371. */
  372. public function delete(array $options = array())
  373. {
  374. if (key_exists($this->_name, $this->_bucket->_objects)) {
  375. unset($this->_bucket->_objects[$this->_name]);
  376. } else {
  377. throw new NotFoundException('key ' . $this->_name . ' not found.');
  378. }
  379. }
  380. /**
  381. * @throws NotFoundException
  382. */
  383. public function update(array $metadata, array $options = array())
  384. {
  385. if (!$this->_exists) {
  386. throw new NotFoundException('key ' . $this->_name . ' not found.');
  387. }
  388. $this->_info = $metadata;
  389. }
  390. public function copy($destination, array $options = array())
  391. {
  392. throw new BadMethodCallException('not supported by this stub');
  393. }
  394. public function rewrite($destination, array $options = array())
  395. {
  396. throw new BadMethodCallException('not supported by this stub');
  397. }
  398. public function rename($name, array $options = array())
  399. {
  400. throw new BadMethodCallException('not supported by this stub');
  401. }
  402. /**
  403. * @throws NotFoundException
  404. */
  405. public function downloadAsString(array $options = array())
  406. {
  407. if (!$this->_exists) {
  408. throw new NotFoundException('key ' . $this->_name . ' not found.');
  409. }
  410. return $this->_data;
  411. }
  412. public function downloadToFile($path, array $options = array())
  413. {
  414. throw new BadMethodCallException('not supported by this stub');
  415. }
  416. public function downloadAsStream(array $options = array())
  417. {
  418. throw new BadMethodCallException('not supported by this stub');
  419. }
  420. public function downloadAsStreamAsync(array $options = array())
  421. {
  422. throw new BadMethodCallException('not supported by this stub');
  423. }
  424. public function signedUrl($expires, array $options = array())
  425. {
  426. throw new BadMethodCallException('not supported by this stub');
  427. }
  428. public function signedUploadUrl($expires, array $options = array())
  429. {
  430. throw new BadMethodCallException('not supported by this stub');
  431. }
  432. public function beginSignedUploadSession(array $options = array())
  433. {
  434. throw new BadMethodCallException('not supported by this stub');
  435. }
  436. public function info(array $options = array())
  437. {
  438. return key_exists('metadata',$this->_info) ? $this->_info['metadata'] : array();
  439. }
  440. public function reload(array $options = array())
  441. {
  442. throw new BadMethodCallException('not supported by this stub');
  443. }
  444. public function name()
  445. {
  446. return $this->_name;
  447. }
  448. public function identity()
  449. {
  450. throw new BadMethodCallException('not supported by this stub');
  451. }
  452. public function gcsUri()
  453. {
  454. return sprintf(
  455. 'gs://%s/%s',
  456. $this->_bucket->name(),
  457. $this->_name
  458. );
  459. }
  460. public function setData($data)
  461. {
  462. $this->_data = $data;
  463. $this->_exists = true;
  464. }
  465. }
  466. /**
  467. * Class ConnectionInterfaceStub required for the stubs.
  468. */
  469. class ConnectionInterfaceStub implements ConnectionInterface
  470. {
  471. public function deleteAcl(array $args = array())
  472. {
  473. throw new BadMethodCallException('not supported by this stub');
  474. }
  475. public function getAcl(array $args = array())
  476. {
  477. throw new BadMethodCallException('not supported by this stub');
  478. }
  479. public function listAcl(array $args = array())
  480. {
  481. throw new BadMethodCallException('not supported by this stub');
  482. }
  483. public function insertAcl(array $args = array())
  484. {
  485. throw new BadMethodCallException('not supported by this stub');
  486. }
  487. public function patchAcl(array $args = array())
  488. {
  489. throw new BadMethodCallException('not supported by this stub');
  490. }
  491. public function deleteBucket(array $args = array())
  492. {
  493. throw new BadMethodCallException('not supported by this stub');
  494. }
  495. public function getBucket(array $args = array())
  496. {
  497. throw new BadMethodCallException('not supported by this stub');
  498. }
  499. public function listBuckets(array $args = array())
  500. {
  501. throw new BadMethodCallException('not supported by this stub');
  502. }
  503. public function insertBucket(array $args = array())
  504. {
  505. throw new BadMethodCallException('not supported by this stub');
  506. }
  507. public function getBucketIamPolicy(array $args)
  508. {
  509. throw new BadMethodCallException('not supported by this stub');
  510. }
  511. public function setBucketIamPolicy(array $args)
  512. {
  513. throw new BadMethodCallException('not supported by this stub');
  514. }
  515. public function testBucketIamPermissions(array $args)
  516. {
  517. throw new BadMethodCallException('not supported by this stub');
  518. }
  519. public function patchBucket(array $args = array())
  520. {
  521. throw new BadMethodCallException('not supported by this stub');
  522. }
  523. public function deleteObject(array $args = array())
  524. {
  525. throw new BadMethodCallException('not supported by this stub');
  526. }
  527. public function copyObject(array $args = array())
  528. {
  529. throw new BadMethodCallException('not supported by this stub');
  530. }
  531. public function rewriteObject(array $args = array())
  532. {
  533. throw new BadMethodCallException('not supported by this stub');
  534. }
  535. public function composeObject(array $args = array())
  536. {
  537. throw new BadMethodCallException('not supported by this stub');
  538. }
  539. public function getObject(array $args = array())
  540. {
  541. throw new BadMethodCallException('not supported by this stub');
  542. }
  543. public function listObjects(array $args = array())
  544. {
  545. throw new BadMethodCallException('not supported by this stub');
  546. }
  547. public function patchObject(array $args = array())
  548. {
  549. throw new BadMethodCallException('not supported by this stub');
  550. }
  551. public function downloadObject(array $args = array())
  552. {
  553. throw new BadMethodCallException('not supported by this stub');
  554. }
  555. public function insertObject(array $args = array())
  556. {
  557. throw new BadMethodCallException('not supported by this stub');
  558. }
  559. public function getNotification(array $args = array())
  560. {
  561. throw new BadMethodCallException('not supported by this stub');
  562. }
  563. public function deleteNotification(array $args = array())
  564. {
  565. throw new BadMethodCallException('not supported by this stub');
  566. }
  567. public function insertNotification(array $args = array())
  568. {
  569. throw new BadMethodCallException('not supported by this stub');
  570. }
  571. public function listNotifications(array $args = array())
  572. {
  573. throw new BadMethodCallException('not supported by this stub');
  574. }
  575. public function getServiceAccount(array $args = array())
  576. {
  577. throw new BadMethodCallException('not supported by this stub');
  578. }
  579. public function lockRetentionPolicy(array $args = array())
  580. {
  581. throw new BadMethodCallException('not supported by this stub');
  582. }
  583. public function createHmacKey(array $args = array())
  584. {
  585. throw new BadMethodCallException('not supported by this stub');
  586. }
  587. public function deleteHmacKey(array $args = array())
  588. {
  589. throw new BadMethodCallException('not supported by this stub');
  590. }
  591. public function getHmacKey(array $args = array())
  592. {
  593. throw new BadMethodCallException('not supported by this stub');
  594. }
  595. public function updateHmacKey(array $args = array())
  596. {
  597. throw new BadMethodCallException('not supported by this stub');
  598. }
  599. public function listHmacKeys(array $args = array())
  600. {
  601. throw new BadMethodCallException('not supported by this stub');
  602. }
  603. }