OnlineTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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-2009 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-2009 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. public function testObjectEncoding()
  246. {
  247. $this->_amazon->createBucket($this->_bucket);
  248. $this->_amazon->putObject($this->_bucket."/this is a 100% test", "testdata");
  249. $this->assertEquals("testdata", $this->_amazon->getObject($this->_bucket."/this is a 100% test"));
  250. $this->_amazon->putObject($this->_bucket."/это тоже тест!", "testdata123");
  251. $this->assertEquals("testdata123", $this->_amazon->getObject($this->_bucket."/это тоже тест!"));
  252. }
  253. public function testBadNames()
  254. {
  255. try {
  256. $this->_amazon->createBucket("This is a Very Bad Name");
  257. $this->fail("Expected exception not thrown");
  258. } catch(Zend_Service_Amazon_S3_Exception $e) {
  259. $this->assertContains("contains invalid characters", $e->getMessage());
  260. }
  261. try {
  262. $this->_amazon->isBucketAvailable("This is a Very Bad Name");
  263. $this->fail("Expected exception not thrown");
  264. } catch(Zend_Uri_Exception $e) {
  265. $this->assertContains("not a valid HTTP host", $e->getMessage());
  266. }
  267. try {
  268. $this->_amazon->putObject("This is a Very Bad Name/And It Gets Worse", "testdata");
  269. $this->fail("Expected exception not thrown");
  270. } catch(Zend_Service_Amazon_S3_Exception $e) {
  271. $this->assertContains("contains invalid characters", $e->getMessage());
  272. }
  273. try {
  274. $this->_amazon->getObject("This is a Very Bad Name/And It Gets Worse");
  275. $this->fail("Expected exception not thrown");
  276. } catch(Zend_Service_Amazon_S3_Exception $e) {
  277. $this->assertContains("contains invalid characters", $e->getMessage());
  278. }
  279. try {
  280. $this->_amazon->getInfo("This is a Very Bad Name/And It Gets Worse");
  281. $this->fail("Expected exception not thrown");
  282. } catch(Zend_Service_Amazon_S3_Exception $e) {
  283. $this->assertContains("contains invalid characters", $e->getMessage());
  284. }
  285. }
  286. public function testAcl()
  287. {
  288. $this->_amazon->createBucket($this->_bucket);
  289. $filedir = dirname(__FILE__)."/_files/";
  290. $this->_amazon->putFile($filedir."testdata.html", $this->_bucket."/zftestfile.html");
  291. $this->_amazon->putFile($filedir."testdata.html", $this->_bucket."/zftestfile2.html",
  292. array(Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ));
  293. $url = 'http://' . Zend_Service_Amazon_S3::S3_ENDPOINT."/".$this->_bucket."/zftestfile.html";
  294. $data = @file_get_contents($url);
  295. $this->assertFalse($data);
  296. $url = 'http://' . Zend_Service_Amazon_S3::S3_ENDPOINT."/".$this->_bucket."/zftestfile2.html";
  297. $data = @file_get_contents($url);
  298. $this->assertEquals(file_get_contents($filedir."testdata.html"), $data);
  299. }
  300. /**
  301. * Test creating bucket with location
  302. * ZF-6728
  303. *
  304. */
  305. public function testCreateBucketEU()
  306. {
  307. $this->_amazon->createBucket($this->_bucket, 'EU');
  308. $this->assertTrue($this->_amazon->isBucketAvailable($this->_bucket));
  309. $list = $this->_amazon->getBuckets();
  310. $this->assertContains($this->_bucket, $list);
  311. }
  312. /**
  313. * Test bucket name with /'s and encoding
  314. *
  315. * ZF-6855
  316. *
  317. */
  318. public function testObjectPath()
  319. {
  320. $this->_amazon->createBucket($this->_bucket);
  321. $filedir = dirname(__FILE__)."/_files/";
  322. $this->_amazon->putFile($filedir."testdata.html", $this->_bucket."/subdir/dir with spaces/zftestfile.html",
  323. array(Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ));
  324. $url = 'http://' . Zend_Service_Amazon_S3::S3_ENDPOINT."/".$this->_bucket."/subdir/dir%20with%20spaces/zftestfile.html";
  325. $data = @file_get_contents($url);
  326. $this->assertEquals(file_get_contents($filedir."testdata.html"), $data);
  327. }
  328. /**
  329. * Test creating object with https
  330. *
  331. * ZF-7029
  332. */
  333. public function testCreateObjectSSL()
  334. {
  335. $this->_amazon->setEndpoint('https://s3.amazonaws.com');
  336. $this->assertEquals('https://s3.amazonaws.com', $this->_amazon->getEndpoint()->getUri());
  337. $this->_amazon->createBucket($this->_bucket);
  338. $this->_amazon->putObject($this->_bucket."/zftest", "testdata");
  339. $this->assertEquals("testdata", $this->_amazon->getObject($this->_bucket."/zftest"));
  340. }
  341. /**
  342. * Test creating bucket with IP
  343. *
  344. * ZF-6686
  345. */
  346. public function testBucketIPMask()
  347. {
  348. try {
  349. $this->_amazon->createBucket("127.0.0.1");
  350. $this->fail("Failed to throw expected exception");
  351. } catch(Zend_Service_Amazon_S3_Exception $e) {
  352. $this->assertContains("cannot be an IP address", $e->getMessage());
  353. }
  354. $this->_amazon->createBucket("123-456-789-123");
  355. $this->assertTrue($this->_amazon->isBucketAvailable("123-456-789-123"));
  356. $this->_amazon->removeBucket("123-456-789-123");
  357. }
  358. /**
  359. * @see ZF-7773
  360. */
  361. public function testGetObjectsByBucketParams()
  362. {
  363. $this->_amazon->createBucket("testgetobjectparams1");
  364. $this->_amazon->putObject("testgetobjectparams1/zftest1", "testdata");
  365. $this->_amazon->putObject("testgetobjectparams1/zftest2", "testdata");
  366. $list = $this->_amazon->getObjectsByBucket("testgetobjectparams1", array('max-keys' => 1));
  367. $this->assertEquals(1, count($list));
  368. $this->_amazon->removeObject("testgetobjectparams1/zftest1", "testdata");
  369. $this->_amazon->removeObject("testgetobjectparams1/zftest2", "testdata");
  370. $this->_amazon->removeBucket("testgetobjectparams1");
  371. }
  372. public function tearDown()
  373. {
  374. unset($this->_amazon->debug);
  375. $this->_amazon->cleanBucket($this->_bucket);
  376. $this->_amazon->removeBucket($this->_bucket);
  377. }
  378. }
  379. /**
  380. * @category Zend
  381. * @package Zend_Service_Amazon
  382. * @subpackage UnitTests
  383. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  384. * @license http://framework.zend.com/license/new-bsd New BSD License
  385. * @group Zend_Service
  386. * @group Zend_Service_Amazon
  387. * @group Zend_Service_Amazon_S3
  388. */
  389. class Zend_Service_Amazon_S3_OnlineTest_Skip extends PHPUnit_Framework_TestCase
  390. {
  391. public function setUp()
  392. {
  393. $this->markTestSkipped('Zend_Service_Amazon_S3 online tests not enabled with an access key ID in '
  394. . 'TestConfiguration.php');
  395. }
  396. public function testNothing()
  397. {
  398. }
  399. }