2
0

OnlineTest.php 14 KB

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