2
0

OnlineTest.php 19 KB

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