2
0

OnlineTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Service_Amazon_S3
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Service_Amazon
  24. */
  25. require_once 'Zend/Service/Amazon/S3.php';
  26. /**
  27. * @see Zend_Http_Client_Adapter_Socket
  28. */
  29. require_once 'Zend/Http/Client/Adapter/Socket.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Service_Amazon_S3
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Service
  37. * @group Zend_Service_Amazon
  38. * @group Zend_Service_Amazon_S3
  39. */
  40. class Zend_Service_Amazon_S3_OnlineTest extends PHPUnit_Framework_TestCase
  41. {
  42. /**
  43. * Reference to Amazon service consumer object
  44. *
  45. * @var Zend_Service_Amazon_S3
  46. */
  47. protected $_amazon;
  48. /**
  49. * Socket based HTTP client adapter
  50. *
  51. * @var Zend_Http_Client_Adapter_Socket
  52. */
  53. protected $_httpClientAdapterSocket;
  54. /**
  55. * Sets up this test case
  56. *
  57. * @return void
  58. */
  59. public function setUp()
  60. {
  61. $this->_amazon = new Zend_Service_Amazon_S3(constant('TESTS_ZEND_SERVICE_AMAZON_ONLINE_ACCESSKEYID'),
  62. constant('TESTS_ZEND_SERVICE_AMAZON_ONLINE_SECRETKEY')
  63. );
  64. $this->_nosuchbucket = "nonexistingbucketnamewhichnobodyshoulduse";
  65. $this->_httpClientAdapterSocket = new Zend_Http_Client_Adapter_Socket();
  66. $this->_bucket = constant('TESTS_ZEND_SERVICE_AMAZON_S3_BUCKET');
  67. $this->_bucketEu = $this->_bucket.'-eu';
  68. $this->_amazon->getHttpClient()
  69. ->setAdapter($this->_httpClientAdapterSocket);
  70. // terms of use compliance: no more than one query per second
  71. sleep(1);
  72. }
  73. /**
  74. * Test creating bucket
  75. *
  76. * @return void
  77. */
  78. public function testCreateBucket()
  79. {
  80. $this->_amazon->createBucket($this->_bucket);
  81. $this->assertTrue($this->_amazon->isBucketAvailable($this->_bucket));
  82. $list = $this->_amazon->getBuckets();
  83. $this->assertContains($this->_bucket, $list);
  84. }
  85. /**
  86. * Test creating object
  87. *
  88. * @return void
  89. */
  90. public function testCreateObject()
  91. {
  92. $this->_amazon->createBucket($this->_bucket);
  93. $this->_amazon->putObject($this->_bucket."/zftest", "testdata");
  94. $this->assertEquals("testdata", $this->_amazon->getObject($this->_bucket."/zftest"));
  95. }
  96. /**
  97. * Get object using streaming and temp files
  98. *
  99. */
  100. public function testGetObjectStream()
  101. {
  102. $this->_amazon->createBucket($this->_bucket);
  103. $this->_amazon->putObject($this->_bucket."/zftest", "testdata");
  104. $response = $this->_amazon->getObjectStream($this->_bucket."/zftest");
  105. $this->assertTrue($response instanceof Zend_Http_Response_Stream, 'The test did not return stream response');
  106. $this->assertTrue(is_resource($response->getStream()), 'Request does not contain stream!');
  107. $stream_name = $response->getStreamName();
  108. $stream_read = stream_get_contents($response->getStream());
  109. $file_read = file_get_contents($stream_name);
  110. $this->assertEquals("testdata", $stream_read, 'Downloaded stream does not seem to match!');
  111. $this->assertEquals("testdata", $file_read, 'Downloaded file does not seem to match!');
  112. }
  113. /**
  114. * Get object using streaming and specific files
  115. *
  116. */
  117. public function testGetObjectStreamNamed()
  118. {
  119. $this->_amazon->createBucket($this->_bucket);
  120. $this->_amazon->putObject($this->_bucket."/zftest", "testdata");
  121. $outfile = tempnam(sys_get_temp_dir(), "output");
  122. $response = $this->_amazon->getObjectStream($this->_bucket."/zftest", $outfile);
  123. $this->assertTrue($response instanceof Zend_Http_Response_Stream, 'The test did not return stream response');
  124. $this->assertTrue(is_resource($response->getStream()), 'Request does not contain stream!');
  125. $this->assertEquals($outfile, $response->getStreamName());
  126. $stream_read = stream_get_contents($response->getStream());
  127. $file_read = file_get_contents($outfile);
  128. $this->assertEquals("testdata", $stream_read, 'Downloaded stream does not seem to match!');
  129. $this->assertEquals("testdata", $file_read, 'Downloaded file does not seem to match!');
  130. }
  131. /**
  132. * Test getting info
  133. *
  134. * @return void
  135. */
  136. public function testGetInfo()
  137. {
  138. $this->_amazon->createBucket($this->_bucket);
  139. $data = "testdata";
  140. $this->_amazon->putObject($this->_bucket."/zftest", $data);
  141. $info = $this->_amazon->getInfo($this->_bucket."/zftest");
  142. $this->assertEquals('"'.md5($data).'"', $info["etag"]);
  143. $this->assertEquals(strlen($data), $info["size"]);
  144. $this->_amazon->putObject($this->_bucket."/zftest.jpg", $data, null);
  145. $info = $this->_amazon->getInfo($this->_bucket."/zftest.jpg");
  146. $this->assertEquals( 'image/jpeg', $info["type"]);
  147. }
  148. public function testNoBucket()
  149. {
  150. $this->assertFalse($this->_amazon->putObject($this->_nosuchbucket."/zftest", "testdata"));
  151. $this->assertFalse($this->_amazon->getObject($this->_nosuchbucket."/zftest"));
  152. $this->assertFalse($this->_amazon->getObjectsByBucket($this->_nosuchbucket));
  153. }
  154. public function testNoObject()
  155. {
  156. $this->_amazon->createBucket($this->_bucket);
  157. $this->assertFalse($this->_amazon->getObject($this->_bucket."/zftest-no-such-object/in/there"));
  158. $this->assertFalse($this->_amazon->getInfo($this->_bucket."/zftest-no-such-object/in/there"));
  159. }
  160. public function testOverwriteObject()
  161. {
  162. $this->_amazon->createBucket($this->_bucket);
  163. $data = "testdata";
  164. $this->_amazon->putObject($this->_bucket."/zftest", $data);
  165. $info = $this->_amazon->getInfo($this->_bucket."/zftest");
  166. $this->assertEquals('"'.md5($data).'"', $info["etag"]);
  167. $this->assertEquals(strlen($data), $info["size"]);
  168. $data = "testdata with some other data";
  169. $this->_amazon->putObject($this->_bucket."/zftest", $data);
  170. $info = $this->_amazon->getInfo($this->_bucket."/zftest");
  171. $this->assertEquals('"'.md5($data).'"', $info["etag"]);
  172. $this->assertEquals(strlen($data), $info["size"]);
  173. }
  174. public function testRemoveObject()
  175. {
  176. $this->_amazon->createBucket($this->_bucket);
  177. $data = "testdata";
  178. $this->_amazon->putObject($this->_bucket."/zftest", $data);
  179. $this->_amazon->removeObject($this->_bucket."/zftest", $data);
  180. $this->assertFalse($this->_amazon->getObject($this->_bucket."/zftest"));
  181. $this->assertFalse($this->_amazon->getInfo($this->_bucket."/zftest"));
  182. }
  183. public function testRemoveBucket()
  184. {
  185. $this->_amazon->createBucket($this->_bucket);
  186. $data = "testdata";
  187. $this->_amazon->putObject($this->_bucket."/zftest", $data);
  188. $this->_amazon->cleanBucket($this->_bucket);
  189. $this->_amazon->removeBucket($this->_bucket);
  190. $this->assertFalse($this->_amazon->isBucketAvailable($this->_bucket));
  191. $this->assertFalse($this->_amazon->isObjectAvailable($this->_bucket."/zftest"));
  192. $this->assertFalse((boolean)$this->_amazon->getObjectsByBucket($this->_bucket));
  193. $list = $this->_amazon->getBuckets();
  194. $this->assertNotContains($this->_bucket, $list);
  195. }
  196. protected function _fileTest($filename, $object, $type, $exp_type, $stream = false)
  197. {
  198. if($stream) {
  199. $this->_amazon->putFile($filename, $object, array(Zend_Service_Amazon_S3::S3_CONTENT_TYPE_HEADER => $type));
  200. } else {
  201. $this->_amazon->putFileStream($filename, $object, array(Zend_Service_Amazon_S3::S3_CONTENT_TYPE_HEADER => $type));
  202. }
  203. $data = file_get_contents($filename);
  204. $this->assertTrue($this->_amazon->isObjectAvailable($object));
  205. $info = $this->_amazon->getInfo($object);
  206. $this->assertEquals('"'.md5_file($filename).'"', $info["etag"]);
  207. $this->assertEquals(filesize($filename), $info["size"]);
  208. $this->assertEquals($exp_type, $info["type"]);
  209. $fdata = $this->_amazon->getObject($object);
  210. $this->assertEquals($data, $fdata);
  211. }
  212. public function testPutFile()
  213. {
  214. $filedir = dirname(__FILE__)."/_files/";
  215. $this->_amazon->createBucket($this->_bucket);
  216. $this->_fileTest($filedir."testdata", $this->_bucket."/zftestfile", null, 'binary/octet-stream');
  217. $this->_fileTest($filedir."testdata", $this->_bucket."/zftestfile2", 'text/plain', 'text/plain');
  218. $this->_fileTest($filedir."testdata.html", $this->_bucket."/zftestfile3", null, 'text/html');
  219. $this->_fileTest($filedir."testdata.html", $this->_bucket."/zftestfile3.html", 'text/plain', 'text/plain');
  220. }
  221. public function testPutFileStream()
  222. {
  223. $filedir = dirname(__FILE__)."/_files/";
  224. $this->_amazon->createBucket($this->_bucket);
  225. $this->_fileTest($filedir."testdata", $this->_bucket."/zftestfile", null, 'binary/octet-stream', true);
  226. $this->_fileTest($filedir."testdata", $this->_bucket."/zftestfile2", 'text/plain', 'text/plain', true);
  227. $this->_fileTest($filedir."testdata.html", $this->_bucket."/zftestfile3", null, 'text/html', true);
  228. $this->_fileTest($filedir."testdata.html", $this->_bucket."/zftestfile3.html", 'text/plain', 'text/plain', true);
  229. }
  230. public function testPutNoFile()
  231. {
  232. $filedir = dirname(__FILE__)."/_files/";
  233. try {
  234. $this->_amazon->putFile($filedir."nosuchfile", $this->_bucket."/zftestfile");
  235. $this->fail("Expected exception not thrown");
  236. } catch(Zend_Service_Amazon_S3_Exception $e) {
  237. $this->assertContains("Cannot read", $e->getMessage());
  238. $this->assertContains("nosuchfile", $e->getMessage());
  239. }
  240. $this->assertFalse($this->_amazon->isObjectAvailable($this->_bucket."/zftestfile"));
  241. }
  242. /**
  243. * @depends testCreateBucket
  244. * @depends testCreateObject
  245. */
  246. public function testCopyObject()
  247. {
  248. $this->_amazon->createBucket($this->_bucket);
  249. $data = "testdata";
  250. $this->_amazon->putObject($this->_bucket."/zftest", $data);
  251. $info1 = $this->_amazon->getInfo($this->_bucket."/zftest");
  252. $this->_amazon->copyObject($this->_bucket."/zftest", $this->_bucket."/zftest2");
  253. $this->assertTrue($this->_amazon->isObjectAvailable($this->_bucket."/zftest"));
  254. $this->assertTrue($this->_amazon->isObjectAvailable($this->_bucket."/zftest2"));
  255. $info2 = $this->_amazon->getInfo($this->_bucket."/zftest2");
  256. $this->assertEquals($info1['etag'], $info2['etag']);
  257. }
  258. /**
  259. * @depends testCopyObject
  260. * @depends testRemoveObject
  261. */
  262. public function testMoveObject()
  263. {
  264. $this->_amazon->createBucket($this->_bucket);
  265. $data = "testdata";
  266. $this->_amazon->putObject($this->_bucket."/zftest", $data);
  267. $info1 = $this->_amazon->getInfo($this->_bucket."/zftest");
  268. $this->_amazon->moveObject($this->_bucket."/zftest", $this->_bucket."/zftest2");
  269. $this->assertFalse($this->_amazon->isObjectAvailable($this->_bucket."/zftest"));
  270. $this->assertTrue($this->_amazon->isObjectAvailable($this->_bucket."/zftest2"));
  271. $info2 = $this->_amazon->getInfo($this->_bucket."/zftest2");
  272. $this->assertEquals($info1['etag'], $info2['etag']);
  273. }
  274. public function testObjectEncoding()
  275. {
  276. $this->_amazon->createBucket($this->_bucket);
  277. $this->_amazon->putObject($this->_bucket."/this is a 100% test", "testdata");
  278. $this->assertEquals("testdata", $this->_amazon->getObject($this->_bucket."/this is a 100% test"));
  279. $this->_amazon->putObject($this->_bucket."/это тоже тест!", "testdata123");
  280. $this->assertEquals("testdata123", $this->_amazon->getObject($this->_bucket."/это тоже тест!"));
  281. }
  282. public function testBadNames()
  283. {
  284. try {
  285. $this->_amazon->createBucket("This is a Very Bad Name");
  286. $this->fail("Expected exception not thrown");
  287. } catch(Zend_Service_Amazon_S3_Exception $e) {
  288. $this->assertContains("contains invalid characters", $e->getMessage());
  289. }
  290. try {
  291. $this->_amazon->isBucketAvailable("This is a Very Bad Name");
  292. $this->fail("Expected exception not thrown");
  293. } catch(Zend_Uri_Exception $e) {
  294. $this->assertContains("not a valid HTTP host", $e->getMessage());
  295. }
  296. try {
  297. $this->_amazon->putObject("This is a Very Bad Name/And It Gets Worse", "testdata");
  298. $this->fail("Expected exception not thrown");
  299. } catch(Zend_Service_Amazon_S3_Exception $e) {
  300. $this->assertContains("contains invalid characters", $e->getMessage());
  301. }
  302. try {
  303. $this->_amazon->getObject("This is a Very Bad Name/And It Gets Worse");
  304. $this->fail("Expected exception not thrown");
  305. } catch(Zend_Service_Amazon_S3_Exception $e) {
  306. $this->assertContains("contains invalid characters", $e->getMessage());
  307. }
  308. try {
  309. $this->_amazon->getInfo("This is a Very Bad Name/And It Gets Worse");
  310. $this->fail("Expected exception not thrown");
  311. } catch(Zend_Service_Amazon_S3_Exception $e) {
  312. $this->assertContains("contains invalid characters", $e->getMessage());
  313. }
  314. }
  315. public function testAcl()
  316. {
  317. $this->_amazon->createBucket($this->_bucket);
  318. $filedir = dirname(__FILE__)."/_files/";
  319. $this->_amazon->putFile($filedir."testdata.html", $this->_bucket."/zftestfile.html");
  320. $this->_amazon->putFile($filedir."testdata.html", $this->_bucket."/zftestfile2.html",
  321. array(Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ));
  322. $url = 'http://' . Zend_Service_Amazon_S3::S3_ENDPOINT."/".$this->_bucket."/zftestfile.html";
  323. $data = @file_get_contents($url);
  324. $this->assertFalse($data);
  325. $url = 'http://' . Zend_Service_Amazon_S3::S3_ENDPOINT."/".$this->_bucket."/zftestfile2.html";
  326. $data = @file_get_contents($url);
  327. $this->assertEquals(file_get_contents($filedir."testdata.html"), $data);
  328. }
  329. /**
  330. * Test creating bucket with location
  331. * ZF-6728
  332. *
  333. */
  334. public function testCreateBucketEU()
  335. {
  336. $this->_amazon->createBucket($this->_bucketEu, 'EU');
  337. $this->assertTrue($this->_amazon->isBucketAvailable($this->_bucketEu));
  338. $list = $this->_amazon->getBuckets();
  339. $this->assertContains($this->_bucketEu, $list);
  340. $this->_amazon->cleanBucket($this->_bucketEu);
  341. $this->_amazon->removeBucket($this->_bucketEu);
  342. }
  343. /**
  344. * Test bucket name with /'s and encoding
  345. *
  346. * ZF-6855
  347. *
  348. */
  349. public function testObjectPath()
  350. {
  351. $this->_amazon->createBucket($this->_bucket);
  352. $filedir = dirname(__FILE__)."/_files/";
  353. $this->_amazon->putFile($filedir."testdata.html", $this->_bucket."/subdir/dir with spaces/zftestfile.html",
  354. array(Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ));
  355. $url = 'http://' . Zend_Service_Amazon_S3::S3_ENDPOINT."/".$this->_bucket."/subdir/dir%20with%20spaces/zftestfile.html";
  356. $data = @file_get_contents($url);
  357. $this->assertEquals(file_get_contents($filedir."testdata.html"), $data);
  358. }
  359. /**
  360. * Test that isObjectAvailable() works if object name contains spaces
  361. *
  362. * @depends testCreateBucket
  363. * @depends testObjectPath
  364. *
  365. * ZF-10017
  366. */
  367. public function testIsObjectAvailableWithSpacesInKey()
  368. {
  369. $this->_amazon->createBucket($this->_bucket);
  370. $filedir = dirname(__FILE__)."/_files/";
  371. $key = $this->_bucket.'/subdir/another dir with spaces/zftestfile.html';
  372. $this->_amazon->putFile($filedir."testdata.html", $key);
  373. $this->assertTrue($this->_amazon->isObjectAvailable($key));
  374. }
  375. /**
  376. * Test creating object with https
  377. *
  378. * ZF-7029
  379. */
  380. public function testCreateObjectSSL()
  381. {
  382. $this->_amazon->setEndpoint('https://s3.amazonaws.com');
  383. $this->assertEquals('https://s3.amazonaws.com', $this->_amazon->getEndpoint()->getUri());
  384. $this->_amazon->createBucket($this->_bucket);
  385. $this->_amazon->putObject($this->_bucket."/zftest", "testdata");
  386. $this->assertEquals("testdata", $this->_amazon->getObject($this->_bucket."/zftest"));
  387. }
  388. /**
  389. * Test creating bucket with IP
  390. *
  391. * ZF-6686
  392. */
  393. public function testBucketIPMask()
  394. {
  395. try {
  396. $this->_amazon->createBucket("127.0.0.1");
  397. $this->fail("Failed to throw expected exception");
  398. } catch(Zend_Service_Amazon_S3_Exception $e) {
  399. $this->assertContains("cannot be an IP address", $e->getMessage());
  400. }
  401. $this->_amazon->createBucket("123-456-789-123");
  402. $this->assertTrue($this->_amazon->isBucketAvailable("123-456-789-123"));
  403. $this->_amazon->removeBucket("123-456-789-123");
  404. }
  405. /**
  406. * @see ZF-7773
  407. */
  408. public function testGetObjectsByBucketParams()
  409. {
  410. $this->_amazon->createBucket("testgetobjectparams1");
  411. $this->_amazon->putObject("testgetobjectparams1/zftest1", "testdata");
  412. $this->_amazon->putObject("testgetobjectparams1/zftest2", "testdata");
  413. $list = $this->_amazon->getObjectsByBucket("testgetobjectparams1", array('max-keys' => 1));
  414. $this->assertEquals(1, count($list));
  415. $this->_amazon->removeObject("testgetobjectparams1/zftest1", "testdata");
  416. $this->_amazon->removeObject("testgetobjectparams1/zftest2", "testdata");
  417. $this->_amazon->removeBucket("testgetobjectparams1");
  418. }
  419. /**
  420. * @see ZF-10219
  421. */
  422. public function testVersionBucket()
  423. {
  424. $this->_amazon->createBucket($this->_bucket);
  425. $response= $this->_amazon->_makeRequest('GET', $this->_bucket.'/?versions', array('versions'=>''));
  426. $this->assertNotNull($response,'The response for the ?versions is empty');
  427. $xml = new SimpleXMLElement($response->getBody());
  428. $this->assertEquals((string) $xml->Name,$this->_bucket,'The bucket name in XML response is not valid');
  429. }
  430. /**
  431. * @see ZF-11401
  432. */
  433. public function testCommonPrefixes()
  434. {
  435. $this->_amazon->createBucket($this->_bucket);
  436. $this->_amazon->putObject($this->_bucket.'/test-folder/test1','test');
  437. $this->_amazon->putObject($this->_bucket.'/test-folder/test2-folder/','');
  438. $params= array(
  439. 'prefix' => 'test-folder/',
  440. 'delimiter' => '/'
  441. );
  442. $response= $this->_amazon->getObjectsAndPrefixesByBucket($this->_bucket,$params);
  443. $this->assertEquals($response['objects'][0],'test-folder/test1');
  444. $this->assertEquals($response['prefixes'][0],'test-folder/test2-folder/');
  445. }
  446. public function tearDown()
  447. {
  448. unset($this->_amazon->debug);
  449. $this->_amazon->cleanBucket($this->_bucket);
  450. $this->_amazon->removeBucket($this->_bucket);
  451. sleep(1);
  452. }
  453. }
  454. /**
  455. * @category Zend
  456. * @package Zend_Service_Amazon
  457. * @subpackage UnitTests
  458. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  459. * @license http://framework.zend.com/license/new-bsd New BSD License
  460. * @group Zend_Service
  461. * @group Zend_Service_Amazon
  462. * @group Zend_Service_Amazon_S3
  463. */
  464. class Zend_Service_Amazon_S3_OnlineTest_Skip extends PHPUnit_Framework_TestCase
  465. {
  466. public function setUp()
  467. {
  468. $this->markTestSkipped('Zend_Service_Amazon_S3 online tests not enabled with an access key ID in '
  469. . 'TestConfiguration.php');
  470. }
  471. public function testNothing()
  472. {
  473. }
  474. }