SecuritygroupsTest.php 20 KB

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