SecuritygroupsTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. <?php
  2. require_once 'PHPUnit/Framework/TestCase.php';
  3. require_once 'Zend/Http/Client.php';
  4. require_once 'Zend/Http/Client/Adapter/Test.php';
  5. require_once 'Zend/Service/Amazon/Ec2/Securitygroups.php';
  6. /**
  7. * Zend_Service_Amazon_Ec2_Securitygroups test case.
  8. */
  9. class Zend_Service_Amazon_Ec2_SecuritygroupsTest extends PHPUnit_Framework_TestCase
  10. {
  11. /**
  12. * @var Zend_Service_Amazon_Ec2_Securitygroups
  13. */
  14. private $Zend_Service_Amazon_Ec2_Securitygroups;
  15. /**
  16. * Prepares the environment before running a test.
  17. */
  18. protected function setUp()
  19. {
  20. parent::setUp();
  21. $this->Zend_Service_Amazon_Ec2_Securitygroups = new Zend_Service_Amazon_Ec2_Securitygroups('access_key', 'secret_access_key');
  22. $adapter = new Zend_Http_Client_Adapter_Test();
  23. $client = new Zend_Http_Client(null, array(
  24. 'adapter' => $adapter
  25. ));
  26. $this->adapter = $adapter;
  27. Zend_Service_Amazon_Ec2_Securitygroups::setHttpClient($client);
  28. }
  29. /**
  30. * Cleans up the environment after running a test.
  31. */
  32. protected function tearDown()
  33. {
  34. unset($this->adapter);
  35. $this->Zend_Service_Amazon_Ec2_Securitygroups = null;
  36. parent::tearDown();
  37. }
  38. /**
  39. * Tests Zend_Service_Amazon_Ec2_Securitygroups->authorize()
  40. */
  41. public function testAuthorizeSinglePort()
  42. {
  43. $rawHttpResponse = "HTTP/1.1 200 OK\r\n"
  44. . "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  45. . "Server: hi\r\n"
  46. . "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  47. . "Status: 200 OK\r\n"
  48. . "Content-type: application/xml; charset=utf-8\r\n"
  49. . "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
  50. . "Connection: close\r\n"
  51. . "\r\n"
  52. . "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
  53. . "<AuthorizeSecurityGroupIngressResponse xmlns=\"http://ec2.amazonaws.com/doc/2009-04-04/\">\r\n"
  54. . " <return>true</return>\r\n"
  55. . "</AuthorizeSecurityGroupIngressResponse>\r\n";
  56. $this->adapter->setResponse($rawHttpResponse);
  57. $return = $this->Zend_Service_Amazon_Ec2_Securitygroups->authorizeIp('MyGroup', 'tcp', '80', '80', '0.0.0.0/0');
  58. $this->assertTrue($return);
  59. }
  60. public function testAuthorizeRangeOfPorts()
  61. {
  62. $rawHttpResponse = "HTTP/1.1 200 OK\r\n"
  63. . "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  64. . "Server: hi\r\n"
  65. . "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  66. . "Status: 200 OK\r\n"
  67. . "Content-type: application/xml; charset=utf-8\r\n"
  68. . "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
  69. . "Connection: close\r\n"
  70. . "\r\n"
  71. . "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
  72. . "<AuthorizeSecurityGroupIngressResponse xmlns=\"http://ec2.amazonaws.com/doc/2009-04-04/\">\r\n"
  73. . " <return>true</return>\r\n"
  74. . "</AuthorizeSecurityGroupIngressResponse>\r\n";
  75. $this->adapter->setResponse($rawHttpResponse);
  76. $return = $this->Zend_Service_Amazon_Ec2_Securitygroups->authorizeIp('MyGroup', 'tcp', '6000', '7000', '0.0.0.0/0');
  77. $this->assertTrue($return);
  78. }
  79. public function testAuthorizeSecurityGroupName()
  80. {
  81. $rawHttpResponse = "HTTP/1.1 200 OK\r\n"
  82. . "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  83. . "Server: hi\r\n"
  84. . "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  85. . "Status: 200 OK\r\n"
  86. . "Content-type: application/xml; charset=utf-8\r\n"
  87. . "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
  88. . "Connection: close\r\n"
  89. . "\r\n"
  90. . "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
  91. . "<AuthorizeSecurityGroupIngressResponse xmlns=\"http://ec2.amazonaws.com/doc/2009-04-04/\">\r\n"
  92. . " <return>true</return>\r\n"
  93. . "</AuthorizeSecurityGroupIngressResponse>\r\n";
  94. $this->adapter->setResponse($rawHttpResponse);
  95. $return = $this->Zend_Service_Amazon_Ec2_Securitygroups->authorizeGroup('MyGroup', 'groupname', '15333848');
  96. $this->assertTrue($return);
  97. }
  98. /**
  99. * Tests Zend_Service_Amazon_Ec2_Securitygroups->create()
  100. */
  101. public function testCreate()
  102. {
  103. $rawHttpResponse = "HTTP/1.1 200 OK\r\n"
  104. . "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  105. . "Server: hi\r\n"
  106. . "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  107. . "Status: 200 OK\r\n"
  108. . "Content-type: application/xml; charset=utf-8\r\n"
  109. . "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
  110. . "Connection: close\r\n"
  111. . "\r\n"
  112. . "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
  113. . "<CreateSecurityGroupResponse xmlns=\"http://ec2.amazonaws.com/doc/2009-04-04/\">\r\n"
  114. . " <return>true</return>\r\n"
  115. . "</CreateSecurityGroupResponse>\r\n";
  116. $this->adapter->setResponse($rawHttpResponse);
  117. $return = $this->Zend_Service_Amazon_Ec2_Securitygroups->create('MyGroup', 'My Security Grup');
  118. $this->assertTrue($return);
  119. }
  120. /**
  121. * Tests Zend_Service_Amazon_Ec2_Securitygroups->delete()
  122. */
  123. public function testDelete()
  124. {
  125. $rawHttpResponse = "HTTP/1.1 200 OK\r\n"
  126. . "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  127. . "Server: hi\r\n"
  128. . "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  129. . "Status: 200 OK\r\n"
  130. . "Content-type: application/xml; charset=utf-8\r\n"
  131. . "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
  132. . "Connection: close\r\n"
  133. . "\r\n"
  134. . "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
  135. . "<DeleteSecurityGroupResponse xmlns=\"http://ec2.amazonaws.com/doc/2009-04-04/\">\r\n"
  136. . " <return>true</return>\r\n"
  137. . "</DeleteSecurityGroupResponse>\r\n";
  138. $this->adapter->setResponse($rawHttpResponse);
  139. $return = $this->Zend_Service_Amazon_Ec2_Securitygroups->delete('MyGroup');
  140. $this->assertTrue($return);
  141. }
  142. /**
  143. * Tests Zend_Service_Amazon_Ec2_Securitygroups->describe()
  144. */
  145. public function testDescribeMultipleSecruityGroups()
  146. {
  147. $rawHttpResponse = "HTTP/1.1 200 OK\r\n"
  148. . "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  149. . "Server: hi\r\n"
  150. . "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  151. . "Status: 200 OK\r\n"
  152. . "Content-type: application/xml; charset=utf-8\r\n"
  153. . "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
  154. . "Connection: close\r\n"
  155. . "\r\n"
  156. . "<DescribeSecurityGroupsResponse xmlns=\"http://ec2.amazonaws.com/doc/2009-04-04/\">\r\n"
  157. . " <securityGroupInfo>\r\n"
  158. . " <item>\r\n"
  159. . " <ownerId>UYY3TLBUXIEON5NQVUUX6OMPWBZIQNFM</ownerId>\r\n"
  160. . " <groupName>WebServers</groupName>\r\n"
  161. . " <groupDescription>Web</groupDescription>\r\n"
  162. . " <ipPermissions>\r\n"
  163. . " <item>\r\n"
  164. . " <ipProtocol>tcp</ipProtocol>\r\n"
  165. . " <fromPort>80</fromPort>\r\n"
  166. . " <toPort>80</toPort>\r\n"
  167. . " <groups/>\r\n"
  168. . " <ipRanges>\r\n"
  169. . " <item>\r\n"
  170. . " <cidrIp>0.0.0.0/0</cidrIp>\r\n"
  171. . " </item>\r\n"
  172. . " </ipRanges>\r\n"
  173. . " </item>\r\n"
  174. . " </ipPermissions>\r\n"
  175. . " </item>\r\n"
  176. . " <item>\r\n"
  177. . " <ownerId>UYY3TLBUXIEON5NQVUUX6OMPWBZIQNFM</ownerId>\r\n"
  178. . " <groupName>RangedPortsBySource</groupName>\r\n"
  179. . " <groupDescription>A</groupDescription>\r\n"
  180. . " <ipPermissions>\r\n"
  181. . " <item>\r\n"
  182. . " <ipProtocol>tcp</ipProtocol>\r\n"
  183. . " <fromPort>6000</fromPort>\r\n"
  184. . " <toPort>7000</toPort>\r\n"
  185. . " <groups/>\r\n"
  186. . " <ipRanges>\r\n"
  187. . " <item>\r\n"
  188. . " <cidrIp>0.0.0.0/0</cidrIp>\r\n"
  189. . " </item>\r\n"
  190. . " </ipRanges>\r\n"
  191. . " </item>\r\n"
  192. . " </ipPermissions>\r\n"
  193. . " </item>\r\n"
  194. . " </securityGroupInfo>\r\n"
  195. . "</DescribeSecurityGroupsResponse>\r\n";
  196. $this->adapter->setResponse($rawHttpResponse);
  197. $return = $this->Zend_Service_Amazon_Ec2_Securitygroups->describe(array('WebServers','RangedPortsBySource'));
  198. $this->assertEquals(2, count($return));
  199. $arrGroups = array(
  200. array(
  201. 'ownerId' => 'UYY3TLBUXIEON5NQVUUX6OMPWBZIQNFM',
  202. 'groupName' => 'WebServers',
  203. 'groupDescription' => 'Web',
  204. 'ipPermissions' => array(0 => array(
  205. 'ipProtocol' => 'tcp',
  206. 'fromPort' => '80',
  207. 'toPort' => '80',
  208. 'ipRanges' => '0.0.0.0/0'
  209. ))
  210. ),
  211. array(
  212. 'ownerId' => 'UYY3TLBUXIEON5NQVUUX6OMPWBZIQNFM',
  213. 'groupName' => 'RangedPortsBySource',
  214. 'groupDescription' => 'A',
  215. 'ipPermissions' => array(0 => array(
  216. 'ipProtocol' => 'tcp',
  217. 'fromPort' => '6000',
  218. 'toPort' => '7000',
  219. 'ipRanges' => '0.0.0.0/0'
  220. ))
  221. )
  222. );
  223. foreach($return as $k => $r) {
  224. $this->assertSame($arrGroups[$k], $r);
  225. }
  226. }
  227. public function testDescribeSingleSecruityGroup()
  228. {
  229. $rawHttpResponse = "HTTP/1.1 200 OK\r\n"
  230. . "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  231. . "Server: hi\r\n"
  232. . "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  233. . "Status: 200 OK\r\n"
  234. . "Content-type: application/xml; charset=utf-8\r\n"
  235. . "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
  236. . "Connection: close\r\n"
  237. . "\r\n"
  238. . "<DescribeSecurityGroupsResponse xmlns=\"http://ec2.amazonaws.com/doc/2009-04-04/\">\r\n"
  239. . " <securityGroupInfo>\r\n"
  240. . " <item>\r\n"
  241. . " <ownerId>UYY3TLBUXIEON5NQVUUX6OMPWBZIQNFM</ownerId>\r\n"
  242. . " <groupName>WebServers</groupName>\r\n"
  243. . " <groupDescription>Web</groupDescription>\r\n"
  244. . " <ipPermissions>\r\n"
  245. . " <item>\r\n"
  246. . " <ipProtocol>tcp</ipProtocol>\r\n"
  247. . " <fromPort>80</fromPort>\r\n"
  248. . " <toPort>80</toPort>\r\n"
  249. . " <groups/>\r\n"
  250. . " <ipRanges>\r\n"
  251. . " <item>\r\n"
  252. . " <cidrIp>0.0.0.0/0</cidrIp>\r\n"
  253. . " </item>\r\n"
  254. . " </ipRanges>\r\n"
  255. . " </item>\r\n"
  256. . " </ipPermissions>\r\n"
  257. . " </item>\r\n"
  258. . " </securityGroupInfo>\r\n"
  259. . "</DescribeSecurityGroupsResponse>\r\n";
  260. $this->adapter->setResponse($rawHttpResponse);
  261. $return = $this->Zend_Service_Amazon_Ec2_Securitygroups->describe('WebServers');
  262. $this->assertEquals(1, count($return));
  263. $arrGroups = array(
  264. array(
  265. 'ownerId' => 'UYY3TLBUXIEON5NQVUUX6OMPWBZIQNFM',
  266. 'groupName' => 'WebServers',
  267. 'groupDescription' => 'Web',
  268. 'ipPermissions' => array(0 => array(
  269. 'ipProtocol' => 'tcp',
  270. 'fromPort' => '80',
  271. 'toPort' => '80',
  272. 'ipRanges' => '0.0.0.0/0'
  273. ))
  274. )
  275. );
  276. foreach($return as $k => $r) {
  277. $this->assertSame($arrGroups[$k], $r);
  278. }
  279. }
  280. /**
  281. * Tests Zend_Service_Amazon_Ec2_Securitygroups->revoke()
  282. */
  283. public function testRevokeSinglePort()
  284. {
  285. $rawHttpResponse = "HTTP/1.1 200 OK\r\n"
  286. . "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  287. . "Server: hi\r\n"
  288. . "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  289. . "Status: 200 OK\r\n"
  290. . "Content-type: application/xml; charset=utf-8\r\n"
  291. . "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
  292. . "Connection: close\r\n"
  293. . "\r\n"
  294. . "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
  295. . "<RevokeSecurityGroupIngressResponse xmlns=\"http://ec2.amazonaws.com/doc/2009-04-04/\">\r\n"
  296. . " <return>true</return>\r\n"
  297. . "</RevokeSecurityGroupIngressResponse>\r\n";
  298. $this->adapter->setResponse($rawHttpResponse);
  299. $return = $this->Zend_Service_Amazon_Ec2_Securitygroups->revokeIp('MyGroup', 'tcp', '80', '80', '0.0.0.0/0');
  300. $this->assertTrue($return);
  301. }
  302. public function testRevokePortRange()
  303. {
  304. $rawHttpResponse = "HTTP/1.1 200 OK\r\n"
  305. . "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  306. . "Server: hi\r\n"
  307. . "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  308. . "Status: 200 OK\r\n"
  309. . "Content-type: application/xml; charset=utf-8\r\n"
  310. . "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
  311. . "Connection: close\r\n"
  312. . "\r\n"
  313. . "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
  314. . "<RevokeSecurityGroupIngressResponse xmlns=\"http://ec2.amazonaws.com/doc/2009-04-04/\">\r\n"
  315. . " <return>true</return>\r\n"
  316. . "</RevokeSecurityGroupIngressResponse>\r\n";
  317. $this->adapter->setResponse($rawHttpResponse);
  318. $return = $this->Zend_Service_Amazon_Ec2_Securitygroups->revokeIp('MyGroup', 'tcp', '6000', '7000', '0.0.0.0/0');
  319. $this->assertTrue($return);
  320. }
  321. public function testRevokeSecurityGroupName()
  322. {
  323. $rawHttpResponse = "HTTP/1.1 200 OK\r\n"
  324. . "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  325. . "Server: hi\r\n"
  326. . "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  327. . "Status: 200 OK\r\n"
  328. . "Content-type: application/xml; charset=utf-8\r\n"
  329. . "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
  330. . "Connection: close\r\n"
  331. . "\r\n"
  332. . "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
  333. . "<RevokeSecurityGroupIngressResponse xmlns=\"http://ec2.amazonaws.com/doc/2009-04-04/\">\r\n"
  334. . " <return>true</return>\r\n"
  335. . "</RevokeSecurityGroupIngressResponse>\r\n";
  336. $this->adapter->setResponse($rawHttpResponse);
  337. $return = $this->Zend_Service_Amazon_Ec2_Securitygroups->revokeGroup('MyGroup', 'groupname', '15333848');
  338. $this->assertTrue($return);
  339. }
  340. }