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