OnlineTest.php 19 KB

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