SecuritygroupsTest.php 20 KB

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