Socket.php 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\Socket\Test\Unit;
  36. use Hoa\Socket as SUT;
  37. use Hoa\Test;
  38. /**
  39. * Class \Hoa\Socket\Test\Unit\Socket.
  40. *
  41. * Test suite for the socket object.
  42. *
  43. * @copyright Copyright © 2007-2017 Hoa community
  44. * @license New BSD License
  45. */
  46. class Socket extends Test\Unit\Suite
  47. {
  48. public function case_full_domain_name()
  49. {
  50. return $this->_case_check(
  51. 'tcp://hoa-project.net:80',
  52. [
  53. 'type' => SUT::ADDRESS_DOMAIN,
  54. 'transport' => 'tcp',
  55. 'address' => 'hoa-project.net',
  56. 'port' => 80
  57. ]
  58. );
  59. }
  60. public function case_domain_name_without_port()
  61. {
  62. return $this->_case_check(
  63. 'tcp://hoa-project.net',
  64. [
  65. 'type' => SUT::ADDRESS_DOMAIN,
  66. 'transport' => 'tcp',
  67. 'address' => 'hoa-project.net',
  68. 'port' => -1
  69. ]
  70. );
  71. }
  72. public function case_full_ipv4()
  73. {
  74. return $this->_case_check(
  75. 'tcp://12.345.67.789:80',
  76. [
  77. 'type' => SUT::ADDRESS_IPV4,
  78. 'transport' => 'tcp',
  79. 'address' => '12.345.67.789',
  80. 'port' => 80
  81. ]
  82. );
  83. }
  84. public function case_ipv4_without_port()
  85. {
  86. return $this->_case_check(
  87. 'tcp://12.345.67.789',
  88. [
  89. 'type' => SUT::ADDRESS_IPV4,
  90. 'transport' => 'tcp',
  91. 'address' => '12.345.67.789',
  92. 'port' => -1
  93. ]
  94. );
  95. }
  96. public function case_ipv4_with_wildcard()
  97. {
  98. return $this->_case_check(
  99. 'tcp://*:80',
  100. [
  101. 'type' => SUT::ADDRESS_IPV4,
  102. 'transport' => 'tcp',
  103. 'address' => '0.0.0.0',
  104. 'port' => 80
  105. ]
  106. );
  107. }
  108. public function case_ipv4_with_wildcard_without_port()
  109. {
  110. return $this->_case_check(
  111. 'tcp://*',
  112. [
  113. 'type' => SUT::ADDRESS_IPV4,
  114. 'transport' => 'tcp',
  115. 'address' => '0.0.0.0',
  116. 'port' => -1
  117. ]
  118. );
  119. }
  120. public function case_full_ipv6()
  121. {
  122. return $this->_case_check(
  123. 'tcp://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:80',
  124. [
  125. 'type' => SUT::ADDRESS_IPV6,
  126. 'transport' => 'tcp',
  127. 'address' => '2001:0db8:85a3:0000:0000:8a2e:0370:7334',
  128. 'port' => 80
  129. ]
  130. );
  131. }
  132. public function case_ipv6_without_port()
  133. {
  134. return $this->_case_check(
  135. 'tcp://2001:0db8:85a3:0000:0000:8a2e:0370:7334',
  136. [
  137. 'type' => SUT::ADDRESS_IPV6,
  138. 'transport' => 'tcp',
  139. 'address' => '2001:0db8:85a3:0000:0000:8a2e:0370:7334',
  140. 'port' => -1
  141. ]
  142. );
  143. }
  144. public function case_short_ipv6()
  145. {
  146. return $this->_case_check(
  147. 'tcp://[2001:0db8:85a3::]:80',
  148. [
  149. 'type' => SUT::ADDRESS_IPV6,
  150. 'transport' => 'tcp',
  151. 'address' => '2001:0db8:85a3::',
  152. 'port' => 80
  153. ]
  154. );
  155. }
  156. public function case_ipv6_disabled_by_STREAM_PF_INET6()
  157. {
  158. $this
  159. ->given(
  160. $this->function->defined = false,
  161. $this->function->function_exists = false
  162. )
  163. ->exception(function () {
  164. new SUT('tcp://[2001:0db8:85a3::]:80');
  165. })
  166. ->isInstanceOf('Hoa\Socket\Exception');
  167. }
  168. public function case_ipv6_disabled_by_AF_INET6()
  169. {
  170. $this
  171. ->given(
  172. $this->function->function_exists = true,
  173. $this->function->defined = function ($constantName) {
  174. return 'AF_INET6' !== $constantName;
  175. }
  176. )
  177. ->exception(function () {
  178. new SUT('tcp://[2001:0db8:85a3::]:80');
  179. })
  180. ->isInstanceOf('Hoa\Socket\Exception');
  181. }
  182. public function case_full_path()
  183. {
  184. return $this->_case_check(
  185. 'file:///Hoa/Socket',
  186. [
  187. 'type' => SUT::ADDRESS_PATH,
  188. 'transport' => 'file',
  189. 'address' => '/Hoa/Socket',
  190. 'port' => -1
  191. ]
  192. );
  193. }
  194. public function case_no_a_URI()
  195. {
  196. $this
  197. ->exception(function () {
  198. new SUT('foobar');
  199. })
  200. ->isInstanceOf('Hoa\Socket\Exception');
  201. }
  202. public function case_has_port()
  203. {
  204. $this
  205. ->when($result = new SUT('tcp://hoa-project.net:80'))
  206. ->then
  207. ->boolean($result->hasPort())
  208. ->isTrue();
  209. }
  210. public function case_has_no_port()
  211. {
  212. $this
  213. ->when($result = new SUT('tcp://hoa-project.net'))
  214. ->then
  215. ->boolean($result->hasPort())
  216. ->isFalse();
  217. }
  218. public function case_is_not_secured()
  219. {
  220. $this
  221. ->when($result = new SUT('tcp://hoa-project.net:80'))
  222. ->then
  223. ->boolean($result->isSecured())
  224. ->isFalse();
  225. }
  226. protected function _case_check($uri, $expect)
  227. {
  228. $this
  229. ->given(
  230. $this->constant->STREAM_PF_INET6 = true,
  231. $this->function->stream_get_transports = ['tcp', 'file']
  232. )
  233. ->when($result = new SUT($uri))
  234. ->then
  235. ->integer($result->getAddressType())
  236. ->isEqualTo($expect['type'])
  237. ->string($result->getTransport())
  238. ->isEqualTo($expect['transport'])
  239. ->string($result->getAddress())
  240. ->isEqualTo($expect['address'])
  241. ->integer($result->getPort())
  242. ->isEqualTo($expect['port']);
  243. }
  244. }