FlashPolicyComponentTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace Ratchet\Application\Server;
  3. use Ratchet\Server\FlashPolicy;
  4. /**
  5. * @covers Ratchet\Server\FlashPolicy
  6. */
  7. class FlashPolicyTest extends \PHPUnit_Framework_TestCase {
  8. protected $_policy;
  9. public function setUp() {
  10. $this->_policy = new FlashPolicy();
  11. }
  12. public function testPolicyRender() {
  13. $this->_policy->setSiteControl('all');
  14. $this->_policy->addAllowedAccess('example.com', '*');
  15. $this->_policy->addAllowedAccess('dev.example.com', '*');
  16. $this->assertInstanceOf('SimpleXMLElement', $this->_policy->renderPolicy());
  17. }
  18. public function testInvalidPolicyReader() {
  19. $this->setExpectedException('UnexpectedValueException');
  20. $this->_policy->renderPolicy();
  21. }
  22. public function testInvalidDomainPolicyReader() {
  23. $this->setExpectedException('UnexpectedValueException');
  24. $this->_policy->setSiteControl('all');
  25. $this->_policy->addAllowedAccess('dev.example.*', '*');
  26. $this->_policy->renderPolicy();
  27. }
  28. /**
  29. * @dataProvider siteControl
  30. */
  31. public function testSiteControlValidation($accept, $permittedCrossDomainPolicies) {
  32. $this->assertEquals($accept, $this->_policy->validateSiteControl($permittedCrossDomainPolicies));
  33. }
  34. public static function siteControl() {
  35. return array(
  36. array(true, 'all')
  37. , array(true, 'none')
  38. , array(true, 'master-only')
  39. , array(false, 'by-content-type')
  40. , array(false, 'by-ftp-filename')
  41. , array(false, '')
  42. , array(false, 'all ')
  43. , array(false, 'asdf')
  44. , array(false, '@893830')
  45. , array(false, '*')
  46. );
  47. }
  48. /**
  49. * @dataProvider URI
  50. */
  51. public function testDomainValidation($accept, $domain) {
  52. $this->assertEquals($accept, $this->_policy->validateDomain($domain));
  53. }
  54. public static function URI() {
  55. return array(
  56. array(true, '*')
  57. , array(true, 'example.com')
  58. , array(true, 'exam-ple.com')
  59. , array(true, '*.example.com')
  60. , array(true, 'www.example.com')
  61. , array(true, 'dev.dev.example.com')
  62. , array(true, 'http://example.com')
  63. , array(true, 'https://example.com')
  64. , array(true, 'http://*.example.com')
  65. , array(false, 'exam*ple.com')
  66. , array(true, '127.0.255.1')
  67. , array(true, 'localhost')
  68. , array(false, 'www.example.*')
  69. , array(false, 'www.exa*le.com')
  70. , array(false, 'www.example.*com')
  71. , array(false, '*.example.*')
  72. , array(false, 'gasldf*$#a0sdf0a8sdf')
  73. );
  74. }
  75. /**
  76. * @dataProvider ports
  77. */
  78. public function testPortValidation($accept, $ports) {
  79. $this->assertEquals($accept, $this->_policy->validatePorts($ports));
  80. }
  81. public static function ports() {
  82. return array(
  83. array(true, '*')
  84. , array(true, '80')
  85. , array(true, '80,443')
  86. , array(true, '507,516-523')
  87. , array(true, '507,516-523,333')
  88. , array(true, '507,516-523,507,516-523')
  89. , array(false, '516-')
  90. , array(true, '516-523,11')
  91. , array(false, '516,-523,11')
  92. , array(false, 'example')
  93. , array(false, 'asdf,123')
  94. , array(false, '--')
  95. , array(false, ',,,')
  96. , array(false, '838*')
  97. );
  98. }
  99. public function testAddAllowedAccessOnlyAcceptsValidPorts() {
  100. $this->setExpectedException('UnexpectedValueException');
  101. $this->_policy->addAllowedAccess('*', 'nope');
  102. }
  103. public function testSetSiteControlThrowsException() {
  104. $this->setExpectedException('UnexpectedValueException');
  105. $this->_policy->setSiteControl('nope');
  106. }
  107. public function testErrorClosesConnection() {
  108. $conn = $this->getMock('\\Ratchet\\ConnectionInterface');
  109. $conn->expects($this->once())->method('close');
  110. $this->_policy->onError($conn, new \Exception);
  111. }
  112. public function testOnMessageSendsString() {
  113. $this->_policy->addAllowedAccess('*', '*');
  114. $conn = $this->getMock('\\Ratchet\\ConnectionInterface');
  115. $conn->expects($this->once())->method('send')->with($this->isType('string'));
  116. $this->_policy->onMessage($conn, ' ');
  117. }
  118. public function testOnOpenExists() {
  119. $this->assertTrue(method_exists($this->_policy, 'onOpen'));
  120. $conn = $this->getMock('\Ratchet\ConnectionInterface');
  121. $this->_policy->onOpen($conn);
  122. }
  123. public function testOnCloseExists() {
  124. $this->assertTrue(method_exists($this->_policy, 'onClose'));
  125. $conn = $this->getMock('\Ratchet\ConnectionInterface');
  126. $this->_policy->onClose($conn);
  127. }
  128. }