2
0

OnlineTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: OnlineTest.php 11973 2008-10-15 16:00:56Z matthew $
  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
  37. * @subpackage UnitTests
  38. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. */
  41. class Zend_Service_Amazon_S3_OnlineTest extends PHPUnit_Framework_TestCase
  42. {
  43. /**
  44. * Reference to Amazon service consumer object
  45. *
  46. * @var Zend_Service_Amazon_S3
  47. */
  48. protected $_amazon;
  49. /**
  50. * Socket based HTTP client adapter
  51. *
  52. * @var Zend_Http_Client_Adapter_Socket
  53. */
  54. protected $_httpClientAdapterSocket;
  55. /**
  56. * Sets up this test case
  57. *
  58. * @return void
  59. */
  60. public function setUp()
  61. {
  62. $this->_amazon = new Zend_Service_Amazon_S3(constant('TESTS_ZEND_SERVICE_AMAZON_ONLINE_ACCESSKEYID'),
  63. constant('TESTS_ZEND_SERVICE_AMAZON_ONLINE_SECRETKEY')
  64. );
  65. $this->_nosuchbucket = "nonexistingbucketnamewhichnobodyshoulduse";
  66. $this->_httpClientAdapterSocket = new Zend_Http_Client_Adapter_Socket();
  67. $this->_bucket = constant('TESTS_ZEND_SERVICE_AMAZON_S3_BUCKET');
  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. * Test getting info
  98. *
  99. * @return void
  100. */
  101. public function testGetInfo()
  102. {
  103. $this->_amazon->createBucket($this->_bucket);
  104. $data = "testdata";
  105. $this->_amazon->putObject($this->_bucket."/zftest", $data);
  106. $info = $this->_amazon->getInfo($this->_bucket."/zftest");
  107. $this->assertEquals('"'.md5($data).'"', $info["etag"]);
  108. $this->assertEquals(strlen($data), $info["size"]);
  109. $this->_amazon->putObject($this->_bucket."/zftest.jpg", $data, null);
  110. $info = $this->_amazon->getInfo($this->_bucket."/zftest.jpg");
  111. $this->assertEquals( 'image/jpeg', $info["type"]);
  112. }
  113. public function testNoBucket()
  114. {
  115. $this->assertFalse($this->_amazon->putObject($this->_nosuchbucket."/zftest", "testdata"));
  116. $this->assertFalse($this->_amazon->getObject($this->_nosuchbucket."/zftest"));
  117. $this->assertFalse($this->_amazon->getObjectsByBucket($this->_nosuchbucket));
  118. }
  119. public function testNoObject()
  120. {
  121. $this->_amazon->createBucket($this->_bucket);
  122. $this->assertFalse($this->_amazon->getObject($this->_bucket."/zftest-no-such-object/in/there"));
  123. $this->assertFalse($this->_amazon->getInfo($this->_bucket."/zftest-no-such-object/in/there"));
  124. }
  125. public function testOverwriteObject()
  126. {
  127. $this->_amazon->createBucket($this->_bucket);
  128. $data = "testdata";
  129. $this->_amazon->putObject($this->_bucket."/zftest", $data);
  130. $info = $this->_amazon->getInfo($this->_bucket."/zftest");
  131. $this->assertEquals('"'.md5($data).'"', $info["etag"]);
  132. $this->assertEquals(strlen($data), $info["size"]);
  133. $data = "testdata with some other data";
  134. $this->_amazon->putObject($this->_bucket."/zftest", $data);
  135. $info = $this->_amazon->getInfo($this->_bucket."/zftest");
  136. $this->assertEquals('"'.md5($data).'"', $info["etag"]);
  137. $this->assertEquals(strlen($data), $info["size"]);
  138. }
  139. public function testRemoveObject()
  140. {
  141. $this->_amazon->createBucket($this->_bucket);
  142. $data = "testdata";
  143. $this->_amazon->putObject($this->_bucket."/zftest", $data);
  144. $this->_amazon->removeObject($this->_bucket."/zftest", $data);
  145. $this->assertFalse($this->_amazon->getObject($this->_bucket."/zftest"));
  146. $this->assertFalse($this->_amazon->getInfo($this->_bucket."/zftest"));
  147. }
  148. public function testRemoveBucket()
  149. {
  150. $this->_amazon->createBucket($this->_bucket);
  151. $data = "testdata";
  152. $this->_amazon->putObject($this->_bucket."/zftest", $data);
  153. $this->_amazon->cleanBucket($this->_bucket);
  154. $this->_amazon->removeBucket($this->_bucket);
  155. $this->assertFalse($this->_amazon->isBucketAvailable($this->_bucket));
  156. $this->assertFalse($this->_amazon->isObjectAvailable($this->_bucket."/zftest"));
  157. $this->assertFalse($this->_amazon->getObjectsByBucket($this->_bucket));
  158. $list = $this->_amazon->getBuckets();
  159. $this->assertNotContains($this->_bucket, $list);
  160. }
  161. protected function _fileTest($filename, $object, $type, $exp_type)
  162. {
  163. $this->_amazon->putFile($filename, $object, array(Zend_Service_Amazon_S3::S3_CONTENT_TYPE_HEADER => $type));
  164. $data = file_get_contents($filename);
  165. $this->assertTrue($this->_amazon->isObjectAvailable($object));
  166. $info = $this->_amazon->getInfo($object);
  167. $this->assertEquals('"'.md5_file($filename).'"', $info["etag"]);
  168. $this->assertEquals(filesize($filename), $info["size"]);
  169. $this->assertEquals($exp_type, $info["type"]);
  170. $fdata = $this->_amazon->getObject($object);
  171. $this->assertEquals($data, $fdata);
  172. }
  173. public function testPutFile()
  174. {
  175. $filedir = dirname(__FILE__)."/_files/";
  176. $this->_amazon->createBucket($this->_bucket);
  177. $this->_fileTest($filedir."testdata", $this->_bucket."/zftestfile", null, 'binary/octet-stream');
  178. $this->_fileTest($filedir."testdata", $this->_bucket."/zftestfile2", 'text/plain', 'text/plain');
  179. $this->_fileTest($filedir."testdata.html", $this->_bucket."/zftestfile3", null, 'text/html');
  180. $this->_fileTest($filedir."testdata.html", $this->_bucket."/zftestfile3.html", 'text/plain', 'text/plain');
  181. }
  182. public function testPutNoFile()
  183. {
  184. $filedir = dirname(__FILE__)."/_files/";
  185. try {
  186. $this->_amazon->putFile($filedir."nosuchfile", $this->_bucket."/zftestfile");
  187. $this->fail("Expected exception not thrown");
  188. } catch(Zend_Service_Amazon_S3_Exception $e) {
  189. $this->assertContains("Cannot read", $e->getMessage());
  190. $this->assertContains("nosuchfile", $e->getMessage());
  191. }
  192. $this->assertFalse($this->_amazon->isObjectAvailable($this->_bucket."/zftestfile"));
  193. }
  194. public function testObjectEncoding()
  195. {
  196. $this->_amazon->createBucket($this->_bucket);
  197. $this->_amazon->putObject($this->_bucket."/this is a 100% test", "testdata");
  198. $this->assertEquals("testdata", $this->_amazon->getObject($this->_bucket."/this is a 100% test"));
  199. $this->_amazon->putObject($this->_bucket."/это тоже тест!", "testdata123");
  200. $this->assertEquals("testdata123", $this->_amazon->getObject($this->_bucket."/это тоже тест!"));
  201. }
  202. public function testBadNames()
  203. {
  204. try {
  205. $this->_amazon->createBucket("This is a Very Bad Name");
  206. $this->fail("Expected exception not thrown");
  207. } catch(Zend_Service_Amazon_S3_Exception $e) {
  208. $this->assertContains("contains invalid characters", $e->getMessage());
  209. }
  210. try {
  211. $this->_amazon->isBucketAvailable("This is a Very Bad Name");
  212. $this->fail("Expected exception not thrown");
  213. } catch(Zend_Uri_Exception $e) {
  214. $this->assertContains("not a valid HTTP host", $e->getMessage());
  215. }
  216. try {
  217. $this->_amazon->putObject("This is a Very Bad Name/And It Gets Worse", "testdata");
  218. $this->fail("Expected exception not thrown");
  219. } catch(Zend_Service_Amazon_S3_Exception $e) {
  220. $this->assertContains("contains invalid characters", $e->getMessage());
  221. }
  222. try {
  223. $this->_amazon->getObject("This is a Very Bad Name/And It Gets Worse");
  224. $this->fail("Expected exception not thrown");
  225. } catch(Zend_Service_Amazon_S3_Exception $e) {
  226. $this->assertContains("contains invalid characters", $e->getMessage());
  227. }
  228. try {
  229. $this->_amazon->getInfo("This is a Very Bad Name/And It Gets Worse");
  230. $this->fail("Expected exception not thrown");
  231. } catch(Zend_Service_Amazon_S3_Exception $e) {
  232. $this->assertContains("contains invalid characters", $e->getMessage());
  233. }
  234. }
  235. public function testAcl()
  236. {
  237. $this->_amazon->createBucket($this->_bucket);
  238. $filedir = dirname(__FILE__)."/_files/";
  239. $this->_amazon->putFile($filedir."testdata.html", $this->_bucket."/zftestfile.html");
  240. $this->_amazon->putFile($filedir."testdata.html", $this->_bucket."/zftestfile2.html",
  241. array(Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ));
  242. $url = 'http://' . Zend_Service_Amazon_S3::S3_ENDPOINT."/".$this->_bucket."/zftestfile.html";
  243. $data = @file_get_contents($url);
  244. $this->assertFalse($data);
  245. $url = 'http://' . Zend_Service_Amazon_S3::S3_ENDPOINT."/".$this->_bucket."/zftestfile2.html";
  246. $data = @file_get_contents($url);
  247. $this->assertEquals(file_get_contents($filedir."testdata.html"), $data);
  248. }
  249. /**
  250. * Test creating bucket with location
  251. * ZF-6728
  252. *
  253. */
  254. public function testCreateBucketEU()
  255. {
  256. $this->_amazon->createBucket($this->_bucket, 'EU');
  257. $this->assertTrue($this->_amazon->isBucketAvailable($this->_bucket));
  258. $list = $this->_amazon->getBuckets();
  259. $this->assertContains($this->_bucket, $list);
  260. }
  261. /**
  262. * Test bucket name with /'s and encoding
  263. *
  264. * ZF-6855
  265. *
  266. */
  267. public function testObjectPath()
  268. {
  269. $this->_amazon->createBucket($this->_bucket);
  270. $filedir = dirname(__FILE__)."/_files/";
  271. $this->_amazon->putFile($filedir."testdata.html", $this->_bucket."/subdir/dir with spaces/zftestfile.html",
  272. array(Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ));
  273. $url = 'http://' . Zend_Service_Amazon_S3::S3_ENDPOINT."/".$this->_bucket."/subdir/dir%20with%20spaces/zftestfile.html";
  274. $data = @file_get_contents($url);
  275. $this->assertEquals(file_get_contents($filedir."testdata.html"), $data);
  276. }
  277. /**
  278. * Test creating object with https
  279. *
  280. * ZF-7029
  281. */
  282. public function testCreateObjectSSL()
  283. {
  284. $this->_amazon->setEndpoint('https://s3.amazonaws.com');
  285. $this->assertEquals('https://s3.amazonaws.com', $this->_amazon->getEndpoint()->getUri());
  286. $this->_amazon->createBucket($this->_bucket);
  287. $this->_amazon->putObject($this->_bucket."/zftest", "testdata");
  288. $this->assertEquals("testdata", $this->_amazon->getObject($this->_bucket."/zftest"));
  289. }
  290. /**
  291. * Test creating bucket with IP
  292. *
  293. * ZF-6686
  294. */
  295. public function testBucketIPMask()
  296. {
  297. try {
  298. $this->_amazon->createBucket("127.0.0.1");
  299. $this->fail("Failed to throw expected exception");
  300. } catch(Zend_Service_Amazon_S3_Exception $e) {
  301. $this->assertContains("cannot be an IP address", $e->getMessage());
  302. }
  303. $this->_amazon->createBucket("123-456-789-123");
  304. $this->assertTrue($this->_amazon->isBucketAvailable("123-456-789-123"));
  305. $this->_amazon->removeBucket("123-456-789-123");
  306. }
  307. public function tearDown()
  308. {
  309. unset($this->_amazon->debug);
  310. $this->_amazon->cleanBucket($this->_bucket);
  311. $this->_amazon->removeBucket($this->_bucket);
  312. }
  313. }
  314. class Zend_Service_Amazon_S3_OnlineTest_Skip extends PHPUnit_Framework_TestCase
  315. {
  316. public function setUp()
  317. {
  318. $this->markTestSkipped('Zend_Service_Amazon_S3 online tests not enabled with an access key ID in '
  319. . 'TestConfiguration.php');
  320. }
  321. public function testNothing()
  322. {
  323. }
  324. }