done
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
package com.example.netshareecohub.routing
|
||||
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class FakeShellExecutor(
|
||||
var rootAvailable: Boolean = true,
|
||||
var shouldFailCommands: Boolean = false
|
||||
) : ShellExecutor {
|
||||
val executedCommands = mutableListOf<String>()
|
||||
|
||||
override fun isRootAvailable(): Boolean = rootAvailable
|
||||
|
||||
override fun executeSu(command: String): CommandResult {
|
||||
return executeSu(listOf(command))
|
||||
}
|
||||
|
||||
override fun executeSu(commands: List<String>): CommandResult {
|
||||
executedCommands.addAll(commands)
|
||||
return if (shouldFailCommands) {
|
||||
CommandResult(1, "", "Permission denied or command failed")
|
||||
} else {
|
||||
CommandResult(0, "uid=0(root)", "")
|
||||
}
|
||||
}
|
||||
|
||||
override fun executeSh(command: String): CommandResult {
|
||||
executedCommands.add(command)
|
||||
return CommandResult(0, "sh success", "")
|
||||
}
|
||||
}
|
||||
|
||||
class RootRoutingManagerTest {
|
||||
|
||||
@Test
|
||||
fun testStartRoutingSuccess() {
|
||||
val fakeShell = FakeShellExecutor(rootAvailable = true)
|
||||
val rootManager = RootRoutingManager(fakeShell)
|
||||
|
||||
val success = rootManager.startRouting(
|
||||
lanInterface = "eth0",
|
||||
upstreamInterface = "wlan0",
|
||||
dnsServer = "1.1.1.1"
|
||||
)
|
||||
|
||||
assertTrue(success)
|
||||
assertTrue(rootManager.isRoutingActive())
|
||||
|
||||
val commands = fakeShell.executedCommands
|
||||
assertTrue(commands.any { it.contains("sysctl") || it.contains("ip_forward") })
|
||||
assertTrue(commands.any { it.contains("POSTROUTING -o wlan0 -j MASQUERADE") })
|
||||
assertTrue(commands.any { it.contains("FORWARD -i eth0 -o wlan0 -j ACCEPT") })
|
||||
assertTrue(commands.any { it.contains("FORWARD -i wlan0 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT") })
|
||||
assertTrue(commands.any { it.contains("PREROUTING -i eth0 -p udp --dport 53 -j DNAT --to-destination 1.1.1.1:53") })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testStartRoutingFailsWithoutRoot() {
|
||||
val fakeShell = FakeShellExecutor(rootAvailable = false)
|
||||
val rootManager = RootRoutingManager(fakeShell)
|
||||
|
||||
val success = rootManager.startRouting("eth0", "wlan0")
|
||||
|
||||
assertFalse(success)
|
||||
assertFalse(rootManager.isRoutingActive())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testStopRoutingCleansUpRules() {
|
||||
val fakeShell = FakeShellExecutor(rootAvailable = true)
|
||||
val rootManager = RootRoutingManager(fakeShell)
|
||||
|
||||
rootManager.startRouting("eth0", "wlan0", "8.8.8.8")
|
||||
fakeShell.executedCommands.clear()
|
||||
|
||||
val stopped = rootManager.stopRouting("eth0", "wlan0", "8.8.8.8")
|
||||
|
||||
assertTrue(stopped)
|
||||
assertFalse(rootManager.isRoutingActive())
|
||||
|
||||
val commands = fakeShell.executedCommands
|
||||
assertTrue(commands.any { it.contains("POSTROUTING -o wlan0 -j MASQUERADE") && it.contains("-D") })
|
||||
assertTrue(commands.any { it.contains("FORWARD -i eth0 -o wlan0 -j ACCEPT") && it.contains("-D") })
|
||||
assertTrue(commands.any { it.contains("PREROUTING -i eth0 -p udp --dport 53 -j DNAT --to-destination 8.8.8.8:53") && it.contains("-D") })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user