IpRiskServiceTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. namespace Tests\Unit;
  3. use App\Services\IpRiskService;
  4. use Tests\TestCase;
  5. /**
  6. * IpRiskService 单元测试
  7. *
  8. * 测试 IP 风险检测逻辑:
  9. * - 云厂商 ASN 关键字命中
  10. * - 非美国 IP 命中
  11. * - 本地/私有 IP 跳过
  12. * - API 失败时的兜底行为
  13. */
  14. class IpRiskServiceTest extends TestCase
  15. {
  16. // ==================== 云厂商 ASN 检测 ====================
  17. /** @test */
  18. public function test_aws_ip_as_risky()
  19. {
  20. $service = $this->createPartialMock(IpRiskService::class, ['queryIpInfo']);
  21. $service->method('queryIpInfo')->willReturn([
  22. 'countryCode' => 'US',
  23. 'as' => 'AS16509 Amazon.com, Inc.',
  24. 'org' => 'Amazon Web Services',
  25. 'isp' => 'Amazon.com, Inc.',
  26. ]);
  27. $result = $service->detect('1.2.3.4');
  28. $this->assertTrue($result['is_risky']);
  29. $this->assertStringContainsString('cloud_asn:', $result['reason']);
  30. $this->assertStringContainsString('amazon', $result['reason']);
  31. }
  32. /** @test */
  33. public function test_google_cloud_ip_as_risky()
  34. {
  35. $service = $this->createPartialMock(IpRiskService::class, ['queryIpInfo']);
  36. $service->method('queryIpInfo')->willReturn([
  37. 'countryCode' => 'US',
  38. 'as' => 'AS15169 Google LLC',
  39. 'org' => 'Google Cloud Platform',
  40. 'isp' => 'Google LLC',
  41. ]);
  42. $result = $service->detect('8.8.8.8');
  43. $this->assertTrue($result['is_risky']);
  44. $this->assertStringContainsString('cloud_asn:', $result['reason']);
  45. }
  46. /** @test */
  47. public function test_azure_ip_as_risky()
  48. {
  49. $service = $this->createPartialMock(IpRiskService::class, ['queryIpInfo']);
  50. $service->method('queryIpInfo')->willReturn([
  51. 'countryCode' => 'US',
  52. 'as' => 'AS8075 Microsoft Corporation',
  53. 'org' => 'Microsoft Azure',
  54. 'isp' => 'Microsoft Corporation',
  55. ]);
  56. $result = $service->detect('40.76.4.15');
  57. $this->assertTrue($result['is_risky']);
  58. $this->assertStringContainsString('microsoft', $result['reason']);
  59. }
  60. /** @test */
  61. public function test_digitalocean_ip_as_risky()
  62. {
  63. $service = $this->createPartialMock(IpRiskService::class, ['queryIpInfo']);
  64. $service->method('queryIpInfo')->willReturn([
  65. 'countryCode' => 'US',
  66. 'as' => 'AS14061 DigitalOcean, LLC',
  67. 'org' => 'DigitalOcean, LLC',
  68. 'isp' => 'DigitalOcean, LLC',
  69. ]);
  70. $result = $service->detect('167.71.0.1');
  71. $this->assertTrue($result['is_risky']);
  72. $this->assertStringContainsString('digitalocean', $result['reason']);
  73. }
  74. // ==================== 非美国 IP 检测 ====================
  75. /** @test */
  76. public function test_china_ip_as_risky()
  77. {
  78. $service = $this->createPartialMock(IpRiskService::class, ['queryIpInfo']);
  79. $service->method('queryIpInfo')->willReturn([
  80. 'countryCode' => 'CN',
  81. 'as' => 'AS4134 CHINANET',
  82. 'org' => 'Chinanet',
  83. 'isp' => 'Chinanet',
  84. ]);
  85. $result = $service->detect('114.114.114.114');
  86. $this->assertTrue($result['is_risky']);
  87. $this->assertStringContainsString('non_us:CN', $result['reason']);
  88. }
  89. /** @test */
  90. public function test_brazil_ip_as_risky()
  91. {
  92. $service = $this->createPartialMock(IpRiskService::class, ['queryIpInfo']);
  93. $service->method('queryIpInfo')->willReturn([
  94. 'countryCode' => 'BR',
  95. 'as' => 'AS7738 Vivo',
  96. 'org' => 'Telefonica Brasil',
  97. 'isp' => 'Vivo',
  98. ]);
  99. $result = $service->detect('200.200.0.1');
  100. $this->assertTrue($result['is_risky']);
  101. $this->assertStringContainsString('non_us:BR', $result['reason']);
  102. }
  103. // ==================== 正常 IP ====================
  104. /** @test */
  105. public function test_normal_us_residential_ip_not_flagged()
  106. {
  107. $service = $this->createPartialMock(IpRiskService::class, ['queryIpInfo']);
  108. $service->method('queryIpInfo')->willReturn([
  109. 'countryCode' => 'US',
  110. 'as' => 'AS7922 Comcast Cable',
  111. 'org' => 'Comcast Cable Communications',
  112. 'isp' => 'Comcast Cable',
  113. ]);
  114. $result = $service->detect('73.162.0.1');
  115. $this->assertFalse($result['is_risky']);
  116. $this->assertEmpty($result['reason']);
  117. }
  118. // ==================== 边界值 ====================
  119. /** @test */
  120. public function test_skips_empty_ip()
  121. {
  122. $service = new IpRiskService();
  123. $result = $service->detect('');
  124. $this->assertFalse($result['is_risky']);
  125. $this->assertEmpty($result['reason']);
  126. }
  127. /** @test */
  128. public function test_skips_localhost_ip()
  129. {
  130. $service = new IpRiskService();
  131. $result = $service->detect('127.0.0.1');
  132. $this->assertFalse($result['is_risky']);
  133. $this->assertEmpty($result['reason']);
  134. }
  135. /** @test */
  136. public function test_skips_ipv6_localhost()
  137. {
  138. $service = new IpRiskService();
  139. $result = $service->detect('::1');
  140. $this->assertFalse($result['is_risky']);
  141. $this->assertEmpty($result['reason']);
  142. }
  143. /** @test */
  144. public function test_skips_private_ip_range_10()
  145. {
  146. $service = new IpRiskService();
  147. $result = $service->detect('10.0.0.1');
  148. $this->assertFalse($result['is_risky']);
  149. $this->assertEmpty($result['reason']);
  150. }
  151. /** @test */
  152. public function test_skips_private_ip_range_192_168()
  153. {
  154. $service = new IpRiskService();
  155. $result = $service->detect('192.168.1.1');
  156. $this->assertFalse($result['is_risky']);
  157. $this->assertEmpty($result['reason']);
  158. }
  159. /** @test */
  160. public function test_safe_when_api_query_fails()
  161. {
  162. $service = $this->createPartialMock(IpRiskService::class, ['queryIpInfo']);
  163. $service->method('queryIpInfo')->willReturn(null);
  164. $result = $service->detect('8.8.8.8');
  165. $this->assertFalse($result['is_risky']);
  166. $this->assertEmpty($result['reason']);
  167. }
  168. /** @test */
  169. public function test_safe_when_api_returns_fail_status()
  170. {
  171. $service = $this->createPartialMock(IpRiskService::class, ['queryIpInfo']);
  172. $service->method('queryIpInfo')->willReturn([
  173. 'status' => 'fail',
  174. 'message' => 'invalid query',
  175. ]);
  176. $result = $service->detect('999.999.999.999');
  177. $this->assertFalse($result['is_risky']);
  178. $this->assertEmpty($result['reason']);
  179. }
  180. /** @test */
  181. public function test_cloud_asn_priority_over_non_us()
  182. {
  183. $service = $this->createPartialMock(IpRiskService::class, ['queryIpInfo']);
  184. $service->method('queryIpInfo')->willReturn([
  185. 'countryCode' => 'US',
  186. 'as' => 'AS16509 Amazon.com, Inc.',
  187. 'org' => 'Amazon Web Services',
  188. 'isp' => 'Amazon.com, Inc.',
  189. ]);
  190. $result = $service->detect('52.95.0.1');
  191. $this->assertTrue($result['is_risky']);
  192. $this->assertStringContainsString('cloud_asn', $result['reason']);
  193. }
  194. }