ToolsTest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Utils;
  4. use PHPUnit\Framework\TestCase;
  5. use function date_default_timezone_set;
  6. use function strlen;
  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. $size = -1024;
  29. $bytes = Tools::autoBytes($size);
  30. $this->assertIsString($bytes);
  31. $this->assertEquals('0B', $bytes);
  32. }
  33. /**
  34. * @covers App\Utils\Tools::autoBytesR
  35. */
  36. public function testAutoBytesR()
  37. {
  38. $size = '1KB';
  39. $bytes = Tools::autoBytesR($size);
  40. $this->assertIsInt($bytes);
  41. $this->assertEquals(1024, $bytes);
  42. $size = '1B';
  43. $bytes = Tools::autoBytesR($size);
  44. $this->assertIsInt($bytes);
  45. $this->assertEquals(1, $bytes);
  46. $size = 'Highly illogical.';
  47. $bytes = Tools::autoBytesR($size);
  48. $this->assertIsInt($bytes);
  49. $this->assertEquals(-1, $bytes);
  50. $size = '42069';
  51. $bytes = Tools::autoBytesR($size);
  52. $this->assertIsInt($bytes);
  53. $this->assertEquals(-1, $bytes);
  54. $size = '1000EB';
  55. $bytes = Tools::autoBytesR($size);
  56. $this->assertIsInt($bytes);
  57. $this->assertEquals(-1, $bytes);
  58. }
  59. /**
  60. * @covers App\Utils\Tools::autoMbps
  61. */
  62. public function testAutoMbps()
  63. {
  64. $bandwidth = 1;
  65. $mbps = Tools::autoMbps($bandwidth);
  66. $this->assertIsString($mbps);
  67. $this->assertEquals('1Mbps', $mbps);
  68. $bandwidth = -1;
  69. $mbps = Tools::autoMbps($bandwidth);
  70. $this->assertIsString($mbps);
  71. $this->assertEquals('0Bps', $mbps);
  72. $bandwidth = 1000000001;
  73. $mbps = Tools::autoMbps($bandwidth);
  74. $this->assertIsString($mbps);
  75. $this->assertEquals('∞', $mbps);
  76. }
  77. /**
  78. * @covers App\Utils\Tools::mbToB
  79. */
  80. public function testMbToB()
  81. {
  82. $traffic = 1;
  83. $mb = 1048576;
  84. $result = Tools::mbToB($traffic);
  85. $this->assertIsInt($result);
  86. $this->assertEquals($traffic * $mb, $result);
  87. $traffic = -1;
  88. $result = Tools::mbToB($traffic);
  89. $this->assertIsInt($result);
  90. $this->assertEquals(0, $result);
  91. }
  92. /**
  93. * @covers App\Utils\Tools::gbToB
  94. */
  95. public function testGbToB()
  96. {
  97. $traffic = 1;
  98. $gb = 1048576 * 1024;
  99. $result = Tools::gbToB($traffic);
  100. $this->assertIsInt($result);
  101. $this->assertEquals($traffic * $gb, $result);
  102. $traffic = -1;
  103. $result = Tools::gbToB($traffic);
  104. $this->assertIsInt($result);
  105. $this->assertEquals(0, $result);
  106. }
  107. /**
  108. * @covers App\Utils\Tools::bToGB
  109. */
  110. public function testBToGB()
  111. {
  112. $traffic = 1048576 * 1024;
  113. $gb = 1048576 * 1024;
  114. $result = Tools::bToGB($traffic);
  115. $this->assertIsFloat($result);
  116. $this->assertEquals($traffic / $gb, $result);
  117. $traffic = -1;
  118. $result = Tools::bToGB($traffic);
  119. $this->assertIsFloat($result);
  120. $this->assertEquals(0, $result);
  121. }
  122. /**
  123. * @covers App\Utils\Tools::bToMB
  124. */
  125. public function testBToMB()
  126. {
  127. $traffic = 1048576;
  128. $mb = 1048576;
  129. $result = Tools::bToMB($traffic);
  130. $this->assertIsFloat($result);
  131. $this->assertEquals($traffic / $mb, $result);
  132. $traffic = -1;
  133. $result = Tools::bToMB($traffic);
  134. $this->assertIsFloat($result);
  135. $this->assertEquals(0, $result);
  136. }
  137. /**
  138. * @covers App\Utils\Tools::genSubToken
  139. */
  140. public function testGenSubToken()
  141. {
  142. $_ENV['sub_token_len'] = 10;
  143. $token = Tools::genSubToken();
  144. $this->assertEquals(10, strlen($token));
  145. $_ENV['sub_token_len'] = 0;
  146. $token = Tools::genSubToken();
  147. $this->assertEquals(8, strlen($token));
  148. $_ENV['sub_token_len'] = -5;
  149. $token = Tools::genSubToken();
  150. $this->assertEquals(8, strlen($token));
  151. }
  152. /**
  153. * @covers App\Utils\Tools::genRandomChar
  154. */
  155. public function testGenRandomChar()
  156. {
  157. $randomString = Tools::genRandomChar();
  158. $this->assertIsString($randomString);
  159. $this->assertEquals(8, strlen($randomString));
  160. $length = 10;
  161. $randomString = Tools::genRandomChar($length);
  162. $this->assertIsString($randomString);
  163. $this->assertEquals($length, strlen($randomString));
  164. $length = 9;
  165. $randomString = Tools::genRandomChar($length);
  166. $this->assertIsString($randomString);
  167. $this->assertEquals($length, strlen($randomString));
  168. $length = 1;
  169. $randomString = Tools::genRandomChar($length);
  170. $this->assertIsString($randomString);
  171. $this->assertEquals(2, strlen($randomString));
  172. }
  173. /**
  174. * @covers App\Utils\Tools::genSs2022UserPk
  175. */
  176. public function testGenSs2022UserPk()
  177. {
  178. $passwd = 'password';
  179. $method = '2022-blake3-aes-128-gcm';
  180. $pk = Tools::genSs2022UserPk($passwd, $method);
  181. $this->assertIsString($pk);
  182. $this->assertEquals('YzAwNjdkNGFmNGU4N2YwMA==', $pk);
  183. $method = '2022-blake3-aes-256-gcm';
  184. $pk = Tools::genSs2022UserPk($passwd, $method);
  185. $this->assertIsString($pk);
  186. $this->assertEquals('YzAwNjdkNGFmNGU4N2YwMGRiYWM2M2I2MTU2ODI4MjM=', $pk);
  187. $method = 'bomb_three_gorges_dam';
  188. $pk = Tools::genSs2022UserPk($passwd, $method);
  189. $this->assertFalse($pk);
  190. }
  191. /**
  192. * @covers App\Utils\Tools::toDateTime
  193. */
  194. public function testToDateTime()
  195. {
  196. date_default_timezone_set('ROC'); // Use Asia/Shanghai or PRC will cause this test to fail
  197. $time = 612907200; // 1989-06-04 04:00:00 UTC+8
  198. $result = Tools::toDateTime($time);
  199. $this->assertIsString($result);
  200. $this->assertEquals('1989-06-04 04:00:00', $result);
  201. $time = -1830412800; // 1912-01-01 00:00:00 UTC+8
  202. $result = Tools::toDateTime($time);
  203. $this->assertIsString($result);
  204. $this->assertEquals('1912-01-01 00:00:00', $result);
  205. }
  206. /**
  207. * @covers App\Utils\Tools::getDir
  208. */
  209. public function testGetDir()
  210. {
  211. // Scenario 1: Valid directory
  212. $dir1 = 'tests/testDir';
  213. $expected1 = ['emptyDir', 'file1', 'file2', 'file3']; // Replace with actual expected result
  214. $result1 = Tools::getDir($dir1);
  215. $this->assertEqualsCanonicalizing($expected1, $result1);
  216. // Scenario 2: Directory with .gitkeep
  217. $dir2 = 'tests/testDir/emptyDir';
  218. $result2 = Tools::getDir($dir2);
  219. $this->assertEqualsCanonicalizing(['.gitkeep'], $result2);
  220. }
  221. /**
  222. * @covers App\Utils\Tools::isParamValidate
  223. */
  224. public function testIsParamValidate()
  225. {
  226. $this->assertTrue(Tools::isParamValidate('default', 'aes-128-gcm'));
  227. $this->assertFalse(Tools::isParamValidate('default', 'rc4-md5'));
  228. }
  229. /**
  230. * @covers App\Utils\Tools::getSsMethod
  231. */
  232. public function testGetSsMethod()
  233. {
  234. // Scenario 1: ss_obfs
  235. $expected1 = [
  236. 'simple_obfs_http',
  237. 'simple_obfs_http_compatible',
  238. 'simple_obfs_tls',
  239. 'simple_obfs_tls_compatible',
  240. ];
  241. $result1 = Tools::getSsMethod('ss_obfs');
  242. $this->assertEquals($expected1, $result1);
  243. // Scenario 2: default
  244. $expected2 = [
  245. 'aes-128-gcm',
  246. 'aes-192-gcm',
  247. 'aes-256-gcm',
  248. 'chacha20-ietf-poly1305',
  249. 'xchacha20-ietf-poly1305',
  250. ];
  251. $result2 = Tools::getSsMethod('default');
  252. $this->assertEquals($expected2, $result2);
  253. // Scenario 3: Random string
  254. $expected3 = [
  255. 'aes-128-gcm',
  256. 'aes-192-gcm',
  257. 'aes-256-gcm',
  258. 'chacha20-ietf-poly1305',
  259. 'xchacha20-ietf-poly1305',
  260. ];
  261. $result3 = Tools::getSsMethod('randomString');
  262. $this->assertEquals($expected3, $result3);
  263. // Scenario 4: Empty string
  264. $expected4 = [
  265. 'aes-128-gcm',
  266. 'aes-192-gcm',
  267. 'aes-256-gcm',
  268. 'chacha20-ietf-poly1305',
  269. 'xchacha20-ietf-poly1305',
  270. ];
  271. $result4 = Tools::getSsMethod();
  272. $this->assertEquals($expected4, $result4);
  273. }
  274. /**
  275. * @covers App\Utils\Tools::isEmail
  276. */
  277. public function testIsEmail()
  278. {
  279. $this->assertTrue(Tools::isEmail('[email protected]'));
  280. $this->assertFalse(Tools::isEmail('test@example'));
  281. }
  282. /**
  283. * @covers App\Utils\Tools::isIPv4
  284. */
  285. public function testIsIPv4()
  286. {
  287. $this->assertTrue(Tools::isIPv4('192.168.0.1'));
  288. $this->assertFalse(Tools::isIPv4('2001:0db8:85a3:0000:0000:8a2e:0370:7334'));
  289. $this->assertFalse(Tools::isIPv4('UwU'));
  290. }
  291. /**
  292. * @covers App\Utils\Tools::isIPv6
  293. */
  294. public function testIsIPv6()
  295. {
  296. $this->assertTrue(Tools::isIPv6('2001:0db8:85a3:0000:0000:8a2e:0370:7334'));
  297. $this->assertFalse(Tools::isIPv6('192.168.0.1'));
  298. $this->assertFalse(Tools::isIPv6('hmm'));
  299. }
  300. /**
  301. * @covers App\Utils\Tools::isInt
  302. */
  303. public function testIsInt()
  304. {
  305. $this->assertTrue(Tools::isInt(123));
  306. $this->assertFalse(Tools::isInt('abc'));
  307. }
  308. /**
  309. * @covers App\Utils\Tools::isJson
  310. */
  311. public function testIsJson()
  312. {
  313. $this->assertTrue(Tools::isJson('{}'));
  314. $this->assertFalse(Tools::isJson('[]'));
  315. $this->assertFalse(Tools::isJson('what the'));
  316. }
  317. }