GoogleCloudStorageTest.php 23 KB

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