|
| 1 | +package com.masterdns.vpn |
| 2 | + |
| 3 | +import com.masterdns.vpn.util.ResolverAnalyzer |
| 4 | +import org.junit.Assert.assertEquals |
| 5 | +import org.junit.Assert.assertNotNull |
| 6 | +import org.junit.Assert.assertNull |
| 7 | +import org.junit.Assert.assertTrue |
| 8 | +import org.junit.Test |
| 9 | + |
| 10 | +class ResolverAnalyzerTest { |
| 11 | + private val analyzer = ResolverAnalyzer |
| 12 | + |
| 13 | + @Test |
| 14 | + fun parseEntry_ipv4DefaultPort() { |
| 15 | + val e = analyzer.parseEntry("1.2.3.4") |
| 16 | + assertNotNull(e) |
| 17 | + assertEquals("1.2.3.4", e!!.host) |
| 18 | + assertEquals(53, e.port) |
| 19 | + assertEquals(false, e.hasExplicitPort) |
| 20 | + } |
| 21 | + |
| 22 | + @Test |
| 23 | + fun parseEntry_ipv4ExplicitPort() { |
| 24 | + val e = analyzer.parseEntry("1.2.3.4:5353") |
| 25 | + assertNotNull(e) |
| 26 | + assertEquals("1.2.3.4", e!!.host) |
| 27 | + assertEquals(5353, e.port) |
| 28 | + assertEquals(true, e.hasExplicitPort) |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + fun parseEntry_ipv6BracketedWithPort() { |
| 33 | + val e = analyzer.parseEntry("[::1]:53") |
| 34 | + assertNotNull(e) |
| 35 | + assertEquals("::1", e!!.host) |
| 36 | + assertEquals(53, e.port) |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + fun parseEntry_blankReturnsNull() { |
| 41 | + assertNull(analyzer.parseEntry("")) |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + fun parseEntry_portOutOfRangeFallsThroughToDefaultHostString() { |
| 46 | + // Characterization of ACTUAL behavior: parseEntry does not reject |
| 47 | + // out-of-range ports as null. For "1.2.3.4:65536" the canHavePort |
| 48 | + // branch fails (65536 not in 1..65535) and execution falls through to |
| 49 | + // ResolverEntry(text, DEFAULT_PORT, false) — i.e. the whole "1.2.3.4:65536" |
| 50 | + // string becomes the host with default port 53 and hasExplicitPort=false. |
| 51 | + // This is the behavior plan 008 (TOML validation) will tighten; lock it in. |
| 52 | + val e = analyzer.parseEntry("1.2.3.4:65536") |
| 53 | + assertNotNull("parseEntry returns non-null for out-of-range port — fall-through to default entry", e) |
| 54 | + assertEquals("1.2.3.4:65536", e!!.host) |
| 55 | + assertEquals(53, e.port) |
| 56 | + assertEquals(false, e.hasExplicitPort) |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + fun expandCidr_v4Slash30Yields2UsableHosts() { |
| 61 | + val hosts = analyzer.expandCidr("192.168.1.0/30") ?: return |
| 62 | + // /30 -> hostBits=2 -> total=4. For IPv4 prefixBits<31, usableStart=1, |
| 63 | + // usableEndExclusive=total-1=3 -> offsets 1..2 -> 192.168.1.1 and .2. |
| 64 | + assertEquals(2, hosts.size) |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + fun expandCidr_hostBitsGT16ReturnsEmpty() { |
| 69 | + // hostBits>16 hits the `if (hostBits > 16) return emptyList()` guard |
| 70 | + // in ResolverAnalyzer.kt:220. For IPv4 /8: totalBits=32, prefixBits=8, |
| 71 | + // hostBits=24 (>16) -> empty. |
| 72 | + val hosts = analyzer.expandCidr("10.0.0.0/8") |
| 73 | + assertTrue("expected empty (hostBits=24 > 16 guard)", hosts?.isEmpty() == true) |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + fun parseIp_normalizes_ipv4() { |
| 78 | + assertEquals("1.2.3.4", analyzer.parseIp("1.2.3.4")) |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + fun parseIp_rejectsNonNumeric() { |
| 83 | + assertNull(analyzer.parseIp("example.com")) |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + fun analyzeAndNormalize_truncatedLargeInput() { |
| 88 | + // MAX_IMPORT_BYTES = 2*1024*1024 = 2097152. Each "1.1.1.1\n" is 8 UTF-8 |
| 89 | + // bytes (incl. the trailing newline). Use 300_000 repetitions -> |
| 90 | + // 300_000 * 8 = 2_400_000 bytes > 2_097_152 -> early-return with |
| 91 | + // truncated=true and empty normalizedText. |
| 92 | + val big = "1.1.1.1\n".repeat(300_000) |
| 93 | + val result = analyzer.analyzeAndNormalize(big, "big.txt") |
| 94 | + assertNotNull(result) |
| 95 | + assertEquals(true, result!!.stats.truncated) |
| 96 | + } |
| 97 | +} |
0 commit comments