SecuritygroupsTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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
  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. require_once 'PHPUnit/Framework/TestCase.php';
  27. require_once 'Zend/Http/Client.php';
  28. require_once 'Zend/Http/Client/Adapter/Test.php';
  29. require_once 'Zend/Service/Amazon/Ec2/Securitygroups.php';
  30. /**
  31. * Zend_Service_Amazon_Ec2_Securitygroups test case.
  32. *
  33. * @category Zend
  34. * @package Zend_Service_Amazon
  35. * @subpackage UnitTests
  36. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. * @group Zend_Service
  39. * @group Zend_Service_Amazon
  40. * @group Zend_Service_Amazon_Ec2
  41. */
  42. class Zend_Service_Amazon_Ec2_SecuritygroupsTest extends PHPUnit_Framework_TestCase
  43. {
  44. /**
  45. * @var Zend_Service_Amazon_Ec2_Securitygroups
  46. */
  47. private $Zend_Service_Amazon_Ec2_Securitygroups;
  48. /**
  49. * Prepares the environment before running a test.
  50. */
  51. protected function setUp()
  52. {
  53. parent::setUp();
  54. $this->Zend_Service_Amazon_Ec2_Securitygroups = new Zend_Service_Amazon_Ec2_Securitygroups('access_key', 'secret_access_key');
  55. $adapter = new Zend_Http_Client_Adapter_Test();
  56. $client = new Zend_Http_Client(null, array(
  57. 'adapter' => $adapter
  58. ));
  59. $this->adapter = $adapter;
  60. Zend_Service_Amazon_Ec2_Securitygroups::setHttpClient($client);
  61. }
  62. /**
  63. * Cleans up the environment after running a test.
  64. */
  65. protected function tearDown()
  66. {
  67. unset($this->adapter);
  68. $this->Zend_Service_Amazon_Ec2_Securitygroups = null;
  69. parent::tearDown();
  70. }
  71. /**
  72. * Tests Zend_Service_Amazon_Ec2_Securitygroups->authorize()
  73. */
  74. public function testAuthorizeSinglePort()
  75. {
  76. $rawHttpResponse = "HTTP/1.1 200 OK\r\n"
  77. . "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  78. . "Server: hi\r\n"
  79. . "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  80. . "Status: 200 OK\r\n"
  81. . "Content-type: application/xml; charset=utf-8\r\n"
  82. . "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
  83. . "Connection: close\r\n"
  84. . "\r\n"
  85. . "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
  86. . "<AuthorizeSecurityGroupIngressResponse xmlns=\"http://ec2.amazonaws.com/doc/2009-04-04/\">\r\n"
  87. . " <return>true</return>\r\n"
  88. . "</AuthorizeSecurityGroupIngressResponse>\r\n";
  89. $this->adapter->setResponse($rawHttpResponse);
  90. $return = $this->Zend_Service_Amazon_Ec2_Securitygroups->authorizeIp('MyGroup', 'tcp', '80', '80', '0.0.0.0/0');
  91. $this->assertTrue($return);
  92. }
  93. public function testAuthorizeRangeOfPorts()
  94. {
  95. $rawHttpResponse = "HTTP/1.1 200 OK\r\n"
  96. . "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  97. . "Server: hi\r\n"
  98. . "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  99. . "Status: 200 OK\r\n"
  100. . "Content-type: application/xml; charset=utf-8\r\n"
  101. . "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
  102. . "Connection: close\r\n"
  103. . "\r\n"
  104. . "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
  105. . "<AuthorizeSecurityGroupIngressResponse xmlns=\"http://ec2.amazonaws.com/doc/2009-04-04/\">\r\n"
  106. . " <return>true</return>\r\n"
  107. . "</AuthorizeSecurityGroupIngressResponse>\r\n";
  108. $this->adapter->setResponse($rawHttpResponse);
  109. $return = $this->Zend_Service_Amazon_Ec2_Securitygroups->authorizeIp('MyGroup', 'tcp', '6000', '7000', '0.0.0.0/0');
  110. $this->assertTrue($return);
  111. }
  112. public function testAuthorizeSecurityGroupName()
  113. {
  114. $rawHttpResponse = "HTTP/1.1 200 OK\r\n"
  115. . "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  116. . "Server: hi\r\n"
  117. . "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  118. . "Status: 200 OK\r\n"
  119. . "Content-type: application/xml; charset=utf-8\r\n"
  120. . "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
  121. . "Connection: close\r\n"
  122. . "\r\n"
  123. . "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
  124. . "<AuthorizeSecurityGroupIngressResponse xmlns=\"http://ec2.amazonaws.com/doc/2009-04-04/\">\r\n"
  125. . " <return>true</return>\r\n"
  126. . "</AuthorizeSecurityGroupIngressResponse>\r\n";
  127. $this->adapter->setResponse($rawHttpResponse);
  128. $return = $this->Zend_Service_Amazon_Ec2_Securitygroups->authorizeGroup('MyGroup', 'groupname', '15333848');
  129. $this->assertTrue($return);
  130. }
  131. /**
  132. * Tests Zend_Service_Amazon_Ec2_Securitygroups->create()
  133. */
  134. public function testCreate()
  135. {
  136. $rawHttpResponse = "HTTP/1.1 200 OK\r\n"
  137. . "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  138. . "Server: hi\r\n"
  139. . "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  140. . "Status: 200 OK\r\n"
  141. . "Content-type: application/xml; charset=utf-8\r\n"
  142. . "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
  143. . "Connection: close\r\n"
  144. . "\r\n"
  145. . "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
  146. . "<CreateSecurityGroupResponse xmlns=\"http://ec2.amazonaws.com/doc/2009-04-04/\">\r\n"
  147. . " <return>true</return>\r\n"
  148. . "</CreateSecurityGroupResponse>\r\n";
  149. $this->adapter->setResponse($rawHttpResponse);
  150. $return = $this->Zend_Service_Amazon_Ec2_Securitygroups->create('MyGroup', 'My Security Grup');
  151. $this->assertTrue($return);
  152. }
  153. /**
  154. * Tests Zend_Service_Amazon_Ec2_Securitygroups->delete()
  155. */
  156. public function testDelete()
  157. {
  158. $rawHttpResponse = "HTTP/1.1 200 OK\r\n"
  159. . "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  160. . "Server: hi\r\n"
  161. . "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  162. . "Status: 200 OK\r\n"
  163. . "Content-type: application/xml; charset=utf-8\r\n"
  164. . "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
  165. . "Connection: close\r\n"
  166. . "\r\n"
  167. . "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
  168. . "<DeleteSecurityGroupResponse xmlns=\"http://ec2.amazonaws.com/doc/2009-04-04/\">\r\n"
  169. . " <return>true</return>\r\n"
  170. . "</DeleteSecurityGroupResponse>\r\n";
  171. $this->adapter->setResponse($rawHttpResponse);
  172. $return = $this->Zend_Service_Amazon_Ec2_Securitygroups->delete('MyGroup');
  173. $this->assertTrue($return);
  174. }
  175. /**
  176. * Tests Zend_Service_Amazon_Ec2_Securitygroups->describe()
  177. */
  178. public function testDescribeMultipleSecruityGroups()
  179. {
  180. $rawHttpResponse = "HTTP/1.1 200 OK\r\n"
  181. . "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  182. . "Server: hi\r\n"
  183. . "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  184. . "Status: 200 OK\r\n"
  185. . "Content-type: application/xml; charset=utf-8\r\n"
  186. . "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
  187. . "Connection: close\r\n"
  188. . "\r\n"
  189. . "<DescribeSecurityGroupsResponse xmlns=\"http://ec2.amazonaws.com/doc/2009-04-04/\">\r\n"
  190. . " <securityGroupInfo>\r\n"
  191. . " <item>\r\n"
  192. . " <ownerId>UYY3TLBUXIEON5NQVUUX6OMPWBZIQNFM</ownerId>\r\n"
  193. . " <groupName>WebServers</groupName>\r\n"
  194. . " <groupDescription>Web</groupDescription>\r\n"
  195. . " <ipPermissions>\r\n"
  196. . " <item>\r\n"
  197. . " <ipProtocol>tcp</ipProtocol>\r\n"
  198. . " <fromPort>80</fromPort>\r\n"
  199. . " <toPort>80</toPort>\r\n"
  200. . " <groups/>\r\n"
  201. . " <ipRanges>\r\n"
  202. . " <item>\r\n"
  203. . " <cidrIp>0.0.0.0/0</cidrIp>\r\n"
  204. . " </item>\r\n"
  205. . " </ipRanges>\r\n"
  206. . " </item>\r\n"
  207. . " </ipPermissions>\r\n"
  208. . " </item>\r\n"
  209. . " <item>\r\n"
  210. . " <ownerId>UYY3TLBUXIEON5NQVUUX6OMPWBZIQNFM</ownerId>\r\n"
  211. . " <groupName>RangedPortsBySource</groupName>\r\n"
  212. . " <groupDescription>A</groupDescription>\r\n"
  213. . " <ipPermissions>\r\n"
  214. . " <item>\r\n"
  215. . " <ipProtocol>tcp</ipProtocol>\r\n"
  216. . " <fromPort>6000</fromPort>\r\n"
  217. . " <toPort>7000</toPort>\r\n"
  218. . " <groups/>\r\n"
  219. . " <ipRanges>\r\n"
  220. . " <item>\r\n"
  221. . " <cidrIp>0.0.0.0/0</cidrIp>\r\n"
  222. . " </item>\r\n"
  223. . " </ipRanges>\r\n"
  224. . " </item>\r\n"
  225. . " </ipPermissions>\r\n"
  226. . " </item>\r\n"
  227. . " </securityGroupInfo>\r\n"
  228. . "</DescribeSecurityGroupsResponse>\r\n";
  229. $this->adapter->setResponse($rawHttpResponse);
  230. $return = $this->Zend_Service_Amazon_Ec2_Securitygroups->describe(array('WebServers','RangedPortsBySource'));
  231. $this->assertEquals(2, count($return));
  232. $arrGroups = array(
  233. array(
  234. 'ownerId' => 'UYY3TLBUXIEON5NQVUUX6OMPWBZIQNFM',
  235. 'groupName' => 'WebServers',
  236. 'groupDescription' => 'Web',
  237. 'ipPermissions' => array(0 => array(
  238. 'ipProtocol' => 'tcp',
  239. 'fromPort' => '80',
  240. 'toPort' => '80',
  241. 'ipRanges' => '0.0.0.0/0'
  242. ))
  243. ),
  244. array(
  245. 'ownerId' => 'UYY3TLBUXIEON5NQVUUX6OMPWBZIQNFM',
  246. 'groupName' => 'RangedPortsBySource',
  247. 'groupDescription' => 'A',
  248. 'ipPermissions' => array(0 => array(
  249. 'ipProtocol' => 'tcp',
  250. 'fromPort' => '6000',
  251. 'toPort' => '7000',
  252. 'ipRanges' => '0.0.0.0/0'
  253. ))
  254. )
  255. );
  256. foreach($return as $k => $r) {
  257. $this->assertSame($arrGroups[$k], $r);
  258. }
  259. }
  260. public function testDescribeSingleSecruityGroup()
  261. {
  262. $rawHttpResponse = "HTTP/1.1 200 OK\r\n"
  263. . "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  264. . "Server: hi\r\n"
  265. . "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  266. . "Status: 200 OK\r\n"
  267. . "Content-type: application/xml; charset=utf-8\r\n"
  268. . "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
  269. . "Connection: close\r\n"
  270. . "\r\n"
  271. . "<DescribeSecurityGroupsResponse xmlns=\"http://ec2.amazonaws.com/doc/2009-04-04/\">\r\n"
  272. . " <securityGroupInfo>\r\n"
  273. . " <item>\r\n"
  274. . " <ownerId>UYY3TLBUXIEON5NQVUUX6OMPWBZIQNFM</ownerId>\r\n"
  275. . " <groupName>WebServers</groupName>\r\n"
  276. . " <groupDescription>Web</groupDescription>\r\n"
  277. . " <ipPermissions>\r\n"
  278. . " <item>\r\n"
  279. . " <ipProtocol>tcp</ipProtocol>\r\n"
  280. . " <fromPort>80</fromPort>\r\n"
  281. . " <toPort>80</toPort>\r\n"
  282. . " <groups/>\r\n"
  283. . " <ipRanges>\r\n"
  284. . " <item>\r\n"
  285. . " <cidrIp>0.0.0.0/0</cidrIp>\r\n"
  286. . " </item>\r\n"
  287. . " </ipRanges>\r\n"
  288. . " </item>\r\n"
  289. . " </ipPermissions>\r\n"
  290. . " </item>\r\n"
  291. . " </securityGroupInfo>\r\n"
  292. . "</DescribeSecurityGroupsResponse>\r\n";
  293. $this->adapter->setResponse($rawHttpResponse);
  294. $return = $this->Zend_Service_Amazon_Ec2_Securitygroups->describe('WebServers');
  295. $this->assertEquals(1, count($return));
  296. $arrGroups = array(
  297. array(
  298. 'ownerId' => 'UYY3TLBUXIEON5NQVUUX6OMPWBZIQNFM',
  299. 'groupName' => 'WebServers',
  300. 'groupDescription' => 'Web',
  301. 'ipPermissions' => array(0 => array(
  302. 'ipProtocol' => 'tcp',
  303. 'fromPort' => '80',
  304. 'toPort' => '80',
  305. 'ipRanges' => '0.0.0.0/0'
  306. ))
  307. )
  308. );
  309. foreach($return as $k => $r) {
  310. $this->assertSame($arrGroups[$k], $r);
  311. }
  312. }
  313. public function testDescribeSingleSecruityGroupWithMultipleIpsSamePort()
  314. {
  315. $rawHttpResponse = "HTTP/1.1 200 OK\r\n"
  316. . "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  317. . "Server: hi\r\n"
  318. . "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  319. . "Status: 200 OK\r\n"
  320. . "Content-type: application/xml; charset=utf-8\r\n"
  321. . "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
  322. . "Connection: close\r\n"
  323. . "\r\n"
  324. . "<DescribeSecurityGroupsResponse xmlns=\"http://ec2.amazonaws.com/doc/2009-04-04/\">\r\n"
  325. . " <securityGroupInfo>\r\n"
  326. . " <item>\r\n"
  327. . " <ownerId>UYY3TLBUXIEON5NQVUUX6OMPWBZIQNFM</ownerId>\r\n"
  328. . " <groupName>WebServers</groupName>\r\n"
  329. . " <groupDescription>Web</groupDescription>\r\n"
  330. . " <ipPermissions>\r\n"
  331. . " <item>\r\n"
  332. . " <ipProtocol>tcp</ipProtocol>\r\n"
  333. . " <fromPort>80</fromPort>\r\n"
  334. . " <toPort>80</toPort>\r\n"
  335. . " <groups/>\r\n"
  336. . " <ipRanges>\r\n"
  337. . " <item>\r\n"
  338. . " <cidrIp>0.0.0.0/0</cidrIp>\r\n"
  339. . " </item>\r\n"
  340. . " <item>\r\n"
  341. . " <cidrIp>1.1.1.1/0</cidrIp>\r\n"
  342. . " </item>\r\n"
  343. . " </ipRanges>\r\n"
  344. . " </item>\r\n"
  345. . " </ipPermissions>\r\n"
  346. . " </item>\r\n"
  347. . " </securityGroupInfo>\r\n"
  348. . "</DescribeSecurityGroupsResponse>\r\n";
  349. $this->adapter->setResponse($rawHttpResponse);
  350. $return = $this->Zend_Service_Amazon_Ec2_Securitygroups->describe('WebServers');
  351. $this->assertEquals(1, count($return));
  352. $arrGroups = array(
  353. array(
  354. 'ownerId' => 'UYY3TLBUXIEON5NQVUUX6OMPWBZIQNFM',
  355. 'groupName' => 'WebServers',
  356. 'groupDescription' => 'Web',
  357. 'ipPermissions' => array(0 => array(
  358. 'ipProtocol' => 'tcp',
  359. 'fromPort' => '80',
  360. 'toPort' => '80',
  361. 'ipRanges' => array(
  362. '0.0.0.0/0',
  363. '1.1.1.1/0'
  364. )
  365. ))
  366. )
  367. );
  368. foreach($return as $k => $r) {
  369. $this->assertSame($arrGroups[$k], $r);
  370. }
  371. }
  372. /**
  373. * Tests Zend_Service_Amazon_Ec2_Securitygroups->revoke()
  374. */
  375. public function testRevokeSinglePort()
  376. {
  377. $rawHttpResponse = "HTTP/1.1 200 OK\r\n"
  378. . "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  379. . "Server: hi\r\n"
  380. . "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  381. . "Status: 200 OK\r\n"
  382. . "Content-type: application/xml; charset=utf-8\r\n"
  383. . "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
  384. . "Connection: close\r\n"
  385. . "\r\n"
  386. . "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
  387. . "<RevokeSecurityGroupIngressResponse xmlns=\"http://ec2.amazonaws.com/doc/2009-04-04/\">\r\n"
  388. . " <return>true</return>\r\n"
  389. . "</RevokeSecurityGroupIngressResponse>\r\n";
  390. $this->adapter->setResponse($rawHttpResponse);
  391. $return = $this->Zend_Service_Amazon_Ec2_Securitygroups->revokeIp('MyGroup', 'tcp', '80', '80', '0.0.0.0/0');
  392. $this->assertTrue($return);
  393. }
  394. public function testRevokePortRange()
  395. {
  396. $rawHttpResponse = "HTTP/1.1 200 OK\r\n"
  397. . "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  398. . "Server: hi\r\n"
  399. . "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  400. . "Status: 200 OK\r\n"
  401. . "Content-type: application/xml; charset=utf-8\r\n"
  402. . "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
  403. . "Connection: close\r\n"
  404. . "\r\n"
  405. . "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
  406. . "<RevokeSecurityGroupIngressResponse xmlns=\"http://ec2.amazonaws.com/doc/2009-04-04/\">\r\n"
  407. . " <return>true</return>\r\n"
  408. . "</RevokeSecurityGroupIngressResponse>\r\n";
  409. $this->adapter->setResponse($rawHttpResponse);
  410. $return = $this->Zend_Service_Amazon_Ec2_Securitygroups->revokeIp('MyGroup', 'tcp', '6000', '7000', '0.0.0.0/0');
  411. $this->assertTrue($return);
  412. }
  413. public function testRevokeSecurityGroupName()
  414. {
  415. $rawHttpResponse = "HTTP/1.1 200 OK\r\n"
  416. . "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  417. . "Server: hi\r\n"
  418. . "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  419. . "Status: 200 OK\r\n"
  420. . "Content-type: application/xml; charset=utf-8\r\n"
  421. . "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
  422. . "Connection: close\r\n"
  423. . "\r\n"
  424. . "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
  425. . "<RevokeSecurityGroupIngressResponse xmlns=\"http://ec2.amazonaws.com/doc/2009-04-04/\">\r\n"
  426. . " <return>true</return>\r\n"
  427. . "</RevokeSecurityGroupIngressResponse>\r\n";
  428. $this->adapter->setResponse($rawHttpResponse);
  429. $return = $this->Zend_Service_Amazon_Ec2_Securitygroups->revokeGroup('MyGroup', 'groupname', '15333848');
  430. $this->assertTrue($return);
  431. }
  432. }