ToolsTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Utils;
  4. use MaxMind\Db\Reader\InvalidDatabaseException;
  5. use PHPUnit\Framework\TestCase;
  6. use function date_default_timezone_set;
  7. class ToolsTest extends TestCase
  8. {
  9. /**
  10. * @covers App\Utils\Tools::getIpLocation
  11. */
  12. public function testGetIpLocation()
  13. {
  14. $_ENV['maxmind_license_key'] = '';
  15. $msg = Tools::getIpLocation('8.8.8.8');
  16. $this->assertIsString($msg);
  17. $this->assertEquals('GeoIP2 服务未配置', $msg);
  18. }
  19. /**
  20. * @covers App\Utils\Tools::autoBytes
  21. */
  22. public function testAutoBytes()
  23. {
  24. $size = 1024;
  25. $bytes = Tools::autoBytes($size);
  26. $this->assertIsString($bytes);
  27. $this->assertEquals('1KB', $bytes);
  28. }
  29. /**
  30. * @covers App\Utils\Tools::autoBytesR
  31. */
  32. public function testAutoBytesR()
  33. {
  34. $size = '1KB';
  35. $bytes = Tools::autoBytesR($size);
  36. $this->assertIsInt($bytes);
  37. $this->assertEquals(1024, $bytes);
  38. }
  39. /**
  40. * @covers App\Utils\Tools::autoMbps
  41. */
  42. public function testAutoMbps()
  43. {
  44. $bandwidth = 1;
  45. $mbps = Tools::autoMbps($bandwidth);
  46. $this->assertIsString($mbps);
  47. $this->assertEquals('1Mbps', $mbps);
  48. }
  49. /**
  50. * @covers App\Utils\Tools::toMB
  51. */
  52. public function testToMB()
  53. {
  54. $traffic = 1;
  55. $mb = 1048576;
  56. $result = Tools::toMB($traffic);
  57. $this->assertIsInt($result);
  58. $this->assertEquals($traffic * $mb, $result);
  59. }
  60. /**
  61. * @covers App\Utils\Tools::toGB
  62. */
  63. public function testToGB()
  64. {
  65. $traffic = 1;
  66. $gb = 1048576 * 1024;
  67. $result = Tools::toGB($traffic);
  68. $this->assertIsInt($result);
  69. $this->assertEquals($traffic * $gb, $result);
  70. }
  71. /**
  72. * @covers App\Utils\Tools::flowToGB
  73. */
  74. public function testFlowToGB()
  75. {
  76. $traffic = 1048576 * 1024;
  77. $gb = 1048576 * 1024;
  78. $result = Tools::flowToGB($traffic);
  79. $this->assertIsFloat($result);
  80. $this->assertEquals($traffic / $gb, $result);
  81. }
  82. /**
  83. * @covers App\Utils\Tools::flowToMB
  84. */
  85. public function testFlowToMB()
  86. {
  87. $traffic = 1048576;
  88. $mb = 1048576;
  89. $result = Tools::flowToMB($traffic);
  90. $this->assertIsFloat($result);
  91. $this->assertEquals($traffic / $mb, $result);
  92. }
  93. /**
  94. * @covers App\Utils\Tools::genRandomChar
  95. */
  96. public function testGenRandomChar()
  97. {
  98. $length = 10;
  99. $randomString = Tools::genRandomChar($length);
  100. $this->assertIsString($randomString);
  101. $this->assertEquals($length, strlen($randomString));
  102. }
  103. /**
  104. * @covers App\Utils\Tools::genSs2022UserPk
  105. */
  106. public function testGenSs2022UserPk()
  107. {
  108. $passwd = 'password';
  109. $method = '2022-blake3-aes-128-gcm';
  110. $pk = Tools::genSs2022UserPk($passwd, $method);
  111. $this->assertIsString($pk);
  112. $this->assertEquals('YzAwNjdkNGFmNGU4N2YwMA==', $pk);
  113. $method = '2022-blake3-aes-256-gcm';
  114. $pk = Tools::genSs2022UserPk($passwd, $method);
  115. $this->assertIsString($pk);
  116. $this->assertEquals('YzAwNjdkNGFmNGU4N2YwMGRiYWM2M2I2MTU2ODI4MjM=', $pk);
  117. $method = 'bomb_three_gorges_dam';
  118. $pk = Tools::genSs2022UserPk($passwd, $method);
  119. $this->assertFalse($pk);
  120. }
  121. /**
  122. * @covers App\Utils\Tools::toDateTime
  123. */
  124. public function testToDateTime()
  125. {
  126. date_default_timezone_set('ROC'); // Use Asia/Shanghai or PRC will cause this test to fail
  127. $time = 612907200; // 1989-06-04 04:00:00 UTC+8
  128. $expected = '1989-06-04 04:00:00';
  129. $result = Tools::toDateTime($time);
  130. $this->assertIsString($result);
  131. $this->assertEquals($expected, $result);
  132. }
  133. /**
  134. * @covers App\Utils\Tools::getDir
  135. */
  136. public function testGetDir()
  137. {
  138. // Scenario 1: Valid directory
  139. $dir1 = 'tests/testDir';
  140. $expected1 = ['emptyDir', 'file1', 'file2', 'file3']; // Replace with actual expected result
  141. $result1 = Tools::getDir($dir1);
  142. $this->assertEqualsCanonicalizing($expected1, $result1);
  143. // Scenario 2: Directory with .gitkeep
  144. $dir2 = 'tests/testDir/emptyDir';
  145. $result2 = Tools::getDir($dir2);
  146. $this->assertEqualsCanonicalizing(['.gitkeep'], $result2);
  147. }
  148. /**
  149. * @covers App\Utils\Tools::isParamValidate
  150. */
  151. public function testIsParamValidate()
  152. {
  153. $this->assertTrue(Tools::isParamValidate('default', 'aes-128-gcm'));
  154. $this->assertFalse(Tools::isParamValidate('default', 'rc4-md5'));
  155. }
  156. /**
  157. * @covers App\Utils\Tools::getSsMethod
  158. */
  159. public function testGetSsMethod()
  160. {
  161. // Scenario 1: ss_obfs
  162. $expected1 = [
  163. 'simple_obfs_http',
  164. 'simple_obfs_http_compatible',
  165. 'simple_obfs_tls',
  166. 'simple_obfs_tls_compatible',
  167. ];
  168. $result1 = Tools::getSsMethod('ss_obfs');
  169. $this->assertEquals($expected1, $result1);
  170. // Scenario 2: default
  171. $expected2 = [
  172. 'aes-128-gcm',
  173. 'aes-192-gcm',
  174. 'aes-256-gcm',
  175. 'chacha20-ietf-poly1305',
  176. 'xchacha20-ietf-poly1305',
  177. ];
  178. $result2 = Tools::getSsMethod('default');
  179. $this->assertEquals($expected2, $result2);
  180. // Scenario 3: Random string
  181. $expected3 = [
  182. 'aes-128-gcm',
  183. 'aes-192-gcm',
  184. 'aes-256-gcm',
  185. 'chacha20-ietf-poly1305',
  186. 'xchacha20-ietf-poly1305',
  187. ];
  188. $result3 = Tools::getSsMethod('randomString');
  189. $this->assertEquals($expected3, $result3);
  190. // Scenario 4: Empty string
  191. $expected4 = [
  192. 'aes-128-gcm',
  193. 'aes-192-gcm',
  194. 'aes-256-gcm',
  195. 'chacha20-ietf-poly1305',
  196. 'xchacha20-ietf-poly1305',
  197. ];
  198. $result4 = Tools::getSsMethod('');
  199. $this->assertEquals($expected4, $result4);
  200. }
  201. /**
  202. * @covers App\Utils\Tools::isEmail
  203. */
  204. public function testIsEmail()
  205. {
  206. $this->assertTrue(Tools::isEmail('[email protected]'));
  207. $this->assertFalse(Tools::isEmail('test@example'));
  208. }
  209. /**
  210. * @covers App\Utils\Tools::isIPv4
  211. */
  212. public function testIsIPv4()
  213. {
  214. $this->assertTrue(Tools::isIPv4('192.168.0.1'));
  215. $this->assertFalse(Tools::isIPv4('2001:0db8:85a3:0000:0000:8a2e:0370:7334'));
  216. }
  217. /**
  218. * @covers App\Utils\Tools::isIPv6
  219. */
  220. public function testIsIPv6()
  221. {
  222. $this->assertTrue(Tools::isIPv6('2001:0db8:85a3:0000:0000:8a2e:0370:7334'));
  223. $this->assertFalse(Tools::isIPv6('192.168.0.1'));
  224. }
  225. /**
  226. * @covers App\Utils\Tools::isInt
  227. */
  228. public function testIsInt()
  229. {
  230. $this->assertTrue(Tools::isInt(123));
  231. $this->assertFalse(Tools::isInt('abc'));
  232. }
  233. /**
  234. * @covers App\Utils\Tools::isJson
  235. */
  236. public function testIsJson()
  237. {
  238. $this->assertTrue(Tools::isJson('{}'));
  239. $this->assertFalse(Tools::isJson('[]'));
  240. $this->assertFalse(Tools::isJson('what the'));
  241. }
  242. }